Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(macrod): poll for updates and auto-update via execve handoff (#8986)
Wolf Mermelstein6c99756a2b18
diff --git a/.github/workflows/build_agent_daemon_on_tag.yml b/.github/workflows/build_agent_daemon_on_tag.ymlindex 75a09c2901..acaa181cb6 100644--- a/.github/workflows/build_agent_daemon_on_tag.yml+++ b/.github/workflows/build_agent_daemon_on_tag.yml@@ -164,6 +164,7 @@ jobs:shell: bashenv:TARGET: x86_64-unknown-linux-musl+ MACROD_RELEASE_TAG: ${{ steps.metadata.outputs.tag }}- name: Package daemonrun: |set -euo pipefail@@ -236,6 +237,18 @@ jobs:echo "skipping smoke run: $TARGET does not execute on this runner"fi+ # A release binary carries its tag: the self-updater compares it with the+ # latest release, so a binary built without it (or served a stale tag from a+ # compiler cache) would never update, or would update forever.+ case "$RELEASE_TAG" in+ v*)+ if ! grep -aqF "$RELEASE_TAG" "$binary"; then+ echo "$binary does not carry its release tag $RELEASE_TAG" >&2+ exit 1+ fi+ ;;+ esac+mkdir -p artifactsstage=$(mktemp -d)install -m 755 "$binary" "$stage/$executable"@@ -273,6 +286,7 @@ jobs:TARGET: x86_64-unknown-linux-muslSLUG: linux-x86_64SAFE_TAG: ${{ steps.metadata.outputs.safe_tag }}+ RELEASE_TAG: ${{ steps.metadata.outputs.tag }}SMOKE_RUN: '1'- name: Upload macrod-linux-x86_64-${{ steps.metadata.outputs.safe_tag }} artifactuses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02@@ -379,6 +393,7 @@ jobs:shell: bashenv:TARGET: aarch64-unknown-linux-musl+ MACROD_RELEASE_TAG: ${{ steps.metadata.outputs.tag }}- name: Package daemonrun: |set -euo pipefail@@ -451,6 +466,18 @@ jobs:echo "skipping smoke run: $TARGET does not execute on this runner"fi+ # A release binary carries its tag: the self-updater compares it with the+ # latest release, so a binary built without it (or served a stale tag from a+ # compiler cache) would never update, or would update forever.+ case "$RELEASE_TAG" in+ v*)+ if ! grep -aqF "$RELEASE_TAG" "$binary"; then+ echo "$binary does not carry its release tag $RELEASE_TAG" >&2+ exit 1+ fi+ ;;+ esac+mkdir -p artifactsstage=$(mktemp -d)install -m 755 "$binary" "$stage/$executable"@@ -488,6 +515,7 @@ jobs:TARGET: aarch64-unknown-linux-muslSLUG: linux-aarch64SAFE_TAG: ${{ steps.metadata.outputs.safe_tag }}+ RELEASE_TAG: ${{ steps.metadata.outputs.tag }}SMOKE_RUN: '0'- name: Upload macrod-linux-aarch64-${{ steps.metadata.outputs.safe_tag }} artifactuses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02@@ -554,6 +582,7 @@ jobs:shell: bashenv:TARGET: aarch64-apple-darwin+ MACROD_RELEASE_TAG: ${{ steps.metadata.outputs.tag }}- name: Package daemonrun: |set -euo pipefail@@ -626,6 +655,18 @@ jobs:echo "skipping smoke run: $TARGET does not execute on this runner"fi+ # A release binary carries its tag: the self-updater compares it with the+ # latest release, so a binary built without it (or served a stale tag from a+ # compiler cache) would never update, or would update forever.+ case "$RELEASE_TAG" in+ v*)+ if ! grep -aqF "$RELEASE_TAG" "$binary"; then+ echo "$binary does not carry its release tag $RELEASE_TAG" >&2+ exit 1+ fi+ ;;+ esac+mkdir -p artifactsstage=$(mktemp -d)install -m 755 "$binary" "$stage/$executable"@@ -663,6 +704,7 @@ jobs:TARGET: aarch64-apple-darwinSLUG: macos-aarch64SAFE_TAG: ${{ steps.metadata.outputs.safe_tag }}+ RELEASE_TAG: ${{ steps.metadata.outputs.tag }}SMOKE_RUN: '1'- name: Upload macrod-macos-aarch64-${{ steps.metadata.outputs.safe_tag }} artifactuses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02@@ -726,6 +768,7 @@ jobs:shell: bashenv:TARGET: x86_64-apple-darwin+ MACROD_RELEASE_TAG: ${{ steps.metadata.outputs.tag }}- name: Package daemonrun: |set -euo pipefail@@ -798,6 +841,18 @@ jobs:echo "skipping smoke run: $TARGET does not execute on this runner"fi+ # A release binary carries its tag: the self-updater compares it with the+ # latest release, so a binary built without it (or served a stale tag from a+ # compiler cache) would never update, or would update forever.+ case "$RELEASE_TAG" in+ v*)+ if ! grep -aqF "$RELEASE_TAG" "$binary"; then+ echo "$binary does not carry its release tag $RELEASE_TAG" >&2+ exit 1+ fi+ ;;+ esac+mkdir -p artifactsstage=$(mktemp -d)install -m 755 "$binary" "$stage/$executable"@@ -835,6 +890,7 @@ jobs:TARGET: x86_64-apple-darwinSLUG: macos-x86_64SAFE_TAG: ${{ steps.metadata.outputs.safe_tag }}+ RELEASE_TAG: ${{ steps.metadata.outputs.tag }}SMOKE_RUN: '0'- name: Upload macrod-macos-x86_64-${{ steps.metadata.outputs.safe_tag }} artifactuses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02@@ -904,6 +960,7 @@ jobs:shell: bashenv:TARGET: x86_64-pc-windows-msvc+ MACROD_RELEASE_TAG: ${{ steps.metadata.outputs.tag }}- name: Package daemonrun: |set -euo pipefail@@ -976,6 +1033,18 @@ jobs:echo "skipping smoke run: $TARGET does not execute on this runner"fi+ # A release binary carries its tag: the self-updater compares it with the+ # latest release, so a binary built without it (or served a stale tag from a+ # compiler cache) would never update, or would update forever.+ case "$RELEASE_TAG" in+ v*)+ if ! grep -aqF "$RELEASE_TAG" "$binary"; then+ echo "$binary does not carry its release tag $RELEASE_TAG" >&2+ exit 1+ fi+ ;;+ esac+mkdir -p artifactsstage=$(mktemp -d)install -m 755 "$binary" "$stage/$executable"@@ -1013,6 +1082,7 @@ jobs:TARGET: x86_64-pc-windows-msvcSLUG: windows-x86_64SAFE_TAG: ${{ steps.metadata.outputs.safe_tag }}+ RELEASE_TAG: ${{ steps.metadata.outputs.tag }}SMOKE_RUN: '1'- name: Upload macrod-windows-x86_64-${{ steps.metadata.outputs.safe_tag }} artifactuses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02diff --git a/.github/workflows/test_macrod_update.yml b/.github/workflows/test_macrod_update.ymlnew file mode 100644index 0000000000..c3eff52356--- /dev/null+++ b/.github/workflows/test_macrod_update.yml@@ -0,0 +1,138 @@+# DO NOT EDIT — regenerate with `cargo x workflows` (from the repository root).+# Source: tooling/xtask/crates/xtask_workflows/src/workflows/test_macrod_update.rs+name: Test macrod update+'on':+ pull_request:+ branches:+ - main+ paths:+ - crates/coding_agent_worker/**+ - '.github/workflows/test_macrod_update.yml'+ workflow_dispatch: {}+permissions:+ contents: read+jobs:+ linux:+ name: macrod update (linux)+ runs-on: namespace-profile-linux-rust-ci+ steps:+ - name: Checkout+ uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd+ with:+ clean: 'false'+ persist-credentials: 'false'+ - name: Mount Namespace cache volume+ uses: namespacelabs/nscloud-cache-action@15799a6b54e5765f85b2aac25b3f0df43ed571c0+ with:+ cache: rust+ path: /nix+ continue-on-error: true+ - name: Setup Nix+ uses: './.github/actions/setup-nix'+ with:+ nix-cache-url: ${{ vars.NIX_CACHE_URL }}+ nix-cache-public-key: ${{ vars.NIX_CACHE_PUBLIC_KEY }}+ aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}+ aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}+ - name: Setup Nix dev shell+ uses: './.github/actions/setup-nix-dev-shell'+ with:+ shell: agent-daemon+ - name: Configure Namespace remote sccache+ if: (github.event_name != 'pull_request' && github.event_name != 'pull_request_target') || github.event.pull_request.head.repo.full_name == github.repository+ run: |-+ set -euo pipefail+ env_file="$(mktemp "$RUNNER_TEMP/namespace-sccache.XXXXXX")"+ trap 'rm -f "$env_file"' EXIT+ if nsc cache sccache setup --cache_name sccache-ci > "$env_file"; then+ # Register credential values as masked BEFORE exporting: GitHub only+ # masks `secrets.*`, so without this SCCACHE_WEBDAV_TOKEN — a broad,+ # ~24h Namespace workspace token (registry + cache write) — printed+ # verbatim in every subsequent step's env dump. Masking is selective by+ # key name: masking non-secrets like the endpoint URL or key prefix+ # would redact those strings everywhere in the logs.+ while IFS= read -r line; do+ k="${line%%=*}"+ v="${line#*=}"+ [ -n "$v" ] && [ "$k" != "$line" ] || continue+ case "$k" in+ *TOKEN*|*SECRET*|*PASSWORD*) echo "::add-mask::$v" ;;+ esac+ done < "$env_file"+ cat "$env_file" >> "$GITHUB_ENV"+ # Force the next compiler invocation to start a server with the new remote+ # backend even if a setup hook happened to launch one already.+ sccache --stop-server >/dev/null 2>&1 || true+ else+ echo "::warning::Namespace remote sccache setup failed; using local cache fallback"+ fi+ - name: Build two tagged releases+ run: crates/coding_agent_worker/e2e/build_releases.sh "$RUNNER_TEMP/releases" v2000.1.1.1 v2000.1.1.2+ shell: bash+ env:+ SQLX_OFFLINE: 'true'+ TARGET: x86_64-unknown-linux-musl+ CARGO_BUILD: cargo zigbuild+ AWS_LC_SYS_CMAKE_BUILDER: '1'+ - name: Self-update end to end+ run: python3 crates/coding_agent_worker/e2e/update_e2e.py --releases "$RUNNER_TEMP/releases" --v1 v2000.1.1.1 --v2 v2000.1.1.2+ shell: bash+ - name: Installer in bash, zsh and fish+ run: nix shell nixpkgs#zsh nixpkgs#fish --command crates/coding_agent_worker/e2e/installer_test.sh "$RUNNER_TEMP/releases" v2000.1.1.1+ shell: bash+ - name: Teardown Nix+ if: always()+ uses: './.github/actions/teardown-nix'+ macos:+ name: macrod update (macos)+ runs-on: nscloud-macos-sequoia-arm64-6x14+ steps:+ - name: Checkout+ uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd+ with:+ clean: 'false'+ persist-credentials: 'false'+ - name: Setup Rust+ uses: './.github/actions/setup-rust'+ with:+ sccache: 'false'+ rust-cache: 'false'+ - name: Build two tagged releases+ run: crates/coding_agent_worker/e2e/build_releases.sh "$RUNNER_TEMP/releases" v2000.1.1.1 v2000.1.1.2+ shell: bash+ env:+ SQLX_OFFLINE: 'true'+ - name: Self-update end to end+ run: python3 crates/coding_agent_worker/e2e/update_e2e.py --releases "$RUNNER_TEMP/releases" --v1 v2000.1.1.1 --v2 v2000.1.1.2+ shell: bash+ - name: Installer in bash and zsh+ run: crates/coding_agent_worker/e2e/installer_test.sh "$RUNNER_TEMP/releases" v2000.1.1.1+ shell: bash+ env:+ INSTALLER_TEST_SHELLS: bash zsh+ windows:+ name: macrod update (windows)+ runs-on: nscloud-windows-2022-amd64-8x16+ steps:+ - name: Checkout+ uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd+ with:+ clean: 'false'+ persist-credentials: 'false'+ - name: Setup Rust+ uses: './.github/actions/setup-rust'+ with:+ sccache: 'false'+ rust-cache: 'false'+ - name: Build two tagged releases+ run: crates/coding_agent_worker/e2e/build_releases.sh "$RUNNER_TEMP/releases" v2000.1.1.1 v2000.1.1.2+ shell: bash+ env:+ SQLX_OFFLINE: 'true'+ AWS_LC_SYS_PREBUILT_NASM: '1'+ - name: Launcher layout and install.ps1+ run: '& ./crates/coding_agent_worker/e2e/windows_test.ps1 -Releases "$env:RUNNER_TEMP/releases" -V1 v2000.1.1.1 -V2 v2000.1.1.2'+ shell: pwsh+ - name: Self-update end to end+ run: '& ./crates/coding_agent_worker/e2e/windows_update_test.ps1 -Releases "$env:RUNNER_TEMP/releases" -V1 v2000.1.1.1 -V2 v2000.1.1.2'+ shell: pwshdiff --git a/Cargo.lock b/Cargo.lockindex 00e34415d3..ff28d0b3da 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4589,9 +4589,11 @@ dependencies = ["crossterm 0.28.1","dashmap","eventsource-stream",+ "flate2","futures","harness_id","harnesses",+ "hex","lazy-regex","macro_env_var","macro_event_broker",@@ -4606,8 +4608,10 @@ dependencies = ["rustls","serde","serde_json",+ "sha2 0.10.9","shell-words","strum 0.27.2",+ "tar","tempfile","thiserror 2.0.18","tokio",@@ -4625,6 +4629,7 @@ dependencies = ["webhook","windows-sys 0.61.2","workspace-hack",+ "zip 9.0.5",][[package]]@@ -7860,7 +7865,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"dependencies = ["libc",- "windows-sys 0.52.0",+ "windows-sys 0.61.2",][[package]]@@ -8157,6 +8162,16 @@ dependencies = ["zune-jpeg",]+[[package]]+name = "filetime"+version = "0.2.29"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5c287a33c7f0a620c38e641e7f60827713987b3c0f26e8ddc9462cc69cf75759"+dependencies = [+ "cfg-if",+ "libc",+]+[[package]]name = "filter_ast"version = "0.1.0"@@ -10191,7 +10206,7 @@ dependencies = ["libc","percent-encoding","pin-project-lite",- "socket2 0.5.10",+ "socket2 0.6.3","system-configuration","tokio","tower-service",@@ -10992,7 +11007,7 @@ checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46"dependencies = ["hermit-abi","libc",- "windows-sys 0.52.0",+ "windows-sys 0.61.2",][[package]]@@ -13885,7 +13900,7 @@ version = "0.50.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"dependencies = [- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -14580,7 +14595,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"dependencies = ["libc",- "windows-sys 0.45.0",+ "windows-sys 0.61.2",][[package]]@@ -15760,7 +15775,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "22505a5c94da8e3b7c2996394d1c933236c4d743e81a410bcca4e6989fc066a4"dependencies = ["bytes",- "heck 0.4.1",+ "heck 0.5.0","itertools 0.12.1","log","multimap",@@ -15780,7 +15795,7 @@ version = "0.14.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "343d3bd7056eda839b03204e68deff7d1b13aba7af2b2fd16890697274262ee7"dependencies = [- "heck 0.4.1",+ "heck 0.5.0","itertools 0.14.0","log","multimap",@@ -15948,7 +15963,7 @@ dependencies = ["quinn-udp","rustc-hash 2.1.2","rustls",- "socket2 0.5.10",+ "socket2 0.6.3","thiserror 2.0.18","tokio","tracing",@@ -15986,7 +16001,7 @@ dependencies = ["cfg_aliases","libc","once_cell",- "socket2 0.5.10",+ "socket2 0.6.3","tracing","windows-sys 0.59.0",]@@ -17126,7 +17141,7 @@ dependencies = ["errno","libc","linux-raw-sys 0.12.1",- "windows-sys 0.52.0",+ "windows-sys 0.61.2",][[package]]@@ -17185,7 +17200,7 @@ dependencies = ["security-framework","security-framework-sys","webpki-root-certs",- "windows-sys 0.52.0",+ "windows-sys 0.61.2",][[package]]@@ -19784,6 +19799,17 @@ version = "1.0.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"+[[package]]+name = "tar"+version = "0.4.46"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "3f6221d9a6003c78398e3b239969f352578258df48c8eb051caadae0015bc840"+dependencies = [+ "filetime",+ "libc",+ "xattr",+]+[[package]]name = "target-lexicon"version = "0.13.5"@@ -19903,7 +19929,7 @@ dependencies = ["getrandom 0.4.2","once_cell","rustix 1.1.4",- "windows-sys 0.52.0",+ "windows-sys 0.61.2",][[package]]@@ -22093,7 +22119,7 @@ version = "0.1.11"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"dependencies = [- "windows-sys 0.48.0",+ "windows-sys 0.61.2",][[package]]@@ -22989,6 +23015,16 @@ dependencies = ["zeroize",]+[[package]]+name = "xattr"+version = "1.6.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156"+dependencies = [+ "libc",+ "rustix 1.1.4",+]+[[package]]name = "xmlparser"version = "0.13.6"diff --git a/Cargo.toml b/Cargo.tomlindex 0ff385c597..995831eec7 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -416,6 +416,7 @@ symphonia = { version = "0.6.1", default-features = false, features = ["ogg","wav",] }+tar = "0.4.46"tempfile = "3"thiserror = "2.0.0"time = "0.3.37"diff --git a/apps/docs/AI/bring-your-own.mdx b/apps/docs/AI/bring-your-own.mdxindex 676c0bb061..ab96139e58 100644--- a/apps/docs/AI/bring-your-own.mdx+++ b/apps/docs/AI/bring-your-own.mdx@@ -51,7 +51,7 @@ You need a macOS, Linux, or Windows computer that can stay online and an installcurl -fsSL https://raw.githubusercontent.com/macro-inc/macro/main/crates/coding_agent_worker/install/install.sh | sh```- This installs `macrod` to `~/.local/bin`. If that folder is not on your `PATH`, the installer prints the line to add.+ This installs `macrod` to `~/.local/bin` and adds that folder to your `PATH` in your shell's startup file (`~/.zshrc`, `~/.bashrc` and `~/.bash_profile`, or fish's `conf.d`), once. Open a new terminal afterwards. To leave your startup files alone, run `curl -fsSL … | sh -s -- --no-modify-path` or set `MACROD_NO_MODIFY_PATH=1`.</Tab><Tab title="Windows">In PowerShell:@@ -60,7 +60,7 @@ You need a macOS, Linux, or Windows computer that can stay online and an installirm https://raw.githubusercontent.com/macro-inc/macro/main/crates/coding_agent_worker/install/install.ps1 | iex```- This installs `macrod.exe` to `%LOCALAPPDATA%\Programs\macrod` and adds it to your `PATH`. Open a new terminal afterwards.+ This installs `macrod.exe` to `%LOCALAPPDATA%\Programs\macrod` and adds that folder to your user `PATH`. Open a new terminal afterwards.</Tab></Tabs>@@ -122,9 +122,25 @@ To reconnect later, run `macrod` from the same directory so it loads your saved## Update macrod-Run the install command again. It replaces `macrod` with the latest release and keeps your `macrod.toml`. A running `macrod` keeps the old version until you restart it.+`macrod` updates itself. While it runs, it checks for a new release every hour, downloads it, and verifies its checksum and that it starts. Your `macrod.toml` and pairing are kept. If an update fails, `macrod` keeps running the current version and shows the error in its **Logs** tab.-To install a specific release instead, set the `MACROD_VERSION` environment variable to its tag, such as `v2026.10.18.0`, before running the install command.+- **macOS and Linux:** `macrod` replaces itself in place as soon as no agent turn is running. Your agent keeps running through the update, so its sessions and anything it started stay alive.+- **Windows:** the new version is installed next to the running one, and the footer shows **Update … ready — restart macrod to use it**. Quit `macrod` and start it again to use it. The `macrod` command always starts the newest installed version and removes older ones.++To see what the updater did, read `~/.macrod/logs/update.log` (`%USERPROFILE%\.macrod\logs\update.log` on Windows). Each line is one JSON event: a `check` with the version found, a `download` with its size and whether the checksum matched, an `apply` with the versions and how the new one was started, and a `startup` line each time `macrod` starts, including after an update. The log never holds tokens or prompts. At about 1 MB it moves to `update.log.1`.++To update right away, run the install command again and restart `macrod`.++To install a specific release, set the `MACROD_VERSION` environment variable to its tag, such as `v2026.10.18.0`, before running the install command. A running `macrod` still updates itself to the latest release.++To turn automatic updates off, add this to `macrod.toml`:++```toml+[update]+auto = false+```++or start `macrod` with `MACROD_NO_UPDATE=1` set. `macrod` then only changes when you run the install command again.## Troubleshootingdiff --git a/crates/agent_runtime_protocol/src/outbound/websocket.rs b/crates/agent_runtime_protocol/src/outbound/websocket.rsindex 2776568ed0..440be488ba 100644--- a/crates/agent_runtime_protocol/src/outbound/websocket.rs+++ b/crates/agent_runtime_protocol/src/outbound/websocket.rs@@ -117,7 +117,9 @@ impl WireFrame for TungsteniteMessage {/// Drive one physical WebSocket until it closes or fails: outgoing JSON text/// pulled from `outgoing` is written to the socket, and incoming text frames-/// are decoded and pushed onto `incoming`.+/// are decoded and pushed onto `incoming`. When `outgoing` ends, this side+/// sends its close frame and keeps reading until the peer closes (or goes+/// silent), so nothing the peer already sent is lost.////// A frame that fails to decode as `Rx` is dropped with a warning rather than/// closing the connection - the rest of the logical stream is unaffected by@@ -137,10 +139,21 @@ async fn run_pump<Rx, Socket, Frame, Error>(// however many it slept through.ping.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);let mut last_inbound = Instant::now();+ // Set once this side has nothing more to send: its close frame is out,+ // and the pump keeps reading until the peer closes too.+ let mut closing = false;loop {tokio::select! {- next_outgoing = outgoing.recv() => {- let Some(payload) = next_outgoing else { break };+ next_outgoing = outgoing.recv(), if !closing => {+ let Some(payload) = next_outgoing else {+ // Frames the peer sent before it sees the close are still+ // delivered; dropping the socket here would discard them.+ if sink.close().await.is_err() {+ break;+ }+ closing = true;+ continue;+ };if sink.send(Frame::text(payload)).await.is_err() {break;}@@ -177,7 +190,7 @@ async fn run_pump<Rx, Socket, Frame, Error>(}// Both halves of a split socket share one connection, so this// is also what flushes any pong the read half queued.- if sink.send(Frame::ping()).await.is_err() {+ if !closing && sink.send(Frame::ping()).await.is_err() {break;}}diff --git a/crates/agent_runtime_protocol/src/outbound/websocket/test.rs b/crates/agent_runtime_protocol/src/outbound/websocket/test.rsindex a82c08eea6..878159313d 100644--- a/crates/agent_runtime_protocol/src/outbound/websocket/test.rs+++ b/crates/agent_runtime_protocol/src/outbound/websocket/test.rs@@ -210,6 +210,65 @@ async fn runtime_initiates_a_real_websocket_and_exchanges_bare_json_envelopes()server_handle.abort();}+#[tokio::test(flavor = "multi_thread")]+async fn frames_sent_before_the_peer_sees_our_close_still_arrive() {+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0")+ .await+ .expect("listener should bind");+ let address = listener.local_addr().expect("listener address");+ // A peer that answers our last frame and only then reads our close.+ let peer = tokio::spawn(async move {+ let (socket, _) = listener.accept().await.expect("peer should accept");+ let mut socket = tokio_tungstenite::accept_async(socket)+ .await+ .expect("peer should upgrade");+ // Our pump pings from the start; wait past that for our last frame.+ loop {+ let frame = socket+ .next()+ .await+ .expect("our frame should arrive")+ .expect("our frame should read");+ if frame.is_text() {+ break;+ }+ }+ socket+ .send(TungsteniteMessage::Text(+ serde_json::to_string(&acp_to_runtime()).unwrap().into(),+ ))+ .await+ .expect("the peer can still send");+ while let Some(Ok(frame)) = socket.next().await {+ if frame.is_close() {+ break;+ }+ }+ });++ let (stream, _response) = tokio_tungstenite::connect_async(format!("ws://{address}"))+ .await+ .expect("runtime should connect");+ let Channel { tx, mut rx } = connect_runtime::<ToServerMessage, ToRuntimeMessage, _>(stream);+ tx.send(event()).unwrap();+ // Nothing more to send: the pump closes our side.+ drop(tx);++ let answer = timeout(PROMPT, rx.recv())+ .await+ .expect("the answer should not hang")+ .expect("the answer sent before our close was read should arrive");+ assert_eq!(+ serde_json::to_value(answer).unwrap(),+ serde_json::to_value(acp_to_runtime()).unwrap()+ );+ let ended = timeout(PROMPT, rx.recv())+ .await+ .expect("the socket should finish closing");+ assert!(ended.is_none(), "the channel ends once the peer has closed");+ peer.await.expect("the peer should finish");+}+struct Events(tokio::sync::mpsc::UnboundedSender<SystemEvent>);impl SystemEventHandler for Events {diff --git a/crates/coding_agent_worker/AGENTS.md b/crates/coding_agent_worker/AGENTS.mdindex e3d988b192..ccbb02e248 100644--- a/crates/coding_agent_worker/AGENTS.md+++ b/crates/coding_agent_worker/AGENTS.md@@ -27,7 +27,33 @@ the local ACP process (Claude Code, Codex, ...), and drives native- `src/executable.rs`: `PATH` lookup; every spawn of a bare command name goesthrough `launchable`, which finds Windows `.cmd` shims (`npm`, `npx`).- `install/`: `install.sh` and `install.ps1`, the one-line installers the docs- serve from `main`. They download `macrod-<os>-<arch>` from the latest release.+ serve from `main`. `install.sh` adds `~/.local/bin` to the shell's startup+ file once (`--no-modify-path` opts out); `install.ps1` installs the Windows+ launcher layout. Both take `MACROD_UPDATE_SOURCE`.+- `src/update.rs` (`src/update/`): self-update. The TUI polls the latest+ GitHub release hourly (or the `MACROD_UPDATE_SOURCE` mirror: `latest.json`+ plus the archives beside it), verifies and smoke-tests the new binary, and+ once `Daemon::is_idle` (no `session/prompt` outstanding, `src/turns.rs`)+ swaps it in and `execve`s. Spans: `macrod.update.check`,+ `macrod.update.download`, `macrod.update.apply`, `macrod.update.handoff`.+ Off with `[update] auto = false` or `MACROD_NO_UPDATE=1`. Every check,+ download, apply and TUI startup appends a JSON line to+ `~/.macrod/logs/update.log` (`src/update/log.rs`; 0600, rotated at 1 MB to+ `update.log.1`); the Logs tab shows the path. Versions and outcomes only.+- `src/handoff.rs`: the Unix hot handoff. `harness.rs` is macrod's own line+ relay (`outbound/acp_process/running.rs`), so it can stop at a line+ boundary and pass the live agent (pid, pipes, unread bytes, running+ prompts) to the next image through a versioned state file named by+ `MACROD_HANDOFF_FD`. State the new image cannot read kills the agent and+ starts fresh. Bump `FORMAT_VERSION` when `HandoffState` changes.+- `src/update/versions.rs`: the Windows layout. `macrod.exe` on `PATH` is a+ launcher for the newest `versions\<tag>\macrod-core.exe`; updates install+ as a new version for the next launch and the footer says so.+- `e2e/`: end-to-end update and installer tests (`build_releases.sh`,+ `update_e2e.py` (update, prompt-in-gap, output-in-gap, and the negative+ cases), `installer_test.sh`, `windows_test.ps1` for the launcher+ layout, `windows_update_test.ps1` for a running macrod updating itself), run+ by the `test_macrod_update` workflow on Linux, macOS and Windows.- `README.md`: native Herdr session behavior (repositories, reconnect, modelswitching, MCP) — read before changing anything under `src/herdr/`.@@ -57,6 +83,11 @@ external chat replies depend on it. Direct session controls still mint IDs.- `cargo test -p coding_agent_worker` (`SQLX_OFFLINE` unset).- `just run` (from this directory) to run the daemon against `macrod.toml` atthe repo root.+- Self-update end to end (Linux/macOS): `e2e/build_releases.sh /tmp/rel+ v2000.1.1.1 v2000.1.1.2`, then `python3 e2e/update_e2e.py --releases /tmp/rel+ --v1 v2000.1.1.1 --v2 v2000.1.1.2`, and `e2e/installer_test.sh /tmp/rel+ v2000.1.1.1` with zsh and fish on `PATH`. `MACROD_UPDATE_CHECK_SECONDS`+ shortens the check interval.- `OPENSSL_NO_VENDOR=0 just build-static` to build and smoke-test the release `macrod` the waythe tag workflow does (needs the Nix devshell: cargo-zigbuild, zig, cmake).The override enables bundled OpenSSL for the musl target in the devshell.@@ -74,6 +105,16 @@ external chat replies depend on it. Direct session controls still mint IDs.Object on Windows (`src/outbound/acp_process/job.rs`).- macrod never pulls into the operator's working branch — new herdr worktreesstart from a fresh fetch of `origin/main` (see README.md).+- Only release builds update themselves: the tag workflow compiles the tag in+ as `MACROD_RELEASE_TAG`, and a build without a `vYYYY.MM.DD.N` tag never+ polls. `macrod service` (the desktop app's bundled copy) never updates; the+ desktop app updates it.+- The update still waits for idle although the agent survives it: the+ harness service ends a session's turn when its runtime connection closes,+ so a turn in flight would be cut off in Macro. After a handoff the service+ re-runs `initialize` and `session/resume` (or `load`) on the live agent.+- A `macrod herdr-acp` harness is a child process, so it is handed off like+ any agent and keeps running the old binary until it restarts.- Switching native agents clears the previous agent's model/extra-args butkeeps storage and focus preferences (README.md, "Setup").diff --git a/crates/coding_agent_worker/Cargo.toml b/crates/coding_agent_worker/Cargo.tomlindex 34b33d1474..50c5a93811 100644--- a/crates/coding_agent_worker/Cargo.toml+++ b/crates/coding_agent_worker/Cargo.toml@@ -31,6 +31,7 @@ eventsource-stream = { workspace = true }futures = { workspace = true }harness_id = { path = "../harness_id" }harnesses = { path = "../harnesses", default-features = false }+hex = { workspace = true }lazy-regex = { workspace = true }macro_event_broker = { path = "../macro_event_broker", default-features = false }macro_env_var = { path = "../macro_env_var" }@@ -44,6 +45,7 @@ rootcause = { workspace = true }rustls = { workspace = true }serde = { workspace = true }serde_json = { workspace = true }+sha2 = { workspace = true }shell-words = { workspace = true }strum = { workspace = true }thiserror = { workspace = true }@@ -64,6 +66,11 @@ workspace-hack = { version = "0.1", path = "../workspace-hack" }[target.'cfg(unix)'.dependencies]rustix = { workspace = true }+# Release archives: tar.gz everywhere but Windows, which gets a zip.+[target.'cfg(not(windows))'.dependencies]+flate2 = { workspace = true }+tar = { workspace = true }+# Stripe (agent_session -> ai_billing) reaches native-tls, which is OpenSSL on# Linux, and a musl build has no system OpenSSL to link: compile it in.[target.'cfg(target_env = "musl")'.dependencies]@@ -73,8 +80,10 @@ openssl-sys = { workspace = true, features = ["vendored"] }windows-sys = { workspace = true, features = ["Win32_Foundation","Win32_Security",+ "Win32_System_Console","Win32_System_JobObjects",] }+zip = { workspace = true }[dev-dependencies]bot_id = { path = "../bot_id", features = ["test-utils"] }diff --git a/crates/coding_agent_worker/config.example.toml b/crates/coding_agent_worker/config.example.tomlindex 3fbe92537b..9ccbda3179 100644--- a/crates/coding_agent_worker/config.example.toml+++ b/crates/coding_agent_worker/config.example.toml@@ -46,3 +46,8 @@ repo_url = "https://github.com/macro-inc/macro"# arguments = [] # Native CLI arguments, one word per element.# storage_path = "/path/to/agents" # Omit for ~/.macrod/herdr/<harness-id>.# focus = true # false opens new sessions in the background.++# Optional: a released macrod checks for a newer release every hour and+# installs it. Set auto = false (or MACROD_NO_UPDATE=1) to turn that off.+# [update]+# auto = truediff --git a/crates/coding_agent_worker/e2e/build_releases.sh b/crates/coding_agent_worker/e2e/build_releases.shnew file mode 100755index 0000000000..f68c05f874--- /dev/null+++ b/crates/coding_agent_worker/e2e/build_releases.sh@@ -0,0 +1,57 @@+#!/usr/bin/env bash+# Build macrod twice from this checkout, as two release tags, and package each+# the way the release workflow does: macrod-<tag>-<os>-<arch>.tar.gz plus its+# .sha256 (a .zip on Windows). A debug build is enough to exercise the+# updater.+#+# build_releases.sh OUT_DIR V1_TAG V2_TAG+#+# TARGET builds for that triple (CI builds musl like the release workflow);+# CARGO_BUILD defaults to `cargo build`, e.g. `cargo zigbuild` for musl.+set -euo pipefail++out=$1+shift+mkdir -p "$out"+executable=macrod+case "$(uname -s)" in+ Linux) os=linux ;;+ Darwin) os=macos ;;+ MINGW* | MSYS* | CYGWIN*) os=windows executable=macrod.exe ;;+ *) echo "unsupported OS" >&2; exit 1 ;;+esac+case "$(uname -m)" in+ x86_64 | amd64) arch=x86_64 ;;+ arm64 | aarch64) arch=aarch64 ;;+esac+target_dir=${CARGO_TARGET_DIR:-target}++binary_dir="$target_dir/debug"+target_args=()+if [ -n "${TARGET:-}" ]; then+ target_args=(--target "$TARGET")+ binary_dir="$target_dir/$TARGET/debug"+fi+# shellcheck disable=SC2206 # a command and its subcommand, split on purpose+cargo_build=(${CARGO_BUILD:-cargo build})++for tag in "$@"; do+ MACROD_RELEASE_TAG="$tag" "${cargo_build[@]}" -p coding_agent_worker --bin macrod "${target_args[@]}"+ stage=$(mktemp -d)+ install -m 755 "$binary_dir/$executable" "$stage/$executable"+ "$stage/$executable" --version | grep -qF "$tag"+ if [ "$os" = windows ]; then+ archive="macrod-$tag-$os-$arch.zip"+ powershell.exe -NoProfile -NonInteractive -Command \+ "Compress-Archive -Path '$(cygpath -w "$stage/$executable")' -DestinationPath '$(cygpath -w "$out/$archive")' -Force"+ else+ archive="macrod-$tag-$os-$arch.tar.gz"+ tar -czf "$out/$archive" -C "$stage" "$executable"+ fi+ (+ cd "$out"+ if command -v sha256sum >/dev/null 2>&1; then sha256sum "$archive"; else shasum -a 256 "$archive"; fi >"$archive.sha256"+ )+ rm -rf "$stage"+ echo "packaged $out/$archive"+donediff --git a/crates/coding_agent_worker/e2e/installer_test.sh b/crates/coding_agent_worker/e2e/installer_test.shnew file mode 100755index 0000000000..90363ef9e1--- /dev/null+++ b/crates/coding_agent_worker/e2e/installer_test.sh@@ -0,0 +1,97 @@+#!/usr/bin/env bash+# install.sh in fresh homes, one per login shell: macrod must resolve in a new+# shell, a second run must not add the PATH line again, and --no-modify-path+# must leave startup files alone. Installs from a directory mirror.+#+# installer_test.sh RELEASES_DIR TAG (INSTALLER_TEST_SHELLS="bash zsh" to pick)+set -euo pipefail++releases=$(cd "$1" && pwd)+tag=$2+here=$(cd "$(dirname "$0")" && pwd)+installer="$here/../install/install.sh"+work=$(mktemp -d)+trap 'rm -rf "$work"' EXIT++mirror="$work/mirror"+mkdir -p "$mirror"+cp "$releases"/macrod-"$tag"-* "$mirror/"+printf '{"tag_name": "%s", "assets": []}\n' "$tag" >"$mirror/latest.json"++marker="# Added by the macrod installer"+# The caller's PATH, which must not already contain the install folder.+base_path=$PATH+failures=0+fail() {+ echo "FAIL: $*" >&2+ failures=$((failures + 1))+}++# Run the installer as a user of $1 with a fresh home; prints the home.+install_for() {+ local shell=$1+ shift+ local home="$work/home-$shell-$RANDOM"+ mkdir -p "$home"+ env -i HOME="$home" PATH="$base_path" SHELL="/bin/$shell" \+ MACROD_UPDATE_SOURCE="$mirror" MACROD_NO_MODIFY_PATH="${MACROD_NO_MODIFY_PATH:-}" \+ sh "$installer" "$@" >"$home.log" 2>&1 ||+ { cat "$home.log" >&2; fail "$shell: install.sh failed"; }+ echo "$home"+}++# What `macrod` resolves to in a new interactive shell of that kind.+resolve_in_new_shell() {+ local shell=$1 home=$2+ case "$shell" in+ bash) env -i HOME="$home" PATH="$base_path" TERM=dumb "$(command -v bash)" -ic 'command -v macrod' 2>/dev/null ;;+ # A login shell too: macOS Terminal starts those.+ bash-login) env -i HOME="$home" PATH="$base_path" TERM=dumb "$(command -v bash)" -lc 'command -v macrod' 2>/dev/null ;;+ zsh) env -i HOME="$home" PATH="$base_path" TERM=dumb "$(command -v zsh)" -ic 'command -v macrod' 2>/dev/null ;;+ fish) env -i HOME="$home" PATH="$base_path" TERM=dumb "$(command -v fish)" -c 'command -v macrod' 2>/dev/null ;;+ esac+}++# Every line of every startup file the installer may touch.+startup_lines() {+ { cat "$1"/.bashrc "$1"/.bash_profile "$1"/.zshrc "$1"/.config/fish/conf.d/macrod.fish 2>/dev/null || true; } | wc -l | tr -d ' '+}++for shell in ${INSTALLER_TEST_SHELLS:-bash zsh fish}; do+ if ! command -v "$shell" >/dev/null 2>&1; then+ fail "$shell is not installed here"+ continue+ fi+ home=$(install_for "$shell")+ expected="$home/.local/bin/macrod"+ resolved=$(resolve_in_new_shell "$shell" "$home" || true)+ [ "$resolved" = "$expected" ] || fail "$shell: a new shell resolves macrod to '$resolved', not $expected"+ if [ "$shell" = bash ] && [ "$(uname -s)" = Darwin ]; then+ resolved=$(resolve_in_new_shell bash-login "$home" || true)+ [ "$resolved" = "$expected" ] || fail "bash login: resolves macrod to '$resolved'"+ fi+ [ "$("$expected" --version)" = "macrod $tag" ] || fail "$shell: wrong version installed"++ # A second run (an update by hand) changes nothing.+ before=$(startup_lines "$home")+ env -i HOME="$home" PATH="$base_path" SHELL="/bin/$shell" MACROD_UPDATE_SOURCE="$mirror" sh "$installer" >"$home.second.log" 2>&1 ||+ fail "$shell: second install failed"+ after=$(startup_lines "$home")+ [ "$before" = "$after" ] || fail "$shell: the second run changed startup files ($before -> $after lines)"+ count=$({ cat "$home"/.bashrc "$home"/.zshrc "$home"/.config/fish/conf.d/macrod.fish 2>/dev/null || true; } | grep -cF "$marker" || true)+ [ "$count" = 1 ] || fail "$shell: $count PATH entries in the primary startup file"+ grep -q "already adds" "$home.second.log" || fail "$shell: the second run did not say it changed nothing"+ echo "ok $shell: $(grep -h '^Added' "$home.log" | tr '\n' ' ')"+done++home=$(install_for bash --no-modify-path)+[ ! -e "$home/.bashrc" ] || fail "--no-modify-path wrote $home/.bashrc"+home=$(MACROD_NO_MODIFY_PATH=1 install_for zsh)+[ ! -e "$home/.zshrc" ] || fail "MACROD_NO_MODIFY_PATH wrote $home/.zshrc"+echo "ok --no-modify-path"++if [ "$failures" -gt 0 ]; then+ echo "$failures installer check(s) failed" >&2+ exit 1+fi+echo "all installer checks passed"diff --git a/crates/coding_agent_worker/e2e/stand_in_agent.py b/crates/coding_agent_worker/e2e/stand_in_agent.pynew file mode 100644index 0000000000..a07ee95ffd--- /dev/null+++ b/crates/coding_agent_worker/e2e/stand_in_agent.py@@ -0,0 +1,41 @@+"""ACP stand-in for the self-update end-to-end test.++Streams a numbered `tick` notification every 20 ms, carrying its own pid,+the pid of a background child it started, and the wall-clock time it was+written, and answers every request.+"""++import json+import os+import subprocess+import sys+import threading+import time++lock = threading.Lock()+child = subprocess.Popen(["sleep", "3600"])+++def send(message):+ with lock:+ sys.stdout.write(json.dumps(message) + "\n")+ sys.stdout.flush()+++def tick():+ n = 0+ while True:+ send({+ "jsonrpc": "2.0",+ "method": "tick",+ "params": {"n": n, "pid": os.getpid(), "child": child.pid, "t": time.time()},+ })+ n += 1+ time.sleep(0.02)+++threading.Thread(target=tick, daemon=True).start()+for line in sys.stdin:+ request = json.loads(line)+ if "id" in request and "method" in request:+ send({"jsonrpc": "2.0", "id": request["id"], "result": {}})diff --git a/crates/coding_agent_worker/e2e/update_e2e.py b/crates/coding_agent_worker/e2e/update_e2e.pynew file mode 100755index 0000000000..69f5278526--- /dev/null+++ b/crates/coding_agent_worker/e2e/update_e2e.py@@ -0,0 +1,477 @@+"""End-to-end test of macrod's self-update on Linux and macOS.++Installs release v1 of macrod with install.sh from a local mirror, runs it in+a pseudo-terminal against a fake runtime gateway with a stand-in ACP agent,+publishes v2 to the mirror, and checks that macrod replaced itself in place:+same macrod pid, same agent pid, the agent's background child alive, and no+gap in the agent's numbered frames across the two gateway connections.++Two more update cases cover macrod's half of the gap between the old image+closing its gateway socket and the new one dialing: `prompt-in-gap` sends a+prompt on the closing socket after macrod has said it is disconnecting and+checks it is answered exactly once, on the new connection; `output-in-gap`+checks agent output written while no socket is up reaches the gateway. The+fake gateway accepts any frame on any connection; the real harness service+keys request ids and sessions per connection, so these show macrod loses+nothing, not that the service keeps a session across the reconnect.++Negative cases run the same way and check that macrod keeps running v1:+`bad-checksum`, `failing-smoke-test`, `opt-out` and `opt-out-config`.++ python3 update_e2e.py --releases DIR --v1 TAG --v2 TAG [--case NAME]++DIR holds macrod-<tag>-<os>-<arch>.tar.gz (+ .sha256) for both tags, as the+release workflow publishes them.+"""++import argparse+import base64+import hashlib+import http.server+import io+import json+import os+import platform+import pty+import re+import fcntl+import termios+import shutil+import socket+import socketserver+import struct+import subprocess+import sys+import tarfile+import tempfile+import threading+import time++HERE = os.path.dirname(os.path.abspath(__file__))+INSTALLER = os.path.join(HERE, "..", "install", "install.sh")+AGENT = os.path.join(HERE, "stand_in_agent.py")+++def platform_slug():+ system = {"Linux": "linux", "Darwin": "macos"}[platform.system()]+ machine = platform.machine().lower()+ arch = {"x86_64": "x86_64", "amd64": "x86_64", "arm64": "aarch64", "aarch64": "aarch64"}[machine]+ return f"{system}-{arch}"+++# ---------------------------------------------------------------- the mirror+++class Mirror:+ """A release mirror directory served over HTTP."""++ def __init__(self, releases, root):+ self.releases = releases+ self.root = root+ handler = lambda *args: http.server.SimpleHTTPRequestHandler(*args, directory=root)+ http.server.SimpleHTTPRequestHandler.log_message = lambda *args: None+ self.server = socketserver.ThreadingTCPServer(("127.0.0.1", 0), handler)+ threading.Thread(target=self.server.serve_forever, daemon=True).start()+ self.url = f"http://127.0.0.1:{self.server.server_address[1]}"++ def archive_name(self, tag):+ return f"macrod-{tag}-{platform_slug()}.tar.gz"++ def publish(self, tag, archive=None, checksum=None):+ """Make `tag` the latest release, from the built archive or a given one."""+ name = self.archive_name(tag)+ data = archive if archive is not None else open(os.path.join(self.releases, name), "rb").read()+ with open(os.path.join(self.root, name), "wb") as out:+ out.write(data)+ digest = checksum or hashlib.sha256(data).hexdigest()+ with open(os.path.join(self.root, name + ".sha256"), "w") as out:+ out.write(f"{digest} {name}\n")+ with open(os.path.join(self.root, "latest.json.tmp"), "w") as out:+ json.dump({"tag_name": tag, "assets": []}, out)+ os.replace(os.path.join(self.root, "latest.json.tmp"), os.path.join(self.root, "latest.json"))+++def broken_archive():+ """A release archive whose macrod cannot even print its version."""+ script = b"#!/bin/sh\nexit 1\n"+ buffer = io.BytesIO()+ with tarfile.open(fileobj=buffer, mode="w:gz") as archive:+ info = tarfile.TarInfo("macrod")+ info.size = len(script)+ info.mode = 0o755+ archive.addfile(info, io.BytesIO(script))+ return buffer.getvalue()+++# ---------------------------------------------------------- the fake backend+++class Gateway:+ """The runtime gateway: accepts macrod's WebSocket and records ticks.++ With `prompt_on_disconnect`, it answers macrod's `disconnected` event by+ sending a `session/prompt` on that same, closing socket.+ """++ PROMPT_ID = "prompt-in-gap"++ def __init__(self, prompt_on_disconnect=False):+ self.prompt_on_disconnect = prompt_on_disconnect+ self.disconnected_at = {} # connection -> wall-clock time+ self.opened_at = {} # connection -> wall-clock time+ self.responses = [] # (connection, id)+ self.listener = socket.socket()+ self.listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)+ self.listener.bind(("127.0.0.1", 0))+ self.listener.listen()+ self.port = self.listener.getsockname()[1]+ self.connections = 0+ self.ticks = [] # (connection, n, pid, child)+ self.lock = threading.Lock()+ threading.Thread(target=self.accept, daemon=True).start()++ def accept(self):+ while True:+ conn, _ = self.listener.accept()+ threading.Thread(target=self.serve, args=(conn,), daemon=True).start()++ def serve(self, conn):+ request = b""+ while b"\r\n\r\n" not in request:+ chunk = conn.recv(4096)+ if not chunk:+ return+ request += chunk+ head, rest = request.split(b"\r\n\r\n", 1)+ headers = {+ name.lower(): value+ for name, value in (+ line.split(": ", 1) for line in head.decode().split("\r\n")[1:] if ": " in line+ )+ }+ if "/runtime/ws" not in head.split(b"\r\n")[0].decode():+ # Any other API call: an empty answer.+ conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 2\r\nConnection: close\r\n\r\n[]")+ conn.close()+ return+ key = headers["sec-websocket-key"]+ accept = base64.b64encode(+ hashlib.sha1((key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").encode()).digest()+ ).decode()+ conn.sendall(+ "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"+ f"Sec-WebSocket-Accept: {accept}\r\n\r\n".encode()+ )+ with self.lock:+ self.connections += 1+ number = self.connections+ self.opened_at[number] = time.time()+ reader = conn.makefile("rb")+ buffered = io.BytesIO(rest)++ def read(count):+ data = buffered.read(count)+ while len(data) < count:+ more = reader.read(count - len(data))+ if not more:+ raise EOFError+ data += more+ return data++ try:+ while True:+ first, second = read(2)+ opcode = first & 0x0F+ length = second & 0x7F+ if length == 126:+ (length,) = struct.unpack(">H", read(2))+ elif length == 127:+ (length,) = struct.unpack(">Q", read(8))+ mask = read(4) if second & 0x80 else b"\0\0\0\0"+ payload = bytes(b ^ mask[i % 4] for i, b in enumerate(read(length)))+ if opcode == 0x8:+ return+ if opcode == 0x9:+ conn.sendall(bytes([0x8A, len(payload)]) + payload)+ continue+ if opcode != 0x1:+ continue+ message = json.loads(payload)+ if message.get("type") == "acp" and message.get("method") == "tick":+ params = message["params"]+ with self.lock:+ self.ticks.append((number, params["n"], params["pid"], params["child"], params["t"]))+ elif message.get("type") == "acp" and "id" in message and "method" not in message:+ with self.lock:+ self.responses.append((number, message["id"]))+ elif message.get("type") == "event" and message.get("event") == "disconnected":+ with self.lock:+ self.disconnected_at[number] = time.time()+ if self.prompt_on_disconnect and number == 1:+ send_text(conn, {+ "type": "acp", "jsonrpc": "2.0", "id": self.PROMPT_ID,+ "method": "session/prompt",+ "params": {"sessionId": "s", "prompt": [{"type": "text", "text": "hi"}]},+ })+ except (EOFError, OSError, ValueError):+ return+ finally:+ conn.close()++ def gap_record(self):+ with self.lock:+ return dict(self.disconnected_at), dict(self.opened_at), list(self.responses)++ def snapshot(self):+ with self.lock:+ return self.connections, list(self.ticks)+++def send_text(conn, message):+ """One unmasked server-to-client WebSocket text frame."""+ payload = json.dumps(message).encode()+ if len(payload) < 126:+ header = bytes([0x81, len(payload)])+ else:+ header = bytes([0x81, 126]) + struct.pack(">H", len(payload))+ conn.sendall(header + payload)+++# ------------------------------------------------------------------- helpers+++def wait_for(description, condition, timeout):+ deadline = time.monotonic() + timeout+ while time.monotonic() < deadline:+ if condition():+ return+ time.sleep(0.2)+ raise AssertionError(f"timed out waiting for {description}")+++def alive(pid):+ try:+ os.kill(pid, 0)+ except ProcessLookupError:+ return False+ return True+++def installed_version(binary):+ return subprocess.run([binary, "--version"], capture_output=True, text=True, check=True).stdout.split()[-1]+++def update_log(home):+ """The events in macrod's update log, without their timestamps."""+ path = os.path.join(home, ".macrod", "logs", "update.log")+ assert os.stat(path).st_mode & 0o777 == 0o600, "the update log is private"+ events = []+ with open(path) as log:+ for line in log:+ event = json.loads(line)+ assert event.pop("time"), "every line is timestamped"+ events.append(event)+ return events+++def assert_in_order(events, expected):+ """Each expected event appears, in this order, with these fields."""+ position = 0+ for want in expected:+ while position < len(events) and any(events[position].get(k) != v for k, v in want.items()):+ position += 1+ assert position < len(events), f"missing {want} in order; log: {events}"+ position += 1+++def run_case(case, releases, v1, v2):+ print(f"== {case}", flush=True)+ work = tempfile.mkdtemp(prefix="macrod-e2e-")+ home = os.path.join(work, "home")+ os.makedirs(home)+ mirror_root = os.path.join(work, "mirror")+ os.makedirs(mirror_root)+ mirror = Mirror(releases, mirror_root)+ mirror.publish(v1)++ env = {+ "HOME": home,+ "PATH": os.environ["PATH"],+ "SHELL": "/bin/bash",+ "TERM": "xterm-256color",+ "MACROD_UPDATE_SOURCE": mirror.url,+ "MACROD_UPDATE_CHECK_SECONDS": "1",+ "RUST_LOG": "info",+ }+ subprocess.run(["sh", INSTALLER], env=env, check=True)+ binary = os.path.join(home, ".local", "bin", "macrod")+ assert installed_version(binary) == v1, "install.sh installs the latest release"++ gateway = Gateway(prompt_on_disconnect=case == "prompt-in-gap")+ base = f"http://127.0.0.1:{gateway.port}"+ workspace = os.path.join(work, "workspace")+ os.makedirs(workspace)+ with open(os.path.join(workspace, "macrod.toml"), "w") as config:+ config.write(f"""+[macro]+api_url = "{base}"+storage_url = "{base}"+web_url = "{base}"++[credentials]+harness_id = "00000000-0000-0000-0000-0000004a125a"+token = "mhns_e2e"+scope = "user"++[harness]+command = "{sys.executable}"+args = ["{AGENT}"]++[workspace]+path = "{workspace}"+""")+ if case == "opt-out-config":+ config.write("\n[update]\nauto = false\n")+ if case == "opt-out":+ env["MACROD_NO_UPDATE"] = "1"++ pid, terminal = pty.fork()+ if pid == 0:+ os.chdir(workspace)+ os.execve(binary, [binary], env)+ # A real terminal size, so the panel lays out and draws.+ fcntl.ioctl(terminal, termios.TIOCSWINSZ, struct.pack("HHHH", 50, 160, 0, 0))+ screen = bytearray()++ def drain():+ while True:+ try:+ data = os.read(terminal, 65536)+ except OSError:+ return+ if not data:+ return+ screen.extend(data)++ threading.Thread(target=drain, daemon=True).start()+ try:+ wait_for("the first connection's ticks", lambda: len(gateway.snapshot()[1]) >= 100, 60)+ _, ticks = gateway.snapshot()+ agent_pid, child_pid = ticks[0][2], ticks[0][3]++ if case in UPDATE_CASES:+ mirror.publish(v2)+ wait_for("the updated macrod's connection", lambda: gateway.snapshot()[0] >= 2, 120)+ wait_for(+ "ticks on the second connection",+ lambda: sum(1 for t in gateway.snapshot()[1] if t[0] == 2) >= 100,+ 60,+ )+ connections, ticks = gateway.snapshot()+ numbers = [t[1] for t in ticks]+ assert numbers == list(range(len(numbers))), f"frames lost or repeated: {numbers}"+ assert {t[2] for t in ticks} == {agent_pid}, "one agent process across the update"+ assert alive(agent_pid) and alive(child_pid), "the agent and its child kept running"+ assert alive(pid), "macrod is running"+ assert installed_version(binary) == v2, "the installed binary is v2"+ if platform.system() == "Linux":+ exe = os.readlink(f"/proc/{pid}/exe")+ assert exe == os.path.realpath(binary), f"macrod runs {exe}"+ assert_in_order(+ update_log(home),+ [+ {"event": "startup", "version": v1, "from_handoff": False},+ {"event": "check", "current": v1, "latest": v2, "outcome": "newer"},+ {"event": "download", "version": v2, "checksum_ok": True, "result": "staged"},+ {"event": "apply", "from": v1, "to": v2, "method": "execve-handoff", "result": "restarting"},+ {"event": "startup", "version": v2, "from_handoff": True},+ ],+ )+ disconnected_at, opened_at, responses = gateway.gap_record()+ if case == "prompt-in-gap":+ answers = [connection for connection, id in responses if id == Gateway.PROMPT_ID]+ assert answers == [2], f"the prompt sent during the handoff is answered once, on the new socket: {answers}"+ print(" a prompt sent on the closing socket was answered once, on the new one")+ if case == "output-in-gap":+ # From the last frame the old image forwarded to the new+ # socket's handshake, nothing was reading the agent.+ gap = (max(t[4] for t in ticks if t[0] == 1), opened_at[2])+ in_gap = [t for t in ticks if gap[0] < t[4] < gap[1]]+ assert in_gap, f"no output was written during the {gap[1] - gap[0]:.2f}s gap"+ assert {t[0] for t in in_gap} == {2}, "output written in the gap arrives on the new socket"+ assert disconnected_at[1] < opened_at[2], "the old socket said it was disconnecting first"+ print(f" {len(in_gap)} frames written during the {gap[1] - gap[0]:.2f}s gap all arrived on the new socket")+ print(+ f" macrod pid {pid} kept, agent pid {agent_pid} kept, child {child_pid} alive, "+ f"{len(numbers)} frames 0..{numbers[-1]} over {connections} connections, now {v2}"+ )+ else:+ if case == "bad-checksum":+ mirror.publish(v2, checksum="0" * 64)+ elif case == "failing-smoke-test":+ mirror.publish(v2, archive=broken_archive())+ else:+ mirror.publish(v2)+ # Several check intervals, each of which would have updated.+ time.sleep(8)+ connections, ticks = gateway.snapshot()+ assert connections == 1, f"macrod reconnected {connections - 1} times"+ assert alive(pid), "macrod is still running"+ assert installed_version(binary) == v1, "the installed binary is still v1"+ assert {t[2] for t in ticks} == {agent_pid}+ leftovers = [name for name in os.listdir(os.path.dirname(binary)) if name.startswith(".macrod")]+ assert not leftovers, f"staging left {leftovers}"+ events = update_log(home)+ assert not [e for e in events if e["event"] == "apply"], f"nothing was applied: {events}"+ if case == "bad-checksum":+ assert_in_order(events, [{"event": "download", "version": v2, "checksum_ok": False}])+ elif case == "failing-smoke-test":+ assert_in_order(events, [{"event": "download", "version": v2, "checksum_ok": True}])+ assert "smoke test" in next(e for e in events if e["event"] == "download")["result"]+ else:+ assert [e["event"] for e in events] == ["startup"], f"updating is off: {events}"+ print(f" still {v1} after publishing {v2}; {len(ticks)} frames on one connection")+ except BaseException:+ text = re.sub(r"\x1b\[[0-9;?]*[A-Za-z]", " ", screen.decode(errors="replace"))+ print("connections/ticks:", gateway.snapshot()[0], len(gateway.snapshot()[1]))+ print(re.sub(r" +", " ", text)[-3000:])+ raise+ finally:+ try:+ os.kill(pid, 15)+ except ProcessLookupError:+ pass+ os.waitpid(pid, 0)+ _, ticks = gateway.snapshot()+ if ticks:+ for leftover in {ticks[0][2], ticks[0][3]}:+ try:+ os.kill(leftover, 9)+ except ProcessLookupError:+ pass+ mirror.server.shutdown()+ shutil.rmtree(work, ignore_errors=True)+++UPDATE_CASES = ["update", "prompt-in-gap", "output-in-gap"]+NEGATIVE_CASES = ["bad-checksum", "failing-smoke-test", "opt-out", "opt-out-config"]+++def main():+ parser = argparse.ArgumentParser()+ parser.add_argument("--releases", required=True)+ parser.add_argument("--v1", required=True)+ parser.add_argument("--v2", required=True)+ parser.add_argument(+ "--case",+ action="append",+ choices=UPDATE_CASES + NEGATIVE_CASES,+ )+ args = parser.parse_args()+ for case in args.case or UPDATE_CASES + NEGATIVE_CASES:+ run_case(case, os.path.abspath(args.releases), args.v1, args.v2)+ print("all cases passed")+++if __name__ == "__main__":+ main()diff --git a/crates/coding_agent_worker/e2e/windows_test.ps1 b/crates/coding_agent_worker/e2e/windows_test.ps1new file mode 100644index 0000000000..ed1a793121--- /dev/null+++ b/crates/coding_agent_worker/e2e/windows_test.ps1@@ -0,0 +1,81 @@+# The Windows install layout end to end: install.ps1 from a folder mirror,+# the launcher in fresh PowerShell and cmd sessions, an update staged the way+# the running macrod stages one, the launcher starting it next time and+# pruning the old version, and exit codes passing through the launcher.+#+# pwsh windows_test.ps1 -Releases DIR -V1 TAG -V2 TAG+param(+ [Parameter(Mandatory)] [string] $Releases,+ [Parameter(Mandatory)] [string] $V1,+ [Parameter(Mandatory)] [string] $V2+)+$ErrorActionPreference = 'Stop'+$installer = Join-Path $PSScriptRoot '..\install\install.ps1'+$work = Join-Path ([IO.Path]::GetTempPath()) ([Guid]::NewGuid())+$mirror = Join-Path $work 'mirror'+$installDir = Join-Path $work 'macrod'+New-Item -ItemType Directory -Path $mirror | Out-Null++function Publish($tag) {+ Copy-Item (Join-Path $Releases "macrod-$tag-windows-x86_64.zip*") $mirror+ @{ tag_name = $tag; assets = @() } | ConvertTo-Json | Set-Content (Join-Path $mirror 'latest.json')+}+function Check($condition, $message) {+ if (-not $condition) { throw "FAIL: $message" }+ Write-Host "ok: $message"+}+# What a brand-new terminal sees: the machine and user PATH as stored.+function Fresh-Path {+ [Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' + [Environment]::GetEnvironmentVariable('Path', 'User')+}+function Version-In-New-Shells {+ $path = Fresh-Path+ $ps = & powershell.exe -NoProfile -Command "`$env:Path = '$path'; (macrod --version)"+ $cmd = & cmd.exe /d /c "set `"PATH=$path`" && macrod --version"+ return @($ps.Trim(), $cmd.Trim())+}++$userPathBefore = [Environment]::GetEnvironmentVariable('Path', 'User')+try {+ Publish $V1+ $env:MACROD_UPDATE_SOURCE = $mirror+ $env:MACROD_INSTALL_DIR = $installDir+ & $installer+ $launcher = Join-Path $installDir 'macrod.exe'+ Check (Test-Path (Join-Path $installDir "versions\$V1\macrod-core.exe")) "install.ps1 lays out versions\$V1"+ $shells = Version-In-New-Shells+ Check ($shells[0] -eq "macrod $V1") "a new PowerShell runs $V1 ($($shells[0]))"+ Check ($shells[1] -eq "macrod $V1") "a new cmd runs $V1 ($($shells[1]))"++ & $installer+ $entries = ([Environment]::GetEnvironmentVariable('Path', 'User') -split ';') | Where-Object { $_ -eq $installDir }+ Check (@($entries).Count -eq 1) 'a second install adds no second PATH entry'++ # Stage v2 the way the running macrod does: verified core into+ # versions\<tag>.partial, then renamed into place.+ $launcherHash = (Get-FileHash $launcher).Hash+ $unpacked = Join-Path $work 'v2'+ Expand-Archive (Join-Path $Releases "macrod-$V2-windows-x86_64.zip") $unpacked+ $partial = Join-Path $installDir "versions\$V2.partial"+ New-Item -ItemType Directory -Path $partial | Out-Null+ Copy-Item (Join-Path $unpacked 'macrod.exe') (Join-Path $partial 'macrod-core.exe')+ Check ((& $launcher --version) -eq "macrod $V1") 'a partial version is never launched'+ Rename-Item $partial $V2++ $shells = Version-In-New-Shells+ Check ($shells[0] -eq "macrod $V2") "after the update a new PowerShell runs $V2"+ Check ($shells[1] -eq "macrod $V2") "after the update a new cmd runs $V2"+ Check ((Get-FileHash $launcher).Hash -eq $launcherHash) 'the launcher itself is unchanged'+ Check (-not (Test-Path (Join-Path $installDir "versions\$V1"))) "the launcher pruned $V1"++ # clap rejects an unknown flag with exit code 2.+ & $launcher --not-a-macrod-flag 2>$null | Out-Null+ Check ($LASTEXITCODE -eq 2) "the launcher passes the core's exit code through ($LASTEXITCODE)"+ Write-Host 'all Windows checks passed'+} finally {+ [Environment]::SetEnvironmentVariable('Path', $userPathBefore, 'User')+ Remove-Item $work -Recurse -Force -ErrorAction SilentlyContinue+}+# The deliberate failure above leaves $LASTEXITCODE at 2, which pwsh would+# report as the step's exit code.+exit 0diff --git a/crates/coding_agent_worker/e2e/windows_update_test.ps1 b/crates/coding_agent_worker/e2e/windows_update_test.ps1new file mode 100644index 0000000000..5536b76e91--- /dev/null+++ b/crates/coding_agent_worker/e2e/windows_update_test.ps1@@ -0,0 +1,200 @@+# The Windows self-update end to end, with a real running macrod: install v1+# with install.ps1 from an HTTP mirror, start the control panel, publish a v2+# whose checksum is wrong and check nothing is installed, then publish v2 and+# check the running v1 downloads, verifies and installs it beside itself+# while it keeps running. Stopping v1 and launching again must run v2 and+# prune v1: on Windows an update is used from the next launch.+#+# pwsh windows_update_test.ps1 -Releases DIR -V1 TAG -V2 TAG+param(+ [Parameter(Mandatory)] [string] $Releases,+ [Parameter(Mandatory)] [string] $V1,+ [Parameter(Mandatory)] [string] $V2+)+$ErrorActionPreference = 'Stop'+$installer = Join-Path $PSScriptRoot '..\install\install.ps1'+$work = Join-Path ([IO.Path]::GetTempPath()) ([Guid]::NewGuid())+$mirror = Join-Path $work 'mirror'+$installDir = Join-Path $work 'macrod'+$runDir = Join-Path $work 'run'+$requests = Join-Path $work 'requests.log'+New-Item -ItemType Directory -Path $mirror, $runDir | Out-Null+New-Item -ItemType File -Path $requests | Out-Null++function Check($condition, $message) {+ if (-not $condition) { throw "FAIL: $message" }+ Write-Host "ok: $message"+}+function Archive($tag) { "macrod-$tag-windows-x86_64.zip" }+function Publish($tag, $checksum) {+ $name = Archive $tag+ Copy-Item (Join-Path $Releases $name) $mirror -Force+ $digest = if ($checksum) { $checksum } else { (Get-FileHash (Join-Path $mirror $name)).Hash.ToLower() }+ Set-Content (Join-Path $mirror "$name.sha256") "$digest $name"+ @{ tag_name = $tag; assets = @() } | ConvertTo-Json | Set-Content (Join-Path $mirror 'latest.json')+ Write-Host "published $tag to the mirror"+}+function Wait-For($description, [scriptblock] $condition, $seconds) {+ $deadline = (Get-Date).AddSeconds($seconds)+ while ((Get-Date) -lt $deadline) {+ if (& $condition) { return }+ Start-Sleep -Milliseconds 500+ }+ throw "FAIL: timed out waiting for $description"+}+function Requests-For($name) {+ @(Get-Content $requests | Where-Object { $_ -like "*/$name" }).Count+}+# The update log's events in order, timestamps dropped.+function Update-Log {+ Get-Content (Join-Path $env:HOME '.macrod\logs\update.log') | ForEach-Object { $_ | ConvertFrom-Json }+}+# Each expected event appears in this order, with these fields.+function Check-Log-In-Order($expected) {+ $events = @(Update-Log)+ $position = 0+ foreach ($want in $expected) {+ while ($position -lt $events.Count -and+ @($want.Keys | Where-Object { $events[$position].$_ -ne $want[$_] }).Count -gt 0) {+ $position+++ }+ Check ($position -lt $events.Count) "the update log has $($want | ConvertTo-Json -Compress)"+ $position+++ }+}+function Cores {+ @(Get-CimInstance Win32_Process -Filter "Name = 'macrod-core.exe'" |+ Where-Object { $_.ExecutablePath -like "$installDir*" })+}++# The mirror over HTTP, logging every request path, so the log shows what+# the running macrod fetched.+$listener = [Net.HttpListener]::new()+$port = Get-Random -Minimum 20000 -Maximum 40000+$listener.Prefixes.Add("http://127.0.0.1:$port/")+$listener.Start()+$server = Start-ThreadJob -ArgumentList $listener, $mirror, $requests -ScriptBlock {+ param($listener, $mirror, $requests)+ while ($listener.IsListening) {+ $context = $listener.GetContext()+ $path = $context.Request.Url.AbsolutePath+ Add-Content $requests "$(Get-Date -Format o) GET $path"+ $file = Join-Path $mirror ($path.TrimStart('/'))+ if (Test-Path -LiteralPath $file -PathType Leaf) {+ $bytes = [IO.File]::ReadAllBytes($file)+ $context.Response.ContentLength64 = $bytes.Length+ $context.Response.OutputStream.Write($bytes, 0, $bytes.Length)+ } else {+ $context.Response.StatusCode = 404+ }+ $context.Response.Close()+ }+}+$mirrorUrl = "http://127.0.0.1:$port"++$userPathBefore = [Environment]::GetEnvironmentVariable('Path', 'User')+$processes = @()+try {+ Publish $V1+ $env:MACROD_UPDATE_SOURCE = $mirrorUrl+ $env:MACROD_INSTALL_DIR = $installDir+ & $installer+ $launcher = Join-Path $installDir 'macrod.exe'+ Check ((& $launcher --version) -eq "macrod $V1") "install.ps1 installed $V1 from the HTTP mirror"++ # A paired-looking config against an unreachable deployment: the panel+ # serves nothing, but it runs, and its update check runs.+ @"+[macro]+api_url = "http://127.0.0.1:9"+storage_url = "http://127.0.0.1:9"+web_url = "http://127.0.0.1:9"++[identity]+scope = "private"++[harness]+command = "cmd"++[workspace]+path = "."++[credentials]+harness_id = "00000000-0000-0000-0000-0000004a125a"+token = "mhns_windows_update_test"+scope = "user"+"@ | Set-Content (Join-Path $runDir 'macrod.toml')+ $env:MACROD_UPDATE_CHECK_SECONDS = '2'+ # macrod keeps ~/.macrod (and its update log) under HOME when it is set.+ $env:HOME = Join-Path $work 'home'++ # Its own console, as a user starts it from a terminal. (PowerShell+ # names are case-insensitive: $v1 would overwrite the $V1 tag.)+ $firstLaunch = Start-Process -FilePath $launcher -WorkingDirectory $runDir -PassThru+ $processes += $firstLaunch+ Wait-For "the $V1 core to start" { (Cores).Count -eq 1 } 30+ $v1Core = (Cores)[0]+ Check ($v1Core.ExecutablePath -like "*\versions\$V1\macrod-core.exe") "the launcher started $V1 (pid $($v1Core.ProcessId), $($v1Core.ExecutablePath))"+ Wait-For "$V1 to check the mirror" { (Requests-For 'latest.json') -ge 1 } 30+ Write-Host "running $V1 checked the mirror"++ # A tampered release: fetched, rejected, nothing installed.+ Publish $V2 ('0' * 64)+ Wait-For "$V1 to fetch the tampered $V2" { (Requests-For (Archive $V2)) -ge 1 } 30+ $seen = Requests-For (Archive $V2)+ Wait-For "$V1 to retry the tampered $V2" { (Requests-For (Archive $V2)) -gt $seen } 30+ Check (-not (Test-Path (Join-Path $installDir "versions\$V2"))) "a $V2 with a wrong checksum is not installed"+ Check (-not $firstLaunch.HasExited) "$V1 keeps running after the rejected update"++ # The real release: downloaded, verified, installed beside the running v1.+ Publish $V2+ Wait-For "$V1 to install $V2" { Test-Path (Join-Path $installDir "versions\$V2\macrod-core.exe") } 60+ Check (-not (Test-Path (Join-Path $installDir "versions\$V2.partial"))) "$V2 was committed, not left partial"+ $installedVersion = & (Join-Path $installDir "versions\$V2\macrod-core.exe") --version+ Check ($installedVersion -eq "macrod $V2") "the installed core is $V2 ($installedVersion)"+ Check (-not $firstLaunch.HasExited) "$V1 keeps running after installing $V2"+ $running = Cores+ Check ($running.Count -eq 1 -and $running[0].ProcessId -eq $v1Core.ProcessId) "the session process is still $V1 (pid $($v1Core.ProcessId)) until the next launch"++ Write-Host '--- mirror requests ---'+ Get-Content $requests | ForEach-Object { Write-Host $_ }++ # The next launch runs v2 and prunes v1.+ Stop-Process -Id $v1Core.ProcessId -Force+ Wait-For "$V1 to exit" { $firstLaunch.HasExited } 30+ Check ((& $launcher --version) -eq "macrod $V2") "after the update the launcher runs $V2"+ $secondLaunch = Start-Process -FilePath $launcher -WorkingDirectory $runDir -PassThru+ $processes += $secondLaunch+ Wait-For "the $V2 core to start" { (Cores).Count -eq 1 } 30+ $v2Core = (Cores)[0]+ Check ($v2Core.ExecutablePath -like "*\versions\$V2\macrod-core.exe") "the next launch started $V2 (pid $($v2Core.ProcessId), $($v2Core.ExecutablePath))"+ Wait-For "the launcher to prune $V1" { -not (Test-Path (Join-Path $installDir "versions\$V1")) } 30+ Check $true "the launcher pruned $V1"+ Check-Log-In-Order @(+ @{ event = 'startup'; version = $V1; from_handoff = $false },+ @{ event = 'check'; current = $V1; latest = $V2; outcome = 'newer' },+ @{ event = 'download'; version = $V2; checksum_ok = $false },+ @{ event = 'download'; version = $V2; checksum_ok = $true; result = 'staged' },+ @{ event = 'apply'; from = $V1; to = $V2; method = 'next-launch'; result = 'installed for the next launch' },+ @{ event = 'startup'; version = $V2; from_handoff = $false }+ )+ Write-Host '--- update log ---'+ Get-Content (Join-Path $env:HOME '.macrod\logs\update.log') | ForEach-Object { Write-Host $_ }+ Write-Host "self-update $V1 -> $V2 passed"+} catch {+ Write-Host "error: $_"+ Write-Host $_.ScriptStackTrace+ Write-Host '--- mirror requests ---'+ Get-Content $requests -ErrorAction SilentlyContinue | ForEach-Object { Write-Host $_ }+ throw+} finally {+ $ids = @($processes | ForEach-Object { $_.Id }) + @(Cores | ForEach-Object { $_.ProcessId })+ foreach ($id in ($ids | Where-Object { $_ })) {+ Stop-Process -Id $id -Force -ErrorAction SilentlyContinue+ }+ $listener.Stop()+ Remove-Job $server -Force -ErrorAction SilentlyContinue+ [Environment]::SetEnvironmentVariable('Path', $userPathBefore, 'User')+ Remove-Item $work -Recurse -Force -ErrorAction SilentlyContinue+}+exit 0diff --git a/crates/coding_agent_worker/install/install.ps1 b/crates/coding_agent_worker/install/install.ps1index da45ad024a..ba95b78035 100644--- a/crates/coding_agent_worker/install/install.ps1+++ b/crates/coding_agent_worker/install/install.ps1@@ -2,9 +2,16 @@## irm https://raw.githubusercontent.com/macro-inc/macro/main/crates/coding_agent_worker/install/install.ps1 | iex#-# Run it again to update. MACROD_VERSION pins a release tag (v2026.10.18.0);-# MACROD_INSTALL_DIR overrides where the binary goes-# (%LOCALAPPDATA%\Programs\macrod), which is added to the user PATH.+# A running macrod downloads updates itself and uses them from its next+# launch; run this again to update right away.+#+# The install folder holds macrod.exe, a launcher that runs the newest+# versions\<tag>\macrod-core.exe. The folder is added to the user PATH once.+#+# MACROD_VERSION a release tag to install (v2026.10.18.0); default latest+# MACROD_INSTALL_DIR the install folder; default %LOCALAPPDATA%\Programs\macrod+# MACROD_UPDATE_SOURCE a mirror (URL or folder) holding latest.json and the+# release archives, instead of GitHub releases# A script block, so `irm | iex` leaves no variables behind in the caller's# session and a failure does not close it.@@ -19,50 +26,82 @@$installDir = if ($env:MACROD_INSTALL_DIR) { $env:MACROD_INSTALL_DIR }else { Join-Path $env:LOCALAPPDATA 'Programs\macrod' }$version = if ($env:MACROD_VERSION) { $env:MACROD_VERSION } else { 'latest' }+ $source = if ($env:MACROD_UPDATE_SOURCE) { $env:MACROD_UPDATE_SOURCE.TrimEnd('/', '\') } else { $null }if (-not [Environment]::Is64BitOperatingSystem) {throw 'macrod install: 64-bit Windows is required'}- # The x86_64 build also runs on Windows on Arm, under emulation.- if ($version -eq 'latest') {- $asset = 'macrod-windows-x86_64.zip'- $url = "https://github.com/$repo/releases/latest/download/$asset"- } else {- $asset = "macrod-$version-windows-x86_64.zip"- $url = "https://github.com/$repo/releases/download/$version/$asset"++ # A URL, or a file in a folder mirror.+ function Get-File($from, $to) {+ if ($from -match '^https?://') {+ Invoke-WebRequest -UseBasicParsing -Uri $from -OutFile $to+ } else {+ Copy-Item -LiteralPath $from -Destination $to+ }}$work = Join-Path ([IO.Path]::GetTempPath()) ([Guid]::NewGuid())New-Item -ItemType Directory -Path $work | Out-Nulltry {+ # The x86_64 build also runs on Windows on Arm, under emulation.+ if ($source) {+ if ($version -eq 'latest') {+ $manifest = Join-Path $work 'latest.json'+ Get-File "$source/latest.json" $manifest+ $version = (Get-Content $manifest -Raw | ConvertFrom-Json).tag_name+ if (-not $version) { throw "macrod install: $source/latest.json names no tag_name" }+ }+ $asset = "macrod-$version-windows-x86_64.zip"+ $url = "$source/$asset"+ } elseif ($version -eq 'latest') {+ $asset = 'macrod-windows-x86_64.zip'+ $url = "https://github.com/$repo/releases/latest/download/$asset"+ } else {+ $asset = "macrod-$version-windows-x86_64.zip"+ $url = "https://github.com/$repo/releases/download/$version/$asset"+ }+Write-Host "Downloading $url"$archive = Join-Path $work $asset- Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $archive- Invoke-WebRequest -UseBasicParsing -Uri "$url.sha256" -OutFile "$archive.sha256"+ Get-File $url $archive+ Get-File "$url.sha256" "$archive.sha256"$expected = ((Get-Content "$archive.sha256" -Raw).Trim() -split '\s+')[0]$actual = (Get-FileHash $archive -Algorithm SHA256).Hashif ($actual -ne $expected) { throw "macrod install: checksum mismatch for $asset" }Expand-Archive -Path $archive -DestinationPath $work -Force- New-Item -ItemType Directory -Path $installDir -Force | Out-Null- $target = Join-Path $installDir 'macrod.exe'- # A running macrod.exe cannot be overwritten, but it can be renamed- # aside; it keeps running from there until it restarts.- if (Test-Path $target) {- Remove-Item "$target.old" -Force -ErrorAction SilentlyContinue- Move-Item $target "$target.old" -Force+ $binary = Join-Path $work 'macrod.exe'+ # The binary names its own release: `macrod v2026.10.18.0`.+ $tag = ((& $binary --version) -split '\s+')[-1]+ if ($tag -notmatch '^v\d') { throw "macrod install: unexpected version '$tag'" }++ $versionDir = Join-Path $installDir "versions\$tag"+ New-Item -ItemType Directory -Path $versionDir -Force | Out-Null+ Copy-Item $binary (Join-Path $versionDir 'macrod-core.exe') -Force++ # The launcher is the same binary. A running launcher cannot be+ # overwritten, but it can be renamed aside; the next install removes it.+ $launcher = Join-Path $installDir 'macrod.exe'+ Remove-Item "$launcher.old" -Force -ErrorAction SilentlyContinue+ if (Test-Path $launcher) {+ Move-Item $launcher "$launcher.old" -Force}- Copy-Item (Join-Path $work 'macrod.exe') $target+ Copy-Item $binary $launcher$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')- if (-not (($userPath -split ';') -contains $installDir)) {- $newPath = if ($userPath) { "$userPath;$installDir" } else { $installDir }- [Environment]::SetEnvironmentVariable('Path', $newPath, 'User')+ $entries = if ($userPath) { $userPath -split ';' | Where-Object { $_ } } else { @() }+ if ($entries -notcontains $installDir) {+ [Environment]::SetEnvironmentVariable('Path', (@($entries) + $installDir) -join ';', 'User')+ Write-Host "Added $installDir to your user PATH; open a new terminal to use it there."+ } else {+ Write-Host "$installDir is already on your user PATH."+ }+ if (($env:Path -split ';') -notcontains $installDir) {$env:Path = "$env:Path;$installDir"- Write-Host "Added $installDir to your PATH; open a new terminal to use it there."}- Write-Host "Installed $(& $target --version) to $target"+ Write-Host "Installed $(& $launcher --version) to $launcher"Write-Host 'Run: macrod'} finally {Remove-Item $work -Recurse -Force -ErrorAction SilentlyContinuediff --git a/crates/coding_agent_worker/install/install.sh b/crates/coding_agent_worker/install/install.shindex d4bc06b240..4a20936d06 100644--- a/crates/coding_agent_worker/install/install.sh+++ b/crates/coding_agent_worker/install/install.sh@@ -3,21 +3,46 @@## curl -fsSL https://raw.githubusercontent.com/macro-inc/macro/main/crates/coding_agent_worker/install/install.sh | sh#-# Run it again to update. MACROD_VERSION pins a release tag (v2026.10.18.0);-# MACROD_INSTALL_DIR overrides where the binary goes (~/.local/bin).+# A running macrod updates itself; run this again to update right away.+#+# Options (flags after `sh -s --` when piped, or environment variables):+# --no-modify-path / MACROD_NO_MODIFY_PATH=1 leave shell startup files alone+# MACROD_VERSION a release tag to install (v2026.10.18.0); default latest+# MACROD_INSTALL_DIR where the binary goes; default ~/.local/bin+# MACROD_UPDATE_SOURCE a mirror (URL or directory) holding latest.json and+# the release archives, instead of GitHub releasesset -eurepo="macro-inc/macro"install_dir="${MACROD_INSTALL_DIR:-$HOME/.local/bin}"version="${MACROD_VERSION:-latest}"+source="${MACROD_UPDATE_SOURCE:-}"+modify_path=1+case "${MACROD_NO_MODIFY_PATH:-}" in+ "" | 0 | false) ;;+ *) modify_path=0 ;;+esac+for arg in "$@"; do+ case "$arg" in+ --no-modify-path) modify_path=0 ;;+ *)+ echo "macrod install: unknown option $arg" >&2+ exit 2+ ;;+ esac+done++# The line every startup file gets, and the comment that marks it as ours so+# a re-run never adds it twice.+marker="# Added by the macrod installer"fail() {echo "macrod install: $*" >&2exit 1}-command -v curl >/dev/null 2>&1 || fail "curl is required"+command -v curl >/dev/null 2>&1 || [ -n "$source" ] || fail "curl is required"case "$(uname -s)" inLinux) os=linux ;;@@ -35,7 +60,29 @@ if [ "$os" = macos ] && [ "$arch" = x86_64 ] && [ "$(sysctl -n hw.optional.arm64arch=aarch64fi-if [ "$version" = latest ]; then+# Fetch $1 (a URL, or a path inside a directory mirror) to the file $2.+fetch() {+ case "$1" in+ https://*) curl -fsSL --proto '=https' --tlsv1.2 -o "$2" "$1" ;;+ # Mirrors are for testing; a local one may be plain http.+ http://*) curl -fsSL -o "$2" "$1" ;;+ *) cp "$1" "$2" ;;+ esac+}++work=$(mktemp -d)+trap 'rm -rf "$work"' EXIT++if [ -n "$source" ]; then+ source="${source%/}"+ if [ "$version" = latest ]; then+ fetch "$source/latest.json" "$work/latest.json" || fail "could not read $source/latest.json"+ version=$(sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$work/latest.json" | head -n 1)+ [ -n "$version" ] || fail "$source/latest.json names no tag_name"+ fi+ asset="macrod-$version-$os-$arch.tar.gz"+ url="$source/$asset"+elif [ "$version" = latest ]; thenasset="macrod-$os-$arch.tar.gz"url="https://github.com/$repo/releases/latest/download/$asset"else@@ -43,22 +90,23 @@ elseurl="https://github.com/$repo/releases/download/$version/$asset"fi-work=$(mktemp -d)-trap 'rm -rf "$work"' EXIT-echo "Downloading $url"-curl -fsSL --proto '=https' --tlsv1.2 -o "$work/$asset" "$url" ||+fetch "$url" "$work/$asset" ||fail "download failed; is $os-$arch published for $version?"-curl -fsSL --proto '=https' --tlsv1.2 -o "$work/$asset.sha256" "$url.sha256" ||+fetch "$url.sha256" "$work/$asset.sha256" ||fail "checksum download failed"(cd "$work"+ # The checksum file names the archive it was made from; check the digest+ # against the file downloaded here, whatever it was called upstream.+ expected=$(cut -d ' ' -f 1 "$asset.sha256")if command -v sha256sum >/dev/null 2>&1; then- sha256sum -c "$asset.sha256" >/dev/null+ actual=$(sha256sum "$asset" | cut -d ' ' -f 1)else- shasum -a 256 -c "$asset.sha256" >/dev/null+ actual=$(shasum -a 256 "$asset" | cut -d ' ' -f 1)fi+ [ -n "$expected" ] && [ "$expected" = "$actual" ]) || fail "checksum mismatch for $asset"tar -xzf "$work/$asset" -C "$work" macrod@@ -68,11 +116,47 @@ install -m 755 "$work/macrod" "$install_dir/.macrod.new"mv -f "$install_dir/.macrod.new" "$install_dir/macrod"echo "Installed $("$install_dir/macrod" --version) to $install_dir/macrod"++# Append $2 to the startup file $1 once; say what changed.+add_to_startup_file() {+ if [ -f "$1" ] && grep -qF "$marker" "$1"; then+ echo "$1 already adds $install_dir to PATH"+ return+ fi+ mkdir -p "$(dirname "$1")"+ printf '\n%s\n%s\n' "$marker" "$2" >>"$1"+ echo "Added $install_dir to PATH in $1"+ changed=1+}++changed=0+if [ "$modify_path" = 1 ]; then+ posix_line="export PATH=\"$install_dir:\$PATH\""+ case "$(basename "${SHELL:-sh}")" in+ zsh) add_to_startup_file "${ZDOTDIR:-$HOME}/.zshrc" "$posix_line" ;;+ bash)+ add_to_startup_file "$HOME/.bashrc" "$posix_line"+ # Login shells (every macOS Terminal window) read .bash_profile, which+ # does not always source .bashrc.+ if [ "$os" = macos ] || [ -f "$HOME/.bash_profile" ]; then+ add_to_startup_file "$HOME/.bash_profile" "$posix_line"+ fi+ ;;+ fish)+ add_to_startup_file "${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/macrod.fish" \+ "fish_add_path --path \"$install_dir\""+ ;;+ *) add_to_startup_file "$HOME/.profile" "$posix_line" ;;+ esac+fi+case ":$PATH:" in*":$install_dir:"*) echo "Run: macrod" ;;*)- echo "$install_dir is not on your PATH. Add it, e.g.:"- echo " echo 'export PATH=\"$install_dir:\$PATH\"' >> ~/.profile"- echo "or run: $install_dir/macrod"+ if [ "$changed" = 1 ] || [ "$modify_path" = 1 ]; then+ echo "Open a new terminal, then run: macrod"+ else+ echo "$install_dir is not on your PATH; run: $install_dir/macrod"+ fi;;esacdiff --git a/crates/coding_agent_worker/src/config.rs b/crates/coding_agent_worker/src/config.rsindex aebb55bfad..ff36540b70 100644--- a/crates/coding_agent_worker/src/config.rs+++ b/crates/coding_agent_worker/src/config.rs@@ -38,6 +38,23 @@ pub struct Config {/// Native Herdr defaults shared by this macrod instance.#[serde(default)]pub herdr: HerdrSettings,+ /// Whether a released macrod keeps itself up to date.+ #[serde(default)]+ pub update: UpdateSettings,+}++/// Self-update preferences. `MACROD_NO_UPDATE=1` turns updating off too.+#[derive(Debug, Clone, Deserialize)]+#[serde(default, deny_unknown_fields)]+pub struct UpdateSettings {+ /// Install newer releases automatically. On by default.+ pub auto: bool,+}++impl Default for UpdateSettings {+ fn default() -> Self {+ Self { auto: true }+ }}/// One native launch configuration, shared by all new sessions.diff --git a/crates/coding_agent_worker/src/config/test.rs b/crates/coding_agent_worker/src/config/test.rsindex 138a3dc210..a034faf552 100644--- a/crates/coding_agent_worker/src/config/test.rs+++ b/crates/coding_agent_worker/src/config/test.rs@@ -137,3 +137,13 @@ fn the_gateway_url_is_the_api_base_with_a_websocket_scheme() {"https://macro.com/app/settings/harness?pair=KX7M-4QHD",);}++#[test]+fn updates_are_on_unless_the_config_turns_them_off() {+ let config: Config = toml::from_str(EXAMPLE).expect("example config parses");+ assert!(config.update.auto);++ let opted_out: Config =+ toml::from_str(&format!("{EXAMPLE}\n[update]\nauto = false\n")).expect("opt-out parses");+ assert!(!opted_out.update.auto);+}diff --git a/crates/coding_agent_worker/src/daemon.rs b/crates/coding_agent_worker/src/daemon.rsindex 79edd5aa2b..6252688caa 100644--- a/crates/coding_agent_worker/src/daemon.rs+++ b/crates/coding_agent_worker/src/daemon.rs@@ -11,10 +11,13 @@ use tokio_util::sync::CancellationToken;use crate::config::{Config, HarnessCredentials};use crate::dispatch::Dispatcher;+use crate::harness::{HandoffSender, Handoffs};+use crate::outbound::acp_process::RunningAgent;use crate::outbound::agent_session::HarnessApi;use crate::outbound::stream::EventStreamClient;use crate::runtime::Runtime;use crate::trigger::{TriggerEvent, handle_event, trigger_filters};+use crate::turns::ActiveTurns;#[cfg(test)]mod test;@@ -29,8 +32,24 @@ const BOUND_AGENT_REFRESH: Duration = Duration::from_secs(10);pub struct Daemon {cancel: CancellationToken,task: tokio::task::JoinHandle<rootcause::Result<()>>,+ turns: ActiveTurns,+ #[cfg_attr(+ not(unix),+ expect(dead_code, reason = "agents are handed off only on Unix")+ )]+ handoff: HandoffSender,}+/// The agent a previous macrod process image handed over, ready to relay.+#[cfg(unix)]+pub type InheritedAgent = crate::handoff::HandedOffAgent;+#[cfg(not(unix))]+pub type InheritedAgent = std::convert::Infallible;++/// How long a handoff may take before macrod gives up on keeping the agent.+#[cfg(unix)]+const HANDOFF_TIMEOUT: Duration = Duration::from_secs(30);+impl Daemon {/// Connect the runtime and listen for agent triggers in background tasks.///@@ -40,6 +59,7 @@ impl Daemon {mut config: Config,credentials: HarnessCredentials,_config_path: &Path,+ inherited: Option<InheritedAgent>,) -> rootcause::Result<Self> {let cancel = CancellationToken::new();let client = EventStreamClient::new(&config.macro_api, &credentials);@@ -69,12 +89,18 @@ impl Daemon {None};+ let turns = ActiveTurns::default();+ let adopted = adopt(inherited, &turns);+ let (handoffs, handoff) = Handoffs::channel();let runtime = Runtime::start(&config.macro_api,&credentials,config.harness.clone(),&config.workspace.path,tap,+ turns.clone(),+ adopted,+ handoffs,);let executor = Dispatcher::new(api, runtime, config.workspace.clone());#[cfg(unix)]@@ -119,7 +145,12 @@ impl Daemon {}});- Ok(Self { cancel, task })+ Ok(Self {+ cancel,+ task,+ turns,+ handoff,+ })}/// Whether the serving task is still alive.@@ -127,6 +158,31 @@ impl Daemon {!self.task.is_finished()}+ /// Whether no agent turn is running.+ pub fn is_idle(&self) -> bool {+ self.turns.is_idle()+ }++ /// Stop serving but keep the agent process running, handing it back so+ /// the next process image can relay it. `None` when no agent is running+ /// or it could not be detached in time; the agent is then stopped.+ #[cfg(unix)]+ pub async fn hand_off(self) -> Option<InheritedAgent> {+ let (reply, handed) = tokio::sync::oneshot::channel();+ let asked = self.handoff.sender.send(reply).await.is_ok();+ let agent = if asked {+ tokio::time::timeout(HANDOFF_TIMEOUT, handed)+ .await+ .ok()+ .and_then(Result::ok)+ .flatten()+ } else {+ None+ };+ self.stop().await;+ agent+ }+/// Stop serving without waiting for an in-flight network request or trigger.////// Dropping the dispatcher also stops its runtime supervisor, WebSocket,@@ -139,6 +195,24 @@ impl Daemon {}}+/// Relay an inherited agent instead of spawning one, continuing its turns.+#[cfg(unix)]+fn adopt(inherited: Option<InheritedAgent>, turns: &ActiveTurns) -> Option<RunningAgent> {+ let agent = inherited?;+ match RunningAgent::adopt(agent, turns) {+ Ok(agent) => Some(agent),+ Err(error) => {+ tracing::warn!(error = %error, "could not relay the inherited agent; starting a new one");+ None+ }+ }+}++#[cfg(not(unix))]+fn adopt(inherited: Option<InheritedAgent>, _turns: &ActiveTurns) -> Option<RunningAgent> {+ inherited.map(|never| match never {})+}+/// Inside herdr, the directory macrod was started from is the repository/// every session works in, and each session gets a herdr window.#[cfg(unix)]diff --git a/crates/coding_agent_worker/src/daemon/test.rs b/crates/coding_agent_worker/src/daemon/test.rsindex 03c9247777..beeb72f496 100644--- a/crates/coding_agent_worker/src/daemon/test.rs+++ b/crates/coding_agent_worker/src/daemon/test.rs@@ -5,6 +5,8 @@ async fn stop_does_not_wait_for_in_flight_work() {let daemon = Daemon {cancel: CancellationToken::new(),task: tokio::spawn(std::future::pending()),+ turns: ActiveTurns::default(),+ handoff: crate::harness::Handoffs::channel().1,};tokio::time::timeout(Duration::from_secs(1), daemon.stop())@@ -46,7 +48,7 @@ async fn startup_without_agents_serves_model_probes_and_stop_closes_the_runtime(assert!(String::from_utf8_lossy(&request[..read]).contains("/harnesses/me/agents"));socket.write_all(b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 2\r\nConnection: close\r\n\r\n[]").await.unwrap();});- let daemon = Daemon::start(config, credentials, Path::new("macrod.toml"))+ let daemon = Daemon::start(config, credentials, Path::new("macrod.toml"), None).await.unwrap();let (socket, _) = tokio::time::timeout(Duration::from_secs(2), gateway.accept())@@ -113,3 +115,156 @@ async fn startup_without_agents_serves_model_probes_and_stop_closes_the_runtime("stopped daemon must not reconnect");}++/// A runtime gateway and storage stand-in for one daemon: storage answers+/// every request with no bound agents.+#[cfg(unix)]+async fn serve_storage(storage: tokio::net::TcpListener) {+ use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};+ loop {+ let Ok((mut socket, _)) = storage.accept().await else {+ return;+ };+ tokio::spawn(async move {+ let mut request = [0; 4096];+ let _ = socket.read(&mut request).await;+ let _ = socket.write_all(b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 2\r\nConnection: close\r\n\r\n[]").await;+ });+ }+}++#[cfg(unix)]+#[tokio::test]+async fn a_handed_off_agent_keeps_running_and_streaming_under_the_next_daemon() {+ use agent_runtime_protocol::domain::schema::v0::{+ AcpMessage, SystemEvent, ToRuntimeMessage, ToServerMessage,+ };+ use futures::{SinkExt as _, StreamExt as _};+ use tokio::net::TcpListener;+ use tokio_tungstenite::tungstenite::Message;++ let gateway = TcpListener::bind("127.0.0.1:0").await.unwrap();+ let storage = TcpListener::bind("127.0.0.1:0").await.unwrap();+ let mut config: Config = toml::from_str(include_str!("../../config.example.toml")).unwrap();+ config.macro_api.api_url = format!("http://{}", gateway.local_addr().unwrap());+ config.macro_api.storage_url = format!("http://{}", storage.local_addr().unwrap());+ config.workspace.path = std::env::current_dir().unwrap();+ config.harness.command = "python3".to_owned();+ config.harness.args = vec!["-c".to_owned(), include_str!("test_ticker.py").to_owned()];+ let credentials = HarnessCredentials {+ harness_id: harness_id::HarnessId::TEST_A,+ token: "mhns_test".to_owned(),+ scope: crate::config::HarnessScope::User,+ };+ tokio::spawn(serve_storage(storage));++ /// Read frames into `ticks` (`(n, pid)`) until `limit` new ticks arrived+ /// or `until` accepts a frame.+ async fn read_ticks(+ socket: &mut tokio_tungstenite::WebSocketStream<tokio::net::TcpStream>,+ ticks: &mut Vec<(u64, u64)>,+ limit: usize,+ mut until: impl FnMut(&ToServerMessage) -> bool,+ ) {+ let start = ticks.len();+ tokio::time::timeout(Duration::from_secs(10), async {+ while let Some(Ok(frame)) = socket.next().await {+ let Message::Text(text) = frame else { continue };+ let message: ToServerMessage = serde_json::from_str(&text).unwrap();+ if let ToServerMessage::Acp(AcpMessage(raw)) = &message {+ let value = serde_json::to_value(raw).unwrap();+ if value["method"] == "tick" {+ ticks.push((+ value["params"]["n"].as_u64().unwrap(),+ value["params"]["pid"].as_u64().unwrap(),+ ));+ }+ }+ if until(&message) || ticks.len() - start >= limit {+ return;+ }+ }+ })+ .await+ .expect("the gateway kept receiving frames");+ }++ let first = Daemon::start(+ config.clone(),+ credentials.clone(),+ Path::new("macrod.toml"),+ None,+ )+ .await+ .unwrap();+ let (socket, _) = gateway.accept().await.unwrap();+ let mut socket = tokio_tungstenite::accept_async(socket).await.unwrap();+ let mut ticks = Vec::new();+ read_ticks(&mut socket, &mut ticks, 50, |_| false).await;++ // Hand off while the agent streams. The first socket carries every line+ // read before the stop, then the disconnect, then closes.+ let handing_off = tokio::spawn(first.hand_off());+ let mut disconnected = false;+ read_ticks(&mut socket, &mut ticks, usize::MAX, |message| {+ disconnected = matches!(+ message,+ ToServerMessage::Event {+ event: SystemEvent::Disconnected+ }+ );+ disconnected+ })+ .await;+ assert!(disconnected, "the handoff announces the disconnect");+ let agent = handing_off+ .await+ .unwrap()+ .expect("the running agent is handed off");+ let handed_pid = u64::try_from(agent.pid().as_raw_nonzero().get()).unwrap();++ let second = Daemon::start(config, credentials, Path::new("macrod.toml"), Some(agent))+ .await+ .unwrap();+ let (socket, _) = gateway.accept().await.unwrap();+ let mut socket = tokio_tungstenite::accept_async(socket).await.unwrap();+ read_ticks(&mut socket, &mut ticks, 50, |_| false).await;++ // No tick lost or repeated across the two relays, and one agent process+ // produced them all.+ for (index, (n, _)) in ticks.iter().enumerate() {+ assert_eq!(+ *n, index as u64,+ "tick {index} arrived out of order or was lost"+ );+ }+ assert!(ticks.iter().all(|(_, pid)| *pid == handed_pid));++ // The adopted agent still takes requests.+ socket+ .send(Message::Text(+ serde_json::to_string(&ToRuntimeMessage::Acp(AcpMessage(+ serde_json::from_value(+ serde_json::json!({"jsonrpc": "2.0", "id": 1, "method": "echo"}),+ )+ .unwrap(),+ )))+ .unwrap()+ .into(),+ ))+ .await+ .unwrap();+ let mut echoed = false;+ read_ticks(&mut socket, &mut ticks, usize::MAX, |message| {+ let ToServerMessage::Acp(AcpMessage(raw)) = message else {+ return false;+ };+ let value = serde_json::to_value(raw).unwrap();+ echoed = value["id"] == 1 && value["result"]["echo"] == "echo";+ echoed+ })+ .await;+ assert!(echoed);++ second.stop().await;+}diff --git a/crates/coding_agent_worker/src/daemon/test_ticker.py b/crates/coding_agent_worker/src/daemon/test_ticker.pynew file mode 100644index 0000000000..1bb129df42--- /dev/null+++ b/crates/coding_agent_worker/src/daemon/test_ticker.py@@ -0,0 +1,29 @@+"""ACP stand-in that streams numbered notifications and answers `echo`."""++import json+import os+import sys+import threading+import time++lock = threading.Lock()+++def send(message):+ with lock:+ sys.stdout.write(json.dumps(message) + "\n")+ sys.stdout.flush()+++def tick():+ n = 0+ while True:+ send({"jsonrpc": "2.0", "method": "tick", "params": {"n": n, "pid": os.getpid()}})+ n += 1+ time.sleep(0.005)+++threading.Thread(target=tick, daemon=True).start()+for line in sys.stdin:+ request = json.loads(line)+ send({"jsonrpc": "2.0", "id": request["id"], "result": {"echo": request["method"], "pid": os.getpid()}})diff --git a/crates/coding_agent_worker/src/handoff.rs b/crates/coding_agent_worker/src/handoff.rsnew file mode 100644index 0000000000..64722bc62d--- /dev/null+++ b/crates/coding_agent_worker/src/handoff.rs@@ -0,0 +1,341 @@+//! Handing the running ACP agent from one macrod process image to the next.+//!+//! A self-update on Unix replaces macrod with `execve`. Children survive an+//! `execve`, and so do file descriptors without `FD_CLOEXEC`, so the agent+//! keeps running and the new image picks up its pipes where the old one+//! stopped: at a line boundary, with any bytes read past it carried over.+//!+//! The state travels in an unlinked temporary file whose descriptor number+//! is in [`HANDOFF_ENV`]. It is versioned: an image that cannot read it kills+//! the agent's process group and starts a fresh agent, which resumes its+//! sessions the way a reconnect always has.++use std::io::{Read as _, Seek as _, Write as _};+use std::os::fd::{AsFd as _, AsRawFd as _, FromRawFd as _, OwnedFd, RawFd};++use rustix::io::FdFlags;+use rustix::process::Pid;++#[cfg(test)]+mod test;++/// Names the inherited descriptor holding the handoff state.+pub const HANDOFF_ENV: &str = "MACROD_HANDOFF_FD";++mod environment {+ macro_env_var::maybe_env_var! {+ /// Set only by macrod when it `execve`s into an update.+ pub struct MacrodHandoffFd;+ }+}++/// Bumped whenever [`HandoffState`] changes shape.+const FORMAT_VERSION: u32 = 1;++/// A running agent, its pipes, and what the relay knew about it.+pub struct HandedOffAgent {+ parts: Option<HandedOffParts>,+}++/// The pieces of a [`HandedOffAgent`], taken by whoever relays it next.+pub struct HandedOffParts {+ pub pid: Pid,+ pub stdin: OwnedFd,+ pub stdout: OwnedFd,+ pub stderr: OwnedFd,+ /// Output read past the last relayed line; the start of the next one.+ pub pending_output: Vec<u8>,+ /// `session/prompt` request ids the agent has not answered yet.+ pub active_prompts: Vec<String>,+}++/// The serialized form. Descriptor numbers are only meaningful in the image+/// the state is handed to.+#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]+struct HandoffState {+ version: u32,+ agent: AgentRecord,+}++#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]+struct AgentRecord {+ pid: i32,+ stdin: RawFd,+ stdout: RawFd,+ stderr: RawFd,+ /// Hex, so the state stays text whatever the agent wrote.+ pending_output: String,+ active_prompts: Vec<String>,+}++/// Why handed-off state could not be used.+#[derive(Debug, thiserror::Error)]+pub enum HandoffError {+ #[error("handoff state version {found} is not {FORMAT_VERSION}")]+ Version { found: u32 },+ #[error("malformed handoff state")]+ Malformed(#[from] serde_json::Error),+ #[error("malformed pending output")]+ PendingOutput(#[from] hex::FromHexError),+ #[error("pid {0} is not a running child of this process")]+ NotAChild(i32),+ #[error(transparent)]+ Io(#[from] std::io::Error),+}++impl HandedOffAgent {+ pub fn new(+ pid: Pid,+ stdin: OwnedFd,+ stdout: OwnedFd,+ stderr: OwnedFd,+ pending_output: Vec<u8>,+ active_prompts: Vec<String>,+ ) -> Self {+ Self {+ parts: Some(HandedOffParts {+ pid,+ stdin,+ stdout,+ stderr,+ pending_output,+ active_prompts,+ }),+ }+ }++ /// Take the agent; it is no longer killed when this value drops.+ pub fn into_parts(mut self) -> HandedOffParts {+ self.parts.take().expect("parts are present until taken")+ }++ /// The agent's pid, which is also its process group.+ pub fn pid(&self) -> Pid {+ self.parts().pid+ }++ /// How many prompts were running when the agent was handed over.+ pub fn active_prompt_count(&self) -> usize {+ self.parts().active_prompts.len()+ }++ fn parts(&self) -> &HandedOffParts {+ self.parts.as_ref().expect("parts are present until taken")+ }++ fn state(&self) -> HandoffState {+ let parts = self.parts();+ HandoffState {+ version: FORMAT_VERSION,+ agent: AgentRecord {+ pid: parts.pid.as_raw_nonzero().get(),+ stdin: parts.stdin.as_raw_fd(),+ stdout: parts.stdout.as_raw_fd(),+ stderr: parts.stderr.as_raw_fd(),+ pending_output: hex::encode(&parts.pending_output),+ active_prompts: parts.active_prompts.clone(),+ },+ }+ }++ /// Make the agent's pipes and its state survive `execve`. Returns the+ /// value for [`HANDOFF_ENV`]; the state file must stay open until then.+ pub fn prepare_exec(&self) -> std::io::Result<(OwnedFd, String)> {+ let parts = self.parts();+ for fd in [&parts.stdin, &parts.stdout, &parts.stderr] {+ rustix::io::fcntl_setfd(fd.as_fd(), FdFlags::empty())?;+ }+ let state = serde_json::to_vec(&self.state()).map_err(std::io::Error::other)?;+ let file = state_file(&state)?;+ let fd = OwnedFd::from(file);+ rustix::io::fcntl_setfd(fd.as_fd(), FdFlags::empty())?;+ let number = fd.as_raw_fd().to_string();+ Ok((fd, number))+ }+}++impl HandedOffAgent {+ /// Kill the agent, as any agent whose relay ended. Joining the returned+ /// reaper gives its exit status.+ fn stop(&mut self) -> Option<Reaper> {+ self.parts.take().map(|parts| kill_and_reap(parts.pid))+ }+}++/// Without a taker, the agent is killed like any agent whose relay ended.+impl Drop for HandedOffAgent {+ fn drop(&mut self) {+ let _ = self.stop();+ }+}++/// The agent the previous process image handed over, if it handed one.+///+/// Read once, at startup: the descriptor number names nothing afterwards.+/// State this image cannot use falls back to killing the agent, so the+/// daemon starts a fresh one.+pub fn take_inherited() -> Option<HandedOffAgent> {+ let number = environment::MacrodHandoffFd::new()?.value()?.to_owned();+ let Ok(fd) = number.parse::<RawFd>() else {+ tracing::warn!(value = %number, "ignoring a malformed {HANDOFF_ENV}");+ return None;+ };+ // SAFETY: only probed, not used, until it is known to be open.+ if rustix::io::fcntl_getfd(unsafe { rustix::fd::BorrowedFd::borrow_raw(fd) }).is_err() {+ tracing::warn!(fd, "the handed-off state descriptor is not open");+ return None;+ }+ // SAFETY: the previous image opened this descriptor for us and passed+ // its number in HANDOFF_ENV; it was checked open above and nothing else+ // in this image has claimed it this early in startup.+ let file = std::fs::File::from(unsafe { OwnedFd::from_raw_fd(fd) });+ match read_state(file).and_then(adopt) {+ Ok(agent) => {+ tracing::info!(+ pid = agent.pid().as_raw_nonzero().get(),+ "picked up the running agent from the previous macrod"+ );+ Some(agent)+ }+ Err((error, fallback)) => {+ tracing::warn!(error = %error, "could not pick up the running agent; starting a new one");+ let _ = fallback.kill();+ crate::update::log::record(crate::update::log::UpdateEvent::Apply {+ from: None,+ to: crate::update::VERSION.to_owned(),+ method: crate::update::log::ApplyMethod::FreshAgentFallback,+ active_prompts: 0,+ result: format!("could not pick up the running agent: {error}"),+ });+ None+ }+ }+}++/// What to clean up when the state cannot be used.+#[derive(Default)]+struct Fallback {+ pid: Option<i32>,+ fds: Vec<RawFd>,+}++impl Fallback {+ /// Read only the fields every format version has, so a version this+ /// image does not know still has its agent stopped.+ fn from_raw(raw: &[u8]) -> Self {+ let Ok(value) = serde_json::from_slice::<serde_json::Value>(raw) else {+ return Self::default();+ };+ let agent = &value["agent"];+ let number = |field: &str| agent[field].as_i64().and_then(|n| i32::try_from(n).ok());+ Self {+ pid: number("pid"),+ fds: ["stdin", "stdout", "stderr"]+ .into_iter()+ .filter_map(number)+ .collect(),+ }+ }++ /// Kill the named agent and close its pipes. Joining the returned+ /// reaper gives the agent's exit status.+ fn kill(self) -> Option<Reaper> {+ let reaper = self.pid.and_then(Pid::from_raw).map(kill_and_reap);+ for fd in self.fds {+ // SAFETY: these descriptors were inherited for the handoff and+ // nothing in this image uses them; closing them is the cleanup.+ if rustix::io::fcntl_getfd(unsafe { rustix::fd::BorrowedFd::borrow_raw(fd) }).is_ok() {+ drop(unsafe { OwnedFd::from_raw_fd(fd) });+ }+ }+ reaper+ }+}++fn read_state(+ mut file: std::fs::File,+) -> Result<(HandoffState, Vec<u8>), (HandoffError, Fallback)> {+ let mut raw = Vec::new();+ file.read_to_end(&mut raw)+ .map_err(|error| (error.into(), Fallback::default()))?;+ match parse_state(&raw) {+ Ok(state) => Ok((state, raw)),+ Err(error) => Err((error, Fallback::from_raw(&raw))),+ }+}++fn parse_state(raw: &[u8]) -> Result<HandoffState, HandoffError> {+ #[derive(serde::Deserialize)]+ struct Versioned {+ version: u32,+ }+ let Versioned { version } = serde_json::from_slice(raw)?;+ if version != FORMAT_VERSION {+ return Err(HandoffError::Version { found: version });+ }+ Ok(serde_json::from_slice(raw)?)+}++fn adopt(+ (state, raw): (HandoffState, Vec<u8>),+) -> Result<HandedOffAgent, (HandoffError, Fallback)> {+ let fallback = || Fallback::from_raw(&raw);+ let record = state.agent;+ let pid = Pid::from_raw(record.pid)+ .ok_or_else(|| (HandoffError::NotAChild(record.pid), fallback()))?;+ // A child that is still running reports no status yet; anything else+ // (exited, or not ours) cannot be relayed.+ match rustix::process::waitpid(Some(pid), rustix::process::WaitOptions::NOHANG) {+ Ok(None) => {}+ _ => return Err((HandoffError::NotAChild(record.pid), fallback())),+ }+ let pending_output =+ hex::decode(&record.pending_output).map_err(|error| (error.into(), fallback()))?;+ let mut fds = Vec::new();+ for fd in [record.stdin, record.stdout, record.stderr] {+ // SAFETY: as for the state descriptor: inherited for this purpose.+ let borrowed = unsafe { rustix::fd::BorrowedFd::borrow_raw(fd) };+ if let Err(error) = rustix::io::fcntl_setfd(borrowed, FdFlags::CLOEXEC) {+ return Err((std::io::Error::from(error).into(), fallback()));+ }+ // SAFETY: open (fcntl succeeded) and owned by nothing else here.+ fds.push(unsafe { OwnedFd::from_raw_fd(fd) });+ }+ let [stdin, stdout, stderr] = <[OwnedFd; 3]>::try_from(fds).expect("three descriptors");+ Ok(HandedOffAgent::new(+ pid,+ stdin,+ stdout,+ stderr,+ pending_output,+ record.active_prompts,+ ))+}++/// The state in an unlinked file: nothing is left on disk whatever happens.+fn state_file(state: &[u8]) -> std::io::Result<std::fs::File> {+ let path = std::env::temp_dir().join(format!(".macrod-handoff-{}", uuid::Uuid::new_v4()));+ let mut file = std::fs::OpenOptions::new()+ .read(true)+ .write(true)+ .create_new(true)+ .open(&path)?;+ let _ = std::fs::remove_file(&path);+ file.write_all(state)?;+ file.rewind()?;+ Ok(file)+}++/// The thread reaping a killed agent; it yields the agent's exit status.+type Reaper = std::thread::JoinHandle<Option<rustix::process::WaitStatus>>;++fn kill_and_reap(pid: Pid) -> Reaper {+ let _ = rustix::process::kill_process_group(pid, rustix::process::Signal::KILL);+ // Reap it off-thread; a SIGKILLed child exits promptly.+ std::thread::spawn(move || {+ rustix::process::waitpid(Some(pid), rustix::process::WaitOptions::empty())+ .ok()+ .flatten()+ .map(|(_, status)| status)+ })+}diff --git a/crates/coding_agent_worker/src/handoff/test.rs b/crates/coding_agent_worker/src/handoff/test.rsnew file mode 100644index 0000000000..32c869bcb6--- /dev/null+++ b/crates/coding_agent_worker/src/handoff/test.rs@@ -0,0 +1,164 @@+use std::os::fd::IntoRawFd as _;++use super::*;++/// A real child in its own process group, the way `AcpProcess` spawns one,+/// with its three pipes.+fn spawn_child() -> (std::process::Child, HandedOffAgent) {+ use std::os::unix::process::CommandExt as _;++ let mut child = std::process::Command::new("cat")+ .stdin(std::process::Stdio::piped())+ .stdout(std::process::Stdio::piped())+ .stderr(std::process::Stdio::piped())+ .process_group(0)+ .spawn()+ .unwrap();+ let pid = Pid::from_raw(i32::try_from(child.id()).unwrap()).unwrap();+ let agent = HandedOffAgent::new(+ pid,+ OwnedFd::from(child.stdin.take().unwrap()),+ OwnedFd::from(child.stdout.take().unwrap()),+ OwnedFd::from(child.stderr.take().unwrap()),+ b"{\"jsonrpc\":\"2.0\",\"method\":\"session/upd".to_vec(),+ vec!["\"agent_session:1:7\"".to_owned()],+ );+ (child, agent)+}++fn is_running(pid: Pid) -> bool {+ matches!(+ rustix::process::waitpid(Some(pid), rustix::process::WaitOptions::NOHANG),+ Ok(None)+ )+}++#[test]+fn state_round_trips_through_the_inherited_descriptor() {+ let (_child, agent) = spawn_child();+ let expected = agent.state();++ let (state_fd, number) = agent.prepare_exec().unwrap();+ assert_eq!(number, state_fd.as_raw_fd().to_string());+ // Survives exec: no FD_CLOEXEC on the state or the pipes.+ for fd in [+ state_fd.as_fd(),+ agent.parts().stdin.as_fd(),+ agent.parts().stdout.as_fd(),+ agent.parts().stderr.as_fd(),+ ] {+ assert_eq!(rustix::io::fcntl_getfd(fd).unwrap(), FdFlags::empty());+ }+ let (state, _) = read_state(std::fs::File::from(state_fd))+ .map_err(|(error, _)| error)+ .unwrap();++ assert_eq!(state, expected);+ // The new image would now own the pipes; release them from this value.+ let parts = agent.into_parts();+ let _ = (+ parts.stdin.into_raw_fd(),+ parts.stdout.into_raw_fd(),+ parts.stderr.into_raw_fd(),+ );+ let adopted = adopt((state, Vec::new()))+ .map_err(|(error, _)| error)+ .unwrap();+ let parts = adopted.into_parts();++ assert_eq!(parts.pid, expected_pid(&expected));+ assert_eq!(+ parts.pending_output,+ b"{\"jsonrpc\":\"2.0\",\"method\":\"session/upd"+ );+ assert_eq!(parts.active_prompts, ["\"agent_session:1:7\""]);+ // Adopted descriptors are close-on-exec again, so later children never+ // inherit them.+ assert_eq!(+ rustix::io::fcntl_getfd(parts.stdin.as_fd()).unwrap(),+ FdFlags::CLOEXEC+ );+ kill_and_reap(parts.pid);+}++fn expected_pid(state: &HandoffState) -> Pid {+ Pid::from_raw(state.agent.pid).unwrap()+}++#[test]+fn an_unknown_format_version_is_refused_but_still_names_the_agent() {+ let raw = br#"{"version": 2, "agent": {"pid": 4242, "stdin": 7, "stdout": 8, "stderr": 9, "sessions": []}}"#;++ let error = parse_state(raw).unwrap_err();+ let fallback = Fallback::from_raw(raw);++ assert!(+ matches!(error, HandoffError::Version { found: 2 }),+ "{error:?}"+ );+ assert_eq!(fallback.pid, Some(4242));+ assert_eq!(fallback.fds, [7, 8, 9]);+}++#[test]+fn malformed_state_is_refused_with_nothing_to_clean_up() {+ let raw = b"not json";++ assert!(matches!(parse_state(raw), Err(HandoffError::Malformed(_))));+ let fallback = Fallback::from_raw(raw);+ assert_eq!(fallback.pid, None);+ assert!(fallback.fds.is_empty());+}++#[test]+fn a_pid_that_is_not_a_running_child_is_refused() {+ let state = HandoffState {+ version: FORMAT_VERSION,+ agent: AgentRecord {+ // pid 1 is never this test's child.+ pid: 1,+ stdin: -1,+ stdout: -1,+ stderr: -1,+ pending_output: String::new(),+ active_prompts: Vec::new(),+ },+ };++ let Err((error, _)) = adopt((state, Vec::new())) else {+ panic!("adopted a process that is not a child");+ };+ assert!(matches!(error, HandoffError::NotAChild(1)), "{error:?}");+}++#[test]+fn an_untaken_handoff_stops_the_agent() {+ // The reaper owns the child's exit status; this test must not wait too.+ let (_child, mut agent) = spawn_child();+ let pid = agent.parts().pid;+ assert!(is_running(pid));++ let reaper = agent.stop().expect("the agent was not taken");++ let status = reaper.join().unwrap().expect("the agent was reaped");+ assert_eq!(status.terminating_signal(), Some(9));+}++#[test]+fn the_fallback_stops_the_agent_it_names() {+ let (_child, agent) = spawn_child();+ let parts = agent.into_parts();+ let raw = serde_json::to_vec(&serde_json::json!({+ "version": 99,+ "agent": {"pid": parts.pid.as_raw_nonzero().get()},+ }))+ .unwrap();++ let reaper = Fallback::from_raw(&raw)+ .kill()+ .expect("the state names a pid");++ // The reaper owns the child's exit status; this test must not wait too.+ let status = reaper.join().unwrap().expect("the agent was reaped");+ assert_eq!(status.terminating_signal(), Some(9));+}diff --git a/crates/coding_agent_worker/src/harness.rs b/crates/coding_agent_worker/src/harness.rsindex 53a8b608f5..254acea244 100644--- a/crates/coding_agent_worker/src/harness.rs+++ b/crates/coding_agent_worker/src/harness.rs@@ -1,5 +1,11 @@//! The harness child process and the bridge between its stdio and the//! gateway channel.+//!+//! The bridge is a line relay: every line the agent writes becomes one ACP+//! message to the service, and every ACP message from the service becomes+//! one line on the agent's stdin. Owning the relay (rather than running the+//! agent through the SDK's client) is what lets a self-update stop it at a+//! line boundary and hand the still-running agent to the next process image.use std::path::Path;use std::sync::Arc;@@ -7,23 +13,29 @@ use std::time::Duration;use crate::config::Harness;use crate::outbound::acp_probe::{ProbeError, ProbeSubprocess, probe_subprocess};-use crate::outbound::acp_process::AcpProcess;+use crate::outbound::acp_process::{AcpProcess, AgentEvent, AttachedAgent, RunningAgent};use crate::self_knowledge::SelfKnowledge;-use agent_client_protocol::{- Channel as AcpChannel, Client, ConnectTo, LineDirection, TransportFrame,-};+use crate::turns::ActiveTurns;+use agent_client_protocol::{LineDirection, RawJsonRpcMessage};use agent_runtime_protocol::domain::connection::{- ConnectionError, ModelProbeHandler, RuntimeChannel, RuntimeConnection,+ ConnectionError, ModelProbeHandler, RuntimeChannel,+};+use agent_runtime_protocol::domain::schema::v0::{+ AcpMessage, ModelProbeResult, SystemEvent, ToRuntimeMessage, ToServerMessage,};-use agent_runtime_protocol::domain::schema::v0::SystemEvent;-use futures::StreamExt as _;-use tokio_util::task::AbortOnDropHandle;+use tokio::sync::oneshot;+use tokio::task::JoinSet;#[cfg(test)]mod test;const MODEL_PROBE_TIMEOUT: Duration = Duration::from_secs(10);+/// How long the relay may take to settle the agent's pipes and flush the+/// socket when handing the agent off.+#[cfg(unix)]+const HANDOFF_TIMEOUT: Duration = Duration::from_secs(10);+/// Why the bridge ended.#[derive(Debug, thiserror::Error)]pub enum BridgeError {@@ -38,89 +50,336 @@ pub enum BridgeError {/// Observes every line crossing the harness's stdio.pub type LineTap = Arc<dyn Fn(&str, LineDirection) + Send + Sync>;-/// Spawn the harness in ACP mode and pump frames until either side ends,-/// handing every ACP line to `tap` when there is one.+/// A request to stop relaying and hand the running agent back, answered+/// once the agent is detached (or with `None` when there is none).+#[cfg(unix)]+pub type HandoffRequest = oneshot::Sender<Option<crate::handoff::HandedOffAgent>>;++/// How the bridge ended without failing.+#[derive(Debug)]+pub enum BridgeEnd {+ /// The service or the agent went away; the agent is stopped.+ Closed,+ /// The agent was handed off as asked and keeps running.+ #[cfg(unix)]+ HandedOff,+}++/// Relay between the harness (spawned in ACP mode, or `adopted` from the+/// previous process image) and the service until either side ends, tracking+/// its turns in `turns` and handing every ACP line to `tap` when there is+/// one. `handoff` asks the bridge to stop at a line boundary and hand the+/// agent over instead.pub async fn bridge(harness: &Harness,cwd: &Path,channel: RuntimeChannel,tap: Option<LineTap>,-) -> Result<(), BridgeError> {+ turns: &ActiveTurns,+ adopted: Option<RunningAgent>,+ handoff: &mut Handoffs,+) -> Result<BridgeEnd, BridgeError> {let probes = HarnessModelProbes {process: probe_process(harness, cwd),};- let (mut runtime, acp) = RuntimeConnection::connect_with_model_probe_handler(channel, probes);-- let agent = AcpProcess::new(&harness.command, harness.args.clone(), cwd)- .envs(harness.env.clone())- // The wire tap: every ndjson line crossing the child's stdio, plus- // its stderr. Enable with RUST_LOG=coding_agent_worker=trace.- .with_debug(move |line, direction| {- tracing::trace!(?direction, line, "acp line");- if let Some(tap) = &tap {- tap(line, direction);- }- });+ let agent_process =+ AcpProcess::new(&harness.command, harness.args.clone(), cwd).envs(harness.env.clone());- let (agent_side, relay_side) = AcpChannel::duplex();- let _relay = AbortOnDropHandle::new(tokio::spawn(relay(acp, relay_side)));+ let RuntimeChannel {+ tx: to_service,+ rx: mut from_service,+ } = channel;+ // Taken by a handoff, which closes the socket by dropping it.+ let mut to_service = Some(to_service);+ to_service+ .as_ref()+ .expect("just set")+ .send(ToServerMessage::Event {+ event: SystemEvent::AcpReady,+ })+ .map_err(|_| BridgeError::Announce(ConnectionError::Closed))?;- runtime- .system_event(SystemEvent::AcpReady)- .map_err(BridgeError::Announce)?;+ let line_turns = turns.clone();+ // The wire tap: every ndjson line crossing the child's stdio, plus its+ // stderr. Enable with RUST_LOG=coding_agent_worker=trace.+ let debug = Arc::new(move |line: &str, direction: LineDirection| {+ tracing::trace!(?direction, line, "acp line");+ line_turns.observe(line, direction);+ if let Some(tap) = &tap {+ tap(line, direction);+ }+ });- let outcome = tokio::select! {- outcome = ConnectTo::<Client>::connect_to(agent, agent_side) => outcome,- () = runtime.closed() => Ok(()),+ let started = match adopted {+ Some(agent) => Ok(agent),+ None => agent_process.start(),+ };+ let outcome = match started {+ Ok(agent) => {+ relay(+ agent.attach(debug),+ &mut to_service,+ &mut from_service,+ &probes,+ turns,+ handoff,+ )+ .await+ }+ Err(error) => Err(BridgeError::Harness(error.to_string())),};+ turns.clear();++ // A handoff already told the service and closed the socket; anything+ // else announces the end. Best effort: a transport that has already+ // failed cannot carry news of its own failure.+ if let Some(to_service) = to_service+ && to_service+ .send(ToServerMessage::Event {+ event: SystemEvent::Disconnected,+ })+ .is_err()+ {+ tracing::debug!("could not announce disconnect");+ }+ match outcome {+ Ok(Relayed::Closed) => Ok(BridgeEnd::Closed),+ #[cfg(unix)]+ Ok(Relayed::HandedOff(())) => Ok(BridgeEnd::HandedOff),+ Err(error) => Err(error),+ }+}++/// Where handoff requests reach the bridge.+pub struct Handoffs {+ #[cfg(unix)]+ pub(crate) requests: tokio::sync::mpsc::Receiver<HandoffRequest>,+}- // Best effort: a transport that has already failed cannot carry news of- // its own failure, and that is not itself worth failing the worker over.- if let Err(error) = runtime.system_event(SystemEvent::Disconnected) {- tracing::debug!(error = ?error, "could not announce disconnect");+impl Handoffs {+ pub fn channel() -> (Self, HandoffSender) {+ #[cfg(unix)]+ {+ let (sender, requests) = tokio::sync::mpsc::channel(1);+ (Self { requests }, HandoffSender { sender })+ }+ #[cfg(not(unix))]+ {+ (Self {}, HandoffSender {})+ }}- outcome.map_err(|error| BridgeError::Harness(error.to_string()))+ /// The next request. Never resolves once every sender is gone.+ #[cfg(unix)]+ pub(crate) async fn next(&mut self) -> HandoffRequest {+ match self.requests.recv().await {+ Some(request) => request,+ None => std::future::pending().await,+ }+ }++ #[cfg(not(unix))]+ pub(crate) async fn next(&mut self) -> std::convert::Infallible {+ std::future::pending().await+ }+}++/// Asks the bridge (through the runtime) for its agent.+#[derive(Clone)]+pub struct HandoffSender {+ #[cfg(unix)]+ pub(crate) sender: tokio::sync::mpsc::Sender<HandoffRequest>,}-/// Pump frames between the service and the agent, teaching each new-/// session's agent about this runtime on its first prompt. Ends when either-/// side closes, which closes the other.-async fn relay(mut service: AcpChannel, mut agent: AcpChannel) {+enum Relayed {+ Closed,+ #[cfg(unix)]+ HandedOff(()),+}++async fn relay(+ mut agent: AttachedAgent,+ to_service_slot: &mut Option<tokio::sync::mpsc::UnboundedSender<ToServerMessage>>,+ from_service: &mut tokio::sync::mpsc::UnboundedReceiver<ToRuntimeMessage>,+ probes: &HarnessModelProbes,+ turns: &ActiveTurns,+ handoff: &mut Handoffs,+) -> Result<Relayed, BridgeError> {+ let to_service = to_service_slot+ .clone()+ .expect("the bridge holds the sender");+ // Owned by the relay: ending it aborts every probe subprocess.+ let mut probe_tasks = JoinSet::new();let mut self_knowledge = SelfKnowledge::default();loop {tokio::select! {- frame = service.rx.next() => {- let Some(frame) = frame else { break };- let frame = match frame {- TransportFrame::Single(message) => {- TransportFrame::Single(teach(&mut self_knowledge, message))+ event = agent.next_event() => match event {+ AgentEvent::Line(line) => {+ let Some(line) = line else {+ // The agent closed its output; report how it ended.+ let status = tokio::time::timeout(+ crate::outbound::acp_process::SHUTDOWN_GRACE_PERIOD,+ agent.exited(),+ )+ .await;+ return match status {+ Ok(Ok(status)) => agent.finish(status).await.map(|()| Relayed::Closed),+ _ => Ok(Relayed::Closed),}- other => other,+ .map_err(|error| BridgeError::Harness(error.to_string()));};- if agent.tx.unbounded_send(frame).is_err() {- break;+ if forward_to_service(&line, &to_service, &mut self_knowledge).is_err() {+ return Ok(Relayed::Closed);+ }+ }+ AgentEvent::Exited(status) => {+ // Lines the agent wrote before exiting are still relayed.+ let flushed = tokio::time::timeout(+ crate::outbound::acp_process::SHUTDOWN_GRACE_PERIOD,+ async {+ while let Some(line) = agent.next_line().await {+ let _ = forward_to_service(&line, &to_service, &mut self_knowledge);+ }+ },+ )+ .await;+ if flushed.is_err() {+ tracing::debug!("agent output stayed open after it exited");+ }+ let status = status.map_err(|error| BridgeError::Harness(error.to_string()))?;+ return agent+ .finish(status)+ .await+ .map(|()| Relayed::Closed)+ .map_err(|error| BridgeError::Harness(error.to_string()));+ }+ },+ message = from_service.recv() => {+ match message {+ Some(ToRuntimeMessage::Acp(AcpMessage(frame))) => {+ if let Some(line) = encode(&teach(&mut self_knowledge, frame))+ && agent.send_line(line).is_err()+ {+ return Ok(Relayed::Closed);+ }+ }+ Some(ToRuntimeMessage::ModelProbeRequest) => {+ let probes = probes.clone();+ let to_service = to_service.clone();+ probe_tasks.spawn(async move {+ let result = match probes.probe().await {+ Ok(config_options) => ModelProbeResult::Available { config_options },+ Err(message) => ModelProbeResult::Error { message },+ };+ let _ = to_service.send(ToServerMessage::ModelProbeResponse { result });+ });+ }+ Some(other) => tracing::debug!(message = ?other, "ignoring an unknown runtime message"),+ None => return Ok(Relayed::Closed),}}- frame = agent.rx.next() => {- let Some(frame) = frame else { break };- if let TransportFrame::Single(message) = &frame- && let Ok(message) = serde_json::to_value(message)+ Some(_) = probe_tasks.join_next(), if !probe_tasks.is_empty() => {}+ request = handoff.next() => {+ #[cfg(unix)]{- self_knowledge.inbound(&message);- }- if service.tx.unbounded_send(frame).is_err() {- break;+ probe_tasks.shutdown().await;+ // Every sender must go for the socket to close.+ to_service_slot.take();+ let handed = hand_off(agent, to_service, from_service, turns, &mut self_knowledge).await;+ let _ = request.send(handed);+ return Ok(Relayed::HandedOff(()));}+ #[cfg(not(unix))]+ match request {}}}}}-fn teach(+/// Stop at a line boundary and give the agent up, losing nothing on the way:+/// lines already read go to the service, messages already sent by the+/// service go to the agent, and the socket is flushed and closed before the+/// agent's pipes are returned.+#[cfg(unix)]+async fn hand_off(+ mut agent: AttachedAgent,+ to_service: tokio::sync::mpsc::UnboundedSender<ToServerMessage>,+ from_service: &mut tokio::sync::mpsc::UnboundedReceiver<ToRuntimeMessage>,+ turns: &ActiveTurns,self_knowledge: &mut SelfKnowledge,- message: agent_client_protocol::RawJsonRpcMessage,-) -> agent_client_protocol::RawJsonRpcMessage {+) -> Option<crate::handoff::HandedOffAgent> {+ let span = tracing::info_span!("macrod.update.handoff");+ let _entered = span.enter();+ agent.stop_reading();+ while let Some(line) = agent.next_line().await {+ let _ = forward_to_service(&line, &to_service, self_knowledge);+ }+ // Closing the sending side lets the socket drain what is queued and shut;+ // the receiving side ends once it has.+ let _ = to_service.send(ToServerMessage::Event {+ event: SystemEvent::Disconnected,+ });+ drop(to_service);+ let drained = tokio::time::timeout(HANDOFF_TIMEOUT, async {+ while let Some(message) = from_service.recv().await {+ if let ToRuntimeMessage::Acp(AcpMessage(frame)) = message+ && let Some(line) = encode(&teach(self_knowledge, frame))+ {+ let _ = agent.send_line(line);+ }+ }+ })+ .await;+ if drained.is_err() {+ tracing::warn!("the service socket did not close in time; handing off anyway");+ }+ let active_prompts = turns.snapshot();+ match agent.detach(HANDOFF_TIMEOUT).await {+ Ok(running) => match running.hand_off(active_prompts) {+ Ok(handed) => Some(handed),+ Err(error) => {+ tracing::warn!(error = %error, "could not hand the agent off");+ None+ }+ },+ Err(error) => {+ tracing::warn!(error = %error, "could not detach the agent");+ None+ }+ }+}++/// Relay one agent line, letting `self_knowledge` learn which session a+/// `session/new` answer created.+fn forward_to_service(+ line: &str,+ to_service: &tokio::sync::mpsc::UnboundedSender<ToServerMessage>,+ self_knowledge: &mut SelfKnowledge,+) -> Result<(), ()> {+ match serde_json::from_str::<RawJsonRpcMessage>(line) {+ Ok(frame) => {+ if let Ok(message) = serde_json::to_value(&frame) {+ self_knowledge.inbound(&message);+ }+ to_service+ .send(ToServerMessage::Acp(AcpMessage(frame)))+ .map_err(|_| ())+ }+ Err(error) => {+ tracing::warn!(error = %error, "dropping an agent line that is not a JSON-RPC message");+ Ok(())+ }+ }+}++fn encode(frame: &RawJsonRpcMessage) -> Option<String> {+ serde_json::to_string(frame)+ .inspect_err(|error| tracing::warn!(error = %error, "could not encode an ACP message"))+ .ok()+}++/// Teach a new session's agent about this runtime on its first prompt.+fn teach(self_knowledge: &mut SelfKnowledge, message: RawJsonRpcMessage) -> RawJsonRpcMessage {let Ok(mut value) = serde_json::to_value(&message) else {return message;};diff --git a/crates/coding_agent_worker/src/harness/test.rs b/crates/coding_agent_worker/src/harness/test.rsindex 82c3762736..c00d5b9f25 100644--- a/crates/coding_agent_worker/src/harness/test.rs+++ b/crates/coding_agent_worker/src/harness/test.rs@@ -1,7 +1,7 @@use super::*;use agent_runtime_protocol::domain::channel::Channel;use agent_runtime_protocol::domain::connection::ServerChannel;-use agent_runtime_protocol::domain::schema::v0::ToServerMessage;+use agent_runtime_protocol::domain::schema::v0::{AcpMessage, ToRuntimeMessage, ToServerMessage};fn harness(command: &str) -> Harness {Harness {@@ -32,6 +32,9 @@ async fn unspawnable_harness_reports_failure() {std::path::Path::new("/"),runtime,None,+ &ActiveTurns::default(),+ None,+ &mut Handoffs::channel().0,).await.expect_err("a harness that cannot be spawned must not look like success");@@ -51,7 +54,16 @@ async fn harness_that_exits_immediately_disconnects() {// `true` spawns cleanly and closes its stdio at once, which is the// shutdown path rather than the spawn-failure path above.- let _ = bridge(&harness("true"), std::path::Path::new("/"), runtime, None).await;+ let _ = bridge(+ &harness("true"),+ std::path::Path::new("/"),+ runtime,+ None,+ &ActiveTurns::default(),+ None,+ &mut Handoffs::channel().0,+ )+ .await;assert_eq!(events(service),@@ -66,9 +78,17 @@ async fn dropped_service_channel_does_not_panic_the_bridge() {// Announcing readiness into a closed channel is a failure to announce, not// a harness failure: nothing was ever spawned.- let error = bridge(&harness("true"), std::path::Path::new("/"), runtime, None)- .await- .expect_err("announcing into a closed channel must fail");+ let error = bridge(+ &harness("true"),+ std::path::Path::new("/"),+ runtime,+ None,+ &ActiveTurns::default(),+ None,+ &mut Handoffs::channel().0,+ )+ .await+ .expect_err("announcing into a closed channel must fail");assert!(matches!(error, BridgeError::Announce(_)), "got {error:?}");}@@ -95,60 +115,85 @@ async fn model_probe_process_failures_are_safely_redacted() {#[tokio::test]async fn relay_teaches_a_new_session_on_its_first_prompt() {- let (mut service, service_side) = AcpChannel::duplex();- let (agent_side, mut agent) = AcpChannel::duplex();- let _relay = tokio::spawn(relay(service_side, agent_side));- let frame = |value: serde_json::Value| {- TransportFrame::Single(serde_json::from_value(value).expect("a valid JSON-RPC message"))+ let (runtime, service) = Channel::duplex();+ let ServerChannel {+ tx: to_bridge,+ rx: mut from_bridge,+ } = service;+ // Answers `session/new` with a session id, and every prompt with the+ // prompt blocks it received.+ let agent = Harness {+ command: "python3".to_owned(),+ args: vec![+ "-c".to_owned(),+ r#"+import json, sys+for line in sys.stdin:+ request = json.loads(line)+ if request["method"] == "session/new":+ result = {"sessionId": "acp-1"}+ else:+ result = {"prompt": request["params"]["prompt"]}+ print(json.dumps({"jsonrpc": "2.0", "id": request["id"], "result": result}), flush=True)+"#+ .to_owned(),+ ],+ env: Default::default(),};- let received = |frame: TransportFrame| match frame {- TransportFrame::Single(message) => {- serde_json::to_value(message).expect("a serializable message")- }- other => panic!("expected one message, got {other:?}"),+ let send = |value: serde_json::Value| {+ to_bridge+ .send(ToRuntimeMessage::Acp(AcpMessage(+ serde_json::from_value(value).expect("a valid JSON-RPC message"),+ )))+ .expect("the bridge is running");};-- service- .tx- .unbounded_send(frame(serde_json::json!({+ let mut handoffs = Handoffs::channel().0;+ let turns = ActiveTurns::default();+ let bridge = bridge(+ &agent,+ std::path::Path::new("/"),+ runtime,+ None,+ &turns,+ None,+ &mut handoffs,+ );+ let conversation = async {+ let mut next_answer = async || loop {+ match from_bridge.recv().await.expect("the bridge is running") {+ ToServerMessage::Acp(AcpMessage(frame)) => {+ break serde_json::to_value(frame).expect("a serializable message");+ }+ ToServerMessage::Event { .. } => {}+ other => panic!("unexpected message {other:?}"),+ }+ };+ send(serde_json::json!({"jsonrpc": "2.0", "id": 1, "method": "session/new","params": {"cwd": "/home/operator/code", "mcpServers": []}- })))- .expect("the relay is running");- agent- .rx- .next()- .await- .expect("session/new reaches the agent");- agent- .tx- .unbounded_send(frame(serde_json::json!({- "jsonrpc": "2.0", "id": 1, "result": {"sessionId": "acp-1"}- })))- .expect("the relay is running");- service- .rx- .next()- .await- .expect("the answer reaches the service");- service- .tx- .unbounded_send(frame(serde_json::json!({+ }));+ next_answer().await;+ send(serde_json::json!({"jsonrpc": "2.0", "id": 2, "method": "session/prompt","params": {"sessionId": "acp-1", "prompt": [{"type": "text", "text": "hello"}]}- })))- .expect("the relay is running");+ }));+ next_answer().await+ };++ let answer = tokio::select! {+ answer = conversation => answer,+ ended = bridge => panic!("the bridge ended early: {ended:?}"),+ };- let prompt = received(agent.rx.next().await.expect("the prompt reaches the agent"));- let blocks = prompt["params"]["prompt"]+ let blocks = answer["result"]["prompt"].as_array()- .expect("the prompt is a block list");- assert_eq!(blocks.len(), 2);+ .expect("the agent echoed the prompt blocks");+ assert_eq!(blocks.len(), 2, "got {answer}");assert!(blocks[0]["text"].as_str().is_some_and(|text| text.starts_with("<macrod-runtime>")),- "got {prompt}"+ "got {answer}");assert_eq!(blocks[1],diff --git a/crates/coding_agent_worker/src/main.rs b/crates/coding_agent_worker/src/main.rsindex 099be4dc5f..c411bbcb83 100644--- a/crates/coding_agent_worker/src/main.rs+++ b/crates/coding_agent_worker/src/main.rs@@ -19,6 +19,8 @@ mod config;mod daemon;mod dispatch;mod executable;+#[cfg(unix)]+mod handoff;mod harness;#[cfg(unix)]mod herdr;@@ -29,6 +31,8 @@ mod self_knowledge;mod service;mod trigger;mod tui;+mod turns;+mod update;use clap::Parser;use std::process::ExitCode;@@ -37,7 +41,7 @@ use std::process::ExitCode;/// bound agents, live sessions, config editing, pairing, removal, and logs -/// one process.#[derive(Parser)]-#[command(name = "macrod", version)]+#[command(name = "macrod", version = update::VERSION)]struct Args {/// Internal browser helper, isolated so terminal browsers cannot claim the TUI's stdin.#[arg(long, hide = true)]@@ -129,6 +133,12 @@ async fn main() -> ExitCode {// means somebody already did this.let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();+ // The installed `macrod.exe` on Windows only starts the newest version.+ #[cfg(windows)]+ if let Some(code) = update::versions::run_as_launcher() {+ std::process::exit(code);+ }+let args = Args::parse();if let Some(url) = args.open_url {return match webbrowser::open(&url) {@@ -234,13 +244,33 @@ async fn main() -> ExitCode {// The TUI owns the terminal, so its logs go to a ring buffer it renders.let logs = tui::LogBuffer::install();- match tui::run(config_path, logs).await {- Ok(()) => ExitCode::SUCCESS,- Err(error) => {- // The ring buffer dies with the process and the terminal is- // restored by now, so the exit error goes straight to stderr.- eprintln!("macrod exited with an error: {error:?}");- ExitCode::FAILURE+ // The agent the macrod this process replaced was relaying, if any.+ #[cfg(unix)]+ let mut inherited = handoff::take_inherited();+ #[cfg(not(unix))]+ let mut inherited = None;+ update::log::record(update::log::UpdateEvent::Startup {+ version: update::VERSION.to_owned(),+ from_handoff: inherited.is_some(),+ });+ loop {+ match tui::run(config_path, logs.clone(), inherited.take()).await {+ Ok(None) => return ExitCode::SUCCESS,+ #[cfg(unix)]+ Ok(Some(restart)) => {+ // Only returns when the new binary could not be started; the+ // panel comes back up and keeps serving, with a fresh agent.+ let error = restart.installed.relaunch(restart.agent);+ tracing::error!(error = ?error, "could not restart into the updated macrod");+ }+ #[cfg(not(unix))]+ Ok(Some(never)) => match never {},+ Err(error) => {+ // The ring buffer dies with the process and the terminal is+ // restored by now, so the exit error goes straight to stderr.+ eprintln!("macrod exited with an error: {error:?}");+ return ExitCode::FAILURE;+ }}}}diff --git a/crates/coding_agent_worker/src/outbound/acp_process.rs b/crates/coding_agent_worker/src/outbound/acp_process.rsindex a634ca4acc..a0d8ad6648 100644--- a/crates/coding_agent_worker/src/outbound/acp_process.rs+++ b/crates/coding_agent_worker/src/outbound/acp_process.rs@@ -45,15 +45,18 @@ use tokio_util::compat::{TokioAsyncReadCompatExt as _, TokioAsyncWriteCompatExt#[cfg(windows)]mod job;+mod running;#[cfg(test)]mod test;+pub(crate) use running::{AgentEvent, AttachedAgent, RunningAgent};+type DebugCallback = Arc<dyn Fn(&str, LineDirection) + Send + Sync + 'static>;const STDERR_CAPTURE_LIMIT: usize = 64 * 1024;const STDERR_READ_BUFFER_SIZE: usize = 8 * 1024;const STDERR_LINE_TRUNCATION_MARKER: &str = "… [stderr line truncated]";-const SHUTDOWN_GRACE_PERIOD: Duration = Duration::from_secs(1);+pub(crate) const SHUTDOWN_GRACE_PERIOD: Duration = Duration::from_secs(1);/// An ACP agent launched as a child of this daemon, in a chosen directory.///@@ -96,7 +99,9 @@ impl AcpProcess {}/// Observe every line crossing the child's stdin and stdout, plus its- /// stderr. Exceptionally long stderr lines are truncated.+ /// stderr. Exceptionally long stderr lines are truncated. The bridge taps+ /// lines in its own relay ([`RunningAgent::attach`]).+ #[cfg(test)]#[must_use]pub(crate) fn with_debug<Callback>(mut self, callback: Callback) -> Selfwhere@@ -119,6 +124,8 @@ impl AcpProcess {{use std::os::unix::process::CommandExt as _;+ // A handoff descriptor number means nothing in the agent.+ command.env_remove(crate::handoff::HANDOFF_ENV);// The child leads its own process group so `ChildGuard` can kill// the whole tree. Agents commonly sit behind wrapper launchers// (`npx …`, `uvx …`): killing only the immediate child orphans the@@ -163,9 +170,9 @@ struct SpawnedChild {}/// Kills the child - and its whole process group (Unix) or job (Windows) --/// when dropped.+/// when dropped, unless it is handed on ([`ChildGuard::release`]).struct ChildGuard {- child: Child,+ child: GuardedChild,/// Captured at spawn: tokio forgets the pid once the child is reaped, and/// the group kill must still reach descendants a wrapper left behind.#[cfg(unix)]@@ -174,6 +181,17 @@ struct ChildGuard {job: job::Job,}+enum GuardedChild {+ Spawned(Child),+ /// A child an earlier macrod process image spawned and handed over. It is+ /// still this process's child, so a blocking `waitpid` reaps it.+ /// `None` once its exit has been reported.+ #[cfg(unix)]+ Adopted(Option<tokio::sync::oneshot::Receiver<std::io::Result<std::process::ExitStatus>>>),+ /// Handed on to the next process image; nothing left to supervise.+ Released,+}+impl ChildGuard {fn new(child: Child) -> std::io::Result<Self> {#[cfg(unix)]@@ -186,7 +204,7 @@ impl ChildGuard {.ok_or_else(|| std::io::Error::other("the child exited before it could be supervised")).and_then(job::Job::containing)?;Ok(Self {- child,+ child: GuardedChild::Spawned(child),#[cfg(unix)]process_group,#[cfg(windows)]@@ -194,8 +212,64 @@ impl ChildGuard {})}+ /// Supervise a child this process inherited across `execve`. It leads+ /// its own process group, like every child [`AcpProcess`] spawns.+ #[cfg(unix)]+ fn adopt(pid: rustix::process::Pid) -> Self {+ let (exit_tx, exit) = tokio::sync::oneshot::channel();+ std::thread::spawn(move || {+ use std::os::unix::process::ExitStatusExt as _;++ let status = rustix::process::waitpid(Some(pid), rustix::process::WaitOptions::empty())+ .map_err(std::io::Error::from)+ .and_then(|status| {+ status.ok_or_else(|| std::io::Error::other("waitpid returned no status"))+ })+ .map(|(_, status)| std::process::ExitStatus::from_raw(status.as_raw()));+ let _ = exit_tx.send(status);+ });+ Self {+ child: GuardedChild::Adopted(Some(exit)),+ process_group: Some(pid),+ }+ }+async fn wait(&mut self) -> std::io::Result<std::process::ExitStatus> {- self.child.wait().await+ match &mut self.child {+ GuardedChild::Spawned(child) => child.wait().await,+ #[cfg(unix)]+ GuardedChild::Adopted(pending_exit) => {+ let Some(exit) = pending_exit.as_mut() else {+ return std::future::pending().await;+ };+ let status = exit+ .await+ .unwrap_or_else(|_| Err(std::io::Error::other("the child waiter stopped")));+ *pending_exit = None;+ status+ }+ GuardedChild::Released => std::future::pending().await,+ }+ }++ /// The child's pid, for handing it to the next process image.+ #[cfg(unix)]+ fn pid(&self) -> Option<rustix::process::Pid> {+ self.process_group+ }++ /// Stop supervising without killing: the child outlives this guard.+ #[cfg(unix)]+ fn release(&mut self) {+ // Forgotten, not dropped: tokio's `Child` kills on drop+ // (`kill_on_drop`) and would otherwise reap the process this image+ // no longer owns.+ if let GuardedChild::Spawned(child) =+ std::mem::replace(&mut self.child, GuardedChild::Released)+ {+ std::mem::forget(child);+ }+ self.process_group = None;}fn terminate(&mut self) {@@ -211,7 +285,9 @@ impl ChildGuard {#[cfg(windows)]self.job.terminate();// A no-op double tap: the group or job already held the child.- drop(self.child.start_kill());+ if let GuardedChild::Spawned(child) = &mut self.child {+ drop(child.start_kill());+ }}}@@ -328,20 +404,27 @@ struct StderrDrainResult {read_error: Option<std::io::Error>,}-/// Read stderr to EOF. Never leaving the pipe unread matters: a full pipe-/// buffer blocks the child.+/// Read stderr to EOF, or until `stop` resolves. Never leaving the pipe+/// unread matters: a full pipe buffer blocks the child.async fn drain_stderr(- mut stderr: impl futures::AsyncRead + Unpin,+ stderr: &mut (impl futures::AsyncRead + Unpin),debug_callback: Option<DebugCallback>,+ stop: impl Future<Output = ()>,) -> StderrDrainResult {use futures::AsyncReadExt as _;let mut tail = StderrTail::default();let mut debug_lines = debug_callback.as_ref().map(|_| StderrDebugLines::default());let mut buffer = [0; STDERR_READ_BUFFER_SIZE];+ let mut stop = pin!(stop);let read_error = loop {- match stderr.read(&mut buffer).await {+ let read = tokio::select! {+ biased;+ () = &mut stop => break None,+ read = stderr.read(&mut buffer) => read,+ };+ match read {Ok(0) => break None,Ok(read) => {let bytes = &buffer[..read];@@ -541,7 +624,7 @@ impl ConnectTo<Client> for AcpProcess {let StderrDrainResult {captured,read_error,- } = drain_stderr(stderr.compat(), debug_callback).await;+ } = drain_stderr(&mut stderr.compat(), debug_callback, std::future::pending()).await;drop(stderr_tx.send(captured));if let Some(error) = read_error {diff --git a/crates/coding_agent_worker/src/outbound/acp_process/running.rs b/crates/coding_agent_worker/src/outbound/acp_process/running.rsnew file mode 100644index 0000000000..8367873d97--- /dev/null+++ b/crates/coding_agent_worker/src/outbound/acp_process/running.rs@@ -0,0 +1,365 @@+//! A spawned ACP agent relayed line by line, which on Unix can be handed to+//! the next macrod process image without stopping.+//!+//! [`AcpProcess::connect_to`] drives an agent through the SDK, which owns+//! the reads and writes. The harness bridge instead relays raw lines, and+//! needs what the SDK cannot give it: to stop reading at a line boundary,+//! finish the writes already queued, and take the pipes back.++use std::time::Duration;++use agent_client_protocol::util::internal_error;+use agent_client_protocol::{Error, LineDirection};+use tokio::io::{AsyncBufReadExt as _, AsyncWriteExt as _, BufReader};+use tokio::process::{ChildStderr, ChildStdin, ChildStdout};+use tokio::sync::{mpsc, oneshot};+use tokio::task::JoinHandle;+use tokio_util::compat::TokioAsyncReadCompatExt as _;++use super::{+ AcpProcess, ChildGuard, DebugCallback, SHUTDOWN_GRACE_PERIOD, SpawnedChild, StderrDrainResult,+ drain_stderr,+};++/// An agent process this daemon started (or inherited), not yet relayed.+pub(crate) struct RunningAgent {+ stdin: ChildStdin,+ stdout: ChildStdout,+ stderr: ChildStderr,+ /// Output read past the last relayed line. Always the start of a line.+ pending_output: Vec<u8>,+ guard: ChildGuard,+}++impl AcpProcess {+ /// Spawn the agent for a line relay ([`RunningAgent::attach`]).+ pub(crate) fn start(&self) -> Result<RunningAgent, Error> {+ let SpawnedChild {+ stdin,+ stdout,+ stderr,+ guard,+ } = self.spawn()?;+ Ok(RunningAgent {+ stdin,+ stdout,+ stderr,+ pending_output: Vec::new(),+ guard,+ })+ }+}++impl RunningAgent {+ /// Start relaying: stdout is split into lines, stdin takes lines, and+ /// stderr is drained. `debug` sees every line in every direction.+ pub(crate) fn attach(self, debug: DebugCallback) -> AttachedAgent {+ let Self {+ stdin,+ stdout,+ mut stderr,+ pending_output,+ guard,+ } = self;++ let (lines_tx, lines) = mpsc::unbounded_channel();+ let (stop_reading, reading_stopped) = oneshot::channel();+ let reader = tokio::spawn(read_lines(+ BufReader::new(stdout),+ pending_output,+ lines_tx,+ reading_stopped,+ debug.clone(),+ ));++ let (writes, writes_rx) = mpsc::unbounded_channel();+ let writer = tokio::spawn(write_lines(stdin, writes_rx, debug.clone()));++ let (stop_stderr, stderr_stopped) = oneshot::channel::<()>();+ let stderr_task = tokio::spawn(async move {+ let drained = drain_stderr(&mut (&mut stderr).compat(), Some(debug), async {+ let _ = stderr_stopped.await;+ })+ .await;+ (stderr, drained)+ });++ AttachedAgent {+ lines,+ stop_reading: Some(stop_reading),+ reader,+ writes,+ writer,+ stop_stderr,+ stderr: stderr_task,+ guard,+ }+ }+}++/// What the relayed agent did next.+pub(crate) enum AgentEvent {+ /// An output line, or `None` once its stdout ended.+ Line(Option<String>),+ /// The process exited.+ Exited(std::io::Result<std::process::ExitStatus>),+}++/// An agent whose stdio is being relayed.+pub(crate) struct AttachedAgent {+ lines: mpsc::UnboundedReceiver<String>,+ stop_reading: Option<oneshot::Sender<()>>,+ reader: JoinHandle<std::io::Result<(BufReader<ChildStdout>, Vec<u8>)>>,+ writes: mpsc::UnboundedSender<String>,+ writer: JoinHandle<std::io::Result<ChildStdin>>,+ stop_stderr: oneshot::Sender<()>,+ stderr: JoinHandle<(ChildStderr, StderrDrainResult)>,+ guard: ChildGuard,+}++impl AttachedAgent {+ /// The agent's next output line; `None` once its stdout has ended or+ /// reading was stopped and every line read before is delivered.+ pub(crate) async fn next_line(&mut self) -> Option<String> {+ self.lines.recv().await+ }++ /// Queue a line for the agent's stdin. Lines are written whole and in+ /// order.+ pub(crate) fn send_line(&self, line: String) -> std::io::Result<()> {+ self.writes+ .send(line)+ .map_err(|_| std::io::Error::other("the agent's stdin writer stopped"))+ }++ /// Resolves when the agent process exits. Cancel-safe.+ pub(crate) async fn exited(&mut self) -> std::io::Result<std::process::ExitStatus> {+ self.guard.wait().await+ }++ /// The next output line or the process exit, whichever comes first.+ /// Cancel-safe.+ pub(crate) async fn next_event(&mut self) -> AgentEvent {+ tokio::select! {+ line = self.lines.recv() => AgentEvent::Line(line),+ status = self.guard.wait() => AgentEvent::Exited(status),+ }+ }++ /// Stop reading after the line in progress; [`Self::next_line`] then+ /// returns what was already read, then `None`.+ pub(crate) fn stop_reading(&mut self) {+ if let Some(stop) = self.stop_reading.take() {+ let _ = stop.send(());+ }+ }++ /// End the relay after the agent exited with `status`: the rest of its+ /// process group is killed, and a failure carries the stderr tail.+ pub(crate) async fn finish(self, status: std::process::ExitStatus) -> Result<(), Error> {+ let Self {+ mut guard,+ stderr,+ stop_stderr,+ reader,+ writer,+ ..+ } = self;+ // A launcher may exit while a descendant still holds its stdio.+ guard.terminate();+ reader.abort();+ writer.abort();+ if status.success() {+ stderr.abort();+ return Ok(());+ }+ let captured = match tokio::time::timeout(SHUTDOWN_GRACE_PERIOD, stderr).await {+ Ok(Ok((_, drained))) => drained.captured,+ _ => {+ drop(stop_stderr);+ String::new()+ }+ };+ if captured.is_empty() {+ Err(internal_error(format!("Process exited with {status}")))+ } else {+ Err(internal_error(format!(+ "Process exited with {status}: {captured}"+ )))+ }+ }++ /// Take the agent back from the relay without stopping it: reading has+ /// stopped ([`Self::stop_reading`]) and every line read was taken, the+ /// queued writes are flushed, and what was read past the last line is+ /// kept for whoever relays the agent next.+ #[cfg(unix)]+ pub(crate) async fn detach(self, within: Duration) -> std::io::Result<RunningAgent> {+ let Self {+ lines,+ stop_reading,+ reader,+ writes,+ writer,+ stop_stderr,+ stderr,+ guard,+ } = self;+ debug_assert!(stop_reading.is_none(), "stop reading before detaching");+ drop(lines);+ drop(writes);+ let _ = stop_stderr.send(());+ let detached = async {+ let (stdout, pending_output) = reader.await.map_err(std::io::Error::other)??;+ let stdin = writer.await.map_err(std::io::Error::other)??;+ let (stderr, _) = stderr.await.map_err(std::io::Error::other)?;+ Ok::<_, std::io::Error>(RunningAgent {+ stdin,+ stdout: stdout.into_inner(),+ stderr,+ pending_output,+ guard,+ })+ };+ tokio::time::timeout(within, detached)+ .await+ .map_err(|_| std::io::Error::other("the agent's pipes did not settle in time"))?+ }+}++/// Split stdout into lines until EOF or `stop`. On stop, returns the reader+/// and the bytes read past the last full line, so nothing is lost.+async fn read_lines(+ mut stdout: BufReader<ChildStdout>,+ mut partial: Vec<u8>,+ lines: mpsc::UnboundedSender<String>,+ mut stop: oneshot::Receiver<()>,+ debug: DebugCallback,+) -> std::io::Result<(BufReader<ChildStdout>, Vec<u8>)> {+ // Output handed over by an earlier process image may hold whole lines.+ while let Some(end) = partial.iter().position(|byte| *byte == b'\n') {+ deliver(partial.drain(..=end).collect(), &lines, &debug);+ }+ loop {+ // `partial` survives a cancelled `read_until`: whatever it read stays+ // in it, and the rest stays in the reader's buffer.+ tokio::select! {+ biased;+ _ = &mut stop => {+ let mut unread = partial;+ unread.extend_from_slice(stdout.buffer());+ let buffered = stdout.buffer().len();+ stdout.consume(buffered);+ return Ok((stdout, unread));+ }+ read = stdout.read_until(b'\n', &mut partial) => {+ if read? == 0 {+ // EOF: an unterminated last line is still a line.+ if !partial.is_empty() {+ deliver(std::mem::take(&mut partial), &lines, &debug);+ }+ return Ok((stdout, Vec::new()));+ }+ if partial.last() == Some(&b'\n') {+ deliver(std::mem::take(&mut partial), &lines, &debug);+ }+ }+ }+ }+}++fn deliver(mut line: Vec<u8>, lines: &mpsc::UnboundedSender<String>, debug: &DebugCallback) {+ if line.last() == Some(&b'\n') {+ line.pop();+ }+ if line.last() == Some(&b'\r') {+ line.pop();+ }+ if line.is_empty() {+ return;+ }+ match String::from_utf8(line) {+ Ok(line) => {+ debug(&line, LineDirection::Stdout);+ let _ = lines.send(line);+ }+ Err(error) => tracing::warn!(error = %error, "dropping a non-UTF-8 line from the agent"),+ }+}++/// Write each queued line whole, then hand stdin back once the queue closes.+async fn write_lines(+ mut stdin: ChildStdin,+ mut writes: mpsc::UnboundedReceiver<String>,+ debug: DebugCallback,+) -> std::io::Result<ChildStdin> {+ while let Some(line) = writes.recv().await {+ debug(&line, LineDirection::Stdin);+ let mut bytes = line.into_bytes();+ bytes.push(b'\n');+ stdin.write_all(&bytes).await?;+ stdin.flush().await?;+ }+ Ok(stdin)+}++#[cfg(unix)]+mod handoff {+ use std::os::fd::OwnedFd;++ use super::{ChildGuard, RunningAgent};+ use crate::handoff::{HandedOffAgent, HandedOffParts};+ use crate::turns::ActiveTurns;++ impl RunningAgent {+ /// Pick up an agent the previous process image handed over, and the+ /// turns it had running.+ pub(crate) fn adopt(agent: HandedOffAgent, turns: &ActiveTurns) -> std::io::Result<Self> {+ let HandedOffParts {+ pid,+ stdin,+ stdout,+ stderr,+ pending_output,+ active_prompts,+ } = agent.into_parts();+ turns.restore(active_prompts);+ let guard = ChildGuard::adopt(pid);+ Ok(Self {+ stdin: tokio::process::ChildStdin::from_std(std::process::ChildStdin::from(stdin))?,+ stdout: tokio::process::ChildStdout::from_std(std::process::ChildStdout::from(+ stdout,+ ))?,+ stderr: tokio::process::ChildStderr::from_std(std::process::ChildStderr::from(+ stderr,+ ))?,+ pending_output,+ guard,+ })+ }++ /// Hand the agent to the next process image. It keeps running; the+ /// guard no longer kills it.+ pub(crate) fn hand_off(+ mut self,+ active_prompts: Vec<String>,+ ) -> std::io::Result<HandedOffAgent> {+ let pid = self+ .guard+ .pid()+ .ok_or_else(|| std::io::Error::other("the agent has no pid"))?;+ let stdin: OwnedFd = self.stdin.into_owned_fd()?;+ let stdout: OwnedFd = self.stdout.into_owned_fd()?;+ let stderr: OwnedFd = self.stderr.into_owned_fd()?;+ self.guard.release();+ Ok(HandedOffAgent::new(+ pid,+ stdin,+ stdout,+ stderr,+ self.pending_output,+ active_prompts,+ ))+ }+ }+}diff --git a/crates/coding_agent_worker/src/outbound/acp_process/test.rs b/crates/coding_agent_worker/src/outbound/acp_process/test.rsindex 820b2ff168..4789007804 100644--- a/crates/coding_agent_worker/src/outbound/acp_process/test.rs+++ b/crates/coding_agent_worker/src/outbound/acp_process/test.rs@@ -114,10 +114,11 @@ async fn stderr_drain_stops_after_read_error() {let (callback, recorded) = recording_debug_callback();let result = drain_stderr(- ErrorAfterData {+ &mut ErrorAfterData {polls: polls.clone(),},Some(callback),+ std::future::pending(),).await;@@ -256,8 +257,12 @@ async fn large_unterminated_stderr_is_fully_drained() {drop(stdin);drop(stdout);+ let mut stderr = stderr.compat();let (drained, status) = tokio::time::timeout(Duration::from_secs(10), async {- futures::join!(drain_stderr(stderr.compat(), None), guard.wait())+ futures::join!(+ drain_stderr(&mut stderr, None, std::future::pending()),+ guard.wait()+ )}).await.expect("stderr drain should not block after its retained tail is full");diff --git a/crates/coding_agent_worker/src/runtime.rs b/crates/coding_agent_worker/src/runtime.rsindex da529ba66d..596e01b6e3 100644--- a/crates/coding_agent_worker/src/runtime.rs+++ b/crates/coding_agent_worker/src/runtime.rs@@ -13,8 +13,10 @@ use tokio_tungstenite::tungstenite;use tokio_util::task::AbortOnDropHandle;use crate::config::{Harness, HarnessCredentials, MacroApi};-use crate::harness::{self, LineTap};+use crate::harness::{self, BridgeEnd, Handoffs, LineTap};+use crate::outbound::acp_process::RunningAgent;use crate::outbound::link;+use crate::turns::ActiveTurns;#[cfg(test)]mod test;@@ -35,12 +37,16 @@ pub struct Runtime {impl Runtime {/// Start connecting with the given credentials and keep the harness/// available for model discovery and agent sessions until dropped.+ #[expect(clippy::too_many_arguments, reason = "the supervisor's whole input")]pub fn start(macro_api: &MacroApi,credentials: &HarnessCredentials,harness: Harness,cwd: &Path,tap: Option<LineTap>,+ turns: ActiveTurns,+ adopted: Option<RunningAgent>,+ handoffs: Handoffs,) -> Self {let (connected_tx, connected) = watch::channel(false);let task = tokio::spawn(serve(@@ -49,7 +55,10 @@ impl Runtime {harness,cwd.to_owned(),tap,+ turns,connected_tx,+ adopted,+ handoffs,));Self {connected,@@ -75,23 +84,55 @@ impl Runtime {/// Reconnect even after a clean close or a long outage: model discovery must/// recover without requiring an agent mention. Authentication failures stop/// the supervisor until the user re-pairs or restarts the daemon.+///+/// An `adopted` agent (handed over by the previous process image) serves the+/// first connection instead of a fresh spawn. A handoff request ends the+/// supervisor: the bridge answers it with its agent, and between bridges it+/// is answered with whatever agent is still waiting for a connection.+#[expect(clippy::too_many_arguments, reason = "the supervisor's whole input")]async fn serve(gateway_url: String,token: String,harness: Harness,cwd: PathBuf,tap: Option<LineTap>,+ turns: ActiveTurns,connected: watch::Sender<bool>,+ mut adopted: Option<RunningAgent>,+ mut handoffs: Handoffs,) {let mut backoff = reconnect_strategy();loop {- match tokio::time::timeout(CONNECT_TIMEOUT, link::dial(&gateway_url, &token)).await {+ let dialed = tokio::select! {+ dialed = tokio::time::timeout(CONNECT_TIMEOUT, link::dial(&gateway_url, &token)) => dialed,+ request = handoffs.next() => {+ answer_between_bridges(request, adopted.take(), &turns);+ return;+ }+ };+ match dialed {Ok(Ok(channel)) => {connected.send_replace(true);- tracing::info!("harness bridge starting");+ tracing::info!(adopted = adopted.is_some(), "harness bridge starting");let started = Instant::now();- match harness::bridge(&harness, &cwd, channel, tap.clone()).await {- Ok(()) => tracing::info!("harness bridge ended; reconnecting"),+ match harness::bridge(+ &harness,+ &cwd,+ channel,+ tap.clone(),+ &turns,+ adopted.take(),+ &mut handoffs,+ )+ .await+ {+ Ok(BridgeEnd::Closed) => tracing::info!("harness bridge ended; reconnecting"),+ #[cfg(unix)]+ Ok(BridgeEnd::HandedOff) => {+ tracing::info!("harness bridge handed its agent off");+ connected.send_replace(false);+ return;+ }Err(error) => {tracing::warn!(error = ?error, "harness bridge ended; reconnecting");}@@ -110,10 +151,42 @@ async fn serve(}Err(_) => tracing::warn!("runtime connection timed out; reconnecting"),}- tokio::time::sleep(backoff.next().unwrap_or(MAX_RECONNECT_DELAY)).await;+ tokio::select! {+ () = tokio::time::sleep(backoff.next().unwrap_or(MAX_RECONNECT_DELAY)) => {}+ request = handoffs.next() => {+ answer_between_bridges(request, adopted.take(), &turns);+ return;+ }+ }}}+/// Answer a handoff that arrived while no bridge was relaying: only an agent+/// adopted and not yet connected is running.+#[cfg(unix)]+fn answer_between_bridges(+ reply: harness::HandoffRequest,+ adopted: Option<RunningAgent>,+ turns: &ActiveTurns,+) {+ let handed = adopted.and_then(|agent| {+ agent+ .hand_off(turns.snapshot())+ .inspect_err(|error| tracing::warn!(error = %error, "could not hand the agent off"))+ .ok()+ });+ let _ = reply.send(handed);+}++#[cfg(not(unix))]+fn answer_between_bridges(+ request: std::convert::Infallible,+ _adopted: Option<RunningAgent>,+ _turns: &ActiveTurns,+) {+ match request {}+}+/// Retry transport failures, server errors, and temporary HTTP refusals./// Other 4xx responses require an operator to fix configuration or credentials.fn worth_redialing(error: &tungstenite::Error) -> bool {diff --git a/crates/coding_agent_worker/src/runtime/test.rs b/crates/coding_agent_worker/src/runtime/test.rsindex 474e97ac09..4558d98791 100644--- a/crates/coding_agent_worker/src/runtime/test.rs+++ b/crates/coding_agent_worker/src/runtime/test.rs@@ -24,6 +24,9 @@ fn start_runtime(listener: &TcpListener) -> Runtime {},Path::new("/"),None,+ ActiveTurns::default(),+ None,+ crate::harness::Handoffs::channel().0,)}diff --git a/crates/coding_agent_worker/src/service.rs b/crates/coding_agent_worker/src/service.rsindex 6e47aa7a58..40f02125d0 100644--- a/crates/coding_agent_worker/src/service.rs+++ b/crates/coding_agent_worker/src/service.rs@@ -223,7 +223,7 @@ async fn start(options: &ServiceOptions) -> rootcause::Result<Daemon> {// Pairing persisted into the file; serve what is actually on disk.let config = Config::load(&path)?;let harness_id = credentials.harness_id.to_string();- let daemon = Daemon::start(config, credentials, &path).await?;+ let daemon = Daemon::start(config, credentials, &path, None).await?;emit(&Event::Running { harness_id });Ok(daemon)}diff --git a/crates/coding_agent_worker/src/tui/app.rs b/crates/coding_agent_worker/src/tui/app.rsindex 39a7b49f25..472e25523e 100644--- a/crates/coding_agent_worker/src/tui/app.rs+++ b/crates/coding_agent_worker/src/tui/app.rs@@ -16,7 +16,7 @@ use super::logging::LogBuffer;use super::platform::{BrowserTarget, copy_to_clipboard};use super::process;use crate::config::{Config, HarnessCredentials};-use crate::daemon::Daemon;+use crate::daemon::{Daemon, InheritedAgent};use crate::outbound::pairing::{ClaimStatus, PairingClient};/// How often the dashboard re-reads the server.@@ -121,13 +121,23 @@ pub(crate) struct App {pub(crate) spinner: usize,pub(crate) logs: LogBuffer,pub(crate) daemon: Option<Daemon>,+ /// The agent the previous macrod handed over, relayed by the first start.+ inherited: Option<InheritedAgent>,+ /// A release staged for the next launch (Windows), shown until restart.+ /// The persistent update log the Logs tab points at.+ pub(crate) update_log: Option<PathBuf>,+ pub(crate) update_ready: Option<String>,pub(crate) pending_browser: Option<BrowserTarget>,last_refresh: Option<Instant>,pub(crate) quit: bool,}impl App {- pub(crate) fn load(config_path: &Path, logs: LogBuffer) -> rootcause::Result<Self> {+ pub(crate) fn load(+ config_path: &Path,+ logs: LogBuffer,+ inherited: Option<InheritedAgent>,+ ) -> rootcause::Result<Self> {let config = Config::load(config_path)?;let credentials = config.credentials@@ -151,6 +161,9 @@ impl App {spinner: 0,logs,daemon: None,+ inherited,+ update_ready: None,+ update_log: crate::update::log::user_log_path(),pending_browser: None,last_refresh: None,quit: false,@@ -170,7 +183,14 @@ impl App {let Some(credentials) = self.credentials.clone() else {return false;};- match Daemon::start(self.config.clone(), credentials, &self.config_path).await {+ match Daemon::start(+ self.config.clone(),+ credentials,+ &self.config_path,+ self.inherited.take(),+ )+ .await+ {Ok(daemon) => {self.daemon = Some(daemon);truediff --git a/crates/coding_agent_worker/src/tui/runner.rs b/crates/coding_agent_worker/src/tui/runner.rsindex e6ac466c8a..70265b1b1e 100644--- a/crates/coding_agent_worker/src/tui/runner.rs+++ b/crates/coding_agent_worker/src/tui/runner.rs@@ -11,6 +11,21 @@ use super::platform::open_pending_browser;use super::quickstart::{Quickstart, QuickstartAction};use super::ui;use crate::config::Config;+use crate::daemon::InheritedAgent;+use crate::update::{self, StagedUpdate};++/// What the caller does once the panel hands over to an updated macrod.+#[cfg(unix)]+pub struct Restart {+ /// The new binary, in place.+ pub installed: update::Installed,+ /// The running agent, for the new binary to keep relaying.+ pub agent: Option<InheritedAgent>,+}++/// Windows never restarts into an update; the launcher starts it next time.+#[cfg(not(unix))]+pub type Restart = std::convert::Infallible;#[cfg(test)]mod test;@@ -18,8 +33,14 @@ mod test;/// Spinner cadence and input latency budget.const TICK: Duration = Duration::from_millis(120);-/// Run the control panel - and the daemon inside it - until the user quits.-pub async fn run(config_path: &Path, logs: LogBuffer) -> rootcause::Result<()> {+/// Run the control panel - and the daemon inside it - until the user quits,+/// or until a self-update is installed and the caller should relaunch.+/// `inherited` is the agent a previous macrod handed over.+pub async fn run(+ config_path: &Path,+ logs: LogBuffer,+ inherited: Option<InheritedAgent>,+) -> rootcause::Result<Option<Restart>> {let (input_tx, mut input_rx) = tokio::sync::mpsc::unbounded_channel();// A dedicated thread owns the blocking crossterm read; the loop below// stays async and free to poll the network.@@ -67,13 +88,13 @@ pub async fn run(config_path: &Path, logs: LogBuffer) -> rootcause::Result<()> {};if needs_quickstart && !quickstarted {ratatui::restore();- return Ok(());+ return Ok(None);}if !config_path.exists() {ratatui::restore();- return Ok(());+ return Ok(None);}- let mut app = match App::load(config_path, logs) {+ let mut app = match App::load(config_path, logs, inherited) {Ok(app) => app,Err(error) => {ratatui::restore();@@ -178,8 +199,10 @@ async fn run_loop(app: &mut App,input: &mut tokio::sync::mpsc::UnboundedReceiver<TermEvent>,pair_on_start: bool,-) -> rootcause::Result<()> {+) -> rootcause::Result<Option<Restart>> {let mut tick = tokio::time::interval(TICK);+ let mut poller = update::start_poller(app.config.update.auto);+ let mut staged: Option<StagedUpdate> = None;// Paired already? Start serving before the first frame.if app.paired() {app.restart_daemon().await;@@ -200,7 +223,7 @@ async fn run_loop(match event {Some(TermEvent::Key(key)) => app.on_key(key).await,Some(_) => {}- None => return Ok(()),+ None => return Ok(None),}apply_pending_browser(app);}@@ -211,12 +234,57 @@ async fn run_loop(if matches!(app.mode, Mode::Normal) {app.maybe_refresh().await;}+ if let Some(update) = poller.as_mut().and_then(|poller| poller.staged.try_recv().ok()) {+ poller = None;+ if cfg!(windows) {+ // Installed beside this one; the launcher starts it next time.+ app.update_ready = Some(update.tag().to_owned());+ } else {+ app.ok(format!("macrod {} is ready; restarting once no turn is running", update.tag()));+ staged = Some(update);+ }+ }+ #[cfg(unix)]+ if let Some(installed) = install_when_idle(app, &mut staged) {+ let agent = match app.daemon.take() {+ Some(daemon) => daemon.hand_off().await,+ None => None,+ };+ return Ok(Some(Restart { installed, agent }));+ }}}if app.quit {app.stop_daemon().await;- return Ok(());+ return Ok(None);+ }+ }+}++/// Swap in a staged update once nothing would be cut short: no agent turn+/// running and no edit, pairing, or install open in the panel.+///+/// The agent process survives the restart, but the service ends a turn+/// when its runtime connection closes, so a turn in flight would still be+/// cut off in Macro: the swap waits for one to finish.+#[cfg(unix)]+fn install_when_idle(+ app: &mut App,+ staged: &mut Option<StagedUpdate>,+) -> Option<update::Installed> {+ let idle = matches!(app.mode, Mode::Normal)+ && app.daemon.as_ref().is_none_or(|daemon| daemon.is_idle());+ if !idle {+ return None;+ }+ let update = staged.take()?;+ let tag = update.tag().to_owned();+ match update.swap() {+ Ok(installed) => Some(installed),+ Err(error) => {+ app.fail(format!("Could not install macrod {tag}: {error}"));+ None}}}diff --git a/crates/coding_agent_worker/src/tui/ui/pages/config/test.rs b/crates/coding_agent_worker/src/tui/ui/pages/config/test.rsindex e10b21c730..ab2b849e5d 100644--- a/crates/coding_agent_worker/src/tui/ui/pages/config/test.rs+++ b/crates/coding_agent_worker/src/tui/ui/pages/config/test.rs@@ -10,7 +10,7 @@ fn short_config_keeps_selected_settings_and_consent_warnings_visible() {let directory = tempfile::tempdir().unwrap();let path = directory.path().join("macrod.toml");std::fs::write(&path, include_str!("../../../../../default.macrod.toml")).unwrap();- let mut app = App::load(&path, LogBuffer::default()).unwrap();+ let mut app = App::load(&path, LogBuffer::default(), None).unwrap();app.tab = Tab::Config;app.config.harness.args = vec!["herdr-acp".to_owned()];app.config.identity.scope = IdentityScope::Team;diff --git a/crates/coding_agent_worker/src/tui/ui/pages/logs.rs b/crates/coding_agent_worker/src/tui/ui/pages/logs.rsindex 7942f832e7..fb954e742f 100644--- a/crates/coding_agent_worker/src/tui/ui/pages/logs.rs+++ b/crates/coding_agent_worker/src/tui/ui/pages/logs.rs@@ -32,5 +32,12 @@ pub(super) fn render(frame: &mut Frame, app: &App, area: Rect) {} else {lines};- frame.render_widget(Paragraph::new(body).block(card("Daemon logs")), area);+ let mut block = card("Daemon logs");+ if let Some(path) = &app.update_log {+ block = block.title_bottom(Line::styled(+ format!(" Update history: {} ", path.display()),+ Style::new().fg(DIM),+ ));+ }+ frame.render_widget(Paragraph::new(body).block(block), area);}diff --git a/crates/coding_agent_worker/src/tui/ui/shell.rs b/crates/coding_agent_worker/src/tui/ui/shell.rsindex b78ce64a14..afe798fa25 100644--- a/crates/coding_agent_worker/src/tui/ui/shell.rs+++ b/crates/coding_agent_worker/src/tui/ui/shell.rs@@ -114,4 +114,16 @@ pub(super) fn render_footer(frame: &mut Frame, app: &App, area: Rect) {Line::from(spans)};frame.render_widget(Paragraph::new(line), area);+ // Bottom right, under everything else: Windows installs an update for+ // the next launch rather than restarting into it.+ if let Some(tag) = &app.update_ready {+ frame.render_widget(+ Paragraph::new(Line::styled(+ format!("Update {tag} ready — restart macrod to use it "),+ Style::new().fg(OK),+ ))+ .alignment(ratatui::layout::Alignment::Right),+ area,+ );+ }}diff --git a/crates/coding_agent_worker/src/turns.rs b/crates/coding_agent_worker/src/turns.rsnew file mode 100644index 0000000000..437b141e72--- /dev/null+++ b/crates/coding_agent_worker/src/turns.rs@@ -0,0 +1,98 @@+//! Which agent turns are running, read off the ACP wire.+//!+//! A turn is a `session/prompt` request the agent has not answered yet. The+//! self-updater restarts macrod only while none is outstanding.++use std::collections::HashSet;+use std::sync::{Arc, Mutex, PoisonError};++use agent_client_protocol::LineDirection;++#[cfg(test)]+mod test;++/// The ids of prompt requests sent to the ACP agent and not yet answered.+#[derive(Clone, Default)]+pub struct ActiveTurns {+ prompt_ids: Arc<Mutex<HashSet<String>>>,+}++/// The JSON-RPC fields that say whether a line starts or ends a turn.+#[derive(serde::Deserialize)]+struct RpcLine {+ id: Option<serde_json::Value>,+ method: Option<String>,+}++impl ActiveTurns {+ /// Record a line crossing the agent's stdio: a prompt request starts a+ /// turn, the response with the same id ends it.+ pub fn observe(&self, line: &str, direction: LineDirection) {+ let mut prompt_ids = self+ .prompt_ids+ .lock()+ .unwrap_or_else(PoisonError::into_inner);+ match direction {+ LineDirection::Stdin => {+ if !line.contains("session/prompt") {+ return;+ }+ if let Ok(RpcLine {+ id: Some(id),+ method: Some(method),+ }) = serde_json::from_str(line)+ && method == "session/prompt"+ {+ prompt_ids.insert(id.to_string());+ }+ }+ LineDirection::Stdout => {+ if prompt_ids.is_empty() {+ return;+ }+ if let Ok(RpcLine {+ id: Some(id),+ method: None,+ }) = serde_json::from_str(line)+ {+ prompt_ids.remove(&id.to_string());+ }+ }+ LineDirection::Stderr => {}+ }+ }++ /// The agent process ended: none of its turns can still be running.+ pub fn clear(&self) {+ self.prompt_ids+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .clear();+ }++ /// The prompt ids still running, to hand to the next process image.+ pub fn snapshot(&self) -> Vec<String> {+ self.prompt_ids+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .iter()+ .cloned()+ .collect()+ }++ /// Continue tracking turns an earlier process image saw start.+ pub fn restore(&self, prompt_ids: impl IntoIterator<Item = String>) {+ self.prompt_ids+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .extend(prompt_ids);+ }++ /// Whether no turn is running.+ pub fn is_idle(&self) -> bool {+ self.prompt_ids+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .is_empty()+ }+}diff --git a/crates/coding_agent_worker/src/turns/test.rs b/crates/coding_agent_worker/src/turns/test.rsnew file mode 100644index 0000000000..d412c51f62--- /dev/null+++ b/crates/coding_agent_worker/src/turns/test.rs@@ -0,0 +1,97 @@+use super::*;++#[test]+fn a_prompt_runs_until_its_response_arrives() {+ let turns = ActiveTurns::default();++ turns.observe(+ r#"{"jsonrpc":"2.0","id":7,"method":"session/prompt","params":{"sessionId":"s1","prompt":[]}}"#,+ LineDirection::Stdin,+ );+ assert!(!turns.is_idle());++ turns.observe(+ r#"{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{}}}"#,+ LineDirection::Stdout,+ );+ turns.observe(+ r#"{"jsonrpc":"2.0","id":8,"result":{}}"#,+ LineDirection::Stdout,+ );+ assert!(+ !turns.is_idle(),+ "only the prompt's own response ends the turn"+ );++ turns.observe(+ r#"{"jsonrpc":"2.0","id":7,"result":{"stopReason":"end_turn"}}"#,+ LineDirection::Stdout,+ );+ assert!(turns.is_idle());+}++#[test]+fn an_error_response_ends_the_turn() {+ let turns = ActiveTurns::default();++ turns.observe(+ r#"{"jsonrpc":"2.0","id":"a","method":"session/prompt","params":{}}"#,+ LineDirection::Stdin,+ );+ turns.observe(+ r#"{"jsonrpc":"2.0","id":"a","error":{"code":-32603,"message":"boom"}}"#,+ LineDirection::Stdout,+ );++ assert!(turns.is_idle());+}++#[test]+fn an_agent_request_with_a_prompt_id_does_not_end_the_turn() {+ let turns = ActiveTurns::default();++ turns.observe(+ r#"{"jsonrpc":"2.0","id":1,"method":"session/prompt","params":{}}"#,+ LineDirection::Stdin,+ );+ turns.observe(+ r#"{"jsonrpc":"2.0","id":1,"method":"session/request_permission","params":{}}"#,+ LineDirection::Stdout,+ );++ assert!(!turns.is_idle());+}++#[test]+fn other_requests_and_plain_text_are_not_turns() {+ let turns = ActiveTurns::default();++ turns.observe(+ r#"{"jsonrpc":"2.0","id":1,"method":"session/new","params":{"cwd":"/"}}"#,+ LineDirection::Stdin,+ );+ turns.observe("not json, mentions session/prompt", LineDirection::Stdin);+ turns.observe(+ r#"{"jsonrpc":"2.0","id":2,"method":"session/prompt","params":{}}"#,+ LineDirection::Stderr,+ );++ assert!(turns.is_idle());+}++#[test]+fn clearing_ends_every_turn() {+ let turns = ActiveTurns::default();+ turns.observe(+ r#"{"jsonrpc":"2.0","id":1,"method":"session/prompt","params":{}}"#,+ LineDirection::Stdin,+ );+ turns.observe(+ r#"{"jsonrpc":"2.0","id":2,"method":"session/prompt","params":{}}"#,+ LineDirection::Stdin,+ );++ turns.clear();++ assert!(turns.is_idle());+}diff --git a/crates/coding_agent_worker/src/update.rs b/crates/coding_agent_worker/src/update.rsnew file mode 100644index 0000000000..f96b13a6f2--- /dev/null+++ b/crates/coding_agent_worker/src/update.rs@@ -0,0 +1,343 @@+//! Self-update for a released `macrod`.+//!+//! A release build polls the repository's latest GitHub release (or the+//! `MACROD_UPDATE_SOURCE` mirror). When it is newer, the platform archive is+//! downloaded, checked against its published SHA-256, and its binary staged+//! and smoke-tested.+//!+//! On Unix the control panel then swaps the binary in and `execve`s into it+//! once no agent turn is running, handing the running agent process over+//! ([`crate::handoff`]) so it never restarts. On Windows the new version is+//! installed beside the running one and the launcher starts it from the next+//! launch ([`versions`]); the panel says so.+//!+//! Every failure is logged and leaves the running process alone; the next+//! poll tries again. Only the terminal control panel updates itself.+//! `macrod service` is driven by an embedding app (the desktop app ships and+//! updates its own copy), builds without a release tag (local and branch+//! builds) never update, and `[update] auto = false` or `MACROD_NO_UPDATE=1`+//! turns updating off.++use std::path::PathBuf;+use std::time::Duration;++use tokio::sync::oneshot;+use tokio_util::task::AbortOnDropHandle;+use tracing::Instrument as _;++mod install;+pub mod log;+mod release;+#[cfg(any(windows, test))]+pub mod versions;++use install::{Installation, StageError};+use log::{CheckOutcome, UpdateEvent};+use release::{ReleaseClient, ReleaseSource, ReleaseVersion};++#[cfg(test)]+mod test;++mod environment {+ macro_env_var::maybe_env_var! {+ /// `1` (or any value but `0`/`false`) turns self-update off.+ pub struct MacrodNoUpdate;+ }+ macro_env_var::maybe_env_var! {+ /// A mirror to update from instead of GitHub releases.+ pub struct MacrodUpdateSource;+ }+ macro_env_var::maybe_env_var! {+ /// Seconds between checks, for testing against a mirror.+ pub struct MacrodUpdateCheckSeconds;+ }+}++/// The release tag this binary was built from, set by the release workflow.+const RELEASE_TAG: Option<&str> = option_env!("MACROD_RELEASE_TAG");++/// What `macrod --version` prints: the release tag, or the crate version for+/// a build without one.+pub const VERSION: &str = match RELEASE_TAG {+ Some(tag) => tag,+ None => env!("CARGO_PKG_VERSION"),+};++/// How often the latest release is checked. GitHub allows an unauthenticated+/// client 60 API requests an hour, so this stays well inside it.+const CHECK_EVERY: Duration = Duration::from_secs(60 * 60);++/// How long the staged binary gets to print its version.+const SMOKE_TEST_TIMEOUT: Duration = Duration::from_secs(10);++/// Why one update attempt failed.+#[derive(Debug, thiserror::Error)]+enum UpdateError {+ #[error("could not read the release source")]+ Fetch(#[from] release::FetchError),+ #[error("could not stage the new binary")]+ Stage(#[from] StageError),+ #[error("the staged binary failed its smoke test: {0}")]+ SmokeTest(String),+}++/// A newer binary, verified, smoke-tested, and ready.+pub struct StagedUpdate {+ tag: String,+ #[cfg_attr(windows, expect(dead_code, reason = "Windows installs while staging"))]+ staged: PathBuf,+ #[cfg_attr(windows, expect(dead_code, reason = "Windows installs while staging"))]+ installation: Installation,+}++impl StagedUpdate {+ /// The release tag being installed.+ pub fn tag(&self) -> &str {+ &self.tag+ }++ /// Move the staged binary over the running one. The running process is+ /// unaffected until [`Installed::relaunch`].+ #[cfg(unix)]+ pub fn swap(self) -> std::io::Result<Installed> {+ let span = tracing::info_span!("macrod.update.apply", tag = %self.tag);+ let _entered = span.enter();+ self.installation.swap(&self.staged).inspect_err(+ |error| tracing::warn!(error = ?error, "could not install the new macrod binary"),+ )?;+ tracing::info!("installed the new macrod binary; restarting");+ Ok(Installed {+ executable: self.installation.executable().to_owned(),+ tag: self.tag,+ })+ }+}++/// A new binary in place of the running one, not yet running.+#[cfg(unix)]+pub struct Installed {+ executable: PathBuf,+ tag: String,+}++#[cfg(unix)]+impl Installed {+ /// Re-execute as the new binary with the same arguments and environment,+ /// handing it the running agent. Returns only on failure.+ pub fn relaunch(self, agent: Option<crate::handoff::HandedOffAgent>) -> std::io::Error {+ install::relaunch(&self.executable, &self.tag, agent)+ }+}++/// Whether this process may update itself: a release build, with updating+/// left on in the config (`auto`) and not turned off by `MACROD_NO_UPDATE`.+fn updates_enabled(release_tag: Option<&str>, auto: bool, no_update: Option<&str>) -> bool {+ let turned_off = no_update.map(str::trim).is_some_and(|value| {+ !value.is_empty() && value != "0" && !value.eq_ignore_ascii_case("false")+ });+ release_tag.and_then(ReleaseVersion::parse).is_some() && auto && !turned_off+}++/// The background update check, alive until dropped.+pub struct UpdatePoller {+ /// Resolves once, with the first newer release staged.+ pub staged: oneshot::Receiver<StagedUpdate>,+ _task: AbortOnDropHandle<()>,+}++/// Start polling for releases, or `None` when this build cannot update+/// itself or updating is turned off (`auto` is `[update] auto`).+pub fn start_poller(auto: bool) -> Option<UpdatePoller> {+ let no_update =+ environment::MacrodNoUpdate::new().and_then(|value| value.value().map(str::to_owned));+ if !updates_enabled(RELEASE_TAG, auto, no_update.as_deref()) {+ tracing::info!(version = VERSION, "self-update is off");+ return None;+ }+ let current = RELEASE_TAG.and_then(ReleaseVersion::parse)?;+ let installation = match Installation::current() {+ Ok(installation) => installation,+ Err(error) => {+ tracing::warn!(error = ?error, "could not locate the running executable; self-update is off");+ return None;+ }+ };+ installation.remove_leftovers();+ let source =+ environment::MacrodUpdateSource::new().and_then(|value| value.value().map(str::to_owned));+ let source = ReleaseSource::new(source.as_deref());+ tracing::info!(version = VERSION, source = ?source, "checking for macrod updates hourly");+ let client = match ReleaseClient::new(source) {+ Ok(client) => client,+ Err(error) => {+ tracing::warn!(error = ?error, "could not build the release client; self-update is off");+ return None;+ }+ };+ let every = environment::MacrodUpdateCheckSeconds::new()+ .and_then(|value| value.value()?.parse().ok())+ .map_or(CHECK_EVERY, Duration::from_secs);+ let (staged_tx, staged) = oneshot::channel();+ let task = tokio::spawn(poll(client, current, installation, staged_tx, every));+ Some(UpdatePoller {+ staged,+ _task: AbortOnDropHandle::new(task),+ })+}++async fn poll(+ client: ReleaseClient,+ current: ReleaseVersion,+ installation: Installation,+ staged_tx: oneshot::Sender<StagedUpdate>,+ every: Duration,+) {+ loop {+ match check_and_stage(&client, current, &installation).await {+ Ok(Some(update)) => {+ let _ = staged_tx.send(update);+ return;+ }+ Ok(None) => {}+ Err(error) => {+ tracing::warn!(error = ?error, "macrod self-update failed; retrying at the next check");+ }+ }+ tokio::time::sleep(every).await;+ }+}++async fn check_and_stage(+ client: &ReleaseClient,+ current: ReleaseVersion,+ installation: &Installation,+) -> Result<Option<StagedUpdate>, UpdateError> {+ let check = tracing::info_span!(+ "macrod.update.check",+ current = %current,+ latest = tracing::field::Empty,+ outcome = tracing::field::Empty,+ );+ let checked = |latest: Option<&str>, outcome: CheckOutcome| {+ log::record(UpdateEvent::Check {+ current: current.to_string(),+ latest: latest.map(str::to_owned),+ outcome,+ });+ };+ let latest = client+ .latest()+ .instrument(check.clone())+ .await+ .inspect_err(|_| checked(None, CheckOutcome::Failed))?;+ check.record("latest", latest.tag.as_str());+ let Some(version) = ReleaseVersion::parse(&latest.tag) else {+ check.record("outcome", "unrecognized_tag");+ checked(Some(&latest.tag), CheckOutcome::UnrecognizedTag);+ return Ok(None);+ };+ if version <= current {+ check.record("outcome", "up_to_date");+ checked(Some(&latest.tag), CheckOutcome::UpToDate);+ return Ok(None);+ }+ let Some(asset) = latest.platform_asset(client.source()) else {+ // The release job uploads every platform at once; a missing one is a+ // build that failed or a platform that is not published.+ check.record("outcome", "no_asset");+ checked(Some(&latest.tag), CheckOutcome::NoAsset);+ tracing::info!(parent: &check, tag = %latest.tag, "newer macrod has no build for this platform");+ return Ok(None);+ };+ check.record("outcome", "newer");+ checked(Some(&latest.tag), CheckOutcome::Newer);++ let download = tracing::info_span!(+ parent: &check,+ "macrod.update.download",+ tag = %latest.tag,+ asset = %asset.archive_name,+ bytes = tracing::field::Empty,+ );+ let mut bytes = 0;+ let staged = async {+ let downloaded = client.download(&asset).await?;+ bytes = downloaded.archive.len() as u64;+ tracing::Span::current().record("bytes", bytes);+ let staging = installation.clone();+ let tag = latest.tag.clone();+ let staged = tokio::task::spawn_blocking(move || {+ staging.stage(&downloaded.archive, &downloaded.checksum, &tag)+ })+ .await+ .map_err(|error| StageError::Io(std::io::Error::other(error)))??;+ smoke_test(&staged).await.inspect_err(|_| {+ let _ = std::fs::remove_file(&staged);+ })?;+ Ok::<_, UpdateError>(staged)+ }+ .instrument(download)+ .await;+ let checksum_ok = !matches!(+ staged,+ Err(UpdateError::Stage(+ StageError::ChecksumMismatch { .. } | StageError::MalformedChecksum+ )) | Err(UpdateError::Fetch(_))+ );+ log::record(UpdateEvent::Download {+ version: latest.tag.clone(),+ bytes,+ checksum_ok,+ result: match &staged {+ Ok(_) => "staged".to_owned(),+ Err(error) => error.to_string(),+ },+ });+ let staged = staged?;+ // Windows launches it from the next start; nothing to swap later.+ #[cfg(windows)]+ let staged = {+ let committed = installation.commit(&latest.tag);+ log::record(UpdateEvent::Apply {+ from: Some(VERSION.to_owned()),+ to: latest.tag.clone(),+ method: log::ApplyMethod::NextLaunch,+ active_prompts: 0,+ result: match &committed {+ Ok(_) => "installed for the next launch".to_owned(),+ Err(error) => format!("could not install: {error}"),+ },+ });+ committed.map_err(StageError::Io)?+ };++ tracing::info!(tag = %latest.tag, "downloaded a newer macrod");+ Ok(Some(StagedUpdate {+ tag: latest.tag,+ staged,+ installation: installation.clone(),+ }))+}++/// The staged binary must run on this machine before it replaces the one+/// that does.+async fn smoke_test(staged: &std::path::Path) -> Result<(), UpdateError> {+ let output = tokio::time::timeout(+ SMOKE_TEST_TIMEOUT,+ tokio::process::Command::new(staged)+ .arg("--version")+ .kill_on_drop(true)+ .output(),+ )+ .await+ .map_err(|_| UpdateError::SmokeTest("timed out".to_owned()))?+ .map_err(|error| UpdateError::SmokeTest(error.to_string()))?;+ if output.status.success() {+ Ok(())+ } else {+ Err(UpdateError::SmokeTest(format!(+ "exited with {}",+ output.status+ )))+ }+}diff --git a/crates/coding_agent_worker/src/update/install.rs b/crates/coding_agent_worker/src/update/install.rsnew file mode 100644index 0000000000..8524829fa2--- /dev/null+++ b/crates/coding_agent_worker/src/update/install.rs@@ -0,0 +1,235 @@+//! Staging a downloaded release next to the running executable, swapping it+//! in, and re-executing.++use std::io::Read as _;+use std::path::{Path, PathBuf};++use sha2::{Digest as _, Sha256};++#[cfg(all(test, unix))]+mod test;++/// The binary's name inside a release archive.+const EXECUTABLE_NAME: &str = if cfg!(windows) {+ "macrod.exe"+} else {+ "macrod"+};++/// Why a downloaded archive could not be staged.+#[derive(Debug, thiserror::Error)]+pub enum StageError {+ #[error("the checksum file holds no SHA-256 digest")]+ MalformedChecksum,+ #[error("checksum mismatch: published {published}, downloaded {downloaded}")]+ ChecksumMismatch {+ published: String,+ downloaded: String,+ },+ #[error("the archive holds no {EXECUTABLE_NAME}")]+ MissingExecutable,+ #[cfg(windows)]+ #[error("could not read the zip archive")]+ Zip(#[from] zip::result::ZipError),+ #[error(transparent)]+ Io(#[from] std::io::Error),+}++/// Where the running executable lives, and where the updater puts a new+/// one: beside it on Unix, so the swap is a rename on one filesystem; in the+/// installation's `versions` folder on Windows ([`super::versions`]).+#[derive(Debug, Clone)]+pub struct Installation {+ executable: PathBuf,+}++impl Installation {+ /// The running executable, with symlinks resolved so the swap replaces+ /// the real file rather than the link to it.+ pub fn current() -> std::io::Result<Self> {+ Ok(Self::at(std::env::current_exe()?.canonicalize()?))+ }++ fn at(executable: PathBuf) -> Self {+ Self { executable }+ }++ pub fn executable(&self) -> &Path {+ &self.executable+ }++ /// The verified new binary, waiting to be swapped in.+ #[cfg(not(windows))]+ fn staged_path(&self, _tag: &str) -> PathBuf {+ self.executable.with_file_name(".macrod.new")+ }++ #[cfg(windows)]+ fn staged_path(&self, tag: &str) -> PathBuf {+ super::versions::Versions::of_executable(&self.executable).staging_executable(tag)+ }++ /// Remove a staged binary an earlier run never swapped in. Best effort.+ pub fn remove_leftovers(&self) {+ #[cfg(not(windows))]+ {+ let leftover = self.staged_path("");+ if let Err(error) = std::fs::remove_file(&leftover)+ && error.kind() != std::io::ErrorKind::NotFound+ {+ tracing::debug!(error = ?error, path = %leftover.display(), "could not remove an updater leftover");+ }+ }+ }++ /// Verify `archive` against the published `checksum` file, then write+ /// its executable to the staged path for `tag`. Nothing is left behind+ /// on failure.+ pub fn stage(&self, archive: &[u8], checksum: &str, tag: &str) -> Result<PathBuf, StageError> {+ verify_checksum(archive, checksum)?;+ let binary = extract_executable(archive)?;+ let staged = self.staged_path(tag);+ if let Some(parent) = staged.parent() {+ std::fs::create_dir_all(parent)?;+ }+ write_executable(&staged, &binary).inspect_err(|_| {+ let _ = std::fs::remove_file(&staged);+ })?;+ Ok(staged)+ }++ /// Put the staged binary where the running one is. The rename replaces+ /// it atomically while the running process keeps its open inode.+ #[cfg(not(windows))]+ pub fn swap(&self, staged: &Path) -> std::io::Result<()> {+ std::fs::rename(staged, &self.executable)+ }++ /// Make the staged version the one the launcher starts next.+ #[cfg(windows)]+ pub fn commit(&self, tag: &str) -> std::io::Result<PathBuf> {+ super::versions::Versions::of_executable(&self.executable).commit(tag)+ }+}++/// The checksum file is `sha256sum` output: the hex digest, then the name.+fn verify_checksum(archive: &[u8], checksum: &str) -> Result<(), StageError> {+ let published = checksum+ .split_whitespace()+ .next()+ .filter(|digest| lazy_regex::regex_is_match!(r"^[0-9a-fA-F]{64}$", digest))+ .ok_or(StageError::MalformedChecksum)?+ .to_ascii_lowercase();+ let downloaded = hex::encode(Sha256::digest(archive));+ if published == downloaded {+ Ok(())+ } else {+ Err(StageError::ChecksumMismatch {+ published,+ downloaded,+ })+ }+}++#[cfg(not(windows))]+fn extract_executable(archive: &[u8]) -> Result<Vec<u8>, StageError> {+ let mut entries = tar::Archive::new(flate2::read::GzDecoder::new(archive));+ for entry in entries.entries()? {+ let mut entry = entry?;+ if entry.path()?.as_ref() == Path::new(EXECUTABLE_NAME) {+ let mut binary = Vec::new();+ entry.read_to_end(&mut binary)?;+ return Ok(binary);+ }+ }+ Err(StageError::MissingExecutable)+}++#[cfg(windows)]+fn extract_executable(archive: &[u8]) -> Result<Vec<u8>, StageError> {+ let mut archive = zip::ZipArchive::new(std::io::Cursor::new(archive))?;+ let mut entry = match archive.by_name(EXECUTABLE_NAME) {+ Ok(entry) => entry,+ Err(zip::result::ZipError::FileNotFound) => return Err(StageError::MissingExecutable),+ Err(error) => return Err(error.into()),+ };+ let mut binary = Vec::new();+ entry.read_to_end(&mut binary)?;+ Ok(binary)+}++fn write_executable(path: &Path, binary: &[u8]) -> std::io::Result<()> {+ use std::io::Write as _;++ if let Err(error) = std::fs::remove_file(path)+ && error.kind() != std::io::ErrorKind::NotFound+ {+ return Err(error);+ }+ let mut options = std::fs::OpenOptions::new();+ options.write(true).create_new(true);+ #[cfg(unix)]+ {+ use std::os::unix::fs::OpenOptionsExt as _;+ options.mode(0o755);+ }+ let mut file = options.open(path)?;+ file.write_all(binary)?;+ file.sync_all()+}++/// Replace this process with `executable`, with the same arguments and+/// environment, handing it `agent` when there is one. Returns only on+/// failure, and then the agent is stopped.+///+/// Takes the path recorded before the swap: once the running file is+/// replaced, Linux reports this process's executable as deleted.+#[cfg(unix)]+pub fn relaunch(+ executable: &Path,+ tag: &str,+ agent: Option<crate::handoff::HandedOffAgent>,+) -> std::io::Error {+ use std::os::unix::process::CommandExt as _;++ use super::log::{self, ApplyMethod, UpdateEvent};++ let mut args = std::env::args_os();+ let mut command = std::process::Command::new(executable);+ if let Some(arg0) = args.next() {+ command.arg0(arg0);+ }+ command.args(args).env_remove(crate::handoff::HANDOFF_ENV);+ // Held open until `exec`: the new image reads the state from it.+ let mut _state = None;+ let mut method = ApplyMethod::ExecveHandoff;+ let active_prompts = agent+ .as_ref()+ .map_or(0, crate::handoff::HandedOffAgent::active_prompt_count);+ if let Some(agent) = &agent {+ match agent.prepare_exec() {+ Ok((state, number)) => {+ command.env(crate::handoff::HANDOFF_ENV, number);+ _state = Some(state);+ }+ Err(error) => {+ method = ApplyMethod::FreshAgentFallback;+ tracing::warn!(error = %error, "could not hand the agent over; it restarts with macrod");+ }+ }+ }+ let applied = |result: String| {+ log::record(UpdateEvent::Apply {+ from: Some(super::VERSION.to_owned()),+ to: tag.to_owned(),+ method,+ active_prompts,+ result,+ });+ };+ applied("restarting".to_owned());+ let error = command.exec();+ applied(format!("restart failed: {error}"));+ drop(agent);+ error+}diff --git a/crates/coding_agent_worker/src/update/install/test.rs b/crates/coding_agent_worker/src/update/install/test.rsnew file mode 100644index 0000000000..d5cbaa705f--- /dev/null+++ b/crates/coding_agent_worker/src/update/install/test.rs@@ -0,0 +1,161 @@+use super::*;++/// A release archive the way the workflow packs one: `tar -czf` of one file.+fn archive_with(name: &str, contents: &[u8]) -> Vec<u8> {+ let mut builder = tar::Builder::new(flate2::write::GzEncoder::new(+ Vec::new(),+ flate2::Compression::default(),+ ));+ let mut header = tar::Header::new_gnu();+ header.set_size(contents.len() as u64);+ header.set_mode(0o755);+ header.set_cksum();+ builder.append_data(&mut header, name, contents).unwrap();+ builder.into_inner().unwrap().finish().unwrap()+}++fn checksum_file(archive: &[u8], name: &str) -> String {+ format!("{} {name}\n", hex::encode(Sha256::digest(archive)))+}++#[test]+fn stages_the_verified_binary_next_to_the_executable() {+ let directory = tempfile::tempdir().unwrap();+ let installation = Installation::at(directory.path().join("macrod"));+ let archive = archive_with("macrod", b"new macrod");++ let staged = installation+ .stage(+ &archive,+ &checksum_file(&archive, "macrod-v2026.10.27.2-linux-x86_64.tar.gz"),+ "v2026.10.27.2",+ )+ .unwrap();++ assert_eq!(staged, directory.path().join(".macrod.new"));+ assert_eq!(std::fs::read(&staged).unwrap(), b"new macrod");+ {+ use std::os::unix::fs::PermissionsExt as _;+ let mode = std::fs::metadata(&staged).unwrap().permissions().mode();+ assert_eq!(mode & 0o777, 0o755);+ }+}++#[test]+fn restaging_replaces_an_earlier_staged_binary() {+ let directory = tempfile::tempdir().unwrap();+ let installation = Installation::at(directory.path().join("macrod"));+ std::fs::write(+ directory.path().join(".macrod.new"),+ b"interrupted download",+ )+ .unwrap();+ let archive = archive_with("macrod", b"new macrod");++ let staged = installation+ .stage(+ &archive,+ &checksum_file(&archive, "macrod.tar.gz"),+ "v2026.10.27.2",+ )+ .unwrap();++ assert_eq!(std::fs::read(staged).unwrap(), b"new macrod");+}++#[test]+fn a_checksum_mismatch_stages_nothing() {+ let directory = tempfile::tempdir().unwrap();+ let installation = Installation::at(directory.path().join("macrod"));+ let archive = archive_with("macrod", b"new macrod");+ let other = archive_with("macrod", b"tampered macrod");++ let error = installation+ .stage(+ &archive,+ &checksum_file(&other, "macrod.tar.gz"),+ "v2026.10.27.2",+ )+ .unwrap_err();++ assert!(+ matches!(error, StageError::ChecksumMismatch { .. }),+ "{error:?}"+ );+ assert!(!directory.path().join(".macrod.new").exists());+}++#[test]+fn a_checksum_file_without_a_digest_is_rejected() {+ let directory = tempfile::tempdir().unwrap();+ let installation = Installation::at(directory.path().join("macrod"));+ let archive = archive_with("macrod", b"new macrod");++ for checksum in ["", "Not Found", "abc123 macrod.tar.gz"] {+ let error = installation+ .stage(&archive, checksum, "v2026.10.27.2")+ .unwrap_err();+ assert!(+ matches!(error, StageError::MalformedChecksum),+ "{checksum:?}: {error:?}"+ );+ }+}++#[test]+fn an_archive_without_the_binary_stages_nothing() {+ let directory = tempfile::tempdir().unwrap();+ let installation = Installation::at(directory.path().join("macrod"));+ let archive = archive_with("README.md", b"not a binary");++ let error = installation+ .stage(+ &archive,+ &checksum_file(&archive, "macrod.tar.gz"),+ "v2026.10.27.2",+ )+ .unwrap_err();++ assert!(matches!(error, StageError::MissingExecutable), "{error:?}");+ assert!(!directory.path().join(".macrod.new").exists());+}++#[test]+fn swapping_replaces_the_executable_and_leaves_nothing_staged() {+ let directory = tempfile::tempdir().unwrap();+ let executable = directory.path().join("macrod");+ std::fs::write(&executable, b"old macrod").unwrap();+ let installation = Installation::at(executable.clone());+ let archive = archive_with("macrod", b"new macrod");+ let staged = installation+ .stage(+ &archive,+ &checksum_file(&archive, "macrod.tar.gz"),+ "v2026.10.27.2",+ )+ .unwrap();+ // A process running the old binary holds it open across the swap.+ let running = std::fs::File::open(&executable).unwrap();++ installation.swap(&staged).unwrap();++ assert_eq!(std::fs::read(&executable).unwrap(), b"new macrod");+ assert!(!staged.exists());+ let mut still_running = Vec::new();+ std::io::Read::read_to_end(&mut &running, &mut still_running).unwrap();+ assert_eq!(still_running, b"old macrod");+}++#[test]+fn leftovers_from_an_earlier_run_are_removed() {+ let directory = tempfile::tempdir().unwrap();+ let executable = directory.path().join("macrod");+ std::fs::write(&executable, b"macrod").unwrap();+ std::fs::write(directory.path().join(".macrod.new"), b"never swapped").unwrap();+ let installation = Installation::at(executable.clone());++ installation.remove_leftovers();++ assert!(!directory.path().join(".macrod.new").exists());+ assert_eq!(std::fs::read(executable).unwrap(), b"macrod");+}diff --git a/crates/coding_agent_worker/src/update/log.rs b/crates/coding_agent_worker/src/update/log.rsnew file mode 100644index 0000000000..86767aba09--- /dev/null+++ b/crates/coding_agent_worker/src/update/log.rs@@ -0,0 +1,164 @@+//! The persistent update log: one JSON line per self-update event in+//! `~/.macrod/logs/update.log`, so an operator can confirm what the updater+//! did after the panel that showed it has restarted. Versions, sizes and+//! outcomes only: never tokens, prompts, or agent output.++use std::io::Write as _;+use std::path::{Path, PathBuf};++#[cfg(test)]+mod test;++mod environment {+ macro_env_var::maybe_env_var! {+ pub struct Home;+ }+ #[cfg(windows)]+ macro_env_var::maybe_env_var! {+ pub struct Userprofile;+ }+}++/// The log rotates to `update.log.1` (replacing an older one) at this size.+const MAX_BYTES: u64 = 1024 * 1024;++/// What a check found.+#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]+#[serde(rename_all = "snake_case")]+pub enum CheckOutcome {+ UpToDate,+ Newer,+ NoAsset,+ UnrecognizedTag,+ Failed,+}++/// How a new version was put into use.+#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]+#[serde(rename_all = "kebab-case")]+pub enum ApplyMethod {+ /// Unix: `execve` into the new binary, handing over the running agent.+ ExecveHandoff,+ /// Unix: `execve` into the new binary, but the agent could not be handed+ /// over and the new image starts a fresh one.+ FreshAgentFallback,+ /// Windows: installed beside the running version, used from the next+ /// launch.+ #[cfg_attr(+ not(windows),+ expect(dead_code, reason = "only Windows installs for the next launch")+ )]+ NextLaunch,+}++/// One line of the log.+#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]+#[serde(tag = "event", rename_all = "snake_case")]+pub enum UpdateEvent {+ /// The control panel started.+ Startup { version: String, from_handoff: bool },+ Check {+ current: String,+ latest: Option<String>,+ outcome: CheckOutcome,+ },+ Download {+ version: String,+ bytes: u64,+ checksum_ok: bool,+ result: String,+ },+ Apply {+ /// `None` when the new image reports it and the old version is gone.+ from: Option<String>,+ to: String,+ method: ApplyMethod,+ active_prompts: usize,+ result: String,+ },+}++#[derive(serde::Serialize)]+struct Line<'a> {+ time: String,+ #[serde(flatten)]+ event: &'a UpdateEvent,+}++/// An update log file.+pub struct UpdateLog {+ path: PathBuf,+ max_bytes: u64,+}++impl UpdateLog {+ /// `~/.macrod/logs/update.log` (`%USERPROFILE%` on Windows), when the+ /// home directory is known.+ pub fn for_user() -> Option<Self> {+ let home = environment::Home::new().and_then(|home| home.value().map(PathBuf::from));+ #[cfg(windows)]+ let home = home.or_else(|| {+ environment::Userprofile::new().and_then(|home| home.value().map(PathBuf::from))+ });+ Some(Self::at(+ home?.join(".macrod").join("logs").join("update.log"),+ MAX_BYTES,+ ))+ }++ fn at(path: PathBuf, max_bytes: u64) -> Self {+ Self { path, max_bytes }+ }++ /// Append `event`. Best effort: a log that cannot be written is reported+ /// in the panel's logs and never stops an update.+ pub fn record(&self, event: &UpdateEvent) {+ if let Err(error) = self.append(event) {+ tracing::warn!(error = %error, path = %self.path.display(), "could not write the update log");+ }+ }++ fn append(&self, event: &UpdateEvent) -> std::io::Result<()> {+ let directory = self.path.parent().unwrap_or(Path::new("."));+ let mut builder = std::fs::DirBuilder::new();+ builder.recursive(true);+ #[cfg(unix)]+ {+ use std::os::unix::fs::DirBuilderExt as _;+ builder.mode(0o700);+ }+ builder.create(directory)?;+ if std::fs::metadata(&self.path).is_ok_and(|metadata| metadata.len() >= self.max_bytes) {+ let mut rotated = self.path.clone().into_os_string();+ rotated.push(".1");+ std::fs::rename(&self.path, rotated)?;+ }+ let mut line = serde_json::to_vec(&Line {+ time: chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),+ event,+ })+ .map_err(std::io::Error::other)?;+ line.push(b'\n');+ let mut options = std::fs::OpenOptions::new();+ options.create(true).append(true);+ #[cfg(unix)]+ {+ use std::os::unix::fs::OpenOptionsExt as _;+ options.mode(0o600);+ }+ // One write per line, so concurrent writers interleave whole lines.+ options.open(&self.path)?.write_all(&line)+ }+}++/// Append `event` to the user's update log.+pub fn record(event: UpdateEvent) {+ if let Some(log) = UpdateLog::for_user() {+ log.record(&event);+ }+}++/// Where the user's update log is, for the panel to point at.+pub fn user_log_path() -> Option<PathBuf> {+ UpdateLog::for_user().map(|log| log.path)+}diff --git a/crates/coding_agent_worker/src/update/log/test.rs b/crates/coding_agent_worker/src/update/log/test.rsnew file mode 100644index 0000000000..5a63befa43--- /dev/null+++ b/crates/coding_agent_worker/src/update/log/test.rs@@ -0,0 +1,108 @@+use super::*;++fn lines(path: &Path) -> Vec<serde_json::Value> {+ std::fs::read_to_string(path)+ .unwrap()+ .lines()+ .map(|line| serde_json::from_str(line).unwrap())+ .collect()+}++#[test]+fn each_event_is_one_json_line() {+ let root = tempfile::tempdir().unwrap();+ let path = root.path().join(".macrod/logs/update.log");+ let log = UpdateLog::at(path.clone(), MAX_BYTES);++ log.record(&UpdateEvent::Startup {+ version: "v2026.10.27.1".to_owned(),+ from_handoff: false,+ });+ log.record(&UpdateEvent::Check {+ current: "v2026.10.27.1".to_owned(),+ latest: Some("v2026.10.27.2".to_owned()),+ outcome: CheckOutcome::Newer,+ });+ log.record(&UpdateEvent::Download {+ version: "v2026.10.27.2".to_owned(),+ bytes: 20_971_520,+ checksum_ok: true,+ result: "staged".to_owned(),+ });+ log.record(&UpdateEvent::Apply {+ from: Some("v2026.10.27.1".to_owned()),+ to: "v2026.10.27.2".to_owned(),+ method: ApplyMethod::ExecveHandoff,+ active_prompts: 0,+ result: "restarting".to_owned(),+ });++ let mut written = lines(&path);+ for line in &mut written {+ let time = line+ .as_object_mut()+ .unwrap()+ .remove("time")+ .expect("every line is timestamped");+ assert!(+ chrono::DateTime::parse_from_rfc3339(time.as_str().unwrap()).is_ok(),+ "{time}"+ );+ }+ assert_eq!(+ written,+ [+ serde_json::json!({"event": "startup", "version": "v2026.10.27.1", "from_handoff": false}),+ serde_json::json!({"event": "check", "current": "v2026.10.27.1", "latest": "v2026.10.27.2", "outcome": "newer"}),+ serde_json::json!({"event": "download", "version": "v2026.10.27.2", "bytes": 20_971_520, "checksum_ok": true, "result": "staged"}),+ serde_json::json!({"event": "apply", "from": "v2026.10.27.1", "to": "v2026.10.27.2", "method": "execve-handoff", "active_prompts": 0, "result": "restarting"}),+ ]+ );+}++#[cfg(unix)]+#[test]+fn the_log_is_private_to_its_owner() {+ use std::os::unix::fs::PermissionsExt as _;++ let root = tempfile::tempdir().unwrap();+ let path = root.path().join(".macrod/logs/update.log");+ UpdateLog::at(path.clone(), MAX_BYTES).record(&UpdateEvent::Startup {+ version: "v2026.10.27.1".to_owned(),+ from_handoff: true,+ });++ let mode = |path: &Path| std::fs::metadata(path).unwrap().permissions().mode() & 0o777;+ assert_eq!(mode(&path), 0o600);+ assert_eq!(mode(path.parent().unwrap()), 0o700);+}++#[test]+fn a_full_log_rotates_keeping_one_previous_file() {+ let root = tempfile::tempdir().unwrap();+ let path = root.path().join("update.log");+ let log = UpdateLog::at(path.clone(), 200);+ let startup = |version: &str| UpdateEvent::Startup {+ version: version.to_owned(),+ from_handoff: false,+ };++ // Lines are about 90 bytes, so every third one takes the file past 200.+ for version in ["v1", "v2", "v3", "v4", "v5", "v6", "v7"] {+ log.record(&startup(version));+ }++ let version = |line: &serde_json::Value| line["version"].as_str().unwrap().to_owned();+ let current: Vec<_> = lines(&path).iter().map(version).collect();+ let previous: Vec<_> = lines(&root.path().join("update.log.1"))+ .iter()+ .map(version)+ .collect();+ assert_eq!(previous, ["v4", "v5", "v6"]);+ assert_eq!(current, ["v7"]);+ let files: Vec<_> = std::fs::read_dir(root.path())+ .unwrap()+ .map(|entry| entry.unwrap().file_name())+ .collect();+ assert_eq!(files.len(), 2, "{files:?}");+}diff --git a/crates/coding_agent_worker/src/update/release.rs b/crates/coding_agent_worker/src/update/release.rsnew file mode 100644index 0000000000..4376fbb538--- /dev/null+++ b/crates/coding_agent_worker/src/update/release.rs@@ -0,0 +1,236 @@+//! The latest macrod release on GitHub and this platform's asset in it.++use std::time::Duration;++#[cfg(test)]+mod test;++/// The release the install scripts and the updater both follow.+const LATEST_RELEASE_URL: &str = "https://api.github.com/repos/macro-inc/macro/releases/latest";++/// What a mirror serves in place of GitHub's release API: the same object,+/// of which only `tag_name` is read.+const MIRROR_MANIFEST: &str = "latest.json";++/// Where releases come from.+#[derive(Debug, Clone, PartialEq, Eq)]+pub enum ReleaseSource {+ /// The repository's GitHub releases.+ GitHub,+ /// `MACROD_UPDATE_SOURCE`: a base URL or a local directory holding+ /// `latest.json` (GitHub's release object; `tag_name` is enough) and the+ /// release's archives and `.sha256` files beside it, named as on GitHub.+ /// For testing an update without publishing one.+ Mirror(String),+}++impl ReleaseSource {+ /// The mirror named by `override_source`, or GitHub.+ pub fn new(override_source: Option<&str>) -> Self {+ match override_source.map(str::trim) {+ Some(source) if !source.is_empty() => {+ Self::Mirror(source.trim_end_matches('/').to_owned())+ }+ _ => Self::GitHub,+ }+ }++ fn mirror_file(base: &str, name: &str) -> String {+ format!("{base}/{name}")+ }+}++/// Covers a slow connection pulling a ~30 MB archive.+const REQUEST_TIMEOUT: Duration = Duration::from_secs(5 * 60);++/// The archive format the release workflow uses for this platform.+#[cfg(windows)]+const ARCHIVE_EXTENSION: &str = "zip";+#[cfg(not(windows))]+const ARCHIVE_EXTENSION: &str = "tar.gz";++/// A release tag's position in the release order. Tags are calendar+/// versions, `vYYYY.MM.DD.N`; older tags carry no build number, which counts+/// as build 0.+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]+pub struct ReleaseVersion {+ year: u16,+ month: u8,+ day: u8,+ build: u16,+}++impl ReleaseVersion {+ /// The version a release tag names, or `None` for any other ref (a+ /// branch build, a pre-release suffix).+ pub fn parse(tag: &str) -> Option<Self> {+ let (_, year, month, day, build) =+ lazy_regex::regex_captures!(r"^v(\d{4})\.(\d{1,2})\.(\d{1,2})(?:\.(\d{1,5}))?$", tag)?;+ Some(Self {+ year: year.parse().ok()?,+ month: month.parse().ok()?,+ day: day.parse().ok()?,+ build: if build.is_empty() {+ 0+ } else {+ build.parse().ok()?+ },+ })+ }+}++impl std::fmt::Display for ReleaseVersion {+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ write!(+ formatter,+ "v{}.{}.{}.{}",+ self.year, self.month, self.day, self.build+ )+ }+}++/// The parts of GitHub's release object the updater reads.+#[derive(Debug, serde::Deserialize)]+pub struct Release {+ #[serde(rename = "tag_name")]+ pub tag: String,+ #[serde(default)]+ assets: Vec<ReleaseAsset>,+}++#[derive(Debug, serde::Deserialize)]+struct ReleaseAsset {+ name: String,+ browser_download_url: String,+}++/// This platform's archive in a release, with its checksum file.+#[derive(Debug, PartialEq, Eq)]+pub struct PlatformAsset {+ pub archive_name: String,+ archive_url: String,+ checksum_url: String,+}++impl Release {+ /// The tagged archive for this OS and CPU, e.g.+ /// `macrod-v2026.10.27.2-linux-x86_64.tar.gz`, and its `.sha256`. The+ /// tagged name rather than the version-less one, so the download cannot+ /// race a newer release.+ pub fn platform_asset(&self, source: &ReleaseSource) -> Option<PlatformAsset> {+ self.asset_for(+ source,+ std::env::consts::OS,+ std::env::consts::ARCH,+ ARCHIVE_EXTENSION,+ )+ }++ fn asset_for(+ &self,+ source: &ReleaseSource,+ os: &str,+ arch: &str,+ extension: &str,+ ) -> Option<PlatformAsset> {+ let archive_name = format!("macrod-{}-{os}-{arch}.{extension}", self.tag);+ let checksum_name = format!("{archive_name}.sha256");+ let url_of = |name: &str| match source {+ ReleaseSource::GitHub => self+ .assets+ .iter()+ .find(|asset| asset.name == name)+ .map(|asset| asset.browser_download_url.clone()),+ // A mirror's files are found by name; a missing one fails the+ // download instead.+ ReleaseSource::Mirror(base) => Some(ReleaseSource::mirror_file(base, name)),+ };+ Some(PlatformAsset {+ archive_url: url_of(&archive_name)?,+ checksum_url: url_of(&checksum_name)?,+ archive_name,+ })+ }+}++/// An archive and the checksum file published beside it.+pub struct Downloaded {+ pub archive: Vec<u8>,+ pub checksum: String,+}++/// Reads releases from GitHub or a mirror.+pub struct ReleaseClient {+ http: reqwest::Client,+ source: ReleaseSource,+}++impl ReleaseClient {+ pub fn new(source: ReleaseSource) -> reqwest::Result<Self> {+ let http = reqwest::Client::builder()+ // GitHub's API rejects requests without a user agent.+ .user_agent(format!("macrod/{}", super::VERSION))+ .timeout(REQUEST_TIMEOUT)+ .build()?;+ Ok(Self { http, source })+ }++ pub fn source(&self) -> &ReleaseSource {+ &self.source+ }++ /// The newest published, non-prerelease release.+ pub async fn latest(&self) -> Result<Release, FetchError> {+ let manifest = match &self.source {+ ReleaseSource::GitHub => {+ return Ok(self+ .http+ .get(LATEST_RELEASE_URL)+ .header(reqwest::header::ACCEPT, "application/vnd.github+json")+ .send()+ .await?+ .error_for_status()?+ .json()+ .await?);+ }+ ReleaseSource::Mirror(base) => ReleaseSource::mirror_file(base, MIRROR_MANIFEST),+ };+ Ok(serde_json::from_slice(&self.fetch(&manifest).await?)?)+ }++ pub async fn download(&self, asset: &PlatformAsset) -> Result<Downloaded, FetchError> {+ let checksum =+ String::from_utf8_lossy(&self.fetch(&asset.checksum_url).await?).into_owned();+ let archive = self.fetch(&asset.archive_url).await?;+ Ok(Downloaded { archive, checksum })+ }++ /// An http(s) URL, or a path on this machine (a mirror directory).+ async fn fetch(&self, location: &str) -> Result<Vec<u8>, FetchError> {+ if location.starts_with("http://") || location.starts_with("https://") {+ Ok(self+ .http+ .get(location)+ .send()+ .await?+ .error_for_status()?+ .bytes()+ .await?+ .to_vec())+ } else {+ let path = location.strip_prefix("file://").unwrap_or(location);+ Ok(tokio::fs::read(path).await?)+ }+ }+}++/// Why a release or its files could not be read.+#[derive(Debug, thiserror::Error)]+pub enum FetchError {+ #[error(transparent)]+ Http(#[from] reqwest::Error),+ #[error(transparent)]+ File(#[from] std::io::Error),+ #[error("malformed release manifest")]+ Manifest(#[from] serde_json::Error),+}diff --git a/crates/coding_agent_worker/src/update/release/test.rs b/crates/coding_agent_worker/src/update/release/test.rsnew file mode 100644index 0000000000..5ed1b1c096--- /dev/null+++ b/crates/coding_agent_worker/src/update/release/test.rs@@ -0,0 +1,133 @@+use super::*;++#[test]+fn release_tags_order_by_calendar_then_build() {+ let older = ReleaseVersion::parse("v2026.10.27.1").unwrap();+ let newer = ReleaseVersion::parse("v2026.10.27.2").unwrap();+ assert!(newer > older);++ // Numeric, not lexical: day 9 is before day 10, September before October.+ assert!(ReleaseVersion::parse("v2026.10.10.0") > ReleaseVersion::parse("v2026.10.9.5"));+ assert!(ReleaseVersion::parse("v2026.10.1.0") > ReleaseVersion::parse("v2026.9.30.9"));+ assert!(ReleaseVersion::parse("v2027.1.1.0") > ReleaseVersion::parse("v2026.12.31.9"));+}++#[test]+fn a_tag_without_a_build_number_is_build_zero() {+ assert_eq!(+ ReleaseVersion::parse("v2026.10.7"),+ ReleaseVersion::parse("v2026.10.7.0")+ );+ assert!(ReleaseVersion::parse("v2026.10.7.1") > ReleaseVersion::parse("v2026.10.7"));+}++#[test]+fn refs_that_are_not_release_tags_have_no_version() {+ for tag in [+ "2026.10.27.0",+ "v2026.10.27.0-rc1",+ "v2026.10",+ "wolf/macrod-self-update",+ "untagged",+ "",+ ] {+ assert_eq!(ReleaseVersion::parse(tag), None, "{tag}");+ }+}++#[test]+fn the_platform_asset_is_the_tagged_archive_with_its_checksum() {+ let release: Release = serde_json::from_str(+ r#"{+ "tag_name": "v2026.10.27.2",+ "assets": [+ {"name": "macrod-linux-x86_64.tar.gz", "browser_download_url": "https://example.test/latest.tar.gz"},+ {"name": "macrod-linux-x86_64.tar.gz.sha256", "browser_download_url": "https://example.test/latest.tar.gz.sha256"},+ {"name": "macrod-v2026.10.27.2-linux-x86_64.tar.gz", "browser_download_url": "https://example.test/tagged.tar.gz"},+ {"name": "macrod-v2026.10.27.2-linux-x86_64.tar.gz.sha256", "browser_download_url": "https://example.test/tagged.tar.gz.sha256"},+ {"name": "macrod-v2026.10.27.2-windows-x86_64.zip", "browser_download_url": "https://example.test/tagged.zip"}+ ]+ }"#,+ )+ .unwrap();++ assert_eq!(+ release.asset_for(&ReleaseSource::GitHub, "linux", "x86_64", "tar.gz"),+ Some(PlatformAsset {+ archive_name: "macrod-v2026.10.27.2-linux-x86_64.tar.gz".to_owned(),+ archive_url: "https://example.test/tagged.tar.gz".to_owned(),+ checksum_url: "https://example.test/tagged.tar.gz.sha256".to_owned(),+ })+ );+ // An archive without its checksum is not installable.+ assert_eq!(+ release.asset_for(&ReleaseSource::GitHub, "windows", "x86_64", "zip"),+ None+ );+ assert_eq!(+ release.asset_for(&ReleaseSource::GitHub, "macos", "aarch64", "tar.gz"),+ None+ );+}++#[test]+fn an_update_source_override_replaces_github() {+ assert_eq!(ReleaseSource::new(None), ReleaseSource::GitHub);+ assert_eq!(ReleaseSource::new(Some(" ")), ReleaseSource::GitHub);+ assert_eq!(+ ReleaseSource::new(Some("http://127.0.0.1:8000/releases/")),+ ReleaseSource::Mirror("http://127.0.0.1:8000/releases".to_owned())+ );+}++#[test]+fn a_mirror_serves_the_tagged_archive_beside_its_manifest() {+ // A mirror's manifest is GitHub's release object; only the tag is needed.+ let release: Release = serde_json::from_str(r#"{"tag_name": "v2026.10.28.2"}"#).unwrap();+ let mirror = ReleaseSource::new(Some("/srv/macrod"));++ assert_eq!(+ release.asset_for(&mirror, "linux", "x86_64", "tar.gz"),+ Some(PlatformAsset {+ archive_name: "macrod-v2026.10.28.2-linux-x86_64.tar.gz".to_owned(),+ archive_url: "/srv/macrod/macrod-v2026.10.28.2-linux-x86_64.tar.gz".to_owned(),+ checksum_url: "/srv/macrod/macrod-v2026.10.28.2-linux-x86_64.tar.gz.sha256".to_owned(),+ })+ );+}++#[tokio::test]+async fn a_directory_mirror_is_read_from_disk() {+ let mirror = tempfile::tempdir().unwrap();+ std::fs::write(+ mirror.path().join("latest.json"),+ r#"{"tag_name": "v2026.10.28.2", "assets": []}"#,+ )+ .unwrap();+ std::fs::write(+ mirror+ .path()+ .join("macrod-v2026.10.28.2-linux-x86_64.tar.gz"),+ b"archive",+ )+ .unwrap();+ std::fs::write(+ mirror+ .path()+ .join("macrod-v2026.10.28.2-linux-x86_64.tar.gz.sha256"),+ b"digest name\n",+ )+ .unwrap();+ let source = ReleaseSource::new(mirror.path().to_str());+ let client = ReleaseClient::new(source.clone()).unwrap();++ let release = client.latest().await.unwrap();+ let asset = release+ .asset_for(&source, "linux", "x86_64", "tar.gz")+ .unwrap();+ let downloaded = client.download(&asset).await.unwrap();++ assert_eq!(release.tag, "v2026.10.28.2");+ assert_eq!(downloaded.archive, b"archive");+ assert_eq!(downloaded.checksum, "digest name\n");+}diff --git a/crates/coding_agent_worker/src/update/test.rs b/crates/coding_agent_worker/src/update/test.rsnew file mode 100644index 0000000000..9b1735f43c--- /dev/null+++ b/crates/coding_agent_worker/src/update/test.rs@@ -0,0 +1,29 @@+use super::*;++#[test]+fn only_release_builds_update() {+ assert!(updates_enabled(Some("v2026.10.28.1"), true, None));+ assert!(!updates_enabled(None, true, None));+ assert!(!updates_enabled(+ Some("wolf/macrod-self-update"),+ true,+ None+ ));+}++#[test]+fn the_config_and_the_environment_each_turn_updates_off() {+ assert!(!updates_enabled(Some("v2026.10.28.1"), false, None));+ for value in ["1", "true", "yes"] {+ assert!(+ !updates_enabled(Some("v2026.10.28.1"), true, Some(value)),+ "{value}"+ );+ }+ for value in ["", "0", "false", "FALSE"] {+ assert!(+ updates_enabled(Some("v2026.10.28.1"), true, Some(value)),+ "{value:?}"+ );+ }+}diff --git a/crates/coding_agent_worker/src/update/versions.rs b/crates/coding_agent_worker/src/update/versions.rsnew file mode 100644index 0000000000..589287180c--- /dev/null+++ b/crates/coding_agent_worker/src/update/versions.rs@@ -0,0 +1,160 @@+//! The Windows install layout: a launcher and the versions it starts.+//!+//! Windows cannot replace a running executable or `exec` into a new one, so+//! the installed `macrod.exe` (the one on `PATH`) is a launcher. It runs the+//! newest `versions\<tag>\macrod-core.exe` with its own arguments and console,+//! waits, and exits with the core's exit code. An update is staged as a new+//! version folder and used from the next launch; the launcher prunes older+//! folders once the newest has started. The launcher is the same binary as+//! the core: which one it is follows from its file name and the `versions`+//! folder beside it.+//!+//! The layout logic is platform-neutral so it is tested everywhere; only the+//! process handling in [`run_as_launcher`] is Windows-only.++use std::path::{Path, PathBuf};++use super::release::ReleaseVersion;++#[cfg(test)]+mod test;++/// The folder of installed versions, beside the launcher.+pub const VERSIONS_DIRECTORY: &str = "versions";++/// The core binary's name inside each version folder.+pub const CORE_EXECUTABLE: &str = "macrod-core.exe";++/// A version folder still being written; never launched.+const PARTIAL_SUFFIX: &str = ".partial";++/// The `versions` folder of one installation.+#[derive(Debug, Clone)]+pub struct Versions {+ directory: PathBuf,+}++impl Versions {+ pub fn new(directory: PathBuf) -> Self {+ Self { directory }+ }++ /// The installation a core binary at `executable` belongs to: its+ /// `versions\<tag>\` parent, or (a plain `macrod.exe` with no layout yet)+ /// a `versions` folder beside it, which turns that `macrod.exe` into the+ /// launcher from its next start.+ pub fn of_executable(executable: &Path) -> Self {+ let folder = executable.parent().unwrap_or(Path::new("."));+ match folder.parent() {+ Some(versions)+ if versions+ .file_name()+ .is_some_and(|name| name == VERSIONS_DIRECTORY)+ && executable+ .file_name()+ .is_some_and(|name| name == CORE_EXECUTABLE) =>+ {+ Self::new(versions.to_owned())+ }+ _ => Self::new(folder.join(VERSIONS_DIRECTORY)),+ }+ }++ pub fn directory(&self) -> &Path {+ &self.directory+ }++ /// Where a version's core is written before it is committed.+ pub fn staging_executable(&self, tag: &str) -> PathBuf {+ self.directory+ .join(format!("{tag}{PARTIAL_SUFFIX}"))+ .join(CORE_EXECUTABLE)+ }++ /// Make a staged version launchable. Replaces an earlier copy of the+ /// same version.+ pub fn commit(&self, tag: &str) -> std::io::Result<PathBuf> {+ let staged = self.directory.join(format!("{tag}{PARTIAL_SUFFIX}"));+ let committed = self.directory.join(tag);+ if committed.exists() {+ std::fs::remove_dir_all(&committed)?;+ }+ std::fs::rename(&staged, &committed)?;+ Ok(committed.join(CORE_EXECUTABLE))+ }++ /// The newest complete version and its core binary.+ pub fn newest(&self) -> Option<(ReleaseVersion, PathBuf)> {+ std::fs::read_dir(&self.directory)+ .ok()?+ .filter_map(Result::ok)+ .filter_map(|entry| {+ let name = entry.file_name();+ let version = ReleaseVersion::parse(name.to_str()?)?;+ let core = entry.path().join(CORE_EXECUTABLE);+ core.is_file().then_some((version, core))+ })+ .max_by_key(|(version, _)| *version)+ }++ /// Remove every complete version folder but `keep`. Partial folders+ /// stay: a running macrod may be staging one, and an abandoned one is+ /// never launched and is overwritten when that version is staged again.+ /// Best effort: a version another macrod is still running from cannot be+ /// removed on Windows, and stays until a later launch.+ pub fn prune(&self, keep: ReleaseVersion) {+ let Ok(entries) = std::fs::read_dir(&self.directory) else {+ return;+ };+ for entry in entries.filter_map(Result::ok) {+ let name = entry.file_name();+ let Some(name) = name.to_str() else { continue };+ let stale = ReleaseVersion::parse(name).is_some_and(|version| version != keep);+ if stale && let Err(error) = std::fs::remove_dir_all(entry.path()) {+ tracing::debug!(error = %error, folder = name, "could not prune an old macrod version");+ }+ }+ }+}++/// Run as the launcher when this binary is the installed `macrod.exe` with a+/// `versions` folder beside it. Returns the core's exit code, or `None` when+/// this process is not a launcher (and should run macrod itself).+#[cfg(windows)]+pub fn run_as_launcher() -> Option<i32> {+ let executable = std::env::current_exe().ok()?;+ if !executable+ .file_name()+ .and_then(|name| name.to_str())+ .is_some_and(|name| name.eq_ignore_ascii_case("macrod.exe"))+ {+ return None;+ }+ let versions = Versions::new(executable.parent()?.join(VERSIONS_DIRECTORY));+ let (version, core) = versions.newest()?;++ // Ctrl+C reaches every process on the console. The core decides what it+ // means; the launcher keeps waiting so the shell gets the core's exit code.+ // SAFETY: a null handler with TRUE only sets this process to ignore Ctrl+C.+ unsafe {+ windows_sys::Win32::System::Console::SetConsoleCtrlHandler(None, 1);+ }+ let mut child = match std::process::Command::new(&core)+ .args(std::env::args_os().skip(1))+ .spawn()+ {+ Ok(child) => child,+ Err(error) => {+ eprintln!("macrod: could not start {}: {error}", core.display());+ return Some(1);+ }+ };+ versions.prune(version);+ match child.wait() {+ Ok(status) => Some(status.code().unwrap_or(1)),+ Err(error) => {+ eprintln!("macrod: lost track of {}: {error}", core.display());+ Some(1)+ }+ }+}diff --git a/crates/coding_agent_worker/src/update/versions/test.rs b/crates/coding_agent_worker/src/update/versions/test.rsnew file mode 100644index 0000000000..33a5527512--- /dev/null+++ b/crates/coding_agent_worker/src/update/versions/test.rs@@ -0,0 +1,99 @@+use super::*;++fn install_version(versions: &Path, tag: &str) {+ let folder = versions.join(tag);+ std::fs::create_dir_all(&folder).unwrap();+ std::fs::write(folder.join(CORE_EXECUTABLE), tag).unwrap();+}++#[test]+fn the_launcher_picks_the_newest_complete_version() {+ let root = tempfile::tempdir().unwrap();+ let versions = Versions::new(root.path().join(VERSIONS_DIRECTORY));+ install_version(versions.directory(), "v2026.10.9.3");+ install_version(versions.directory(), "v2026.10.10.1");+ install_version(versions.directory(), "v2026.10.10.0");+ // Still being written, and not a version at all: neither is launched.+ install_version(versions.directory(), "v2026.12.1.0.partial");+ install_version(versions.directory(), "scratch");+ // A version folder without its core is not complete.+ std::fs::create_dir_all(versions.directory().join("v2026.11.1.0")).unwrap();++ let (version, core) = versions.newest().unwrap();++ assert_eq!(Some(version), ReleaseVersion::parse("v2026.10.10.1"));+ assert_eq!(+ core,+ versions+ .directory()+ .join("v2026.10.10.1")+ .join(CORE_EXECUTABLE)+ );+}++#[test]+fn no_versions_means_no_launcher() {+ let root = tempfile::tempdir().unwrap();+ assert!(+ Versions::new(root.path().join(VERSIONS_DIRECTORY))+ .newest()+ .is_none()+ );+}++#[test]+fn pruning_keeps_the_launched_version_and_any_being_staged() {+ let root = tempfile::tempdir().unwrap();+ let versions = Versions::new(root.path().join(VERSIONS_DIRECTORY));+ install_version(versions.directory(), "v2026.10.9.3");+ install_version(versions.directory(), "v2026.10.10.1");+ install_version(versions.directory(), "v2026.10.11.0.partial");+ install_version(versions.directory(), "scratch");++ versions.prune(ReleaseVersion::parse("v2026.10.10.1").unwrap());++ let mut left: Vec<_> = std::fs::read_dir(versions.directory())+ .unwrap()+ .map(|entry| entry.unwrap().file_name().into_string().unwrap())+ .collect();+ left.sort();+ // A running macrod may be writing the partial version right now.+ assert_eq!(left, ["scratch", "v2026.10.10.1", "v2026.10.11.0.partial"]);+}++#[test]+fn a_staged_version_is_launchable_only_once_committed() {+ let root = tempfile::tempdir().unwrap();+ let versions = Versions::new(root.path().join(VERSIONS_DIRECTORY));+ install_version(versions.directory(), "v2026.10.9.3");+ let staged = versions.staging_executable("v2026.10.10.1");+ std::fs::create_dir_all(staged.parent().unwrap()).unwrap();+ std::fs::write(&staged, "new").unwrap();++ assert_eq!(+ versions.newest().map(|(version, _)| version),+ ReleaseVersion::parse("v2026.10.9.3")+ );+ let core = versions.commit("v2026.10.10.1").unwrap();++ assert_eq!(std::fs::read_to_string(&core).unwrap(), "new");+ assert_eq!(versions.newest().map(|(_, path)| path), Some(core));+}++#[test]+fn a_core_finds_its_installation_and_a_plain_binary_starts_one() {+ let core = Path::new("/apps/macrod")+ .join(VERSIONS_DIRECTORY)+ .join("v2026.10.10.1")+ .join(CORE_EXECUTABLE);+ assert_eq!(+ Versions::of_executable(&core).directory(),+ Path::new("/apps/macrod").join(VERSIONS_DIRECTORY)+ );++ let plain = Path::new("/apps/macrod/macrod.exe");+ assert_eq!(+ Versions::of_executable(plain).directory(),+ Path::new("/apps/macrod").join(VERSIONS_DIRECTORY)+ );+}diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows.rs b/tooling/xtask/crates/xtask_workflows/src/workflows.rsindex 43b3a3964f..649ad567c6 100644--- a/tooling/xtask/crates/xtask_workflows/src/workflows.rs+++ b/tooling/xtask/crates/xtask_workflows/src/workflows.rs@@ -59,6 +59,7 @@ mod reusable_preview_service;mod runners;mod sdk_check;mod steps;+mod test_macrod_update;mod vars;mod web_app_check_main;mod web_artifact_paths;@@ -137,6 +138,11 @@ const WORKFLOWS: &[WorkflowFile] = &[file_name: "build_agent_daemon_on_tag.yml",render_yaml: || render_gh_workflow(build_agent_daemon_on_tag::build_agent_daemon_on_tag)(),},+ WorkflowFile {+ slug: "test_macrod_update",+ file_name: "test_macrod_update.yml",+ render_yaml: || render_gh_workflow(test_macrod_update::test_macrod_update)(),+ },WorkflowFile {slug: "build_appimage_on_tag",file_name: "build_appimage_on_tag.yml",diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/build_agent_daemon_on_tag.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/build_agent_daemon_on_tag.rsindex a34897505b..bbc3fb4356 100644--- a/tooling/xtask/crates/xtask_workflows/src/workflows/build_agent_daemon_on_tag.rs+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/build_agent_daemon_on_tag.rs@@ -192,12 +192,10 @@ fn linux_job(target: &DaemonTarget) -> Job {// cache on failure, so it is never load-bearing..add_step(steps::configure_namespace_sccache(vars::CI_SCCACHE_NAME)).add_step(steps::derive_artifact_metadata(RESOLVED_REF))- .add_step(- Step::new("Build daemon")- .run(include_str!("scripts/build_agent_daemon_linux.sh"))- .shell("bash")- .add_env(("TARGET", target.triple)),- )+ .add_step(build_step(+ target,+ include_str!("scripts/build_agent_daemon_linux.sh"),+ )).add_step(package_step(target)).add_step(upload_step(target)).add_step(steps::teardown_nix())@@ -212,12 +210,10 @@ fn macos_job(target: &DaemonTarget) -> Job {.add_step(steps::checkout_ref(RESOLVED_REF)).add_step(steps::setup_rust_light()).add_step(steps::derive_artifact_metadata(RESOLVED_REF))- .add_step(- Step::new("Build daemon")- .run(include_str!("scripts/build_agent_daemon_macos.sh"))- .shell("bash")- .add_env(("TARGET", target.triple)),- )+ .add_step(build_step(+ target,+ include_str!("scripts/build_agent_daemon_macos.sh"),+ )).add_step(package_step(target)).add_step(upload_step(target))}@@ -231,16 +227,24 @@ fn windows_job(target: &DaemonTarget) -> Job {.add_step(steps::checkout_ref(RESOLVED_REF)).add_step(steps::setup_rust_light()).add_step(steps::derive_artifact_metadata(RESOLVED_REF))- .add_step(- Step::new("Build daemon")- .run(include_str!("scripts/build_agent_daemon_windows.sh"))- .shell("bash")- .add_env(("TARGET", target.triple)),- )+ .add_step(build_step(+ target,+ include_str!("scripts/build_agent_daemon_windows.sh"),+ )).add_step(package_step(target)).add_step(upload_step(target))}+/// The release tag is compiled in: `macrod --version` prints it and the+/// self-updater compares it with the latest release.+fn build_step(target: &DaemonTarget, script: &str) -> Step<Run> {+ Step::new("Build daemon")+ .run(script)+ .shell("bash")+ .add_env(("TARGET", target.triple))+ .add_env(("MACROD_RELEASE_TAG", "${{ steps.metadata.outputs.tag }}"))+}+fn package_step(target: &DaemonTarget) -> Step<Run> {Step::new("Package daemon").run(include_str!("scripts/package_agent_daemon.sh"))@@ -248,6 +252,7 @@ fn package_step(target: &DaemonTarget) -> Step<Run> {.add_env(("TARGET", target.triple)).add_env(("SLUG", target.slug)).add_env(("SAFE_TAG", "${{ steps.metadata.outputs.safe_tag }}"))+ .add_env(("RELEASE_TAG", "${{ steps.metadata.outputs.tag }}")).add_env(("SMOKE_RUN", if target.smoke_run { "1" } else { "0" }))}diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/package_agent_daemon.sh b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/package_agent_daemon.shindex 4e2a9f7b04..9d8e2099ba 100644--- a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/package_agent_daemon.sh+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/package_agent_daemon.sh@@ -68,6 +68,18 @@ elseecho "skipping smoke run: $TARGET does not execute on this runner"fi+# A release binary carries its tag: the self-updater compares it with the+# latest release, so a binary built without it (or served a stale tag from a+# compiler cache) would never update, or would update forever.+case "$RELEASE_TAG" in+ v*)+ if ! grep -aqF "$RELEASE_TAG" "$binary"; then+ echo "$binary does not carry its release tag $RELEASE_TAG" >&2+ exit 1+ fi+ ;;+esac+mkdir -p artifactsstage=$(mktemp -d)install -m 755 "$binary" "$stage/$executable"diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/test_macrod_update.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/test_macrod_update.rsnew file mode 100644index 0000000000..e399110da1--- /dev/null+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/test_macrod_update.rs@@ -0,0 +1,132 @@+//! `Test macrod update` — macrod's self-update and installers, end to end, on+//! Linux, macOS, and Windows.+//!+//! Each job builds macrod twice from the checkout, as two fake release tags,+//! and runs `crates/coding_agent_worker/e2e/` against a local release mirror:+//! the hot handoff and its negative cases plus the shell installer on Linux+//! and macOS, and the launcher layout plus `install.ps1` on Windows. Nothing+//! is published. Runs on pull requests that touch macrod, and on demand.++use gh_workflow::{Event, Job, Level, Permissions, PullRequest, Run, Step, Workflow};++use crate::workflows::{runners, steps, vars};++#[cfg(test)]+mod test;++const E2E: &str = "crates/coding_agent_worker/e2e";+const V1: &str = "v2000.1.1.1";+const V2: &str = "v2000.1.1.2";++/// Build the workflow.+pub fn test_macrod_update() -> Workflow {+ Workflow::new("Test macrod update")+ .permissions(Permissions {+ contents: Some(Level::Read),+ ..Default::default()+ })+ .on(Event::default()+ .pull_request(+ PullRequest::default()+ .add_branch("main")+ .add_path(xtask_paths::repo_glob!("crates/coding_agent_worker/**"))+ .add_path(xtask_paths::repo_glob!(+ ".github/workflows/test_macrod_update.yml"+ )),+ )+ .workflow_dispatch(gh_workflow::WorkflowDispatch::default()))+ .add_job("linux", linux_job())+ .add_job("macos", macos_job())+ .add_job("windows", windows_job())+}++/// Builds musl like the release workflow, in the same minimal Nix shell.+fn linux_job() -> Job {+ Job::default()+ .name("macrod update (linux)")+ .runs_on(runners::Runner::RustCi.to_string())+ .add_step(steps::checkout(false, false))+ .add_step(steps::mount_cache_volume())+ .add_step(steps::setup_nix_with_cache())+ .add_step(steps::setup_dev_shell_named("agent-daemon"))+ .add_step(steps::configure_namespace_sccache(vars::CI_SCCACHE_NAME))+ .add_step(+ build_step()+ .add_env(("TARGET", "x86_64-unknown-linux-musl"))+ .add_env(("CARGO_BUILD", "cargo zigbuild"))+ .add_env(("AWS_LC_SYS_CMAKE_BUILDER", "1")),+ )+ .add_step(update_step())+ .add_step(+ Step::new("Installer in bash, zsh and fish")+ .run(format!(+ "nix shell nixpkgs#zsh nixpkgs#fish --command {E2E}/installer_test.sh \"$RUNNER_TEMP/releases\" {V1}"+ ))+ .shell("bash"),+ )+ .add_step(steps::teardown_nix())+}++fn macos_job() -> Job {+ Job::default()+ .name("macrod update (macos)")+ .runs_on(runners::Runner::MacOsArm.to_string())+ .add_step(steps::checkout(false, false))+ .add_step(steps::setup_rust_light())+ .add_step(build_step())+ .add_step(update_step())+ .add_step(+ Step::new("Installer in bash and zsh")+ .run(format!(+ "{E2E}/installer_test.sh \"$RUNNER_TEMP/releases\" {V1}"+ ))+ .shell("bash")+ .add_env(("INSTALLER_TEST_SHELLS", "bash zsh")),+ )+}++/// Windows has no hot handoff: this checks the launcher layout and the+/// installer, with builds from the runner's MSVC toolchain.+fn windows_job() -> Job {+ Job::default()+ .name("macrod update (windows)")+ .runs_on(runners::Runner::Windows.to_string())+ .add_step(steps::checkout(false, false))+ .add_step(steps::setup_rust_light())+ .add_step(+ build_step()+ // aws-lc-sys ships its NASM objects pre-assembled.+ .add_env(("AWS_LC_SYS_PREBUILT_NASM", "1")),+ )+ .add_step(+ Step::new("Launcher layout and install.ps1")+ .run(format!(+ "& ./{E2E}/windows_test.ps1 -Releases \"$env:RUNNER_TEMP/releases\" -V1 {V1} -V2 {V2}"+ ))+ .shell("pwsh"),+ )+ .add_step(+ Step::new("Self-update end to end")+ .run(format!(+ "& ./{E2E}/windows_update_test.ps1 -Releases \"$env:RUNNER_TEMP/releases\" -V1 {V1} -V2 {V2}"+ ))+ .shell("pwsh"),+ )+}++fn build_step() -> Step<Run> {+ Step::new("Build two tagged releases")+ .run(format!(+ "{E2E}/build_releases.sh \"$RUNNER_TEMP/releases\" {V1} {V2}"+ ))+ .shell("bash")+ .add_env(("SQLX_OFFLINE", "true"))+}++fn update_step() -> Step<Run> {+ Step::new("Self-update end to end")+ .run(format!(+ "python3 {E2E}/update_e2e.py --releases \"$RUNNER_TEMP/releases\" --v1 {V1} --v2 {V2}"+ ))+ .shell("bash")+}diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/test_macrod_update/test.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/test_macrod_update/test.rsnew file mode 100644index 0000000000..3657ee9c05--- /dev/null+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/test_macrod_update/test.rs@@ -0,0 +1,18 @@+use super::*;++#[test]+fn runs_the_end_to_end_tests_on_every_platform() {+ let yaml = test_macrod_update()+ .to_string()+ .expect("workflow should serialize");+ for runner in [+ runners::Runner::RustCi,+ runners::Runner::MacOsArm,+ runners::Runner::Windows,+ ] {+ assert!(yaml.contains(&runner.to_string()), "{runner}");+ }+ assert!(yaml.contains("update_e2e.py"));+ assert!(yaml.contains("windows_test.ps1"));+ assert!(yaml.contains("crates/coding_agent_worker/**"));+}