From d48814bac5c85eddeb5edb0c070ae1bedce5f014 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:16:25 +0100 Subject: [PATCH 01/42] ci: run what the tests already need, and check the corpus copy - The rust jobs now also run on config.example.json (bridge-runtime's tests compile it in and check every key) and on the Android copy of the display corpus. - client-ffi's corpus test compares the Android copy with the fixture, and regenerate_the_fixture writes both: a copy left behind would have the Kotlin side test a shape Rust no longer produces. - The Android job runs lintDebug; the build aborts on lint errors, but no job ran it, so a lint failure surfaced only on a local build. Co-Authored-By: Claude Code --- .github/workflows/ci.yml | 8 +++-- .../tests/display_entries_corpus.rs | 30 +++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7ff5be56..f060f633 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -78,6 +78,10 @@ jobs: - '.github/workflows/ci.yml' rust: - 'crates/**' + # bridge-runtime's tests compile it in and check every key. + - 'config.example.json' + # client-ffi's corpus test compares the Android copy. + - 'apps/android/app/src/test/resources/display_entries_corpus.json' - 'Cargo.toml' - 'Cargo.lock' - '.github/workflows/ci.yml' @@ -297,9 +301,9 @@ jobs: exit 1 } - - name: Gradle test + Paparazzi verify + - name: Gradle test + Paparazzi verify + lint working-directory: apps/android - run: ./gradlew testDebugUnitTest verifyPaparazziDebug --console=plain + run: ./gradlew testDebugUnitTest verifyPaparazziDebug lintDebug --console=plain # The deployment side, which no other job touches: every shell script must # at least parse, and the bridge image must build (never pushed here). The diff --git a/crates/client-ffi/tests/display_entries_corpus.rs b/crates/client-ffi/tests/display_entries_corpus.rs index 39abd143..e897b844 100644 --- a/crates/client-ffi/tests/display_entries_corpus.rs +++ b/crates/client-ffi/tests/display_entries_corpus.rs @@ -10,8 +10,7 @@ //! //! Regenerate after a deliberate shape change: //! `cargo test -p client-ffi --test display_entries_corpus -- --ignored regenerate_the_fixture` -//! then copy the file to the Android test resources path above and re-run -//! both sides' tests. +//! writes both copies; then re-run both sides' tests. use client_runtime::client_core::presentation::activity::build_activity; use client_runtime::client_core::presentation::display_entries::{ @@ -122,11 +121,31 @@ fn corpus_json() -> String { serde_json::to_string_pretty(&combined).unwrap() } +/// The Android test resources' copy, which the Kotlin side decodes. +const ANDROID_COPY: &str = "../../apps/android/app/src/test/resources/display_entries_corpus.json"; + +/// A fixture as committed, whatever line endings a checkout gave it. +fn normalized(text: &str) -> String { + text.replace("\r\n", "\n").trim().to_string() +} + #[test] fn matches_the_committed_fixture() { let actual = corpus_json(); let expected = include_str!("../fixtures/display_entries_corpus.json"); - assert_eq!(actual.trim(), expected.trim()); + assert_eq!(actual.trim(), normalized(expected)); +} + +#[test] +fn the_android_copy_is_the_same_fixture() { + // Two consumers of one shape: a copy left behind after a regeneration + // would have the Kotlin side test a shape Rust no longer produces. + let android = include_str!("../../../apps/android/app/src/test/resources/display_entries_corpus.json"); + assert_eq!( + normalized(android), + normalized(include_str!("../fixtures/display_entries_corpus.json")), + "{ANDROID_COPY} differs from fixtures/display_entries_corpus.json: run regenerate_the_fixture" + ); } /// `cargo test -p client-ffi --test display_entries_corpus -- --ignored @@ -134,6 +153,7 @@ fn matches_the_committed_fixture() { #[test] #[ignore] fn regenerate_the_fixture() { - let path = concat!(env!("CARGO_MANIFEST_DIR"), "/fixtures/display_entries_corpus.json"); - std::fs::write(path, corpus_json() + "\n").unwrap(); + let json = corpus_json() + "\n"; + std::fs::write(concat!(env!("CARGO_MANIFEST_DIR"), "/fixtures/display_entries_corpus.json"), &json).unwrap(); + std::fs::write(std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(ANDROID_COPY), &json).unwrap(); } From 5099bd9815a932e1f763b8e2ba2386861a6faa79 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:16:26 +0100 Subject: [PATCH 02/42] docs(claude): what a test is for here, and the narrowest run per area Behaviour through the public surface, fakes held to the real agent, every absolute constraint with a test proven by breaking it, determinism, and a bug fix starting from its test. The verification line now says that a change the app sees also needs the Android bar. Co-Authored-By: Claude Code --- .claude/CLAUDE.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/.claude/CLAUDE.md b/.claude/CLAUDE.md index 7a1dc172..2c47d9dc 100644 --- a/.claude/CLAUDE.md +++ b/.claude/CLAUDE.md @@ -70,7 +70,8 @@ the vendored toolchain — the caller does not choose, and does not need to know ./codedeck doctor # Workspace verification, in throwaway containers (nothing installed locally -# is required). A change is not done until `check` is green. +# is required). A change is not done until `check` is green — plus, for a +# change the app sees, `./codedeck android test verifyPaparazziDebug lintDebug`. ./codedeck check # agent host typecheck + test, cargo clippy + test (every crate) ./codedeck typecheck # typecheck / clippy only ./codedeck test # tests only @@ -135,6 +136,30 @@ credentials (`--env` is for the agent's own paths, not its keys). claude-code, deepseek-harness or qwen-code spends the account. The commands for each tier are in the verify-changes skill. +### Tests + +What a test is for here: catching the regression an agent would otherwise +ship. So: + +- **Behaviour through the public surface** (a command in, effects or wire + frames out) — never a restated constant, a serde derive, a library round + trip or a private counter. +- **A fake must match the real agent.** Every fake of an agent or server + (OpenCode client, ACP agent, Claude SDK) states only behaviour checked + against the real binary (`./codedeck live --fake-model`); one + shared fake per agent, and a divergence found live becomes a test case. +- **Each absolute constraint has a test that fails when it is broken** (Tor + routing, secrets out of logs and the wire, total decoders). A new test is + proven by breaking the code it guards once and watching it fail. +- **Deterministic:** injected clocks or paused tokio time, waits on events; + a sleep is a timeout guard, never the thing that orders a test. +- A bug fix starts with the test that reproduces it. + +Narrowest run per area: `dev cargo test -p [name]`, `dev vitest run + [-t name]`, `android test --tests `, and the corpora +(`crates/protocol/fixtures/corpus.json`, the display corpus) for wire and +transcript shape. + ### CI and releases - `.github/workflows/ci.yml` runs on every push to `master` and every PR: the From 8735d7a18e883ab55bec94cb1f07721e6bf4c78d Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:16:17 +0100 Subject: [PATCH 03/42] test(transport): prove proxied dials and direct-link pinning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The absolute constraint that all relay traffic goes through the configured SOCKS5 proxy had no test watching a real proxy: the existing proxy test only checked that live sockets were torn down. The direct link's certificate pin was likewise only exercised indirectly by bridge-runtime, never by the transport itself. Add a minimal SOCKS5 proxy and a connection-counting listener as test-only support, then guard: - ws: with a proxy set, a relay dial reaches the proxy, asks it to CONNECT to the relay's host as a domain name (remote DNS is what makes .onion relays reachable, and keeps the address off the local resolver) and port, and never opens a direct connection to the relay. - direct: an .onion endpoint is dialled through the proxy by name, for the same reason. - direct: a certificate that is not the pinned one is refused while the advertised pin brings the link up, so an intermediary on the LAN cannot impersonate the bridge. - direct: a non-onion direct endpoint is dialled directly even while a proxy is set — a direct link is not relay traffic, so Orbot never sees it. The pinning tests run a real TLS server with a self-signed certificate, which adds rcgen and tokio-rustls as dev-dependencies (versions already in the lockfile through bridge-runtime). Co-Authored-By: Claude Code --- Cargo.lock | 2 + crates/nostr-transport/Cargo.toml | 6 ++ crates/nostr-transport/src/direct.rs | 111 +++++++++++++++++++++ crates/nostr-transport/src/lib.rs | 3 + crates/nostr-transport/src/test_support.rs | 103 +++++++++++++++++++ crates/nostr-transport/src/ws.rs | 35 +++++++ 6 files changed, 260 insertions(+) create mode 100644 crates/nostr-transport/src/test_support.rs diff --git a/Cargo.lock b/Cargo.lock index df9ca7b4..94a2beba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1497,10 +1497,12 @@ dependencies = [ "log", "nostr", "protocol", + "rcgen", "rustls", "serde_json", "sha2", "tokio", + "tokio-rustls", "tokio-socks", "tokio-tungstenite", "tokio-util", diff --git a/crates/nostr-transport/Cargo.toml b/crates/nostr-transport/Cargo.toml index d12dc7a1..e0dc9031 100644 --- a/crates/nostr-transport/Cargo.toml +++ b/crates/nostr-transport/Cargo.toml @@ -39,3 +39,9 @@ nostr = { version = "0.44", default-features = false, features = ["std", "nip44" [dev-dependencies] tokio = { version = "1", features = ["rt", "net", "time", "sync", "macros", "io-util", "test-util"] } +# The direct-link pinning tests run a real TLS server: a self-signed +# certificate generated in the test, served through the same rustls/ring stack +# the bridge uses. +rcgen = { version = "0.13", default-features = false, features = ["crypto", "ring"] } +tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] } +rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] } diff --git a/crates/nostr-transport/src/direct.rs b/crates/nostr-transport/src/direct.rs index af3c055d..98a12eac 100644 --- a/crates/nostr-transport/src/direct.rs +++ b/crates/nostr-transport/src/direct.rs @@ -817,4 +817,115 @@ mod tests { assert!(!is_onion("wss://192.168.1.20:7447")); assert!(!is_onion("wss://laptop.tail1234.ts.net:7447")); } + + /// A minimal direct-link server: a fresh self-signed certificate over TLS, + /// then a WebSocket that sends a CHALLENGE, waits for the HELLO and + /// answers READY. Returns the `wss://` endpoint and the certificate's + /// SHA-256 — what the bridge's heartbeat advertises as the pin. + async fn direct_server() -> (String, String) { + use rustls::pki_types::{PrivateKeyDer, PrivatePkcs8KeyDer}; + + let made = rcgen::generate_simple_self_signed(vec!["codedeck-bridge".to_string()]).expect("make a certificate"); + let cert = made.cert.der().to_vec(); + let pin: String = Sha256::digest(&cert).iter().map(|b| format!("{b:02x}")).collect(); + let key = made.key_pair.serialize_der(); + let config = rustls::ServerConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider())) + .with_safe_default_protocol_versions() + .expect("ring supports the default protocol versions") + .with_no_client_auth() + .with_single_cert( + vec![CertificateDer::from(cert)], + PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(key)), + ) + .expect("a valid certificate and key"); + let acceptor = tokio_rustls::TlsAcceptor::from(Arc::new(config)); + let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind the direct server"); + let addr = listener.local_addr().expect("the direct server's address"); + tokio::spawn(async move { + while let Ok((tcp, _)) = listener.accept().await { + let acceptor = acceptor.clone(); + tokio::spawn(async move { + let Ok(tls) = acceptor.accept(tcp).await else { return }; + let Ok(mut ws) = tokio_tungstenite::accept_async(tls).await else { return }; + let challenge = encode_direct_frame(&DirectFrame::Challenge("challenge".to_string())); + if ws.send(Message::Text(challenge)).await.is_err() { + return; + } + while let Some(Ok(message)) = ws.next().await { + if let Message::Text(text) = message { + if matches!(decode_direct_frame(&text), Ok(DirectFrame::Hello { .. })) { + let _ = ws.send(Message::Text(encode_direct_frame(&DirectFrame::Ready))).await; + break; + } + } + } + // Stay open until the client closes: a READY it has not + // read yet must not be lost to an abrupt close. + while let Some(Ok(_)) = ws.next().await {} + }); + } + }); + (format!("wss://{addr}"), pin) + } + + /// A direct link's certificate is trusted only when it is exactly the one + /// the bridge advertised, so a LAN man-in-the-middle (or a rebinding DNS + /// answer) cannot impersonate the bridge even though no CA vouches for it. + #[tokio::test] + async fn a_direct_link_refuses_a_certificate_that_does_not_match_the_pin_and_connects_with_the_matching_one() { + let (endpoint, pin) = direct_server().await; + + let wrong = link_with(&[&endpoint], Some(&"00".repeat(32)), None); + let err = match wrong.connect(&endpoint).await { + Ok(_) => panic!("a certificate that is not the pinned one must not bring the link up"), + Err(err) => err, + }; + assert!( + err.contains("does not match the bridge's pin"), + "the handshake must fail on the pin, not for another reason: {err}" + ); + + let right = link_with(&[&endpoint], Some(&pin), None); + right.connect(&endpoint).await.expect("the advertised pin brings the link up"); + } + + /// An `.onion` endpoint is only reachable through Tor, so it is dialled + /// through the proxy — and by name, since the phone's own resolver cannot + /// resolve onion addresses at all. + #[tokio::test] + async fn an_onion_direct_endpoint_is_dialled_through_the_proxy_by_name() { + let (proxy, request) = crate::test_support::socks5_proxy().await; + let err = match dial("ws://abc.onion:7447", Some(proxy.to_string()), None).await { + Ok(_) => panic!("the proxy closes after CONNECT, so no link can come up"), + Err(err) => err, + }; + assert!(!err.starts_with("socks5:"), "the proxy answered, so the dial must not fail in SOCKS5: {err}"); + + let request = tokio::time::timeout(Duration::from_secs(5), request) + .await + .expect("the onion endpoint was not dialled through the SOCKS5 proxy") + .expect("the SOCKS5 proxy reported its CONNECT"); + assert_eq!(request.atyp, 3, "an onion address must be resolved by Tor, not by the phone's resolver"); + assert_eq!(request.host, "abc.onion"); + assert_eq!(request.port, 7447); + } + + /// A direct link to the user's own bridge is not relay traffic: it is + /// dialled directly even while Orbot is set, and Orbot never sees it. + #[tokio::test] + async fn a_direct_endpoint_on_the_lan_is_dialled_directly_even_when_a_proxy_is_set() { + let (endpoint, pin) = direct_server().await; + // A listening address, not a dead one: an endpoint wrongly sent + // through the proxy would reach this listener and fail there instead + // of coming up. + let (proxy, proxy_connections) = crate::test_support::counting_listener().await; + let link = link_with(&[&endpoint], Some(&pin), Some(&proxy.to_string())); + + link.connect(&endpoint).await.expect("a LAN endpoint is dialled directly, not through Orbot"); + assert_eq!( + proxy_connections.load(std::sync::atomic::Ordering::SeqCst), + 0, + "a non-onion direct endpoint was sent through the proxy" + ); + } } diff --git a/crates/nostr-transport/src/lib.rs b/crates/nostr-transport/src/lib.rs index d1de730d..2ba0ff83 100644 --- a/crates/nostr-transport/src/lib.rs +++ b/crates/nostr-transport/src/lib.rs @@ -23,6 +23,9 @@ pub mod ws; #[cfg(any(test, feature = "mock"))] pub mod mock; +#[cfg(test)] +mod test_support; + pub use port::{AuthSigner, Filter, NostrEvent, SubCallbacks, Transport, TransportSub}; pub use publish::{classify_publish, combine_publish, PublishResult, PublishVerdict}; pub use ws::{WsConfig, WsTransport}; diff --git a/crates/nostr-transport/src/test_support.rs b/crates/nostr-transport/src/test_support.rs new file mode 100644 index 00000000..ddcae23a --- /dev/null +++ b/crates/nostr-transport/src/test_support.rs @@ -0,0 +1,103 @@ +//! Test-only network stand-ins for the transport's socket paths: a minimal +//! SOCKS5 proxy that reports the CONNECT it was asked for (so a test can see +//! where a proxied dial really went), and a TCP listener that counts +//! connections (so a test can see that a dial did NOT go somewhere). + +use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr}; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Arc; + +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::{TcpListener, TcpStream}; +use tokio::sync::oneshot; + +/// What a SOCKS5 client asked the proxy to CONNECT to. +pub(crate) struct Socks5Request { + /// The SOCKS5 address type: 1 IPv4, 3 domain name, 4 IPv6. + pub atyp: u8, + pub host: String, + pub port: u16, +} + +/// Bind a minimal SOCKS5 proxy (no authentication, CONNECT only) on +/// loopback. It accepts one client, reports the CONNECT it was asked for over +/// the returned channel, then answers success and closes. Not relaying is +/// deliberate: the test only needs to observe the target, and a dial that +/// reached the relay bypassing the proxy is then visible on the relay's own +/// listener instead. +pub(crate) async fn socks5_proxy() -> (SocketAddr, oneshot::Receiver) { + let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind the SOCKS5 proxy"); + let addr = listener.local_addr().expect("the SOCKS5 proxy's address"); + let (report, request) = oneshot::channel(); + tokio::spawn(async move { + let Ok((mut conn, _)) = listener.accept().await else { return }; + let Some(asked) = read_connect(&mut conn).await else { return }; + // Report before answering: a dial that gets a reply and then fails at + // the WebSocket handshake (the socket closes here) is still observed. + let _ = report.send(asked); + // Success, with an all-zero IPv4 bound address. + let _ = conn.write_all(&[0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0]).await; + let _ = conn.flush().await; + }); + (addr, request) +} + +/// Read a SOCKS5 greeting and one CONNECT request, answering the greeting +/// with "no authentication". `None` if the peer does not speak SOCKS5. +async fn read_connect(conn: &mut TcpStream) -> Option { + let mut greeting = [0u8; 2]; + conn.read_exact(&mut greeting).await.ok()?; + if greeting[0] != 5 { + return None; + } + let mut methods = vec![0u8; greeting[1] as usize]; + conn.read_exact(&mut methods).await.ok()?; + conn.write_all(&[0x05, 0x00]).await.ok()?; + + let mut head = [0u8; 4]; + conn.read_exact(&mut head).await.ok()?; + if head[0] != 5 || head[1] != 1 { + return None; // only CONNECT is implemented + } + let host = match head[3] { + 1 => { + let mut raw = [0u8; 4]; + conn.read_exact(&mut raw).await.ok()?; + Ipv4Addr::from(raw).to_string() + } + 3 => { + let mut len = [0u8; 1]; + conn.read_exact(&mut len).await.ok()?; + let mut raw = vec![0u8; len[0] as usize]; + conn.read_exact(&mut raw).await.ok()?; + String::from_utf8(raw).ok()? + } + 4 => { + let mut raw = [0u8; 16]; + conn.read_exact(&mut raw).await.ok()?; + Ipv6Addr::from(raw).to_string() + } + _ => return None, + }; + let mut port = [0u8; 2]; + conn.read_exact(&mut port).await.ok()?; + Some(Socks5Request { atyp: head[3], host, port: u16::from_be_bytes(port) }) +} + +/// Bind a loopback listener that counts every connection it accepts and then +/// drops it at once. A dial that ignores the proxy and connects directly +/// shows up in the count; a dial routed through the proxy does not. +pub(crate) async fn counting_listener() -> (SocketAddr, Arc) { + let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind the counting listener"); + let addr = listener.local_addr().expect("the counting listener's address"); + let seen = Arc::new(AtomicUsize::new(0)); + let counted = Arc::clone(&seen); + tokio::spawn(async move { + loop { + let Ok((socket, _)) = listener.accept().await else { return }; + counted.fetch_add(1, Ordering::SeqCst); + drop(socket); + } + }); + (addr, seen) +} diff --git a/crates/nostr-transport/src/ws.rs b/crates/nostr-transport/src/ws.rs index a6c20460..e91cae08 100644 --- a/crates/nostr-transport/src/ws.rs +++ b/crates/nostr-transport/src/ws.rs @@ -1366,6 +1366,41 @@ mod tests { assert!(!err.contains("cleartext"), "{err}"); } + /// The absolute constraint this guards: with a proxy set, a relay is + /// never dialled directly, and the proxy is asked to CONNECT to the + /// relay's *name*. Resolving the name locally would break `.onion` + /// relays (only Tor can resolve them) and would leak the relay's address + /// to the local resolver besides. + #[tokio::test] + async fn a_proxied_relay_dial_asks_the_socks_proxy_for_the_relay_name_and_never_connects_directly() { + // Stands in for the relay: it resolves from `localhost`, so a dial + // that ignored the proxy would land here and be counted. + let (relay, direct_connections) = crate::test_support::counting_listener().await; + + let (proxy, request) = crate::test_support::socks5_proxy().await; + let err = match dial(&format!("ws://localhost:{}", relay.port()), Some(proxy.to_string())).await { + Ok(_) => panic!("the proxy closes after CONNECT, so no ws handshake can complete"), + Err(err) => err, + }; + assert!(!err.starts_with("socks5:"), "the proxy answered, so the dial must not fail in SOCKS5: {err}"); + + let request = tokio::time::timeout(Duration::from_secs(5), request) + .await + .expect("the relay dial never reached the SOCKS5 proxy") + .expect("the SOCKS5 proxy reported its CONNECT"); + assert_eq!( + request.atyp, 3, + "the relay host must be sent as a domain name (the proxy does the DNS lookup, which is what reaches .onion addresses), not as a locally resolved IP" + ); + assert_eq!(request.host, "localhost", "the proxy was asked for the wrong host"); + assert_eq!(request.port, relay.port(), "the proxy was asked for the wrong port"); + assert_eq!( + direct_connections.load(std::sync::atomic::Ordering::SeqCst), + 0, + "the relay was dialled directly even though a proxy is set" + ); + } + #[tokio::test] async fn set_proxy_updates_what_the_next_dial_uses_and_closes_every_live_connection() { LocalSet::new() From 55fc839732b8789ac8799803482f5a229e78ee5e Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:18:25 +0100 Subject: [PATCH 04/42] test(bridge): guard that every Nostr path uses the configured Tor proxy With a SOCKS5 proxy configured, no relay dial and no Blossom request may reach a Nostr server directly. The runtime passed the proxy separately to `Relays::start` and `NostrHttp::new`, and no test ever supplied one, so a dropped argument would have gone unnoticed. `work::NostrTransports::from_config` now builds both transports from one config value, and `Relays::start_with` takes the relays and the proxy from it; `runtime::run` uses those. That leaves one decision to test instead of two call sites to keep in step. `tests/tor_routing.rs` runs in-test SOCKS5 and HTTP servers: - with a proxy, a relay dial asks the proxy for the relay's host and port and nothing reaches the relay directly; - with a proxy, a Blossom download of an onion URL crosses the proxy and decrypts; - without a proxy, Nostr HTTP goes direct and an onion URL is refused rather than dialled; - loopback is reached directly even when a proxy is configured. Mutation checks: passing `None` in `start_with`, building the HTTP client without the proxy, routing loopback through the proxy, and dropping the onion refusal each made the corresponding test fail. Co-Authored-By: Claude Code --- crates/bridge-runtime/src/relay.rs | 14 + crates/bridge-runtime/src/runtime.rs | 10 +- crates/bridge-runtime/src/work.rs | 26 ++ crates/bridge-runtime/tests/tor_routing.rs | 354 +++++++++++++++++++++ 4 files changed, 400 insertions(+), 4 deletions(-) create mode 100644 crates/bridge-runtime/tests/tor_routing.rs diff --git a/crates/bridge-runtime/src/relay.rs b/crates/bridge-runtime/src/relay.rs index 139ace88..d04bd7de 100644 --- a/crates/bridge-runtime/src/relay.rs +++ b/crates/bridge-runtime/src/relay.rs @@ -700,6 +700,20 @@ impl Relays { Self { transport, bridge_pubkey, inputs, commands: None, pairing: None, jobs } } + /// Start with the routing the config produced + /// ([`NostrTransports`](crate::work::NostrTransports)): the same relays + /// and the same proxy the Blossom client uses. Taking both from one value + /// is what keeps a configured Tor proxy on every relay dial as well as + /// every Blossom request. + pub fn start_with( + nostr: &crate::work::NostrTransports, + keys: Keypair, + inputs: mpsc::UnboundedSender, + direct: Option>, + ) -> Self { + Self::start(nostr.relays.clone(), keys, nostr.proxy.clone(), inputs, direct) + } + pub fn publish(&self, to: Vec, message: BridgeToPhone) { let _ = self.jobs.send(Job::Publish { to, message: Box::new(message) }); } diff --git a/crates/bridge-runtime/src/runtime.rs b/crates/bridge-runtime/src/runtime.rs index da9175e7..0dc53304 100644 --- a/crates/bridge-runtime/src/runtime.rs +++ b/crates/bridge-runtime/src/runtime.rs @@ -173,10 +173,12 @@ pub async fn run(config: Config, state: StateFile, keys: Keypair, options: Optio let web_config = crate::web::ConfigView { path: config.config_file.clone(), fields: crate::config::web_config_fields(&config) }; let direct = crate::direct::start(&config.direct, &config.home, keys.pubkey_hex.clone(), inputs.clone(), web_config).await?; engine_config.direct = direct.as_ref().map(|d| d.info.clone()); - let relays = Relays::start( - config.relays.clone(), + // The relays and the Blossom client share one routing decision, so a + // configured Tor proxy cannot apply to one and not the other. + let nostr = work::NostrTransports::from_config(&config)?; + let relays = Relays::start_with( + &nostr, keys, - config.tor_proxy.clone(), inputs.clone(), direct.as_ref().map(|d| Rc::clone(&d.hub)), ); @@ -195,7 +197,7 @@ pub async fn run(config: Config, state: StateFile, keys: Keypair, options: Optio }); } - let nostr_http = work::NostrHttp::new(config.tor_proxy.as_deref())?; + let nostr_http = nostr.http; let terminals = Terminals::new(config.home.join("audit"), config.home.clone(), inputs.clone()); // The audit's writer thread, fed by a channel: the runtime never touches // the log file itself. diff --git a/crates/bridge-runtime/src/work.rs b/crates/bridge-runtime/src/work.rs index fee04bc0..8bf3328b 100644 --- a/crates/bridge-runtime/src/work.rs +++ b/crates/bridge-runtime/src/work.rs @@ -10,12 +10,38 @@ use std::time::Duration; use tokio::process::Command; +use crate::config::Config; + const HTTP_TIMEOUT: Duration = Duration::from_secs(15); pub fn http_client() -> reqwest::Client { reqwest::Client::builder().timeout(HTTP_TIMEOUT).build().expect("the HTTP client builds") } +/// The two transports that carry Nostr traffic — relay connections and +/// HTTP to a Blossom server — built from one config value. They are built +/// together because Tor is all-or-nothing: when the owner set a proxy, a +/// relay dial or a Blossom request that skipped it would tell a Nostr server +/// where the bridge is, which is exactly what the proxy is there to prevent. +/// Both take their proxy from here, so neither path can drift to a direct +/// connection while one is configured. +pub struct NostrTransports { + pub relays: Vec, + /// `host:port`, for the relay transport. + pub proxy: Option, + pub http: NostrHttp, +} + +impl NostrTransports { + /// A proxy the HTTP client cannot build is an error, never a silent + /// fallback to a direct connection. + pub fn from_config(config: &Config) -> Result { + let proxy = config.tor_proxy.clone(); + let http = NostrHttp::new(proxy.as_deref())?; + Ok(Self { relays: config.relays.clone(), proxy, http }) + } +} + /// HTTP to Nostr servers. With a Tor proxy set it all goes through the /// proxy, as every relay connection does — those servers learn no more /// about the bridge than the relays do — except to loopback, which never diff --git a/crates/bridge-runtime/tests/tor_routing.rs b/crates/bridge-runtime/tests/tor_routing.rs new file mode 100644 index 00000000..bc87b977 --- /dev/null +++ b/crates/bridge-runtime/tests/tor_routing.rs @@ -0,0 +1,354 @@ +//! The bridge's Tor rule, exercised end to end with in-test SOCKS5 and HTTP +//! servers: with a proxy configured, every relay dial and every Blossom +//! download goes through it; without one, Nostr HTTP goes direct, loopback is +//! reached directly even with a proxy set, and a `.onion` service is refused +//! rather than dialled directly. +//! +//! These are the only tests that make the wiring in `runtime.rs` and +//! `relay.rs` carry a real proxy: everything else passes `None`, so a dropped +//! proxy argument would otherwise go unnoticed. + +use std::io; +use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr}; +use std::time::Duration; + +use aes_gcm::aead::{Aead, KeyInit}; +use aes_gcm::{Aes256Gcm, Nonce}; +use bridge_runtime::config::{AuditSettings, Config, DirectConfig, TerminalSettings}; +use bridge_runtime::relay::Relays; +use bridge_runtime::uploads::fetch_blossom; +use bridge_runtime::work::{NostrHttp, NostrTransports}; +use protocol::capabilities::BridgeHostKind; +use protocol::commands::UploadFileBlossomMsg; +use protocol::crypto::generate_keypair; +use sha2::{Digest, Sha256}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::{TcpListener, TcpStream}; +use tokio::sync::mpsc; +use tokio::task::LocalSet; + +/// Waits for something the rule says must happen. +const HAPPENS: Duration = Duration::from_secs(10); +/// Gives something the rule says must not happen every chance to. A failure +/// guard, not pacing: the positive assertion of the same test already proves +/// the request took the proxied path. +const NEVER: Duration = Duration::from_millis(300); + +fn config(proxy: Option, relays: Vec) -> Config { + let home = std::env::temp_dir().join("codedeck-tor-routing-test"); + Config { + config_file: home.join("config.json"), + config_file_exists: false, + home, + machine: "test".into(), + host_kind: BridgeHostKind::Cli, + relays, + workspace_roots: vec![std::env::temp_dir()], + tor_proxy: proxy.map(|a| a.to_string()), + transcript_keep_last: None, + node_path: "node".into(), + agent_host_path: "agent-host/dist/main.js".into(), + test_mode: true, + host_env: Default::default(), + direct: DirectConfig::default(), + terminal: TerminalSettings::default(), + audit: AuditSettings::default(), + overridden: Default::default(), + } +} + +/// The destination of one SOCKS5 `CONNECT`. +#[derive(Debug, Clone, PartialEq, Eq)] +struct Connect { + host: String, + port: u16, +} + +/// An in-test SOCKS5 server on loopback (no authentication) that reports +/// every `CONNECT` target it is asked for. With `forward` set it opens that +/// TCP stream and pipes the bytes, so a whole dial — a WebSocket handshake, +/// an HTTP download — completes through the proxy; without one it refuses, +/// which is what a client that asked the proxy gets instead of a direct +/// connection. +struct Socks5 { + addr: SocketAddr, + connects: mpsc::UnboundedReceiver, +} + +async fn socks5(forward: Option) -> Socks5 { + let listener = TcpListener::bind("127.0.0.1:0").await.expect("the in-test SOCKS5 proxy binds"); + let addr = listener.local_addr().expect("the in-test SOCKS5 proxy has an address"); + let (seen, connects) = mpsc::unbounded_channel(); + tokio::spawn(async move { + while let Ok((mut socket, _)) = listener.accept().await { + let seen = seen.clone(); + tokio::spawn(async move { + let Ok(target) = read_connect(&mut socket).await else { return }; + let _ = seen.send(target); + let Some(dest) = forward else { + // General failure: the client learns nothing was dialled. + let _ = socket.write_all(&[5, 1, 0, 1, 0, 0, 0, 0, 0, 0]).await; + return; + }; + let Ok(mut upstream) = TcpStream::connect(dest).await else { + let _ = socket.write_all(&[5, 1, 0, 1, 0, 0, 0, 0, 0, 0]).await; + return; + }; + // Success. The bound address is never read by these clients. + if socket.write_all(&[5, 0, 0, 1, 0, 0, 0, 0, 0, 0]).await.is_ok() { + let _ = tokio::io::copy_bidirectional(&mut socket, &mut upstream).await; + } + }); + } + }); + Socks5 { addr, connects } +} + +/// Complete the SOCKS5 greeting, then read the `CONNECT` request. The caller +/// sends the reply, once it knows where the connection goes. +async fn read_connect(socket: &mut TcpStream) -> io::Result { + let mut greeting = [0u8; 2]; + socket.read_exact(&mut greeting).await?; + if greeting[0] != 5 { + return Err(invalid("not SOCKS5")); + } + let mut methods = vec![0u8; greeting[1] as usize]; + socket.read_exact(&mut methods).await?; + socket.write_all(&[5, 0]).await?; // no authentication + let mut head = [0u8; 4]; + socket.read_exact(&mut head).await?; + if head[0] != 5 || head[1] != 1 { + return Err(invalid("not a SOCKS5 CONNECT")); + } + let host = match head[3] { + 1 => { + let mut bytes = [0u8; 4]; + socket.read_exact(&mut bytes).await?; + Ipv4Addr::from(bytes).to_string() + } + 3 => { + let mut len = [0u8; 1]; + socket.read_exact(&mut len).await?; + let mut bytes = vec![0u8; len[0] as usize]; + socket.read_exact(&mut bytes).await?; + String::from_utf8(bytes).map_err(|_| invalid("the CONNECT host is not UTF-8"))? + } + 4 => { + let mut bytes = [0u8; 16]; + socket.read_exact(&mut bytes).await?; + Ipv6Addr::from(bytes).to_string() + } + other => return Err(invalid(&format!("unknown SOCKS5 address type {other}"))), + }; + let mut port = [0u8; 2]; + socket.read_exact(&mut port).await?; + Ok(Connect { host, port: u16::from_be_bytes(port) }) +} + +fn invalid(reason: &str) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, reason.to_string()) +} + +/// A loopback HTTP server that answers every request with `body` and reports +/// each request line it saw. `Connection: close`, so a client's connection +/// ends with the response. +async fn http_serving(body: Vec) -> (SocketAddr, mpsc::UnboundedReceiver) { + let listener = TcpListener::bind("127.0.0.1:0").await.expect("the in-test HTTP server binds"); + let addr = listener.local_addr().expect("the in-test HTTP server has an address"); + let (requests, seen) = mpsc::unbounded_channel(); + tokio::spawn(async move { + while let Ok((mut socket, _)) = listener.accept().await { + let body = body.clone(); + let requests = requests.clone(); + tokio::spawn(async move { + let mut raw = Vec::new(); + let mut buf = [0u8; 1024]; + // A GET carries no body; read up to the end of the headers. + while !raw.windows(4).any(|w| w == b"\r\n\r\n") { + let Ok(n) = socket.read(&mut buf).await else { return }; + if n == 0 { + return; + } + raw.extend_from_slice(&buf[..n]); + } + let line = String::from_utf8_lossy(&raw).lines().next().unwrap_or_default().to_string(); + let _ = requests.send(line); + let mut response = + format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", body.len()).into_bytes(); + response.extend_from_slice(&body); + let _ = socket.write_all(&response).await; + let _ = socket.shutdown().await; + }); + } + }); + (addr, seen) +} + +/// An encrypted blob and the key material a phone sends beside its URL. +struct Encrypted { + bytes: Vec, + hash: String, + key: String, + iv: String, +} + +/// Encrypt `plaintext` the way the phone does before uploading it to +/// Blossom: AES-256-GCM, the tag at the end. +fn encrypted(plaintext: &[u8]) -> Encrypted { + let (key, iv) = ([7u8; 32], [9u8; 12]); + let bytes = Aes256Gcm::new_from_slice(&key) + .expect("a 256-bit key") + .encrypt(Nonce::from_slice(&iv), plaintext) + .expect("the blob encrypts"); + Encrypted { hash: hex::encode(Sha256::digest(&bytes)), bytes, key: hex::encode(key), iv: hex::encode(iv) } +} + +/// The Blossom message the phone would send for `url` holding `blob`. +fn blossom_message(url: String, blob: &Encrypted) -> UploadFileBlossomMsg { + UploadFileBlossomMsg { + session_id: "s".into(), + hash: blob.hash.clone(), + url, + key: blob.key.clone(), + iv: blob.iv.clone(), + filename: "photo.png".into(), + mime_type: "image/png".into(), + text: String::new(), + size_bytes: blob.bytes.len() as u64, + } +} + +/// With a proxy configured, the relay transport must reach its relay through +/// it — the CONNECT names the relay's host and port — and must not have +/// dialled the relay itself. +#[tokio::test] +async fn a_configured_tor_proxy_carries_every_relay_dial() { + LocalSet::new() + .run_until(async { + // The relay. Its listener is the only way to reach it; the proxy + // is told to forward there, so a connection at its front door can + // only have come through the proxy. + let relay = TcpListener::bind("127.0.0.1:0").await.expect("the in-test relay binds"); + let relay_addr = relay.local_addr().expect("the in-test relay has an address"); + let mut proxy = socks5(Some(relay_addr)).await; + + let config = config(Some(proxy.addr), vec![format!("ws://{relay_addr}")]); + let nostr = NostrTransports::from_config(&config).expect("the configured proxy builds"); + let (inputs, _events) = mpsc::unbounded_channel(); + let _relays = Relays::start_with(&nostr, generate_keypair(), inputs, None); + + // The proxy is what the transport reached, and it was asked for + // the relay the config names. + let target = tokio::time::timeout(HAPPENS, proxy.connects.recv()) + .await + .expect("the bridge dialled its relay through the configured proxy") + .expect("the proxy reports the CONNECT"); + assert_eq!((target.host.as_str(), target.port), ("127.0.0.1", relay_addr.port()), "the proxy was asked for the configured relay"); + + // And the proxied connection is the one that arrives: complete + // its handshake so the transport stays on it. + let (tcp, _) = tokio::time::timeout(HAPPENS, relay.accept()) + .await + .expect("the proxied connection reaches the relay") + .expect("the relay accepts"); + tokio::time::timeout(HAPPENS, tokio_tungstenite::accept_async(tcp)) + .await + .expect("the relay's WebSocket handshake completes") + .expect("the relay accepts the WebSocket handshake"); + + assert!( + tokio::time::timeout(NEVER, relay.accept()).await.is_err(), + "a second connection reached the relay directly; every dial must go through the proxy" + ); + }) + .await; +} + +/// With a proxy configured, a Blossom download must be made through it: the +/// proxy sees the ONION host the message names, and the download succeeds +/// only because the proxy carried it. +#[tokio::test] +async fn a_configured_tor_proxy_carries_a_blossom_download() { + let blob = encrypted(b"photo bytes"); + let (http_addr, mut requests) = http_serving(blob.bytes.clone()).await; + let mut proxy = socks5(Some(http_addr)).await; + let config = config(Some(proxy.addr), vec![]); + let nostr = NostrTransports::from_config(&config).expect("the configured proxy builds"); + + // An onion URL cannot resolve on this machine: only the proxy can carry + // it, so a successful download proves the proxy was used. + let url = format!("http://abcdefghijklmnop.onion:{}/blob", http_addr.port()); + let msg = blossom_message(url, &blob); + let data = tokio::time::timeout(HAPPENS, fetch_blossom(&nostr.http, &msg)) + .await + .expect("the download finishes") + .expect("the proxied Blossom download succeeds"); + assert_eq!(data, b"photo bytes", "the blob decrypts to what the phone encrypted"); + + let target = tokio::time::timeout(HAPPENS, proxy.connects.recv()) + .await + .expect("the Blossom request went through the configured proxy") + .expect("the proxy reports the CONNECT"); + assert_eq!( + (target.host.as_str(), target.port), + ("abcdefghijklmnop.onion", http_addr.port()), + "the proxy was asked for the Blossom host the message names" + ); + let line = tokio::time::timeout(HAPPENS, requests.recv()) + .await + .expect("the Blossom server answered") + .expect("the Blossom server saw the request"); + assert_eq!(line, "GET /blob HTTP/1.1", "the request crossed the proxy unchanged"); + assert!( + tokio::time::timeout(NEVER, requests.recv()).await.is_err(), + "a second request reached the Blossom server; the download must go through the proxy once" + ); +} + +/// Without a proxy, Nostr HTTP goes direct — and an onion service is refused +/// instead of being dialled directly. +#[tokio::test] +async fn without_a_proxy_nostr_http_goes_direct_and_refuses_onion() { + let (http_addr, mut requests) = http_serving(b"direct".to_vec()).await; + let mut unused_proxy = socks5(None).await; + let http = NostrHttp::new(None).expect("a direct client builds"); + + // Loopback is reachable over plain HTTP, and with no proxy it is the + // direct client that carries the request. + let url = format!("http://{http_addr}/x"); + let client = http.for_url(&url).expect("loopback needs no proxy"); + let body = client.get(&url).send().await.expect("the direct request reaches the server").text().await.expect("the body reads"); + assert_eq!(body, "direct"); + assert_eq!(tokio::time::timeout(HAPPENS, requests.recv()).await.expect("the server saw the request").as_deref(), Some("GET /x HTTP/1.1")); + assert!( + tokio::time::timeout(NEVER, unused_proxy.connects.recv()).await.is_err(), + "no proxy was configured, so no CONNECT may have been made" + ); + + // A .onion service is out of reach without Tor: an error, never a direct + // attempt at the name. + let onion = blossom_message("http://abcdefghijklmnop.onion/blob".into(), &encrypted(b"x")); + let err = fetch_blossom(&http, &onion).await.expect_err("an onion download without Tor is refused"); + assert!(err.contains("Tor proxy"), "the refusal names the missing Tor proxy: {err}"); +} + +/// Loopback is the one Nostr destination that skips the proxy: it never +/// leaves the machine, so a configured proxy must not carry it, and a +/// loopback request still succeeds while the proxy is set. +#[tokio::test] +async fn loopback_nostr_http_skips_the_proxy_even_when_one_is_configured() { + let (http_addr, mut requests) = http_serving(b"local".to_vec()).await; + let mut proxy = socks5(None).await; + let proxy_addr = proxy.addr.to_string(); + let http = NostrHttp::new(Some(&proxy_addr)).expect("the configured proxy builds"); + + let url = format!("http://{http_addr}/loopback"); + let client = http.for_url(&url).expect("loopback needs no proxy"); + let body = client.get(&url).send().await.expect("the loopback request reaches the server").text().await.expect("the body reads"); + assert_eq!(body, "local"); + assert_eq!(tokio::time::timeout(HAPPENS, requests.recv()).await.expect("the server saw the request").as_deref(), Some("GET /loopback HTTP/1.1")); + assert!( + tokio::time::timeout(NEVER, proxy.connects.recv()).await.is_err(), + "loopback must not be routed through the configured proxy" + ); +} From 71d939a10e8f1de3750dc77a9b4bc282ca24fdf4 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:22:56 +0100 Subject: [PATCH 05/42] fix(client): follow Tor's reconnect timing when Tor is toggled at runtime CoreConfig::new picks the Tor backoff and connect deadline only when the core boots. A phone that started with Tor off and turned Orbot on later kept the clearnet connect deadline (45 s), so an attempt still dialling through Tor was abandoned while the transport itself allows a proxied dial 60 s to finish. Re-derive the reconnect timing in the SetTorEnabled handler, so the deadline always matches the transport actually in force. Co-Authored-By: Claude Code --- crates/client-runtime/src/runtime.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/client-runtime/src/runtime.rs b/crates/client-runtime/src/runtime.rs index 470fbb10..17857e03 100644 --- a/crates/client-runtime/src/runtime.rs +++ b/crates/client-runtime/src/runtime.rs @@ -290,7 +290,9 @@ pub struct CoreConfig { /// trusts the device id derived from the first key, never this name. pub device_label: Option, /// Backoff / stale-window timing. [`CoreConfig::new`] picks the Tor variant - /// when `tor` is set; tests override directly. + /// when `tor` is set; tests override directly. The loop re-derives it the + /// same way whenever `Intent::SetTorEnabled` changes the transport at + /// runtime. pub reconnect: ReconnectConfig, } @@ -2183,6 +2185,12 @@ impl Loop { let proxy = if on { self.tor_proxy_address.clone() } else { None }; self.nostr.set_proxy(proxy.clone()); self.http.set_proxy(proxy.as_deref()); + // The timing must follow the transport in force, not only the one + // this process booted with: a phone that starts on clearnet and + // turns Orbot on later would otherwise keep the shorter clearnet + // connect deadline, which abandons an attempt while the transport + // is still allowed to finish a Tor dial. + self.reconnect = if on { TOR_RECONNECT_CONFIG } else { DEFAULT_RECONNECT_CONFIG }; } // Orbot toggled, a machine removed, or its endpoints edited. self.sync_direct_links(); From ff69a3b19917a8659c1ac5d210d360981f519eb5 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:22:56 +0100 Subject: [PATCH 06/42] test(client): guard Tor routing when it is toggled at runtime - Enabling Tor on a live core must redial the relay through the SOCKS5 proxy, and Blossom must use it too; disabling Tor must dial both directly again. - Enabling Tor must widen the connect deadline to the Tor value. - With Tor on, a direct link's onion endpoint goes through the proxy while a LAN endpoint is dialled directly. The tests speak through the runtime's public surface: intents in, socket and CONNECT observations out. A loopback SOCKS5 proxy records every CONNECT target and forwards the reachable ones. MockRelay now reports accepted connections, so a test can prove a new relay connection was made without depending on frame ordering. Co-Authored-By: Claude Code --- crates/client-runtime/src/runtime/tests.rs | 335 +++++++++++++++++++++ crates/nostr-transport/src/mock.rs | 16 + 2 files changed, 351 insertions(+) diff --git a/crates/client-runtime/src/runtime/tests.rs b/crates/client-runtime/src/runtime/tests.rs index f2803c3f..eb3c740c 100644 --- a/crates/client-runtime/src/runtime/tests.rs +++ b/crates/client-runtime/src/runtime/tests.rs @@ -288,6 +288,341 @@ async fn spawn_leaves_the_http_port_direct_when_tor_is_off() { .await; } +/// Spawn a core with the given Tor settings; the caller points it at relays. +/// The `fast_reconnect` override keeps the FSM timers out of the test's way. +async fn core_with_tor( + phone: &Keypair, + spy: Rc, + ports: CorePorts, + proxy: Option, + tor: bool, +) -> Core { + Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy, + tor, + device_label: None, + reconnect: fast_reconnect(), + }, + ports, + spy, + Rc::new(FixedClock(RefCell::new(1_000_000))), + Rc::new(ZeroEntropy), + ) + .await +} + +/// A SOCKS5 proxy on loopback that records every `CONNECT` target and, for a +/// reachable target, forwards the connection. A test asserts through it that +/// traffic expected to be proxied really is — and that traffic expected to be +/// direct never appears here. +struct SocksProxy { + /// `127.0.0.1:` — the proxy address a `CoreConfig` is given. + address: String, + connects: mpsc::UnboundedReceiver<(String, u16)>, +} + +impl SocksProxy { + async fn spawn() -> Self { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap().to_string(); + let (tx, connects) = mpsc::unbounded_channel(); + tokio::task::spawn_local(async move { + while let Ok((socket, _)) = listener.accept().await { + tokio::task::spawn_local(serve_socks5(socket, tx.clone())); + } + }); + Self { address, connects } + } + + /// The next `CONNECT` the proxy saw, or `None` if none came within `within`. + async fn next_connect(&mut self, within: Duration) -> Option<(String, u16)> { + tokio::time::timeout(within, self.connects.recv()).await.ok().flatten() + } + + /// A `CONNECT` the proxy saw and nobody consumed yet. + fn pending_connect(&mut self) -> Option<(String, u16)> { + self.connects.try_recv().ok() + } +} + +/// Serve one SOCKS5 client: no-auth greeting, one `CONNECT`, then forward. +/// A `.onion` name cannot be resolved here, so it is refused the way a Tor +/// proxy refuses a service it cannot reach — the point for the tests is that +/// the request reached the proxy at all. +async fn serve_socks5( + mut client: tokio::net::TcpStream, + connects: mpsc::UnboundedSender<(String, u16)>, +) { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + let mut greeting = [0u8; 2]; + if client.read_exact(&mut greeting).await.is_err() || greeting[0] != 5 { + return; + } + let mut methods = vec![0u8; greeting[1] as usize]; + if client.read_exact(&mut methods).await.is_err() { + return; + } + if client.write_all(&[5, 0]).await.is_err() { + return; + } + + let mut request = [0u8; 4]; + if client.read_exact(&mut request).await.is_err() || request[0] != 5 || request[1] != 1 { + return; + } + let host = match request[3] { + 1 => { + let mut ip = [0u8; 4]; + if client.read_exact(&mut ip).await.is_err() { + return; + } + ip.iter().map(|b| b.to_string()).collect::>().join(".") + } + 3 => { + let mut len = [0u8; 1]; + if client.read_exact(&mut len).await.is_err() { + return; + } + let mut name = vec![0u8; len[0] as usize]; + if client.read_exact(&mut name).await.is_err() { + return; + } + String::from_utf8_lossy(&name).to_string() + } + 4 => { + let mut ip = [0u8; 16]; + if client.read_exact(&mut ip).await.is_err() { + return; + } + std::net::Ipv6Addr::from(ip).to_string() + } + _ => return, + }; + let mut port = [0u8; 2]; + if client.read_exact(&mut port).await.is_err() { + return; + } + let port = u16::from_be_bytes(port); + let _ = connects.send((host.clone(), port)); + + if host.ends_with(".onion") { + let _ = client.write_all(&[5, 4, 0, 1, 0, 0, 0, 0, 0, 0]).await; + return; + } + match tokio::net::TcpStream::connect((host.as_str(), port)).await { + Ok(mut upstream) => { + if client.write_all(&[5, 0, 0, 1, 0, 0, 0, 0, 0, 0]).await.is_err() { + return; + } + let _ = tokio::io::copy_bidirectional(&mut client, &mut upstream).await; + } + Err(_) => { + let _ = client.write_all(&[5, 4, 0, 1, 0, 0, 0, 0, 0, 0]).await; + } + } +} + +/// Tor is switched on while the core is already connected: the next relay +/// connection is dialled through the SOCKS5 proxy (no Nostr traffic takes a +/// direct path while Tor is on), Blossom uploads follow, and switching Tor +/// off redials relay traffic directly. +#[tokio::test] +async fn toggling_tor_at_runtime_moves_relay_and_http_traffic_through_the_proxy_and_back() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let spy = Rc::new(Spy::default()); + let recording = Rc::new(RecordingHttp::default()); + let mut proxy = SocksProxy::spawn().await; + let proxy_address = proxy.address.clone(); + + let core = core_with_tor( + &phone, + Rc::clone(&spy), + CorePorts { + http: Rc::clone(&recording) as Rc, + ..CorePorts::default() + }, + Some(proxy_address.clone()), + false, + ) + .await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.set_relays(vec![mock.url.clone()]); + core.start(); + eose_all(&mut mock).await; + while mock.try_accept() {} + assert!( + proxy.pending_connect().is_none(), + "with Tor off the relay connection must not touch the proxy" + ); + assert!(recording.proxy_calls.borrow().is_empty(), "with Tor off Blossom stays direct"); + + core.dispatch(Intent::SetTorEnabled(true)).await; + let relay = mock.url.trim_start_matches("ws://").to_string(); + let (relay_host, relay_port) = relay.split_once(':').unwrap(); + assert_eq!( + proxy.next_connect(Duration::from_secs(5)).await, + Some((relay_host.to_string(), relay_port.parse().unwrap())), + "enabling Tor must redial the relay through the proxy" + ); + assert!( + mock.next_accept(Duration::from_secs(5)).await, + "the proxied relay dial must reach the relay" + ); + assert_eq!( + recording.proxy_calls.borrow().as_slice(), + [Some(proxy_address.clone())], + "Blossom uploads must follow the same switch" + ); + assert!(proxy.pending_connect().is_none(), "one relay, one proxied dial"); + + core.dispatch(Intent::SetTorEnabled(false)).await; + assert!( + mock.next_accept(Duration::from_secs(5)).await, + "disabling Tor must redial the relay" + ); + assert!( + proxy.pending_connect().is_none(), + "a relay dial with Tor off must bypass the proxy" + ); + assert_eq!( + recording.proxy_calls.borrow().as_slice(), + [Some(proxy_address.clone()), None], + "Blossom is direct again once Tor is off" + ); + }) + .await; +} + +/// Enabling Tor at runtime swaps in the Tor reconnect timing: the connect +/// deadline must cover the slower dial the proxy just introduced. With the +/// clearnet deadline still in force, an attempt that is still dialling is +/// abandoned long before Tor's own transport would give up. +#[tokio::test] +async fn enabling_tor_at_runtime_widens_the_connect_deadline_to_tors() { + LocalSet::new() + .run_until(async { + // Accepts the TCP connection but never answers the WebSocket + // handshake, so the attempt stays `Connecting`. + let silent = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let relay = format!("ws://{}", silent.local_addr().unwrap()); + tokio::task::spawn_local(async move { + let mut held = Vec::new(); + while let Ok((socket, _)) = silent.accept().await { + held.push(socket); + } + }); + let proxy = SocksProxy::spawn().await; + let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone)), + proxy: Some(proxy.address.clone()), + tor: false, + device_label: None, + reconnect: DEFAULT_RECONNECT_CONFIG, + }, + CorePorts::default(), + Rc::new(Spy::default()), + Rc::clone(&clock) as Rc, + Rc::new(ZeroEntropy), + ) + .await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.set_relays(vec![relay]); + core.start(); + assert_eq!( + core.connection_status().await.0, + ConnectionStatus::Connecting, + "the attempt starts on the clearnet timing" + ); + + core.dispatch(Intent::SetTorEnabled(true)).await; + *clock.0.borrow_mut() += DEFAULT_RECONNECT_CONFIG.connect_deadline_ms + 1; + core.tick(); + assert_eq!( + core.connection_status().await.0, + ConnectionStatus::Connecting, + "enabling Tor must widen the connect deadline, not keep the clearnet one" + ); + + *clock.0.borrow_mut() = 1_000_000 + TOR_RECONNECT_CONFIG.connect_deadline_ms; + core.tick(); + assert_eq!( + core.connection_status().await.0, + ConnectionStatus::WaitingRetry, + "the Tor deadline still bounds an attempt that never reaches EOSE" + ); + }) + .await; +} + +/// With Tor on, a direct link's `.onion` endpoint goes through the proxy — +/// only Tor reaches it — while a LAN endpoint is dialled directly. A direct +/// link is the user's own bridge on their own network, not Nostr traffic. +#[tokio::test] +async fn with_tor_on_a_direct_link_proxies_its_onion_endpoint_and_dials_lan_directly() { + LocalSet::new() + .run_until(async { + let lan = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let lan_address = lan.local_addr().unwrap(); + let (lan_hit_tx, mut lan_hit_rx) = mpsc::unbounded_channel(); + tokio::task::spawn_local(async move { + let mut held = Vec::new(); + while let Ok((socket, _)) = lan.accept().await { + held.push(socket); + let _ = lan_hit_tx.send(()); + } + }); + let mut proxy = SocksProxy::spawn().await; + + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + state.machines.get_mut(&machine.pubkey_hex).unwrap().direct = Some(protocol::direct::DirectInfo { + endpoints: vec!["ws://abc.onion:7448".into(), format!("ws://{lan_address}")], + cert_sha256: None, + }); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + // No relay is configured: nothing but the direct link may reach + // the proxy, so its CONNECT log is unambiguous. + let core = core_with_tor( + &phone, + Rc::new(Spy::default()), + CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, + Some(proxy.address.clone()), + true, + ) + .await; + core.start(); + + assert_eq!( + proxy.next_connect(Duration::from_secs(5)).await, + Some(("abc.onion".to_string(), 7448)), + "the onion endpoint must be dialled through the proxy" + ); + tokio::time::timeout(Duration::from_secs(5), lan_hit_rx.recv()) + .await + .expect("the LAN endpoint must be dialled") + .expect("the LAN listener is alive"); + assert!( + proxy.pending_connect().is_none(), + "the LAN endpoint must be dialled directly, never through the proxy" + ); + }) + .await; +} + /// Decrypt + decode a relay `EVENT` frame as a `PhoneToBridge` command from /// `phone` to `machine` — `None` if the frame isn't an EVENT, isn't tagged /// for `machine`, or doesn't decrypt/decode as one. diff --git a/crates/nostr-transport/src/mock.rs b/crates/nostr-transport/src/mock.rs index 1978f152..cf1a7657 100644 --- a/crates/nostr-transport/src/mock.rs +++ b/crates/nostr-transport/src/mock.rs @@ -16,6 +16,9 @@ pub struct MockRelay { pub url: String, inbound: mpsc::UnboundedReceiver, outbound: mpsc::UnboundedSender, + /// One signal per accepted TCP connection, in accept order. Frames alone + /// cannot say WHICH connection carried them; this can. + accepted: mpsc::UnboundedReceiver<()>, } impl MockRelay { @@ -32,6 +35,16 @@ impl MockRelay { tokio::time::timeout(wait, self.inbound.recv()).await.ok().flatten() } + /// Wait up to `wait` for the next TCP connection to be accepted. + pub async fn next_accept(&mut self, wait: Duration) -> bool { + tokio::time::timeout(wait, self.accepted.recv()).await.ok().flatten().is_some() + } + + /// Whether a connection was accepted and nobody consumed it yet. + pub fn try_accept(&mut self) -> bool { + self.accepted.try_recv().is_ok() + } + /// Push a text frame to the client. pub fn push(&self, frame: impl Into) { let _ = self.outbound.send(frame.into()); @@ -49,9 +62,11 @@ pub async fn mock_relay() -> MockRelay { let port = listener.local_addr().unwrap().port(); let (in_tx, inbound) = mpsc::unbounded_channel::(); let (outbound, mut out_rx) = mpsc::unbounded_channel::(); + let (accept_tx, accepted) = mpsc::unbounded_channel::<()>(); tokio::spawn(async move { loop { let Ok((tcp, _)) = listener.accept().await else { return }; + let _ = accept_tx.send(()); let Ok(mut ws) = tokio_tungstenite::accept_async(tcp).await else { continue }; loop { tokio::select! { @@ -74,5 +89,6 @@ pub async fn mock_relay() -> MockRelay { url: format!("ws://127.0.0.1:{port}"), inbound, outbound, + accepted, } } From 015bdcec8ccde782b12ad05ba30703f545a52b5d Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:22:57 +0100 Subject: [PATCH 07/42] fix(agent-host): keep credentials out of error text and logs Error text can come from layers that do not know which values are secret: an HTTP client may quote the request headers it failed on, a spawned OpenCode server may print the environment it ran in, and a provider or CLI refusal may repeat the key it was given. Those errors were logged and relayed to the phone verbatim. Add `sdk/redact` and remove the credential values a path knows about from any error text before it is logged or returned: the provider token and headers read from an endpoint, an OpenCode provider token in the server environment and in a catalog read, an account key or a pasted sign-in address, an MCP server's env, header and argument values, and the credentials a Claude session was started with. Diagnostics stay otherwise intact. Co-Authored-By: Claude Code --- .../agent-host/src/drivers/claude/driver.ts | 31 +++++++++++--- packages/agent-host/src/drivers/claude/mcp.ts | 7 +++- .../src/drivers/opencode/accounts.ts | 8 +++- .../agent-host/src/drivers/opencode/driver.ts | 23 +++++++++-- .../agent-host/src/drivers/opencode/mcp.ts | 17 +++++--- packages/agent-host/src/sdk/mcp.ts | 15 ++++++- packages/agent-host/src/sdk/providerModels.ts | 8 +++- packages/agent-host/src/sdk/redact.ts | 41 +++++++++++++++++++ 8 files changed, 130 insertions(+), 20 deletions(-) create mode 100644 packages/agent-host/src/sdk/redact.ts diff --git a/packages/agent-host/src/drivers/claude/driver.ts b/packages/agent-host/src/drivers/claude/driver.ts index 71b9164a..2061db23 100644 --- a/packages/agent-host/src/drivers/claude/driver.ts +++ b/packages/agent-host/src/drivers/claude/driver.ts @@ -37,6 +37,7 @@ import type { UsageData, } from '../../sdk/types'; import { sdkMessageToEntries } from './adapter'; +import { redactSecrets } from '../../sdk/redact'; import { ANTHROPIC_API_KEY_CREDENTIAL, buildClaudeEnv } from './env'; import { CLAUDE_STYLE } from './style'; import { @@ -183,12 +184,25 @@ export interface ClaudeDriverDeps { readVersion?: () => Promise; } +/** The credential values one session's parameters carry. */ +function sessionSecrets(params: StartSession): string[] { + return [ + ...Object.values(params.credentials ?? {}), + ...Object.values(params.env ?? {}), + ...(params.provider ? [params.provider.authToken] : []), + ]; +} + export class ClaudeSession implements DriverSession { private handle: SdkSessionHandle | null = null; private readonly translate: TranslateContext; /** Told a tool name no agent's table lists, which the log then carries. */ private readonly reportUnknownTool: (toolName: string) => void; private readonly resumeTarget: string | null; + /** Every credential value this session was handed. The SDK's errors carry + * the CLI's stderr tail, which can quote the environment it ran in, so + * these are removed from anything the session logs or reports. */ + private readonly secrets: readonly string[]; /** Coalesces the SDK's streamed deltas into `partial` events (sdk/partial): * a few signed, relayed updates a second, not one per token. */ private readonly partials: PartialCoalescer; @@ -246,6 +260,7 @@ export class ClaudeSession implements DriverSession { private readonly options: ClaudeDriverOptions, ) { this.resumeTarget = params.resume ?? null; + this.secrets = sessionSecrets(params); this.reportUnknownTool = unknownToolReporter((message) => this.ctx.log(message)); this.translate = newTranslateContext(this.reportUnknownTool); this.partials = new PartialCoalescer((update) => { @@ -341,7 +356,10 @@ export class ClaudeSession implements DriverSession { this.ended = true; this.dropLiveText(); void Promise.resolve(this.handle?.end()).catch(() => {}); - this.ctx.emit({ type: 'ended', ...(error ? { error } : {}), ...(resumeLost ? { resumeLost } : {}) }); + // The reason can be an SDK error quoting the CLI's stderr tail; it + // travels to the phone, so no credential value may survive in it. + const safe = error === undefined ? undefined : redactSecrets(error, this.secrets); + this.ctx.emit({ type: 'ended', ...(safe ? { error: safe } : {}), ...(resumeLost ? { resumeLost } : {}) }); } private async consume(handle: SdkSessionHandle): Promise { @@ -394,12 +412,13 @@ export class ClaudeSession implements DriverSession { if (msg.type === 'auth_status') { const auth = msg as SdkAuthStatusMessage; if (!auth.error) return; - this.ctx.log(`[claude] auth error for ${this.ctx.sessionId}: ${auth.error}`); + const reason = redactSecrets(auth.error, this.secrets); + this.ctx.log(`[claude] auth error for ${this.ctx.sessionId}: ${reason}`); if (!this.ready) { - this.finish(`Authentication failed: ${auth.error}`); + this.finish(`Authentication failed: ${reason}`); return; } - this.entries([{ entryType: 'notice', kind: 'auth_error', text: `Authentication failed: ${auth.error}`, timestamp: new Date().toISOString() }]); + this.entries([{ entryType: 'notice', kind: 'auth_error', text: `Authentication failed: ${reason}`, timestamp: new Date().toISOString() }]); return; } @@ -671,7 +690,7 @@ export class ClaudeSession implements DriverSession { queueMicrotask(() => { void this.handle?.setPermissionMode(mode).then( () => this.setMode(mode), - (err) => this.ctx.log(`[claude] could not switch ${this.ctx.sessionId} to ${mode}: ${err}`), + (err) => this.ctx.log(`[claude] could not switch ${this.ctx.sessionId} to ${mode}: ${redactSecrets(String(err), this.secrets)}`), ); }); return { behavior: 'allow', updatedInput: input }; @@ -775,7 +794,7 @@ export class ClaudeSession implements DriverSession { this.handle.getUsageSnapshot().then( (raw) => (raw === null || raw === undefined ? null : normalizeUsage(raw)), (err) => { - this.ctx.log(`[claude] usage failed for ${this.ctx.sessionId}: ${err}`); + this.ctx.log(`[claude] usage failed for ${this.ctx.sessionId}: ${redactSecrets(String(err), this.secrets)}`); return null; }, ), diff --git a/packages/agent-host/src/drivers/claude/mcp.ts b/packages/agent-host/src/drivers/claude/mcp.ts index 8a0cbc05..ac10d762 100644 --- a/packages/agent-host/src/drivers/claude/mcp.ts +++ b/packages/agent-host/src/drivers/claude/mcp.ts @@ -15,7 +15,8 @@ import { readFile } from 'node:fs/promises'; import { homedir } from 'node:os'; import * as path from 'node:path'; import type { McpManager, McpState } from '../../sdk/driver'; -import { serverInfo } from '../../sdk/mcp'; +import { serverInfo, setupSecrets } from '../../sdk/mcp'; +import { redactSecrets } from '../../sdk/redact'; import type { McpAction, McpServerAdd, McpServerInfo } from '../../sdk/types'; import { failureMessage, type CliRunner } from './plugins'; @@ -109,12 +110,14 @@ export class ClaudeMcp implements McpManager { if (runs.length === 0) { throw new Error('Claude Code cannot switch a server off without removing it. Switch it off in a session instead.'); } + // A CLI refusal can quote the JSON it was handed — env, headers and all. + const secrets = servers.flatMap((server) => setupSecrets(server.setup)); for (const args of runs) { // A server added again replaces the old one: `add-json` refuses a // name that exists, so it is removed first (a missing one is fine). if (action === 'add') await this.run(['mcp', 'remove', '-s', 'user', '--', args[5]!], CHANGE_TIMEOUT_MS); const result = await this.run(args, CHANGE_TIMEOUT_MS); - if (result.code !== 0) throw new Error(failureMessage(result)); + if (result.code !== 0) throw new Error(redactSecrets(failureMessage(result), secrets)); } await this.onChanged().catch(() => {}); return this.list(); diff --git a/packages/agent-host/src/drivers/opencode/accounts.ts b/packages/agent-host/src/drivers/opencode/accounts.ts index 3b79490b..ea267819 100644 --- a/packages/agent-host/src/drivers/opencode/accounts.ts +++ b/packages/agent-host/src/drivers/opencode/accounts.ts @@ -25,6 +25,7 @@ * is read here, and nothing of it is passed on. */ import type { OpenCodeClient } from '@opencode/client'; +import { redactSecrets } from '../../sdk/redact'; import type { AccountChangeResult, AccountManager, SignInState } from '../../sdk/driver'; import type { AccountChange, AccountConnection, AccountField, AccountMethod, AgentAccount, SignInStatus } from '../../sdk/types'; @@ -120,7 +121,12 @@ export class OpenCodeAccounts implements AccountManager { return { signIn: await this.cancel(client, account, change.attempt) }; } } catch (err) { - throw new Error(reason(err)); + // The server's refusal can repeat the key it was given, and a pasted + // sign-in address carries its one-time code. Neither may travel back + // to the phone in the error. + const secrets = + change.kind === 'key' ? [change.key] : change.kind === 'finish' ? [change.callbackUrl] : []; + throw new Error(redactSecrets(reason(err), secrets)); } } diff --git a/packages/agent-host/src/drivers/opencode/driver.ts b/packages/agent-host/src/drivers/opencode/driver.ts index 2e05ecb7..ec69371b 100644 --- a/packages/agent-host/src/drivers/opencode/driver.ts +++ b/packages/agent-host/src/drivers/opencode/driver.ts @@ -86,6 +86,7 @@ import { } from './server'; import type { ProviderHttp } from '../../sdk/net'; import { isValidProviderBaseUrl } from '../../sdk/provider'; +import { redactSecrets } from '../../sdk/redact'; import { checkToken, listModels, OPENAI_API } from '../../sdk/providerApi'; import type { EndpointModel } from '../../sdk/providerModels'; import { OPENCODE_STYLE } from './style'; @@ -1926,7 +1927,12 @@ export class OpenCodeDriver implements Driver { ...(this.options.port !== undefined ? { port: this.options.port } : {}), }); } catch (err) { - throw new Error(`The OpenCode server failed to start: ${err instanceof Error ? err.message : String(err)}`); + // A child that dies can print the environment it ran in; the provider + // tokens and the server's own password are the part that may not + // travel on to the log or the bridge. + const reason = err instanceof Error ? err.message : String(err); + const secrets = [...Object.values(setup.env), ...Object.values(setup.headers)]; + throw new Error(`The OpenCode server failed to start: ${redactSecrets(reason, secrets)}`); } if (this.stopped) { await server.close(); @@ -2048,7 +2054,13 @@ export class OpenCodeDriver implements Driver { await this.clientPromise?.catch(() => {}); // What the running server lists, less what the profiles it runs with // added: the catalog a server without them would have. - const read = await this.readCatalog(); + // The running server's config holds the profiles it was started with; + // the ones just handed over are the other values a read failure may + // quote, since the client sees the same config. + const read = await this.readCatalog([ + ...this.placed.map((p) => p.profile.authToken), + ...usable.map((p) => p.authToken), + ]); const live = read && withoutProfiles(read, this.serving); // It keeps what models.dev filled in, which no server lists. A server // that cannot say keeps the catalog it was configured from: a restart @@ -2103,7 +2115,7 @@ export class OpenCodeDriver implements Driver { /** The running server's catalog (catalog.ts). None when it cannot say: * with no other to go by, every profile is then a provider of its own, * nothing refused or filled in. */ - private async readCatalog(): Promise { + private async readCatalog(secrets: readonly string[] = this.placed.map((p) => p.profile.authToken)): Promise { try { const client = await this.clientPromise; if (!client) return undefined; @@ -2132,7 +2144,10 @@ export class OpenCodeDriver implements Driver { connected: new Set(available.data.map((p) => p.id)), }; } catch (err) { - this.options.log(`[opencode] could not read its provider catalog, so profiles are not checked against it: ${err instanceof Error ? err.message : String(err)}`); + const reason = err instanceof Error ? err.message : String(err); + this.options.log( + `[opencode] could not read its provider catalog, so profiles are not checked against it: ${redactSecrets(reason, secrets)}`, + ); return undefined; } } diff --git a/packages/agent-host/src/drivers/opencode/mcp.ts b/packages/agent-host/src/drivers/opencode/mcp.ts index be4dde10..08a7c994 100644 --- a/packages/agent-host/src/drivers/opencode/mcp.ts +++ b/packages/agent-host/src/drivers/opencode/mcp.ts @@ -13,7 +13,8 @@ */ import type { OpenCodeClient } from '@opencode/client'; import type { McpManager, McpState, SessionMcpState } from '../../sdk/driver'; -import { mcpStatus, serverInfo } from '../../sdk/mcp'; +import { mcpStatus, serverInfo, setupSecrets } from '../../sdk/mcp'; +import { redactSecrets } from '../../sdk/redact'; import type { McpAction, McpServerAdd, McpServerInfo } from '../../sdk/types'; /** A server as OpenCode v2's config holds it. Absent env or headers are left @@ -67,9 +68,15 @@ export class OpenCodeMcp implements McpManager { if (missing) throw new Error(`OpenCode has no MCP server '${missing}'.`); for (const name of names) await this.call(`remove ${name}`, () => client.mcp.remove({ server: name })); } else if (action === 'add') { + // A refusal can quote the config it was handed — env, headers and + // all. The setup's credential values are removed before it travels. + const secrets = servers.flatMap((add) => setupSecrets(add.setup)); for (const add of servers) { - await this.call(`add ${add.name}`, () => - client.mcp.add({ server: add.name, config: openCodeServerConfig(add) as never })); + await this.call( + `add ${add.name}`, + () => client.mcp.add({ server: add.name, config: openCodeServerConfig(add) as never }), + secrets, + ); } } else { const missing = names.find((n) => !current.has(n)); @@ -93,11 +100,11 @@ export class OpenCodeMcp implements McpManager { return new Set(listed.data.map((s) => s.name)); } - private async call(what: string, work: () => Promise): Promise { + private async call(what: string, work: () => Promise, secrets: readonly string[] = []): Promise { try { await work(); } catch (err) { - throw new Error(`OpenCode could not ${what}: ${reason(err)}`); + throw new Error(redactSecrets(`OpenCode could not ${what}: ${reason(err)}`, secrets)); } } diff --git a/packages/agent-host/src/sdk/mcp.ts b/packages/agent-host/src/sdk/mcp.ts index 7c941490..a12052e6 100644 --- a/packages/agent-host/src/sdk/mcp.ts +++ b/packages/agent-host/src/sdk/mcp.ts @@ -4,7 +4,7 @@ * (its arguments may carry a token); a remote one its URL without user info, * query or fragment; env variables and headers by name only. */ -import type { McpServerInfo, McpStatus } from './types'; +import type { McpServerInfo, McpServerSetup, McpStatus } from './types'; /** `url` without the parts that may carry a credential. */ export function redactUrl(url: string): string { @@ -40,6 +40,19 @@ export function serverInfo(raw: RawServer): McpServerInfo { }; } +/** + * The credential values one server's setup carries: env and header values, + * and — for a stdio server — its arguments, which commonly hold a token. + * These are removed from any error text about the server before it is logged + * or sent on. + */ +export function setupSecrets(setup: McpServerSetup): string[] { + if (setup.type === 'stdio') { + return [...(setup.args ?? []), ...Object.values(setup.env ?? {})]; + } + return [setup.url, ...Object.values(setup.headers ?? {})]; +} + /** An agent's own status word for a server, mapped onto the wire's. */ export function mcpStatus(raw: string | undefined): McpStatus { switch (raw) { diff --git a/packages/agent-host/src/sdk/providerModels.ts b/packages/agent-host/src/sdk/providerModels.ts index 3cfe1918..6dbbeca2 100644 --- a/packages/agent-host/src/sdk/providerModels.ts +++ b/packages/agent-host/src/sdk/providerModels.ts @@ -15,6 +15,7 @@ * `deepseek/deepseek-chat`) read the same way. */ import type { HttpGet } from './net'; +import { redactSecrets } from './redact'; /** One model an endpoint serves. */ export interface EndpointModel { @@ -165,6 +166,10 @@ export async function readProviderModels( } catch { throw new Error(`'${baseUrl}' is not a URL`); } + // An HTTP client's failure can quote the request it made, headers and all. + // The token is the one thing that text must never carry on to a log or the + // phone, so every form of it that was sent is removed before anyone sees it. + const sent = [options.token, ...Object.values(options.headers ?? {})]; let response: Awaited>; try { response = await options.httpGet(url, { @@ -172,7 +177,8 @@ export async function readProviderModels( ...(options.token ? { authorization: `Bearer ${options.token}` } : {}), }); } catch (error) { - throw new Error(`${url} could not be read (${error instanceof Error ? error.message : String(error)})`); + const reason = error instanceof Error ? error.message : String(error); + throw new Error(`${url} could not be read (${redactSecrets(reason, sent)})`); } if (response.status === 401 || response.status === 403) { throw new Error(`the provider refused the token (HTTP ${response.status})`); diff --git a/packages/agent-host/src/sdk/redact.ts b/packages/agent-host/src/sdk/redact.ts new file mode 100644 index 00000000..3d889618 --- /dev/null +++ b/packages/agent-host/src/sdk/redact.ts @@ -0,0 +1,41 @@ +/** + * Take credential values out of text that is about to be logged or sent to + * the phone. + * + * Some error text is not ours: an HTTP client can quote the request it + * failed on (headers included), a spawned agent can print its environment, + * and a provider's own refusal can repeat the key it was given. Those layers + * must keep their diagnostics, so the value is removed here instead — in the + * forms it commonly travels in, so neither the raw token nor its base64 + * encoding survives. Only values long enough to be a credential are + * replaced: a shorter one can occur in ordinary words and would mangle the + * message without hiding a real secret. + */ + +export const REDACTED = '[redacted]'; + +/** Below this length a value is not treated as a credential. */ +const MIN_SECRET_LENGTH = 8; + +/** The forms one secret may appear in, longest first. */ +function formsOf(secret: string): string[] { + const bytes = Buffer.from(secret, 'utf8'); + const forms = new Set([ + secret, + `Bearer ${secret}`, + bytes.toString('base64'), + bytes.toString('base64').replace(/=+$/, ''), + bytes.toString('base64url'), + ]); + return [...forms].filter((form) => form.length >= MIN_SECRET_LENGTH).sort((a, b) => b.length - a.length); +} + +/** `text` with every occurrence of each secret (and its encodings) removed. */ +export function redactSecrets(text: string, secrets: Iterable): string { + let out = text; + for (const secret of secrets) { + if (typeof secret !== 'string' || secret.length < MIN_SECRET_LENGTH) continue; + for (const form of formsOf(secret)) out = out.split(form).join(REDACTED); + } + return out; +} From 3f91711eac0a9ae0b2bec7dc6055e3f5be90c8cd Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:23:07 +0100 Subject: [PATCH 08/42] test(agent-host): guard secrets against log and wire leaks The credential rule was checked on request payloads and process environments but never on the diagnostic log or the frames the host relays. Add one shared guard (`sdk/__tests__/secrets`) that generates distinctive secrets and fails on a whole value, any 12-character part, a Bearer header or a base64 encoding, naming the offending log line, wire frame or session event. Drive each place a secret enters the host through the public APIs: host request handling, provider checks and model listings for all four drivers, the session environment mapping of opencode, claude, deepseek and qwen, OpenCode accounts and sign-in, MCP server setup in opencode, claude and deepseek, and OpenCode plugin options. Failure paths are covered because that is where the value escaped: HTTP errors quoting headers, a failed server start quoting the environment, a refused key, a failed paste-back sign-in and a CLI refusal quoting the server JSON. Mutation-checked by logging the secret in three guarded paths (an OpenCode server-start failure, a Claude session failure and DeepSeek's endpoint-catalog read); each test failed as intended, and the log lines were removed again. Co-Authored-By: Claude Code --- .../claude/__tests__/claudeSecrets.test.ts | 146 +++++++++ .../__tests__/deepseekSecrets.test.ts | 151 +++++++++ .../__tests__/opencodeSecrets.test.ts | 295 ++++++++++++++++++ .../qwen/__tests__/qwenSecrets.test.ts | 146 +++++++++ .../src/host/__tests__/secrets.test.ts | 150 +++++++++ .../src/sdk/__tests__/secrets.test.ts | 40 +++ .../agent-host/src/sdk/__tests__/secrets.ts | 112 +++++++ 7 files changed, 1040 insertions(+) create mode 100644 packages/agent-host/src/drivers/claude/__tests__/claudeSecrets.test.ts create mode 100644 packages/agent-host/src/drivers/deepseek/__tests__/deepseekSecrets.test.ts create mode 100644 packages/agent-host/src/drivers/opencode/__tests__/opencodeSecrets.test.ts create mode 100644 packages/agent-host/src/drivers/qwen/__tests__/qwenSecrets.test.ts create mode 100644 packages/agent-host/src/host/__tests__/secrets.test.ts create mode 100644 packages/agent-host/src/sdk/__tests__/secrets.test.ts create mode 100644 packages/agent-host/src/sdk/__tests__/secrets.ts diff --git a/packages/agent-host/src/drivers/claude/__tests__/claudeSecrets.test.ts b/packages/agent-host/src/drivers/claude/__tests__/claudeSecrets.test.ts new file mode 100644 index 00000000..61a38cc8 --- /dev/null +++ b/packages/agent-host/src/drivers/claude/__tests__/claudeSecrets.test.ts @@ -0,0 +1,146 @@ +/** + * Claude Code's secret-carrying paths: a provider token on the API, the + * credentials a session's environment carries, and an MCP server whose setup + * holds a token. + * + * The SDK and the `claude` CLI are seams here. A failure at either is + * exactly where a credential escapes: SDK errors carry the CLI's stderr tail, + * and a CLI refusal can quote the JSON it was given. + */ +import { describe, expect, it } from 'vitest'; +import type { ProviderHttp } from '../../../sdk/net'; +import type { ProviderBinding, StartSession } from '../../../sdk/types'; +import { TEST_SECRETS, expectNoSecretLeaks } from '../../../sdk/__tests__/secrets'; +import { recordingContext } from '../../../sdk/__tests__/context'; +import type { SdkFacade, SdkSessionHandle, SdkSessionOptions } from '../facade'; +import { ClaudeDriver } from '../driver'; +import type { CliResult } from '../plugins'; + +const profile = (over: Partial = {}): ProviderBinding => ({ + id: 'home', + label: 'Home router', + baseUrl: 'https://gw.example', + authToken: TEST_SECRETS.providerToken, + models: [{ id: 'm' }], + defaultModel: 'm', + ...over, +}); + +/** A facade that answers the control probe and then keeps its stream open. */ +function fakeFacade(probe: Promise = Promise.resolve()): { + facade: SdkFacade; + sessions: SdkSessionOptions[]; +} { + const sessions: SdkSessionOptions[] = []; + const handle = { + probeReady: () => probe, + messages: () => + (async function* () { + await new Promise(() => {}); + })(), + pushInput: () => {}, + setPermissionMode: async () => {}, + setModel: async () => {}, + setEffort: async () => {}, + interrupt: async () => {}, + stopTask: async () => {}, + getContextUsage: async () => null, + supportedCommands: async () => null, + mcpServerStatus: async () => null, + toggleMcpServer: async () => {}, + reloadPlugins: async () => {}, + getUsageSnapshot: async () => null, + end: async () => {}, + } as unknown as SdkSessionHandle; + const facade = { + createSession: (options: SdkSessionOptions) => { + sessions.push(options); + return handle; + }, + supportedModels: async () => [], + deleteSession: async () => {}, + } as unknown as SdkFacade; + return { facade, sessions }; +} + +describe('Claude provider profiles', () => { + it('checks and lists a profile without echoing its token on failure', async () => { + const deny = async () => { + throw new Error(`HTTP 500 — x-api-key: ${TEST_SECRETS.providerToken}`); + }; + const providerHttp: ProviderHttp = { get: deny, post: deny }; + const driver = new ClaudeDriver({ facade: fakeFacade().facade, providerHttp }); + expect(await driver.checkProvider(profile(), 'm')).toBeUndefined(); + const failure = await driver.listProviderModels('https://gw.example', TEST_SECRETS.providerToken).then( + () => undefined, + (error: Error) => error, + ); + expect(failure?.message).toMatch(/gw\.example/); + expectNoSecretLeaks({ frames: [failure?.message ?? ''] }); + }); +}); + +describe('Claude session environment', () => { + it('hands the credentials to the session without logging or reporting them', async () => { + const { facade, sessions } = fakeFacade(); + const ctx = recordingContext(); + const params: StartSession = { + sessionId: 's1', + agent: 'claude-code', + cwd: '/w', + provider: profile(), + env: { GITHUB_TOKEN: TEST_SECRETS.githubPat }, + }; + new ClaudeDriver({ facade }).startSession(params, ctx); + await ctx.waitFor((event) => event.type === 'ready'); + + // The path was driven: the environment carried both secrets. + expect(sessions[0]?.env?.ANTHROPIC_AUTH_TOKEN).toBe(TEST_SECRETS.providerToken); + expect(sessions[0]?.env?.GITHUB_TOKEN).toBe(TEST_SECRETS.githubPat); + expectNoSecretLeaks({ logs: ctx.logs, events: ctx.events }); + }); + + it('does not repeat a spawn failure that quotes the environment', async () => { + // The SDK's error carries the CLI's stderr tail; a CLI that printed its + // environment would have the token in it. + const { facade } = fakeFacade( + Promise.reject(new Error(`spawn failed: ANTHROPIC_AUTH_TOKEN=${TEST_SECRETS.providerToken}`)), + ); + const ctx = recordingContext(); + new ClaudeDriver({ facade }).startSession( + { sessionId: 's1', agent: 'claude-code', cwd: '/w', provider: profile() }, + ctx, + ); + const ended = await ctx.ended(); + expect(ended.error).toMatch(/did not respond/); + expectNoSecretLeaks({ logs: ctx.logs, events: ctx.events }); + }); +}); + +describe('Claude MCP servers', () => { + it('reports a refused server without the secret in its setup', async () => { + const runCli = async (args: string[]): Promise => + args[1] === 'remove' + ? { code: 0, stdout: '', stderr: '' } + : // The CLI's refusal quotes the server JSON it was handed. + { code: 1, stdout: '', stderr: `Error: invalid config: ${args.at(-1)}` }; + const driver = new ClaudeDriver({ facade: fakeFacade().facade, manageMcp: true, runCli }); + const failure = await driver.mcp! + .act( + 'add', + [ + { + name: 'gh', + setup: { type: 'stdio', command: 'npx', args: ['gh-mcp'], env: { GITHUB_TOKEN: TEST_SECRETS.githubPat } }, + }, + ], + [], + ) + .then( + () => undefined, + (error: Error) => error, + ); + expect(failure?.message).toMatch(/invalid config/); + expectNoSecretLeaks({ frames: [failure?.message ?? ''] }); + }); +}); diff --git a/packages/agent-host/src/drivers/deepseek/__tests__/deepseekSecrets.test.ts b/packages/agent-host/src/drivers/deepseek/__tests__/deepseekSecrets.test.ts new file mode 100644 index 00000000..a812e602 --- /dev/null +++ b/packages/agent-host/src/drivers/deepseek/__tests__/deepseekSecrets.test.ts @@ -0,0 +1,151 @@ +/** + * The DeepSeek Harness's secret-carrying paths: the key its endpoint is + * read with, the environment a session's process carries, and an MCP server + * whose setup holds a token. + * + * The catalog read at start is the interesting failure: it is best-effort, + * logged, and its HTTP error can quote the header the key travelled in. + */ +import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import * as path from 'node:path'; +import { describe, expect, it, vi } from 'vitest'; +import { FakeAgent } from '../../../acp/__tests__/fakeAgent'; +import { recordingContext } from '../../../sdk/__tests__/context'; +import { TEST_SECRETS, expectNoSecretLeaks } from '../../../sdk/__tests__/secrets'; +import type { HttpGet, ProviderHttp } from '../../../sdk/net'; +import type { ProviderBinding } from '../../../sdk/types'; +import { DeepSeekDriver } from '../driver'; +import { DeepSeekRuntime } from '../runtime'; + +const profile = (over: Partial = {}): ProviderBinding => ({ + id: 'home', + label: 'Home router', + baseUrl: 'https://gw.example', + authToken: TEST_SECRETS.providerToken, + models: [{ id: 'm' }], + defaultModel: 'm', + ...over, +}); + +function harnessHome(): string { + const home = mkdtempSync(path.join(tmpdir(), 'dsh-secrets-')); + mkdirSync(path.join(home, 'profiles', 'acp'), { recursive: true }); + writeFileSync(path.join(home, 'profiles', 'acp', 'cordis.patch.yml'), '# the profile layer\n[]\n'); + return home; +} + +/** The option state a harness session starts with. */ +const configOptions = (): unknown[] => [ + { + id: 'model', + name: 'Model', + category: 'model', + type: 'select', + currentValue: '["deepseek-official","deepseek-v4-flash"]', + options: [{ value: '["deepseek-official","deepseek-v4-flash"]', name: 'deepseek-v4-flash' }], + }, +]; + +function withDriver( + options: { env?: NodeJS.ProcessEnv; httpGet?: HttpGet; providerHttp?: ProviderHttp; log?: (line: string) => void } = {}, +) { + const home = harnessHome(); + const agent = new FakeAgent(); + agent.options = configOptions(); + const spawns: Array<{ command: string; args: string[]; env: Record }> = []; + const logs: string[] = []; + const log = (line: string): void => { + logs.push(line); + }; + const runtime = new DeepSeekRuntime({ + home, + cacheDir: path.join(home, 'agents'), + installDsh: async () => '/fake/dsh/lib/bin.js', + spawnFn: ((command: string, args: string[], spawnOptions: { env: Record }) => { + spawns.push({ command, args, env: spawnOptions.env }); + return agent.child; + }) as never, + idleCloseMs: 0, + log: options.log ?? log, + }); + const driver = DeepSeekDriver.create({ + runtime, + home, + baseEnv: options.env ?? {}, + log: options.log ?? log, + ...(options.httpGet ? { httpGet: options.httpGet } : {}), + ...(options.providerHttp ? { providerHttp: options.providerHttp } : {}), + }); + return { driver, agent, home, spawns, logs }; +} + +describe('DeepSeek endpoint catalog', () => { + it('does not log the key when the endpoint cannot list its models', async () => { + const ready = withDriver({ + env: { DEEPSEEK_BASE_URL: 'https://relay.example', DEEPSEEK_API_KEY: TEST_SECRETS.apiKey }, + httpGet: async () => { + throw new Error(`request failed: authorization=Bearer ${TEST_SECRETS.apiKey}`); + }, + }); + await vi.waitFor(() => expect(ready.logs.some((line) => line.includes('no model list'))).toBe(true)); + expectNoSecretLeaks({ logs: ready.logs }); + }); +}); + +describe('DeepSeek provider profiles', () => { + it('checks and lists a profile without echoing its token on failure', async () => { + const deny = async () => { + throw new Error(`HTTP 500 — authorization: Bearer ${TEST_SECRETS.providerToken}`); + }; + const ready = withDriver({ providerHttp: { get: deny, post: deny } }); + expect(await ready.driver.checkProvider(profile(), 'm')).toBeUndefined(); + const failure = await ready.driver.listProviderModels('https://gw.example', TEST_SECRETS.providerToken).then( + () => undefined, + (error: Error) => error, + ); + expect(failure?.message).toMatch(/gw\.example/); + expectNoSecretLeaks({ logs: ready.logs, frames: [failure?.message ?? ''] }); + }); +}); + +describe('DeepSeek session environment', () => { + it('hands the credentials to the harness without logging or reporting them', async () => { + const ready = withDriver(); + const ctx = recordingContext(); + ready.driver.startSession( + { + sessionId: 's1', + agent: 'deepseek-harness', + cwd: '/w', + provider: profile(), + env: { GITHUB_TOKEN: TEST_SECRETS.githubPat }, + }, + ctx, + ); + await ctx.waitFor((event) => event.type === 'ready'); + + // The path was driven: the process carried both secrets. + expect(ready.spawns[0]?.env.DEEPSEEK_API_KEY).toBe(TEST_SECRETS.providerToken); + expect(ready.spawns[0]?.env.GITHUB_TOKEN).toBe(TEST_SECRETS.githubPat); + expectNoSecretLeaks({ logs: ctx.logs, events: ctx.events }); + }); +}); + +describe('DeepSeek MCP servers', () => { + it('stores a server whose env holds a token without logging or reporting it', async () => { + const ready = withDriver(); + const state = await ready.driver.mcp.act( + 'add', + [ + { + name: 'gh', + setup: { type: 'stdio', command: 'npx', args: ['gh-mcp'], env: { GITHUB_TOKEN: TEST_SECRETS.githubPat } }, + }, + ], + [], + ); + expect(state.servers).toEqual([{ name: 'gh', transport: 'stdio', target: 'npx', envKeys: ['GITHUB_TOKEN'], enabled: true }]); + expectNoSecretLeaks({ logs: ready.logs, frames: [state] }); + }); +}); diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeSecrets.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeSecrets.test.ts new file mode 100644 index 00000000..a6021750 --- /dev/null +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeSecrets.test.ts @@ -0,0 +1,295 @@ +/** + * OpenCode's secret-carrying paths, and the rule they all keep: a provider + * token, an account key or a sign-in address never reaches the host's log + * or the reply the bridge relays. + * + * The failures are the point. A spawned server that dies prints its + * environment, the server that reads its config can quote it back in an + * error, and the client's own refusal can repeat the key it was given — each + * is a place a value would otherwise escape. + */ +import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import * as path from 'node:path'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import type { OpenCodeClient } from '@opencode/client'; +import type { ProviderHttp } from '../../../sdk/net'; +import type { ProviderBinding } from '../../../sdk/types'; +import { TEST_SECRETS, expectNoSecretLeaks } from '../../../sdk/__tests__/secrets'; +import { OpenCodeAccounts } from '../accounts'; +import { OpenCodeDriver } from '../driver'; +import type { OpenCodeServerHandle, StartOpenCodeServerOptions } from '../server'; + +const profile = (over: Partial = {}): ProviderBinding => ({ + id: 'home', + label: 'Home router', + baseUrl: 'http://192.168.1.2:3458', + authToken: TEST_SECRETS.providerToken, + models: [{ id: 'kimi-k3' }], + defaultModel: 'kimi-k3', + ...over, +}); + +const handle = (port: number): OpenCodeServerHandle => ({ + url: `http://127.0.0.1:${port}`, + pid: port, + exited: new Promise(() => {}), + close: async () => {}, +}); + +/** The little of v2's client a server start and a catalog read touch. */ +function catalogClient(over: Record = {}): OpenCodeClient { + return { + event: { + subscribe: () => + (async function* () { + await new Promise(() => {}); + })(), + }, + integration: { list: async () => ({ location: { directory: '/tmp' }, data: [] }) }, + provider: { list: async () => ({ location: { directory: '/tmp' }, data: [] }) }, + model: { + list: async () => ({ location: { directory: '/tmp' }, data: [] }), + default: async () => ({ location: { directory: '/tmp' }, data: null }), + }, + config: { get: async () => [] }, + mcp: { + list: async () => ({ location: { directory: '/tmp' }, data: [] }), + add: async () => undefined, + }, + plugin: { list: async () => ({ location: { directory: '/tmp' }, data: [] }) }, + ...over, + } as unknown as OpenCodeClient; +} + +/** A real first start, so a later one is not mistaken for it. */ +let dataHome: string; +beforeEach(() => { + dataHome = mkdtempSync(path.join(tmpdir(), 'oc-secrets-')); + mkdirSync(path.join(dataHome, 'opencode')); + writeFileSync(path.join(dataHome, 'opencode', 'opencode.db'), ''); + vi.stubEnv('XDG_DATA_HOME', dataHome); +}); +afterEach(() => { + vi.unstubAllEnvs(); +}); + +async function started( + options: { + starts?: StartOpenCodeServerOptions[]; + startServer?: (options: StartOpenCodeServerOptions) => Promise; + client?: OpenCodeClient; + log?: (line: string) => void; + providerHttp?: ProviderHttp; + } = {}, +): Promise { + return OpenCodeDriver.create({ + autoStart: true, + binaryPath: process.execPath, + log: options.log ?? (() => {}), + catalogWaitMs: 0, + modelsRefreshWaitMs: 0, + startServer: + options.startServer ?? + (async (startOptions) => { + options.starts?.push(startOptions); + return handle(4100 + (options.starts?.length ?? 0)); + }), + connect: () => options.client ?? catalogClient(), + ...(options.providerHttp ? { providerHttp: options.providerHttp } : {}), + }); +} + +describe('OpenCode provider profiles', () => { + it('does not log the token when a restarted server fails to start', async () => { + const logs: string[] = []; + const starts: StartOpenCodeServerOptions[] = []; + let count = 0; + const driver = await started({ + log: (line) => logs.push(line), + startServer: async (options) => { + starts.push(options); + if (++count === 1) return handle(4100); + // The child failed and its output quotes the environment it ran in. + throw new Error(`opencode serve exited with code 1: CODEDECK_PROVIDER_KEY_0=${options.env?.CODEDECK_PROVIDER_KEY_0}`); + }, + }); + + const failure = await driver.setProviders([profile()]).then( + () => undefined, + (error: Error) => error, + ); + expect(failure?.message).toMatch(/server failed to start/); + // The path was driven: the token was in the child's environment. + expect(starts.at(-1)?.env?.CODEDECK_PROVIDER_KEY_0).toBe(TEST_SECRETS.providerToken); + expectNoSecretLeaks({ logs, frames: [failure?.message ?? ''] }); + await driver.shutdown(); + }); + + it('does not log the token when the server cannot report its catalog', async () => { + const logs: string[] = []; + const client = catalogClient({ + provider: { + list: async () => { + // The server reads its config back to callers, tokens included. + throw new Error(`could not read the config: ${TEST_SECRETS.providerToken}`); + }, + }, + }); + const driver = await started({ log: (line) => logs.push(line), client }); + await driver.setProviders([profile()]); + expectNoSecretLeaks({ logs }); + await driver.shutdown(); + }); + + it('checks and lists a profile without echoing its token on failure', async () => { + const deny = async () => { + throw new Error(`HTTP 500 — authorization: Bearer ${TEST_SECRETS.providerToken}`); + }; + const driver = await started({ providerHttp: { get: deny, post: deny } }); + expect(await driver.checkProvider(profile(), 'kimi-k3')).toBeUndefined(); + const failure = await driver.listProviderModels('http://192.168.1.2:3458', TEST_SECRETS.providerToken).then( + () => undefined, + (error: Error) => error, + ); + expect(failure?.message).toMatch(/192\.168\.1\.2/); + expectNoSecretLeaks({ frames: [failure?.message ?? ''] }); + await driver.shutdown(); + }); +}); + +describe('OpenCode accounts', () => { + const openai = { + id: 'openai', + name: 'OpenAI', + methods: [ + { type: 'key' }, + { id: 'chatgpt', type: 'oauth', label: 'Sign in with ChatGPT' }, + ], + connections: [], + }; + + it('reports a refused key without the key', async () => { + const logs: string[] = []; + const client = { + integration: { + list: async () => ({ data: [openai] }), + connect: { + key: async () => { + throw new Error(`the provider refused the key ${TEST_SECRETS.apiKey}`); + }, + }, + }, + credential: { list: async () => [] }, + } as unknown as OpenCodeClient; + const accounts = new OpenCodeAccounts(async () => client, { log: (line) => logs.push(line) }); + const failure = await accounts + .change('openai', { kind: 'key', method: 'key', key: TEST_SECRETS.apiKey }, () => {}) + .then( + () => undefined, + (error: Error) => error, + ); + expect(failure?.message).toMatch(/refused the key/); + expectNoSecretLeaks({ logs, frames: [failure?.message ?? ''] }); + }); + + it('reports a failed paste-back sign-in without the address it was given', async () => { + const callback = 'http://127.0.0.1:45493/auth/callback'; + const client = { + integration: { + list: async () => ({ data: [{ ...openai, methods: [openai.methods[0], { id: 'chatgpt', type: 'oauth', label: 'ChatGPT' }] }] }), + oauth: { + connect: async () => ({ + data: { + attemptID: 'a1', + url: `https://auth.openai.com/authorize?redirect_uri=${encodeURIComponent(callback)}&state=s`, + instructions: 'Sign in in the browser', + mode: 'auto', + time: { created: 0, expires: 10_000 }, + }, + }), + }, + }, + credential: { list: async () => [] }, + } as unknown as OpenCodeClient; + const logs: string[] = []; + const accounts = new OpenCodeAccounts(async () => client, { + log: (line) => logs.push(line), + pollMs: 60_000, + // The machine cannot open the address; the failure repeats it. + open: async (url) => { + throw new Error(`the machine could not open ${url}`); + }, + }); + await accounts.change('openai', { kind: 'sign-in', method: 'chatgpt' }, () => {}); + const pasted = `${callback}?code=${TEST_SECRETS.sessionCode}&state=s`; + const failure = await accounts + .change('openai', { kind: 'finish', attempt: 'a1', callbackUrl: pasted }, () => {}) + .then( + () => undefined, + (error: Error) => error, + ); + expect(failure?.message).toMatch(/could not open/); + expectNoSecretLeaks({ logs, frames: [failure?.message ?? ''] }); + accounts.close(); + }); +}); + +describe('OpenCode MCP servers', () => { + it('reports a refused server without the secret in its setup', async () => { + const logs: string[] = []; + const client = catalogClient({ + mcp: { + list: async () => ({ location: { directory: '/tmp' }, data: [] }), + add: async ({ config }: { config: unknown }) => { + // The server's refusal quotes the config it was handed. + throw new Error(`invalid config: ${JSON.stringify(config)}`); + }, + }, + }); + const driver = await started({ log: (line) => logs.push(line), client }); + const failure = await driver.mcp + .act( + 'add', + [ + { + name: 'gh', + setup: { + type: 'http', + url: 'https://x/mcp', + headers: { Authorization: `Bearer ${TEST_SECRETS.githubPat}` }, + }, + }, + ], + [], + ) + .then( + () => undefined, + (error: Error) => error, + ); + expect(failure?.message).toMatch(/could not add gh/); + expectNoSecretLeaks({ logs, frames: [failure?.message ?? ''] }); + await driver.shutdown(); + }); +}); + +describe('OpenCode plugin configuration', () => { + it('does not list or log an option that carries a key', async () => { + const configHome = mkdtempSync(path.join(tmpdir(), 'oc-config-')); + mkdirSync(path.join(configHome, 'opencode')); + writeFileSync( + path.join(configHome, 'opencode', 'opencode.json'), + JSON.stringify({ plugins: [{ package: 'x-plugin', options: { apiKey: TEST_SECRETS.apiKey } }] }), + ); + vi.stubEnv('XDG_CONFIG_HOME', configHome); + const logs: string[] = []; + const client = catalogClient({ + plugin: { list: async () => ({ location: { directory: '/tmp' }, data: [{ id: 'x-plugin', state: { status: 'ok' } }] }) }, + }); + const driver = await started({ log: (line) => logs.push(line), client }); + const state = await driver.plugins.list(false); + expect(state.installed.map((plugin) => plugin.id)).toEqual(['x-plugin']); + expectNoSecretLeaks({ logs, frames: [state] }); + await driver.shutdown(); + }); +}); diff --git a/packages/agent-host/src/drivers/qwen/__tests__/qwenSecrets.test.ts b/packages/agent-host/src/drivers/qwen/__tests__/qwenSecrets.test.ts new file mode 100644 index 00000000..6671ec37 --- /dev/null +++ b/packages/agent-host/src/drivers/qwen/__tests__/qwenSecrets.test.ts @@ -0,0 +1,146 @@ +/** + * Qwen Code's secret-carrying paths: the provider token its settings refer + * to and its process environment carries, and the provider API a profile is + * checked on. + * + * The token is deliberately absent from `settings.json` (the file anything + * on the machine can read); it exists only in the session's environment. + * These tests drive that path and assert neither the log nor a session event + * ever repeats it. + */ +import { mkdtempSync, readFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import * as path from 'node:path'; +import { describe, expect, it } from 'vitest'; +import { FakeAgent } from '../../../acp/__tests__/fakeAgent'; +import { recordingContext } from '../../../sdk/__tests__/context'; +import { TEST_SECRETS, expectNoSecretLeaks } from '../../../sdk/__tests__/secrets'; +import type { ProviderHttp } from '../../../sdk/net'; +import type { ProviderBinding } from '../../../sdk/types'; +import { QwenDriver } from '../driver'; +import { QwenRuntime } from '../runtime'; +import { qwenKeyEnvName } from '../settings'; + +const profile = (over: Partial = {}): ProviderBinding => ({ + id: 'home', + label: 'Home router', + baseUrl: 'https://gw.example', + authToken: TEST_SECRETS.providerToken, + models: [{ id: 'qwen3-coder', label: 'Qwen3 Coder' }], + defaultModel: 'qwen3-coder', + ...over, +}); + +/** The option state a Qwen Code session starts with. */ +const configOptions = (): unknown[] => [ + { + id: 'mode', + name: 'Mode', + category: 'mode', + type: 'select', + currentValue: 'auto', + options: [ + { value: 'default', name: 'Default' }, + { value: 'auto', name: 'Auto' }, + ], + }, + { + id: 'model', + name: 'Model', + category: 'model', + type: 'select', + currentValue: 'qwen3-coder(openai)', + options: [{ value: 'qwen3-coder(openai)', name: 'Qwen3 Coder' }], + }, +]; + +const modeState = (): unknown => ({ + currentModeId: 'auto', + availableModes: [ + { id: 'default', name: 'Default' }, + { id: 'auto', name: 'Auto' }, + ], +}); + +function withDriver(providerHttp?: ProviderHttp) { + const home = mkdtempSync(path.join(tmpdir(), 'qwen-secrets-')); + const agent = new FakeAgent(); + agent.options = configOptions(); + agent.modes = modeState(); + const spawns: Array<{ command: string; args: string[]; env: Record }> = []; + const logs: string[] = []; + const log = (line: string): void => { + logs.push(line); + }; + const runtime = new QwenRuntime({ + home, + cacheDir: path.join(home, 'agents'), + installQwen: async () => '/fake/qwen/cli-entry.js', + spawnFn: ((command: string, args: string[], spawnOptions: { env: Record }) => { + spawns.push({ command, args, env: spawnOptions.env }); + return agent.child; + }) as never, + idleCloseMs: 0, + log, + }); + const driver = QwenDriver.create({ + runtime, + home, + baseEnv: {}, + log, + ...(providerHttp ? { providerHttp } : {}), + }); + return { driver, agent, home, spawns, logs }; +} + +describe('Qwen provider settings', () => { + it('writes the profiles without the token, and logs none of it', async () => { + const ready = withDriver(); + expect(await ready.driver.setProviders([profile()])).toEqual([]); + const settings = readFileSync(path.join(ready.home, 'settings.json'), 'utf8'); + expect(settings).not.toContain(TEST_SECRETS.providerToken); + expectNoSecretLeaks({ logs: ready.logs, frames: [settings] }); + void ready.driver.shutdown(); + }); + + it('checks and lists a profile without echoing its token on failure', async () => { + const deny = async () => { + throw new Error(`HTTP 500 — authorization: Bearer ${TEST_SECRETS.providerToken}`); + }; + const ready = withDriver({ get: deny, post: deny }); + expect(await ready.driver.checkProvider(profile(), 'qwen3-coder')).toBeUndefined(); + const failure = await ready.driver.listProviderModels('https://gw.example', TEST_SECRETS.providerToken).then( + () => undefined, + (error: Error) => error, + ); + expect(failure?.message).toMatch(/gw\.example/); + expectNoSecretLeaks({ logs: ready.logs, frames: [failure?.message ?? ''] }); + void ready.driver.shutdown(); + }); +}); + +describe('Qwen session environment', () => { + it('hands the profile token to the process without logging or reporting it', async () => { + const ready = withDriver(); + await ready.driver.setProviders([profile()]); + const ctx = recordingContext(); + ready.driver.startSession( + { + sessionId: 's1', + agent: 'qwen-code', + cwd: '/w', + env: { GITHUB_TOKEN: TEST_SECRETS.githubPat }, + }, + ctx, + ); + await ctx.waitFor((event) => event.type === 'ready'); + + // The path was driven: the token reached the process under the name the + // settings entry refers to, beside the bridge-level credential. + const keyName = qwenKeyEnvName('home', 'https://gw.example/v1'); + expect(ready.spawns[0]?.env[keyName]).toBe(TEST_SECRETS.providerToken); + expect(ready.spawns[0]?.env.GITHUB_TOKEN).toBe(TEST_SECRETS.githubPat); + expectNoSecretLeaks({ logs: [...ctx.logs, ...ready.logs], events: ctx.events }); + void ready.driver.shutdown(); + }); +}); diff --git a/packages/agent-host/src/host/__tests__/secrets.test.ts b/packages/agent-host/src/host/__tests__/secrets.test.ts new file mode 100644 index 00000000..4ed097db --- /dev/null +++ b/packages/agent-host/src/host/__tests__/secrets.test.ts @@ -0,0 +1,150 @@ +/** + * The host's own request handling never relays a credential: not in the + * reply it writes for the bridge, and not in the diagnostic line it logs. + * + * The provider checks here run the real provider code over a fake HTTP seam, + * so the failure text the host receives is the same shape a real client's + * would be — one that may quote the request headers (the token included). + */ +import { describe, expect, it } from 'vitest'; +import type { HttpGet, HttpPost } from '../../sdk/net'; +import type { Driver } from '../../sdk/driver'; +import type { AgentInfo, ProviderBinding } from '../../sdk/types'; +import { OPENAI_API, checkToken, listModels } from '../../sdk/providerApi'; +import { TEST_SECRETS, expectNoSecretLeaks } from '../../sdk/__tests__/secrets'; +import { FakeDriver } from '../../drivers/fake/driver'; +import { Agents } from '../agents'; +import { AgentHost } from '../host'; + +interface Frame { + v: number; + id?: string; + kind: string; + payload?: Record; +} + +const INFO: AgentInfo = { + id: 'stub', + displayName: 'Stub', + modes: [], + efforts: [], + supports: {} as AgentInfo['supports'], + credentials: [], +}; + +const profile = (): ProviderBinding => ({ + id: 'home', + label: 'Home router', + baseUrl: 'https://gw.example', + authToken: TEST_SECRETS.providerToken, + models: [{ id: 'm' }], +}); + +/** A driver whose provider paths are the real ones, over a fake HTTP seam. */ +function providerDriver(http: { get: HttpGet; post: HttpPost }): Driver { + return { + info: () => INFO, + startSession: () => { + throw new Error('this test does not start sessions'); + }, + listModels: async () => ({ models: [] }), + checkProvider: (provider, model) => checkToken(OPENAI_API, provider, model, http.post), + listProviderModels: (baseUrl, token) => listModels(OPENAI_API, baseUrl, token, http.get), + }; +} + +function harness(driver: Driver) { + const frames: Frame[] = []; + const logs: string[] = []; + const host = new AgentHost( + Agents.of([driver]), + { write: (line) => frames.push(JSON.parse(line) as Frame), log: (line) => logs.push(line) }, + '9.9.9', + ); + let next = 0; + const send = (kind: string, payload: Record, id = `b${++next}`) => + host.handleLine(JSON.stringify({ v: 1, id, kind, payload })); + const reply = (id: string): Frame | undefined => frames.find((frame) => frame.id === id); + return { host, frames, logs, send, reply }; +} + +describe('the host relays no secret', () => { + it('a provider check whose request failed answers without the token', async () => { + const h = harness( + providerDriver({ + post: async () => { + throw new Error(`HTTP 500 — authorization: Bearer ${TEST_SECRETS.providerToken}`); + }, + get: async () => { + throw new Error('not used'); + }, + }), + ); + await h.send('check-provider', { agent: 'stub', provider: profile(), model: 'm' }, 'c'); + expect(h.reply('c')).toMatchObject({ kind: 'credential-checked', payload: {} }); + expectNoSecretLeaks({ logs: h.logs, frames: h.frames }); + }); + + it('a model list that failed does not echo the token in its error', async () => { + const h = harness( + providerDriver({ + post: async () => ({ status: 200 }), + get: async (_url, headers) => { + // A client whose failure quotes the request it was given. + throw new Error(`request failed: authorization=${headers.authorization}`); + }, + }), + ); + await h.send( + 'list-provider-models', + { agent: 'stub', baseUrl: 'https://gw.example', authToken: TEST_SECRETS.providerToken }, + 'l', + ); + expect(h.reply('l')).toMatchObject({ kind: 'error' }); + expectNoSecretLeaks({ logs: h.logs, frames: h.frames }); + }); + + it('an MCP server added with a header secret is reported without it', async () => { + const h = harness(new FakeDriver()); + await h.send( + 'mcp-action', + { + agent: 'fake', + action: 'add', + servers: [ + { + name: 'gh', + setup: { type: 'http', url: 'https://x/mcp', headers: { Authorization: `Bearer ${TEST_SECRETS.githubPat}` } }, + }, + ], + }, + 'm', + ); + expect(h.reply('m')).toMatchObject({ + kind: 'mcp-servers', + payload: { servers: [{ name: 'gh', headerKeys: ['Authorization'] }] }, + }); + expectNoSecretLeaks({ logs: h.logs, frames: h.frames }); + }); + + it('a session that fails to start does not carry its credentials in the error', async () => { + // The host receives whatever a driver throws; a driver that quotes an + // environment variable in its refusal must still not put the value on + // the wire. (The drivers themselves redact before throwing — see each + // driver's secrets test — so this asserts the host does not add it back.) + const h = harness(new FakeDriver()); + await h.send( + 'start-session', + { + sessionId: 's1', + agent: 'fake', + cwd: '', + credentials: { fake_token: TEST_SECRETS.apiKey }, + env: { GITHUB_TOKEN: TEST_SECRETS.githubPat }, + }, + 'st', + ); + expect(h.reply('st')).toMatchObject({ kind: 'error', payload: { message: 'the fake agent needs a working directory' } }); + expectNoSecretLeaks({ logs: h.logs, frames: h.frames }); + }); +}); diff --git a/packages/agent-host/src/sdk/__tests__/secrets.test.ts b/packages/agent-host/src/sdk/__tests__/secrets.test.ts new file mode 100644 index 00000000..9a465d22 --- /dev/null +++ b/packages/agent-host/src/sdk/__tests__/secrets.test.ts @@ -0,0 +1,40 @@ +/** + * The secret guard itself: a clean line passes, and every way a credential + * commonly escapes — whole, truncated, in a Bearer header, base64-encoded — + * is caught with a message that names what leaked and where. + */ +import { describe, expect, it } from 'vitest'; +import { LEAK_WINDOW, TEST_SECRETS, expectNoSecretLeaks, findSecretLeak } from './secrets'; + +const token = TEST_SECRETS.providerToken; + +describe('the secret guard', () => { + it('passes an ordinary diagnostic line', () => { + expect(() => + expectNoSecretLeaks({ + logs: ['[agent-host] dropped frame: malformed frame: no kind'], + frames: [{ kind: 'error', payload: { message: 'the provider refused the token (HTTP 401)' } }], + }), + ).not.toThrow(); + }); + + it('catches the whole value, a truncated value, a Bearer header and base64', () => { + expect(findSecretLeak(`token=${token}`)).toMatch(/the value/); + expect(findSecretLeak(`token=${token.slice(0, LEAK_WINDOW)}`)).toMatch(/12-character part/); + expect(findSecretLeak(`authorization: Bearer ${token}`)).toMatch(/Bearer/); + expect(findSecretLeak(`encoded ${Buffer.from(token).toString('base64')}`)).toMatch(/base64/); + }); + + it('names the leaking log line and the secret', () => { + expect(() => + expectNoSecretLeaks({ logs: ['clean', `provider failed: ${token}`] }), + ).toThrow(/log line 2 contains the value of/); + }); + + it('checks frames and session events, not only logs', () => { + expect(() => expectNoSecretLeaks({ frames: [{ message: `Bearer ${token}` }] })).toThrow(/wire frame 1/); + expect(() => expectNoSecretLeaks({ events: [{ type: 'ended', error: `key ${TEST_SECRETS.apiKey}` }] })).toThrow( + /session event 1/, + ); + }); +}); diff --git a/packages/agent-host/src/sdk/__tests__/secrets.ts b/packages/agent-host/src/sdk/__tests__/secrets.ts new file mode 100644 index 00000000..6cd69ae9 --- /dev/null +++ b/packages/agent-host/src/sdk/__tests__/secrets.ts @@ -0,0 +1,112 @@ +/** + * The one assertion every secret-carrying path is tested with: no value of a + * credential may reach a diagnostic log line or a frame the host relays to + * the bridge. + * + * A leak is rarely the whole token. An error that quotes a request may carry + * the token inside a header (`Bearer …`, `x-api-key: …`), an encoder may + * transform it, and a message that truncates it still exposes enough to use. + * The values here are long and distinctive, so a random 12-character window + * of one cannot occur in an ordinary sentence — checking every window catches + * a partial leak as surely as the whole value, and the common encodings catch + * the same value in a form the raw check would miss. + */ + +/** How much of a secret is enough to count as a leak. */ +export const LEAK_WINDOW = 12; + +/** + * Distinctive credentials, one per kind a host process handles. They are + * long enough for the window check and unrelated to each other, so a line + * that leaks one is never mistaken for a line that merely names another. + */ +export const TEST_SECRETS = { + /** A provider profile's API token. */ + providerToken: 'sk-cdx-7Qm2Lp9Rt4Vb8Nz1Kx5Jr3Hd', + /** A stored API key (Anthropic's, DeepSeek's). */ + apiKey: 'ant-cdx-3Vn8Kq2Ws6Ye1Pu4Tz9Mo7Lb', + /** A GitHub personal access token handed to a session's environment. */ + githubPat: 'ghp_cdx9Rt4Vb8Nz1Kx5Jr3Hd7Qm', + /** The secret half of an OAuth paste-back address. */ + sessionCode: 'cdx-code-6Ye1Pu4Tz9Mo7Lb3Vn8Kq2Ws', +} as const; + +/** One way a secret can appear in text. */ +interface Encoding { + form: string; + text: string; +} + +/** The forms a secret is looked for in, beyond its raw characters. */ +function encodingsOf(secret: string): Encoding[] { + const bytes = Buffer.from(secret, 'utf8'); + const base64 = bytes.toString('base64'); + const base64url = bytes.toString('base64url'); + // Longer, more specific forms first: a Bearer value contains the raw + // secret, so it must be named as the header it leaked in, not as the + // value alone. + return [ + { form: 'base64', text: base64 }, + // Some encoders omit the padding. + { form: 'base64 without padding', text: base64.replace(/=+$/, '') }, + { form: 'base64url', text: base64url }, + { form: 'a Bearer header value', text: `Bearer ${secret}` }, + { form: 'the value', text: secret }, + ]; +} + +/** + * What `text` leaks of `secrets`, in words for the failure message; undefined + * when it carries none of them. + */ +export function findSecretLeak(text: string, secrets: readonly string[] = Object.values(TEST_SECRETS)): string | undefined { + for (const secret of secrets) { + for (const encoding of encodingsOf(secret)) { + if (text.includes(encoding.text)) return `${encoding.form} of '${secret}'`; + } + for (let at = 0; at + LEAK_WINDOW <= secret.length; at++) { + const part = secret.slice(at, at + LEAK_WINDOW); + if (text.includes(part)) return `a ${LEAK_WINDOW}-character part of '${secret}' ('${part}')`; + } + } + return undefined; +} + +/** Everything a test captured from one path. */ +export interface CapturedTraffic { + /** Diagnostic lines the host or a driver logged. */ + logs?: readonly string[]; + /** Frames written to the bridge, or values a driver returned. */ + frames?: readonly unknown[]; + /** Events a session reported; these reach the phone too. */ + events?: readonly unknown[]; +} + +function asText(value: unknown): string { + if (typeof value === 'string') return value; + try { + return JSON.stringify(value) ?? String(value); + } catch { + return String(value); + } +} + +/** + * Fail — naming the exact log line, frame or event — when any captured text + * carries a secret in any of the forms above. + */ +export function expectNoSecretLeaks( + traffic: CapturedTraffic, + secrets: readonly string[] = Object.values(TEST_SECRETS), +): void { + const leaks: string[] = []; + const check = (label: string, value: unknown): void => { + const text = asText(value); + const leak = findSecretLeak(text, secrets); + if (leak) leaks.push(`${label} contains ${leak}: ${JSON.stringify(text)}`); + }; + (traffic.logs ?? []).forEach((line, index) => check(`log line ${index + 1}`, line)); + (traffic.frames ?? []).forEach((frame, index) => check(`wire frame ${index + 1}`, frame)); + (traffic.events ?? []).forEach((event, index) => check(`session event ${index + 1}`, event)); + if (leaks.length > 0) throw new Error(`secret leak:\n${leaks.join('\n')}`); +} From 2fc800ecfbb7b64c386c674414260b885499f76f Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:48:02 +0100 Subject: [PATCH 09/42] fix(acp): close a session only when the agent advertises session/close Qwen Code's ACP surface advertises `list` and `resume` but not `close`: a `session/close` request is answered -32601 "Method not found". The shared session base sent one anyway when a session ended, and again for a `session/new` the start had given up on, because the fake had Qwen Code advertising the method and answering it. The DeepSeek Harness does serve it, so only the phone's Qwen Code sessions were affected. Gate both calls on the process's advertised sessionCapabilities, the way `session/resume` already is. A session that does not serve close is over when the driver lets it go; a late-created one is left to the agent's own state rather than sent a method it does not have. Ending a session now also waits for the replies to its pending permission asks to be written before releasing the connection. The in-flight close request used to give those writes a turn; without it an ask cancelled as the session ends could be dropped instead of answered. The ACP fake takes a per-agent profile (`deepseek` or `qwen`) built from a probe of the real agents, so the Qwen Code tests drive the surface it really has. Co-Authored-By: Claude Code --- .../agent-host/src/acp/__tests__/fakeAgent.ts | 476 +++++++++++++++--- packages/agent-host/src/acp/acp.ts | 33 ++ packages/agent-host/src/acp/session.ts | 30 +- .../drivers/qwen/__tests__/qwenDriver.test.ts | 32 +- .../qwen/__tests__/qwenSecrets.test.ts | 2 +- 5 files changed, 491 insertions(+), 82 deletions(-) diff --git a/packages/agent-host/src/acp/__tests__/fakeAgent.ts b/packages/agent-host/src/acp/__tests__/fakeAgent.ts index 738ba357..6e552913 100644 --- a/packages/agent-host/src/acp/__tests__/fakeAgent.ts +++ b/packages/agent-host/src/acp/__tests__/fakeAgent.ts @@ -3,16 +3,51 @@ * requests a session makes, in the shapes a real agent sends, so a driver * can be tested without the CLI, a credential or a network. * - * The shapes are ACP's own: a session's option state, `tool_call` updates - * carrying the tool name as their title, permission asks that name only the - * tool call, and stop reasons rather than a synthetic end. A test sets - * `options` to the option state its agent composes, and `onPrompt` to script - * one turn — sending updates and asking for a permission — while everything - * else is answered the way an agent would. + * Two agents sit behind this fake, and they do not behave alike. `deepseek` + * reproduces the DeepSeek Harness's `dsh --profile acp` (pinned 0.2.1-rc.2), + * `qwen` reproduces Qwen Code's `qwen --acp` (pinned 0.26.1). A test says + * which one it drives; everything a driver can observe — the initialize + * capabilities, how a session is created, resumed, listed and closed, where a + * tool call's name travels, what a refused config option says, the permission + * ask and the end of a turn — comes from that profile. + * + * Verified against dsh 0.2.1-rc.2 / qwen-code 0.26.1 on 2026-10-27; re-probe + * on a bump. The probes (raw ACP over the agent's stdio, throwaway homes and + * the local fake model for the turns) answered, per agent: + * - initialize: agentInfo, prompt/mcp/session capabilities, `loadSession` + * and `authMethods`; only the harness advertises `session/close`, only + * Qwen Code advertises `loadSession`; both advertise `list` and `resume`. + * - session/new, session/resume (active and after close), session/list (the + * harness lists a conversation only once it is closed; Qwen Code lists it + * while live and needs `cwd`), session/load (Qwen Code has it, the harness + * answers method-not-found) and session/close (Qwen Code answers + * method-not-found; the harness closes once and calls a second close of + * the same id unknown). + * - a `/tool` turn: the harness's `tool_call` names the tool as `title` + * (`bash`, `kind: "other"`) with its arguments; Qwen Code's first + * `tool_call` carries `_meta.toolName` and no arguments, and the + * arguments arrive in a following `tool_call_update`. Neither sends `name`. + * - session/set_config_option with an offered and an unoffered model and + * effort, and with an unknown config id: both refuse an unoffered value + * with -32602 and their own message. + * - session/set_mode: offered, unoffered, and on the harness (no modes, so + * method-not-found). + * - session/request_permission: the harness names only the tool call's id + * (it asks when a model requests a wider sandbox, which is why the probe + * also read `@deepseek-ai/dsh-acp`'s ask); Qwen Code repeats the call's + * facts and offers allow-always choices beside Allow and Reject. + * - a turn ends with a `stopReason`; Qwen Code adds its branch point in + * `_meta`. + * + * A test still sets `options` to the option state its agent composes and + * `onPrompt` to script one turn — sending updates and asking for a + * permission — while everything else is answered the way that agent does. */ import { EventEmitter } from 'node:events'; import { PassThrough } from 'node:stream'; -import type { SpawnFn } from '../runtime'; + +/** Which real agent a `FakeAgent` stands in for. */ +export type AcpProfile = 'deepseek' | 'qwen'; /** Minimal fake ChildProcess: the streams the driver talks over, plus the * exit and kill behaviour the runtime's close ladder needs. */ @@ -49,6 +84,251 @@ interface Sent { error?: unknown; } +/** One conversation the fake created or resumed. */ +interface SessionRecord { + sessionId: string; + cwd: string; + closed: boolean; + /** The conversation's opening line, which Qwen Code shows as its title. */ + title: string; + /** How many turns ran in it (Qwen Code turns them into branch points). */ + turns: number; +} + +/** What differs between the two real agents, verified by the probes above. */ +interface Profile { + agentInfo: unknown; + authMethods: unknown[]; + /** `agentCapabilities` exactly as `initialize` answers it. */ + agentCapabilities: Record; + /** Whether `session/close` is served at all. */ + closesSessions: boolean; + /** Whether `session/set_mode` is served at all. */ + hasModes: boolean; + /** How `session/list` reads: the harness lists persisted conversations + * only (a live one is not there yet), Qwen Code lists the live ones for + * the `cwd` it is given and needs that `cwd`. */ + list: 'persisted' | 'by-cwd'; + /** The tool name a `tool_call` update carries, and its shape. */ + toolCall(sessionId: string, toolCallId: string, name: string, input: Record): unknown[]; + /** The permission ask an agent sends for one tool call. */ + permissionAsk(toolCallId: string, facts: ToolCallFacts | undefined, options: unknown[] | undefined): Record; + /** The refusal of a `session/set_config_option` the agent did not offer. */ + configRefusal(configId: string, value: string, options: unknown[]): string; + /** The refusal of a config id the agent does not have. */ + unknownConfigRefusal(configId: string): string; + /** What a settled `session/prompt` answers beyond its stop reason. */ + promptResult(session: SessionRecord): Record; +} + +/** What a tool call's own update said, for a later permission ask. */ +interface ToolCallFacts { + name: string; + input: Record; +} + +/** The method-not-found error an ACP agent answers with (the SDK's own + * wording, quotes included). */ +function methodNotFound(method: string): { code: number; message: string; data: { method: string } } { + return { code: -32601, message: `"Method not found": ${method}`, data: { method } }; +} + +/** A select option's values, flat or grouped (the harness groups its models + * by provider). */ +function offeredValues(options: unknown[]): string[] { + const values: string[] = []; + for (const entry of options) { + const record = asRecord(entry); + if (Array.isArray(record.options)) values.push(...offeredValues(record.options)); + else if (typeof record.value === 'string') values.push(record.value); + } + return values; +} + +/** The model id the harness's selector value encodes (`[provider, model]`). */ +function harnessModelId(currentValue: string): string { + try { + const parsed: unknown = JSON.parse(currentValue); + if (Array.isArray(parsed) && parsed.length === 2 && typeof parsed[0] === 'string' && typeof parsed[1] === 'string') { + return `${parsed[0]}/${parsed[1]}`; + } + } catch { + // Not a selector: the value is already what it looks like. + } + return currentValue; +} + +/** The display title Qwen Code gives a tool: a friendly word, never the + * tool's own name (which travels in `_meta.toolName`). */ +function qwenTitle(name: string): string { + switch (name) { + case 'run_shell_command': + return 'Shell'; + case 'edit': + case 'write_file': + return 'Edit'; + default: + return name; + } +} + +const DEEPSEEK: Profile = { + agentInfo: { name: 'deepseek-harness-acp', version: '0.0.1' }, + authMethods: [], + agentCapabilities: { + mcpCapabilities: { http: true }, + promptCapabilities: { image: false, audio: false, embeddedContext: false }, + sessionCapabilities: { close: {}, list: {}, resume: {} }, + }, + closesSessions: true, + hasModes: false, + list: 'persisted', + toolCall(_sessionId, toolCallId, name, input) { + // The harness sends the standard lifecycle with the tool's own name as + // its title, `kind: "other"`, and the arguments already in the start. + return [{ sessionUpdate: 'tool_call', toolCallId, title: name, kind: 'other', status: 'in_progress', rawInput: input }]; + }, + permissionAsk(toolCallId, _facts, options) { + // It names only the call; the driver reads the tool from the call's own + // earlier update. + return { + toolCall: { toolCallId }, + options: + options ?? + [ + { optionId: 'allow-once', name: 'Allow once', kind: 'allow_once' }, + { optionId: 'reject-once', name: 'Reject', kind: 'reject_once' }, + ], + }; + }, + configRefusal(configId, value, options) { + if (configId === 'model') return `Invalid params: unknown model option: ${value}`; + const current = asRecord(options.find((entry) => asRecord(entry).id === configId)).currentValue; + return `Invalid params: unknown reasoning effort for ${harnessModelId(typeof current === 'string' ? current : '')}: ${value}`; + }, + unknownConfigRefusal(configId) { + return `Invalid params: unknown session config option: ${configId}`; + }, + promptResult() { + return {}; + }, +}; + +const QWEN: Profile = { + agentInfo: { name: 'qwen-code', title: 'Qwen Code', version: '0.26.1' }, + authMethods: [ + { + id: 'openai', + name: 'Use OpenAI API key', + description: 'Requires setting the `OPENAI_API_KEY` environment variable', + _meta: { type: 'terminal', args: ['--auth-type=openai'] }, + }, + ], + agentCapabilities: { + loadSession: true, + promptCapabilities: { image: true, audio: true, embeddedContext: true }, + sessionCapabilities: { list: {}, resume: {} }, + mcpCapabilities: { sse: true, http: true }, + _meta: { imageCapability: { autoHandlesWrongModel: true, maxBytes: 10_380_902, maxImagesPerTurn: 4 } }, + }, + closesSessions: false, + hasModes: true, + list: 'by-cwd', + toolCall(_sessionId, toolCallId, name, input) { + const meta = { toolName: name, provenance: 'builtin' }; + // The first update says what is about to run and carries no arguments; + // the arguments arrive in the update that starts it running. + const updates: unknown[] = [ + { + sessionUpdate: 'tool_call', + toolCallId, + status: 'pending', + title: qwenTitle(name), + content: [], + locations: [], + kind: qwenKind(name), + rawInput: {}, + _meta: { ...meta, phase: 'preparing' }, + }, + ]; + if (Object.keys(input).length > 0) { + updates.push({ + sessionUpdate: 'tool_call_update', + toolCallId, + status: 'in_progress', + title: qwenTitle(name), + content: [], + locations: [], + kind: qwenKind(name), + rawInput: input, + _meta: { ...meta, startedAt: Date.now() }, + }); + } + return updates; + }, + permissionAsk(toolCallId, facts, options) { + const name = facts?.name ?? 'run_shell_command'; + return { + toolCall: { + toolCallId, + status: 'pending', + title: qwenTitle(name), + content: [], + locations: [], + kind: qwenKind(name), + rawInput: facts?.input ?? {}, + _meta: { toolName: name }, + }, + options: + options ?? + [ + { optionId: 'proceed_always_project', name: 'Always Allow in project', kind: 'allow_always' }, + { optionId: 'proceed_always_user', name: 'Always Allow for user', kind: 'allow_always' }, + { optionId: 'proceed_once', name: 'Allow', kind: 'allow_once' }, + { optionId: 'cancel', name: 'Reject', kind: 'reject_once' }, + ], + }; + }, + configRefusal(configId, value, options) { + if (configId === 'model') return `Invalid params: Model '${value}' not found for authType 'openai'`; + const offered = offeredValues(options); + // The message puts the provider default first, then the option list. + const choices = ['default', ...offered.filter((entry) => entry !== 'default')].join(', '); + return `Invalid params: Unknown reasoning effort: ${value}. Choose one of: ${choices}`; + }, + unknownConfigRefusal(configId) { + return `Invalid params: Unsupported configId: ${configId}`; + }, + promptResult(session) { + return { + _meta: { + 'qwen.branchPoint': { + assistantRecordUuid: `${session.sessionId}-assistant-${session.turns}`, + checkpointUuid: `${session.sessionId}-checkpoint-${session.turns}`, + }, + }, + }; + }, +}; + +function qwenKind(name: string): string { + switch (name) { + case 'run_shell_command': + return 'execute'; + case 'edit': + case 'write_file': + return 'edit'; + default: + return 'other'; + } +} + +function asRecord(value: unknown): Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) ? (value as Record) : {}; +} + +const PROFILES: Record = { deepseek: DEEPSEEK, qwen: QWEN }; + export class FakeAgent { readonly child = new FakeChild(); /** Requests the driver sent, in order. */ @@ -67,7 +347,7 @@ export class FakeAgent { /** The mode state those answers carry, for an agent that has modes. */ modes: unknown = null; /** What `initialize` answers as the agent's own name and version. */ - agentInfo: unknown = { name: 'fake-agent', version: '0.0.1' }; + agentInfo: unknown; /** Make `session/resume` fail the way a missing conversation does. */ resumeError: string | undefined; /** Whether the agent accepts the values `set_config_option` is given. */ @@ -83,18 +363,24 @@ export class FakeAgent { * times out and only then creates its session. */ holdNextNewSession = false; private heldNew?: { id: number | string; params: Record }; - /** Whether the agent answers `session/close` at all — a wedged end. */ + /** Whether the agent answers `session/close` at all — a wedged end. Only + * an agent that advertises the method is asked. */ ignoreClose = false; /** Whether the agent ignores `session/cancel` — a wedged turn. */ ignoresCancel = false; /** Scripts one turn; the default completes at once. */ onPrompt: ((sessionId: string, text: string, agent: FakeAgent) => Promise | string) | undefined; + private readonly profile: Profile; + private readonly sessions: SessionRecord[] = []; + private readonly callFacts = new Map(); private nextId = 1; private readonly pending = new Map void>(); private readonly inbound: Sent[] = []; private buffer = ''; - constructor(readonly spawnFn: SpawnFn = (() => this.child) as unknown as SpawnFn) { + constructor(readonly acpProfile: AcpProfile = 'deepseek') { + this.profile = PROFILES[acpProfile]; + this.agentInfo = this.profile.agentInfo; this.child.stdin.on('data', (chunk: Buffer) => this.receive(chunk.toString())); // Nothing reads the agent's own log here; keep the stream drained. this.child.stderr.resume(); @@ -140,15 +426,13 @@ export class FakeAgent { protocolVersion: 1, agentInfo: this.agentInfo, agentCapabilities: { - mcpCapabilities: { http: true }, - promptCapabilities: { image: false, audio: false, embeddedContext: false }, + ...this.profile.agentCapabilities, sessionCapabilities: { - close: {}, - list: {}, - ...(this.supportsResume === true ? { resume: {} } : this.supportsResume === 'null' ? { resume: null } : {}), + ...(asRecord(this.profile.agentCapabilities.sessionCapabilities) ?? {}), + ...(this.supportsResume === true ? { resume: {} } : this.supportsResume === 'null' ? { resume: null } : { resume: undefined }), }, }, - authMethods: [], + authMethods: this.profile.authMethods, }); return; case 'session/new': { @@ -164,7 +448,8 @@ export class FakeAgent { } this.newSessions.push(params); const sessionId = `s${this.newSessions.length}`; - this.reply(id, { sessionId, configOptions: this.options, modes: this.modes }); + this.sessions.push({ sessionId, cwd: String(params.cwd ?? ''), closed: false, title: '(session)', turns: 0 }); + this.reply(id, this.sessionState(sessionId)); return; } case 'session/resume': { @@ -178,10 +463,49 @@ export class FakeAgent { this.errorReply(id, -32601, 'Method not found: session/resume'); return; } - this.reply(id, { configOptions: this.options, modes: this.modes }); + const sessionId = String(params.sessionId); + const record = this.sessions.find((entry) => entry.sessionId === sessionId); + if (record === undefined) { + this.sessions.push({ sessionId, cwd: String(params.cwd ?? ''), closed: false, title: '(session)', turns: 0 }); + } else { + record.closed = false; + } + this.reply(id, this.sessionState(sessionId, false)); + return; + } + case 'session/list': { + const cwd = typeof params.cwd === 'string' ? params.cwd : undefined; + // The harness lists a conversation only once it has been closed (and + // flushed); Qwen Code lists the ones it has while they are live, for + // the cwd it is asked about. + const listed = + this.profile.list === 'persisted' + ? this.sessions.filter((entry) => entry.closed) + : cwd === undefined + ? [] + : this.sessions.filter((entry) => entry.cwd === cwd); + this.reply(id, { + sessions: listed.map((entry) => ({ sessionId: entry.sessionId, cwd: entry.cwd, title: entry.title })), + }); + return; + } + case 'session/load': { + // The harness has no load; Qwen Code has it but this fake does not + // model a conversation transcript, so it answers like a resume. + if (this.acpProfile === 'deepseek') { + const error = methodNotFound('session/load'); + this.rawErrorReply(id, error); + return; + } + this.reply(id, this.sessionState(String(params.sessionId), false)); return; } case 'session/set_mode': { + if (!this.profile.hasModes) { + const error = methodNotFound('session/set_mode'); + this.rawErrorReply(id, error); + return; + } const sessionId = String(params.sessionId); const modeId = String(params.modeId); this.setModes.push({ sessionId, modeId }); @@ -199,21 +523,18 @@ export class FakeAgent { const configId = String(params.configId); const value = String(params.value); this.setOptions.push({ sessionId: String(params.sessionId), configId, value }); - // The agent answers a change with the complete, updated option - // state — including a current value the change did not accept. const option = this.options.find( (entry) => typeof entry === 'object' && entry !== null && (entry as { id?: string }).id === configId, - ) as { options?: Array<{ value?: string; options?: Array<{ value?: string }> }> } | undefined; - // A select option's values may be offered flat or grouped (an agent - // groups its models by provider). - const offered = - option?.options?.some((entry) => - Array.isArray(entry.options) - ? entry.options.some((inner) => inner.value === value) - : entry.value === value, - ) === true; + ); + if (option === undefined) { + this.errorReply(id, -32602, this.profile.unknownConfigRefusal(configId)); + return; + } + // The agent answers a change with the complete, updated option + // state — including a current value the change did not accept. + const offered = offeredValues([option]).includes(value); if (!this.acceptsOptions || !offered) { - this.errorReply(id, -32602, `Invalid params: unknown ${configId} option: ${value}`); + this.errorReply(id, -32602, this.profile.configRefusal(configId, value, this.options)); return; } this.options = this.options.map((entry) => (entry === option ? { ...(entry as object), currentValue: value } : entry)); @@ -221,8 +542,22 @@ export class FakeAgent { return; } case 'session/close': { + if (!this.profile.closesSessions) { + const error = methodNotFound('session/close'); + this.rawErrorReply(id, error); + return; + } if (this.ignoreClose) return; - this.closed.push(String(params.sessionId)); + const sessionId = String(params.sessionId); + const record = this.sessions.find((entry) => entry.sessionId === sessionId); + if (record === undefined || record.closed) { + // A second close of the same id is unknown: the harness removes the + // record as it closes. + this.errorReply(id, -32602, `Invalid params: unknown session: ${sessionId}`); + return; + } + record.closed = true; + this.closed.push(sessionId); this.reply(id, {}); return; } @@ -242,7 +577,12 @@ export class FakeAgent { // A scripted turn may have been completed by a cancel already. if (this.turnIds.get(sessionId) === id) { this.turnIds.delete(sessionId); - this.reply(id, { stopReason: stop }); + const record = this.sessions.find((entry) => entry.sessionId === sessionId); + if (record !== undefined) { + record.turns += 1; + if (record.title === '(session)' && text.trim() !== '') record.title = text.trim(); + } + this.reply(id, { stopReason: stop, ...(record ? this.profile.promptResult(record) : {}) }); } return; } @@ -251,6 +591,34 @@ export class FakeAgent { } } + /** What `session/new`, `session/resume` and `session/load` answer with. + * `withId` is false for the two that continue a conversation the client + * named (ACP does not repeat its id). */ + private sessionState(sessionId: string, withId = true): Record { + const record = this.sessions.find((entry) => entry.sessionId === sessionId); + return { + ...(withId ? { sessionId } : {}), + configOptions: this.options, + ...(this.modes !== null ? { modes: this.modes } : {}), + // Qwen Code also summarises the model beside the config options. + ...(record !== undefined && this.profile.list === 'by-cwd' ? { models: this.qwenModels() } : {}), + }; + } + + private qwenModels(): Record { + const option = asRecord(this.options.find((entry) => asRecord(entry).id === 'model')); + const entries = Array.isArray(option.options) ? option.options : []; + return { + currentModelId: typeof option.currentValue === 'string' ? option.currentValue : '', + availableModels: entries.flatMap((entry) => { + const model = asRecord(entry); + return typeof model.value === 'string' + ? [{ modelId: model.value, name: typeof model.name === 'string' ? model.name : model.value, description: null }] + : []; + }), + }; + } + private readonly turnIds = new Map(); /** Answer the `session/new` held by `holdNextNewSession`, as an agent that @@ -260,7 +628,9 @@ export class FakeAgent { if (!held) return; this.heldNew = undefined; this.newSessions.push(held.params); - this.reply(held.id, { sessionId: `s${this.newSessions.length}`, configOptions: this.options, modes: this.modes }); + const sessionId = `s${this.newSessions.length}`; + this.sessions.push({ sessionId, cwd: String(held.params.cwd ?? ''), closed: false, title: '(session)', turns: 0 }); + this.reply(held.id, this.sessionState(sessionId)); } /** Finish the turn running in `sessionId`, as an agent does for a cancelled @@ -293,8 +663,14 @@ export class FakeAgent { this.update(sessionId, { sessionUpdate: 'agent_thought_chunk', content: { type: 'text', text } }); } - toolCall(sessionId: string, toolCallId: string, name: string, rawInput: unknown = {}): void { - this.update(sessionId, { sessionUpdate: 'tool_call', toolCallId, title: name, kind: 'other', status: 'in_progress', rawInput }); + /** One started tool call, in this agent's shape: the harness names the tool + * as the update's title; Qwen Code keeps it in `_meta.toolName` and sends + * the arguments in a second update. */ + toolCall(sessionId: string, toolCallId: string, name: string, rawInput: Record = {}): void { + this.callFacts.set(toolCallId, { name, input: rawInput }); + for (const update of this.profile.toolCall(sessionId, toolCallId, name, rawInput)) { + this.update(sessionId, update); + } } toolResult(sessionId: string, toolCallId: string, text: string, failed = false): void { @@ -317,29 +693,9 @@ export class FakeAgent { toolCallId: string, options?: unknown[], ): Promise<{ outcome: string; optionId?: string } | undefined> { - const id = 9000 + this.nextId++; - const message = new Promise((resolve) => this.pending.set(id, resolve)); - this.child.stdout.write( - `${JSON.stringify({ - jsonrpc: '2.0', - id, - method: 'session/request_permission', - params: { - sessionId, - toolCall: { toolCallId }, - options: - options ?? - [ - { optionId: 'allow-once', name: 'Allow once', kind: 'allow_once' }, - { optionId: 'reject-once', name: 'Reject', kind: 'reject_once' }, - ], - }, - })}\n`, - ); - return message.then((answer) => { - const outcome = ( - answer.result as { outcome?: { outcome?: string; optionId?: string } } | undefined - )?.outcome; + const ask = this.profile.permissionAsk(toolCallId, this.callFacts.get(toolCallId), options); + return this.requestPermission({ sessionId, ...ask }).then((result) => { + const outcome = (result as { outcome?: { outcome?: string; optionId?: string } } | undefined)?.outcome; return outcome === undefined ? undefined : { outcome: outcome.outcome ?? '', ...(outcome.optionId ? { optionId: outcome.optionId } : {}) }; }); } @@ -376,4 +732,8 @@ export class FakeAgent { private errorReply(id: number | string, code: number, message: string): void { this.child.stdout.write(`${JSON.stringify({ jsonrpc: '2.0', id, error: { code, message } })}\n`); } + + private rawErrorReply(id: number | string, error: { code: number; message: string; data: unknown }): void { + this.child.stdout.write(`${JSON.stringify({ jsonrpc: '2.0', id, error })}\n`); + } } diff --git a/packages/agent-host/src/acp/acp.ts b/packages/agent-host/src/acp/acp.ts index 348cb391..64c0cf2a 100644 --- a/packages/agent-host/src/acp/acp.ts +++ b/packages/agent-host/src/acp/acp.ts @@ -84,6 +84,11 @@ export class AcpClient { private readonly notifications = new Map void>(); private readonly requests = new Map Promise>(); private unknownRequest: ((method: string, params: unknown) => Promise | undefined) | undefined; + /** Agent requests being handled, and who waits for the last answer to be + * written: a session ending settles its pending asks and must not let the + * connection drop before their replies are on the wire. */ + private inFlight = 0; + private readonly idleWaiters: Array<() => void> = []; private nextId = 1; private buffer = ''; /** A character whose bytes straddle two chunks waits in here, so a @@ -118,6 +123,23 @@ export class AcpClient { return this.ending === undefined; } + /** + * Settles once every request the agent has sent so far has been answered + * (or the connection has closed). A session ending waits on this after + * settling its pending asks: the reply to an ask is written a few microtasks + * after the decision, and tearing the connection down first would leave the + * agent with a dropped request instead of the cancellation the session + * decided. + */ + whenIdle(): Promise { + if (this.ending !== undefined || this.inFlight === 0) return Promise.resolve(); + return new Promise((resolve) => this.idleWaiters.push(resolve)); + } + + private releaseIdle(): void { + for (const resolve of this.idleWaiters.splice(0)) resolve(); + } + /** Send one request and wait for its reply. */ request( method: M, @@ -187,6 +209,7 @@ export class AcpClient { pending.reject(reason); } this.pending.clear(); + this.releaseIdle(); this.settleDone(closed); } @@ -264,6 +287,16 @@ export class AcpClient { /** Answer one agent request. A handler that throws becomes a JSON-RPC * error, so the agent is never left waiting on a reply. */ private async answer(id: WireId, method: string, params: unknown): Promise { + this.inFlight++; + try { + await this.answerNow(id, method, params); + } finally { + this.inFlight--; + if (this.inFlight === 0) this.releaseIdle(); + } + } + + private async answerNow(id: WireId, method: string, params: unknown): Promise { const handler = this.requests.get(method); if (!handler) { // An agent's own extension method: the driver's fallback gets it, and diff --git a/packages/agent-host/src/acp/session.ts b/packages/agent-host/src/acp/session.ts index d91dae62..8d990305 100644 --- a/packages/agent-host/src/acp/session.ts +++ b/packages/agent-host/src/acp/session.ts @@ -259,6 +259,10 @@ export abstract class AcpSession< params: { sessionId?: string; cwd: string; mcpServers: [] }, ): Promise { const client = this.client; + // Read now: a start that gives up ends the session (and drops its + // process) before the late answer arrives, so the capability cannot be + // looked up then. + const closesSessions = this.process?.capabilities.sessionCapabilities?.close != null; const request = method === 'session/new' ? client.request('session/new', params as { cwd: string; mcpServers: [] }) @@ -268,7 +272,7 @@ export abstract class AcpSession< (result) => { if (!timedOut) return; const sessionId = (result as { sessionId?: unknown } | null)?.sessionId; - if (typeof sessionId === 'string' && sessionId !== '') this.closeOrphanSession(client, sessionId); + if (typeof sessionId === 'string' && sessionId !== '') this.closeOrphanSession(client, sessionId, closesSessions); }, () => {}, ); @@ -290,8 +294,10 @@ export abstract class AcpSession< } /** Close a session the agent created after its start was given up on. The - * connection may be gone by now, so this is best effort. */ - private closeOrphanSession(client: AcpClient, sessionId: string): void { + * connection may be gone by now, so this is best effort — and an agent + * that does not serve `session/close` is not asked (Qwen Code does not). */ + private closeOrphanSession(client: AcpClient, sessionId: string, closesSessions: boolean): void { + if (!closesSessions) return; this.ctx.log(`[${this.deps.tag}] closing ${sessionId}: its start had already timed out`); void client.request('session/close', { sessionId }, { timeoutMs: SESSION_REQUEST_TIMEOUT_MS }).catch(() => {}); } @@ -584,13 +590,23 @@ export abstract class AcpSession< this.turnOpen = false; this.clearCancelGrace(); this.cancelPendingAsks(); + // The reply to a cancelled ask is written a few microtasks after the + // decision. Wait for it before the connection is let go: an agent that + // serves `session/close` yields here anyway while that request is in + // flight, but one that does not (Qwen Code) would otherwise have its + // answer cut off by the teardown. + await process_?.client.whenIdle(); if (sessionId !== undefined && process_) { // Closing the session lets the agent cancel its work and flush; the // conversation stays on disk for a later resume. Bounded, and skipped - // once the agent is gone: a wedged process must not hold this open. - await process_.client - .request('session/close', { sessionId }, { timeoutMs: SESSION_REQUEST_TIMEOUT_MS }) - .catch(() => {}); + // once the agent is gone and for an agent that does not serve + // `session/close` at all (Qwen Code advertises `list` and `resume` + // only): a method it does not have is not a method to call. + if (process_.capabilities.sessionCapabilities?.close != null) { + await process_.client + .request('session/close', { sessionId }, { timeoutMs: SESSION_REQUEST_TIMEOUT_MS }) + .catch(() => {}); + } } this.abandon(); } diff --git a/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts b/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts index e456c9c0..81f80be5 100644 --- a/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts +++ b/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts @@ -84,7 +84,7 @@ interface Harness { function withDriver(options: { env?: NodeJS.ProcessEnv; qwenPath?: string; providers?: ProviderBinding[] } = {}): Harness { const home = mkdtempSync(path.join(tmpdir(), 'codedeck-qwen-')); - const agent = new FakeAgent(); + const agent = new FakeAgent('qwen'); agent.options = configOptions(); agent.modes = modeState(); const spawns: Harness['spawns'] = []; @@ -238,7 +238,7 @@ describe('QwenSession startup', () => { } }); - it('closes the session an agent creates after the start request timed out', async () => { + it('does not ask an agent without session/close to dispose of a late session', async () => { const ready = withDriver(); // A live session keeps the shared process (and connection) up while the // next start times out. @@ -256,10 +256,15 @@ describe('QwenSession startup', () => { vi.useRealTimers(); } - // The agent finally answers, naming the session it created; it is not - // this driver's to keep, so it is closed rather than orphaned. + // Qwen Code advertises `list` and `resume` only, so the late session + // cannot be closed; a call to a method the agent does not serve would + // only be refused. It stays in the agent's own state. ready.agent.answerNewSession(); - await vi.waitFor(() => expect(ready.agent.closed).toEqual(['s2'])); + // A reply to a request sent after the late answer proves that answer was + // handled first: both travel the one stdout stream, in order. + await ready.agent.extRequest('craft/drainMidTurnQueue', { sessionId: 's1' }); + expect(ready.agent.requests.some((request) => request.method === 'session/close')).toBe(false); + expect(ready.agent.closed).toEqual([]); }); it('applies the mode and the model a session was started with', async () => { @@ -702,19 +707,14 @@ describe('QwenSession resume', () => { }); describe('QwenSession shutdown', () => { - it('returns from end() when the agent never answers session/close', async () => { + it('ends a session without asking an agent that does not serve session/close', async () => { const ready = withDriver(); await started(ready); - ready.agent.ignoreClose = true; - vi.useFakeTimers(); - try { - const ending = ready.session.end(); - await vi.advanceTimersByTimeAsync(SESSION_REQUEST_TIMEOUT_MS); - await ending; - expect(ready.agent.closed).toEqual([]); - } finally { - vi.useRealTimers(); - } + await ready.session.end(); + // Qwen Code advertises `list` and `resume` only. Its session is over when + // the driver lets it go; there is no close call to make. + expect(ready.agent.requests.some((request) => request.method === 'session/close')).toBe(false); + expect(ready.agent.closed).toEqual([]); }); it('ends a turn locally when the agent ignores the cancel, and the next message still runs', async () => { diff --git a/packages/agent-host/src/drivers/qwen/__tests__/qwenSecrets.test.ts b/packages/agent-host/src/drivers/qwen/__tests__/qwenSecrets.test.ts index 6671ec37..249c8c31 100644 --- a/packages/agent-host/src/drivers/qwen/__tests__/qwenSecrets.test.ts +++ b/packages/agent-host/src/drivers/qwen/__tests__/qwenSecrets.test.ts @@ -64,7 +64,7 @@ const modeState = (): unknown => ({ function withDriver(providerHttp?: ProviderHttp) { const home = mkdtempSync(path.join(tmpdir(), 'qwen-secrets-')); - const agent = new FakeAgent(); + const agent = new FakeAgent('qwen'); agent.options = configOptions(); agent.modes = modeState(); const spawns: Array<{ command: string; args: string[]; env: Record }> = []; From 7617a0ca3858e58134c471836fc8bc4ad59c125d Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:54:12 +0100 Subject: [PATCH 10/42] test(agent-host): reproduce the verified ACP agents in the fake MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ACP fake assumed one agent's surface for both drivers: it advertised close/list/resume to Qwen Code, which serves list and resume only, and its tool-call and config-refusal shapes were never checked against a real build. The tests therefore passed across behaviour the agents do not have. Probe dsh 0.2.1-rc.2 and qwen-code 0.26.1 over ACP — initialize, the session lifecycle, a /tool turn against the local fake model, set_config_option with offered and unoffered values, set_mode, and a permission ask — and give the fake a per-agent profile built from what was observed: - the harness advertises close/list/resume, names a tool as the tool_call title (`bash`, kind "other") with its arguments, refuses an unoffered model or effort with "unknown model option" / "unknown reasoning effort for : ", has no set_mode, lists only closed conversations, and its permission ask names only the tool call's id; - Qwen Code advertises list/resume and loadSession but not close, keeps the tool name in `_meta.toolName` (its title is the display word "Shell") and sends the arguments in a following tool_call_update, refuses with "Model '' not found for authType 'openai'" and "Unknown reasoning effort …", has set_mode, needs a cwd for session/list, and repeats the call's facts in its permission ask. The DeepSeek and Qwen Code tests now name the profile they drive, and profiles.test.ts locks the shapes to the probe so a later edit cannot drift silently. Re-probe on either agent's version bump. Co-Authored-By: Claude Code --- .../agent-host/src/acp/__tests__/fakeAgent.ts | 8 +- .../src/acp/__tests__/profiles.test.ts | 285 ++++++++++++++++++ .../deepseek/__tests__/deepseekDriver.test.ts | 43 ++- .../__tests__/deepseekSecrets.test.ts | 2 +- .../drivers/qwen/__tests__/qwenDriver.test.ts | 29 +- 5 files changed, 336 insertions(+), 31 deletions(-) create mode 100644 packages/agent-host/src/acp/__tests__/profiles.test.ts diff --git a/packages/agent-host/src/acp/__tests__/fakeAgent.ts b/packages/agent-host/src/acp/__tests__/fakeAgent.ts index 6e552913..a3c9bbcf 100644 --- a/packages/agent-host/src/acp/__tests__/fakeAgent.ts +++ b/packages/agent-host/src/acp/__tests__/fakeAgent.ts @@ -203,8 +203,9 @@ const DEEPSEEK: Profile = { }, configRefusal(configId, value, options) { if (configId === 'model') return `Invalid params: unknown model option: ${value}`; - const current = asRecord(options.find((entry) => asRecord(entry).id === configId)).currentValue; - return `Invalid params: unknown reasoning effort for ${harnessModelId(typeof current === 'string' ? current : '')}: ${value}`; + // The harness names the model the effort was refused for. + const model = asRecord(options.find((entry) => asRecord(entry).id === 'model')).currentValue; + return `Invalid params: unknown reasoning effort for ${harnessModelId(typeof model === 'string' ? model : '')}: ${value}`; }, unknownConfigRefusal(configId) { return `Invalid params: unknown session config option: ${configId}`; @@ -291,7 +292,8 @@ const QWEN: Profile = { }, configRefusal(configId, value, options) { if (configId === 'model') return `Invalid params: Model '${value}' not found for authType 'openai'`; - const offered = offeredValues(options); + const option = options.find((entry) => asRecord(entry).id === configId); + const offered = offeredValues(option === undefined ? [] : [option]); // The message puts the provider default first, then the option list. const choices = ['default', ...offered.filter((entry) => entry !== 'default')].join(', '); return `Invalid params: Unknown reasoning effort: ${value}. Choose one of: ${choices}`; diff --git a/packages/agent-host/src/acp/__tests__/profiles.test.ts b/packages/agent-host/src/acp/__tests__/profiles.test.ts new file mode 100644 index 00000000..a6efa39c --- /dev/null +++ b/packages/agent-host/src/acp/__tests__/profiles.test.ts @@ -0,0 +1,285 @@ +/** + * The ACP fake stands in for two real agents, and a fake is only useful for + * as long as its shapes are the agent's. These tests drive it with the + * driver's own ACP client and assert what the pinned builds were probed + * answering: the initialize capabilities, the session lifecycle each one + * serves, how a tool call names its tool, what an unoffered config value is + * refused with, the permission ask, and the end of a turn. + * + * The expectations are the probe's observations (the comment on fakeAgent.ts + * says how they were taken). A bump of either agent should be re-probed + * before these are touched. + */ +import { describe, expect, it } from 'vitest'; +import { AcpClient, AcpRequestError } from '../acp'; +import { FakeAgent, type AcpProfile } from './fakeAgent'; + +/** An ACP client on one fake's stdio, as a driver's runtime would open it. */ +function connect(profile: AcpProfile): { agent: FakeAgent; client: AcpClient } { + const agent = new FakeAgent(profile); + const client = new AcpClient({ stdin: agent.child.stdin, stdout: agent.child.stdout, log: () => {}, tag: 'profile' }); + return { agent, client }; +} + +async function initialize(profile: AcpProfile) { + const { agent, client } = connect(profile); + const response = await client.request('initialize', { protocolVersion: 1, clientCapabilities: {} }); + return { agent, client, response }; +} + +const MODEL_OPTION = { + id: 'model', + name: 'Model', + category: 'model', + type: 'select', + currentValue: 'm1', + options: [{ value: 'm1', name: 'M1' }], +}; + +const EFFORT_OPTION = { + id: 'reasoning_effort', + name: 'Reasoning effort', + category: 'thought_level', + type: 'select', + currentValue: 'high', + options: [ + { value: 'off', name: 'Off' }, + { value: 'high', name: 'High' }, + ], +}; + +/** The refusal an agent answers a request with, as the client raises it. */ +async function expectRefusal(promise: Promise): Promise { + const failure = await promise.then( + () => undefined, + (error: unknown) => error, + ); + expect(failure).toBeInstanceOf(AcpRequestError); + return failure as AcpRequestError; +} + +describe('initialize, as each pinned agent answers it', () => { + it('the DeepSeek Harness advertises close, list and resume, and no load', async () => { + const { response } = await initialize('deepseek'); + expect(response.agentInfo).toEqual({ name: 'deepseek-harness-acp', version: '0.0.1' }); + expect(response.agentCapabilities).toEqual({ + mcpCapabilities: { http: true }, + promptCapabilities: { image: false, audio: false, embeddedContext: false }, + sessionCapabilities: { close: {}, list: {}, resume: {} }, + }); + expect(response.authMethods).toEqual([]); + }); + + it('Qwen Code advertises list and resume, load (not close), and no auth prompt', async () => { + const { response } = await initialize('qwen'); + expect(response.agentInfo).toEqual({ name: 'qwen-code', title: 'Qwen Code', version: '0.26.1' }); + expect(response.agentCapabilities).toMatchObject({ + loadSession: true, + promptCapabilities: { image: true, audio: true, embeddedContext: true }, + mcpCapabilities: { sse: true, http: true }, + sessionCapabilities: { list: {}, resume: {} }, + }); + // `session/close` is the one lifecycle method it does not have. + expect(response.agentCapabilities?.sessionCapabilities).not.toHaveProperty('close'); + }); +}); + +describe('the session lifecycle each agent serves', () => { + it('the harness closes a session, then calls the same id unknown; it lists only closed ones', async () => { + const { agent, client } = await initialize('deepseek'); + const created = await client.request('session/new', { cwd: '/w', mcpServers: [] }); + // Nothing is listed while the conversation is live. + expect(await client.request('session/list', { cwd: '/w' })).toEqual({ sessions: [] }); + + expect(await client.request('session/close', { sessionId: created.sessionId })).toEqual({}); + expect(agent.closed).toEqual([created.sessionId]); + expect(await client.request('session/list', { cwd: '/w' })).toMatchObject({ + sessions: [{ sessionId: created.sessionId, cwd: '/w' }], + }); + const again = await expectRefusal(client.request('session/close', { sessionId: created.sessionId })); + expect(again).toMatchObject({ code: -32602, message: `Invalid params: unknown session: ${created.sessionId}` }); + + // It has no session/load. + const load = await expectRefusal(client.request('session/load', { sessionId: created.sessionId, cwd: '/w', mcpServers: [] })); + expect(load).toMatchObject({ code: -32601, message: '"Method not found": session/load' }); + }); + + it('Qwen Code refuses session/close and lists live sessions for the cwd it is given', async () => { + const { client } = await initialize('qwen'); + const created = await client.request('session/new', { cwd: '/w', mcpServers: [] }); + + // It lists the conversation while it is live — but only for a cwd. + expect(await client.request('session/list', { cwd: '/w' })).toMatchObject({ + sessions: [{ sessionId: created.sessionId, cwd: '/w' }], + }); + expect(await client.request('session/list', {})).toEqual({ sessions: [] }); + + const close = await expectRefusal(client.request('session/close', { sessionId: created.sessionId })); + expect(close).toMatchObject({ code: -32601, message: '"Method not found": session/close' }); + }); +}); + +describe('config options', () => { + it('the harness refuses an unoffered model and an unknown config id', async () => { + const { agent, client } = await initialize('deepseek'); + agent.options = [MODEL_OPTION, EFFORT_OPTION]; + const created = await client.request('session/new', { cwd: '/w', mcpServers: [] }); + + const accepted = await client.request('session/set_config_option', { sessionId: created.sessionId, configId: 'model', value: 'm1' }); + expect(accepted.configOptions?.[0]).toMatchObject({ id: 'model', currentValue: 'm1' }); + const model = await expectRefusal( + client.request('session/set_config_option', { sessionId: created.sessionId, configId: 'model', value: 'nope' }), + ); + expect(model).toMatchObject({ code: -32602, message: 'Invalid params: unknown model option: nope' }); + const effort = await expectRefusal( + client.request('session/set_config_option', { sessionId: created.sessionId, configId: 'reasoning_effort', value: 'turbo' }), + ); + expect(effort).toMatchObject({ code: -32602, message: 'Invalid params: unknown reasoning effort for m1: turbo' }); + const id = await expectRefusal( + client.request('session/set_config_option', { sessionId: created.sessionId, configId: 'nope', value: 'x' }), + ); + expect(id).toMatchObject({ code: -32602, message: 'Invalid params: unknown session config option: nope' }); + }); + + it('Qwen Code refuses an unoffered model and effort with its own wording', async () => { + const { agent, client } = await initialize('qwen'); + agent.options = [MODEL_OPTION, EFFORT_OPTION]; + const created = await client.request('session/new', { cwd: '/w', mcpServers: [] }); + + const model = await expectRefusal( + client.request('session/set_config_option', { sessionId: created.sessionId, configId: 'model', value: 'nope' }), + ); + expect(model).toMatchObject({ code: -32602, message: "Invalid params: Model 'nope' not found for authType 'openai'" }); + const effort = await expectRefusal( + client.request('session/set_config_option', { sessionId: created.sessionId, configId: 'reasoning_effort', value: 'turbo' }), + ); + expect(effort).toMatchObject({ + code: -32602, + message: 'Invalid params: Unknown reasoning effort: turbo. Choose one of: default, off, high', + }); + const id = await expectRefusal( + client.request('session/set_config_option', { sessionId: created.sessionId, configId: 'nope', value: 'x' }), + ); + expect(id).toMatchObject({ code: -32602, message: 'Invalid params: Unsupported configId: nope' }); + }); +}); + +describe('a tool call names its tool the way each agent does', () => { + it('the harness puts the name in title and takes no _meta', async () => { + const { agent, client } = await initialize('deepseek'); + const updates: Array> = []; + client.on('session/update', (notification) => updates.push(notification.update as Record)); + + agent.toolCall('s1', 'c1', 'bash', { command: 'ls' }); + // A permission round trip proves the notification before it was read. + client.onRequest('session/request_permission', async () => ({ outcome: { outcome: 'selected', optionId: 'allow-once' } as const })); + await agent.askPermission('s1', 'c1'); + + expect(updates).toEqual([ + { sessionUpdate: 'tool_call', toolCallId: 'c1', title: 'bash', kind: 'other', status: 'in_progress', rawInput: { command: 'ls' } }, + ]); + expect(updates[0]).not.toHaveProperty('name'); + expect(updates[0]).not.toHaveProperty('_meta'); + }); + + it('Qwen Code keeps the name in _meta.toolName and sends the arguments in a second update', async () => { + const { agent, client } = await initialize('qwen'); + const updates: Array> = []; + client.on('session/update', (notification) => updates.push(notification.update as Record)); + + agent.toolCall('s1', 'c1', 'run_shell_command', { command: 'ls' }); + client.onRequest('session/request_permission', async () => ({ outcome: { outcome: 'selected', optionId: 'proceed_once' } as const })); + await agent.askPermission('s1', 'c1'); + + expect(updates).toHaveLength(2); + expect(updates[0]).toMatchObject({ + sessionUpdate: 'tool_call', + toolCallId: 'c1', + status: 'pending', + title: 'Shell', + kind: 'execute', + rawInput: {}, + _meta: { toolName: 'run_shell_command' }, + }); + expect(updates[0]).not.toHaveProperty('name'); + expect(updates[1]).toMatchObject({ + sessionUpdate: 'tool_call_update', + toolCallId: 'c1', + status: 'in_progress', + rawInput: { command: 'ls' }, + _meta: { toolName: 'run_shell_command' }, + }); + }); +}); + +describe('the permission ask', () => { + it('the harness names only the tool call and offers Allow once or Reject', async () => { + const { agent: harness, client: peer } = connect('deepseek'); + let asked: Record | undefined; + peer.onRequest('session/request_permission', async (request) => { + asked = request as Record; + return { outcome: { outcome: 'selected', optionId: 'allow-once' } as const }; + }); + + const outcome = await harness.askPermission('s1', 'c1'); + expect(outcome).toEqual({ outcome: 'selected', optionId: 'allow-once' }); + expect(asked).toEqual({ + sessionId: 's1', + toolCall: { toolCallId: 'c1' }, + options: [ + { optionId: 'allow-once', name: 'Allow once', kind: 'allow_once' }, + { optionId: 'reject-once', name: 'Reject', kind: 'reject_once' }, + ], + }); + }); + + it('Qwen Code repeats the call and offers allow-always choices beside Allow and Reject', async () => { + const { agent, client } = await initialize('qwen'); + let asked: Record | undefined; + client.onRequest('session/request_permission', async (request) => { + asked = request as Record; + return { outcome: { outcome: 'selected', optionId: 'proceed_once' } as const }; + }); + + agent.toolCall('s1', 'c1', 'run_shell_command', { command: 'echo hi' }); + const outcome = await agent.askPermission('s1', 'c1'); + expect(outcome).toEqual({ outcome: 'selected', optionId: 'proceed_once' }); + + const options = (asked as { options: Array<{ optionId: string; kind: string }> }).options; + expect(options.map((option) => option.optionId)).toEqual([ + 'proceed_always_project', + 'proceed_always_user', + 'proceed_once', + 'cancel', + ]); + expect(options.map((option) => option.kind)).toEqual(['allow_always', 'allow_always', 'allow_once', 'reject_once']); + expect(asked).toMatchObject({ + toolCall: { + toolCallId: 'c1', + status: 'pending', + title: 'Shell', + kind: 'execute', + rawInput: { command: 'echo hi' }, + _meta: { toolName: 'run_shell_command' }, + }, + }); + }); +}); + +describe('the end of a turn', () => { + it('the harness answers a stop reason alone; Qwen Code adds its branch point', async () => { + const deepseek = await initialize('deepseek'); + const deepseekSession = await deepseek.client.request('session/new', { cwd: '/w', mcpServers: [] }); + expect( + await deepseek.client.request('session/prompt', { sessionId: deepseekSession.sessionId, prompt: [{ type: 'text', text: 'hi' }] }), + ).toEqual({ stopReason: 'end_turn' }); + + const qwen = await initialize('qwen'); + const qwenSession = await qwen.client.request('session/new', { cwd: '/w', mcpServers: [] }); + const result = await qwen.client.request('session/prompt', { sessionId: qwenSession.sessionId, prompt: [{ type: 'text', text: 'hi' }] }); + expect(result).toMatchObject({ + stopReason: 'end_turn', + _meta: { 'qwen.branchPoint': { assistantRecordUuid: expect.any(String), checkpointUuid: expect.any(String) } }, + }); + }); +}); diff --git a/packages/agent-host/src/drivers/deepseek/__tests__/deepseekDriver.test.ts b/packages/agent-host/src/drivers/deepseek/__tests__/deepseekDriver.test.ts index bfa4479f..5224849a 100644 --- a/packages/agent-host/src/drivers/deepseek/__tests__/deepseekDriver.test.ts +++ b/packages/agent-host/src/drivers/deepseek/__tests__/deepseekDriver.test.ts @@ -4,6 +4,7 @@ import { createServer, type Socket } from 'node:net'; import { tmpdir } from 'node:os'; import * as path from 'node:path'; import { describe, expect, it, vi } from 'vitest'; +import { SESSION_REQUEST_TIMEOUT_MS } from '../../../acp/session'; import { recordingContext, type RecordingContext } from '../../../sdk/__tests__/context'; import type { StartSession } from '../../../sdk/types'; import type { DriverSession } from '../../../sdk/driver'; @@ -76,7 +77,7 @@ function withDriver( options: { idleCloseMs?: number; env?: NodeJS.ProcessEnv; dshPath?: string; log?: (line: string) => void } = {}, ): Harness { const home = harnessHome(); - const harness = new FakeAgent(); + const harness = new FakeAgent('deepseek'); harness.options = configOptions(); const spawns: Harness['spawns'] = []; const logs: string[] = []; @@ -533,6 +534,46 @@ describe('DeepSeekSession lifecycle', () => { await vi.waitFor(() => expect(ready.harness.child.killed).toContain('SIGTERM')); }); + it('closes the session the harness creates after the start request timed out', async () => { + const ready = withDriver(); + // A live session keeps the shared process (and connection) up while the + // next start times out. + await started(ready); + ready.harness.holdNextNewSession = true; + vi.useFakeTimers(); + try { + const ctx = recordingContext(); + ready.driver.startSession(start({ sessionId: 'b2' }), ctx); + await vi.advanceTimersByTimeAsync(SESSION_REQUEST_TIMEOUT_MS); + expect(ctx.events.find((event) => event.type === 'ended')).toMatchObject({ + error: expect.stringMatching(/session\/new did not answer/), + }); + } finally { + vi.useRealTimers(); + } + + // The harness advertises `session/close`: the session it finally + // answered with is not this driver's to keep, so it is closed rather + // than orphaned. + ready.harness.answerNewSession(); + await vi.waitFor(() => expect(ready.harness.closed).toEqual(['s2'])); + }); + + it('returns from end() when the harness never answers session/close', async () => { + const ready = withDriver(); + await started(ready); + ready.harness.ignoreClose = true; + vi.useFakeTimers(); + try { + const ending = ready.session.end(); + await vi.advanceTimersByTimeAsync(SESSION_REQUEST_TIMEOUT_MS); + await ending; + expect(ready.harness.closed).toEqual([]); + } finally { + vi.useRealTimers(); + } + }); + it('closes sessions ended together one by one, and the shared process only after the last', async () => { const ready = withDriver(); const sessions: DriverSession[] = []; diff --git a/packages/agent-host/src/drivers/deepseek/__tests__/deepseekSecrets.test.ts b/packages/agent-host/src/drivers/deepseek/__tests__/deepseekSecrets.test.ts index a812e602..68e9f353 100644 --- a/packages/agent-host/src/drivers/deepseek/__tests__/deepseekSecrets.test.ts +++ b/packages/agent-host/src/drivers/deepseek/__tests__/deepseekSecrets.test.ts @@ -51,7 +51,7 @@ function withDriver( options: { env?: NodeJS.ProcessEnv; httpGet?: HttpGet; providerHttp?: ProviderHttp; log?: (line: string) => void } = {}, ) { const home = harnessHome(); - const agent = new FakeAgent(); + const agent = new FakeAgent('deepseek'); agent.options = configOptions(); const spawns: Array<{ command: string; args: string[]; env: Record }> = []; const logs: string[] = []; diff --git a/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts b/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts index 81f80be5..834bf27a 100644 --- a/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts +++ b/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts @@ -363,32 +363,9 @@ describe('QwenSession updates', () => { ready.agent.message('s1', ''); ready.agent.thought('s1', 'weighing options'); ready.agent.message('s1', 'Here is the plan.'); - // The first update carries the title and no arguments, as Qwen Code sends it. - ready.agent.send('session/update', { - sessionId: 's1', - update: { - sessionUpdate: 'tool_call', - toolCallId: 'c1', - title: 'Shell', - kind: 'execute', - status: 'pending', - rawInput: {}, - _meta: { toolName: 'run_shell_command' }, - }, - }); - // The running update carries the arguments. - ready.agent.send('session/update', { - sessionId: 's1', - update: { - sessionUpdate: 'tool_call_update', - toolCallId: 'c1', - status: 'in_progress', - title: 'Shell: npm ci', - kind: 'execute', - rawInput: { command: 'npm ci\nnpm test' }, - _meta: { toolName: 'run_shell_command' }, - }, - }); + // The first update carries the title and no arguments; the arguments + // arrive in the update that starts the call, as Qwen Code sends them. + ready.agent.toolCall('s1', 'c1', 'run_shell_command', { command: 'npm ci\nnpm test' }); ready.agent.toolResult('s1', 'c1', 'all green'); await vi.waitFor(() => expect(ctx.entries().filter((entry) => entry.entryType === 'tool_result')).toHaveLength(1)); From 3dc425205d34d0cfc8bd1362c28eb1c7a037f716 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:58:29 +0100 Subject: [PATCH 11/42] fix(acp): an ending session waits on its own asks, not another's An ending session waits for the agent's requests to be answered so the cancellations it just decided reach the agent before the connection is let go. But one agent process serves several sessions, and the wait covered all of them: ending one session while another had a permission card open stalled until someone answered that card (or the host's end timeout ran out). The wait is now bounded at one second; the replies it exists for are written within microtasks. Co-Authored-By: Claude Code --- packages/agent-host/src/acp/session.ts | 15 +++++++++++++-- .../src/drivers/qwen/__tests__/qwenDriver.test.ts | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/agent-host/src/acp/session.ts b/packages/agent-host/src/acp/session.ts index 8d990305..42df2c3e 100644 --- a/packages/agent-host/src/acp/session.ts +++ b/packages/agent-host/src/acp/session.ts @@ -55,6 +55,12 @@ export interface AcpSessionDeps { * shutdown for ever. */ export const SESSION_REQUEST_TIMEOUT_MS = 15_000; +/** How long an ending session waits for the agent's requests to be answered + * before it lets the connection go: the cancellations it just decided are + * written within microtasks, and anything still open past this belongs to + * another session on the same process. */ +const IDLE_WAIT_MS = 1_000; + /** After `session/cancel`, how long the prompt may stay unsettled before the * turn is ended here anyway: an agent that ignores the cancel must not keep * the phone showing a turn that will never finish. */ @@ -594,8 +600,13 @@ export abstract class AcpSession< // decision. Wait for it before the connection is let go: an agent that // serves `session/close` yields here anyway while that request is in // flight, but one that does not (Qwen Code) would otherwise have its - // answer cut off by the teardown. - await process_?.client.whenIdle(); + // answer cut off by the teardown. Bounded: the process can serve other + // sessions, whose own asks stay open until someone answers them. + if (process_) { + let timer: ReturnType | undefined; + await Promise.race([process_.client.whenIdle(), new Promise((resolve) => (timer = setTimeout(resolve, IDLE_WAIT_MS)))]); + clearTimeout(timer); + } if (sessionId !== undefined && process_) { // Closing the session lets the agent cancel its work and flush; the // conversation stays on disk for a later resume. Bounded, and skipped diff --git a/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts b/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts index 834bf27a..c19c99c0 100644 --- a/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts +++ b/packages/agent-host/src/drivers/qwen/__tests__/qwenDriver.test.ts @@ -737,6 +737,20 @@ describe('QwenSession shutdown', () => { await ready.session.end(); await expect(asked).resolves.toMatchObject({ outcome: 'cancelled' }); }); + + it('ends without waiting on another session’s open permission ask', async () => { + // One agent process serves both sessions; the second one's card stays + // open until someone answers it, which may be never. + const ready = withDriver(); + await started(ready, { sessionId: 'b1' }); + const first = ready.session!; + const other = await started(ready, { sessionId: 'b2' }, recordingContext({ permission: () => new Promise(() => {}) })); + void ready.agent.askPermission('s2', 'call-2'); + await vi.waitFor(() => expect(other.permissions).toHaveLength(1)); + const begun = Date.now(); + await first.end(); + expect(Date.now() - begun).toBeLessThan(3_000); + }); }); describe('QwenDriver conversations', () => { From f03aa0986b93af64e7d22a5536ab94431645e5eb Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:46:31 +0100 Subject: [PATCH 12/42] fix(opencode): read MCP and plugin state from what 2.1.4 reports Verified against OpenCode 2.1.4 before any test was moved: - config.get answers an array of documents whose mcp section is {servers: {name: config}}; reading info.mcp as the server map found nothing, so the phone never saw the machine's MCP servers. - mcp.add and mcp.remove change the running inventory only, not the config file. The manager now records the adds and removes of the current client (a replaced server starts from its config again), so an added server is listed and a removed one is not until the server restarts. - mcp.disconnect reports status "disabled" in mcp.list and leaves the config entry untouched, so the off switch is the inventory's status. - plugin.list names a package plugin by source.target; a package entry has no id. A failed package now reads as failed rather than as a healthy config line. The shared fake states these behaviours and the rest the probes found; opencodeVerifiedServer.test.ts reproduces the four bugs over it. Co-Authored-By: Claude Code --- .../drivers/opencode/__tests__/fakeServer.ts | 668 ++++++++++++++++++ .../opencode/__tests__/opencodeMcp.test.ts | 3 +- .../__tests__/opencodeVerifiedServer.test.ts | 93 +++ .../agent-host/src/drivers/opencode/mcp.ts | 74 +- .../src/drivers/opencode/plugins.ts | 17 +- 5 files changed, 838 insertions(+), 17 deletions(-) create mode 100644 packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts create mode 100644 packages/agent-host/src/drivers/opencode/__tests__/opencodeVerifiedServer.test.ts diff --git a/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts b/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts new file mode 100644 index 00000000..9eca9c5e --- /dev/null +++ b/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts @@ -0,0 +1,668 @@ +/** + * One fake OpenCode 2.x server for every OpenCode test. + * + * Behaviour verified against OpenCode 2.1.4 on 2026-10-27; re-probe on a + * version bump (`packages/agent-host/tools/fake-model.mjs` for a real + * session, throwaway XDG dirs and `serve --port=0` for the rest). What the + * probes established, and this fake therefore states: + * + * - `provider.list` lists only the providers the server can use, and an + * empty list for the first moments after it starts (registration); the + * `integration.list` ids are every provider it knows (about 235) and + * `provider.get` describes one that `provider.list` leaves out. + * - `config.get` is an array of documents: one `{type:'document', info}` + * from the config file and one `{type:'directory', info:{}}`. A config + * file's `mcp` section reads back as `{servers: {name: config}}`, not as + * the bare map the file spells. + * - MCP `add` puts a server in the running inventory only: `mcp.list` + * shows it, `config.get` and the file do not, and a server restart loses + * it. `remove` likewise drops a config-file server from the inventory + * without touching the file, so it returns on the next start. + * `disconnect` reports status `disabled` in `mcp.list` and changes + * nothing in the config document; `connect` reports `connected`. + * - `plugin.list` is the 90 built-in plugins by `id` plus package plugins + * named by `source.target` (they carry no `id`). A package that fails to + * load is a `{source:{type:'package',target}, state:{status:'failed', + * error}}` entry. `plugin.update({targets})` accepts only packages in + * that inventory, and throws `InvalidRequestError` ("Plugin packages are + * not in the current server inventory: …") otherwise. + * - `integration.connect.key` creates an active credential, deactivating + * the integration's previous one; `credential.list` carries the secret; + * `credential.activate` switches which one is active; removing the + * active one leaves another of that integration active. + * `integration.oauth.status` is stateful (an attempt stays complete) and + * throws `IntegrationAttemptNotFoundError` for an attempt it does not + * know; `oauth.cancel` of an unknown attempt resolves. + * - `event.subscribe` opens immediately (a `server.connected` event at + * once), buffers what happens while the caller is still setting up, and + * stays open for the session's life. A client abort ends the iterator + * cleanly; the server being killed makes it throw (a `ClientError: + * Transport: terminated`), which is what the driver turns into an error + * end. `holdStream` therefore defaults to true. + * - A real turn is `session.execution.started`, `session.step.started`, + * `session.text.delta`/`ended`, `session.step.ended` (with the step's own + * `tokens`) and `session.execution.succeeded`; 2.1.4 does not send + * `session.idle` for that turn. + * + * The fake's own state, not a mock's return value, is what its methods read + * and write, so a test can make calls in any order and see what a server + * would. Every method is a `vi.fn` around that state, so a test can assert + * a call, or replace one method where the scenario is about a failure. + */ +import { vi } from 'vitest'; +import type { + AgentGetOutput, + AgentInfo, + CredentialEntry, + IntegrationInfo, + McpServer, + ModelInfo, + OpenCodeClient, + PluginInfo, + ProviderInfo, + SessionInfo, + V2Event, +} from '@opencode/client'; + +/** An event as the client's async iterator yields it; `type` is the switch + * the driver reads. */ +export type FakeEvent = V2Event & { type: string; data?: Record; location?: { directory?: string } }; + +export interface FakeAgentRow { + id: string; + name: string; + mode: 'primary' | 'subagent' | 'all'; + hidden: boolean; + description?: string; + color?: string; + /** The agent's own permission rules, restated after the mode's. */ + rules?: ReadonlyArray<{ action: string; resource: string; effect: 'allow' | 'deny' | 'ask' }>; +} + +/** A plugin as 2.1.4's `plugin.list` states it: built-ins by `id`, packages + * by `source.target` (there is no `id`). */ +export type FakePlugin = Pick & { features?: Record }; + +export interface FakeMcpServer { + name: string; + /** The status word 2.1.4 reports. */ + status: 'connected' | 'pending' | 'disabled' | 'failed' | 'needs_auth'; + error?: string; +} + +export interface FakeSignIn { + attemptID: string; + url: string; + instructions?: string; + mode?: string; + created?: number; + expires?: number; + status: 'pending' | 'complete' | 'failed' | 'expired'; + message?: string; +} + +export interface FakeServerOptions { + /** Events the stream yields once it opens. */ + events?: FakeEvent[]; + /** Keep the stream open after `events`, as a real server does (default + * true). A test that only needs the rest of the session to finish sets + * it false, and the stream closes cleanly at the end of the script. */ + holdStream?: boolean; + /** Providers the server knows: the first are usable, `usable: false` + * ones are only describable through `provider.get`. */ + providers?: Array<{ info: ProviderInfo; models?: ModelInfo[]; usable?: boolean }>; + /** The server's `OPENCODE_CONFIG_CONTENT`, as the driver hands it over: + * its `providers` become usable providers with models. */ + config?: { providers?: Record }; + /** `provider.list` answers empty for this many calls first. */ + providerRegistration?: number; + /** Integrations that are no model provider (web search, for one). */ + integrations?: string[]; + /** Agent rows `agent.list` answers with. A function sees the call number, + * for OpenCode's built-ins-then-directory-agents order. */ + agents?: FakeAgentRow[] | ((call: number) => FakeAgentRow[]); + /** The config file's `plugins` entries (as the manager reads them). */ + pluginConfig?: Array }>; + /** Package plugins the server loaded (or failed to load). */ + plugins?: FakePlugin[]; + /** The config file's MCP servers, served through `config.get`. */ + mcpConfig?: Record; disabled?: boolean } | { type: 'remote'; url: string; headers?: Record; disabled?: boolean }>; + /** Servers in the running inventory (defaults to `mcpConfig`, connected). */ + mcpServers?: FakeMcpServer[]; + /** Integrations `integration.list` answers with. Defaults to the provider + * ids plus `integrations`. */ + integrationRows?: IntegrationInfo[]; + credentials?: CredentialEntry[]; + /** Sign-ins the server knows, by attempt id (`oauth.status` is stateful). */ + signIns?: FakeSignIn[]; + /** The id `session.create` gives a new session. */ + sessionId?: string; + /** The session `session.get` answers, or one `session.create` made. A + * resumed id the fake does not know is `SessionNotFoundError`. */ + session?: Partial; + /** The version `server.info` reports. */ + version?: string; + /** Make `server.info` fail, as a server that cannot answer. */ + serverInfoError?: Error; + /** Per-method replacement, applied over the stateful fake (the old + * `clientWith`'s `over`). */ + over?: Record; +} + +export interface FakeProviderConfig { + name?: string; + package?: string; + settings?: { baseURL?: string; [key: string]: unknown }; + models?: Record; +} + +export interface FakeModelConfig { + name?: string; + family?: string; + limit?: { context?: number; output?: number }; + capabilities?: { tools?: boolean; input?: string[]; output?: string[]; temperature?: boolean; reasoning?: unknown }; + cost?: ModelInfo['cost'] | Record; +} + +export interface FakeOpenCodeServer { + client: OpenCodeClient; + /** The state the methods read and write. */ + state: { + providers: Map; + models: Map; + createdSessions: Map; + plugins: FakePlugin[]; + mcpConfig: Record; + mcp: Map; + integrations: IntegrationInfo[]; + credentials: CredentialEntry[]; + signIns: Map; + events: FakeEvent[]; + }; + /** Queue an event for the open stream. */ + push(event: FakeEvent): void; + /** End the stream cleanly, as a client abort does. */ + endStream(): void; + /** Fail the stream, as a server killed under it does. */ + failStream(error: unknown): void; + /** Resolves once the scripted events have been consumed (each has been + * handed to the driver's loop). */ + scripted(): Promise; + /** Resolves after the script has been consumed and the driver's + * off-stream handlers have had their microtasks and one macrotask. */ + settled(): Promise; + /** Change a sign-in's status, which `oauth.status` then keeps reporting. */ + setSignIn(attemptID: string, status: FakeSignIn['status'], message?: string): void; +} + +const BUILTIN_IDS = new Set(['build', 'plan', 'general', 'explore', 'compaction', 'title', 'summary']); + +/** The agents a stock 2.1.4 server lists, in its own order. */ +export const OPENCODE_AGENTS: FakeAgentRow[] = [ + { id: 'build', name: 'Build', mode: 'primary', hidden: false, rules: [{ action: '*', resource: '*', effect: 'allow' }, { action: 'read', resource: '*.env', effect: 'ask' }] }, + { id: 'plan', name: 'Plan', mode: 'primary', hidden: false, rules: [{ action: '*', resource: '*', effect: 'allow' }, { action: 'edit', resource: '*', effect: 'deny' }, { action: 'edit', resource: '/plans/*', effect: 'allow' }] }, + { id: 'general', name: 'General', mode: 'subagent', hidden: false }, + { id: 'explore', name: 'Explore', mode: 'subagent', hidden: false }, + { id: 'compaction', name: 'Compaction', mode: 'primary', hidden: true }, + { id: 'title', name: 'Title', mode: 'primary', hidden: true }, + { id: 'summary', name: 'Summary', mode: 'primary', hidden: true }, +]; + +/** The same list plus a directory's own `review` agent. */ +export const OPENCODE_AGENTS_WITH_REVIEW: FakeAgentRow[] = [ + ...OPENCODE_AGENTS, + { id: 'review', name: 'review', mode: 'primary', hidden: false, description: 'Reviews code', color: '#E5C07B' }, +]; + +/** An integration as 2.1.4's `integration.list` states one: no `tinyfish` + * provider, methods without ids for a plain key. */ +const INTEGRATIONS: IntegrationInfo[] = [ + { + id: 'nano-gpt', + name: 'NanoGPT', + methods: [{ type: 'key' }, { type: 'env', names: ['NANO_GPT_API_KEY'] }], + connections: [{ type: 'credential', id: 'cred_1', label: 'work', method: 'key' }], + }, + { + id: 'github-copilot', + name: 'GitHub Copilot', + methods: [ + { type: 'env', names: ['GITHUB_TOKEN'] }, + { + id: 'device', + type: 'oauth', + label: 'Login with GitHub Copilot', + form: [ + { key: 'deploymentType', title: 'Deployment', required: true, type: 'string', options: [{ value: 'github.com', label: 'GitHub.com' }] }, + { key: 'enterpriseUrl', title: 'Enterprise URL', required: true, type: 'string', when: [{ key: 'deploymentType', op: 'eq', value: 'enterprise' }] }, + { key: 'server', hidden: true, type: 'string', default: 'https://x' }, + ], + }, + ], + connections: [{ type: 'env', name: 'GITHUB_TOKEN' }], + }, + { id: 'openai', name: 'OpenAI', methods: [{ type: 'key' }, { id: 'chatgpt', type: 'oauth', label: 'Sign in with ChatGPT' }], connections: [] }, +]; + +const DEFAULT_ACCESS = { input: ['text'], output: ['text'] } as const; + +/** The model a provider's config entry names, as `model.list` reports it. */ +function modelFromConfig(providerID: string, modelID: string, entry: FakeModelConfig): ModelInfo { + const reasoning = entry.capabilities?.reasoning as { effort?: string[]; toggle?: boolean } | undefined; + const variants = (reasoning?.effort ?? []).map((id) => ({ id })) as ModelInfo['variants']; + const cost = Array.isArray(entry.cost) ? entry.cost : entry.cost ? [entry.cost] : []; + return { + id: modelID, + modelID, + providerID, + name: entry.name ?? modelID, + ...(entry.family ? { family: entry.family } : {}), + capabilities: { + tools: entry.capabilities?.tools ?? true, + input: [...(entry.capabilities?.input ?? DEFAULT_ACCESS.input)], + output: [...(entry.capabilities?.output ?? DEFAULT_ACCESS.output)], + ...(entry.capabilities?.temperature !== undefined ? { temperature: entry.capabilities.temperature } : {}), + }, + variants, + cost, + limit: { context: entry.limit?.context ?? 0, output: entry.limit?.output ?? 0 }, + status: 'active', + enabled: true, + } as ModelInfo; +} + +/** A provider as the server describes it: config providers are usable and + * carry their config's endpoint. */ +function providerFromConfig(id: string, entry: FakeProviderConfig): ProviderInfo { + return { + id, + name: entry.name ?? id, + activation: 'enabled', + package: entry.package ?? '@opencode/ai/providers/openai-compatible', + settings: { ...(entry.settings ?? {}) }, + } as ProviderInfo; +} + +export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServer { + const at = { location: { directory: '/tmp' } }; + const events: FakeEvent[] = []; + let ended = false; + let failure: unknown; + const waiters: Array<() => void> = []; + let resolveScripted!: () => void; + const scripted = new Promise((resolve) => (resolveScripted = resolve)); + const wake = () => waiters.splice(0).forEach((waiter) => waiter()); + + const providers = new Map(); + const models = new Map(); + const usable = new Set(); + for (const entry of options.providers ?? []) { + providers.set(entry.info.id, entry.info); + if (entry.usable !== false) usable.add(entry.info.id); + for (const model of entry.models ?? []) models.set(`${model.providerID}/${model.modelID}`, model); + } + for (const [id, entry] of Object.entries(options.config?.providers ?? {})) { + const provider = providerFromConfig(id, entry); + providers.set(id, provider); + usable.add(id); + for (const [modelID, model] of Object.entries(entry.models ?? {})) { + const listed = modelFromConfig(id, modelID, model); + models.set(`${id}/${modelID}`, listed); + } + } + + const pluginConfig = options.pluginConfig ?? []; + const plugins: FakePlugin[] = options.plugins ?? [ + { id: 'opencode.config.mcp', source: { type: 'builtin' }, state: { status: 'active' } }, + { id: 'opencode.plan', source: { type: 'builtin' }, state: { status: 'active' } }, + ]; + + const mcpConfig = options.mcpConfig ?? {}; + const mcp = new Map( + options.mcpServers + ? options.mcpServers.map((server) => [server.name, { ...server }] as const) + : Object.entries(mcpConfig).map(([name, entry]) => [name, { name, status: entry.disabled ? 'disabled' : 'connected' }] as const), + ); + + const integrationRows = options.integrationRows ?? [ + ...INTEGRATIONS, + ...[...providers.keys()].filter((id) => !INTEGRATIONS.some((i) => i.id === id)).map((id) => ({ id, name: id, methods: [], connections: [] })), + ...(options.integrations ?? []).map((id) => ({ id, name: id, methods: [], connections: [] })), + ]; + const integrations = integrationRows.map((row) => ({ ...row, methods: [...row.methods], connections: [...row.connections] })); + + const credentials: CredentialEntry[] = (options.credentials ?? []).map((credential) => ({ ...credential })); + const signIns = new Map((options.signIns ?? []).map((attempt) => [attempt.attemptID, attempt])); + + const createdSessions = new Map(); + if (options.session?.id) createdSessions.set(options.session.id, options.session as SessionInfo); + + const agents = (call: number): FakeAgentRow[] => + typeof options.agents === 'function' ? options.agents(call) : options.agents ?? OPENCODE_AGENTS_WITH_REVIEW; + let agentListCalls = 0; + + const agentInfo = (row: FakeAgentRow): AgentInfo => + ({ + id: row.id, + name: row.name, + mode: row.mode, + hidden: row.hidden, + ...(row.description ? { description: row.description } : {}), + ...(row.color ? { color: row.color } : {}), + request: {}, + permissions: row.rules ?? [], + }) as unknown as AgentInfo; + + let providerListCalls = 0; + const registration = options.providerRegistration ?? 0; + + const client = { + server: { + info: vi.fn(async () => { + if (options.serverInfoError) throw options.serverInfoError; + return { version: options.version ?? '2.1.4' }; + }), + }, + location: { get: vi.fn(async () => ({ directory: '/tmp' })), reload: vi.fn(async () => undefined) }, + agent: { + list: vi.fn(async () => { + const rows = agents(agentListCalls++); + return { ...at, data: rows.map(agentInfo) }; + }), + get: vi.fn(async ({ agentID }: { agentID: string }): Promise => { + const row = agents(0).find((a) => a.id === agentID); + if (!row) throw Object.assign(new Error(`no agent ${agentID}`), { name: 'AgentNotFoundError' }); + return { ...at, data: agentInfo(row) }; + }), + }, + plugin: { + list: vi.fn(async () => ({ ...at, data: plugins })), + check: vi.fn(async ({ target }: { target?: string }) => { + if (!target || !plugins.some((p) => p.source.type === 'package' && p.source.target === target)) { + throw Object.assign(new Error(`Plugin package is not in the current server inventory: ${target ?? ''}`), { name: 'InvalidRequestError' }); + } + return { ...at, data: plugins }; + }), + update: vi.fn(async ({ targets }: { targets: ReadonlyArray }) => { + const missing = targets.find((target) => !plugins.some((p) => p.source.type === 'package' && p.source.target === target)); + if (missing) { + throw Object.assign(new Error(`Plugin packages are not in the current server inventory: ${missing}`), { name: 'InvalidRequestError' }); + } + }), + }, + session: { + create: vi.fn(async (input: Record = {}) => { + const id = (input.id as string | undefined) ?? options.sessionId ?? 'ses_1'; + const session = { + id, + projectID: 'p', + ...(input.model ? { model: input.model } : {}), + ...(input.agent ? { agent: input.agent } : {}), + ...(input.permissions ? { permissions: input.permissions } : {}), + ...(input.title ? { title: input.title } : {}), + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + time: { created: 0, updated: 0 }, + ...(input.location ? { location: input.location } : at), + ...options.session, + } as unknown as SessionInfo; + createdSessions.set(id, session); + return session; + }), + get: vi.fn(async ({ sessionID }: { sessionID: string }) => { + const session = createdSessions.get(sessionID); + if (!session) throw Object.assign(new Error(`Session not found: ${sessionID}`), { name: 'SessionNotFoundError' }); + return session; + }), + remove: vi.fn(async () => undefined), + update: vi.fn(async () => undefined), + switchAgent: vi.fn(async () => undefined), + switchModel: vi.fn(async () => undefined), + interrupt: vi.fn(async () => undefined), + prompt: vi.fn(async () => ({})), + command: vi.fn(async () => undefined), + compact: vi.fn(async () => ({})), + diff: vi.fn(async () => []), + revert: { stage: vi.fn(async () => ({})), clear: vi.fn(async () => undefined), commit: vi.fn(async () => undefined) }, + form: { reply: vi.fn(async () => undefined), cancel: vi.fn(async () => undefined) }, + }, + shell: { + list: vi.fn(async () => ({ ...at, data: [] })), + create: vi.fn(async () => ({ ...at, data: { id: 'sh_1' } })), + get: vi.fn(async () => ({ ...at, data: { id: 'sh_1' } })), + output: vi.fn(async () => ({ ...at, data: { output: '', cursor: 0, size: 0, truncated: false } })), + remove: vi.fn(async () => undefined), + }, + permission: { reply: vi.fn(async () => undefined) }, + message: { list: vi.fn(async () => ({ data: [], cursor: {} })) }, + command: { list: vi.fn(async () => ({ ...at, data: [] })) }, + mcp: { + list: vi.fn(async () => ({ + ...at, + data: [...mcp.values()].sort((a, b) => a.name.localeCompare(b.name)).map((server) => ({ + name: server.name, + status: server.status === 'failed' || server.status === 'needs_auth' ? { status: server.status, error: server.error ?? 'failed' } : { status: server.status }, + })) as McpServer[], + })), + add: vi.fn(async ({ server, config }: { server: string; config: Record }) => { + // 2.1.4 keeps an added server in the running inventory only: it is + // not written to the config document or the file. + mcp.set(server, { name: server, status: 'connected' }); + }), + remove: vi.fn(async ({ server }: { server: string }) => { + if (!mcp.has(server)) throw Object.assign(new Error(`MCP server not found: ${server}`), { name: 'McpServerNotFoundError' }); + // Removing does not rewrite the config file; a restart brings a + // config-file server back. + mcp.delete(server); + }), + connect: vi.fn(async ({ server }: { server: string }) => { + const found = mcp.get(server); + if (!found) throw Object.assign(new Error(`MCP server not found: ${server}`), { name: 'McpServerNotFoundError' }); + found.status = 'connected'; + }), + disconnect: vi.fn(async ({ server }: { server: string }) => { + const found = mcp.get(server); + if (!found) throw Object.assign(new Error(`MCP server not found: ${server}`), { name: 'McpServerNotFoundError' }); + found.status = 'disabled'; + }), + }, + config: { + get: vi.fn(async () => [ + { + type: 'document', + path: '/tmp/opencode/opencode.json', + info: { + ...(Object.keys(mcpConfig).length > 0 ? { mcp: { servers: mcpConfig } } : {}), + ...(pluginConfig.length > 0 ? { plugins: pluginConfig } : {}), + }, + }, + { type: 'directory', path: '/tmp/opencode', info: {} }, + ]), + shells: vi.fn(async () => []), + update: vi.fn(async () => undefined), + }, + provider: { + list: vi.fn(async () => { + if (providerListCalls++ < registration) return { ...at, data: [] }; + return { ...at, data: [...providers.values()].filter((provider) => usable.has(provider.id)) }; + }), + get: vi.fn(async ({ providerID }: { providerID: string }) => { + const provider = providers.get(providerID); + if (!provider) throw Object.assign(new Error(`Provider not found: ${providerID}`), { name: 'ProviderNotFoundError' }); + return { ...at, data: provider }; + }), + }, + model: { + list: vi.fn(async () => ({ ...at, data: [...models.values()].filter((model) => usable.has(model.providerID)) })), + default: vi.fn(async () => ({ ...at, data: null })), + }, + integration: { + list: vi.fn(async () => ({ ...at, data: integrations.map((row) => ({ ...row, methods: [...row.methods], connections: [...row.connections] })) })), + get: vi.fn(async ({ integrationID }: { integrationID: string }) => { + const row = integrations.find((integration) => integration.id === integrationID); + if (!row) throw Object.assign(new Error(`Integration not found: ${integrationID}`), { name: 'IntegrationNotFoundError' }); + return { ...at, data: { ...row, methods: [...row.methods], connections: [...row.connections] } }; + }), + connect: { + key: vi.fn(async ({ integrationID, key, label }: { integrationID: string; key: string; label?: string }) => { + const row = integrations.find((integration) => integration.id === integrationID); + if (!row) throw Object.assign(new Error(`Integration not found: ${integrationID}`), { name: 'IntegrationNotFoundError' }); + // One active credential per integration. + for (const credential of credentials) if (credential.integrationID === integrationID) credential.active = false; + const credential = { + id: `cred_${credentials.length + 1}`, + integrationID, + label: label ?? integrationID, + active: true, + value: { type: 'key', key }, + } as CredentialEntry; + credentials.push(credential); + row.connections.push({ type: 'credential', id: credential.id, label: credential.label, method: 'key' }); + }), + external: vi.fn(async () => undefined), + }, + oauth: { + connect: vi.fn(async ({ integrationID, methodID }: { integrationID: string; methodID?: string }) => { + const id = `con_${signIns.size + 1}`; + const attempt: FakeSignIn = { + attemptID: id, + url: options.signIns?.[0]?.url ?? 'https://github.com/login/device?user_code=ABCD', + instructions: options.signIns?.[0]?.instructions ?? 'Enter code: ABCD', + mode: 'auto', + created: 0, + expires: 10_000, + status: 'pending', + }; + signIns.set(id, attempt); + return { ...at, data: { attemptID: id, url: attempt.url, instructions: attempt.instructions, mode: attempt.mode, time: { created: attempt.created!, expires: attempt.expires! } } }; + }), + status: vi.fn(async ({ attemptID }: { attemptID: string }) => { + const attempt = signIns.get(attemptID); + if (!attempt) throw Object.assign(new Error(`OAuth attempt not found: ${attemptID}`), { name: 'IntegrationAttemptNotFoundError' }); + return { + ...at, + data: { + status: attempt.status, + ...(attempt.status === 'failed' ? { message: attempt.message ?? 'sign-in failed' } : {}), + time: { created: attempt.created ?? 0, expires: attempt.expires ?? 10_000 }, + }, + }; + }), + complete: vi.fn(async ({ attemptID }: { attemptID: string }) => { + const attempt = signIns.get(attemptID); + if (attempt) attempt.status = 'complete'; + }), + cancel: vi.fn(async ({ attemptID }: { attemptID: string }) => { + signIns.delete(attemptID); + }), + }, + command: { + connect: vi.fn(async () => ({ ...at, data: { attemptID: 'cmd_1', time: { created: 0, expires: 10_000 } } })), + status: vi.fn(async () => ({ ...at, data: { status: 'pending', time: { created: 0, expires: 10_000 } } })), + cancel: vi.fn(async () => undefined), + }, + }, + credential: { + list: vi.fn(async () => credentials.map((credential) => ({ ...credential }))), + create: vi.fn(async (input: Record) => { + const credential = { id: `cred_${credentials.length + 1}`, active: false, ...input } as CredentialEntry; + credentials.push(credential); + return credential; + }), + update: vi.fn(async () => undefined), + activate: vi.fn(async ({ credentialID }: { credentialID: string }) => { + const target = credentials.find((credential) => credential.id === credentialID); + if (!target) return; // 2.1.4 answers an unknown id with success (verified). + for (const credential of credentials) if (credential.integrationID === target.integrationID) credential.active = false; + target.active = true; + }), + remove: vi.fn(async ({ credentialID }: { credentialID: string }) => { + const index = credentials.findIndex((credential) => credential.id === credentialID); + if (index < 0) return; + const [removed] = credentials.splice(index, 1); + const row = integrations.find((integration) => integration.id === removed!.integrationID); + if (row) row.connections = row.connections.filter((connection) => connection.type !== 'credential' || connection.id !== credentialID); + // The server leaves another of that integration active. + if (removed!.active) { + const next = credentials.find((credential) => credential.integrationID === removed!.integrationID); + if (next) next.active = true; + } + }), + }, + event: { + subscribe: vi.fn((request: { signal?: AbortSignal } | undefined) => stream(request?.signal)), + }, + } as unknown as OpenCodeClient; + + const server: FakeOpenCodeServer = { + client: { ...client, ...(options.over ?? {}) } as OpenCodeClient, + state: { providers, models, createdSessions, plugins, mcpConfig, mcp, integrations, credentials, signIns, events }, + push: (event) => { + events.push(event); + wake(); + }, + endStream: () => { + ended = true; + wake(); + }, + failStream: (error) => { + failure = error; + ended = true; + wake(); + }, + scripted: () => scripted, + settled: async () => { + await scripted; + await new Promise((resolve) => setImmediate(resolve)); + }, + setSignIn: (attemptID, status, message) => { + const attempt = signIns.get(attemptID); + if (!attempt) throw new Error(`no sign-in ${attemptID}`); + attempt.status = status; + if (message) attempt.message = message; + }, + }; + + async function* stream(signal?: AbortSignal): AsyncGenerator { + const onAbort = () => { + ended = true; + wake(); + }; + signal?.addEventListener('abort', onAbort, { once: true }); + try { + // 2.1.4's first event, and why subscribing before creating a session + // does not miss it: the request opens on subscribe, not on first read. + yield { type: 'server.connected', data: {} } as FakeEvent; + for (const event of options.events ?? []) { + if (ended) return; + yield event; + } + resolveScripted(); + if (options.holdStream === false) { + // A test convenience: the script is over, and one macrotask lets the + // driver's off-stream ask handlers run before the close. + await new Promise((resolve) => setImmediate(resolve)); + ended = true; + } + for (;;) { + if (events.length > 0) { + yield events.shift()!; + continue; + } + if (failure !== undefined) throw failure; + if (ended) return; + await new Promise((resolve) => waiters.push(resolve)); + } + } finally { + signal?.removeEventListener('abort', onAbort); + } + } + + return server; +} + +/** The agents whose ids are OpenCode's built-ins, for tests that model a + * directory's agents loading after them. */ +export function builtinAgentIds(): Set { + return new Set(BUILTIN_IDS); +} diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeMcp.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeMcp.test.ts index ea4476f2..4cf156cc 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/opencodeMcp.test.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeMcp.test.ts @@ -11,7 +11,8 @@ function server(config: Record>, status: Entry[] const servers = [...status]; const client = { config: { - get: vi.fn(async () => [{ type: 'document', info: { mcp: config } }]), + // 2.1.4 reads the file's `mcp` map back as `{servers: …}`. + get: vi.fn(async () => [{ type: 'document', info: { mcp: { servers: config } } }]), }, mcp: { list: vi.fn(async () => ({ location: { directory: '/w' }, data: servers })), diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeVerifiedServer.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeVerifiedServer.test.ts new file mode 100644 index 00000000..4d77f084 --- /dev/null +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeVerifiedServer.test.ts @@ -0,0 +1,93 @@ +/** + * Behaviours the probes against OpenCode 2.1.4 established that the driver + * did not follow: the config document's MCP section, a server that only + * lives in the running inventory, the status word a disconnect leaves, and + * a package plugin named by its source rather than by an `id`. + * + * These go through the driver's public managers, over the verified fake, so + * breaking the driver here is what a real server would show. + */ +import { mkdtemp, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import * as path from 'node:path'; +import { describe, expect, it } from 'vitest'; +import { OpenCodeMcp } from '../mcp'; +import { OpenCodePlugins } from '../plugins'; +import { fakeOpenCode } from './fakeServer'; + +const serverOf = () => + fakeOpenCode({ + mcpConfig: { + fs: { type: 'local', command: ['npx', '-y', 'srv'], environment: { API_KEY: 'k' } }, + }, + }); + +describe('MCP servers as 2.1.4 holds them', () => { + it('reads the config document\'s `servers` map, which is not the file\'s bare map', async () => { + const server = serverOf(); + const mcp = new OpenCodeMcp(async () => server.client); + const state = await mcp.list(); + // The config file spells `mcp: {fs: …}`; `config.get` reads it back as + // `mcp: {servers: {fs: …}}`. Reading the section itself lists nothing. + expect(state).toEqual({ + servers: [{ name: 'fs', transport: 'stdio', target: 'npx', envKeys: ['API_KEY'], enabled: true }], + toggles: true, + }); + }); + + it('lists a server it added, which lives in the running inventory only', async () => { + const server = serverOf(); + const mcp = new OpenCodeMcp(async () => server.client); + const state = await mcp.act('add', [{ name: 'gh', setup: { type: 'http', url: 'https://new/mcp' } }], []); + // `mcp.add` does not write the config document (verified); the server's + // inventory is where the new server is. + expect(server.state.mcpConfig).toEqual({ fs: { type: 'local', command: ['npx', '-y', 'srv'], environment: { API_KEY: 'k' } } }); + expect(state.servers.map((s) => s.name)).toEqual(['fs', 'gh']); + expect(state.servers.find((s) => s.name === 'gh')).toMatchObject({ transport: 'http', target: 'https://new/mcp', enabled: true }); + }); + + it('reports a server the API disconnected as disabled, where the config keeps saying nothing', async () => { + const server = serverOf(); + const mcp = new OpenCodeMcp(async () => server.client); + const state = await mcp.act('disable', [], ['fs']); + // `mcp.disconnect` changes the inventory's status; the config entry is + // untouched, so its `disabled` flag cannot be the switch. + expect(server.state.mcpConfig.fs).toEqual({ type: 'local', command: ['npx', '-y', 'srv'], environment: { API_KEY: 'k' } }); + expect(state.servers[0]).toMatchObject({ name: 'fs', enabled: false }); + expect((await mcp.act('enable', [], ['fs'])).servers[0]).toMatchObject({ name: 'fs', enabled: true }); + }); + + it('drops a removed server from the list for this server, though its file entry stays', async () => { + const server = serverOf(); + const mcp = new OpenCodeMcp(async () => server.client); + const state = await mcp.act('remove', [], ['fs']); + // `mcp.remove` does not rewrite the file; a restart brings the config + // server back, and so does a new manager over a new client. + expect(server.state.mcpConfig).toHaveProperty('fs'); + expect(state.servers).toEqual([]); + // Adding the same name again puts it back in the inventory. + const added = await mcp.act('add', [{ name: 'fs', setup: { type: 'stdio', command: 'uvx', args: ['srv'] } }], []); + expect(added.servers).toEqual([{ name: 'fs', transport: 'stdio', target: 'uvx', enabled: true }]); + }); +}); + +describe('plugins as 2.1.4 reports them', () => { + it('matches a failed package by its source target, because a package entry has no id', async () => { + const dir = await mkdtemp(path.join(tmpdir(), 'oc-plugins-')); + const file = path.join(dir, 'opencode.json'); + await writeFile(file, JSON.stringify({ plugins: ['@acme/oc-plugin'] })); + const server = fakeOpenCode({ + plugins: [ + { + source: { type: 'package', target: '@acme/oc-plugin' }, + state: { status: 'failed', error: 'Plugin failed to load', ref: 'err_1' }, + }, + ], + }); + const manager = new OpenCodePlugins(async () => server.client, () => [file]); + const state = await manager.list(); + expect(state.installed).toEqual([ + { id: '@acme/oc-plugin', name: '@acme/oc-plugin', enabled: false, error: 'Plugin failed to load' }, + ]); + }); +}); diff --git a/packages/agent-host/src/drivers/opencode/mcp.ts b/packages/agent-host/src/drivers/opencode/mcp.ts index 08a7c994..4bac0344 100644 --- a/packages/agent-host/src/drivers/opencode/mcp.ts +++ b/packages/agent-host/src/drivers/opencode/mcp.ts @@ -4,9 +4,13 @@ * * V2 serves the whole lifecycle over its API — list, add, remove, connect, * disconnect — so nothing here edits a config file by hand any more (v1 had - * no way to delete a server, and rewrote the global config itself). A config - * change reloads the projects OpenCode has open, which restarts their running - * sessions, as for any change to its config. + * no way to delete a server, and rewrote the global config itself). Its + * running inventory and its config file are not the same thing (verified + * against 2.1.4): `add` and `remove` change the inventory only, so a removed + * config server comes back on the next start and an added one is gone; a + * `disconnect` is the inventory's `disabled` status, not a config flag. This + * manager therefore holds the adds and removes of the current server, and + * reads the switch from `mcp.list`. * * In a session, a server is connected or disconnected for the session's * project — OpenCode has no narrower scope — so the switch is project-wide. @@ -52,31 +56,50 @@ export function openCodeServers(mcp: unknown): McpServerInfo[] { export class OpenCodeMcp implements McpManager { /** One MCP change at a time: each reads the list, then writes it. */ private queue: Promise = Promise.resolve(); + /** Servers added through the API in this server's lifetime. V2 keeps an + * added server in its running inventory and not in the config document + * (verified against 2.1.4), so the manager is the only other place that + * knows what they are. */ + private readonly added = new Map>(); + /** Servers removed through the API: a config-file server leaves the + * running inventory but not the file, so it would otherwise be listed + * again at once. A new server (and so a restart) clears both. */ + private readonly removed = new Set(); + /** The client these two sets belong to. */ + private lastClient: OpenCodeClient | undefined; constructor(private readonly client: () => Promise) {} list(): Promise { - return this.serial(async () => this.state(await this.client())); + return this.serial(async () => { + const client = await this.client(); + this.forClient(client); + return this.state(client); + }); } act(action: McpAction, servers: McpServerAdd[], names: string[]): Promise { return this.serial(async () => { const client = await this.client(); + this.forClient(client); const current = await this.known(client); if (action === 'remove') { const missing = names.find((n) => !current.has(n)); if (missing) throw new Error(`OpenCode has no MCP server '${missing}'.`); for (const name of names) await this.call(`remove ${name}`, () => client.mcp.remove({ server: name })); + for (const name of names) { + this.added.delete(name); + this.removed.add(name); + } } else if (action === 'add') { // A refusal can quote the config it was handed — env, headers and // all. The setup's credential values are removed before it travels. const secrets = servers.flatMap((add) => setupSecrets(add.setup)); for (const add of servers) { - await this.call( - `add ${add.name}`, - () => client.mcp.add({ server: add.name, config: openCodeServerConfig(add) as never }), - secrets, - ); + const config = openCodeServerConfig(add); + await this.call(`add ${add.name}`, () => client.mcp.add({ server: add.name, config: config as never }), secrets); + this.added.set(add.name, config); + this.removed.delete(add.name); } } else { const missing = names.find((n) => !current.has(n)); @@ -114,20 +137,43 @@ export class OpenCodeMcp implements McpManager { return next; } + /** Forget what belongs to a server process that has been replaced. */ + private forClient(client: OpenCodeClient): void { + if (this.lastClient === client) return; + this.added.clear(); + this.removed.clear(); + this.lastClient = client; + } + private async state(client: OpenCodeClient): Promise { + // What was added or removed belongs to one server process: a replaced + // server has started from its config again. + this.forClient(client); // The server lists which servers exist; only the config says what each // one is (its command, its URL), so both are read. V2's config answer is - // a list of documents whose shape is the config file's, read loosely - // here for the one section that matters. + // a list of documents, and its `mcp` section holds the servers under + // `servers` — the file's bare `mcp` map is not the shape of the answer. const [config, listed] = await Promise.all([ client.config.get().catch(() => []), client.mcp.list().catch((err: unknown) => { throw new Error(`OpenCode could not report its MCP servers: ${reason(err)}`); }), ]); - const documents = config as unknown as Array<{ info?: { mcp?: Record } }>; - const mcp = documents.find((entry) => entry.info?.mcp)?.info?.mcp ?? {}; - return { servers: openCodeServers(mcp), toggles: true }; + const documents = config as unknown as Array<{ type?: string; info?: { mcp?: { servers?: Record } } }>; + const configured = documents.find((entry) => entry.info?.mcp?.servers !== undefined)?.info?.mcp?.servers ?? {}; + // A server added through the API is not in the config document; the + // manager's record stands in for it, and a removed one is kept out until + // this server process starts again. + const known = new Map(); + for (const [name, entry] of Object.entries(configured)) if (!this.removed.has(name)) known.set(name, entry); + for (const [name, entry] of this.added) known.set(name, entry); + // The switch is the inventory's status, not a flag in the config: a + // `disconnect` leaves the config entry as it was (verified against 2.1.4). + const off = new Set(listed.data.filter((server) => server.status.status === 'disabled').map((server) => server.name)); + return { + servers: openCodeServers(Object.fromEntries(known)).map((server) => (off.has(server.name) ? { ...server, enabled: false } : server)), + toggles: true, + }; } } diff --git a/packages/agent-host/src/drivers/opencode/plugins.ts b/packages/agent-host/src/drivers/opencode/plugins.ts index bfe257a3..ced69065 100644 --- a/packages/agent-host/src/drivers/opencode/plugins.ts +++ b/packages/agent-host/src/drivers/opencode/plugins.ts @@ -12,7 +12,9 @@ * * OpenCode has no marketplaces, so a plugin is installed by its package * name. V2 serves an explicit update, which the phone can ask for; OpenCode - * also updates its own plugins when it loads them. + * also updates its own plugins when it loads them. Its `plugin.list` names + * a package plugin by `source.target` (a package entry has no `id`; verified + * against 2.1.4), which is the spec this file's `plugins` list holds. */ import { existsSync } from 'node:fs'; import { readFile, writeFile } from 'node:fs/promises'; @@ -154,7 +156,18 @@ export class OpenCodePlugins implements PluginManager { // reads as failed rather than as a config line that looks fine. const loaded = await this.client() .then((client) => client.plugin.list()) - .then((listed) => new Map(listed.data.map((p) => [p.id, p.state]))) + .then( + (listed) => + new Map( + listed.data.flatMap((plugin) => { + // A package plugin has no `id`: 2.1.4 names it by its source + // target, which is the package spec the config lists. Built-in + // plugins (§ the rest of the list) do have one. + const id = plugin.id ?? (plugin.source.type === 'package' ? plugin.source.target : undefined); + return id ? [[id, plugin.state] as const] : []; + }), + ), + ) .catch(() => new Map()); const plugins = entries .map(toInstalledPlugin) From 6f5da00f39f43911b96271c3f430c0ac5be5a233 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:00:19 +0100 Subject: [PATCH 13/42] test(opencode): one verified fake server for every OpenCode test The driver, catalog, single-start, provider, plugin, MCP, account and host tests each carried their own fake client, and they disagreed with OpenCode 2.1.4 where it mattered: a config document's MCP section, a package plugin with no id, a sign-in status that stays complete, and an event stream that stays open after a turn. They now share fakeServer.ts, whose header lists the probes behind it. Each test keeps its intent: the driver tests still run to the end of the session, through the fake's explicit close (or failure), and a turn is scripted with 2.1.4's own ending (session.execution.succeeded), while the session.idle fixtures remain as the other ending the driver supports. The catalog and single-start servers derive their providers and models from the config the driver hands them, as the real server does. No assertion was weakened: the account tests now check the active flag a credential switch really changes, and the lifecycle tests keep the stream open, abort it cleanly, and kill the server under it. Co-Authored-By: Claude Code --- .../drivers/opencode/__tests__/fakeServer.ts | 87 +++++++++--- .../__tests__/opencodeAccounts.test.ts | 130 +++++++++-------- .../__tests__/opencodeCatalog.test.ts | 54 ++++--- .../opencode/__tests__/opencodeDriver.test.ts | 134 ++++++++++-------- .../opencode/__tests__/opencodeMcp.test.ts | 108 +++++--------- .../__tests__/opencodePlugins.test.ts | 20 ++- .../__tests__/opencodeProviders.test.ts | 89 ++++++------ .../__tests__/opencodeSingleStart.test.ts | 84 ++++------- .../src/host/__tests__/opencodeServer.test.ts | 102 ++++--------- 9 files changed, 361 insertions(+), 447 deletions(-) diff --git a/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts b/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts index 9eca9c5e..f0f52794 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts @@ -61,12 +61,17 @@ import type { PluginInfo, ProviderInfo, SessionInfo, - V2Event, } from '@opencode/client'; /** An event as the client's async iterator yields it; `type` is the switch - * the driver reads. */ -export type FakeEvent = V2Event & { type: string; data?: Record; location?: { directory?: string } }; + * the driver reads. The rest of a 2.1.4 event (`id`, `created`, `durable`) + * is not read by the driver, so a fixture may state only what it needs. */ +export type FakeEvent = { + type: string; + data?: Record; + location?: { directory?: string }; + [key: string]: unknown; +}; export interface FakeAgentRow { id: string; @@ -118,6 +123,10 @@ export interface FakeServerOptions { providerRegistration?: number; /** Integrations that are no model provider (web search, for one). */ integrations?: string[]; + /** Providers whose `provider.get` fails, as a request can. */ + providerGetFails?: Set; + /** Per-provider delay before `provider.get` answers, for answer order. */ + providerGetDelayMs?: Record; /** Agent rows `agent.list` answers with. A function sees the call number, * for OpenCode's built-ins-then-directory-agents order. */ agents?: FakeAgentRow[] | ((call: number) => FakeAgentRow[]); @@ -272,14 +281,17 @@ function modelFromConfig(providerID: string, modelID: string, entry: FakeModelCo } /** A provider as the server describes it: config providers are usable and - * carry their config's endpoint. */ -function providerFromConfig(id: string, entry: FakeProviderConfig): ProviderInfo { + * carry their config's endpoint. A config entry naming a provider the + * server already knows keeps that provider's endpoint (and package) and + * overrides only what the entry states. */ +function providerFromConfig(id: string, entry: FakeProviderConfig, base: ProviderInfo | undefined): ProviderInfo { return { + ...(base ?? {}), id, - name: entry.name ?? id, + name: entry.name ?? base?.name ?? id, activation: 'enabled', - package: entry.package ?? '@opencode/ai/providers/openai-compatible', - settings: { ...(entry.settings ?? {}) }, + package: entry.package ?? base?.package ?? '@opencode/ai/providers/openai-compatible', + settings: { ...(base?.settings ?? {}), ...(entry.settings ?? {}) }, } as ProviderInfo; } @@ -302,7 +314,7 @@ export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServe for (const model of entry.models ?? []) models.set(`${model.providerID}/${model.modelID}`, model); } for (const [id, entry] of Object.entries(options.config?.providers ?? {})) { - const provider = providerFromConfig(id, entry); + const provider = providerFromConfig(id, entry, providers.get(id)); providers.set(id, provider); usable.add(id); for (const [modelID, model] of Object.entries(entry.models ?? {})) { @@ -312,10 +324,20 @@ export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServe } const pluginConfig = options.pluginConfig ?? []; - const plugins: FakePlugin[] = options.plugins ?? [ - { id: 'opencode.config.mcp', source: { type: 'builtin' }, state: { status: 'active' } }, - { id: 'opencode.plan', source: { type: 'builtin' }, state: { status: 'active' } }, - ]; + const pluginSpec = (entry: (typeof pluginConfig)[number]) => (typeof entry === 'string' ? entry : entry.package); + const plugins: FakePlugin[] = + options.plugins ?? + [ + { id: 'opencode.config.mcp', source: { type: 'builtin' }, state: { status: 'active' } }, + { id: 'opencode.plan', source: { type: 'builtin' }, state: { status: 'active' } }, + // A config package the server loaded is in the inventory under its + // source target; a `-name` entry is switched off and not loaded. + ...pluginConfig.flatMap((entry): FakePlugin[] => { + const spec = pluginSpec(entry); + if (!spec || spec.startsWith('-')) return []; + return [{ source: { type: 'package', target: spec }, state: { status: 'active' } }]; + }), + ]; const mcpConfig = options.mcpConfig ?? {}; const mcp = new Map( @@ -332,7 +354,9 @@ export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServe const integrations = integrationRows.map((row) => ({ ...row, methods: [...row.methods], connections: [...row.connections] })); const credentials: CredentialEntry[] = (options.credentials ?? []).map((credential) => ({ ...credential })); - const signIns = new Map((options.signIns ?? []).map((attempt) => [attempt.attemptID, attempt])); + const signInTemplates = options.signIns ?? []; + let signInTemplate = 0; + const signIns = new Map(signInTemplates.map((attempt) => [attempt.attemptID, { ...attempt }])); const createdSessions = new Map(); if (options.session?.id) createdSessions.set(options.session.id, options.session as SessionInfo); @@ -487,6 +511,9 @@ export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServe return { ...at, data: [...providers.values()].filter((provider) => usable.has(provider.id)) }; }), get: vi.fn(async ({ providerID }: { providerID: string }) => { + if (options.providerGetFails?.has(providerID)) throw new Error(`fetch failed for ${providerID}`); + const delay = options.providerGetDelayMs?.[providerID]; + if (delay) await new Promise((resolve) => setTimeout(resolve, delay)); const provider = providers.get(providerID); if (!provider) throw Object.assign(new Error(`Provider not found: ${providerID}`), { name: 'ProviderNotFoundError' }); return { ...at, data: provider }; @@ -523,15 +550,20 @@ export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServe }, oauth: { connect: vi.fn(async ({ integrationID, methodID }: { integrationID: string; methodID?: string }) => { - const id = `con_${signIns.size + 1}`; + // A scripted attempt (the test's own URL and id) is used first, as + // the server would hand back the one it started. + const template = signInTemplates[signInTemplate]; + if (template) signInTemplate++; + const id = template?.attemptID ?? `con_${signIns.size + 1}`; const attempt: FakeSignIn = { + ...(template ?? {}), attemptID: id, - url: options.signIns?.[0]?.url ?? 'https://github.com/login/device?user_code=ABCD', - instructions: options.signIns?.[0]?.instructions ?? 'Enter code: ABCD', - mode: 'auto', - created: 0, - expires: 10_000, - status: 'pending', + url: template?.url ?? 'https://github.com/login/device?user_code=ABCD', + instructions: template?.instructions ?? 'Enter code: ABCD', + mode: template?.mode ?? 'auto', + created: template?.created ?? 0, + expires: template?.expires ?? 10_000, + status: template?.status ?? 'pending', }; signIns.set(id, attempt); return { ...at, data: { attemptID: id, url: attempt.url, instructions: attempt.instructions, mode: attempt.mode, time: { created: attempt.created!, expires: attempt.expires! } } }; @@ -594,8 +626,19 @@ export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServe }, } as unknown as OpenCodeClient; + // A per-method replacement, one level in: `{ session: { get } }` keeps the + // rest of the session methods the fake already has. + const withOverrides: Record = { ...(client as unknown as Record) }; + for (const [group, value] of Object.entries(options.over ?? {})) { + const base = withOverrides[group]; + withOverrides[group] = + value && typeof value === 'object' && !Array.isArray(value) && base && typeof base === 'object' + ? { ...(base as Record), ...(value as Record) } + : value; + } + const server: FakeOpenCodeServer = { - client: { ...client, ...(options.over ?? {}) } as OpenCodeClient, + client: withOverrides as unknown as OpenCodeClient, state: { providers, models, createdSessions, plugins, mcpConfig, mcp, integrations, credentials, signIns, events }, push: (event) => { events.push(event); diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeAccounts.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeAccounts.test.ts index f55191f9..1e94785c 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/opencodeAccounts.test.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeAccounts.test.ts @@ -2,71 +2,32 @@ * OpenCode's provider accounts: the list a phone sees (no secret in it), * a key, a device sign-in watched to its end, and a sign-in finished by * pasting back its loopback address — only that address. + * + * The server's side is the shared fake: its `oauth.status` is stateful, as + * 2.1.4's is, so a sign-in completes when the fake is told it did, not by a + * queued answer that becomes `pending` again on the next poll. */ import { describe, it, expect, vi } from 'vitest'; -import type { OpenCodeClient } from '@opencode/client'; import { OpenCodeAccounts, loopbackCallback } from '../accounts'; import type { SignInState } from '../../../sdk/driver'; +import { fakeOpenCode, type FakeOpenCodeServer } from './fakeServer'; -const integrations = [ - { - id: 'nano-gpt', - name: 'NanoGPT', - methods: [{ type: 'key' }, { type: 'env', names: ['NANO_GPT_API_KEY'] }], - connections: [{ type: 'credential', id: 'cred_1', label: 'work', method: 'key' }], - }, - { - id: 'github-copilot', - name: 'GitHub Copilot', - methods: [ - { type: 'env', names: ['GITHUB_TOKEN'] }, - { - id: 'device', - type: 'oauth', - label: 'Login with GitHub Copilot', - form: [ - { key: 'deploymentType', title: 'Deployment', required: true, type: 'string', options: [{ value: 'github.com', label: 'GitHub.com' }] }, - { key: 'enterpriseUrl', title: 'Enterprise URL', required: true, type: 'string', when: [{ key: 'deploymentType', op: 'eq', value: 'enterprise' }] }, - { key: 'server', hidden: true, type: 'string', default: 'https://x' }, - ], - }, - ], - connections: [{ type: 'env', name: 'GITHUB_TOKEN' }], - }, - { id: 'openai', name: 'OpenAI', methods: [{ type: 'key' }, { id: 'chatgpt', type: 'oauth', label: 'Sign in with ChatGPT' }], connections: [] }, -]; +const secret = { type: 'key', key: 'sk-SECRET' } as const; -function fakeClient(attemptUrl = 'https://github.com/login/device?user_code=ABCD') { - const statuses: string[] = []; - const client = { - integration: { - list: vi.fn(async () => ({ data: integrations })), - connect: { key: vi.fn(async () => undefined) }, - oauth: { - connect: vi.fn(async () => ({ - data: { attemptID: 'con_1', url: attemptUrl, instructions: 'Enter code: ABCD', mode: 'auto', time: { created: 0, expires: 10_000 } }, - })), - status: vi.fn(async () => ({ data: { status: statuses.shift() ?? 'pending', time: { created: 0, expires: 10_000 } } })), - cancel: vi.fn(async () => undefined), - }, - }, - credential: { - // The real list carries the secret: it must never reach a phone. - list: vi.fn(async () => [{ id: 'cred_1', integrationID: 'nano-gpt', label: 'work', active: true, value: { type: 'key', key: 'sk-SECRET' } }]), - activate: vi.fn(async () => undefined), - remove: vi.fn(async () => undefined), - }, - }; - return { client, statuses }; +function server(attemptUrl = 'https://github.com/login/device?user_code=ABCD'): FakeOpenCodeServer { + return fakeOpenCode({ + credentials: [{ id: 'cred_1', integrationID: 'nano-gpt', label: 'work', active: true, value: secret }], + signIns: [{ attemptID: 'con_1', url: attemptUrl, instructions: 'Enter code: ABCD', status: 'pending' }], + }); } -const accountsOf = (client: unknown, extra: Partial[1]> = {}) => - new OpenCodeAccounts(async () => client as unknown as OpenCodeClient, { pollMs: 1, now: () => 0, ...extra }); +const accountsOf = (fake: FakeOpenCodeServer, extra: Partial[1]> = {}) => + new OpenCodeAccounts(async () => fake.client, { pollMs: 1, now: () => 0, ...extra }); describe('OpenCode accounts', () => { it('lists what a phone can connect, and the connections without their secrets', async () => { - const { client } = fakeClient(); - const connected = await accountsOf(client).list(false); + const fake = server(); + const connected = await accountsOf(fake).list(false); expect(connected.map((a) => a.id)).toEqual(['github-copilot', 'nano-gpt']); expect(connected.find((a) => a.id === 'nano-gpt')).toEqual({ id: 'nano-gpt', @@ -81,27 +42,53 @@ describe('OpenCode accounts', () => { expect(copilot.methods?.[0]?.fields?.map((f) => f.key)).toEqual(['deploymentType', 'enterpriseUrl']); expect(copilot.methods?.[0]?.fields?.[1]?.when).toEqual([{ key: 'deploymentType', value: 'enterprise' }]); expect(JSON.stringify(connected)).not.toContain('sk-SECRET'); - expect((await accountsOf(client).list(true)).map((a) => a.id)).toEqual(['github-copilot', 'nano-gpt', 'openai']); + expect((await accountsOf(fake).list(true)).map((a) => a.id)).toEqual(['github-copilot', 'nano-gpt', 'openai']); }); it('keeps a key and answers with the connected accounts', async () => { - const { client } = fakeClient(); - const result = await accountsOf(client).change('openai', { kind: 'key', method: 'key', key: 'sk-new', label: 'home' }, () => {}); - expect(client.integration.connect.key).toHaveBeenCalledWith({ integrationID: 'openai', key: 'sk-new', label: 'home' }); + const fake = server(); + const result = await accountsOf(fake).change('openai', { kind: 'key', method: 'key', key: 'sk-new', label: 'home' }, () => {}); + expect(fake.client.integration.connect.key).toHaveBeenCalledWith({ integrationID: 'openai', key: 'sk-new', label: 'home' }); expect('accounts' in result).toBe(true); + const accounts = (result as { accounts: Array<{ id: string; connections?: unknown[] }> }).accounts; + expect(accounts.find((a) => a.id === 'openai')!.connections).toEqual([ + { id: 'cred_2', label: 'home', kind: 'key', active: true, removable: true }, + ]); + }); + + it("switches which of an integration's credentials is active", async () => { + const fake = server(); + // A second key for the same provider leaves the first inactive. + await accountsOf(fake).change('openai', { kind: 'key', method: 'key', key: 'sk-one', label: 'one' }, () => {}); + await accountsOf(fake).change('openai', { kind: 'key', method: 'key', key: 'sk-two', label: 'two' }, () => {}); + const result = await accountsOf(fake).change('openai', { kind: 'activate', connection: 'cred_2' }, () => {}); + const openai = (result as { accounts: Array<{ id: string; connections: Array<{ id: string; active?: boolean }> }> }).accounts.find( + (a) => a.id === 'openai', + )!; + expect(openai.connections.map((c) => [c.id, c.active])).toEqual([ + ['cred_2', true], + ['cred_3', false], + ]); + // A removed active credential leaves the other one active (verified). + const removed = await accountsOf(fake).change('openai', { kind: 'remove', connection: 'cred_2' }, () => {}); + const left = (removed as { accounts: Array<{ id: string; connections: Array<{ id: string; active?: boolean }> }> }).accounts.find( + (a) => a.id === 'openai', + )!; + expect(left.connections).toEqual([{ id: 'cred_3', label: 'two', kind: 'key', active: true, removable: true }]); }); it('starts a device sign-in, lists the integrations first, and says once when it completes', async () => { - const { client, statuses } = fakeClient(); - statuses.push('pending', 'complete'); + const fake = server(); const ended: SignInState[] = []; - const result = await accountsOf(client).change( + const result = await accountsOf(fake).change( 'github-copilot', { kind: 'sign-in', method: 'device', answers: { deploymentType: 'github.com' } }, (end) => ended.push(end), ); - expect(client.integration.list.mock.invocationCallOrder[0]).toBeLessThan(client.integration.oauth.connect.mock.invocationCallOrder[0]!); - expect(client.integration.oauth.connect).toHaveBeenCalledWith({ + const list = fake.client.integration.list as unknown as { mock: { invocationCallOrder: number[] } }; + const connect = fake.client.integration.oauth.connect as unknown as { mock: { invocationCallOrder: number[] } }; + expect(list.mock.invocationCallOrder[0]).toBeLessThan(connect.mock.invocationCallOrder[0]!); + expect(fake.client.integration.oauth.connect).toHaveBeenCalledWith({ integrationID: 'github-copilot', methodID: 'device', answer: { deploymentType: 'github.com' }, @@ -113,14 +100,25 @@ describe('OpenCode accounts', () => { attempt: { attemptId: 'con_1', url: 'https://github.com/login/device?user_code=ABCD', instructions: 'Enter code: ABCD', expiresAt: 10_000 }, }, }); - await vi.waitFor(() => expect(ended).toEqual([{ status: 'complete', attemptId: 'con_1' }])); + // The status stays `complete` once it is, as 2.1.4's does; the driver + // must report the end exactly once and then stop asking. + fake.setSignIn('con_1', 'complete'); + await expect + .poll(() => ended) + .toEqual([{ status: 'complete', attemptId: 'con_1' }]); + const calls = (fake.client.integration.oauth.status as unknown as { mock: { calls: unknown[] } }).mock.calls.length; + await new Promise((resolve) => setTimeout(resolve, 10)); + expect(ended).toHaveLength(1); + expect((fake.client.integration.oauth.status as unknown as { mock: { calls: unknown[] } }).mock.calls.length).toBe(calls); }); it("finishes a paste-back sign-in only with that sign-in's own address", async () => { const callback = 'http://127.0.0.1:45493/auth/callback'; - const { client } = fakeClient(`https://auth.openai.com/authorize?redirect_uri=${encodeURIComponent(callback)}&state=s`); + const fake = server(`https://auth.openai.com/authorize?redirect_uri=${encodeURIComponent(callback)}&state=s`); + // The attempt the driver started has the id the fake makes; its status + // does not matter to the paste path. const open = vi.fn(async () => undefined); - const accounts = accountsOf(client, { open, pollMs: 60_000 }); + const accounts = accountsOf(fake, { open, pollMs: 60_000 }); const started = await accounts.change('openai', { kind: 'sign-in', method: 'chatgpt' }, () => {}); expect('signIn' in started && started.signIn.attempt?.pasteBack).toBe(true); diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeCatalog.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeCatalog.test.ts index 996a3da0..be7f2028 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/opencodeCatalog.test.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeCatalog.test.ts @@ -8,8 +8,9 @@ import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import * as path from 'node:path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -import type { ModelInfo, OpenCodeClient, ProviderInfo } from '@opencode/client'; +import type { ModelInfo, ProviderInfo } from '@opencode/client'; import { OpenCodeDriver } from '../driver'; +import { fakeOpenCode, type FakeProviderConfig } from './fakeServer'; import { readCatalogCache } from '../catalogCache'; import { modelsDevSource, parseModelsDev } from '../modelsDev'; import type { StartOpenCodeServerOptions } from '../server'; @@ -96,38 +97,33 @@ describe('an OpenCode driver reading its 2.x server', () => { /** Providers whose description fails, as a request can. */ const failing = new Set(); + const zenFree = { + id: 'zen-free', + modelID: 'zen-free', + providerID: 'opencode', + name: 'Zen', + enabled: true, + status: 'active', + capabilities: { tools: true, input: ['text'], output: ['text'] }, + variants: [], + cost: [], + limit: { context: 1, output: 1 }, + } as unknown as ModelInfo; + /** A server that registers its providers after `quietPolls` asks, lists * the ones it can use and those its config adds, and describes the rest * only when asked for one. */ function serverFor(config: { providers?: Record }, quietPolls = 0) { - let polls = 0; - // A provider the config signs in to keeps the endpoint the catalog gives it. - const endpoint = (id: string) => KNOWN.find((p) => p.id === id)?.settings?.baseURL as string | undefined; - const added = Object.entries(config.providers ?? {}).map(([id, p]) => provider(id, p.name ?? id, p.settings?.baseURL ?? endpoint(id) ?? '', 'enabled')); - const all = new Map([...KNOWN, ...added].map((p) => [p.id, p])); - const usable = () => (polls++ < quietPolls ? [] : [...all.values()].filter((p) => p.activation === 'enabled')); - const at = { location: { directory: '/tmp' } }; - return { - integration: { list: async () => ({ ...at, data: [...KNOWN.map((p) => p.id), 'tinyfish'].map((id) => ({ id, name: id, methods: [], connections: [] })) }) }, - provider: { - list: async () => ({ ...at, data: usable() }), - get: async ({ providerID }: { providerID: string }) => { - if (failing.has(providerID)) throw new Error('fetch failed'); - // Answers come back in any order. - await new Promise((resolve) => setTimeout(resolve, providerID === 'opencode-go' ? 5 : 0)); - const found = all.get(providerID); - if (!found) throw Object.assign(new Error('Provider not found'), { name: 'ProviderNotFoundError' }); - return { ...at, data: found }; - }, - }, - model: { - list: async () => ({ - ...at, - data: usable().length === 0 ? [] : [{ providerID: 'opencode', modelID: 'zen-free', name: 'Zen', enabled: true, status: 'active', capabilities: { tools: true, input: ['text'], output: ['text'] }, variants: [], cost: [], limit: { context: 1, output: 1 } }] as unknown as ModelInfo[], - }), - default: async () => ({ ...at, data: null }), - }, - } as unknown as OpenCodeClient; + return fakeOpenCode({ + providers: KNOWN.map((info) => ({ info, usable: info.activation === 'enabled', ...(info.id === 'opencode' ? { models: [zenFree] } : {}) })), + config: config as { providers?: Record }, + providerRegistration: quietPolls, + providerGetFails: failing, + providerGetDelayMs: { 'opencode-go': 5 }, + // What the test's server knows: the catalog providers and the web + // search, not the accounts integrations the stock fake carries. + integrationRows: [...KNOWN.map((p) => p.id), 'tinyfish'].map((id) => ({ id, name: id, methods: [], connections: [] })), + }).client; } function managed(options: { catalogCache?: string; quietPolls?: number; get?: ReturnType } = {}) { diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts index a1cc11c8..012eed34 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts @@ -16,6 +16,7 @@ import { OpenCodeDriver, openCodeContext, pickDefaultModel, reasoningLevels, toF import type { StartSession } from '../../../sdk/types'; import { PARTIAL_INTERVAL_MS } from '../../../sdk/partial'; import { recordingContext, type Handlers } from '../../../sdk/__tests__/context'; +import { fakeOpenCode } from './fakeServer'; type V2 = Record & { type: string; data?: Record }; @@ -66,50 +67,12 @@ function agentApi(rows: () => AgentRow[]) { }; } -/** A client whose event stream yields `events`, then stays open briefly so - * the ask → reply chains settle before the stream ends. */ +/** A client whose event stream yields `events`, then closes (a test + * convenience: 2.1.4's stream stays open, and `holdStream` is what the + * lifecycle tests use to keep it open and close or fail it themselves). The + * rest is the shared verified fake. */ function clientWith(events: V2[], over: Record = {}): OpenCodeClient { - async function* stream(): AsyncGenerator { - for (const e of events) yield e; - await new Promise((r) => setTimeout(r, 20)); - } - const client = { - event: { subscribe: vi.fn(() => stream()) }, - session: { - create: vi.fn(async () => ({ id: 'ses_1', projectID: 'p', cost: 0, tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, time: { created: 0, updated: 0 }, location: { directory: '/tmp' } })), - get: vi.fn(), - remove: vi.fn(async () => undefined), - update: vi.fn(async () => undefined), - switchAgent: vi.fn(async () => undefined), - switchModel: vi.fn(async () => undefined), - interrupt: vi.fn(async () => undefined), - prompt: vi.fn(async () => ({})), - command: vi.fn(async () => undefined), - compact: vi.fn(async () => ({})), - diff: vi.fn(async () => []), - revert: { stage: vi.fn(async () => ({})), clear: vi.fn(async () => undefined), commit: vi.fn(async () => undefined) }, - form: { reply: vi.fn(async () => undefined), cancel: vi.fn(async () => undefined) }, - }, - shell: { - list: vi.fn(async () => ({ location: { directory: '/tmp' }, data: [] })), - create: vi.fn(async () => ({ location: { directory: '/tmp' }, data: { id: 'sh_1' } })), - get: vi.fn(async () => ({ location: { directory: '/tmp' }, data: { id: 'sh_1' } })), - output: vi.fn(async () => ({ location: { directory: '/tmp' }, data: { output: '', cursor: 0, size: 0, truncated: false } })), - remove: vi.fn(async () => undefined), - }, - permission: { reply: vi.fn(async () => undefined) }, - // Each agent's rules: OpenCode's catch-all allow, then what it forbids. - agent: agentApi(() => ALL_AGENTS), - location: { get: vi.fn(async () => ({ directory: '/tmp' })) }, - message: { list: vi.fn(async () => ({ data: [], cursor: {} })) }, - command: { list: vi.fn(async () => ({ location: { directory: '/tmp' }, data: [] })) }, - mcp: { list: vi.fn(async () => ({ location: { directory: '/tmp' }, data: [] })) }, - provider: { list: vi.fn(async () => ({ location: { directory: '/tmp' }, data: [] })) }, - model: { list: vi.fn(async () => ({ location: { directory: '/tmp' }, data: [] })), default: vi.fn(async () => ({ location: { directory: '/tmp' }, data: null })) }, - plugin: { list: vi.fn(async () => ({ location: { directory: '/tmp' }, data: [] })) }, - ...over, - } as unknown as OpenCodeClient; - return client; + return fakeOpenCode({ events: events as never, holdStream: false, over }).client; } function start(client: OpenCodeClient, overrides: Partial = {}, handlers: Handlers = {}) { @@ -118,17 +81,21 @@ function start(client: OpenCodeClient, overrides: Partial = {}, ha return ctx; } -/** The events v2 sends for one assistant turn: the execution starting, the - * model's text, and the turn ending. */ +/** The events 2.1.4 sends for one assistant turn: the execution starting, the + * model's text, and the execution succeeding. (It does not send + * `session.idle` for that turn; the driver handles that too, and the tests + * below use it where a turn is ended without an execution result.) */ const turn = (text: string, sessionID = 'ses_1'): V2[] => [ { type: 'session.execution.started', data: { sessionID } }, { type: 'session.step.started', data: { sessionID, assistantMessageID: 'm1', agent: 'build', model: { providerID: 'opencode', id: 'exo-free' }, started: 0 } }, { type: 'session.text.ended', data: { sessionID, assistantMessageID: 'm1', ordinal: 0, text } }, - { type: 'session.idle', data: { sessionID } }, + { type: 'session.execution.succeeded', data: { sessionID } }, ]; describe('OpenCode session lifecycle', () => { - it('reports its conversation id, becomes ready, and ends normally when the stream closes', async () => { + it('reports its conversation id, becomes ready, and ends normally when the client aborts the stream', async () => { + // `clientWith` closes after its script, the clean end a client abort + // gives (verified: 2.1.4's iterator returns, it does not throw). const ctx = start(clientWith([])); expect(await ctx.ended()).toEqual({ type: 'ended' }); expect(ctx.events).toContainEqual({ type: 'info', nativeSessionId: 'ses_1', mode: 'ask', persona: 'build' }); @@ -148,14 +115,40 @@ describe('OpenCode session lifecycle', () => { ); }); - it('ends with the error when the stream rejects before any session exists', async () => { - const client = clientWith([], { - event: { subscribe: vi.fn(() => { throw new Error('connection refused'); }) }, + it('ends with the error when the server cannot create the session', async () => { + const server = fakeOpenCode({ + holdStream: true, + over: { session: { create: vi.fn(async () => { throw new Error('connection refused'); }) } }, }); - const ctx = start(client); - expect(await ctx.ended()).toEqual({ type: 'ended', error: 'connection refused' }); + const ctx = start(server.client); + expect(await ctx.ended()).toEqual({ type: 'ended', error: 'OpenCode session.create failed: connection refused' }); expect(ctx.events.some((e) => e.type === 'ready')).toBe(false); - expect(client.session.create).not.toHaveBeenCalled(); + }); + + it('keeps the stream open for the session\'s life, and closes it cleanly on an abort', async () => { + // 2.1.4's stream does not end after a turn; the end comes from the + // client's abort (or the server dying, below). + const server = fakeOpenCode({ holdStream: true }); + const ctx = start(server.client); + await ctx.waitFor((e) => e.type === 'ready'); + server.push({ + type: 'session.text.ended', + data: { sessionID: 'ses_1', assistantMessageID: 'm1', ordinal: 0, text: 'still here' }, + }); + await ctx.waitFor((e) => e.type === 'entries' && e.entries.some((x) => x.entryType === 'text')); + expect(ctx.events.some((e) => e.type === 'ended')).toBe(false); + server.endStream(); + expect(await ctx.ended()).toEqual({ type: 'ended' }); + }); + + it('ends with the error when the server is killed under the stream', async () => { + // Verified: a server that stops makes the iterator throw (a transport + // ClientError), which is what the bridge resumes the session from. + const server = fakeOpenCode({ holdStream: true }); + const ctx = start(server.client); + await ctx.waitFor((e) => e.type === 'ready'); + server.failStream(new Error('Transport: terminated')); + expect(await ctx.ended()).toEqual({ type: 'ended', error: 'Transport: terminated' }); }); }); @@ -1216,22 +1209,26 @@ describe('an OpenCode server it starts', () => { let refreshed = false; const state = { asked: false }; const model = (modelID: string) => ({ providerID: 'opencode', modelID, name: modelID, enabled: true, status: 'active' }); - const client = { - event: { subscribe: vi.fn(() => events(state, () => void (refreshed = true))) }, - model: { - list: async () => { - state.asked = true; - return { data: refreshed ? [model('cat-jam-free')] : [model('exo-free')] }; + const client = fakeOpenCode({ + holdStream: true, + over: { + event: { subscribe: vi.fn(() => events(state, () => void (refreshed = true))) }, + model: { + list: async () => { + state.asked = true; + return { data: refreshed ? [model('cat-jam-free')] : [model('exo-free')] }; + }, + default: async () => ({ data: { providerID: 'opencode', modelID: refreshed ? 'cat-jam-free' : 'exo-free' } }), }, - default: async () => ({ data: { providerID: 'opencode', modelID: refreshed ? 'cat-jam-free' : 'exo-free' } }), }, - } as unknown as OpenCodeClient; + }).client; const create = (modelsRefreshWaitMs?: number) => OpenCodeDriver.create({ autoStart: true, // Any file stands in for the executable: the start is faked. binaryPath: process.execPath, log: () => {}, + catalogWaitMs: 0, startServer: async () => ({ url: 'http://127.0.0.1:4100', pid: 1, exited: new Promise(() => {}), close: async () => {} }), connect: () => client, ...(modelsRefreshWaitMs !== undefined ? { modelsRefreshWaitMs } : {}), @@ -1290,13 +1287,24 @@ describe('an OpenCode server started for the first time that cannot be asked', ( it('is used at once, without an unhandled failure', async () => { vi.stubEnv('XDG_DATA_HOME', mkdtempSync(path.join(tmpdir(), 'oc-data-'))); - // No event stream and no models at all: both throw when touched. + // No usable catalog and no models at all: both throw when touched. const driver = await OpenCodeDriver.create({ autoStart: true, binaryPath: process.execPath, log: () => {}, + modelsRefreshWaitMs: 0, startServer: async () => ({ url: 'http://127.0.0.1:4100', pid: 1, exited: new Promise(() => {}), close: async () => {} }), - connect: () => ({}) as OpenCodeClient, + connect: () => + fakeOpenCode({ + holdStream: true, + over: { + provider: { list: async () => { throw new Error('no catalog'); } }, + model: { + list: async () => { throw new Error('no models'); }, + default: async () => { throw new Error('no default'); }, + }, + }, + }).client, }); expect((await driver.listModels()).models).toEqual([]); }); diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeMcp.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeMcp.test.ts index 4cf156cc..5ae124e7 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/opencodeMcp.test.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeMcp.test.ts @@ -1,45 +1,9 @@ -import { describe, expect, it, vi } from 'vitest'; -import type { OpenCodeClient } from '@opencode/client'; +import { describe, expect, it } from 'vitest'; import { OpenCodeMcp, openCodeServerConfig, openCodeServers, openCodeSessionMcp, toggleOpenCodeMcp } from '../mcp'; import { redactUrl } from '../../../sdk/mcp'; +import { fakeOpenCode } from './fakeServer'; -type Entry = { name: string; status: { status: string; error?: string } }; - -/** A server that holds its MCP servers as v2 does: the config is where a - * server's own settings live, the MCP API is where its lifecycle does. */ -function server(config: Record>, status: Entry[] = []) { - const servers = [...status]; - const client = { - config: { - // 2.1.4 reads the file's `mcp` map back as `{servers: …}`. - get: vi.fn(async () => [{ type: 'document', info: { mcp: { servers: config } } }]), - }, - mcp: { - list: vi.fn(async () => ({ location: { directory: '/w' }, data: servers })), - add: vi.fn(async ({ server: name, config: entry }: { server: string; config: Record }) => { - config[name] = entry; - servers.push({ name, status: { status: 'connected' } }); - }), - remove: vi.fn(async ({ server: name }: { server: string }) => { - delete config[name]; - const at = servers.findIndex((s) => s.name === name); - if (at >= 0) servers.splice(at, 1); - }), - connect: vi.fn(async ({ server: name }: { server: string }) => { - const found = servers.find((s) => s.name === name); - if (found) found.status = { status: 'connected' }; - if (config[name]) delete config[name]!.disabled; - }), - disconnect: vi.fn(async ({ server: name }: { server: string }) => { - const found = servers.find((s) => s.name === name); - if (found) found.status = { status: 'disabled' }; - // Switching a server off leaves it configured but disabled. - if (config[name]) config[name] = { ...config[name], disabled: true }; - }), - }, - } as unknown as OpenCodeClient; - return { client, config, servers }; -} +const at = { location: { directory: '/w' } }; describe('OpenCode MCP servers', () => { it('are listed without a secret: the program, the redacted URL, the key names', () => { @@ -55,56 +19,52 @@ describe('OpenCode MCP servers', () => { }); it('adds a server through the MCP API, whole', async () => { - const { client, config } = server({}); - const mcp = new OpenCodeMcp(async () => client); + const server = fakeOpenCode(); + const mcp = new OpenCodeMcp(async () => server.client); const state = await mcp.act('add', [{ name: 'gh', setup: { type: 'http', url: 'https://new/mcp' } }], []); - expect(config.gh).toEqual({ type: 'remote', url: 'https://new/mcp' }); + // The API keeps it in the running inventory; the config document is not + // where 2.1.4 records an add. + expect(server.state.mcpConfig).toEqual({}); + expect(server.state.mcp.get('gh')).toMatchObject({ status: 'connected' }); expect(state.servers.map((s) => s.name)).toEqual(['gh']); await mcp.act('add', [{ name: 'fs', setup: { type: 'stdio', command: 'uvx', args: ['srv'], env: { K: 'v' } } }], []); - expect(config.fs).toEqual({ type: 'local', command: ['uvx', 'srv'], environment: { K: 'v' } }); + expect((await mcp.list()).servers.find((s) => s.name === 'fs')).toMatchObject({ + transport: 'stdio', + target: 'uvx', + envKeys: ['K'], + }); }); it('switches a server off and on through the MCP API', async () => { - const { client, servers } = server( - { fs: { type: 'local', command: ['uvx', 'srv'] } }, - [{ name: 'fs', status: { status: 'connected' } }], - ); - const mcp = new OpenCodeMcp(async () => client); + const server = fakeOpenCode({ mcpConfig: { fs: { type: 'local', command: ['uvx', 'srv'] } } }); + const mcp = new OpenCodeMcp(async () => server.client); expect((await mcp.act('disable', [], ['fs'])).servers[0]!.enabled).toBe(false); - expect(servers[0]!.status.status).toBe('disabled'); + expect(server.state.mcp.get('fs')!.status).toBe('disabled'); expect((await mcp.act('enable', [], ['fs'])).servers[0]!.enabled).toBe(true); + expect(server.state.mcp.get('fs')!.status).toBe('connected'); await expect(mcp.act('enable', [], ['nope'])).rejects.toThrow(/no MCP server 'nope'/); await expect(mcp.act('remove', [], ['nope'])).rejects.toThrow(/no MCP server 'nope'/); }); - it('removes a server through the MCP API', async () => { - const { client, config } = server( - { gh: { type: 'remote', url: 'https://x' }, fs: { type: 'local', command: ['x'] } }, - [{ name: 'gh', status: { status: 'connected' } }, { name: 'fs', status: { status: 'connected' } }], - ); - const mcp = new OpenCodeMcp(async () => client); + it('removes a server through the MCP API, from the running inventory', async () => { + const server = fakeOpenCode({ mcpConfig: { gh: { type: 'remote', url: 'https://x' }, fs: { type: 'local', command: ['x'] } } }); + const mcp = new OpenCodeMcp(async () => server.client); expect((await mcp.act('remove', [], ['gh'])).servers.map((s) => s.name)).toEqual(['fs']); - expect(Object.keys(config)).toEqual(['fs']); - expect(client.mcp.remove).toHaveBeenCalledWith({ server: 'gh' }); + expect(server.client.mcp.remove).toHaveBeenCalledWith({ server: 'gh' }); + // The config file keeps the entry; a new server would start with it. + expect(server.state.mcpConfig).toHaveProperty('gh'); }); it('reports and switches a session project-wide', async () => { - const client = { - mcp: { - list: vi.fn(async () => ({ - location: { directory: '/w' }, - data: [ - { name: 'gh', status: { status: 'connected' } }, - { name: 'lin', status: { status: 'needs_auth', error: 'login' } }, - { name: 'fs', status: { status: 'failed', error: 'exit 1' } }, - ], - })), - connect: vi.fn(async () => {}), - disconnect: vi.fn(async () => {}), - }, - } as unknown as OpenCodeClient; - expect(await openCodeSessionMcp(client, '/w')).toEqual({ + const server = fakeOpenCode({ + mcpServers: [ + { name: 'gh', status: 'connected' }, + { name: 'lin', status: 'needs_auth', error: 'login' }, + { name: 'fs', status: 'failed', error: 'exit 1' }, + ], + }); + expect(await openCodeSessionMcp(server.client, '/w')).toEqual({ servers: [ { name: 'fs', status: 'failed', error: 'exit 1' }, { name: 'gh', status: 'connected' }, @@ -113,8 +73,8 @@ describe('OpenCode MCP servers', () => { toggles: true, projectWide: true, }); - await toggleOpenCodeMcp(client, '/w', 'gh', false); - expect(client.mcp.disconnect).toHaveBeenCalledWith({ server: 'gh', location: { directory: '/w' } }); + await toggleOpenCodeMcp(server.client, '/w', 'gh', false); + expect(server.client.mcp.disconnect).toHaveBeenCalledWith({ server: 'gh', ...at }); }); it('writes remote servers as OpenCode remote, stdio as local', () => { diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodePlugins.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodePlugins.test.ts index 56376c64..9e8a3216 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/opencodePlugins.test.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodePlugins.test.ts @@ -1,26 +1,21 @@ import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import * as path from 'node:path'; -import { afterEach, describe, expect, it, vi } from 'vitest'; -import type { OpenCodeClient } from '@opencode/client'; +import { afterEach, describe, expect, it } from 'vitest'; import { OpenCodePlugins, toInstalledPlugin } from '../plugins'; +import { fakeOpenCode, type FakePlugin } from './fakeServer'; /** A server over one global config file: v2's config API cannot write the * plugin list, so the file is the store and `plugin.list` is what the server * reports as loaded. */ -async function server(plugins: unknown[], loaded: Array<{ id: string; state: unknown }> = []) { +async function server(plugins: unknown[], loaded?: FakePlugin[]) { const dir = await mkdtemp(path.join(tmpdir(), 'oc-plugins-')); const file = path.join(dir, 'opencode.json'); await writeFile(file, JSON.stringify({ plugins })); - const client = { - plugin: { - list: vi.fn(async () => ({ location: { directory: dir }, data: loaded })), - update: vi.fn(async () => undefined), - }, - } as unknown as OpenCodeClient; - const manager = new OpenCodePlugins(async () => client, () => [file]); + const fake = fakeOpenCode({ pluginConfig: plugins as Array, ...(loaded ? { plugins: loaded } : {}) }); + const manager = new OpenCodePlugins(async () => fake.client, () => [file]); const written = async () => JSON.parse(await readFile(file, 'utf8')) as { plugins: unknown[] }; - return { manager, written, client, dir, file }; + return { manager, written, client: fake.client, server: fake, dir, file }; } const dirs: string[] = []; @@ -44,9 +39,10 @@ describe('OpenCode plugins', () => { }); it('reports a plugin the server could not load as failed', async () => { + // 2.1.4 names a package plugin by `source.target`, not by an `id`. const { manager, dir } = await server( ['@acme/oc-plugin'], - [{ id: '@acme/oc-plugin', state: { status: 'failed', error: 'boom' } }], + [{ source: { type: 'package', target: '@acme/oc-plugin' }, state: { status: 'failed', error: 'boom', ref: 'err_1' } }], ); dirs.push(dir); const state = await manager.list(); diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeProviders.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeProviders.test.ts index 4e5ffa48..a493ba48 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/opencodeProviders.test.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeProviders.test.ts @@ -7,13 +7,14 @@ import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import * as path from 'node:path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -import type { OpenCodeClient, ProviderInfo } from '@opencode/client'; +import type { ProviderInfo } from '@opencode/client'; import { OpenCodeDriver } from '../driver'; import { type Catalog, type CatalogModel, EMPTY_CATALOG } from '../catalog'; import { placeProfiles, profileModelGroup, profileProviderId, providersConfig, servedBy, serverSetup } from '../providers'; import type { StartOpenCodeServerOptions } from '../server'; import type { ProviderBinding } from '../../../sdk/types'; import { recordingContext } from '../../../sdk/__tests__/context'; +import { fakeOpenCode } from './fakeServer'; const router = (over: Partial = {}): ProviderBinding => ({ id: 'router', @@ -233,6 +234,7 @@ describe('an OpenCode driver given provider profiles', () => { // Any file stands in for the executable: the start is faked. binaryPath: process.execPath, log: () => {}, + catalogWaitMs: 0, startServer: async (options) => { starts.push(options); const n = starts.length; @@ -240,7 +242,7 @@ describe('an OpenCode driver given provider profiles', () => { }, connect: (config) => { connects.push(config); - return {} as OpenCodeClient; + return fakeOpenCode({ holdStream: true }).client; }, }); return { driver, starts, closed, connects }; @@ -277,26 +279,19 @@ describe('an OpenCode driver given provider profiles', () => { }); it('ends the sessions on the server it replaces, so the bridge resumes them on the new one', async () => { - // Its event stream never opens: a session stays on the server it started on. - const client = { - // The stream opens and never yields: the session stays on the server - // it started on until that server is replaced. - event: { - subscribe: () => - (async function* () { - await new Promise(() => {}); - })(), - }, - provider: { list: async () => { throw new Error('no catalog'); } }, - model: { - list: async () => { throw new Error('no models'); }, - default: async () => { throw new Error('no default'); }, - }, - session: { - get: async () => { throw new Error('that session is gone'); }, - create: async () => ({ id: 'ses_1' }), + // A server that cannot be asked for its catalog, whose event stream + // stays open: a session stays on the server it started on until that + // server is replaced. + const client = fakeOpenCode({ + holdStream: true, + over: { + provider: { list: async () => { throw new Error('no catalog'); } }, + model: { + list: async () => { throw new Error('no models'); }, + default: async () => { throw new Error('no default'); }, + }, }, - } as unknown as OpenCodeClient; + }).client; let starts = 0; const driver = await OpenCodeDriver.create({ autoStart: true, @@ -327,28 +322,31 @@ describe('an OpenCode driver given provider profiles', () => { let release!: () => void; const closing = new Promise((resolve) => (release = resolve)); let starts = 0; - const client = (n: number) => - ({ - provider: { list: async () => ({ location: { directory: '/tmp' }, data: [] }) }, - model: { - list: async () => ({ - location: { directory: '/tmp' }, - data: [ - { id: `m${n}`, modelID: `m${n}`, providerID: 'zen', name: `M${n}`, enabled: true, status: 'active', cost: [], variants: [], limit: { context: 1_000, output: 100 } }, - ], - }), - default: async () => ({ location: { directory: '/tmp' }, data: null }), + const client = () => + fakeOpenCode({ + holdStream: true, + over: { + model: { + list: async () => ({ + location: { directory: '/tmp' }, + data: [ + { id: `m${starts}`, modelID: `m${starts}`, providerID: 'zen', name: `M${starts}`, enabled: true, status: 'active', cost: [], variants: [], limit: { context: 1_000, output: 100 } }, + ], + }), + default: async () => ({ location: { directory: '/tmp' }, data: null }), + }, }, - }) as unknown as OpenCodeClient; + }).client; const driver = await OpenCodeDriver.create({ autoStart: true, binaryPath: process.execPath, log: () => {}, + catalogWaitMs: 0, startServer: async () => { const n = ++starts; return { url: `http://127.0.0.1:${4100 + n}`, pid: n, exited: new Promise(() => {}), close: () => closing }; }, - connect: () => client(starts), + connect: () => client(), }); const restart = driver.setProviders([router()]); await new Promise((resolve) => setTimeout(resolve, 0)); @@ -372,19 +370,12 @@ describe('an OpenCode driver given provider profiles', () => { }, // The running server lists what it has, the profiles it was given too. connect: () => - ({ - integration: { list: async () => ({ location: { directory: '/tmp' }, data: ids.map((id) => ({ id, name: id, methods: [], connections: [] })) }) }, - provider: { - list: async () => ({ - location: { directory: '/tmp' }, - data: ids.map((id) => ({ id, name: id, activation: 'enabled', package: '@opencode/ai/providers/openai-compatible' })), - }), - }, - model: { - list: async () => ({ location: { directory: '/tmp' }, data: [] }), - default: async () => ({ location: { directory: '/tmp' }, data: null }), - }, - }) as unknown as OpenCodeClient, + fakeOpenCode({ + holdStream: true, + providers: ids.map((id) => ({ + info: { id, name: id, activation: 'enabled', package: '@opencode/ai/providers/openai-compatible' } as ProviderInfo, + })), + }).client, }); expect(await driver.setProviders([router({ label: 'DeepSeek' })])).toEqual([ { id: 'router', reason: "OpenCode already has a provider called 'deepseek'. Give this profile another name." }, @@ -401,7 +392,7 @@ describe('an OpenCode driver given provider profiles', () => { }); it('refuses them for a server it does not start', async () => { - const driver = await OpenCodeDriver.create({ serverUrl: 'http://127.0.0.1:4096', log: () => {}, connect: () => ({}) as OpenCodeClient }); + const driver = await OpenCodeDriver.create({ serverUrl: 'http://127.0.0.1:4096', log: () => {}, connect: () => fakeOpenCode().client }); expect(driver.info().supports?.providerModels).toBe(false); await expect(driver.setProviders([router()])).rejects.toThrow(/does not start/); }); @@ -409,7 +400,7 @@ describe('an OpenCode driver given provider profiles', () => { it('checks and lists a profile as an OpenAI-compatible endpoint', async () => { const post = vi.fn().mockResolvedValue({ status: 401 }); const get = vi.fn().mockResolvedValue({ status: 200, text: JSON.stringify({ data: [{ id: 'kimi-k3' }] }) }); - const driver = await OpenCodeDriver.create({ serverUrl: 'http://x', log: () => {}, providerHttp: { get, post }, connect: () => ({}) as OpenCodeClient }); + const driver = await OpenCodeDriver.create({ serverUrl: 'http://x', log: () => {}, providerHttp: { get, post }, connect: () => fakeOpenCode().client }); expect(await driver.checkProvider(router(), 'kimi-k3')).toBe(false); const [url, headers, body] = post.mock.calls[0]!; expect(url).toBe('http://192.168.1.2:3458/v1/chat/completions'); diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeSingleStart.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeSingleStart.test.ts index 4921e295..c39d1e3e 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/opencodeSingleStart.test.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeSingleStart.test.ts @@ -16,6 +16,7 @@ import { placeProfiles, providersFingerprint, servedBy, withoutProfiles } from ' import type { StartOpenCodeServerOptions } from '../server'; import type { ProviderBinding } from '../../../sdk/types'; import { recordingContext } from '../../../sdk/__tests__/context'; +import { fakeOpenCode, type FakeProviderConfig } from './fakeServer'; const router = (over: Partial = {}): ProviderBinding => ({ id: 'router', @@ -151,70 +152,33 @@ describe('an OpenCode driver with a kept catalog', () => { type AddedProvider = { name?: string; settings?: { baseURL?: string }; models?: Record }; + // A model as the server lists it: its variants spelled from how it reasons. + const listed = (m: Partial & { providerID: string; modelID: string }): ModelInfo => + ({ + id: m.modelID, + name: m.modelID, + enabled: true, + status: 'active', + cost: [], + limit: { context: 0, output: 0 }, + capabilities: { tools: false, input: [], output: [] }, + ...m, + variants: m.reasoning?.supported ? (m.reasoning.effort ?? []).map((id) => ({ id })) : [], + }) as unknown as ModelInfo; + /** A server as OpenCode 2.x is one: it lists the providers it can use * (`known`'s connected ones, and what its config adds) and their models, * names every other as an integration and describes it when asked. */ function serverFor(known: Catalog, config: { providers?: Record }): OpenCodeClient { - const providers = new Map( - [...known.providers].map(([id, p]) => [id, { ...p, activation: known.connected.has(id) ? 'enabled' : 'auto' } as ProviderInfo]), - ); - const available = new Set(known.connected); - // A model as the server lists it: its variants spelled from how it reasons. - const listed = (m: Partial & { providerID: string; modelID: string }): ModelInfo => - ({ - id: m.modelID, - name: m.modelID, - enabled: true, - status: 'active', - cost: [], - limit: { context: 0, output: 0 }, - capabilities: { tools: false, input: [], output: [] }, - ...m, - variants: m.reasoning?.supported ? (m.reasoning.effort ?? []).map((id) => ({ id })) : [], - }) as unknown as ModelInfo; - const models = [...known.models.values()].map(listed); - for (const [id, entry] of Object.entries(config.providers ?? {})) { - const base = providers.get(id); - providers.set(id, { - id, - name: entry.name ?? id, - activation: 'enabled', - package: '@opencode/ai/providers/openai-compatible', - settings: { ...(base?.settings ?? {}), ...(entry.settings?.baseURL ? { baseURL: entry.settings.baseURL } : {}) }, - } as ProviderInfo); - available.add(id); - for (const modelID of Object.keys(entry.models ?? {})) models.push(listed({ modelID, providerID: id })); - } - const at = { location: { directory: '/tmp' } }; - return { - // The stream opens and never yields: a session stays on its server. - event: { - subscribe: () => - (async function* () { - await new Promise(() => {}); - })(), - }, - integration: { list: async () => ({ ...at, data: [...providers.keys(), 'tinyfish'].map((id) => ({ id, name: id, methods: [], connections: [] })) }) }, - provider: { - list: async () => ({ ...at, data: [...providers.values()].filter((p) => available.has(p.id)) }), - get: async ({ providerID }: { providerID: string }) => { - const provider = providers.get(providerID); - if (!provider) throw Object.assign(new Error('not found'), { name: 'ProviderNotFoundError' }); - return { ...at, data: provider }; - }, - }, - model: { - list: async () => ({ ...at, data: models.filter((m) => available.has(m.providerID)) }), - default: async () => ({ ...at, data: null }), - }, - session: { - get: async () => { - throw new Error('that session is gone'); - }, - create: async () => ({ id: 'ses_1' }), - update: async () => ({ id: 'ses_1' }), - }, - } as unknown as OpenCodeClient; + return fakeOpenCode({ + providers: [...known.providers].map(([id, info]) => ({ + info: { ...info, activation: known.connected.has(id) ? 'enabled' : 'auto' } as ProviderInfo, + usable: known.connected.has(id), + models: [...known.models.values()].filter((m) => m.providerID === id).map(listed), + })), + config: config as { providers?: Record }, + integrations: ['tinyfish'], + }).client; } function managed(catalogCache: string, firstProvidersWaitMs?: number) { diff --git a/packages/agent-host/src/host/__tests__/opencodeServer.test.ts b/packages/agent-host/src/host/__tests__/opencodeServer.test.ts index d244aa28..37861e61 100644 --- a/packages/agent-host/src/host/__tests__/opencodeServer.test.ts +++ b/packages/agent-host/src/host/__tests__/opencodeServer.test.ts @@ -8,81 +8,37 @@ import { mkdtempSync } from 'node:fs'; import { tmpdir } from 'node:os'; import * as path from 'node:path'; import { afterEach, describe, expect, it, vi } from 'vitest'; -import type { OpenCodeClient } from '@opencode/client'; import { OpenCodeDriver } from '../../drivers/opencode/driver'; +import { fakeOpenCode, type FakeOpenCodeServer } from '../../drivers/opencode/__tests__/fakeServer'; import { Agents } from '../agents'; import { AgentHost } from '../host'; -/** The v2 client calls a session makes as it starts, answered from one - * scripted room. Only what a default (no persona, no resume) start touches - * is here. */ -function fakeClient(stream: () => AsyncGenerator>): OpenCodeClient { - const session = () => ({ - id: 'ses_1', - projectID: 'p', - cost: 0, - tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, - time: { created: 0, updated: 0 }, - location: { directory: '/tmp' }, - }); - return { - event: { subscribe: vi.fn(stream) }, - session: { - create: vi.fn(async () => session()), - get: vi.fn(async () => session()), - remove: vi.fn(async () => undefined), - update: vi.fn(async () => undefined), - switchAgent: vi.fn(async () => undefined), - switchModel: vi.fn(async () => undefined), - interrupt: vi.fn(async () => undefined), - prompt: vi.fn(async () => ({})), - command: vi.fn(async () => undefined), - compact: vi.fn(async () => ({})), - diff: vi.fn(async () => []), - revert: { stage: vi.fn(async () => ({})), clear: vi.fn(async () => undefined), commit: vi.fn(async () => undefined) }, - form: { reply: vi.fn(async () => undefined), cancel: vi.fn(async () => undefined) }, - }, - shell: { list: vi.fn(async () => ({ location: { directory: '/tmp' }, data: [] })) }, - permission: { reply: vi.fn(async () => undefined) }, - agent: { - get: vi.fn(async ({ agentID }: { agentID: string }) => ({ - location: { directory: '/tmp' }, - data: { id: agentID, name: agentID, mode: 'primary', hidden: false, request: {}, permissions: [] }, - })), - list: vi.fn(async () => ({ - location: { directory: '/tmp' }, - data: [ - { id: 'build', name: 'Build', mode: 'primary', hidden: false, request: {}, permissions: [] }, - { id: 'plan', name: 'Plan', mode: 'primary', hidden: false, request: {}, permissions: [] }, +/** A stock 2.1.4 server with one usable provider and its model, as the + * driver's catalog reads them. The stream stays open, so a session outlives + * the test's start until the fake server is lost. */ +function fakeServer(): FakeOpenCodeServer { + return fakeOpenCode({ + holdStream: true, + providers: [ + { + info: { id: 'opencode', name: 'OpenCode', activation: 'enabled', package: '@opencode/ai/providers/openai-compatible' }, + models: [ + { + id: 'cat-jam-free', + modelID: 'cat-jam-free', + providerID: 'opencode', + name: 'cat-jam-free', + enabled: true, + status: 'active', + cost: [], + variants: [], + limit: { context: 0, output: 0 }, + capabilities: { tools: true, input: ['text'], output: ['text'] }, + } as never, ], - })), - }, - location: { get: vi.fn(async () => ({ directory: '/tmp' })) }, - message: { list: vi.fn(async () => ({ data: [], cursor: {} })) }, - command: { list: vi.fn(async () => ({ location: { directory: '/tmp' }, data: [] })) }, - mcp: { list: vi.fn(async () => ({ location: { directory: '/tmp' }, data: [] })) }, - // A server lists the provider it can use once it has registered it. - provider: { - list: vi.fn(async () => ({ - location: { directory: '/tmp' }, - data: [{ id: 'opencode', name: 'OpenCode', activation: 'enabled', package: '@opencode/ai/providers/openai-compatible' }], - })), - }, - model: { - list: vi.fn(async () => ({ - location: { directory: '/tmp' }, - data: [{ providerID: 'opencode', modelID: 'cat-jam-free', name: 'cat-jam-free', enabled: true, status: 'active' }], - })), - default: vi.fn(async () => ({ location: { directory: '/tmp' }, data: null })), - }, - plugin: { list: vi.fn(async () => ({ location: { directory: '/tmp' }, data: [] })) }, - server: { info: vi.fn(async () => ({ version: '2.0.28' })) }, - } as unknown as OpenCodeClient; -} - -/** The stream a session reads until the test ends it. */ -async function* openStream(): AsyncGenerator> { - await new Promise(() => {}); + }, + ], + }); } /** An AgentHost over one driver, with a reader for its session events. */ @@ -107,6 +63,7 @@ describe('an OpenCode server the host starts', () => { it('is started again after it crashed, and its exit is not reported as unavailable', async () => { vi.stubEnv('XDG_DATA_HOME', mkdtempSync(path.join(tmpdir(), 'oc-data-'))); + const server = fakeServer(); const exits: Array<() => void> = []; let starts = 0; const driver = await OpenCodeDriver.create({ @@ -119,7 +76,7 @@ describe('an OpenCode server the host starts', () => { const exited = new Promise((resolve) => exits.push(resolve)); return { url: 'http://127.0.0.1:4100', pid: 1, exited, close: async () => {} }; }, - connect: () => fakeClient(openStream), + connect: () => server.client, }); const { out, send, events } = hostFor(driver); await send('st1', 'start-session', { sessionId: 's1', agent: 'opencode', cwd: '/tmp' }); @@ -145,6 +102,7 @@ describe('an OpenCode server the host starts', () => { it('is started again on the next session after its first start failed', async () => { vi.stubEnv('XDG_DATA_HOME', mkdtempSync(path.join(tmpdir(), 'oc-data-'))); + const server = fakeServer(); let starts = 0; const driver = await OpenCodeDriver.create({ autoStart: true, @@ -156,7 +114,7 @@ describe('an OpenCode server the host starts', () => { if (starts === 1) throw new Error('port 4100 is in use'); return { url: 'http://127.0.0.1:4100', pid: 1, exited: new Promise(() => {}), close: async () => {} }; }, - connect: () => fakeClient(openStream), + connect: () => server.client, }); // A failed first start is retried, not refused for the host's lifetime. expect(driver.info().unavailableReason).toBeUndefined(); From 60ebf9e9b1542123ac25f4433ff467c72e699775 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:01:49 +0100 Subject: [PATCH 14/42] test(opencode): keep the fake to the surface the tests use The fake carried two promises and a built-in id helper no test asked for. The stream's explicit end, failure and sign-in controls are what the tests drive; the rest only widened the fake's surface. Co-Authored-By: Claude Code --- .../drivers/opencode/__tests__/fakeServer.ts | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts b/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts index f0f52794..58f50a04 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/fakeServer.ts @@ -194,18 +194,10 @@ export interface FakeOpenCodeServer { endStream(): void; /** Fail the stream, as a server killed under it does. */ failStream(error: unknown): void; - /** Resolves once the scripted events have been consumed (each has been - * handed to the driver's loop). */ - scripted(): Promise; - /** Resolves after the script has been consumed and the driver's - * off-stream handlers have had their microtasks and one macrotask. */ - settled(): Promise; /** Change a sign-in's status, which `oauth.status` then keeps reporting. */ setSignIn(attemptID: string, status: FakeSignIn['status'], message?: string): void; } -const BUILTIN_IDS = new Set(['build', 'plan', 'general', 'explore', 'compaction', 'title', 'summary']); - /** The agents a stock 2.1.4 server lists, in its own order. */ export const OPENCODE_AGENTS: FakeAgentRow[] = [ { id: 'build', name: 'Build', mode: 'primary', hidden: false, rules: [{ action: '*', resource: '*', effect: 'allow' }, { action: 'read', resource: '*.env', effect: 'ask' }] }, @@ -301,8 +293,6 @@ export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServe let ended = false; let failure: unknown; const waiters: Array<() => void> = []; - let resolveScripted!: () => void; - const scripted = new Promise((resolve) => (resolveScripted = resolve)); const wake = () => waiters.splice(0).forEach((waiter) => waiter()); const providers = new Map(); @@ -653,11 +643,6 @@ export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServe ended = true; wake(); }, - scripted: () => scripted, - settled: async () => { - await scripted; - await new Promise((resolve) => setImmediate(resolve)); - }, setSignIn: (attemptID, status, message) => { const attempt = signIns.get(attemptID); if (!attempt) throw new Error(`no sign-in ${attemptID}`); @@ -680,7 +665,6 @@ export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServe if (ended) return; yield event; } - resolveScripted(); if (options.holdStream === false) { // A test convenience: the script is over, and one macrotask lets the // driver's off-stream ask handlers run before the close. @@ -703,9 +687,3 @@ export function fakeOpenCode(options: FakeServerOptions = {}): FakeOpenCodeServe return server; } - -/** The agents whose ids are OpenCode's built-ins, for tests that model a - * directory's agents loading after them. */ -export function builtinAgentIds(): Set { - return new Set(BUILTIN_IDS); -} From 0b56e402a60fc3e9971afe86c51ce7f8b7b0ef9c Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:00:06 +0100 Subject: [PATCH 15/42] test(protocol): drop tests that only restate their definitions The defaults projection, the pinned kind ranges, the capability list and version, the ChunkEnvelope serde round trip and the enum rename values each compared a constant with itself. The two per-direction representative fixture lists duplicated the corpus conformance test, which already decodes and re-round-trips every message type plus its rejections; the one semantic assertion they carried (agent-style userMessage shapes) stays as its own test. The fixed test secret is now one constant instead of three copies. Co-Authored-By: Claude Code --- crates/protocol/src/capabilities.rs | 10 --- crates/protocol/src/chunking.rs | 8 --- crates/protocol/src/commands.rs | 64 ------------------- crates/protocol/src/common.rs | 10 --- crates/protocol/src/crypto.rs | 2 +- crates/protocol/src/defaults.rs | 12 ---- crates/protocol/src/events.rs | 99 +---------------------------- crates/protocol/src/kinds.rs | 10 --- crates/protocol/src/lib.rs | 8 +++ crates/protocol/src/nip42.rs | 2 +- 10 files changed, 13 insertions(+), 212 deletions(-) diff --git a/crates/protocol/src/capabilities.rs b/crates/protocol/src/capabilities.rs index cfd34760..85ff3034 100644 --- a/crates/protocol/src/capabilities.rs +++ b/crates/protocol/src/capabilities.rs @@ -77,16 +77,6 @@ impl BridgeHostKind { mod tests { use super::*; - #[test] - fn protocol_version_is_12() { - assert_eq!(PROTOCOL_VERSION, 12); - } - - #[test] - fn capability_list() { - assert_eq!(ALL_BRIDGE_CAPABILITIES, ["files", "session-keys"]); - } - #[test] fn host_kind_round_trips_and_rejects_unknown() { for k in [BridgeHostKind::Cli, BridgeHostKind::Service] { diff --git a/crates/protocol/src/chunking.rs b/crates/protocol/src/chunking.rs index 7e4afdf1..0e3c4f51 100644 --- a/crates/protocol/src/chunking.rs +++ b/crates/protocol/src/chunking.rs @@ -411,14 +411,6 @@ mod tests { assert!(parse_chunk_envelope(r#"{"type":"chunk","cid":"c","i":0,"n":1,"part":"x"}"#).is_none()); // n<2 } - #[test] - fn parse_chunk_envelope_round_trips_a_valid_envelope() { - let raw = r#"{"type":"chunk","cid":"c1","i":2,"n":4,"part":"abc"}"#; - let env = parse_chunk_envelope(raw).unwrap(); - assert_eq!(env, ChunkEnvelope { kind: "chunk".into(), cid: "c1".into(), i: 2, n: 4, part: "abc".into() }); - assert_eq!(serde_json::to_string(&env).unwrap(), raw); - } - #[test] fn assembler_passes_ordinary_messages_straight_through() { let mut a = ChunkAssembler::new(); diff --git a/crates/protocol/src/commands.rs b/crates/protocol/src/commands.rs index b08282f5..20464234 100644 --- a/crates/protocol/src/commands.rs +++ b/crates/protocol/src/commands.rs @@ -678,70 +678,6 @@ mod tests { assert!(matches!(m, PhoneToBridge::Input(InputMsg { input_id: None, .. }))); } - #[test] - fn every_variant_decodes_from_a_representative_fixture() { - rt(&json!({"v":12,"type":"permission-response","sessionId":"s","requestId":"r","optionId":"allow_always"})); - rt(&json!({"v":12,"type":"question-response","sessionId":"s","requestId":"q","index":1,"answer":{"kind":"options","selected":[0,2]}})); - rt(&json!({"v":12,"type":"question-response","sessionId":"s","requestId":"q","index":0,"answer":{"kind":"text","text":"blue"}})); - rt(&json!({"v":12,"type":"plan-response","sessionId":"s","requestId":"p","optionId":"approve"})); - rt(&json!({"v":12,"type":"set-option","sessionId":"s","option":"mode","value":"plan"})); - rt(&json!({"v":12,"type":"set-option","sessionId":"s","option":"model","value":"anthropic/claude-x"})); - rt(&json!({"v":12,"type":"sync-request","sessionId":"s","haveRanges":[[1,40],[61,80]]})); - rt(&json!({"v":12,"type":"sync-ack","syncId":"y","ranges":[[1,50],[51,100]]})); - rt(&json!({"v":12,"type":"create-session","agent":"opencode","effort":"high","cwd":"proj","createCwd":true,"providerId":"p"})); - rt(&json!({"v":12,"type":"refresh-sessions"})); - rt(&json!({"v":12,"type":"close-session","sessionId":"s"})); - rt(&json!({"v":12,"type":"interrupt","sessionId":"s"})); - rt(&json!({"v":12,"type":"create-folder","path":"a/b","root":"/w","requestId":"r"})); - rt(&json!({"v":12,"type":"upload-file","sessionId":"s","hash":"h","url":"u","key":"k","iv":"iv","filename":"f","mimeType":"image/png","text":"","sizeBytes":123})); - rt(&json!({"v":12,"type":"upload-file","sessionId":"s","uploadId":"u","filename":"f","mimeType":"image/png","base64Data":"x","text":"","chunkIndex":0,"totalChunks":2})); - rt(&json!({"v":12,"type":"usage-request","sessionId":"s"})); - rt(&json!({"v":12,"type":"models-request","agent":"opencode"})); - rt(&json!({"v":12,"type":"agent-style-request","agent":"opencode"})); - rt(&json!({"v":12,"type":"commands-request","sessionId":"s"})); - rt(&json!({"v":12,"type":"personas-request","agent":"opencode"})); - rt(&json!({"v":12,"type":"personas-request","agent":"opencode","sessionId":"s"})); - rt(&json!({"v":12,"type":"plugins-request","agent":"claude-code"})); - rt(&json!({"v":12,"type":"plugins-request","agent":"claude-code","available":true})); - rt(&json!({"v":12,"type":"plugin-action","agent":"claude-code","action":"add-marketplace","target":"me/skills"})); - rt(&json!({"v":12,"type":"plugin-action","agent":"claude-code","action":"update","target":"c@m"})); - rt(&json!({"v":12,"type":"agent-action","agent":"opencode","action":"install"})); - rt(&json!({"v":12,"type":"agent-action","agent":"opencode","action":"remove"})); - rt(&json!({"v":12,"type":"mcp-request","agent":"claude-code"})); - rt(&json!({"v":12,"type":"mcp-action","agent":"claude-code","action":"add","servers":[ - {"name":"github","transport":{"type":"http","url":"https://api.githubcopilot.com/mcp/","headers":{"Authorization":"Bearer t"}}}, - {"name":"fs","transport":{"type":"stdio","command":"npx","args":["-y","server-fs","/w"],"env":{"K":"v"}}}, - {"name":"old","transport":{"type":"sse","url":"https://x/sse"}}]})); - rt(&json!({"v":12,"type":"mcp-action","agent":"opencode","action":"disable","names":["github"]})); - rt(&json!({"v":12,"type":"session-mcp-request","sessionId":"s"})); - rt(&json!({"v":12,"type":"session-mcp-toggle","sessionId":"s","name":"github","enabled":false})); - rt(&json!({"v":12,"type":"permission-rules-request","agent":"opencode"})); - rt(&json!({"v":12,"type":"permission-rules-remove","agent":"opencode","ids":["psv_1","psv_2"]})); - rt(&json!({"v":12,"type":"accounts-request","agent":"opencode","all":true})); - rt(&json!({"v":12,"type":"account-action","agent":"opencode","account":"nano-gpt","action":{"kind":"key","method":"key","key":"sk-x","label":"work"}})); - rt(&json!({"v":12,"type":"account-action","agent":"opencode","account":"github-copilot","action":{"kind":"sign-in","method":"device","answers":{"deploymentType":"github.com"}}})); - rt(&json!({"v":12,"type":"account-action","agent":"opencode","account":"openai","action":{"kind":"finish","attempt":"con_1","callbackUrl":"http://127.0.0.1:1/auth/callback?code=c"}})); - rt(&json!({"v":12,"type":"account-action","agent":"opencode","account":"openai","action":{"kind":"cancel","attempt":"con_1"}})); - rt(&json!({"v":12,"type":"account-action","agent":"opencode","account":"openai","action":{"kind":"activate","connection":"cred_1"}})); - rt(&json!({"v":12,"type":"account-action","agent":"opencode","account":"openai","action":{"kind":"remove","connection":"cred_1"}})); - rt(&json!({"v":12,"type":"pair-request","npub":"npub1","pubkeyHex":"aa","label":"phone","token":"t"})); - rt(&json!({"v":12,"type":"pair-request","npub":"npub1","pubkeyHex":"aa","label":"phone","token":"t","sessionKey":{"pubkeyHex":"bb","bridgePubkeyHex":"cc","expiresAt":1800000000}})); - rt(&json!({"v":12,"type":"provider-profiles-request"})); - rt(&json!({"v":12,"type":"web-sign-in","code":"K7QM-4XPA"})); - rt(&json!({"v":12,"type":"web-browsers-request"})); - rt(&json!({"v":12,"type":"web-browser-sign-out","id":"0123456789abcdef"})); - rt(&json!({"v":12,"type":"session-key","sessionKey":{"pubkeyHex":"bb","bridgePubkeyHex":"cc","expiresAt":1800000000}})); - rt(&json!({"v":12,"type":"session-key","sessionKey":{"pubkeyHex":"bb","bridgePubkeyHex":"cc","expiresAt":1800000000,"label":"Pixel 8"}})); - rt(&json!({"v":12,"type":"presence","watchingTerminal":"t1"})); - rt(&json!({"v":12,"type":"terminal-open","openId":"o1","cols":80,"rows":24})); - rt(&json!({"v":12,"type":"terminal-open","openId":"o1","cols":80,"rows":24,"session":"s"})); - rt(&json!({"v":12,"type":"terminal-input","term":"t1","seq":1,"data":"aGk="})); - rt(&json!({"v":12,"type":"terminal-resize","term":"t1","cols":132,"rows":43})); - rt(&json!({"v":12,"type":"terminal-replay","term":"t1","from":4096})); - rt(&json!({"v":12,"type":"terminal-takeover","term":"t1"})); - rt(&json!({"v":12,"type":"terminal-close","term":"t1"})); - } - #[test] fn a_terminal_message_rejects_what_its_fields_cannot_hold() { // A window size past u16, and input without its bytes. diff --git a/crates/protocol/src/common.rs b/crates/protocol/src/common.rs index 3f34fc8a..c1c2150d 100644 --- a/crates/protocol/src/common.rs +++ b/crates/protocol/src/common.rs @@ -1561,16 +1561,6 @@ mod tests { e } - #[test] - fn enum_wire_values() { - assert_eq!(serde_json::to_string(&SessionState::WaitingPermission).unwrap(), r#""waiting_permission""#); - assert_eq!(serde_json::to_string(&DiffLineType::Del).unwrap(), r#""del""#); - assert_eq!(serde_json::to_string(&ToolKind::SwitchMode).unwrap(), r#""switch_mode""#); - assert_eq!(serde_json::to_string(&PermissionOptionKind::AllowAlways).unwrap(), r#""allow_always""#); - assert_eq!(serde_json::to_string(&SessionOption::Effort).unwrap(), r#""effort""#); - assert_eq!(serde_json::to_string(&NoticeKind::AuthError).unwrap(), r#""auth_error""#); - } - #[test] fn an_open_vocabulary_takes_an_unknown_value_as_its_catch_all() { let de = |s: &str| serde_json::from_str::(s).unwrap(); diff --git a/crates/protocol/src/crypto.rs b/crates/protocol/src/crypto.rs index 4e0a93ce..d9700d13 100644 --- a/crates/protocol/src/crypto.rs +++ b/crates/protocol/src/crypto.rs @@ -136,7 +136,7 @@ mod tests { use super::*; // NIP-44 spec test keys (nips/44.md vectors): sec = 0x…01 and 0x…02. - const SEC1: &str = "0000000000000000000000000000000000000000000000000000000000000001"; + use crate::TEST_SECRET_HEX as SEC1; const SEC2: &str = "0000000000000000000000000000000000000000000000000000000000000002"; #[test] diff --git a/crates/protocol/src/defaults.rs b/crates/protocol/src/defaults.rs index 948d3840..060bbe04 100644 --- a/crates/protocol/src/defaults.rs +++ b/crates/protocol/src/defaults.rs @@ -20,15 +20,3 @@ pub fn protocol_defaults() -> ProtocolDefaults { provider_base_url_error: PROVIDER_BASE_URL_ERROR.to_string(), } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn defaults_carry_the_relay_lists_and_error_text() { - let d = protocol_defaults(); - assert_eq!(d.default_relays.len(), 3); - assert_eq!(d.provider_base_url_error, PROVIDER_BASE_URL_ERROR); - } -} diff --git a/crates/protocol/src/events.rs b/crates/protocol/src/events.rs index b512bb95..50330eaf 100644 --- a/crates/protocol/src/events.rs +++ b/crates/protocol/src/events.rs @@ -715,7 +715,6 @@ pub enum BridgeToPhone { #[cfg(test)] mod tests { use super::*; - use crate::common::EntryBody; use serde_json::json; fn rt(v: &serde_json::Value) -> BridgeToPhone { @@ -728,9 +727,6 @@ mod tests { fn session() -> serde_json::Value { json!({"id":"s","agent":"claude-code","slug":"sl","cwd":"/w","lastActivity":"t","lineCount":1,"title":null,"project":"p","state":"running","seqHigh":10}) } - fn entry() -> serde_json::Value { - json!({"timestamp":"t","entryType":"text","role":"agent","text":"hello"}) - } fn agent() -> serde_json::Value { json!({ "id":"claude-code","displayName":"Claude Code", @@ -778,29 +774,9 @@ mod tests { } #[test] - fn every_variant_decodes_from_a_representative_fixture() { - rt(&json!({"v":12,"type":"output","sessionId":"s","seq":7,"entries":[entry(),entry()]})); - rt(&json!({"v":12,"type":"input-ack","sessionId":"s","inputId":"i"})); - rt(&json!({"v":12,"type":"sync-begin","sessionId":"s","syncId":"y","seqHigh":100,"ranges":[[1,50]]})); - rt(&json!({"v":12,"type":"sync-chunk","sessionId":"s","syncId":"y","range":[1,2],"entries":[{"seq":1,"entry":entry()},{"seq":2,"entry":entry()}]})); - rt(&json!({"v":12,"type":"sync-end","sessionId":"s","syncId":"y","deliveredRanges":[[1,50]]})); - rt(&json!({"v":12,"type":"session-pending","pendingId":"p","machine":"m","createdAt":"t"})); - rt(&json!({"v":12,"type":"session-ready","pendingId":"p","session":session()})); - rt(&json!({"v":12,"type":"session-failed","pendingId":"p","reason":"boom"})); - rt(&json!({"v":12,"type":"input-failed","sessionId":"s","reason":"no-session","inputId":"i"})); - rt(&json!({"v":12,"type":"close-session-ack","sessionId":"s","success":true})); - rt(&json!({"v":12,"type":"option-confirmed","sessionId":"s","option":"mode","value":"plan"})); - rt(&json!({"v":12,"type":"folder-ack","requestId":"r","success":true,"path":"a/b"})); - rt(&json!({"v":12,"type":"usage","sessionId":"s","usage":{"available":true,"plan":"max","windows":[{"label":"5h","utilization":42.0,"resetsAt":"t"}],"fetchedAt":"t"}})); - rt(&json!({"v":12,"type":"models","agent":"claude-code","models":[{"id":"m1","label":"M1"},{"id":"m2"}],"defaultModel":"m1"})); - rt(&json!({"v":12,"type":"models","agent":"opencode","models":[],"error":"sdk offline"})); - rt(&json!({"v":12,"type":"agent-style","agent":"pi"})); - let styled = rt(&json!({"v":12,"type":"agent-style","agent":"claude-code","styleHash":"0123456789abcdef","style":{ - "spinner":{"frames":["·","✢"],"intervalMs":120,"bounce":true}, - "workingWords":["Brewing"],"workingCounter":true, - "toolGlyphs":{"other":"⏺"},"resultMark":"⎿", - "userMessage":{"shape":"prompt","mark":">"}, - "wordmark":["▐▛███▜▌"],"accent":"#D77757" + fn an_agent_style_user_message_shape_decodes_to_its_variant() { + let styled = rt(&json!({"v":12,"type":"agent-style","agent":"claude-code","style":{ + "userMessage":{"shape":"prompt","mark":">"} }})); let BridgeToPhone::AgentStyle(m) = styled else { panic!() }; assert_eq!(m.style.unwrap().user_message, crate::common::UserMessageStyle::Prompt { mark: ">".into() }); @@ -808,58 +784,6 @@ mod tests { let m = rt(&json!({"v":12,"type":"agent-style","agent":"x","style":{"userMessage":{"shape":"card"}}})); let BridgeToPhone::AgentStyle(m) = m else { panic!() }; assert_eq!(m.style.unwrap().user_message, crate::common::UserMessageStyle::Unknown); - rt(&json!({"v":12,"type":"commands","sessionId":"s","commands":[{"name":"compact","description":"Compact","argumentHint":""},{"name":"p:x"}]})); - rt(&json!({"v":12,"type":"commands","sessionId":"s","commands":[],"error":"not running"})); - rt(&json!({"v":12,"type":"personas","agent":"opencode","sessionId":"s","personas":[{"id":"build","label":"Build"},{"id":"review","label":"review","description":"Reviews","color":"#E5C07B"}]})); - rt(&json!({"v":12,"type":"personas","agent":"opencode","personas":[],"error":"not running"})); - rt(&json!({"v":12,"type":"plugins","agent":"claude-code", - "installed":[{"id":"c@m","name":"c","marketplace":"m","version":"1","description":"d","enabled":false}], - "marketplaces":[{"name":"m","source":"me/skills"}],"toggles":true, - "available":[{"id":"x@m","name":"x","marketplace":"m","installCount":12}]})); - rt(&json!({"v":12,"type":"plugins","agent":"opencode","installed":[{"id":"opencode-wakatime","name":"opencode-wakatime","enabled":true}]})); - rt(&json!({"v":12,"type":"plugins","agent":"claude-code","installed":[],"error":"no claude"})); - rt(&json!({"v":12,"type":"plugin-ack","agent":"claude-code","action":"install","target":"x@m","success":false,"error":"not found"})); - rt(&json!({"v":12,"type":"plugin-ack","agent":"claude-code","action":"update","target":"c@m","success":true,"message":"Updated from 0.1.0 to 0.2.0."})); - rt(&json!({"v":12,"type":"agent-ack","agent":"opencode","action":"install","success":true})); - rt(&json!({"v":12,"type":"agent-ack","agent":"claude-code","action":"remove","success":false,"error":"Claude Code is on this machine outside CodeDeck."})); - rt(&json!({"v":12,"type":"mcp-servers","agent":"claude-code","servers":[ - {"name":"github","transport":"http","target":"https://api.githubcopilot.com/mcp/","headerKeys":["Authorization"],"enabled":true}, - {"name":"fs","transport":"stdio","target":"npx","envKeys":["K"],"enabled":false}],"toggles":true})); - rt(&json!({"v":12,"type":"mcp-servers","agent":"claude-code","servers":[],"error":"no claude"})); - rt(&json!({"v":12,"type":"mcp-ack","agent":"claude-code","action":"add","names":["github"],"success":false,"error":"bad url"})); - rt(&json!({"v":12,"type":"session-mcp","sessionId":"s","servers":[ - {"name":"github","status":"connected","tools":12},{"name":"x","status":"needs-auth"}, - {"name":"y","status":"failed","error":"exit 1"}],"toggles":true,"projectWide":true})); - rt(&json!({"v":12,"type":"session-mcp","sessionId":"s","servers":[],"error":"not running"})); - rt(&json!({"v":12,"type":"permission-rules","agent":"opencode","rules":[ - {"id":"psv_1","project":"/home/me/code","tool":"shell","pattern":"git status *","createdAt":1_792_008_058_637_u64}]})); - rt(&json!({"v":12,"type":"permission-rules","agent":"opencode","rules":[],"error":"OpenCode is not running"})); - rt(&json!({"v":12,"type":"accounts","agent":"opencode","all":true,"accounts":[ - {"id":"github-copilot","name":"GitHub Copilot","methods":[{"id":"device","kind":"sign-in","label":"Login with GitHub Copilot","fields":[ - {"key":"deploymentType","title":"Select GitHub deployment type","required":true,"options":[{"id":"github.com","label":"GitHub.com"}]}, - {"key":"enterpriseUrl","title":"GitHub Enterprise URL","required":true,"when":[{"key":"deploymentType","value":"enterprise"}]}]}], - "connections":[{"id":"GITHUB_TOKEN","label":"GITHUB_TOKEN","kind":"env"}]}, - {"id":"nano-gpt","name":"NanoGPT","methods":[{"id":"key","kind":"key"}],"connections":[{"id":"cred_1","label":"work","kind":"key","active":true,"removable":true}]}]})); - rt(&json!({"v":12,"type":"accounts","agent":"opencode","accounts":[],"error":"OpenCode is not running"})); - rt(&json!({"v":12,"type":"account-sign-in","agent":"opencode","account":"opencode","status":"started","attemptId":"con_1", - "attempt":{"attemptId":"con_1","url":"https://opencode.ai/console/device?user_code=WFZW-JRCC","instructions":"Enter code: WFZW-JRCC","expiresAt":1_792_010_428_118_u64}})); - rt(&json!({"v":12,"type":"account-sign-in","agent":"opencode","account":"openai","status":"failed","attemptId":"con_2","error":"ChatGPT sign-in did not return a client ID."})); - rt(&json!({"v":12,"type":"credentials-ack","machine":"m","agent":"claude-code","success":true, - "credentials":[{"id":"anthropic_api_key","label":"Anthropic API key","present":true,"valid":true}]})); - rt(&json!({"v":12,"type":"pair-ack","machine":"m","ok":false,"reason":"bad-token","relays":["wss://r"],"host":"cli"})); - rt(&json!({"v":12,"type":"provider-profiles","machine":"m","profiles":[{"id":"p","agent":"claude-code","label":"L","baseUrl":"https://x","models":[{"id":"m"}],"hasToken":true}]})); - rt(&json!({"v":12,"type":"provider-profile-ack","machine":"m","profileId":"p","success":true,"tokenValid":false})); - rt(&json!({"v":12,"type":"terminal-opened","openId":"o1","term":"t1","shell":"/bin/bash"})); - rt(&json!({"v":12,"type":"terminal-output","term":"t1","offset":0,"data":"bHMgLWxhCg=="})); - rt(&json!({"v":12,"type":"terminal-input-from","term":"t1","seq":4})); - rt(&json!({"v":12,"type":"terminal-exited","term":"t1","code":0,"reason":"exit"})); - rt(&json!({"v":12,"type":"terminal-exited","openId":"o1","reason":"not-allowed","device":"d1"})); - rt(&json!({"v":12,"type":"terminal-event","term":"t1","what":"takeover","device":"d1","name":"Pixel 8","via":"relay","at":1792008058637_u64})); - rt(&json!({"v":12,"type":"web-sign-in-result","code":"K7QM-4XPA","signedIn":true})); - rt(&json!({"v":12,"type":"web-browsers","browsers":[]})); - rt(&json!({"v":12,"type":"web-browsers","browsers":[{ - "id":"0123456789abcdef","by":"npub1owner","sinceMs":1792008058637_u64,"lastSeenMs":1792008059000_u64, - "expiresMs":1804598458637_u64,"peer":"192.168.1.20","agent":"Mozilla/5.0"}]})); } #[test] @@ -894,21 +818,4 @@ mod tests { assert_eq!(e.what, crate::common::TerminalEventKind::Unknown); assert_eq!(e.via, crate::common::TerminalVia::Unknown); } - - #[test] - fn a_typed_entry_survives_the_full_message() { - let m = rt(&json!({"v":12,"type":"output","sessionId":"s","seq":1,"entries":[{ - "timestamp":"t","entryType":"diff","path":"f","lines":[{"type":"add","text":"a"}],"truncated":true - }]})); - match m { - BridgeToPhone::Output(o) => match &o.entries[0].body { - EntryBody::Diff { truncated, lines, .. } => { - assert!(*truncated); - assert_eq!(lines.len(), 1); - } - other => panic!("{other:?}"), - }, - _ => panic!(), - } - } } diff --git a/crates/protocol/src/kinds.rs b/crates/protocol/src/kinds.rs index 555c0ccd..21182f98 100644 --- a/crates/protocol/src/kinds.rs +++ b/crates/protocol/src/kinds.rs @@ -57,14 +57,4 @@ mod tests { assert_eq!(RESPONSE_EXPIRY_SECONDS, 3600); assert_eq!(CODEDECK_KINDS, [30515, 4515, 4516, 24515]); } - - #[test] - fn live_kind_is_in_the_ephemeral_range() { - assert!((20000..30000).contains(&LIVE_KIND)); - } - - #[test] - fn session_list_kind_is_addressable_replaceable() { - assert!((30000..40000).contains(&SESSION_LIST_KIND)); - } } diff --git a/crates/protocol/src/lib.rs b/crates/protocol/src/lib.rs index d742d493..74cacf32 100644 --- a/crates/protocol/src/lib.rs +++ b/crates/protocol/src/lib.rs @@ -33,3 +33,11 @@ pub use codec::{ decode_bridge_to_phone, decode_phone_to_bridge, encode_bridge_to_phone, encode_phone_to_bridge, DecodeResult, }; + +/// The fixed secret key this crate's tests derive identities from (NIP-44's +/// own first vector). Derivation from it is deterministic, so a test computes +/// the expected key or signature from this one value instead of hard-coding +/// them a second time. +#[cfg(test)] +pub(crate) const TEST_SECRET_HEX: &str = + "0000000000000000000000000000000000000000000000000000000000000001"; diff --git a/crates/protocol/src/nip42.rs b/crates/protocol/src/nip42.rs index d20aad79..4e0d3714 100644 --- a/crates/protocol/src/nip42.rs +++ b/crates/protocol/src/nip42.rs @@ -36,7 +36,7 @@ mod tests { use super::*; use crate::crypto::keypair_from_secret_hex; - const SEC: &str = "0000000000000000000000000000000000000000000000000000000000000001"; + use crate::TEST_SECRET_HEX as SEC; fn tag<'a>(e: &'a SignedEvent, name: &str) -> Option<&'a str> { e.tags From 6aec1cdda65ebdc8389249aeb65bca0ce1a6ea73 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:00:23 +0100 Subject: [PATCH 16/42] test(transport): wait for the event a test asserts instead of a flat sleep The four fixed sleeps in the relay tests now wait on what they stood for: the delivered event and EOSE, the close callback, a barrier event from a second subscription or from every relay. The onion cleartext test dials through the in-test SOCKS5 proxy, so it no longer needs an .onion DNS lookup. The direct-link budget tests assert what the peer receives and whether a later publish still fits, not private byte counters, and the backoff test pins jitter bounds and the cap rather than recomputing the implementation's formula. wait_until is shared from test_support. Co-Authored-By: Claude Code --- crates/nostr-transport/src/direct.rs | 48 ++++++------ crates/nostr-transport/src/test_support.rs | 13 ++++ crates/nostr-transport/src/ws.rs | 90 +++++++++++++++------- 3 files changed, 98 insertions(+), 53 deletions(-) diff --git a/crates/nostr-transport/src/direct.rs b/crates/nostr-transport/src/direct.rs index 98a12eac..d1113bb2 100644 --- a/crates/nostr-transport/src/direct.rs +++ b/crates/nostr-transport/src/direct.rs @@ -566,6 +566,7 @@ impl ServerCertVerifier for Pinned { #[cfg(test)] mod tests { use super::*; + use crate::test_support::wait_until; use protocol::crypto::generate_keypair; use tokio::net::TcpListener; use tokio::task::LocalSet; @@ -661,15 +662,12 @@ mod tests { tokio::task::spawn_local(async move { link.publish(&ev, Duration::from_millis(100)).await }) }; let _frame = rx.recv().await.expect("the first frame was queued"); - assert_eq!((link.0.queued_bytes.get(), link.0.pending_bytes.get()), (len, len)); // The second would push the link past its budget: it fails now // and the link closes instead of growing. assert!(link.publish(&ev, Duration::from_millis(50)).await.is_none()); assert!(link.0.up.borrow().is_none(), "the link was closed"); assert!(first.await.unwrap().is_none(), "the queued publish got no OK"); - assert_eq!(link.0.pending_bytes.get(), 0); - assert!(link.0.pending.borrow().is_empty()); }) .await; } @@ -680,14 +678,15 @@ mod tests { .run_until(async { let ev = a_signed_event("e1", "hello"); let len = encode_direct_frame(&DirectFrame::Event(ev.clone())).len(); - let (link, mut rx) = up_link(len * 10); + // The queued frame plus one more reservation fit, but the + // queued frame plus two do not. + let (link, mut rx) = up_link(len * 3 - 1); let waiting = { let (link, ev) = (link.clone(), ev.clone()); tokio::task::spawn_local(async move { link.publish(&ev, Duration::from_secs(2)).await }) }; let _frame = rx.recv().await.expect("the frame was queued"); - assert_eq!(link.0.pending_bytes.get(), len); let ok = encode_direct_frame(&DirectFrame::Ok { id: "e1".into(), @@ -696,26 +695,21 @@ mod tests { }); assert!(link.on_frame(&ok)); assert_eq!(waiting.await.unwrap().unwrap().verdict, PublishVerdict::Accepted); - assert_eq!(link.0.pending_bytes.get(), 0); - assert!(link.0.pending.borrow().is_empty()); - // Not written (no serve loop): the queue still holds its bytes, - // and the link is still up. - assert_eq!(link.0.queued_bytes.get(), len); + + // The OK released the reservation: a second publish now fits + // the budget (without the release it would fail and close the + // link). Not written (no serve loop), and the link is still up. + let next = a_signed_event("e2", "hello"); + assert!( + link.publish(&next, Duration::from_millis(20)).await.is_none(), + "no OK is ever sent for the second publish" + ); + assert!(rx.try_recv().is_ok(), "the second publish fit the budget after the first released its bytes"); assert!(link.0.up.borrow().is_some()); }) .await; } - async fn wait_until(mut cond: impl FnMut() -> bool) { - for _ in 0..300 { - if cond() { - return; - } - tokio::time::sleep(Duration::from_millis(10)).await; - } - panic!("condition never became true"); - } - #[tokio::test] async fn closing_the_link_ends_a_serve_blocked_on_a_write_and_reports_it_down() { LocalSet::new() @@ -786,18 +780,23 @@ mod tests { tokio::task::spawn_local(async move { link.publish(&ev, Duration::from_secs(2)).await }) }; let _frame = rx.recv().await.expect("the first frame was queued"); - assert_eq!(link.0.pending_bytes.get(), len); let second = { let (link, ev) = (link.clone(), ev.clone()); tokio::task::spawn_local(async move { link.publish(&ev, Duration::from_secs(2)).await }) }; let _frame = rx.recv().await.expect("the replacement frame was queued"); - // The superseded waiter released its bytes: only the new - // reservation is counted. - assert_eq!(link.0.pending_bytes.get(), len); assert!(first.await.unwrap().is_none(), "the superseded publish got no answer"); + // The superseded waiter released its bytes: with the budget at + // four frames, a third distinct publish fits only because the + // first reservation was given back (otherwise it would exceed + // the budget and close the link). + let third = a_signed_event("e2", "hello"); + assert!(link.publish(&third, Duration::from_millis(20)).await.is_none(), "no OK is sent"); + assert!(rx.try_recv().is_ok(), "the third publish fit, so the superseded reservation was released"); + assert!(link.0.up.borrow().is_some()); + let ok = encode_direct_frame(&DirectFrame::Ok { id: "e1".into(), accepted: true, @@ -805,7 +804,6 @@ mod tests { }); assert!(link.on_frame(&ok)); assert_eq!(second.await.unwrap().unwrap().verdict, PublishVerdict::Accepted); - assert_eq!(link.0.pending_bytes.get(), 0); }) .await; } diff --git a/crates/nostr-transport/src/test_support.rs b/crates/nostr-transport/src/test_support.rs index ddcae23a..937c1ffc 100644 --- a/crates/nostr-transport/src/test_support.rs +++ b/crates/nostr-transport/src/test_support.rs @@ -11,6 +11,19 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::{TcpListener, TcpStream}; use tokio::sync::oneshot; +/// Poll a condition until it holds, yielding between checks. The alternative, +/// a flat sleep, is both slower than needed and a race with however long the +/// event it stands for takes on a loaded machine. +pub(crate) async fn wait_until(mut cond: impl FnMut() -> bool) { + for _ in 0..300 { + if cond() { + return; + } + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + panic!("condition never became true"); +} + /// What a SOCKS5 client asked the proxy to CONNECT to. pub(crate) struct Socks5Request { /// The SOCKS5 address type: 1 IPv4, 3 domain name, 4 IPv6. diff --git a/crates/nostr-transport/src/ws.rs b/crates/nostr-transport/src/ws.rs index e91cae08..c57c3118 100644 --- a/crates/nostr-transport/src/ws.rs +++ b/crates/nostr-transport/src/ws.rs @@ -1308,6 +1308,7 @@ async fn dial(relay: &str, proxy: Option) -> Result mod tests { use super::*; use crate::mock::{mock_relay, MockRelay}; + use crate::test_support::wait_until; use protocol::crypto::{generate_keypair, keypair_from_secret_hex, Keypair}; use nostr::JsonUtil; use std::cell::RefCell; @@ -1332,12 +1333,18 @@ mod tests { #[tokio::test] async fn dial_does_not_refuse_cleartext_to_onion_on_scheme_grounds() { - // No real Tor network in a test — this fails on the actual connection - // attempt (no proxy, an .onion address does not resolve over plain - // DNS), but that failure must be a dial/DNS error, never the + // A loopback stand-in for Tor: the proxy is asked to CONNECT to the + // onion name (no local resolver sees it), answers success and closes, + // so the dial fails at the WebSocket handshake — never the // cleartext-rejection this test exists to rule out. - let err = dial("ws://expyuzz4wqqyqhjn.onion", None).await.unwrap_err(); + let (proxy, request) = crate::test_support::socks5_proxy().await; + let err = dial("ws://expyuzz4wqqyqhjn.onion:7447", Some(proxy.to_string())).await.unwrap_err(); assert!(!err.contains("cleartext"), "{err}"); + let request = tokio::time::timeout(Duration::from_secs(5), request) + .await + .expect("the onion dial never reached the proxy") + .expect("the proxy reported its CONNECT"); + assert_eq!(request.host, "expyuzz4wqqyqhjn.onion", "the proxy must resolve the onion name, not this machine"); } #[tokio::test] @@ -1450,16 +1457,6 @@ mod tests { .await; } - async fn wait_until(mut cond: impl FnMut() -> bool) { - for _ in 0..200 { - if cond() { - return; - } - tokio::time::sleep(Duration::from_millis(10)).await; - } - panic!("condition never became true"); - } - #[tokio::test] async fn a_deliberate_shutdown_stops_reporting_the_relay_as_connected_at_once() { LocalSet::new() @@ -1684,7 +1681,7 @@ mod tests { mock.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "hi"))); mock.push(r#"["EOSE","cd-1"]"#.to_string()); - tokio::time::sleep(Duration::from_millis(120)).await; + wait_until(|| events.borrow().len() == 1 && *eosed.borrow()).await; assert_eq!(events.borrow().len(), 1); assert_eq!(events.borrow()[0].kind, 24515); @@ -1946,7 +1943,7 @@ mod tests { let _req = mock.next_frame().await; mock.close(); - tokio::time::sleep(Duration::from_millis(150)).await; + wait_until(|| *closes.borrow() == 1).await; assert_eq!(*closes.borrow(), 1); }) .await; @@ -1973,12 +1970,29 @@ mod tests { ); let _req = mock.next_frame().await; + // A second, live subscription is the barrier that proves the + // late frame was handled: frames arrive over one socket in + // order, so its delivery follows the dropped one. + let live: Rc>> = Rc::new(RefCell::new(vec![])); + let live_c = Rc::clone(&live); + let _live_sub = t.subscribe( + a_filter(&phone), + SubCallbacks { + on_event: Rc::new(move |ev| live_c.borrow_mut().push(ev.clone())), + on_eose: Rc::new(|| {}), + on_close: Rc::new(|_| {}), + }, + ); + assert!(mock.next_frame().await.starts_with(r#"["REQ","cd-2","#)); + sub.close(); assert_eq!(mock.next_frame().await, r#"["CLOSE","cd-1"]"#); - // a late EVENT for the dropped sub must not reach the callback + // A late EVENT for the dropped sub must not reach its callback, + // while the next one for the live sub still does. mock.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "late"))); - tokio::time::sleep(Duration::from_millis(100)).await; + mock.push(format!(r#"["EVENT","cd-2",{}]"#, signed_note(24515, "live"))); + wait_until(|| live.borrow().len() == 1).await; assert!(events.borrow().is_empty()); }) .await; @@ -2070,12 +2084,23 @@ mod tests { #[test] fn the_redial_delay_grows_per_failure_up_to_the_cap() { - for attempt in 1..=20 { - let full = RETRY_BASE.saturating_mul(1 << (attempt - 1).min(16)).min(RETRY_MAX); - let delay = TIMING.retry_delay(attempt, "wss://r.example"); - assert!(delay >= full / 2 && delay <= full, "attempt {attempt}: {delay:?}"); + let delay = |attempt| TIMING.retry_delay(attempt, "wss://r.example"); + // Each step doubles until the cap, and jitter stays in a step's upper + // half, so consecutive steps keep rising while there is room to double. + let mut previous = Duration::ZERO; + for attempt in 1..=8 { + let d = delay(attempt); + assert!(d >= previous, "attempt {attempt}: {d:?} below the previous step {previous:?}"); + previous = d; + } + // Past that the cap holds, and jitter never takes a delay below the + // first step's floor — a failing relay is never hammered. + for attempt in 1..=30 { + let d = delay(attempt); + assert!(d <= RETRY_MAX, "attempt {attempt}: {d:?} past the cap"); + assert!(d >= RETRY_BASE / 2, "attempt {attempt}: {d:?} below the first step's floor"); } - assert!(TIMING.retry_delay(30, "wss://r.example") <= RETRY_MAX); + assert!(delay(30) >= RETRY_MAX / 2, "the delay settles within the cap's jitter band"); } #[tokio::test] @@ -2259,14 +2284,23 @@ mod tests { wait_until(|| seen.eoses.get() == 1).await; assert_eq!(seen.events.borrow().len(), 1); - // A second event, live, from a few relays: still one. + // A second event, live, from a few relays: still one. Each of + // those relays also sends a distinct barrier event after it; + // once every barrier is delivered, every copy of the live + // event has been through dedup too. let live = signed_note(24515, "live"); - for m in &mocks[..5] { + let live_id = serde_json::from_str::(&live).unwrap()["id"].as_str().unwrap().to_string(); + for (i, m) in mocks.iter().enumerate().take(5) { m.push(format!(r#"["EVENT","cd-1",{live}]"#)); + m.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, &format!("barrier {i}")))); } - wait_until(|| seen.events.borrow().len() == 2).await; - tokio::time::sleep(Duration::from_millis(100)).await; - assert_eq!(seen.events.borrow().len(), 2); + wait_until(|| seen.events.borrow().len() == 7).await; + assert_eq!( + seen.events.borrow().iter().filter(|id| **id == live_id).count(), + 1, + "the live event was delivered more than once: {:?}", + seen.events.borrow() + ); // Nineteen relays dropping is not a close; the last one is. for m in &mocks[..19] { From 3632f96a7f4f73260cda3460c8b9e1de308d4699 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:02:36 +0100 Subject: [PATCH 17/42] test(client): wait on the state each test asserts, and drop tautologies The runtime suite's 120 ms settle() gave way to waits on the event that names the behaviour: a connection status, a spy event, a view value, the session-key store or the signer counter. The eleven remaining uses are checks that something did NOT happen, fake-clock steps that must land before the test moves the clock, silent-event timing the views cannot name, and private backup state. Removed: the decide_* truth tables the coordinator tests already exercise, the backoff formula restatement and a one-line signer JSON wrapper, three view projections that only copy store fields, and the DTO copies in the FFI intent mapping (one representative mapping remains). The hydrated store fixture is shared from one test_support module. Co-Authored-By: Claude Code --- crates/client-core/src/connection.rs | 22 +- crates/client-core/src/image_chunks.rs | 8 - crates/client-core/src/notifications.rs | 24 -- crates/client-ffi/src/intent.rs | 165 -------- crates/client-runtime/src/dispatch.rs | 8 +- crates/client-runtime/src/intent.rs | 9 +- crates/client-runtime/src/lib.rs | 3 + crates/client-runtime/src/runtime/tests.rs | 425 ++++++++++++++++----- crates/client-runtime/src/signer.rs | 11 - crates/client-runtime/src/test_support.rs | 24 ++ crates/client-runtime/src/view.rs | 56 +-- 11 files changed, 368 insertions(+), 387 deletions(-) create mode 100644 crates/client-runtime/src/test_support.rs diff --git a/crates/client-core/src/connection.rs b/crates/client-core/src/connection.rs index cd2280b4..8ca83003 100644 --- a/crates/client-core/src/connection.rs +++ b/crates/client-core/src/connection.rs @@ -432,17 +432,6 @@ mod tests { // --- backoff + jitter --- - #[test] - fn exponential_base_with_30s_cap() { - let d = |a| backoff_delay_ms(a, 0.0, CFG); - assert_eq!(d(0), RECONNECT_BASE_MS); - assert_eq!(d(1), 4_000); - assert_eq!(d(2), 8_000); - assert_eq!(d(3), 16_000); - assert_eq!(d(4), RECONNECT_MAX_MS); - assert_eq!(d(50), RECONNECT_MAX_MS); - } - #[test] fn the_tor_config_scales_the_connect_deadline_with_the_slower_transport() { const { assert!(DEFAULT_RECONNECT_CONFIG.connect_deadline_ms == 45_000) }; @@ -453,14 +442,17 @@ mod tests { } #[test] - fn jitter_adds_at_most_25pct_never_negative() { + fn jitter_never_brings_a_retry_forward_and_clamps_out_of_range_entropy() { for attempt in [0, 1, 2, 3, 4, 10] { let base = backoff_delay_ms(attempt, 0.0, CFG); - let max_jitter = (base as f64 * RECONNECT_JITTER_FRACTION).floor() as u64; - assert!(backoff_delay_ms(attempt, 1.0, CFG) <= base + max_jitter); assert!(backoff_delay_ms(attempt, 0.5, CFG) >= base); + // Entropy is a fraction: anything below the range means none, + // anything above it means the same as the top of the range. assert_eq!(backoff_delay_ms(attempt, -5.0, CFG), base); - assert_eq!(backoff_delay_ms(attempt, 99.0, CFG), base + max_jitter); + assert_eq!( + backoff_delay_ms(attempt, 99.0, CFG), + backoff_delay_ms(attempt, 1.0, CFG) + ); } } diff --git a/crates/client-core/src/image_chunks.rs b/crates/client-core/src/image_chunks.rs index 590b755a..a598152c 100644 --- a/crates/client-core/src/image_chunks.rs +++ b/crates/client-core/src/image_chunks.rs @@ -69,14 +69,6 @@ mod tests { assert_eq!(chunk_base64("abcdefgh", 3), vec!["abc", "def", "gh"]); } - #[test] - fn base64_to_bytes_round_trips_and_handles_empty() { - let bytes = [0u8, 1, 2, 250, 255, 128]; - let b64 = base64::engine::general_purpose::STANDARD.encode(bytes); - assert_eq!(base64_to_bytes(&b64).unwrap(), bytes); - assert!(base64_to_bytes("").unwrap().is_empty()); - } - #[test] fn blossom_hash_from_url_extracts_the_trailing_segment() { let hash = "f".repeat(64); diff --git a/crates/client-core/src/notifications.rs b/crates/client-core/src/notifications.rs index 86b2348e..e7b455af 100644 --- a/crates/client-core/src/notifications.rs +++ b/crates/client-core/src/notifications.rs @@ -379,30 +379,6 @@ mod tests { OutputEntry::new("1970-01-01T00:00:00.000Z", body) } - #[test] - fn decide_notify_matrix() { - // hidden -> always - assert!(decide_notify(&perm("m", "s"), false, Some("m s"))); - // visible + viewing this session -> no - assert!(!decide_notify(&perm("m", "s"), true, Some("m s"))); - // visible + viewing a different session -> yes - assert!(decide_notify(&perm("m", "s"), true, Some("m other"))); - // visible + no session panel (e.g. the session list) -> yes - assert!(decide_notify(&perm("m", "s"), true, None)); - } - - #[test] - fn decide_ping_matrix() { - // hidden -> always - assert!(decide_ping(&perm("m", "s"), false, Some("m s"))); - // visible + viewing this session -> no - assert!(!decide_ping(&perm("m", "s"), true, Some("m s"))); - // visible + viewing a different session -> yes - assert!(decide_ping(&perm("m", "s"), true, Some("m other"))); - // visible + no session panel -> yes - assert!(decide_ping(&perm("m", "s"), true, None)); - } - #[test] fn keys_and_tags() { assert_eq!(notify_key(&perm("m", "s")), "permission-request m s"); diff --git a/crates/client-ffi/src/intent.rs b/crates/client-ffi/src/intent.rs index 1c7055d4..059c7985 100644 --- a/crates/client-ffi/src/intent.rs +++ b/crates/client-ffi/src/intent.rs @@ -853,30 +853,6 @@ mod tests { ); } - #[test] - fn send_session_file_maps_field_for_field() { - let intent = UniffiIntent::SendSessionFile { - machine: "m".into(), - session_id: "s".into(), - text: "look at this".into(), - data: vec![0x89, b'P', b'N', b'G'], - filename: "cat.png".into(), - mime_type: "image/png".into(), - }; - let mapped: Intent = intent.try_into().unwrap(); - assert_eq!( - mapped, - Intent::SendSessionFile(SessionFileSend { - machine: "m".into(), - session_id: "s".into(), - text: "look at this".into(), - data: vec![0x89, b'P', b'N', b'G'], - filename: "cat.png".into(), - mime_type: "image/png".into(), - }) - ); - } - #[test] fn set_option_parses_the_same_wire_spelling_the_protocol_uses() { let intent = UniffiIntent::SetOption { @@ -967,40 +943,6 @@ mod tests { )); } - #[test] - fn set_provider_profile_upsert_maps_field_for_field() { - let intent = UniffiIntent::SetProviderProfile { - machine: "m".into(), - profile_id: "kimi-k3".into(), - profile: Some(UniffiProviderProfileWrite { - agent: "claude-code".into(), - label: "Kimi K3".into(), - base_url: "https://api.moonshot.ai/anthropic".into(), - auth_token: UniffiTristate::Set { value: "sk-1".into() }, - models: vec![UniffiProviderModelWrite { id: "kimi-k3".into(), label: Some("Kimi K3".into()) }], - models_from_provider: true, - default_model: Some("kimi-k3".into()), - }), - }; - let mapped: Intent = intent.try_into().unwrap(); - assert_eq!( - mapped, - Intent::SetProviderProfile { - machine: "m".into(), - profile_id: "kimi-k3".into(), - profile: Some(ProviderProfileWrite { - agent: "claude-code".into(), - label: "Kimi K3".into(), - base_url: "https://api.moonshot.ai/anthropic".into(), - auth_token: Tristate::Set("sk-1".into()), - models: vec![ProviderModel { id: "kimi-k3".into(), label: Some("Kimi K3".into()), ..Default::default() }], - models_from_provider: true, - default_model: Some("kimi-k3".into()), - }), - } - ); - } - #[test] fn set_provider_profile_none_maps_to_delete() { let intent = UniffiIntent::SetProviderProfile { @@ -1045,111 +987,4 @@ mod tests { assert_eq!(mapped, Intent::ClearWebSignIn { machine: "m".into() }); } - #[test] - fn delete_session_maps_field_for_field() { - let intent = UniffiIntent::DeleteSession { - machine: "m".into(), - session_id: "s".into(), - label: Some("Deleted the-slug".into()), - }; - let mapped: Intent = intent.try_into().unwrap(); - assert_eq!( - mapped, - Intent::DeleteSession { - machine: "m".into(), - session_id: "s".into(), - label: Some("Deleted the-slug".into()), - } - ); - } - - #[test] - fn undo_delete_maps_to_the_unit_variant() { - let mapped: Intent = UniffiIntent::UndoDelete.try_into().unwrap(); - assert_eq!(mapped, Intent::UndoDelete); - } - - #[test] - fn dismiss_pending_session_maps_field_for_field() { - let intent = UniffiIntent::DismissPendingSession { pending_id: "p1".into() }; - let mapped: Intent = intent.try_into().unwrap(); - assert_eq!(mapped, Intent::DismissPendingSession { pending_id: "p1".into() }); - } - - #[test] - fn the_terminal_intents_map_field_for_field() { - let mapped: Intent = UniffiIntent::OpenTerminal { - machine: "m".into(), - session: Some("s1".into()), - cols: 100, - rows: 40, - } - .try_into() - .unwrap(); - assert_eq!( - mapped, - Intent::OpenTerminal { machine: "m".into(), session: Some("s1".into()), cols: 100, rows: 40 } - ); - - let mapped: Intent = UniffiIntent::AttachTerminal { machine: "m".into(), term: "t1".into() } - .try_into() - .unwrap(); - assert_eq!(mapped, Intent::AttachTerminal { machine: "m".into(), term: "t1".into() }); - - let mapped: Intent = UniffiIntent::DetachTerminal { machine: "m".into() }.try_into().unwrap(); - assert_eq!(mapped, Intent::DetachTerminal { machine: "m".into() }); - - let mapped: Intent = UniffiIntent::TerminalInput { - machine: "m".into(), - term: "t1".into(), - bytes: b"\x1b[A".to_vec(), - alternate_screen: true, - } - .try_into() - .unwrap(); - assert_eq!( - mapped, - Intent::TerminalInput { - machine: "m".into(), - term: "t1".into(), - bytes: b"\x1b[A".to_vec(), - alternate_screen: true, - } - ); - - let mapped: Intent = UniffiIntent::TerminalPaste { - machine: "m".into(), - term: "t1".into(), - bytes: b"pasted".to_vec(), - } - .try_into() - .unwrap(); - assert_eq!( - mapped, - Intent::TerminalPaste { machine: "m".into(), term: "t1".into(), bytes: b"pasted".to_vec() } - ); - - let mapped: Intent = UniffiIntent::ResizeTerminal { - machine: "m".into(), - term: "t1".into(), - cols: 132, - rows: 43, - } - .try_into() - .unwrap(); - assert_eq!( - mapped, - Intent::ResizeTerminal { machine: "m".into(), term: "t1".into(), cols: 132, rows: 43 } - ); - - let mapped: Intent = UniffiIntent::TakeOverTerminal { machine: "m".into(), term: "t1".into() } - .try_into() - .unwrap(); - assert_eq!(mapped, Intent::TakeOverTerminal { machine: "m".into(), term: "t1".into() }); - - let mapped: Intent = UniffiIntent::CloseTerminal { machine: "m".into(), term: "t1".into() } - .try_into() - .unwrap(); - assert_eq!(mapped, Intent::CloseTerminal { machine: "m".into(), term: "t1".into() }); - } } diff --git a/crates/client-runtime/src/dispatch.rs b/crates/client-runtime/src/dispatch.rs index 5a6a3dce..67f2209f 100644 --- a/crates/client-runtime/src/dispatch.rs +++ b/crates/client-runtime/src/dispatch.rs @@ -1080,12 +1080,8 @@ mod tests { } async fn stores() -> (CoreStores, MemoryTranscriptStore, PhoneKeys) { - let kv = MemoryKv::new(); - let ts = MemoryTranscriptStore::new(); - let h = hydrate(&kv, &ts, &StoresConfig::default()).await; - let identity = protocol::crypto::generate_keypair(); - let (ring, _) = client_core::stores::session_key::SessionKeyRing::load(None, 1_000); - (h.stores, ts, PhoneKeys::new(&identity.pubkey_hex, &ring.current, None)) + let (stores, ts) = crate::test_support::stores_with_transcript().await; + (stores, ts, crate::test_support::phone_keys()) } fn text_entry(content: &str) -> OutputEntry { diff --git a/crates/client-runtime/src/intent.rs b/crates/client-runtime/src/intent.rs index 13cc060c..98b4f936 100644 --- a/crates/client-runtime/src/intent.rs +++ b/crates/client-runtime/src/intent.rs @@ -1264,16 +1264,9 @@ pub(crate) fn apply_delete_effects(stores: &mut CoreStores, effects: Vec (CoreStores, PhoneKeys) { - let kv = MemoryKv::new(); - let ts = MemoryTranscriptStore::new(); - let h = hydrate(&kv, &ts, &StoresConfig::default()).await; - let identity = protocol::crypto::generate_keypair(); - let (ring, _) = client_core::stores::session_key::SessionKeyRing::load(None, 1_000); - (h.stores, PhoneKeys::new(&identity.pubkey_hex, &ring.current, None)) + (crate::test_support::stores().await, crate::test_support::phone_keys()) } fn ctx() -> IntentCtx { diff --git a/crates/client-runtime/src/lib.rs b/crates/client-runtime/src/lib.rs index f7f04d35..2ab46e1f 100644 --- a/crates/client-runtime/src/lib.rs +++ b/crates/client-runtime/src/lib.rs @@ -31,6 +31,9 @@ pub mod signer; pub mod stores; pub mod view; +#[cfg(test)] +mod test_support; + /// The relay transport (WebSocket + SOCKS5 driver, frame codec, publish /// verdicts), shared with other runtimes as the `nostr-transport` crate. pub use nostr_transport as transport; diff --git a/crates/client-runtime/src/runtime/tests.rs b/crates/client-runtime/src/runtime/tests.rs index eb3c740c..028041fd 100644 --- a/crates/client-runtime/src/runtime/tests.rs +++ b/crates/client-runtime/src/runtime/tests.rs @@ -67,10 +67,57 @@ fn connect_deadline() -> u64 { fast_reconnect().connect_deadline_ms } +/// A fixed wait, only where no event names what the test needs: a check that +/// something did NOT happen (give the loop time to prove the absence), a +/// fake-clock step that must land before the test moves the clock on, or a +/// private state the views do not expose. async fn settle() { tokio::time::sleep(Duration::from_millis(120)).await; } +/// Wait for a condition that names the event a test needs, instead of always +/// burning the same wall-clock time and hoping it was enough. +async fn wait_for(mut cond: impl FnMut() -> bool) { + for _ in 0..600 { + if cond() { + return; + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + panic!("condition never became true"); +} + +/// Wait until the live FSM reports `want`. +async fn wait_status(core: &Core, want: ConnectionStatus) { + for _ in 0..600 { + if core.connection_status().await.0 == want { + return; + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + panic!("the core never reached {want:?}"); +} + +async fn wait_connected(core: &Core) { + wait_status(core, ConnectionStatus::Connected).await; +} + +/// [`wait_for`] for a probe that has to query a view (answered in order with +/// everything the loop had already queued). +async fn wait_for_async(mut probe: F) +where + F: FnMut() -> Fut, + Fut: std::future::Future, +{ + for _ in 0..600 { + if probe().await { + return; + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + panic!("condition never became true"); +} + async fn core_for(mock: &MockRelay, phone: &Keypair, spy: Rc) -> Core { core_for_ports(mock, phone, spy, CorePorts::default()).await } @@ -171,7 +218,7 @@ async fn a_stalled_image_upload_does_not_block_the_loop() { core.set_machines(vec![generate_keypair().pubkey_hex]); core.start(); eose_all(&mut mock).await; - settle().await; + wait_connected(&core).await; core.dispatch(Intent::SetBlossomServer("https://blossom.example".into())).await; let core2 = core.clone(); @@ -187,7 +234,11 @@ async fn a_stalled_image_upload_does_not_block_the_loop() { })) .await; }); - settle().await; + // Let the upload task reach the hanging `put` before checking the + // loop still answers. + for _ in 0..10 { + tokio::task::yield_now().await; + } // The upload is still hanging, yet the loop answers at once. tokio::time::timeout(Duration::from_secs(1), core.machines_view()) @@ -681,7 +732,8 @@ async fn start_subscribes_and_reports_connected_after_all_eose() { core.start(); eose_all(&mut mock).await; - settle().await; + wait_for(|| spy.statuses.lock().unwrap().last().is_some_and(|(s, _)| *s == ConnectionStatus::Connected)) + .await; let statuses = spy.statuses.lock().unwrap().clone(); assert!( @@ -734,14 +786,21 @@ async fn a_relay_dying_while_another_survives_is_reported_at_once() { eose_all(&mut mock1).await; eose_all(&mut mock2).await; - settle().await; + wait_for(|| spy.connected_relays.lock().unwrap().last().is_some_and(|r| r.len() == 2)).await; let before = spy.connected_relays.lock().unwrap().last().unwrap().clone(); assert_eq!(before.len(), 2, "{before:?}"); // Real time only, far short of the watchdog's 30 s tick. mock2.close(); - settle().await; + wait_for(|| { + spy.connected_relays + .lock() + .unwrap() + .last() + .is_some_and(|r| r == &vec![mock1.url.clone()]) + }) + .await; let after = spy.connected_relays.lock().unwrap().last().unwrap().clone(); assert_eq!(after, vec![mock1.url.clone()], "{after:?}"); @@ -781,7 +840,7 @@ async fn a_bridge_message_is_decrypted_decoded_and_delivered() { r#"["EVENT","cd-3",{}]"#, ::as_json(&event) )); - settle().await; + wait_for(|| spy.messages.lock().unwrap().len() == 1).await; let messages = spy.messages.lock().unwrap(); assert_eq!(messages.len(), 1, "{messages:?}"); @@ -820,7 +879,13 @@ async fn a_bridge_of_another_version_is_marked_until_it_is_read_again() { host: None, }); push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &ack); - settle().await; + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { core.machines_view().await.machines.contains_key(&key) } + }) + .await; let sub = drain_traffic_resubscribe(&mut mock).await; let push_raw = |plaintext: &str| { @@ -837,12 +902,25 @@ async fn a_bridge_of_another_version_is_marked_until_it_is_read_again() { assert_eq!(mark().await, None); push_raw(r#"{"v":13,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#); - settle().await; + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { + core.machines_view().await.machines[&key].incompatible.map(|i| i.theirs) == Some(Some(13)) + } + }) + .await; assert_eq!(mark().await, Some(Some(13))); let delivered = spy.messages.lock().unwrap().len(); push_raw(r#"{"v":12,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#); - settle().await; + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { core.machines_view().await.machines[&key].incompatible.is_none() } + }) + .await; assert_eq!(mark().await, None); assert_eq!(spy.messages.lock().unwrap().len(), delivered + 1); }) @@ -865,7 +943,6 @@ async fn a_notify_worthy_event_while_backgrounded_fires_the_ping_core_event() { // Backgrounded — `decide_ping` wants a chime unconditionally // once the app isn't visible, for any notify-worthy event. core.pause(); - settle().await; let msg = protocol::codec::decode_bridge_to_phone( r#"{"v":12,"type":"session-failed","pendingId":"p1","reason":"boom"}"#, @@ -886,7 +963,7 @@ async fn a_notify_worthy_event_while_backgrounded_fires_the_ping_core_event() { r#"["EVENT","cd-1",{}]"#, ::as_json(&event) )); - settle().await; + wait_for(|| spy.events.lock().unwrap().contains(&CoreEvent::Ping)).await; let events = spy.events.lock().unwrap(); assert!(events.contains(&CoreEvent::Ping), "{events:?}"); @@ -940,7 +1017,14 @@ async fn create_folder_round_trips_to_a_matching_folder_ack_core_event() { r#"["EVENT","cd-1",{}]"#, ::as_json(&event) )); - settle().await; + wait_for(|| { + spy.events.lock().unwrap().iter().any(|e| matches!( + e, + CoreEvent::FolderAck { request_id, success: true, path: Some(p), error: None } + if request_id == "req-1" && p == "sub/dir" + )) + }) + .await; let events = spy.events.lock().unwrap(); assert!( @@ -1066,17 +1150,22 @@ async fn a_socket_drop_backs_off_and_reconnects() { core.set_machines(vec![machine.pubkey_hex.clone()]); core.start(); eose_all(&mut mock).await; - settle().await; + wait_connected(&core).await; mock.close(); - settle().await; + // fast_reconnect base is 40ms: the backoff and its retry follow. + wait_for(|| { + let statuses = spy.statuses.lock().unwrap(); + statuses.iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry) + && statuses.iter().filter(|(s, _)| *s == ConnectionStatus::Connecting).count() >= 2 + }) + .await; let statuses = spy.statuses.lock().unwrap().clone(); assert!( statuses.iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry), "{statuses:?}" ); - // fast_reconnect base is 40ms — the retry has fired by now. assert!( statuses.iter().filter(|(s, _)| *s == ConnectionStatus::Connecting).count() >= 2, "expected a second Connecting after backoff: {statuses:?}" @@ -1085,24 +1174,6 @@ async fn a_socket_drop_backs_off_and_reconnects() { .await; } -#[test] -fn the_watchdog_is_armed_only_where_it_has_work() { - for status in [ - ConnectionStatus::Idle, - ConnectionStatus::Connecting, - ConnectionStatus::Connected, - ConnectionStatus::WaitingRetry, - ConnectionStatus::Offline, - ConnectionStatus::Stopped, - ] { - assert_eq!( - stale_watchdog_needed(status), - matches!(status, ConnectionStatus::Connecting | ConnectionStatus::Connected), - "{status:?}" - ); - } -} - #[tokio::test] async fn a_connect_that_never_reaches_eose_is_abandoned_and_retried() { LocalSet::new() @@ -1124,12 +1195,18 @@ async fn a_connect_that_never_reaches_eose_is_abandoned_and_retried() { .await; core.set_machines(vec![generate_keypair().pubkey_hex]); core.start(); - settle().await; + wait_status(&core, ConnectionStatus::Connecting).await; assert_eq!(core.connection_status().await.0, ConnectionStatus::Connecting); *clock.0.borrow_mut() += connect_deadline() + 1_000; core.tick(); - settle().await; // fast_reconnect's 40ms retry has fired by now + // fast_reconnect's 40ms retry has fired by now. + wait_for(|| { + let statuses: Vec = + spy.statuses.lock().unwrap().iter().map(|(s, _)| *s).collect(); + statuses.windows(2).any(|w| w == [ConnectionStatus::WaitingRetry, ConnectionStatus::Connecting]) + }) + .await; let statuses: Vec = spy.statuses.lock().unwrap().iter().map(|(s, _)| *s).collect(); @@ -1160,7 +1237,7 @@ async fn a_paused_connect_is_not_abandoned_and_a_resume_restarts_its_clock() { .await; core.set_machines(vec![generate_keypair().pubkey_hex]); core.start(); - settle().await; + wait_status(&core, ConnectionStatus::Connecting).await; assert_eq!(core.connection_status().await.0, ConnectionStatus::Connecting); // Hidden past the 500ms visibility debounce, then dozing: wall @@ -1169,7 +1246,6 @@ async fn a_paused_connect_is_not_abandoned_and_a_resume_restarts_its_clock() { tokio::time::sleep(Duration::from_millis(600)).await; *clock.0.borrow_mut() += connect_deadline() + 60_000; core.tick(); - settle().await; assert_eq!( core.connection_status().await.0, ConnectionStatus::Connecting, @@ -1178,11 +1254,12 @@ async fn a_paused_connect_is_not_abandoned_and_a_resume_restarts_its_clock() { // Resume re-dials while still Connecting: that is a new attempt, // with a fresh window rather than the abandoned one's leftovers. + // The re-dial runs on the fake clock, so let the loop take it in + // before moving that clock. core.resume(); settle().await; *clock.0.borrow_mut() += connect_deadline() - 1_000; core.tick(); - settle().await; assert_eq!( core.connection_status().await.0, ConnectionStatus::Connecting, @@ -1191,7 +1268,10 @@ async fn a_paused_connect_is_not_abandoned_and_a_resume_restarts_its_clock() { *clock.0.borrow_mut() += 2_000; core.tick(); - settle().await; + wait_for(|| { + spy.statuses.lock().unwrap().iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry) + }) + .await; assert!( spy.statuses.lock().unwrap().iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry), "the resumed attempt is abandoned once its own deadline passes" @@ -1211,11 +1291,12 @@ async fn stop_is_terminal_no_reconnect() { core.set_machines(vec![generate_keypair().pubkey_hex]); core.start(); eose_all(&mut mock).await; - settle().await; + wait_connected(&core).await; core.stop(); - settle().await; + wait_status(&core, ConnectionStatus::Stopped).await; mock.close(); + // Nothing after Stopped, even when the socket closes. settle().await; let statuses = spy.statuses.lock().unwrap().clone(); @@ -1242,10 +1323,10 @@ async fn connection_status_query_reflects_the_live_fsm() { assert_eq!(core.connection_status().await.0, ConnectionStatus::Idle); core.start(); eose_all(&mut mock).await; - settle().await; + wait_connected(&core).await; assert_eq!(core.connection_status().await.0, ConnectionStatus::Connected); core.stop(); - settle().await; + wait_status(&core, ConnectionStatus::Stopped).await; assert_eq!(core.connection_status().await.0, ConnectionStatus::Stopped); }) .await; @@ -1287,7 +1368,6 @@ async fn a_machine_paired_in_a_prior_run_is_resubscribed_on_a_fresh_boot_without // Hangs (and `next_frame` panics at its 2s budget) if the // subscription author list is still empty at this point. eose_all(&mut mock).await; - settle().await; // A real heartbeat from that same machine must still reach the // machines view — proving the subscription actually scopes to @@ -1311,7 +1391,19 @@ async fn a_machine_paired_in_a_prior_run_is_resubscribed_on_a_fresh_boot_without r#"["EVENT","cd-1",{}]"#, ::as_json(&event) )); - settle().await; + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { + core.machines_view() + .await + .machines + .get(&key) + .is_some_and(|m| m.last_heartbeat_at.is_some()) + } + }) + .await; let view = core.machines_view().await; let m = view.machines.get(&machine.pubkey_hex).expect("machine still known"); @@ -1331,7 +1423,7 @@ async fn set_relays_repoints_the_transport_to_the_new_relay() { core.set_machines(vec![generate_keypair().pubkey_hex]); core.start(); eose_all(&mut first).await; - settle().await; + wait_connected(&core).await; core.set_relays(vec![second.url.clone()]); // the new relay gets the three REQs; drain them @@ -1353,7 +1445,7 @@ async fn keepalive_probes_the_relays_and_keeps_a_healthy_connection() { core.set_machines(vec![generate_keypair().pubkey_hex]); core.start(); eose_all(&mut mock).await; - settle().await; + wait_connected(&core).await; // The mock answers the probe's ping, so the check is quick // and leaves the connection as it was. @@ -1411,17 +1503,8 @@ async fn connection_view_query_reflects_the_live_status() { assert_eq!(core.connection_view().await.unwrap().status, "idle"); core.start(); eose_all(&mut mock).await; - // let the EOSEs propagate through the transport → NostrClient → - // the FSM (a few extra spawn_local tasks now share the loop). - let mut status = String::new(); - for _ in 0..10 { - settle().await; - status = core.connection_view().await.unwrap().status; - if status == "connected" { - break; - } - } - assert_eq!(status, "connected"); + wait_connected(&core).await; + assert_eq!(core.connection_view().await.unwrap().status, "connected"); }) .await; } @@ -1443,7 +1526,6 @@ async fn an_intent_becomes_a_signed_command_on_the_wire() { session_id: "s1".into(), }) .await; - settle().await; // among the published EVENTs find the one wrapped for the // machine. @@ -1504,7 +1586,7 @@ async fn editing_a_machines_relays_repoints_the_transport() { // Dialled from the stored machine alone: no relay configured. core.start(); eose_all(&mut first).await; - settle().await; + wait_connected(&core).await; core.dispatch(Intent::SetMachineRelays { machine: machine.pubkey_hex.clone(), @@ -1595,7 +1677,7 @@ async fn coming_back_to_a_session_cancels_what_was_notified_while_away() { .await; core.pause(); core.resume(); - settle().await; + wait_for(|| notifier.cancelled().len() == 2).await; let tag = client_core::notifications::session_notify_tag("m1", "s1"); assert_eq!(notifier.cancelled(), [tag.clone(), tag]); @@ -1677,7 +1759,7 @@ async fn sending_a_session_image_uploads_to_blossom_and_publishes_the_command() core.set_machines(vec![machine.pubkey_hex.clone()]); core.start(); eose_all(&mut mock).await; - settle().await; + wait_connected(&core).await; core.dispatch(Intent::SetBlossomServer("https://blossom.example".into())).await; @@ -1735,7 +1817,7 @@ async fn with_no_blossom_server_a_file_goes_through_the_relays() { core.set_machines(vec![machine.pubkey_hex.clone()]); core.start(); eose_all(&mut mock).await; - settle().await; + wait_connected(&core).await; let core2 = core.clone(); let machine_pubkey = machine.pubkey_hex.clone(); @@ -1790,7 +1872,7 @@ async fn sending_a_session_image_falls_back_to_chunks_when_blossom_is_unreachabl core.set_machines(vec![machine.pubkey_hex.clone()]); core.start(); eose_all(&mut mock).await; - settle().await; + wait_connected(&core).await; core.dispatch(Intent::SetBlossomServer("https://blossom.example".into())).await; @@ -1854,7 +1936,12 @@ async fn the_connection_status_change_emits_a_state_changed_event() { core.set_machines(vec![generate_keypair().pubkey_hex]); core.start(); eose_all(&mut mock).await; - settle().await; + wait_for(|| { + spy.events.lock().unwrap().contains(&CoreEvent::StateChanged { + slice: SliceId::Connection, + }) + }) + .await; let events = spy.events.lock().unwrap(); assert!(events.contains(&CoreEvent::StateChanged { @@ -1896,7 +1983,12 @@ async fn a_session_pending_message_populates_the_view_and_emits_a_state_changed_ r#"["EVENT","cd-3",{}]"#, ::as_json(&event) )); - settle().await; + wait_for(|| { + spy.events.lock().unwrap().contains(&CoreEvent::StateChanged { + slice: SliceId::PendingSessions, + }) + }) + .await; { let events = spy.events.lock().unwrap(); @@ -1944,7 +2036,11 @@ async fn dismiss_pending_session_removes_it_and_emits_a_state_changed_event() { r#"["EVENT","cd-3",{}]"#, ::as_json(&event) )); - settle().await; + wait_for_async(|| { + let core = core.clone(); + async move { core.pending_sessions_view().await.pending.contains_key("p1") } + }) + .await; assert!(core.pending_sessions_view().await.pending.contains_key("p1")); core.dispatch(Intent::DismissPendingSession { @@ -1993,7 +2089,13 @@ async fn an_output_message_populates_the_transcript_view_and_emits_transcript_ap r#"["EVENT","cd-3",{}]"#, ::as_json(&event) )); - settle().await; + wait_for(|| { + spy.events.lock().unwrap().contains(&CoreEvent::TranscriptAppended { + machine: machine.pubkey_hex.clone(), + session_id: "s1".to_string(), + }) + }) + .await; let view = core.transcript_view(machine.pubkey_hex.clone(), "s1".to_string()).await; assert_eq!(view.rows.len(), 1); @@ -2120,7 +2222,6 @@ async fn a_phone_with_no_relays_pairs_over_the_relays_its_pairing_names() { ) .await; core.start(); - settle().await; core.dispatch(Intent::BeginManualPairing { npub: machine.npub.clone(), @@ -2190,7 +2291,13 @@ async fn remove_machine_intent_drops_it_from_the_view_and_erases_its_transcript( host: None, }), ); - settle().await; + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { core.machines_view().await.machines.contains_key(&key) } + }) + .await; assert!(core .machines_view() .await @@ -2211,14 +2318,31 @@ async fn remove_machine_intent_drops_it_from_the_view_and_erases_its_transcript( ) .unwrap(); push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &sessions_msg); - settle().await; + let listed_key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = listed_key.clone(); + async move { + core.machines_view() + .await + .machines + .get(&key) + .is_some_and(|m| m.sessions.contains_key("s1")) + } + }) + .await; let msg = protocol::codec::decode_bridge_to_phone( r#"{"v":12,"type":"output","sessionId":"s1","seq":1,"entries":[{"entryType":"text","role":"agent","text":"hi","timestamp":"t"}]}"#, ) .unwrap(); push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &msg); - settle().await; + wait_for_async(|| { + let core = core.clone(); + let key = machine.pubkey_hex.clone(); + async move { core.transcript_view(key, "s1".to_string()).await.rows.len() == 1 } + }) + .await; // Sanity: the transcript is really there before forgetting the machine. let before = core.transcript_view(machine.pubkey_hex.clone(), "s1".to_string()).await; @@ -2228,7 +2352,12 @@ async fn remove_machine_intent_drops_it_from_the_view_and_erases_its_transcript( pubkey_hex: machine.pubkey_hex.clone(), }) .await; - settle().await; + wait_for_async(|| { + let core = core.clone(); + let key = machine.pubkey_hex.clone(); + async move { core.transcript_view(key, "s1".to_string()).await.rows.is_empty() } + }) + .await; assert!(!core .machines_view() @@ -2303,7 +2432,13 @@ async fn the_undo_toast_clears_itself_when_the_window_expires_without_a_tap() { host: None, }), ); - settle().await; + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { core.machines_view().await.machines.contains_key(&key) } + }) + .await; let sub = drain_traffic_resubscribe(&mut mock).await; let sessions_msg = protocol::codec::decode_bridge_to_phone( @@ -2313,7 +2448,19 @@ async fn the_undo_toast_clears_itself_when_the_window_expires_without_a_tap() { ) .unwrap(); push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &sessions_msg); - settle().await; + let listed_key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = listed_key.clone(); + async move { + core.machines_view() + .await + .machines + .get(&key) + .is_some_and(|m| m.sessions.contains_key("s1")) + } + }) + .await; core.dispatch(Intent::DeleteSession { machine: machine.pubkey_hex.clone(), @@ -2402,7 +2549,8 @@ async fn deleting_sessions_in_a_row_closes_each_one_exactly_once() { )]); let notifier = RecordingNotifier::new(); let ports = CorePorts { kv: Rc::new(kv), notifier: Rc::new(notifier.clone()), ..CorePorts::default() }; - let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + let spy = Rc::new(Spy::default()); + let core = core_for_ports(&mock, &phone, Rc::clone(&spy), ports).await; core.start(); eose_all(&mut mock).await; let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; @@ -2410,7 +2558,13 @@ async fn deleting_sessions_in_a_row_closes_each_one_exactly_once() { let listed = heartbeat_listing(&["s1", "s2", "s3"]); push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &listed); - settle().await; + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { core.machines_view().await.machines[&key].sessions.len() == 3 } + }) + .await; let sessions = |view: MachinesView| -> Vec { view.machines[&machine.pubkey_hex].sessions.keys().cloned().collect() }; @@ -2440,8 +2594,20 @@ async fn deleting_sessions_in_a_row_closes_each_one_exactly_once() { // The bridge has closed s1 only; its next list still has the // other two, which must stay deleted on the phone. let after_first = heartbeat_listing(&["s2", "s3"]); + // A heartbeat for a session already deleted still notifies; wait + // for that to prove the frame was folded in before asserting it + // does not resurrect anything. + let machine_changes = || { + spy.events + .lock() + .unwrap() + .iter() + .filter(|e| matches!(e, CoreEvent::StateChanged { slice: SliceId::Machines })) + .count() + }; + let before = machine_changes(); push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &after_first); - settle().await; + wait_for(|| machine_changes() > before).await; assert!(sessions(core.machines_view().await).is_empty()); // Backgrounded inside s3's undo window: committed now, well @@ -2710,7 +2876,7 @@ async fn a_restored_machine_is_granted_first_and_nothing_to_the_identity_is_decr push_bridge_to_phone_event(&mock, &machine, &grant.pubkey_hex, "cd-1", &listed); let sync = next_command_via(&mut mock, &phone, &grant.pubkey_hex, &machine).await; assert!(matches!(sync, Some(PhoneToBridge::SyncRequest(_))), "{sync:?}"); - settle().await; + wait_for(|| spy.messages.lock().unwrap().len() == 1).await; assert_eq!(decrypts.get(), 0, "the identity decrypted nothing"); assert_eq!(spy.messages.lock().unwrap().len(), 1, "only the heartbeat under the key was read"); }) @@ -2744,7 +2910,7 @@ async fn an_unconfirmed_grant_lets_what_waited_go_on_under_the_identity() { tokio::time::resume(); let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - settle().await; + wait_for(|| decrypts.get() == 1).await; assert_eq!(decrypts.get(), 1, "the held heartbeat is read with the identity"); assert!(spy.messages.lock().unwrap().iter().any(|(_, m)| matches!(m, BridgeToPhone::Sessions(_)))); }) @@ -2913,7 +3079,15 @@ async fn a_machine_settings_change_nobody_answers_stops_being_busy_at_the_deadli tokio::time::pause(); tokio::time::advance(Duration::from_millis(SLOW_SETTINGS_ANSWER_TIMEOUT_MS + 1)).await; tokio::time::resume(); - settle().await; + wait_for_async(|| { + let core = core.clone(); + let key = machine.pubkey_hex.clone(); + async move { + let view = core.machines_view().await; + view.machines[&key].plugins["claude-code"].failure.is_some() + } + }) + .await; let view = core.machines_view().await; let plugins = &view.machines[&machine.pubkey_hex].plugins["claude-code"]; @@ -2981,7 +3155,7 @@ async fn a_rotated_key_is_granted_and_the_old_one_dropped_once_confirmed() { // A heartbeat under the new key confirms it: the old one goes. push_bridge_to_phone_event(&mock, &machine, &new, "cd-1", &heartbeat); - settle().await; + wait_for(|| store.ring().previous.is_none()).await; assert!(store.ring().previous.is_none()); core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; let cmd = next_command_via(&mut mock, &phone, &new, &machine).await; @@ -3236,7 +3410,6 @@ async fn a_burst_of_sync_chunks_costs_one_signed_ack() { other => panic!("expected a sync-ack, got {other:?}"), }; assert_eq!((ack.sync_id.as_str(), ack.ranges), ("y1", vec![(1, 1), (2, 2), (3, 3)])); - settle().await; assert_eq!(signs.get(), 2, "one signature for the three chunks"); }) .await; @@ -3289,7 +3462,12 @@ async fn a_sync_cycle_that_goes_silent_is_re_requested_by_the_watchdog() { ) .unwrap(); push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &begin); - settle().await; + wait_for_async(|| { + let core = core.clone(); + let key = machine.pubkey_hex.clone(); + async move { core.transcript_view(key, "s1".to_string()).await.sync.target == 3 } + }) + .await; *clock.0.borrow_mut() += client_core::stores::transcript::SYNC_STALL_MS + 1; @@ -3355,12 +3533,18 @@ async fn a_sync_stall_is_not_re_requested_on_a_dead_connection() { ) .unwrap(); push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &begin); - settle().await; + wait_for_async(|| { + let core = core.clone(); + let key = machine.pubkey_hex.clone(); + async move { core.transcript_view(key, "s1".to_string()).await.sync.target == 3 } + }) + .await; core.set_online(false); - settle().await; *clock.0.borrow_mut() += client_core::stores::transcript::SYNC_STALL_MS + 1; core.tick(); + // Nothing is published, so there is no event to wait on: give the + // tick's spawned work a moment before asserting the absence. settle().await; assert_eq!(signs.get(), 1, "no request is published without a live connection"); }) @@ -3431,7 +3615,11 @@ async fn backup_on(core: &Core, mock: &mut MockRelay, me: &str) -> String { let sub = answer_backup_req(mock, me, &[]).await; let event = next_frame_of(mock, "EVENT").await[1].clone(); mock.push(serde_json::json!(["OK", event["id"], true, ""]).to_string()); - settle().await; + wait_for_async(|| { + let core = core.clone(); + async move { core.settings_view().await.unwrap().backup.saved_at.is_some() } + }) + .await; sub } @@ -3482,7 +3670,16 @@ async fn a_deletion_on_another_device_turns_backup_off_here() { let signer = crate::signer::LocalSigner(phone.clone()); let removed = crate::backup::seal_removed(&signer, 5_000).await.unwrap(); mock.push(serde_json::json!(["EVENT", sub, removed]).to_string()); - settle().await; + wait_for_async(|| { + let core = core.clone(); + async move { + matches!( + core.settings_view().await.unwrap().backup.status, + crate::backup::BackupStatus::Failed { .. } + ) + } + }) + .await; let backup = settings_backup(&core).await; assert_eq!(backup.relay, None); assert!(matches!(backup.status, crate::backup::BackupStatus::Failed { reason } if reason.contains("another device"))); @@ -3521,7 +3718,16 @@ async fn a_backup_relay_ending_the_subscription_shows_as_a_failure() { core.start(); let sub = backup_on(&core, &mut mock, &phone.pubkey_hex).await; mock.push(serde_json::json!(["CLOSED", sub, "restricted: members only"]).to_string()); - settle().await; + wait_for_async(|| { + let core = core.clone(); + async move { + matches!( + core.settings_view().await.unwrap().backup.status, + crate::backup::BackupStatus::Failed { .. } + ) + } + }) + .await; let backup = settings_backup(&core).await; assert!(backup.relay.is_some(), "backup stays on, and is asked for again later"); assert!(matches!(backup.status, crate::backup::BackupStatus::Failed { reason } if reason.contains("members only"))); @@ -3563,7 +3769,11 @@ async fn a_backup_saved_by_one_phone_restores_the_machines_on_another() { assert!(!wire.contains("my laptop") && !wire.contains("Work lab") && !wire.contains(&machine.pubkey_hex)); assert!(!wire.contains(&old_secret)); first.push(serde_json::json!(["OK", event["id"], true, ""]).to_string()); - settle().await; + wait_for_async(|| { + let old = old.clone(); + async move { old.settings_view().await.unwrap().backup.saved_at.is_some() } + }) + .await; let saved = settings_backup(&old).await; assert_eq!(saved.relay.as_deref(), Some(first.url.as_str())); assert!(saved.saved_at.is_some()); @@ -3577,6 +3787,10 @@ async fn a_backup_saved_by_one_phone_restores_the_machines_on_another() { new.start(); new.dispatch(Intent::SetBackupRelay(second.url.clone())).await; answer_backup_req(&mut second, &phone.pubkey_hex, &[event]).await; + // The backup's "the relay answered" flag is private, and both the + // merged machine and an Idle status can be observed while its EOSE + // is still in flight, so no view names the point where a save is + // allowed to run. Give the answer time to be folded in. settle().await; let machines = new.machines_view().await.machines; let restored = &machines[&machine.pubkey_hex]; @@ -3899,7 +4113,11 @@ async fn keys_held_back_redraw_the_predicted_echo() { core.dispatch(Intent::AttachTerminal { machine: machine.pubkey_hex.clone(), term: "t1".into() }) .await; - settle().await; + wait_for_async(|| { + let core = core.clone(); + async move { core.ui_view().await.terminals.attached.is_some() } + }) + .await; let ui_changes = || { spy.events .lock() @@ -3916,7 +4134,13 @@ async fn keys_held_back_redraw_the_predicted_echo() { alternate_screen: false, }) .await; - settle().await; + wait_for_async(|| { + let core = core.clone(); + async move { + core.ui_view().await.terminals.attached.unwrap().waiting_input == b"l".to_vec() + } + }) + .await; assert_eq!(core.ui_view().await.terminals.attached.unwrap().waiting_input, b"l".to_vec()); assert!(ui_changes() > before, "a held key must redraw the app"); @@ -3928,7 +4152,7 @@ async fn keys_held_back_redraw_the_predicted_echo() { alternate_screen: false, }) .await; - settle().await; + wait_for(|| ui_changes() > before).await; assert!(core.ui_view().await.terminals.attached.unwrap().waiting_input.is_empty()); assert!(ui_changes() > before, "the send must clear the prediction"); }) @@ -3952,7 +4176,11 @@ async fn terminal_output_reaches_the_host_on_its_own_event() { core.dispatch(Intent::AttachTerminal { machine: machine.pubkey_hex.clone(), term: "t1".into() }) .await; - settle().await; + wait_for_async(|| { + let core = core.clone(); + async move { core.ui_view().await.terminals.attached.is_some() } + }) + .await; // "hi" in base64, the shell's first output at offset 0. push_bridge_to_phone_event_at( @@ -3969,7 +4197,14 @@ async fn terminal_output_reaches_the_host_on_its_own_event() { LIVE_KIND, None, ); - settle().await; + wait_for(|| { + spy.events.lock().unwrap().iter().any(|e| matches!( + e, + CoreEvent::TerminalOutput { term, offset: 0, bytes, .. } + if term == "t1" && bytes == b"hi" + )) + }) + .await; { let events = spy.events.lock().unwrap(); diff --git a/crates/client-runtime/src/signer.rs b/crates/client-runtime/src/signer.rs index 31b855c1..1f590f88 100644 --- a/crates/client-runtime/src/signer.rs +++ b/crates/client-runtime/src/signer.rs @@ -326,15 +326,4 @@ mod tests { let unnamed = PhoneKeys::new("ab", &session, None); assert_eq!(unnamed.grant_for("cd", None).label, None); } - - #[test] - fn signed_event_json_round_trips() { - let k = generate_keypair(); - let mut ev = nostr::EventBuilder::text_note("hi").build(nostr::Keys::new(k.secret_key.clone()).public_key()); - ev.ensure_id(); - assert!(unsigned_event_json(&ev).contains("\"id\"")); - let signed = ev.sign_with_keys(&nostr::Keys::new(k.secret_key.clone())).unwrap(); - assert_eq!(signed_event_from_json(&signed.as_json()).unwrap(), signed); - assert!(signed_event_from_json("{}").is_err()); - } } diff --git a/crates/client-runtime/src/test_support.rs b/crates/client-runtime/src/test_support.rs new file mode 100644 index 00000000..cd9f6dce --- /dev/null +++ b/crates/client-runtime/src/test_support.rs @@ -0,0 +1,24 @@ +//! Shared fixtures for this crate's unit tests: a hydrated store and the +//! phone-side keys, identical wherever a module needs them. + +use crate::ports::{MemoryKv, MemoryTranscriptStore}; +use crate::signer::PhoneKeys; +use crate::stores::{hydrate, CoreStores, StoresConfig}; + +pub(crate) async fn stores_with_transcript() -> (CoreStores, MemoryTranscriptStore) { + let kv = MemoryKv::new(); + let ts = MemoryTranscriptStore::new(); + let h = hydrate(&kv, &ts, &StoresConfig::default()).await; + (h.stores, ts) +} + +pub(crate) async fn stores() -> CoreStores { + stores_with_transcript().await.0 +} + +/// Keys for a fresh random identity, with a current session key. +pub(crate) fn phone_keys() -> PhoneKeys { + let identity = protocol::crypto::generate_keypair(); + let (ring, _) = client_core::stores::session_key::SessionKeyRing::load(None, 1_000); + PhoneKeys::new(&identity.pubkey_hex, &ring.current, None) +} diff --git a/crates/client-runtime/src/view.rs b/crates/client-runtime/src/view.rs index b5542624..4123412b 100644 --- a/crates/client-runtime/src/view.rs +++ b/crates/client-runtime/src/view.rs @@ -493,14 +493,7 @@ impl TranscriptRowsView { #[cfg(test)] mod tests { use super::*; - use crate::ports::{MemoryKv, MemoryTranscriptStore}; - use crate::stores::{hydrate, StoresConfig}; - - async fn stores() -> CoreStores { - let kv = MemoryKv::new(); - let ts = MemoryTranscriptStore::new(); - hydrate(&kv, &ts, &StoresConfig::default()).await.stores - } + use crate::test_support::stores; #[test] fn connection_view_maps_the_status_string() { @@ -513,21 +506,6 @@ mod tests { assert_eq!(json, r#"{"status":"connected","needsPairingCheck":false,"connectedRelays":[]}"#); } - #[tokio::test] - async fn settings_and_machines_views_are_thin_over_the_store() { - let mut s = stores().await; - s.machines.register_machine("m", "laptop", None, None, &["wss://extra.example".to_string()]); - s.settings.set_ui_scale(1.2); - - let mv = MachinesView::from_stores(&s); - assert_eq!(mv.machines["m"].relays, vec!["wss://extra.example"]); - let sv = SettingsView::from_stores(&s); - assert_eq!(sv.data.ui_scale, 1.2); - assert_eq!(sv.backup.relay, None); - // The settings' own fields stay at the top level. - assert!(serde_json::to_string(&sv).unwrap().contains("\"uiScale\":1.2")); - } - #[tokio::test] async fn pairing_view_projects_phase_and_candidate() { use client_core::stores::pairing::{PairingCandidate, PairingPhase, PairingState}; @@ -550,38 +528,6 @@ mod tests { assert_eq!(pv.candidate.unwrap().machine, "(manual)"); } - #[tokio::test] - async fn quick_prompts_view_is_thin_over_the_store() { - let mut s = stores().await; - s.quick_prompts.add_prompt("qp-1", "Go", "continue"); - let qpv = QuickPromptsView::from_stores(&s); - assert_eq!(qpv.prompts.len(), 1); - assert_eq!(qpv.prompts[0].label, "Go"); - let json = serde_json::to_string(&qpv).unwrap(); - assert!(json.contains(r#""prompts":[{"id":"qp-1","label":"Go","text":"continue"}]"#)); - } - - #[tokio::test] - async fn ui_view_is_thin_over_the_store() { - let mut s = stores().await; - s.ui.select_machine(Some("m1")); - s.ui.mark_session_unread("m1", "s1"); - s.ui.set_plan_approval_choice("card1", "2"); - s.ui.note_web_sign_in_sent("m1", "K7QM-4XPA"); - - let uv = UiView::from_stores(&s); - assert_eq!(uv.selected_machine.as_deref(), Some("m1")); - assert!(uv.unread_sessions.contains(&client_core::notifications::session_key_of("m1", "s1"))); - assert_eq!(uv.plan_approval_choices.get("card1").map(String::as_str), Some("2")); - assert_eq!(uv.web_sign_in["m1"].code, "K7QM-4XPA"); - assert_eq!( - uv.web_sign_in["m1"].state, - client_core::stores::ui::WebSignInState::Sending - ); - let json = serde_json::to_string(&uv).unwrap(); - assert!(json.contains(r#""selectedMachine":"m1""#)); - } - #[tokio::test] async fn outbox_view_is_ordered_oldest_first() { use client_core::stores::outbox::OutboxState; From 1d949a4b830def68cb8b4634ddcf3589abe31c2b Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:02:48 +0100 Subject: [PATCH 18/42] test(bridge): remove duplicated fixtures and a git-absence check One session-default test now covers both the absent and the unknown mode/effort fallbacks. The persona gate is proven once, through set-option. The web-browser effect passthrough tests went, keeping the unpaired-identity gate. The git_head test asserted None in a temp dir, which is equally what a missing or failing git produces, so it did not exercise the code path it named. The set-option rig helper is shared by the card and persona suites. Co-Authored-By: Claude Code --- crates/bridge-core/tests/cards.rs | 17 +++++------- crates/bridge-core/tests/personas.rs | 18 ------------- crates/bridge-core/tests/sessions.rs | 23 +++++++---------- crates/bridge-core/tests/support/mod.rs | 9 +++++++ crates/bridge-core/tests/web_browsers.rs | 33 ------------------------ crates/bridge-runtime/src/work.rs | 6 ----- 6 files changed, 25 insertions(+), 81 deletions(-) diff --git a/crates/bridge-core/tests/cards.rs b/crates/bridge-core/tests/cards.rs index 12f30c76..bd5af4b4 100644 --- a/crates/bridge-core/tests/cards.rs +++ b/crates/bridge-core/tests/cards.rs @@ -271,11 +271,6 @@ fn a_card_for_a_session_that_is_not_running_is_cancelled_at_once() { assert!(matches!(reply_to(&mut rig, &h), BridgeMessage::PermissionOutcome(SelectOutcome::Cancelled { .. }))); } -fn set_option(rig: &mut Rig, s: &str, option: &str, value: &str) -> Option { - rig.send(json!({"v":12,"type":"set-option","sessionId":s,"option":option,"value":value})); - rig.host_frames().into_iter().find(|f| matches!(f.message, BridgeMessage::SetOption { .. })).and_then(|f| f.id) -} - fn confirmed(rig: &mut Rig) -> Vec { rig.messages() .into_iter() @@ -290,27 +285,27 @@ fn confirmed(rig: &mut Rig) -> Vec { fn set_option_is_checked_against_the_catalog_then_confirmed() { let mut rig = Rig::new(); let s = ready(&mut rig); - assert_eq!(set_option(&mut rig, &s, "mode", "warp"), None, "not an alpha mode"); - let id = set_option(&mut rig, &s, "mode", "plan").expect("sent"); + assert!(set_option(&mut rig, &s, "mode", "warp").is_none(), "not an alpha mode"); + let (id, _) = set_option(&mut rig, &s, "mode", "plan").expect("sent"); rig.host_reply(&id, HostMessage::Ack); let msgs = rig.messages(); assert!(msgs.iter().any(|m| matches!(m, BridgeToPhone::OptionConfirmed(o) if o.value == "plan"))); assert_eq!(last_heartbeat(&msgs).sessions[0].mode.as_deref(), Some("plan")); - let id = set_option(&mut rig, &s, "effort", "high").unwrap(); + let (id, _) = set_option(&mut rig, &s, "effort", "high").unwrap(); rig.host_reply(&id, HostMessage::Error { message: "no".into() }); assert_eq!(confirmed(&mut rig), ["high"], "the agent's default effort stays in force"); - let id = set_option(&mut rig, &s, "effort", "low").unwrap(); + let (id, _) = set_option(&mut rig, &s, "effort", "low").unwrap(); rig.host_reply(&id, HostMessage::Ack); assert_eq!(confirmed(&mut rig), ["low"]); - let id = set_option(&mut rig, &s, "effort", "high").unwrap(); + let (id, _) = set_option(&mut rig, &s, "effort", "high").unwrap(); rig.host_reply(&id, HostMessage::Error { message: "no".into() }); assert_eq!(confirmed(&mut rig), ["low"], "a refused change confirms the effort in force"); // Beta lists no levels of its own: its levels are per model, so the // agent judges one — and its refusal confirms nothing new. let b = rig.ready_session("beta"); - let id = set_option(&mut rig, &b, "effort", "max").expect("left to the agent"); + let (id, _) = set_option(&mut rig, &b, "effort", "max").expect("left to the agent"); rig.host_reply(&id, HostMessage::Error { message: "not a level of this model".into() }); assert!(!confirmed(&mut rig).contains(&"max".to_string())); } diff --git a/crates/bridge-core/tests/personas.rs b/crates/bridge-core/tests/personas.rs index e77c969b..fe7aa12e 100644 --- a/crates/bridge-core/tests/personas.rs +++ b/crates/bridge-core/tests/personas.rs @@ -44,15 +44,6 @@ fn info_persona(persona: &str) -> SessionEvent { } } -/// Send a `set-option` and return the host frame it produced, if any. -fn set_option(rig: &mut Rig, s: &str, option: &str, value: &str) -> Option<(String, BridgeMessage)> { - rig.send(json!({"v":12,"type":"set-option","sessionId":s,"option":option,"value":value})); - rig.host_frames() - .into_iter() - .find(|f| matches!(f.message, BridgeMessage::SetOption { .. })) - .map(|f| (f.id.unwrap_or_default(), f.message)) -} - #[test] fn personas_are_asked_of_the_agent_or_the_phone_is_told_why_not() { let mut rig = Rig::new(); @@ -167,15 +158,6 @@ fn a_persona_the_agent_does_not_have_leaves_the_session_on_the_default_it_report assert_eq!(params.persona.as_deref(), Some("build")); } -#[test] -fn a_persona_on_an_agent_without_them_is_ignored() { - let mut rig = Rig::new(); - rig.host_up(); - rig.send(json!({"v":12,"type":"create-session","agent":"alpha","persona":"review"})); - let (_, params) = rig.start_request(); - assert_eq!(params.persona, None); -} - #[test] fn set_option_switches_the_persona_only_for_an_agent_that_has_them() { let mut rig = Rig::new(); diff --git a/crates/bridge-core/tests/sessions.rs b/crates/bridge-core/tests/sessions.rs index 93825f25..827e125c 100644 --- a/crates/bridge-core/tests/sessions.rs +++ b/crates/bridge-core/tests/sessions.rs @@ -165,21 +165,18 @@ fn an_agent_that_stops_before_ready_fails_the_session() { } #[test] -fn an_unknown_mode_or_effort_falls_back_to_the_agent_default() { +fn a_session_without_a_known_mode_or_effort_records_the_agent_defaults() { let mut rig = Rig::new(); rig.host_up(); - rig.send(json!({"v":12,"type":"create-session","agent":"alpha","mode":"warp","effort":"max"})); - let (_, p) = rig.start_request(); - assert_eq!((p.mode.as_deref(), p.effort.as_deref()), (Some("ask"), Some("high"))); -} - -#[test] -fn a_session_without_mode_or_effort_records_the_agent_defaults() { - let mut rig = Rig::new(); - rig.host_up(); - rig.send(json!({"v":12,"type":"create-session","agent":"alpha"})); - let (_, p) = rig.start_request(); - assert_eq!((p.mode.as_deref(), p.effort.as_deref()), (Some("ask"), Some("high"))); + // Neither given, and values the catalog does not know: both end at the + // agent's own defaults rather than being forwarded. + for extra in [json!({}), json!({"mode": "warp", "effort": "max"})] { + let mut msg = json!({"v":12,"type":"create-session","agent":"alpha"}); + msg.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone()); + rig.send(msg); + let (_, p) = rig.start_request(); + assert_eq!((p.mode.as_deref(), p.effort.as_deref()), (Some("ask"), Some("high"))); + } // An agent with no default effort leaves it unset. rig.send(json!({"v":12,"type":"create-session","agent":"beta"})); let (_, p) = rig.start_request(); diff --git a/crates/bridge-core/tests/support/mod.rs b/crates/bridge-core/tests/support/mod.rs index 3b4839bd..f317bf2f 100644 --- a/crates/bridge-core/tests/support/mod.rs +++ b/crates/bridge-core/tests/support/mod.rs @@ -413,3 +413,12 @@ pub fn heartbeats(msgs: &[BridgeToPhone]) -> Vec protocol::events::SessionListMsg { heartbeats(msgs).pop().expect("a heartbeat was published") } + +/// Send a `set-option` and return the host frame it produced, if any. +pub fn set_option(rig: &mut Rig, s: &str, option: &str, value: &str) -> Option<(String, BridgeMessage)> { + rig.send(json!({"v":12,"type":"set-option","sessionId":s,"option":option,"value":value})); + rig.host_frames() + .into_iter() + .find(|f| matches!(f.message, BridgeMessage::SetOption { .. })) + .map(|f| (f.id.unwrap_or_default(), f.message)) +} diff --git a/crates/bridge-core/tests/web_browsers.rs b/crates/bridge-core/tests/web_browsers.rs index 9fe94a0c..9ec452f7 100644 --- a/crates/bridge-core/tests/web_browsers.rs +++ b/crates/bridge-core/tests/web_browsers.rs @@ -10,39 +10,6 @@ use protocol::crypto::generate_keypair; use serde_json::json; use support::*; -/// The client a `web-browsers-request` asked the runtime to answer, if any. -fn requested(rig: &mut Rig) -> Option> { - rig.send(json!({"v":12,"type":"web-browsers-request"})); - rig.take().into_iter().find_map(|e| match e { - Effect::WebBrowsers { client } => Some(vec![client.phone]), - _ => None, - }) -} - -#[test] -fn a_request_asks_the_runtime_for_the_list_for_the_asking_client() { - let mut rig = Rig::new(); - let phone = rig.phone.pubkey_hex.clone(); - assert_eq!(requested(&mut rig), Some(vec![phone])); -} - -#[test] -fn a_sign_out_names_the_browser_for_the_runtime_to_end() { - let mut rig = Rig::new(); - let phone = rig.phone.pubkey_hex.clone(); - rig.send(json!({"v":12,"type":"web-browser-sign-out","id":"0123456789abcdef"})); - let (id, to) = rig - .take() - .into_iter() - .find_map(|e| match e { - Effect::WebBrowserSignOut { id, client } => Some((id, vec![client.phone])), - _ => None, - }) - .expect("a sign-out effect"); - assert_eq!(id, "0123456789abcdef"); - assert_eq!(to, vec![phone]); -} - #[test] fn an_unpaired_identity_gets_no_list_and_ends_no_browser() { let mut rig = Rig::new(); diff --git a/crates/bridge-runtime/src/work.rs b/crates/bridge-runtime/src/work.rs index 8bf3328b..9decfee5 100644 --- a/crates/bridge-runtime/src/work.rs +++ b/crates/bridge-runtime/src/work.rs @@ -128,10 +128,4 @@ mod tests { assert!(!is_onion_http("http://.onion/x")); assert!(!is_onion_http("http://abcdef.onion.example.com/x")); } - - #[tokio::test] - async fn git_head_is_none_outside_a_repository() { - let dir = tempfile::tempdir().unwrap(); - assert_eq!(git_head(dir.path()).await, None); - } } From 9360e504bdef940281987714718b0de519913412 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:02:59 +0100 Subject: [PATCH 19/42] test(android): drop a wall-clock bound, one duplicate golden and extra loaders The megabyte markdown test keeps its block-count check and loses the under-a-second assertion, which measured the machine rather than the split. The tallest welcome state was snapshotted in both the standard and the tall-device class; keep the tall-device golden and its image. The three transcript corpus loaders become one in DesignFixtures. Co-Authored-By: Claude Code --- .../com/codedeck/plus/ui/DesignFixtures.kt | 18 ++++---- .../ui/screens/WelcomeScreenSnapshotTest.kt | 18 -------- .../transcript/DisplayEntriesFixtureTest.kt | 8 +--- .../plus/ui/transcript/MarkdownBlocksTest.kt | 5 +-- .../ui/transcript/TranscriptRowsParityTest.kt | 39 ++++++------------ ..._the_tallest_state_scrolls_as_one_page.png | Bin 91817 -> 0 bytes 6 files changed, 24 insertions(+), 64 deletions(-) delete mode 100644 apps/android/app/src/test/snapshots/images/com.codedeck.plus.ui.screens_WelcomeScreenSnapshotTest_the_tallest_state_scrolls_as_one_page.png diff --git a/apps/android/app/src/test/java/com/codedeck/plus/ui/DesignFixtures.kt b/apps/android/app/src/test/java/com/codedeck/plus/ui/DesignFixtures.kt index 13ad8f91..55164f36 100644 --- a/apps/android/app/src/test/java/com/codedeck/plus/ui/DesignFixtures.kt +++ b/apps/android/app/src/test/java/com/codedeck/plus/ui/DesignFixtures.kt @@ -410,18 +410,18 @@ internal object DesignFixtures { UniffiQuickPrompt("q2", "Run the tests", "Run the tests and fix what fails."), ) - /** The shared transcript corpus (see `DisplayEntriesFixtureTest`): every entry kind. */ - fun transcript(): List { - val raw = javaClass.classLoader!!.getResourceAsStream("display_entries_corpus.json")!!.bufferedReader().readText() - return parseDisplayEntries(displayEntriesJson.parseToJsonElement(raw).jsonObject.getValue("displayEntries").toString()) - } + /** The raw shared corpus (see `DisplayEntriesFixtureTest`). */ + fun corpusText(): String = + javaClass.classLoader!!.getResourceAsStream("display_entries_corpus.json")!!.bufferedReader().readText() + + /** The shared transcript corpus: every entry kind. */ + fun transcript(): List = + parseDisplayEntries(displayEntriesJson.parseToJsonElement(corpusText()).jsonObject.getValue("displayEntries").toString()) /** The corpus's activity: a plan under way, a sub-agent at work and a * background command running beside one that failed. */ - fun activity(): ActivityView { - val raw = javaClass.classLoader!!.getResourceAsStream("display_entries_corpus.json")!!.bufferedReader().readText() - return parseActivity(displayEntriesJson.parseToJsonElement(raw).jsonObject.getValue("activity").toString()) - } + fun activity(): ActivityView = + parseActivity(displayEntriesJson.parseToJsonElement(corpusText()).jsonObject.getValue("activity").toString()) /** A plan document and the cards that wait on the user, unanswered. */ val waitingCards: List = listOf( diff --git a/apps/android/app/src/test/java/com/codedeck/plus/ui/screens/WelcomeScreenSnapshotTest.kt b/apps/android/app/src/test/java/com/codedeck/plus/ui/screens/WelcomeScreenSnapshotTest.kt index 22e20aa8..54c4ae93 100644 --- a/apps/android/app/src/test/java/com/codedeck/plus/ui/screens/WelcomeScreenSnapshotTest.kt +++ b/apps/android/app/src/test/java/com/codedeck/plus/ui/screens/WelcomeScreenSnapshotTest.kt @@ -76,22 +76,4 @@ class WelcomeScreenSnapshotTest { } } - /** The tallest the screen gets: two signers, the import open, an error. - * The whole screen scrolls as one page, from its top. */ - @Test - fun the_tallest_state_scrolls_as_one_page() { - paparazzi.snapshot { - CodeDeckTheme { - WelcomeScreen( - signers = listOf(amber, SignerAppInfo("com.example.signer", "Other signer")), - busy = null, - error = "That is not an nsec or a hex secret key.", - onUseSigner = {}, - onCreateKey = {}, - onImportKey = {}, - importInitiallyOpen = true, - ) - } - } - } } diff --git a/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/DisplayEntriesFixtureTest.kt b/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/DisplayEntriesFixtureTest.kt index b7c0026e..817a326a 100644 --- a/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/DisplayEntriesFixtureTest.kt +++ b/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/DisplayEntriesFixtureTest.kt @@ -1,5 +1,6 @@ package com.codedeck.plus.ui.transcript +import com.codedeck.plus.ui.DesignFixtures import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue @@ -21,14 +22,9 @@ import org.junit.Test */ class DisplayEntriesFixtureTest { - private fun loadFixture(): String = - javaClass.classLoader!!.getResourceAsStream("display_entries_corpus.json")!! - .bufferedReader() - .readText() - @Test fun decodes_the_shared_corpus_fixture_into_every_display_kind() { - val root = displayEntriesJson.parseToJsonElement(loadFixture()).jsonObject + val root = displayEntriesJson.parseToJsonElement(DesignFixtures.corpusText()).jsonObject val entries = parseDisplayEntries(root.getValue("displayEntries").toString()) val pending = parsePendingPermission(root.getValue("pendingPermission").toString()) diff --git a/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/MarkdownBlocksTest.kt b/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/MarkdownBlocksTest.kt index ca9762b1..86973b66 100644 --- a/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/MarkdownBlocksTest.kt +++ b/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/MarkdownBlocksTest.kt @@ -58,12 +58,9 @@ class MarkdownBlocksTest { assertEquals(text.split(' ').size, blocks.joinToString(" ").split(Regex("\\s+")).size) } - @Test fun aMegabyteSplitsQuickly() { + @Test fun aMegabyteSplitsIntoBlocks() { val text = paragraphs(4_000) - val start = System.nanoTime() val blocks = markdownBlocks(text) - val ms = (System.nanoTime() - start) / 1_000_000 assertTrue("${blocks.size} blocks", blocks.size > 200) - assertTrue("took $ms ms", ms < 1_000) } } diff --git a/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/TranscriptRowsParityTest.kt b/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/TranscriptRowsParityTest.kt index 34edb138..61901505 100644 --- a/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/TranscriptRowsParityTest.kt +++ b/apps/android/app/src/test/java/com/codedeck/plus/ui/transcript/TranscriptRowsParityTest.kt @@ -23,7 +23,6 @@ import com.codedeck.plus.ui.transcript.rows.PermissionCard import com.codedeck.plus.ui.transcript.rows.PlanApprovalCard import com.codedeck.plus.ui.transcript.rows.QuestionCard import com.codedeck.plus.ui.transcript.rows.ToolGroupRow -import kotlinx.serialization.json.jsonObject import org.junit.Rule import org.junit.Test import androidx.compose.runtime.CompositionLocalProvider @@ -60,13 +59,6 @@ class TranscriptRowsParityTest { } } - private fun corpus(): List { - val root = displayEntriesJson.parseToJsonElement( - javaClass.classLoader!!.getResourceAsStream("display_entries_corpus.json")!!.bufferedReader().readText(), - ).jsonObject - return parseDisplayEntries(root.getValue("displayEntries").toString()) - } - /** Lone calls and the working line as each agent's look draws them: the * result line after its mark, and Claude Code's counter. */ @Test @@ -102,7 +94,7 @@ class TranscriptRowsParityTest { @Test fun tool_group_rows() { - val groups = corpus().filterIsInstance() + val groups = DesignFixtures.transcript().filterIsInstance() val work = editGroup() // A lone running command, as the transcript shows it mid-turn. val lone = work.copy( @@ -120,7 +112,7 @@ class TranscriptRowsParityTest { ActivityRow(null, turn = 0, onOpen = null) // A sub-agent at work, and background tasks as rows. ActivityRow(agentGroup().steps[0] as ToolStep.Call, turn = 0, onOpen = {}) - corpus().filterIsInstance().forEach { TaskRow(it) } + DesignFixtures.transcript().filterIsInstance().forEach { TaskRow(it) } TaskRow(DisplayEntry.Task(0, "bg-2", "shell", "npm run dev", "stopped")) } } @@ -138,7 +130,7 @@ class TranscriptRowsParityTest { @Test fun tool_sheet_timeline() { - val work = corpus().filterIsInstance()[1] + val work = DesignFixtures.transcript().filterIsInstance()[1] paparazzi.snapshot { Sheet { ToolSheetContent(work, live = false, openPath = emptyList(), onOpenPath = {}, onClose = {}) } } } @@ -156,12 +148,12 @@ class TranscriptRowsParityTest { /** The group with an edit and a failed command. */ private fun editGroup(): DisplayEntry.ToolGroup = - corpus().filterIsInstance().first { it.added > 0 } + DesignFixtures.transcript().filterIsInstance().first { it.added > 0 } /** The group holding the corpus's sub-agent, checklist and background * command. */ private fun agentGroup(): DisplayEntry.ToolGroup = - corpus().filterIsInstance().first { g -> g.steps.any { (it as? ToolStep.Call)?.toolKind == "agent" } } + DesignFixtures.transcript().filterIsInstance().first { g -> g.steps.any { (it as? ToolStep.Call)?.toolKind == "agent" } } @Test fun tool_sheet_agent_page() { @@ -192,23 +184,16 @@ class TranscriptRowsParityTest { paparazzi.snapshot { Sheet { ToolSheetContent(group, live = false, openPath = listOf(group.steps[1].seq), onOpenPath = {}, onClose = {}) } } } - private fun activity(): ActivityView { - val root = displayEntriesJson.parseToJsonElement( - javaClass.classLoader!!.getResourceAsStream("display_entries_corpus.json")!!.bufferedReader().readText(), - ).jsonObject - return parseActivity(root.getValue("activity").toString()) - } - @Test fun activity_sheet() { paparazzi.snapshot { - Sheet { ActivitySheetContent(activity(), live = false, canStop = true, onOpenAgent = {}, onStopTask = {}, onClose = {}) } + Sheet { ActivitySheetContent(DesignFixtures.activity(), live = false, canStop = true, onOpenAgent = {}, onStopTask = {}, onClose = {}) } } } @Test fun activity_bar() { - val a = activity() + val a = DesignFixtures.activity() paparazzi.snapshot { Dark { Column { @@ -222,13 +207,13 @@ class TranscriptRowsParityTest { @Test fun diff_card() { - val diff = corpus().filterIsInstance().first() + val diff = DesignFixtures.transcript().filterIsInstance().first() paparazzi.snapshot { Dark { DiffRow(diff.path, diff.lines, diff.truncated, expanded = false, onToggle = {}) } } } @Test fun permission_card_pending() { - val permission = corpus().filterIsInstance().last() + val permission = DesignFixtures.transcript().filterIsInstance().last() paparazzi.snapshot { Dark { PermissionCard(permission, "machine", "session", responded = false, actions = {}) } } @@ -236,7 +221,7 @@ class TranscriptRowsParityTest { @Test fun plan_approval_card_pending() { - val plan = corpus().filterIsInstance().first() + val plan = DesignFixtures.transcript().filterIsInstance().first() paparazzi.snapshot { Dark { PlanApprovalCard(plan, "machine", "session", responded = false, choice = null, actions = {}) } } @@ -244,7 +229,7 @@ class TranscriptRowsParityTest { @Test fun question_card_pending() { - val question = corpus().filterIsInstance().first() + val question = DesignFixtures.transcript().filterIsInstance().first() paparazzi.snapshot { Dark { QuestionCard(question, "machine", "session", respondedCards = emptySet(), actions = {}) } } @@ -252,7 +237,7 @@ class TranscriptRowsParityTest { @Test fun question_group_card_pending() { - val group = corpus().filterIsInstance().first { it.questions.size > 1 } + val group = DesignFixtures.transcript().filterIsInstance().first { it.questions.size > 1 } paparazzi.snapshot { Dark { QuestionCard(group, "machine", "session", respondedCards = emptySet(), actions = {}) } } diff --git a/apps/android/app/src/test/snapshots/images/com.codedeck.plus.ui.screens_WelcomeScreenSnapshotTest_the_tallest_state_scrolls_as_one_page.png b/apps/android/app/src/test/snapshots/images/com.codedeck.plus.ui.screens_WelcomeScreenSnapshotTest_the_tallest_state_scrolls_as_one_page.png deleted file mode 100644 index 14696ea3bbe1ecb674aef2e0884341f976887d7f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 91817 zcmdSARZv`A7$u6+Nr2EeK?02imq73UY24l2g1fr}8g~y8+%>p60RjXF?h@SH=kU)= z)t##QaOd&zvZ=1qr?-6jleN|tDlaRB{`%c(I5;?T32~?*92^1|4i0e#gaUk`?^Nar z2XAU80Tohqn?J}vc2icn>u=D7(e%RJ2q925wqOUey!>gMDr#n{w|!fepI6*FI-J&N zXl{94dVD(1yg-Zp)n=3`E$nvf?(=qOB!l-l)AQ(d`8?zJestyOs-fc*c@U;B47Caj zdie(|^fH8jgn>aGiup1?0{$NZ4j6a{^aDorGKe6(e1#VB-#7o)@c+vf{GTiNe_6u+ zu@yMr<;{Y0o4A`BH}i?S(4V#|x+v)H7_ez%fFEwW)-6(_53f#pz8#3nW=opF?6@7^o^(DMKUcd=XZkzM$e@1YdumSvVJ*&N z;h3^?I_DQw=RK#sgb$NA@icNO5k#Mag3$=6+dM8~Ij!d-$H!*J=1AMem>Tn>4{{j=yP#G>Nbz3(NikCyU{24p9Gd_fhtze>9b zJ2)VouN!^}cZo|hkxKe$Z7HQ?vQs%Bs@c#kS*lv@_@~CCRQu=GKNFJY|FXZ`@%oe+ z4U8sP9x)sBN7TY1cG=7)a0prSW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9=*hKuUE?q@9C}N_-ralm~=>Qw*~0(vw+gY z%}DM_A3jq}9_z&zd|WK^*|RP{|50@(agH5FMZ9#u=dC2;P=TpG<~sT;xLF;Mkdd*< zz!Datt+XPEupu2vO7|pONzR^-Ky}@{t$M^QGu%HR(}Ol)5{rn+q+2sw9K}&o`Znh? z_p`4$JBbOu`sFL47;;EUZcHLCEeXdM$Fx1I|CUtGsZnOjU$ahj7umd5srkGojN#=K z@~gC;l^MI!DO968Rn@qd*Jsi?0nv%|X)p=Dj{@{Nx$Ofj@D%V5|Yb zCyyrfR z10)U@mvf!gbgqv*O1NCO^A*!A;_@;pP3nh+fB6oc%$E#IVKa@5^^ah3R4SOus;8AxG~&Z^a)!+?^D;tqP1!&)(N^V25W^FR4L2A4 z#;j$Z5O#(MGkL1WWag!EpN*@6b{`wMJ%lP1X#xiZ}=7W{Vp8G+Y5(C3_Wq*Kha}Yt_Q*U;W zFrG<=-w#$JtSxqYVL4%pP;Pg(iHeFsghE887glKanVCEZT4R-ppE+zA?h$jKqa1N- zB$^+2WoJ*7m85Giz`xCRe#LIf7qmj>b5^Vz5JX%?Jf~Ii3<|8J6(QRXdCsn2X&;m$ z zbjym1dndF#RN6{7<&o8T(3VjZ6@>_e2!)7FEUchdNwSw7D+ED({!<-QR1y_d?ZD{L zrA1*x7M_KOf(E&=xrrmBig!ePlclyTJJJUmT{Qq7d<5 a<^KWFly}^bD>;k+0000 Date: Tue, 27 Oct 2026 01:16:30 +0100 Subject: [PATCH 20/42] fix(opencode): a failed session lookup is not a lost resume Resuming a conversation asked the server for the session and treated every failure as "the server no longer has it". A transport error, such as an unreachable server, therefore fell through to creating a fresh conversation, discarding the transcript for a failure that says nothing about the session. Only a not-found answer now starts a fresh conversation (with the resume-lost notice). Any other failure ends the session with the lookup error, so the bridge can retry once the server is reachable again. Co-Authored-By: Claude Code --- packages/agent-host/src/drivers/opencode/driver.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/agent-host/src/drivers/opencode/driver.ts b/packages/agent-host/src/drivers/opencode/driver.ts index ec69371b..603093da 100644 --- a/packages/agent-host/src/drivers/opencode/driver.ts +++ b/packages/agent-host/src/drivers/opencode/driver.ts @@ -684,6 +684,14 @@ export class OpenCodeSession implements DriverSession { try { return { session: await client.session.get({ sessionID: params.resume }), resumeLost: false }; } catch (err) { + // Only "the server does not have this session" is a lost + // conversation. A transport failure says nothing about the session: + // falling through to a fresh one would discard the transcript for a + // server that is merely unreachable, and that server could not + // create the new session either. + if (!isNotFound(err)) { + throw new Error(`OpenCode could not look up session ${params.resume}: ${formatOpenCodeError(err)}`); + } this.ctx.log(`[opencode] resume ${params.resume} not found server-side (${formatOpenCodeError(err)}) — starting a fresh session instead`); } } From c0aa00fb0c5bc8c6f475da1a2c87c770baa02a53 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:16:36 +0100 Subject: [PATCH 21/42] test(opencode): guard a lost resume and a failed resume lookup A conversation the server no longer has must start a fresh one and report the session_restart notice with the new native session id, so the phone does not keep resuming a dead id. A lookup that fails for any other reason must end the session with that error instead: the transcript is not lost, and a fresh conversation on an unreachable server would fail anyway. The not-found answer is the one a real 2.1.4 server gives (SessionNotFoundError for an unknown id); an unreachable server gives a transport ClientError. Co-Authored-By: Claude Code --- .../opencode/__tests__/opencodeDriver.test.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts index 012eed34..2e3cf021 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts @@ -152,6 +152,54 @@ describe('OpenCode session lifecycle', () => { }); }); +describe('OpenCode resuming a conversation', () => { + /** The session_restart notices the session reported. */ + const notices = (ctx: ReturnType) => + ctx.entries().filter((e) => e.entryType === 'notice') as Array<{ kind: string; text: string }>; + + it('starts a fresh conversation and says so when the server no longer has the session', async () => { + // Verified against 2.1.4: `session.get` for an id the server does not + // know throws `SessionNotFoundError` ("Session not found: "). + const server = fakeOpenCode({ holdStream: false, sessionId: 'ses_new' }); + const ctx = start(server.client, { resume: 'ses_gone' }); + await ctx.ended(); + + expect(notices(ctx)).toHaveLength(1); + expect(notices(ctx)[0]).toMatchObject({ kind: 'session_restart' }); + expect(notices(ctx)[0]!.text).toMatch(/does not remember earlier turns/); + // The phone must learn the id actually running: the one it asked to + // resume no longer exists, and a silent swap would make it resume the + // dead id again. + expect(ctx.events).toContainEqual(expect.objectContaining({ type: 'info', nativeSessionId: 'ses_new' })); + expect(server.client.session.create).toHaveBeenCalled(); + expect(ctx.logs.join('\n')).toMatch(/resume ses_gone not found server-side/); + }); + + it('ends with the error, not a lost-resume, when looking the session up fails', async () => { + // Verified: an unreachable server is a `ClientError` ("Transport: fetch + // failed"), not a not-found. Starting a fresh conversation on it would + // throw the transcript away for a failure that says nothing about the + // session, and the new session could not be created anyway. + const server = fakeOpenCode({ + holdStream: true, + over: { + session: { + get: vi.fn(async () => { + throw Object.assign(new Error('Transport: fetch failed'), { name: 'ClientError' }); + }), + }, + }, + }); + const ctx = start(server.client, { resume: 'ses_1' }); + expect(await ctx.ended()).toMatchObject({ + type: 'ended', + error: expect.stringMatching(/could not look up session ses_1.*Transport: fetch failed/), + }); + expect(notices(ctx)).toEqual([]); + expect(server.client.session.create).not.toHaveBeenCalled(); + }); +}); + describe('OpenCode turn reporting', () => { it('reports the model text, and the turn running then idle', async () => { const ctx = start(clientWith(turn('hello there'))); From 4ee00d696717125aefcd014b42a57741eac4e2e0 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:17:06 +0100 Subject: [PATCH 22/42] test(host): guard the protocol stream and the drop of unparseable lines The bridge reads the host's stdout as one protocol frame per line, so anything else on it is corruption. The entry point's streams, console redirect and host construction move to host/serve.ts, reachable from a test without spawning the process, and the entry keeps the same wiring and the same order (the redirect is in place before any driver loads). The tests prove that a driver's console.log lands on stderr while the protocol frame is the only thing written to stdout, and that a line the host cannot parse is logged through stderr and dropped without stopping the host from answering the request after it. Co-Authored-By: Claude Code --- .../src/host/__tests__/serve.test.ts | 98 +++++++++++++++++++ packages/agent-host/src/host/main.ts | 31 ++---- packages/agent-host/src/host/serve.ts | 55 +++++++++++ 3 files changed, 159 insertions(+), 25 deletions(-) create mode 100644 packages/agent-host/src/host/__tests__/serve.test.ts create mode 100644 packages/agent-host/src/host/serve.ts diff --git a/packages/agent-host/src/host/__tests__/serve.test.ts b/packages/agent-host/src/host/__tests__/serve.test.ts new file mode 100644 index 00000000..458a4925 --- /dev/null +++ b/packages/agent-host/src/host/__tests__/serve.test.ts @@ -0,0 +1,98 @@ +/** + * What main.ts composes: the host's stdio and the host itself. Two + * invariants the bridge depends on — nothing but a protocol frame may reach + * stdout (whatever a driver logs is on stderr), and a line the host cannot + * parse is logged and dropped rather than ending the process. + */ +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import { Writable } from 'node:stream'; +import { afterEach, describe, expect, it } from 'vitest'; +import pkg from '../../../package.json'; +import { DRIVER_PROTOCOL_VERSION } from '../../sdk/types'; +import { startHost, wire } from '../serve'; + +const METHODS = ['log', 'info', 'debug', 'warn'] as const; +/** The console as it was before a test replaced it. */ +const ORIGINAL = METHODS.map((method) => (console as unknown as Record)[method]); +const dirs: string[] = []; + +afterEach(() => { + METHODS.forEach((method, index) => { + (console as unknown as Record)[method] = ORIGINAL[index]; + }); + for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); +}); + +/** A stream that keeps what was written to it. */ +function sink() { + const chunks: string[] = []; + return { + stream: new Writable({ + write(chunk, _encoding, done) { + chunks.push(String(chunk)); + done(); + }, + }), + text: () => chunks.join(''), + }; +} + +/** A scratch agent cache, so loading the fake driver touches nothing real. */ +function cacheDir(): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'host-serve-')); + dirs.push(dir); + return dir; +} + +describe('the host process wiring', () => { + it('sends a driver console.log to stderr, never onto the protocol stream', () => { + const out = sink(); + const err = sink(); + const io = wire({ out: out.stream, err: err.stream }); + + // What an SDK or a driver does when it has something to say. On stdout + // this would be a line the bridge tries to parse as a frame. + console.log('driver chatter', 42); + console.info('more chatter'); + io.write('{"v":1,"kind":"ack"}'); + + expect(out.text()).toBe('{"v":1,"kind":"ack"}\n'); + expect(err.text()).toBe('driver chatter 42\nmore chatter\n'); + }); + + it('drops a line it cannot parse with a log, and keeps serving', async () => { + const out = sink(); + const err = sink(); + const host = await startHost(wire({ out: out.stream, err: err.stream }), { + CODEDECK_AGENT_HOST_DRIVERS: 'fake', + CODEDECK_AGENT_CACHE: cacheDir(), + }); + try { + // Not JSON at all: nothing may be answered with a frame, and the host + // must still answer the request that follows it. + await host.handleLine('{ this is not a frame'); + expect(out.text()).toBe(''); + expect(err.text()).toContain('[agent-host] dropped frame: malformed frame:'); + + await host.handleLine( + JSON.stringify({ v: DRIVER_PROTOCOL_VERSION, id: 'b1', kind: 'initialize', payload: { bridgeVersion: '12.0.0' } }), + ); + const frames = out + .text() + .split('\n') + .filter((line) => line.length > 0) + .map((line) => JSON.parse(line) as Record); + expect(frames).toHaveLength(1); + expect(frames[0]).toMatchObject({ + v: DRIVER_PROTOCOL_VERSION, + id: 'b1', + kind: 'initialized', + payload: { hostVersion: pkg.version, agents: [{ id: 'fake' }] }, + }); + } finally { + await host.shutdown(); + } + }); +}); diff --git a/packages/agent-host/src/host/main.ts b/packages/agent-host/src/host/main.ts index 8ed405f5..f8e7e7c3 100644 --- a/packages/agent-host/src/host/main.ts +++ b/packages/agent-host/src/host/main.ts @@ -27,18 +27,12 @@ * CODEDECK_NPM_REGISTRY an npm mirror to install them from */ import * as readline from 'node:readline'; -import pkg from '../../package.json'; -import { agentCacheDir, registryUrl, withoutAgentBin } from '../install/agentInstall'; -import { Agents } from './agents'; import { runCommand } from './commands'; -import { AgentHost, type HostIo } from './host'; -import { selectModules } from './modules'; +import { driverEnv, startHost, wire } from './serve'; -// stdout carries protocol frames only; a stray console.log from any library -// would corrupt the stream, so every console method writes to stderr. -for (const method of ['log', 'info', 'debug', 'warn'] as const) { - console[method] = (...args: unknown[]) => console.error(...args); -} +// Built before any driver is, so a library that logs during its load cannot +// reach stdout: `wire` sends every console method to stderr. +const io = wire(); const log = (message: string): void => { process.stderr.write(`${message} @@ -48,27 +42,14 @@ const log = (message: string): void => { process.on('uncaughtException', (err) => log(`[agent-host] uncaught exception: ${err instanceof Error ? err.stack : String(err)}`)); process.on('unhandledRejection', (err) => log(`[agent-host] unhandled rejection: ${err instanceof Error ? err.stack : String(err)}`)); -/** What every module is handed, from the host's environment. */ -function driverEnv(env: NodeJS.ProcessEnv) { - const cacheDir = agentCacheDir(env); - return { env, lookupEnv: withoutAgentBin(env, cacheDir), cacheDir, registry: registryUrl(env), log }; -} - async function main(): Promise { const args = process.argv.slice(2); if (args.length > 0) { // One command, then exit without ever reading stdin. - process.exitCode = await runCommand(args, driverEnv(process.env), (line) => process.stdout.write(`${line}\n`)); + process.exitCode = await runCommand(args, driverEnv(process.env, io.log), (line) => process.stdout.write(`${line}\n`)); return; } - const io: HostIo = { - write: (line) => { - process.stdout.write(`${line}\n`); - }, - log, - }; - const agents = await Agents.load(selectModules(process.env, log), driverEnv(process.env)); - const host = new AgentHost(agents, io, pkg.version); + const host = await startHost(io, process.env); const lines = readline.createInterface({ input: process.stdin, crlfDelay: Infinity }); // Lines are handled concurrently: a slow request (a model list, a mode // switch) must not hold up the replies that unblock a waiting agent. diff --git a/packages/agent-host/src/host/serve.ts b/packages/agent-host/src/host/serve.ts new file mode 100644 index 00000000..af97cb88 --- /dev/null +++ b/packages/agent-host/src/host/serve.ts @@ -0,0 +1,55 @@ +/** + * What the host process is made of: its two output streams and the host + * built from the environment the bridge passes down. Kept out of main.ts so + * both can be exercised without spawning the process. + * + * stdout carries protocol frames only, one per line. Every console method a + * driver or an SDK calls writes to stderr instead, which the bridge copies + * into its log — a stray console.log on stdout would corrupt the stream the + * bridge parses. + */ +import { format } from 'node:util'; +import pkg from '../../package.json'; +import { agentCacheDir, registryUrl, withoutAgentBin } from '../install/agentInstall'; +import { Agents, type BaseEnv } from './agents'; +import { AgentHost, type HostIo } from './host'; +import { selectModules } from './modules'; + +/** Where the host writes: protocol frames, and diagnostics. */ +export interface HostStreams { + out: NodeJS.WritableStream; + err: NodeJS.WritableStream; +} + +/** + * The host's output streams, with every console method sent to stderr. A + * driver that logs the way libraries do must not be able to write a line + * the bridge would try to parse as a frame. + */ +export function wire({ out, err }: HostStreams = { out: process.stdout, err: process.stderr }): HostIo { + for (const method of ['log', 'info', 'debug', 'warn'] as const) { + console[method] = (...args: unknown[]) => { + err.write(`${format(...args)}\n`); + }; + } + return { + write: (line) => { + out.write(`${line}\n`); + }, + log: (message) => { + err.write(`${message}\n`); + }, + }; +} + +/** What every module is handed, from the host's environment. */ +export function driverEnv(env: NodeJS.ProcessEnv, log: (message: string) => void): BaseEnv { + const cacheDir = agentCacheDir(env); + return { env, lookupEnv: withoutAgentBin(env, cacheDir), cacheDir, registry: registryUrl(env), log }; +} + +/** Load the drivers the environment selects, and build the host over them. */ +export async function startHost(io: HostIo, env: NodeJS.ProcessEnv = process.env): Promise { + const agents = await Agents.load(selectModules(env, io.log), driverEnv(env, io.log)); + return new AgentHost(agents, io, pkg.version); +} From 49527e474891e46092887b7cebfa7da88a381314 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:16:19 +0100 Subject: [PATCH 23/42] test(protocol): pin both sides of the chunk threshold and group restart A fragment stream sits on two boundaries that were only half covered: the safe plaintext size is one event, and the very next byte must split. The split test now asserts every fragment fits the cap and that the parts still reproduce the message exactly, so an off-by-one in the comparison or in the per-fragment budget fails here instead of at a relay's 65535-byte content limit. The assembler also had no test for a second fragment of a cid arriving with a different `n` (a corrupt sender or a cid collision). It must drop the old group rather than merge both senders' parts, which the new test watches through `open_count` and the exact assembled JSON. Co-Authored-By: Claude Code --- crates/protocol/src/chunking.rs | 37 ++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/crates/protocol/src/chunking.rs b/crates/protocol/src/chunking.rs index 0e3c4f51..ecf6c411 100644 --- a/crates/protocol/src/chunking.rs +++ b/crates/protocol/src/chunking.rs @@ -354,10 +354,26 @@ mod tests { #[test] fn message_right_at_the_threshold_goes_out_as_one_frame() { let json = big_json(NIP44_SAFE_PLAINTEXT_BYTES, "x"); - assert!(json.len() <= NIP44_SAFE_PLAINTEXT_BYTES); + assert_eq!(json.len(), NIP44_SAFE_PLAINTEXT_BYTES); assert_eq!(frame_encoded_message(&json, cid_gen("cid")), vec![json.clone()]); } + /// One byte past the safe plaintext size is one byte too many: the + /// message must split, every fragment must fit the cap, and the parts must + /// still reproduce it exactly. + #[test] + fn one_byte_past_the_threshold_splits_into_frames_within_the_cap() { + let json = big_json(NIP44_SAFE_PLAINTEXT_BYTES + 1, "x"); + assert_eq!(json.len(), NIP44_SAFE_PLAINTEXT_BYTES + 1); + let frames = frame_encoded_message(&json, cid_gen("over")); + assert!(frames.len() >= 2, "a {} byte message went out as one frame", json.len()); + for (i, f) in frames.iter().enumerate() { + assert!(utf8_size(f) <= NIP44_SAFE_PLAINTEXT_BYTES, "frame {i} is {} bytes", utf8_size(f)); + } + let rebuilt: String = frames.iter().map(|f| parse_chunk_envelope(f).unwrap().part).collect(); + assert_eq!(rebuilt, json); + } + #[test] fn oversize_splits_into_ge2_fragments_each_within_the_cap_and_reassembles_exactly() { let json = big_json(200_000, "x"); @@ -473,6 +489,25 @@ mod tests { } } + /// A second fragment for a cid that claims a different fragment count is a + /// corrupt sender: the group is abandoned and started afresh, never merged + /// into a message made of both senders' parts. + #[test] + fn assembler_abandons_a_group_when_the_fragment_count_changes() { + let mut a = ChunkAssembler::new(); + assert_eq!(a.offer(&encode_envelope("c", 0, 2, "old-A"), 0), AssemblerResult::Buffered); + // Same cid, different n: the old group is dropped and this starts a new one. + assert_eq!(a.offer(&encode_envelope("c", 0, 3, "new-A"), 0), AssemblerResult::Buffered); + assert_eq!(a.open_count(), 1, "the abandoned group must be gone, not kept beside the new one"); + assert_eq!(a.offer(&encode_envelope("c", 1, 3, "new-B"), 0), AssemblerResult::Buffered); + assert_eq!( + a.offer(&encode_envelope("c", 2, 3, "new-C"), 0), + AssemblerResult::Assembled { json: "new-Anew-Bnew-C".into() }, + "the abandoned group's parts must not leak into the assembled message" + ); + assert_eq!(a.open_count(), 0); + } + #[test] fn assembler_never_surfaces_partial_and_sweeps_after_ttl() { let mut a = ChunkAssembler::with_limits(1000, DEFAULT_MAX_OPEN, DEFAULT_MAX_BYTES); From 178ca0634b2d6730de4a2d1d11ba04d73eb5f9d1 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:16:27 +0100 Subject: [PATCH 24/42] test(protocol): run packed payloads through the public decoders Both `decode_phone_to_bridge` and `decode_bridge_to_phone` unpack before they parse, but the corpus only ever held plain JSON objects, so nothing proved that path on a real wire payload. The corpus now carries two valid packed messages (one each way, generated with `packing::pack`), and a string entry in the valid/rejected lists means raw wire text used as-is rather than a JSON object re-serialized into quotes that would never reach `unpack`. Four malformed packed payloads join the rejected list: invalid base64, a truncated deflate stream, a stream inflating to non-UTF-8, and one inflating past `MAX_UNPACKED_BYTES`. The conformance test now requires those to fail *while unpacking* (`invalid packed payload`), so a change that lets a bad stream through and merely fails the later JSON parse is caught. Co-Authored-By: Claude Code --- crates/protocol/fixtures/README.md | 8 +++ crates/protocol/fixtures/corpus.json | 15 +++-- crates/protocol/tests/codec_conformance.rs | 70 +++++++++++++++++----- 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/crates/protocol/fixtures/README.md b/crates/protocol/fixtures/README.md index 8bf703c2..d63e64ba 100644 --- a/crates/protocol/fixtures/README.md +++ b/crates/protocol/fixtures/README.md @@ -8,6 +8,14 @@ implementation of the protocol can test itself against. - `phoneToBridge.valid` / `bridgeToPhone.valid` — each decodes, and `encode(decode(x))` re-decodes to an equal value (semantic round-trip). - `phoneToBridge.rejected` / `bridgeToPhone.rejected` — each is a decode error. +- A **string** entry in either list is raw wire text rather than a JSON + object: a packed payload (`~` + base64 of the raw deflate stream, see + `packing.rs`). It is generated with the crate's own `pack`, not by hand, and + is written verbatim (re-serializing it as a JSON string would hide it from + the packing path). A rejected string fixture must fail *unpacking*. + The malformed ones are: bad base64, a truncated deflate stream, a stream + that inflates to non-UTF-8, and one that inflates past + `MAX_UNPACKED_BYTES`. - `forwardCompatible` — each still decodes: unknown fields are ignored, and an unknown value of an open vocabulary (entry type, tool, task and notice kind, MCP status, session state, install state, terminal end reason, diff --git a/crates/protocol/fixtures/corpus.json b/crates/protocol/fixtures/corpus.json index 4685a343..8c0c4f21 100644 --- a/crates/protocol/fixtures/corpus.json +++ b/crates/protocol/fixtures/corpus.json @@ -84,7 +84,8 @@ { "v": 12, "type": "terminal-close", "term": "t1" }, { "v": 12, "type": "web-sign-in", "code": "K7QM-4XPA" }, { "v": 12, "type": "web-browsers-request" }, - { "v": 12, "type": "web-browser-sign-out", "id": "0123456789abcdef" } + { "v": 12, "type": "web-browser-sign-out", "id": "0123456789abcdef" }, + "~7crJDcJADAXQViyfIyQ4pgPKAGJgWMaT2JNV6T2TNqJ/+8tbuOf6fKnYpyRcc4gpO1dsYhY0XpuyWekuo5fob6E2h8eX7p0OkZ460if/k5H20tF+/27zRI2+TgQMDAwMDAwMDAwMDAx8GMzrBg==" ], "rejected": [ { "v": 12, "type": "web-sign-in" }, @@ -126,7 +127,11 @@ { "v": 12, "type": "keypress", "sessionId": "s", "key": "1", "context": "plan-approval" }, { "v": 12, "type": "terminal-input", "term": "t1", "seq": -1, "data": "aGk=" }, { "v": 12, "type": "terminal-open", "openId": "o1", "cols": 70000, "rows": 24 }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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB2Ag==" ] }, "bridgeToPhone": { @@ -224,7 +229,8 @@ { "v": 12, "type": "sessions", "machine": "m", "sessions": [], "agents": [], "terminals": [{ "term": "t1", "shell": "/bin/bash", "opened": 1792008058637, "device": "bf1f111943b3930a4c64a5c06d9153b2", "name": "Pixel 8", "writer": "bf1f111943b3930a4c64a5c06d9153b2", "cols": 80, "rows": 24 }, { "term": "t2", "shell": "/bin/zsh", "opened": 1792008058638, "device": "aa11", "writer": "aa11", "cols": 132, "rows": 43 }] }, { "v": 12, "type": "web-sign-in-result", "code": "K7QM-4XPA", "signedIn": true }, { "v": 12, "type": "web-browsers", "browsers": [] }, - { "v": 12, "type": "web-browsers", "browsers": [{ "id": "0123456789abcdef", "by": "npub1owner", "sinceMs": 1792008058637, "lastSeenMs": 1792008059000, "expiresMs": 1804598458637, "peer": "192.168.1.20", "agent": "Mozilla/5.0 (Android 15)" }] } + { "v": 12, "type": "web-browsers", "browsers": [{ "id": "0123456789abcdef", "by": "npub1owner", "sinceMs": 1792008058637, "lastSeenMs": 1792008059000, "expiresMs": 1804598458637, "peer": "192.168.1.20", "agent": "Mozilla/5.0 (Android 15)" }] }, + "~7cxNDoJADAXgq0y6BlJJNIYbuGelcYFScRSYYX4QJNzdAW5hmnTz+r52gh6yXRqBGzVBBso77R1EYMlaqdpTGZZ2zV2AEVDrjCQL2WUCJxuyrmh0MCmmhxiPMe5zxGydBBHPsJ2M+fbf0bB8N6peUlGFLsR1G8onic7L+1vcjPq04qEG8fKNtkL1ZMRS18V3FKWqEsGYMWPGjBkzZsyYMWPGjP8Gw3ydfw==" ], "rejected": [ { "v": 10, "type": "input-ack", "sessionId": "s", "inputId": "i" }, @@ -246,7 +252,8 @@ { "v": 12, "type": "pair-ack", "machine": "m", "ok": true, "reason": "meltdown" }, { "v": 12, "type": "mode-confirmed", "sessionId": "s", "mode": "plan" }, { "v": 12, "type": "web-browsers" }, - { "v": 12, "type": "warp", "sessionId": "s" } + { "v": 12, "type": "warp", "sessionId": "s" }, + "~+//vLwA=" ] }, "directFrames": { diff --git a/crates/protocol/tests/codec_conformance.rs b/crates/protocol/tests/codec_conformance.rs index b09d8c1b..ad15fe82 100644 --- a/crates/protocol/tests/codec_conformance.rs +++ b/crates/protocol/tests/codec_conformance.rs @@ -13,10 +13,31 @@ fn corpus() -> Value { serde_json::from_str(CORPUS).expect("corpus.json is valid JSON") } +/// A corpus entry is a message object, or — for a packed payload — the raw +/// wire text as a JSON string. Decoders take wire text, so a string entry is +/// used as-is; re-serializing it would quote it into valid but meaningless +/// JSON and the packing path would never run. +fn wire_text(entry: &Value) -> String { + entry.as_str().map(str::to_string).unwrap_or_else(|| entry.to_string()) +} + +/// A packed fixture must really be packed, or the corpus has stopped +/// exercising `packing::unpack` through the public decoders. +fn assert_packed(entry: &Value, where_: &str) { + if entry.is_string() { + let text = wire_text(entry); + assert!( + text.starts_with(protocol::packing::PACKED_PREFIX), + "{where_}: a string fixture must be a packed payload, got {text:.80}" + ); + } +} + #[test] fn phone_to_bridge_valid_decode_and_semantic_round_trip() { for (i, msg) in corpus()["phoneToBridge"]["valid"].as_array().unwrap().iter().enumerate() { - let json = msg.to_string(); + assert_packed(msg, &format!("phoneToBridge.valid[{i}]")); + let json = wire_text(msg); let decoded = decode_phone_to_bridge(&json) .unwrap_or_else(|e| panic!("phoneToBridge.valid[{i}] {json} -> {e}")); let wire = encode_phone_to_bridge(&decoded) @@ -30,18 +51,26 @@ fn phone_to_bridge_valid_decode_and_semantic_round_trip() { #[test] fn phone_to_bridge_rejected_are_errors() { for (i, msg) in corpus()["phoneToBridge"]["rejected"].as_array().unwrap().iter().enumerate() { - let json = msg.to_string(); - assert!( - decode_phone_to_bridge(&json).is_err(), - "phoneToBridge.rejected[{i}] {json} decoded but should not have" - ); + let json = wire_text(msg); + let error = decode_phone_to_bridge(&json) + .err() + .unwrap_or_else(|| panic!("phoneToBridge.rejected[{i}] {json:.160} decoded but should not have")); + if msg.is_string() { + // A malformed packed fixture must be refused by unpacking, not + // merely fail JSON parsing after a wrong successful unpack. + assert!( + error.to_string().starts_with("invalid packed payload"), + "phoneToBridge.rejected[{i}] must fail unpacking, got: {error}" + ); + } } } #[test] fn bridge_to_phone_valid_decode_and_semantic_round_trip() { for (i, msg) in corpus()["bridgeToPhone"]["valid"].as_array().unwrap().iter().enumerate() { - let json = msg.to_string(); + assert_packed(msg, &format!("bridgeToPhone.valid[{i}]")); + let json = wire_text(msg); let decoded = decode_bridge_to_phone(&json) .unwrap_or_else(|e| panic!("bridgeToPhone.valid[{i}] {json} -> {e}")); let wire = encode_bridge_to_phone(&decoded); @@ -54,11 +83,16 @@ fn bridge_to_phone_valid_decode_and_semantic_round_trip() { #[test] fn bridge_to_phone_rejected_are_errors() { for (i, msg) in corpus()["bridgeToPhone"]["rejected"].as_array().unwrap().iter().enumerate() { - let json = msg.to_string(); - assert!( - decode_bridge_to_phone(&json).is_err(), - "bridgeToPhone.rejected[{i}] {json} decoded but should not have" - ); + let json = wire_text(msg); + let error = decode_bridge_to_phone(&json) + .err() + .unwrap_or_else(|| panic!("bridgeToPhone.rejected[{i}] {json:.160} decoded but should not have")); + if msg.is_string() { + assert!( + error.to_string().starts_with("invalid packed payload"), + "bridgeToPhone.rejected[{i}] must fail unpacking, got: {error}" + ); + } } } @@ -167,14 +201,18 @@ fn corpus_covers_every_message_type() { let c = corpus(); let mut p2b = std::collections::BTreeSet::new(); for msg in c["phoneToBridge"]["valid"].as_array().unwrap() { - let decoded = decode_phone_to_bridge(&msg.to_string()).unwrap(); - assert_eq!(p2b_type(&decoded), msg["type"].as_str().unwrap(), "wire name of {msg}"); + let decoded = decode_phone_to_bridge(&wire_text(msg)).unwrap(); + if let Some(ty) = msg["type"].as_str() { + assert_eq!(p2b_type(&decoded), ty, "wire name of {msg}"); + } p2b.insert(p2b_type(&decoded)); } let mut b2p = std::collections::BTreeSet::new(); for msg in c["bridgeToPhone"]["valid"].as_array().unwrap() { - let decoded = decode_bridge_to_phone(&msg.to_string()).unwrap(); - assert_eq!(b2p_type(&decoded), msg["type"].as_str().unwrap(), "wire name of {msg}"); + let decoded = decode_bridge_to_phone(&wire_text(msg)).unwrap(); + if let Some(ty) = msg["type"].as_str() { + assert_eq!(b2p_type(&decoded), ty, "wire name of {msg}"); + } b2p.insert(b2p_type(&decoded)); } assert_eq!(p2b.len(), P2B_TYPES, "phone→bridge types with a fixture: {p2b:?}"); From 0a15c00a61db5958aab12cdc0a94a6398c8545f7 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:19:28 +0100 Subject: [PATCH 25/42] test(protocol,nostr-transport): property-test decoder totality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every phone-wire decoder is total by contract, and the relay frame parser runs in the socket read loop on whatever a relay sends; a panic on a hostile payload crashes the phone or the bridge rather than dropping one message. Nothing exercised that, because the corpus only holds well-formed examples. The new properties feed three shapes to `decode_phone_to_bridge`, `decode_bridge_to_phone`, `decode_direct_frame`, `parse_chunk_envelope`, the chunk assembler and `parse_relay_message`: arbitrary text, arbitrary bytes (read lossily, as a hostile payload would be), and one to a handful of corrupted bytes in every message the corpus calls valid — including real chunk fragments, so the assembler sees frames it otherwise only gets from a bridge. 256 cases per property, so the suite stays well under a second in debug next to the deterministic tests; proptest still shrinks a failure to a minimal input (a planted `unwrap` shrinks to the empty frame). Co-Authored-By: Claude Code --- Cargo.lock | 157 ++++++++++++++++++- crates/nostr-transport/Cargo.toml | 3 + crates/nostr-transport/tests/frames_props.rs | 73 +++++++++ crates/protocol/Cargo.toml | 6 + crates/protocol/tests/totality_props.rs | 115 ++++++++++++++ 5 files changed, 350 insertions(+), 4 deletions(-) create mode 100644 crates/nostr-transport/tests/frames_props.rs create mode 100644 crates/protocol/tests/totality_props.rs diff --git a/Cargo.lock b/Cargo.lock index 94a2beba..dc093549 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -296,6 +296,25 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "bit-set" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56d87354e4229f54a44f7bf2435906a4656dba36026ab6eaca629a2c436a691c" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5727b15fa97d4f4fee0a3b7c3d550ed0269f54329207b86388de918604e31269" +dependencies = [ + "borsh", + "serde", +] + [[package]] name = "bitcoin-consensus-encoding" version = "1.2.0" @@ -363,6 +382,30 @@ dependencies = [ "generic-array", ] +[[package]] +name = "borsh" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "553c5d846a6ba5150c65e3b1b8ec073bcf1abc20f9b7220de384a4443ea4e20a" +dependencies = [ + "borsh-derive", + "bytes", + "cfg_aliases 0.2.2", +] + +[[package]] +name = "borsh-derive" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12cdfe656708a01f89b451a7d36466e6fe6c414de0aa18fc54f864f6f9ca9f56" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 3.0.5", +] + [[package]] name = "bridge-core" version = "1.3.0-rc5" @@ -649,6 +692,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" +[[package]] +name = "core_detect" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" + [[package]] name = "cpufeatures" version = "0.2.17" @@ -848,6 +897,12 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e0f1c7c3a72c66fd80abe965175f7523475c0489a87d3ff9d6e8c87d87a9d2d" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "foldhash" version = "0.1.5" @@ -1496,6 +1551,7 @@ dependencies = [ "futures-util", "log", "nostr", + "proptest", "protocol", "rcgen", "rustls", @@ -1515,6 +1571,15 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.37.3" @@ -1670,6 +1735,15 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "proc-macro-crate" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e67ba7e9b2b56446f1d419b1d807906278ffa1a658a8a5d8a39dcb1f5a78614f" +dependencies = [ + "toml_edit", +] + [[package]] name = "proc-macro2" version = "1.0.107" @@ -1679,6 +1753,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "proptest" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8530004ccb15eae51c7e40009fbe317f341f804db54dc033eec1c50be28cfa0" +dependencies = [ + "bit-set", + "bit-vec", + "bitflags 2.13.2", + "chacha20 0.10.2", + "core_detect", + "num-traits", + "rand 0.10.2", + "rand_xorshift", + "regex-syntax", + "rusty-fork", + "tempfile", + "unarray", +] + [[package]] name = "protocol" version = "1.3.0-rc5" @@ -1687,6 +1781,7 @@ dependencies = [ "hex", "miniz_oxide", "nostr", + "proptest", "serde", "serde_json", "specta", @@ -1700,6 +1795,12 @@ version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d68782463e408eb1e668cf6152704bd856c78c5b6417adaee3203d8f4c1fc9ec" +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quinn" version = "0.11.12" @@ -1827,6 +1928,15 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "rand_xorshift" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60aa6af80be32871323012e02e6e65f8a7cc7890931ae421d217ad8fe0df2ccf" +dependencies = [ + "rand_core 0.10.1", +] + [[package]] name = "rcgen" version = "0.13.2" @@ -2009,6 +2119,18 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" +[[package]] +name = "rusty-fork" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6bf79ff24e648f6da1f8d1f011e9cac26491b619e6b9280f2b47f1774e6ee2" +dependencies = [ + "fnv", + "quick-error", + "tempfile", + "wait-timeout", +] + [[package]] name = "ryu" version = "1.0.23" @@ -2559,18 +2681,30 @@ dependencies = [ [[package]] name = "toml_datetime" -version = "1.1.1+spec-1.1.0" +version = "1.1.2+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3165f65f62e28e0115a00b2ebdd37eb6f3b641855f9d636d3cd4103767159ad7" +checksum = "2b86d767906c6c42421dcba507eb9d203e779497710a47782a224bb871653053" dependencies = [ "serde_core", ] +[[package]] +name = "toml_edit" +version = "0.25.17+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3641d5bbb5349a79e1020a242d251efbc546ad8048d133958323ce9c40a9c9c" +dependencies = [ + "indexmap", + "toml_datetime", + "toml_parser", + "winnow", +] + [[package]] name = "toml_parser" -version = "1.1.3+spec-1.1.0" +version = "1.1.5+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d38ac1cf9b95face32296c0a3ede1fdc270627c9d9c02a7274dd6d960dc4d56" +checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" dependencies = [ "winnow", ] @@ -2677,6 +2811,12 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + [[package]] name = "unicode-ident" version = "1.0.24" @@ -2890,6 +3030,15 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "wait-timeout" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11" +dependencies = [ + "libc", +] + [[package]] name = "want" version = "0.3.1" diff --git a/crates/nostr-transport/Cargo.toml b/crates/nostr-transport/Cargo.toml index e0dc9031..a8090082 100644 --- a/crates/nostr-transport/Cargo.toml +++ b/crates/nostr-transport/Cargo.toml @@ -45,3 +45,6 @@ tokio = { version = "1", features = ["rt", "net", "time", "sync", "macros", "io- rcgen = { version = "0.13", default-features = false, features = ["crypto", "ring"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] } rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] } +# Totality of the relay frame codec: arbitrary bytes and mutated frames must +# be an `Err`/`Unknown`, never a panic in the read loop. +proptest = "1" diff --git a/crates/nostr-transport/tests/frames_props.rs b/crates/nostr-transport/tests/frames_props.rs new file mode 100644 index 00000000..ad43a62c --- /dev/null +++ b/crates/nostr-transport/tests/frames_props.rs @@ -0,0 +1,73 @@ +//! Totality: [`parse_relay_message`] runs in the socket read loop on +//! whatever a relay sends, so it must answer `Ok` or `Err` for any text — +//! including a frame that is one byte away from valid. A panic here kills the +//! read loop rather than dropping the frame. +//! +//! Cases per property: [`CASES`] keeps this a few seconds in debug while still +//! walking every verb's argument checks; proptest shrinks a failure to a +//! minimal input. + +use nostr_transport::frames::parse_relay_message; +use proptest::prelude::*; + +const CASES: u32 = 256; + +/// Frames the relay side legitimately sends, one per parsed shape (plus an +/// unknown verb, which is `Unknown` rather than an error). +const VALID_FRAMES: &[&str] = &[ + r#"["EVENT","sub-1",{"id":"aa","kind":24515,"pubkey":"bb","content":"x","tags":[],"created_at":1,"sig":"cc"}]"#, + r#"["EOSE","s"]"#, + r#"["CLOSED","s","auth-required: nope"]"#, + r#"["CLOSED","s"]"#, + r#"["OK","evt-id",true,""]"#, + r#"["OK","evt-id",false,"rate-limited: slow down"]"#, + r#"["NOTICE","hello"]"#, + r#"["AUTH","challenge-xyz"]"#, + r#"["COUNT","s",{"count":3}]"#, +]; + +/// Flip, overwrite or delete bytes, then pass the result through as UTF-8 (the +/// parser takes `&str`; invalid sequences become U+FFFD, as a lossy read of a +/// hostile frame would). +fn mutate(text: &str, mutations: &[u8]) -> String { + let mut bytes = text.as_bytes().to_vec(); + for (k, m) in mutations.iter().enumerate() { + if bytes.is_empty() { + break; + } + let at = (usize::from(*m) * 31 + k) % bytes.len(); + match *m % 3 { + 0 => bytes[at] ^= 0x5a, + 1 => bytes[at] = *m, + _ => { + bytes.remove(at); + } + } + } + String::from_utf8_lossy(&bytes).into_owned() +} + +proptest! { + #![proptest_config(ProptestConfig { + cases: CASES, + // These live in `tests/`, where proptest cannot find the source-tree + // regression file and warns every run; a failure still prints its seed. + failure_persistence: None, + ..ProptestConfig::default() + })] + + #[test] + fn arbitrary_text_never_panics_the_relay_frame_parser(text in any::()) { + let _ = parse_relay_message(&text); + } + + #[test] + fn arbitrary_bytes_never_panic_the_relay_frame_parser(bytes in proptest::collection::vec(any::(), 0..256)) { + let _ = parse_relay_message(&String::from_utf8_lossy(&bytes)); + } + + #[test] + fn mutated_relay_frames_never_panic_the_parser(frames in prop::sample::select(VALID_FRAMES), mutations in proptest::collection::vec(any::(), 0..=24)) { + let _ = parse_relay_message(&mutate(frames, &mutations)); + } +} diff --git a/crates/protocol/Cargo.toml b/crates/protocol/Cargo.toml index fce43cf7..63a2c2cf 100644 --- a/crates/protocol/Cargo.toml +++ b/crates/protocol/Cargo.toml @@ -35,3 +35,9 @@ specta = { version = "=2.0.0-rc.25", features = ["derive", "serde_json"] } # JSON (as opposed to a real shape like `OutputEntry`) are overridden to # TS `unknown` instead of being expanded. specta-typescript = "0.0.12" + +[dev-dependencies] +# Totality: property tests feed arbitrary bytes and mutated corpus messages +# to every public decoder, so a panic on a hostile payload fails CI instead of +# taking down a phone or the bridge. +proptest = "1" diff --git a/crates/protocol/tests/totality_props.rs b/crates/protocol/tests/totality_props.rs new file mode 100644 index 00000000..459d2d8b --- /dev/null +++ b/crates/protocol/tests/totality_props.rs @@ -0,0 +1,115 @@ +//! Totality: every public decoder returns `Ok` or `Err` for whatever reaches +//! it — arbitrary bytes, and arbitrary mutations of the messages the corpus +//! says are valid. A panic here is a remote crash: these inputs come from a +//! relay or a peer, not from code we control. The properties call the +//! decoders and do nothing else; proptest reports a panic as a failure and +//! shrinks the input to a minimal one. +//! +//! Cases per property: see [`CASES`]. Enough to walk the decoders' UTF-8, +//! index and serde paths on every run, small enough to stay well inside a +//! debug test run. + +use proptest::prelude::*; +use protocol::chunking::{frame_encoded_message, parse_chunk_envelope, ChunkAssembler}; +use protocol::direct::decode_direct_frame; +use protocol::{decode_bridge_to_phone, decode_phone_to_bridge}; +use serde_json::Value; + +/// Anything at or below the point where a decoder would have to be quadratic +/// to notice; the corpus mutations below cover the large-payload paths. +const CASES: u32 = 256; + +const CORPUS: &str = include_str!("../fixtures/corpus.json"); + +fn config() -> ProptestConfig { + // The source-tree regression file is for `src` unit tests; these live in + // `tests/`, where proptest cannot find it and says so every run. A + // failure still prints its seed, so it is reproducible from the output. + ProptestConfig { cases: CASES, failure_persistence: None, ..ProptestConfig::default() } +} + +/// Every public decoder the phone and the bridge ingest with, on one input. +/// `now_ms` is pushed to both ends of the assembler's clock too, so a single +/// `offer` also exercises the TTL sweep. +fn exercise_ingest(text: &str) { + let _ = decode_phone_to_bridge(text); + let _ = decode_bridge_to_phone(text); + let _ = decode_direct_frame(text); + let _ = parse_chunk_envelope(text); + let mut assembler = ChunkAssembler::new(); + let _ = assembler.offer(text, 0); + let _ = assembler.offer(text, u64::MAX); +} + +/// Every wire message the corpus calls valid, in its wire form (a packed +/// fixture is used verbatim), plus real chunk fragments — one message per +/// fragment — so the mutation property reaches the assembler with frames it +/// would otherwise only see from a real bridge. +fn valid_wire_messages() -> &'static [String] { + static MESSAGES: std::sync::OnceLock> = std::sync::OnceLock::new(); + MESSAGES.get_or_init(|| { + let corpus: Value = serde_json::from_str(CORPUS).expect("corpus.json is valid JSON"); + let mut out = Vec::new(); + for dir in ["phoneToBridge", "bridgeToPhone"] { + for msg in corpus[dir]["valid"].as_array().expect("a valid list") { + out.push(msg.as_str().map(str::to_string).unwrap_or_else(|| msg.to_string())); + } + } + for frame in corpus["directFrames"]["valid"].as_array().expect("a valid list") { + out.push(frame.to_string()); + } + let large = format!(r#"{{"type":"output","sessionId":"s","seq":1,"content":"{}"}}"#, "x".repeat(200_000)); + out.extend(frame_encoded_message(&large, || "cid-property".to_string())); + out + }) +} + +/// Flip, overwrite or delete bytes, then pass the result through as UTF-8 +/// (a decoder takes `&str`; invalid sequences become U+FFFD, as a lossy read +/// of a hostile payload would). +fn mutate(text: &str, mutations: &[u8]) -> String { + let mut bytes = text.as_bytes().to_vec(); + for (k, m) in mutations.iter().enumerate() { + if bytes.is_empty() { + break; + } + let at = (usize::from(*m) * 31 + k) % bytes.len(); + match *m % 3 { + 0 => bytes[at] ^= 0x5a, + 1 => bytes[at] = *m, + _ => { + bytes.remove(at); + } + } + } + String::from_utf8_lossy(&bytes).into_owned() +} + +proptest! { + #![proptest_config(config())] + + /// Unstructured text: no message shape at all, which is what a hostile + /// relay or a scan of random traffic delivers. + #[test] + fn arbitrary_text_never_panics_an_ingest_decoder(text in any::()) { + exercise_ingest(&text); + } + + #[test] + fn arbitrary_bytes_never_panic_an_ingest_decoder(bytes in proptest::collection::vec(any::(), 0..256)) { + exercise_ingest(&String::from_utf8_lossy(&bytes)); + } + + /// Near-valid traffic: one to a handful of corrupted bytes in a message + /// the corpus accepts. This is the shape a decoder bug hides in — a body + /// that parses until a field is missing, an index is short or a length + /// runs past the end. + #[test] + fn mutated_valid_messages_never_panic_an_ingest_decoder( + index in 0usize..valid_wire_messages().len(), + mutations in proptest::collection::vec(any::(), 0..=24), + ) { + let text = mutate(&valid_wire_messages()[index], &mutations); + exercise_ingest(&text); + } +} From 6e520049c93862bfaa3b7b5c409b961266bad335 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:20:24 +0100 Subject: [PATCH 26/42] test(protocol): pin forward compatibility phone to bridge too The forwardCompatible section proves that a peer newer than the decoder is readable: serde ignores fields it does not know, so an added optional field reaches an older bridge as nothing rather than as a decode failure. Every entry there was bridge to phone except one, leaving the phone's own commands unpinned. Phone-to-bridge entries now cover a top-level unknown field on a command with no nested object, on one with a plain field set, on a response, and on the untagged upload-file union where both variants must still be recognised. The closed vocabularies (session option, plugin / agent / MCP / account action) deliberately stay rejected, which the existing rejected fixtures already pin. Co-Authored-By: Claude Code --- crates/protocol/fixtures/corpus.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/protocol/fixtures/corpus.json b/crates/protocol/fixtures/corpus.json index 8c0c4f21..89870383 100644 --- a/crates/protocol/fixtures/corpus.json +++ b/crates/protocol/fixtures/corpus.json @@ -286,6 +286,10 @@ "forwardCompatible": [ { "dir": "b2p", "msg": { "v": 12, "type": "agent-style", "agent": "x", "style": { "userMessage": { "shape": "card", "corner": 3 }, "futureKnob": 1 } } }, { "dir": "p2b", "msg": { "v": 12, "type": "input", "sessionId": "s", "text": "hi", "unknownFutureField": 1 } }, + { "dir": "p2b", "msg": { "v": 12, "type": "create-session", "agent": "opencode", "futureOption": { "a": 1 }, "anotherNewField": true } }, + { "dir": "p2b", "msg": { "v": 12, "type": "set-option", "sessionId": "s", "option": "mode", "value": "plan", "appliedAt": 1 } }, + { "dir": "p2b", "msg": { "v": 12, "type": "permission-response", "sessionId": "s", "requestId": "r", "optionId": "allow_once", "decidedBy": "me" } }, + { "dir": "p2b", "msg": { "v": 12, "type": "upload-file", "sessionId": "s", "hash": "h", "url": "u", "key": "k", "iv": "iv", "filename": "f.png", "mimeType": "image/png", "text": "", "sizeBytes": 12345, "futureField": "x" } }, { "dir": "b2p", "msg": { "v": 12, "type": "input-ack", "sessionId": "s", "inputId": "i", "somethingNew": { "a": 1 } } }, { "dir": "b2p", "msg": { "v": 12, "type": "sessions", "machine": "m", "sessions": [], "agents": [{ "id": "pi", "displayName": "Pi", "futureField": true }], "capabilities": ["files", "future-cap"] } }, { "dir": "b2p", "msg": { "v": 12, "type": "output", "sessionId": "s", "seq": 1, "entries": [{ "timestamp": "t", "entryType": "status", "text": "x", "futureEnvelopeField": 1 }] } }, From 0404d57d61e74259cd35795e8e2827b4d80d2925 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:23:24 +0100 Subject: [PATCH 27/42] test(nostr-transport): cover what set_relays and subscribe_to actually open MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `set_relays` only touches the diff — relays dropped from the list are closed deliberately and added ones dialled — and `subscribe_to` pins a subscription to the relays it names. Both drive every connection and subscription the phone has, and neither had a test; a regression would either keep a removed relay's socket alive, wait forever for its EOSE, or leak a pinned subscription's REQ to a relay it never named. The tests use the mock relay and `wait_until`, no fixed sleeps: the removed relay leaves `connected_relays` and the subscription, which had EOSE from the surviving relay only, completes synchronously; an added relay receives the open subscription's REQ as it comes up; and a pinned subscription's REQ reaches only its named relay, with an ordinary second subscription acting as the barrier that proves it (the unnamed relay's first frame is that second REQ). Co-Authored-By: Claude Code --- crates/nostr-transport/src/ws.rs | 132 ++++++++++++++++++++++++++++--- 1 file changed, 122 insertions(+), 10 deletions(-) diff --git a/crates/nostr-transport/src/ws.rs b/crates/nostr-transport/src/ws.rs index c57c3118..2687c9b4 100644 --- a/crates/nostr-transport/src/ws.rs +++ b/crates/nostr-transport/src/ws.rs @@ -1457,6 +1457,116 @@ mod tests { .await; } + /// `set_relays` only touches the diff: a relay dropped from the list is + /// closed deliberately (no `on_close`) and its socket stops holding up + /// the subscriptions it carried, so EOSE the other relays already sent + /// completes them right away. + #[tokio::test] + async fn set_relays_closes_a_removed_relay_and_stops_the_sub_waiting_on_it() { + LocalSet::new() + .run_until(async { + let mut a = mock_relay().await; + let mut b = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { + relays: vec![a.url.clone(), b.url.clone()], + auth: Rc::new(phone.clone()), + proxy: None, + }); + t.ensure_connected(); + wait_until(|| t.connected_relays().len() == 2).await; + let (_sub, seen) = recording_sub(&t, &phone); + assert!(a.next_frame().await.starts_with(r#"["REQ","cd-1""#)); + assert!(b.next_frame().await.starts_with(r#"["REQ","cd-1""#)); + + // B has stored everything it has, A has not: the subscription + // is not complete while A still carries it. The barrier event + // orders the wait behind B's EOSE. + b.push(r#"["EOSE","cd-1"]"#.to_string()); + b.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "barrier"))); + wait_until(|| !seen.events.borrow().is_empty()).await; + assert_eq!(seen.eoses.get(), 0, "A has not EOSEd, so the sub must still wait on it"); + + t.set_relays(std::slice::from_ref(&b.url)); + + assert_eq!(t.connected_relays(), BTreeSet::from([b.url.clone()]), "the removed relay must no longer be connected"); + assert_eq!(seen.eoses.get(), 1, "with A closed, the sub is complete from B alone"); + assert_eq!(seen.closes.get(), 0, "a relay removed on purpose does not close the subscription"); + t.shutdown(); + }) + .await; + } + + /// A relay added by `set_relays` is dialled and every open subscription is + /// REQ'd to it as it comes up, so it starts carrying traffic immediately. + #[tokio::test] + async fn set_relays_dials_and_subscribes_an_added_relay() { + LocalSet::new() + .run_until(async { + let mut a = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&a, &phone); + t.ensure_connected(); + let (_sub, _seen) = recording_sub(&t, &phone); + assert!(a.next_frame().await.starts_with(r#"["REQ","cd-1""#)); + + let mut c = mock_relay().await; + t.set_relays(&[a.url.clone(), c.url.clone()]); + wait_until(|| t.connected_relays().len() == 2).await; + + assert!( + c.next_frame().await.starts_with(r#"["REQ","cd-1""#), + "the added relay must be sent the open subscription's REQ" + ); + assert!(t.connected_relays().contains(&a.url), "the unchanged relay keeps its socket"); + t.shutdown(); + }) + .await; + } + + /// `subscribe_to` pins a subscription to the named relays: only they get + /// the REQ, and EOSE from one of them is enough although another relay is + /// connected and has said nothing. + #[tokio::test] + async fn subscribe_to_opens_the_subscription_only_on_the_named_relays() { + LocalSet::new() + .run_until(async { + let mut a = mock_relay().await; + let mut b = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { + relays: vec![a.url.clone(), b.url.clone()], + auth: Rc::new(phone.clone()), + proxy: None, + }); + t.ensure_connected(); + wait_until(|| t.connected_relays().len() == 2).await; + + let (callbacks, seen_b) = recording_callbacks(); + let _pinned = t.subscribe_to(std::slice::from_ref(&b.url), a_filter(&phone), callbacks); + assert!(b.next_frame().await.starts_with(r#"["REQ","cd-1""#), "the named relay must carry the sub"); + + // A second, ordinary subscription goes everywhere. A's first + // frame must be ITS REQ: had cd-1 been sent to A, that would + // have arrived first. + let (_all, _seen_a) = recording_sub(&t, &phone); + assert!( + a.next_frame().await.starts_with(r#"["REQ","cd-2""#), + "the pinned subscription must never reach an unnamed relay" + ); + assert!(b.next_frame().await.starts_with(r#"["REQ","cd-2""#)); + + // B's EOSE completes the pinned sub although A, connected and + // REQ'd for the other sub only, never answered for this one. + b.push(r#"["EOSE","cd-1"]"#.to_string()); + b.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "barrier"))); + wait_until(|| !seen_b.events.borrow().is_empty()).await; + assert_eq!(seen_b.eoses.get(), 1, "the pinned sub waits only on the relay it named"); + t.shutdown(); + }) + .await; + } + #[tokio::test] async fn a_deliberate_shutdown_stops_reporting_the_relay_as_connected_at_once() { LocalSet::new() @@ -2014,18 +2124,20 @@ mod tests { closes: Cell, } - fn recording_sub(t: &WsTransport, phone: &Keypair) -> (Box, Rc) { + fn recording_callbacks() -> (SubCallbacks, Rc) { let seen = Rc::new(Seen::default()); let (a, b, c) = (Rc::clone(&seen), Rc::clone(&seen), Rc::clone(&seen)); - let sub = t.subscribe( - a_filter(phone), - SubCallbacks { - on_event: Rc::new(move |ev| a.events.borrow_mut().push(ev.id.clone())), - on_eose: Rc::new(move || b.eoses.set(b.eoses.get() + 1)), - on_close: Rc::new(move |_| c.closes.set(c.closes.get() + 1)), - }, - ); - (sub, seen) + let callbacks = SubCallbacks { + on_event: Rc::new(move |ev| a.events.borrow_mut().push(ev.id.clone())), + on_eose: Rc::new(move || b.eoses.set(b.eoses.get() + 1)), + on_close: Rc::new(move |_| c.closes.set(c.closes.get() + 1)), + }; + (callbacks, seen) + } + + fn recording_sub(t: &WsTransport, phone: &Keypair) -> (Box, Rc) { + let (callbacks, seen) = recording_callbacks(); + (t.subscribe(a_filter(phone), callbacks), seen) } #[tokio::test] From 7aa856df1ed9c183856b01f4e71a4bca2ace245f Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:25:21 +0100 Subject: [PATCH 28/42] test(nostr-transport): pin how leniently auth-required is recognised NIP-42 relays signal an auth demand with a machine-readable `auth-required:` prefix on CLOSED and on a rejected OK. The router matches it case insensitively and after leading whitespace, because a refusal taken for an ordinary close ends a subscription that AUTH could have revived; the prefix still has to be the whole prefix at the start of the message, or any error text that merely mentions the phrase would park a publish or a subscription forever. The tests state both halves: upper-case and space-led spellings keep the sub or publish waiting for the AUTH answer and re-send them once it lands, while `auth-required` without the colon, prefixed by other text, or absent is rejected as a plain close. Co-Authored-By: Claude Code --- crates/nostr-transport/src/router.rs | 80 ++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/crates/nostr-transport/src/router.rs b/crates/nostr-transport/src/router.rs index 43607cbe..daf6508d 100644 --- a/crates/nostr-transport/src/router.rs +++ b/crates/nostr-transport/src/router.rs @@ -1135,6 +1135,86 @@ mod tests { assert!(r.route(R1, closed("s1", "auth-required: x")).is_empty()); } + /// The NIP-42 prefix is matched leniently on purpose: relays spell it in + /// other cases and with leading whitespace, and treating a refusal as an + /// ordinary CLOSED would kill a subscription that AUTH could have + /// revived. It must still be the whole prefix, at the start of the + /// message. + #[test] + fn the_auth_required_prefix_is_case_insensitive_but_anchored() { + for yes in [ + "auth-required: sign in", + "AUTH-REQUIRED: sign in", + "Auth-Required: sign in", + " auth-required: sign in", + "\tauth-required:sign in", + ] { + assert!(is_auth_required(yes), "{yes:?} names the prefix"); + } + for no in [ + "", + "auth-required", + "auth-requiredx: not the prefix", + "error: auth-required: x", + "x auth-required: y", + "auth required: x", + ] { + assert!(!is_auth_required(no), "{no:?} does not name the prefix"); + } + } + + /// A relay that spells the prefix in another case or after a space must + /// still park the subscription until our AUTH is accepted, not kill it. + #[test] + fn a_refusal_spelled_with_other_case_still_waits_for_the_auth_ok() { + let mut r = router_with_two_relays(); + r.open_sub("s1", &relays()); + r.auth_sent(R1, "auth-1"); + assert!(r.route(R1, closed("s1", " AUTH-REQUIRED: sign in first")).is_empty()); + // Parked on R1's AUTH (which does not hold EOSE back), not killed: R2's + // EOSE completes the stored set and no close fires. + assert_eq!( + r.route(R2, RelayMessage::Eose { sub_id: "s1".into() }), + vec![RouterAction::Eose { sub_id: "s1".into() }] + ); + // When the AUTH lands, the REQ goes to R1 again. + assert_eq!( + r.route(R1, ok("auth-1", true, "")), + vec![RouterAction::ResubAfterAuth { relay: R1.into(), sub_id: "s1".into() }] + ); + } + + /// The same leniency applies to the `OK` verdict on a publish: an + /// upper-case refusal is re-sent after AUTH acceptance. + #[test] + fn an_ok_refusal_spelled_with_other_case_waits_for_the_auth_ok() { + let mut r = Router::new(); + r.relay_connected(R1); + r.auth_sent(R1, "auth-1"); + r.open_publish("evt", &[R1.to_string()]); + assert!(r.route(R1, ok("evt", false, "AUTH-REQUIRED: sign in")).is_empty()); + assert_eq!( + r.route(R1, ok("auth-1", true, "")), + vec![RouterAction::RepublishAfterAuth { relay: R1.into(), event_id: "evt".into() }] + ); + } + + /// A message that merely mentions the phrase is not an auth demand: it + /// ends the subscription, which is what the anchoring in the match above + /// protects. + #[test] + fn a_message_that_only_mentions_auth_required_is_final() { + let mut r = Router::new(); + r.relay_connected(R1); + r.open_sub("s1", &[R1.to_string()]); + r.auth_sent(R1, "auth-1"); + let actions = r.route(R1, closed("s1", "error: auth-required: not at the start")); + assert!( + actions.iter().any(|a| matches!(a, RouterAction::SubClosed { .. })), + "{actions:?}" + ); + } + #[test] fn publish_settles_on_first_acceptance_across_relays() { let mut r = router_with_two_relays(); From 353d90df6190e0dd6a3ed6ef5942d169b92ec480 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:16:56 +0100 Subject: [PATCH 29/42] fix(bridge): make a command's id durable before it runs The ingest cursor and dedup ids were written only on the heartbeat and at shutdown. A crash between an accepted command and the next heartbeat left the relay's `since` replay (last seen minus a 5 s grace) free to run the same command again: a duplicate session, input sent twice. Persist the cursor as soon as a command's id is remembered and before dispatching it, so the replay is a no-op. Live-kind keystrokes are unchanged: they take no dedup slot and do not move the cursor. Co-Authored-By: Claude Code --- crates/bridge-core/src/engine/phone.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/bridge-core/src/engine/phone.rs b/crates/bridge-core/src/engine/phone.rs index d67e4e1a..2fba4513 100644 --- a/crates/bridge-core/src/engine/phone.rs +++ b/crates/bridge-core/src/engine/phone.rs @@ -99,7 +99,16 @@ impl Engine { let accepted = if event.kind == protocol::kinds::LIVE_KIND { self.ingest.accept_live(event, &self.config.keys, &keys, now_secs) } else { - self.ingest.accept(event, &self.config.keys, &keys, now_secs) + let accepted = self.ingest.accept(event, &self.config.keys, &keys, now_secs); + // The id is durable before the command runs. The cursor is + // otherwise written only on the heartbeat, and a crash in + // between leaves the relay's `since` replay free to run the + // same command a second time (a duplicate session, input + // sent twice). + if accepted.is_some() { + self.persist_cursor(); + } + accepted }; let Some((msg, key)) = accepted else { return }; // The live kind carries terminal input from a phone and From 6acd22413585287185c65300ef67769d5da43570 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:17:05 +0100 Subject: [PATCH 30/42] test(bridge): a replayed command does not run twice across a restart The relay replays from the cursor minus a five-second grace after a reconnect, so the same command event reaches a bridge that just came back. Two rig tests now pin the dedup down: one where the heartbeat ran before the restart (it stored the cursor) and one where the bridge crashed before a heartbeat, which is the case the per-command cursor write is for. The second creates a session, restarts the engine over the same store, and replays the event: a second session-pending would mean the command ran twice. Co-Authored-By: Claude Code --- crates/bridge-core/tests/restart.rs | 82 +++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 crates/bridge-core/tests/restart.rs diff --git a/crates/bridge-core/tests/restart.rs b/crates/bridge-core/tests/restart.rs new file mode 100644 index 00000000..0bdcb696 --- /dev/null +++ b/crates/bridge-core/tests/restart.rs @@ -0,0 +1,82 @@ +//! What a bridge restart must not lose or repeat: a command the relay +//! replays into a bridge that came back, and a session the phone was told was +//! being created when the bridge went away. + +mod support; + +use bridge_core::{InboundEvent, Input, Via}; +use protocol::events::BridgeToPhone; +use serde_json::json; +use support::*; + +/// A create command, so a second run shows up as a second `session-pending`. +fn create_session(rig: &mut Rig) -> InboundEvent { + let phone = rig.phone.clone(); + rig.phone_event(&phone, json!({"v":12,"type":"create-session","agent":"alpha"}), Via::Relay) +} + +fn pending_ids(msgs: &[BridgeToPhone]) -> Vec { + msgs.iter() + .filter_map(|m| match m { + BridgeToPhone::SessionPending(p) => Some(p.pending_id.clone()), + _ => None, + }) + .collect() +} + +/// The engine persists the ingest cursor on its heartbeat, not per command, +/// so a crash between a command and the next heartbeat must still leave the +/// id durable: the relay's `since` replay would otherwise run it again. +#[test] +fn a_command_is_not_run_twice_when_the_bridge_restarts_before_a_heartbeat() { + let mut rig = Rig::with(RigOptions { + configure: |c| { + c.heartbeat_interval_ms = 1_000; + c.git_poll_interval_ms = 0; + c.retention_interval_ms = 0; + }, + ..Default::default() + }); + rig.host_up(); + rig.take(); + + let event = create_session(&mut rig); + assert_eq!(pending_ids(&rig.messages()).len(), 1, "the command ran once before the restart"); + + // A crash: no shutdown and no heartbeat since the command. + let mut rig = rig.restart(); + rig.take(); + rig.input(Input::RelayEvent { event, via: Via::Relay }); + assert_eq!( + pending_ids(&rig.messages()), + Vec::::new(), + "the relay's replay of an already-run command must create no second session" + ); +} + +/// The same replay after a heartbeat, whose cursor flush is what makes the +/// id durable today; the two paths must agree. +#[test] +fn a_command_is_not_run_twice_when_the_bridge_restarts_after_a_heartbeat() { + let mut rig = Rig::with(RigOptions { + configure: |c| { + c.heartbeat_interval_ms = 1_000; + c.git_poll_interval_ms = 0; + c.retention_interval_ms = 0; + }, + ..Default::default() + }); + rig.host_up(); + rig.take(); + + let event = create_session(&mut rig); + assert_eq!(pending_ids(&rig.messages()).len(), 1, "the command ran once"); + + rig.advance(1_000); + assert_eq!(rig.store.snapshot().get("processedEventIds").map(|ids| ids.contains(&event.id)), Some(true), "the heartbeat stored the id"); + + let mut rig = rig.restart(); + rig.take(); + rig.input(Input::RelayEvent { event, via: Via::Relay }); + assert_eq!(pending_ids(&rig.messages()), Vec::::new(), "the heartbeat's stored id dedups the replay"); +} From 1fb153708832f45775f701f90e016fd04fc3be14 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:17:38 +0100 Subject: [PATCH 31/42] test(bridge): pin how a session pending at a restart is lost A created session joins the registry (and the heartbeat) only when the agent reports it ready; while it waits, the phone has seen session-pending but the id is in no stored document. A restart in that window therefore drops it without a session-failed, without listing it and without a tombstone: the phone's placeholder hangs until its own ten-minute sweep expires it. The test drives a create to pending, restarts the engine, completes the host handshake and takes a heartbeat, and asserts the phone is told something about the pending id. It is ignored because the bridge does not answer yet; it is the reproduction for the reported bug. Co-Authored-By: Claude Code --- crates/bridge-core/tests/restart.rs | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/bridge-core/tests/restart.rs b/crates/bridge-core/tests/restart.rs index 0bdcb696..0dde81d2 100644 --- a/crates/bridge-core/tests/restart.rs +++ b/crates/bridge-core/tests/restart.rs @@ -80,3 +80,38 @@ fn a_command_is_not_run_twice_when_the_bridge_restarts_after_a_heartbeat() { rig.input(Input::RelayEvent { event, via: Via::Relay }); assert_eq!(pending_ids(&rig.messages()), Vec::::new(), "the heartbeat's stored id dedups the replay"); } + +/// A session the phone asked for is not in the registry until the agent +/// answers (`listed` turns true at `session-ready`). A restart before that +/// answer therefore drops it after the phone has already been shown a +/// placeholder: no `session-failed` follows, the heartbeat neither lists the +/// id nor tombstones it, and the phone only loses the placeholder when its +/// own ten-minute sweep gives up. The phone should be told. +#[test] +#[ignore = "known bug: a create-session pending at a bridge restart is dropped without an answer to the phone"] +fn a_create_pending_at_a_restart_is_answered_not_lost() { + let mut rig = Rig::new(); + rig.host_up(); + rig.take(); + rig.send(json!({"v":12,"type":"create-session","agent":"alpha"})); + let pending = pending_ids(&rig.messages()).pop().expect("the phone saw session-pending"); + assert!( + !rig.store.snapshot().get("registry").is_some_and(|registry| registry.contains(&pending)), + "a pending session is deliberately not in the registry" + ); + + // A crash and restart before the agent ever answered. + let mut rig = rig.restart(); + rig.take(); + rig.host_up(); + rig.advance(60_000); + let msgs = rig.messages(); + let failed = msgs.iter().any(|m| matches!(m, BridgeToPhone::SessionFailed(f) if f.pending_id == pending)); + let heartbeat = last_heartbeat(&msgs); + let listed = heartbeat.sessions.iter().any(|s| s.id == pending); + let removed = heartbeat.removed_sessions.as_deref().unwrap_or_default().contains(&pending); + assert!( + failed || listed || removed, + "the phone was told nothing about pending session {pending}: it saw session-pending before the restart" + ); +} From d779fb9c30914bd6e706fe7bd889dbad204b5b95 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:18:15 +0100 Subject: [PATCH 32/42] refactor(direct): thread the listener's bounds through as Limits The connection caps, the handshake, hello and idle timeouts and the maximum frame size were constants read deep inside the accept and serve loops, which left their refusal paths only reachable by opening sixty-four sockets or waiting out a sixty-second timeout. Gather them into a private Limits value that `start` fills with the production numbers, and pass it down. No behaviour changes; a test can now hand the same loops small bounds. Co-Authored-By: Claude Code --- crates/bridge-runtime/src/direct.rs | 67 +++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/crates/bridge-runtime/src/direct.rs b/crates/bridge-runtime/src/direct.rs index 8be51978..c504916f 100644 --- a/crates/bridge-runtime/src/direct.rs +++ b/crates/bridge-runtime/src/direct.rs @@ -86,6 +86,32 @@ const FRAME_LANE_BYTES: usize = 64 * 1024 * 1024; /// deliver, as the phone would reconnect and be handed the same pass again. const FRAME_LANE_STALL: Duration = Duration::from_secs(30); +/// The bounds one direct-link server runs with. Production uses the numbers +/// above; a test passes small ones, so it never opens sixty-four sockets or +/// waits out a production timeout to see a refusal. +#[derive(Clone, Copy)] +struct Limits { + handshake: Duration, + hello: Duration, + idle: Duration, + max_connections: usize, + max_per_host: usize, + max_frame_bytes: usize, +} + +impl Default for Limits { + fn default() -> Self { + Self { + handshake: HANDSHAKE_TIMEOUT, + hello: HELLO_TIMEOUT, + idle: IDLE_TIMEOUT, + max_connections: MAX_CONNECTIONS, + max_per_host: MAX_CONNECTIONS_PER_HOST, + max_frame_bytes: MAX_FRAME_BYTES, + } + } +} + fn now_secs() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0) } @@ -427,6 +453,7 @@ pub async fn start( let npub = protocol::crypto::npub_from_hex(&bridge_pubkey).unwrap_or_else(|_| bridge_pubkey.clone()); let web = crate::web::Web::new(npub, &config.endpoints, inputs.clone(), home, web_config); let hub = Hub::new(bridge_pubkey, inputs); + let limits = Limits::default(); let mut tasks = Vec::new(); let mut bound_wss = None; let mut cert_sha256 = None; @@ -458,7 +485,7 @@ pub async fn start( } acceptor.accept(stream).await.ok() } - }) + }, limits) .await })); } @@ -467,7 +494,7 @@ pub async fn start( log::info!("[Direct] Listening on ws://{addr} for an onion service"); let hub = Rc::clone(&hub); tasks.push(tokio::task::spawn_local(async move { - accept_loop(listener, hub, None, |stream| async move { Some(stream) }).await + accept_loop(listener, hub, None, |stream| async move { Some(stream) }, limits).await })); } let endpoints = advertised_endpoints(config, bound_wss, in_container()); @@ -544,7 +571,7 @@ fn lan_ip() -> Option { socket.local_addr().ok().map(|a| a.ip()).filter(|ip| !ip.is_unspecified() && !ip.is_loopback()) } -async fn accept_loop(listener: TcpListener, hub: Rc, web: Option>, secure: F) +async fn accept_loop(listener: TcpListener, hub: Rc, web: Option>, secure: F, limits: Limits) where S: AsyncRead + AsyncWrite + Unpin + 'static, F: Fn(tokio::net::TcpStream) -> Fut + 'static, @@ -559,16 +586,16 @@ where continue; } }; - if hub.open.get() >= MAX_CONNECTIONS { - log::warn!("[Direct] Refusing {peer}: {MAX_CONNECTIONS} connections open"); + if hub.open.get() >= limits.max_connections { + log::warn!("[Direct] Refusing {peer}: {} connections open", limits.max_connections); continue; } let host = crate::web::rate_key(peer.ip()); { let mut by_host = hub.open_by_host.borrow_mut(); let held = by_host.entry(host).or_insert(0); - if *held >= MAX_CONNECTIONS_PER_HOST { - log::info!("[Direct] Refusing {peer}: {MAX_CONNECTIONS_PER_HOST} connections open from that host"); + if *held >= limits.max_per_host { + log::info!("[Direct] Refusing {peer}: {} connections open from that host", limits.max_per_host); continue; } *held += 1; @@ -578,10 +605,10 @@ where let web = web.clone(); let secured = secure(stream); tokio::task::spawn_local(async move { - if let Ok(Some(stream)) = tokio::time::timeout(HANDSHAKE_TIMEOUT, secured).await { + if let Ok(Some(stream)) = tokio::time::timeout(limits.handshake, secured).await { match web { - Some(web) => route(stream, peer.ip(), &hub, &web).await, - None => serve(stream, &hub).await, + Some(web) => route(stream, peer.ip(), &hub, &web, limits).await, + None => serve(stream, &hub, limits).await, } } hub.open.set(hub.open.get() - 1); @@ -599,10 +626,10 @@ where /// A connection on the listener that also serves the web page: read the /// request head, then hand a WebSocket upgrade to the direct link (with the /// bytes already read put back in front) and anything else to the page. -async fn route(mut stream: S, peer: IpAddr, hub: &Hub, web: &crate::web::Web) { - let Ok(Some(head)) = tokio::time::timeout(HANDSHAKE_TIMEOUT, crate::web::read_head(&mut stream)).await else { return }; +async fn route(mut stream: S, peer: IpAddr, hub: &Hub, web: &crate::web::Web, limits: Limits) { + let Ok(Some(head)) = tokio::time::timeout(limits.handshake, crate::web::read_head(&mut stream)).await else { return }; if crate::web::is_websocket(&head) { - serve(Replay { prefix: head, at: 0, inner: stream }, hub).await; + serve(Replay { prefix: head, at: 0, inner: stream }, hub, limits).await; } else { crate::web::serve(&head, stream, peer, web).await; } @@ -643,9 +670,13 @@ impl AsyncWrite for Replay { } /// One connection: the handshake, then events both ways. -async fn serve(stream: S, hub: &Hub) { - let config = WebSocketConfig { max_message_size: Some(MAX_FRAME_BYTES), max_frame_size: Some(MAX_FRAME_BYTES), ..Default::default() }; - let Ok(Ok(ws)) = tokio::time::timeout(HANDSHAKE_TIMEOUT, tokio_tungstenite::accept_async_with_config(stream, Some(config))).await +async fn serve(stream: S, hub: &Hub, limits: Limits) { + let config = WebSocketConfig { + max_message_size: Some(limits.max_frame_bytes), + max_frame_size: Some(limits.max_frame_bytes), + ..Default::default() + }; + let Ok(Ok(ws)) = tokio::time::timeout(limits.handshake, tokio_tungstenite::accept_async_with_config(stream, Some(config))).await else { return; }; @@ -656,7 +687,7 @@ async fn serve(stream: S, hub: &Hub) { if sink.send(send(&DirectFrame::Challenge(challenge.clone()))).await.is_err() { return; } - let hello = tokio::time::timeout(HELLO_TIMEOUT, next_text(&mut source)).await.ok().flatten(); + let hello = tokio::time::timeout(limits.hello, next_text(&mut source)).await.ok().flatten(); let (auth, since, recipients) = match hello.as_deref().map(decode_direct_frame) { Some(Ok(DirectFrame::Hello { auth, since, recipients })) => (auth, since, recipients), _ => { @@ -702,7 +733,7 @@ async fn serve(stream: S, hub: &Hub) { ok = sink.send(Message::Text(text)).await.is_ok(); } }, - incoming = tokio::time::timeout(IDLE_TIMEOUT, source.next()) => match incoming { + incoming = tokio::time::timeout(limits.idle, source.next()) => match incoming { Ok(Some(Ok(Message::Text(text)))) => match decode_direct_frame(&text) { Ok(DirectFrame::Event(event)) => { let id = event.id.clone(); From 198e9da9b6ad7f0c7d405c59b58f894e67564d09 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:18:47 +0100 Subject: [PATCH 33/42] test(direct): the connection caps, frame limit and hello deadline The direct-link listener refuses a host past its per-host cap and the listener past its total cap, drops a peer that never answers the challenge with HELLO, and stops reading a frame over its size limit. None of those paths had a test: each needed the production bound, so a test now passes a small Limits value (never a wait of the 60-second hello timeout or a sixty-fourth socket). Two 1-host tests pin the two caps separately on loopback; the hello test waits 50 ms and expects the bridge's `closed: expected HELLO`; the frame test sends one message over a 1 KiB limit and expects the connection to end. Co-Authored-By: Claude Code --- crates/bridge-runtime/src/direct.rs | 127 ++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/crates/bridge-runtime/src/direct.rs b/crates/bridge-runtime/src/direct.rs index c504916f..9ac20c60 100644 --- a/crates/bridge-runtime/src/direct.rs +++ b/crates/bridge-runtime/src/direct.rs @@ -829,6 +829,7 @@ mod tests { use super::*; use protocol::crypto::generate_keypair; use protocol::nip42::build_auth_event; + use tokio::task::LocalSet; #[test] fn a_container_does_not_advertise_its_own_address() { @@ -1040,4 +1041,130 @@ mod tests { assert_eq!(first.sha256_hex().len(), 64); assert!(first.acceptor().is_ok()); } + + // --- the listener's bounds --- + + /// A plain listener (no TLS, no page) on a loopback port, with `limits`. + /// The accept task lives on the test's `LocalSet`. + async fn plain_listener(limits: Limits) -> (SocketAddr, Rc) { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + let (inputs, _rx) = mpsc::unbounded_channel(); + let hub = Hub::new("b".into(), inputs); + let accepting = Rc::clone(&hub); + tokio::task::spawn_local(accept_loop(listener, accepting, None, |stream| async move { Some(stream) }, limits)); + (addr, hub) + } + + /// Wait until the accept loop has opened `n` connections: its counters + /// move before the handshake, so this is the state the next connect sees. + async fn connections_reach(hub: &Hub, n: usize) { + let deadline = tokio::time::Instant::now() + Duration::from_secs(5); + while hub.open.get() != n { + assert!(tokio::time::Instant::now() < deadline, "the accept loop did not open {n} connection(s); open={}", hub.open.get()); + tokio::time::sleep(Duration::from_millis(1)).await; + } + } + + /// The next connection is accepted, then closed before any handshake: + /// reading it yields end-of-file. + async fn assert_refused_and_closed(addr: SocketAddr, hub: &Hub, open_now: usize) { + use tokio::io::AsyncReadExt; + let mut refused = tokio::net::TcpStream::connect(addr).await.unwrap(); + let mut buf = [0u8; 1]; + let read = tokio::time::timeout(Duration::from_secs(5), refused.read(&mut buf)) + .await + .expect("the refused connection is closed") + .expect("the refused connection is readable to its end"); + assert_eq!(read, 0, "the connection over a cap is closed, not served"); + assert_eq!(hub.open.get(), open_now, "a refused connection takes no slot"); + } + + #[tokio::test] + async fn one_host_past_its_connection_cap_is_refused() { + LocalSet::new() + .run_until(async { + let limits = Limits { max_connections: 8, max_per_host: 1, handshake: Duration::from_secs(5), ..Limits::default() }; + let (addr, hub) = plain_listener(limits).await; + let _held = tokio::net::TcpStream::connect(addr).await.unwrap(); + connections_reach(&hub, 1).await; + assert_refused_and_closed(addr, &hub, 1).await; + }) + .await; + } + + #[tokio::test] + async fn the_total_connection_cap_refuses_the_next_one() { + LocalSet::new() + .run_until(async { + let limits = Limits { max_connections: 1, max_per_host: 8, handshake: Duration::from_secs(5), ..Limits::default() }; + let (addr, hub) = plain_listener(limits).await; + let _held = tokio::net::TcpStream::connect(addr).await.unwrap(); + connections_reach(&hub, 1).await; + assert_refused_and_closed(addr, &hub, 1).await; + }) + .await; + } + + async fn next_ws_frame(ws: &mut tokio_tungstenite::WebSocketStream) -> DirectFrame { + loop { + match ws.next().await.expect("the socket is open").expect("a valid frame") { + Message::Text(text) => return decode_direct_frame(&text).unwrap(), + _ => continue, + } + } + } + + #[tokio::test] + async fn a_peer_that_never_says_hello_is_dropped_after_the_hello_timeout() { + LocalSet::new() + .run_until(async { + // The production wait is a minute; the test says 50 ms. + let limits = Limits { hello: Duration::from_millis(50), handshake: Duration::from_secs(5), ..Limits::default() }; + let (addr, _hub) = plain_listener(limits).await; + let tcp = tokio::net::TcpStream::connect(addr).await.unwrap(); + let (mut ws, _) = tokio_tungstenite::client_async(format!("ws://{addr}"), tcp).await.expect("the WebSocket handshake"); + assert!(matches!(next_ws_frame(&mut ws).await, DirectFrame::Challenge(_))); + let answer = tokio::time::timeout(Duration::from_secs(5), next_ws_frame(&mut ws)) + .await + .expect("a silent peer is answered once its hello deadline passes"); + assert_eq!(answer, DirectFrame::Closed("expected HELLO".into())); + }) + .await; + } + + #[tokio::test] + async fn a_frame_over_the_socket_limit_closes_the_connection() { + LocalSet::new() + .run_until(async { + let limits = Limits { max_frame_bytes: 1024, hello: Duration::from_secs(5), handshake: Duration::from_secs(5), ..Limits::default() }; + let (addr, hub) = plain_listener(limits).await; + let phone = generate_keypair(); + hub.set_paired(std::slice::from_ref(&phone.pubkey_hex)); + let tcp = tokio::net::TcpStream::connect(addr).await.unwrap(); + let (mut ws, _) = tokio_tungstenite::client_async(format!("ws://{addr}"), tcp).await.expect("the WebSocket handshake"); + let DirectFrame::Challenge(challenge) = next_ws_frame(&mut ws).await else { panic!("expected a challenge") }; + let auth = build_auth_event(&phone, "ws://direct", &challenge, now_secs() * 1000).unwrap(); + let hello = DirectFrame::Hello { auth, since: 0, recipients: vec![] }; + ws.send(Message::Text(encode_direct_frame(&hello))).await.unwrap(); + assert_eq!(next_ws_frame(&mut ws).await, DirectFrame::Ready); + + // One text message larger than the server accepts. + ws.send(Message::Text("x".repeat(limits.max_frame_bytes + 1))).await.unwrap(); + let ended = tokio::time::timeout(Duration::from_secs(5), async { + loop { + match ws.next().await { + Some(Ok(Message::Text(text))) => panic!("an oversize frame was delivered: {text}"), + Some(Ok(_)) => continue, + Some(Err(_)) | None => break, + } + } + }) + .await; + assert!(ended.is_ok(), "an oversize frame ends the connection"); + assert!(hub.conns.borrow().is_empty(), "the connection was unregistered"); + }) + .await; + } } + From c76b35cecf2e2b94bb629c1b3389c955b47a1da6 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:18:55 +0100 Subject: [PATCH 34/42] test(bridge): the real state.json round-trips a full engine document The engine's state went through an in-memory store in every bridge-core test, and the file itself was covered only by the ignored end-to-end test. This writes the documents a bridge keeps between heartbeats - paired phones, the registry with its sessions and nested fields, the ingest cursor and ids, the list revision, a provider profile without a token and empty credentials - through the public StateFile, and reads them back with a fresh one: same identity, same documents, and the registry truly nested in the file rather than an escaped string. A truncated file must be reported with what to do about it and left untouched, not silently treated as a new bridge that has lost its identity and pairings. Co-Authored-By: Claude Code --- crates/bridge-runtime/tests/state.rs | 114 +++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 crates/bridge-runtime/tests/state.rs diff --git a/crates/bridge-runtime/tests/state.rs b/crates/bridge-runtime/tests/state.rs new file mode 100644 index 00000000..eb71de69 --- /dev/null +++ b/crates/bridge-runtime/tests/state.rs @@ -0,0 +1,114 @@ +//! The real `state.json` behind the engine's `Store`: a representative +//! document written through the public `StateFile`, read back by a fresh one, +//! and a damaged file reported instead of silently replaced with an empty +//! state (which would lose the bridge identity and every pairing). + +use std::fs; + +use bridge_core::registry::{RegistryDoc, SessionRecord}; +use bridge_core::settings::{ProviderProfile, StoredCredentials, StoredProfiles}; +use bridge_core::{store_keys, PairedPhone}; +use bridge_runtime::state::StateFile; +use protocol::common::ProviderModel; +use serde_json::json; + +fn record(id: &str) -> SessionRecord { + SessionRecord { + session_id: id.into(), + agent: "alpha".into(), + cwd: "/w/app".into(), + native_session_id: Some("native-1".into()), + previous_native_session_id: Some("native-0".into()), + model: Some("m1".into()), + provider_id: Some("kimi".into()), + effort: Some("high".into()), + mode: Some("plan".into()), + persona: Some("review".into()), + title: Some("Fix the parser".into()), + project: "app".into(), + created_at: "2026-08-05T00:00:00.000Z".into(), + last_activity: "2026-08-05T00:10:00.000Z".into(), + committed: true, + context_window: Some(200_000), + context_percentage: Some(12.5), + } +} + +/// The documents a bridge stores between heartbeats, including the nested +/// registry and provider profiles, come back byte for byte through a fresh +/// `StateFile` — the shape a restarted bridge reads. +#[test] +fn a_representative_engine_state_survives_a_reopen() { + let dir = tempfile::tempdir().unwrap(); + let phone = PairedPhone { + npub: "npub1example".into(), + pubkey_hex: "ab".repeat(32), + label: "Pixel".into(), + paired_at: "2026-08-05T00:00:00.000Z".into(), + clients: Vec::new(), + }; + // A profile without a token is a stored profile too: sessions cannot + // start on it, but a restart must still list it. + let profiles = StoredProfiles { + profiles: vec![ProviderProfile { + id: "kimi".into(), + agent: "alpha".into(), + label: "Kimi".into(), + base_url: "https://api.example/v1".into(), + auth_token: None, + models: vec![ProviderModel { id: "k2".into(), label: Some("K2".into()), provider: None, context_window: Some(128_000) }], + models_from_provider: true, + default_model: Some("k2".into()), + updated_at: Some("2026-08-05T00:00:00.000Z".into()), + }], + }; + let values = [ + (store_keys::PAIRED_PHONES, serde_json::to_string(&vec![phone]).unwrap()), + (store_keys::REGISTRY, serde_json::to_string(&RegistryDoc { sessions: vec![record("s1"), record("s2")], removed_sessions: vec!["s0".into()] }).unwrap()), + (store_keys::LAST_SEEN, "1800000000".into()), + (store_keys::PROCESSED_IDS, serde_json::to_string(&["ev1", "ev2", "ev3"]).unwrap()), + (store_keys::LIST_REV, "1800000123".into()), + (store_keys::PROVIDER_PROFILES, serde_json::to_string(&profiles).unwrap()), + (store_keys::CREDENTIALS, serde_json::to_string(&StoredCredentials::default()).unwrap()), + ]; + + let identity = { + let state = StateFile::open(dir.path()).unwrap(); + let identity = state.identity().unwrap(); + for (key, value) in &values { + state.set(key, value).unwrap(); + } + state.flush().unwrap(); + identity + }; + + let again = StateFile::open(dir.path()).unwrap(); + assert_eq!(again.identity().unwrap().pubkey_hex, identity.pubkey_hex, "the identity is the same bridge"); + for (key, value) in &values { + // Objects come back from `get` in compact, key-sorted form: the + // document is the same, which is what a restarted engine parses. + let got = serde_json::from_str::(&again.get(key).expect("the key is there")).unwrap(); + assert_eq!(got, serde_json::from_str::(value).unwrap(), "{key} came back as it was stored"); + } + // The registry is really nested in the file, not an escaped string, so a + // human can read it and #154's shape is what a reopen parses. + let raw: serde_json::Value = serde_json::from_str(&fs::read_to_string(dir.path().join("state.json")).unwrap()).unwrap(); + assert_eq!(raw["kv"][store_keys::REGISTRY]["sessions"][1]["sessionId"], json!("s2")); + assert_eq!(raw["kv"][store_keys::REGISTRY]["sessions"][0]["contextPercentage"], json!(12.5)); +} + +/// A state file torn by a crash is reported with what to do about it. The +/// bridge must not quietly start with an empty document: that would mint a +/// new identity and forget every pairing. +#[test] +fn a_truncated_state_file_is_reported_not_silently_emptied() { + let dir = tempfile::tempdir().unwrap(); + let full = r#"{"secretKeyHex":"11","kv":{"pairedPhones":[{"npub":"npub1example"}],"lastSeenTimestamp":42}}"#; + let torn = &full[..full.len() / 2]; + fs::write(dir.path().join("state.json"), torn).unwrap(); + + let err = StateFile::open(dir.path()).err().expect("a truncated file is an error"); + assert!(err.contains("corrupt state file"), "the error names the file's state: {err}"); + assert!(err.contains("Move it aside"), "the error says how to recover: {err}"); + assert_eq!(fs::read_to_string(dir.path().join("state.json")).unwrap(), torn, "the damaged file is left for the owner, not overwritten"); +} From 04a5d584ce33fb4c8a149ea9e2d79edb44e12217 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:19:18 +0100 Subject: [PATCH 35/42] test(terminal): a Windows shell gets only the variables built_env names built_env is the allowlist that keeps the bridge's own environment - the agents' provider keys among it - out of a terminal's shell, and the Windows half was untested; only the Unix half had a test through a real pty. This unit test calls the function itself: with a sentinel variable set in the process, every returned name must be one of the nine system variables it names, carry the process's own value, and never be the sentinel; SHELL and TERM, which the Unix shell is given, must not appear. It runs in CI's Windows job. Co-Authored-By: Claude Code --- crates/bridge-runtime/src/terminal/mod.rs | 27 ++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/bridge-runtime/src/terminal/mod.rs b/crates/bridge-runtime/src/terminal/mod.rs index 6b1a6464..ad3a051f 100644 --- a/crates/bridge-runtime/src/terminal/mod.rs +++ b/crates/bridge-runtime/src/terminal/mod.rs @@ -652,7 +652,6 @@ mod tests { /// Tests here read and write the process environment (a pty inherits /// none of it, which is what is being checked), and `std::env` is not /// safe to mutate while another thread reads it — so they take turns. - #[cfg(unix)] fn env_lock() -> std::sync::MutexGuard<'static, ()> { static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); LOCK.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) @@ -993,4 +992,30 @@ mod tests { terms.forget("gone"); assert!(terms.replay("gone", 0).is_none()); } + + /// The shell is handed the system variables it reads to find programs + /// and its own directories, and nothing else the bridge inherited: a + /// provider key exported for the agents must not reach a terminal. + #[cfg(windows)] + #[test] + fn a_windows_shell_gets_only_the_variables_built_env_names() { + let _guard = env_lock(); + const LEAK: &str = "CDX_BUILT_ENV_TEST_SECRET"; + std::env::set_var(LEAK, "must-not-leak"); + let env = built_env("pwsh"); + std::env::remove_var(LEAK); + + const NAMED: [&str; 9] = ["PATH", "SystemRoot", "ComSpec", "PATHEXT", "USERPROFILE", "APPDATA", "LOCALAPPDATA", "TEMP", "TMP"]; + for (name, value) in &env { + let name = *name; + assert!(NAMED.contains(&name), "the shell was given a variable built_env does not name: {name}"); + assert!(!value.is_empty(), "{name} is empty"); + assert_eq!(std::env::var(name).ok().as_deref(), Some(value.as_str()), "{name} is the process's own value"); + } + assert!(env.iter().all(|(name, _)| *name != LEAK), "a variable the bridge holds leaked into the shell"); + assert!( + env.iter().all(|(name, _)| *name != "SHELL" && *name != "TERM"), + "a Windows shell is not given the Unix variables" + ); + } } From fd1e5048cfed256622dc5ecf96551156c086df2e Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:21:01 +0100 Subject: [PATCH 36/42] test(bridge): describe the replay tests by their invariant The docs were written against the pre-fix engine and said the cursor is only persisted on the heartbeat, which is no longer true. State what each test guards instead: a relay replay must be a no-op whichever of the two writes got the id to disk. Co-Authored-By: Claude Code --- crates/bridge-core/tests/restart.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/bridge-core/tests/restart.rs b/crates/bridge-core/tests/restart.rs index 0dde81d2..cec4501b 100644 --- a/crates/bridge-core/tests/restart.rs +++ b/crates/bridge-core/tests/restart.rs @@ -24,9 +24,10 @@ fn pending_ids(msgs: &[BridgeToPhone]) -> Vec { .collect() } -/// The engine persists the ingest cursor on its heartbeat, not per command, -/// so a crash between a command and the next heartbeat must still leave the -/// id durable: the relay's `since` replay would otherwise run it again. +/// The relay replays from the cursor minus a five-second grace after a +/// reconnect, so a command the bridge already ran reaches it again once it +/// is back. A restart before the next heartbeat must not lose the id it was +/// remembered under: the replay would create a second session. #[test] fn a_command_is_not_run_twice_when_the_bridge_restarts_before_a_heartbeat() { let mut rig = Rig::with(RigOptions { @@ -54,8 +55,8 @@ fn a_command_is_not_run_twice_when_the_bridge_restarts_before_a_heartbeat() { ); } -/// The same replay after a heartbeat, whose cursor flush is what makes the -/// id durable today; the two paths must agree. +/// The same replay when a heartbeat also ran before the restart and stored +/// the cursor; the two paths must agree. #[test] fn a_command_is_not_run_twice_when_the_bridge_restarts_after_a_heartbeat() { let mut rig = Rig::with(RigOptions { From 21ca097cadd95b63fa85c1d177320a5a416a4cf3 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 08:56:48 +0100 Subject: [PATCH 37/42] docs(bridge): stop linking a private constant from a public doc RUSTDOCFLAGS=-D warnings rejects an intra-doc link from public documentation to a private item, so cargo doc failed on the browser sign-out doc. Name the constant in code font instead. Co-Authored-By: Claude Code --- crates/bridge-runtime/src/web.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bridge-runtime/src/web.rs b/crates/bridge-runtime/src/web.rs index 76409432..c48fe845 100644 --- a/crates/bridge-runtime/src/web.rs +++ b/crates/bridge-runtime/src/web.rs @@ -360,7 +360,7 @@ impl Web { } } - /// Sign out the browser whose id (the first [`ID_LEN`] hex characters of + /// Sign out the browser whose id (the first `ID_LEN` hex characters of /// its key) is `id`; false when no signed-in browser has it. Any /// signed-in browser may sign out any other: they are all the owner's. /// The engine asks for this on `web-browser-sign-out`, and the page's own From 821f79be95bf2c3a7b81eca4284f1764247084a7 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 08:57:17 +0100 Subject: [PATCH 38/42] refactor(nostr-transport): move the WebSocket transport's tests to their own file Pure move: the inline test module body now lives in ws/tests.rs, with one level of indentation removed. No behaviour change. Co-Authored-By: Claude Code --- crates/nostr-transport/src/ws.rs | 1238 +----------------------- crates/nostr-transport/src/ws/tests.rs | 1235 +++++++++++++++++++++++ 2 files changed, 1236 insertions(+), 1237 deletions(-) create mode 100644 crates/nostr-transport/src/ws/tests.rs diff --git a/crates/nostr-transport/src/ws.rs b/crates/nostr-transport/src/ws.rs index 2687c9b4..f0c32de2 100644 --- a/crates/nostr-transport/src/ws.rs +++ b/crates/nostr-transport/src/ws.rs @@ -1305,1240 +1305,4 @@ async fn dial(relay: &str, proxy: Option) -> Result } #[cfg(test)] -mod tests { - use super::*; - use crate::mock::{mock_relay, MockRelay}; - use crate::test_support::wait_until; - use protocol::crypto::{generate_keypair, keypair_from_secret_hex, Keypair}; - use nostr::JsonUtil; - use std::cell::RefCell; - use tokio::task::LocalSet; - - const SEC_PHONE: &str = - "0000000000000000000000000000000000000000000000000000000000000001"; - - fn transport(mock: &MockRelay, phone: &Keypair) -> WsTransport { - WsTransport::new(WsConfig { - relays: vec![mock.url.clone()], - auth: Rc::new(phone.clone()), - proxy: None, - }) - } - - #[tokio::test] - async fn dial_refuses_cleartext_to_a_public_host() { - let err = dial("ws://relay.example.com", None).await.unwrap_err(); - assert!(err.contains("cleartext"), "{err}"); - } - - #[tokio::test] - async fn dial_does_not_refuse_cleartext_to_onion_on_scheme_grounds() { - // A loopback stand-in for Tor: the proxy is asked to CONNECT to the - // onion name (no local resolver sees it), answers success and closes, - // so the dial fails at the WebSocket handshake — never the - // cleartext-rejection this test exists to rule out. - let (proxy, request) = crate::test_support::socks5_proxy().await; - let err = dial("ws://expyuzz4wqqyqhjn.onion:7447", Some(proxy.to_string())).await.unwrap_err(); - assert!(!err.contains("cleartext"), "{err}"); - let request = tokio::time::timeout(Duration::from_secs(5), request) - .await - .expect("the onion dial never reached the proxy") - .expect("the proxy reported its CONNECT"); - assert_eq!(request.host, "expyuzz4wqqyqhjn.onion", "the proxy must resolve the onion name, not this machine"); - } - - #[tokio::test] - async fn dial_does_not_refuse_cleartext_to_loopback() { - let mock = mock_relay().await; - // A real connect+handshake against the mock relay itself, over ws:// - // 127.0.0.1 — proves loopback is genuinely usable, not just "not - // rejected before some other failure". - dial(&mock.url, None).await.unwrap(); - } - - #[test] - fn loopback_includes_the_bracketed_ipv6_form_url_gives() { - for host in ["127.0.0.1", "::1", "[::1]", "localhost"] { - assert!(is_loopback(host), "{host}"); - } - assert!(!is_loopback("192.168.1.20")); - assert!(!is_loopback("[::2]")); - } - - #[tokio::test] - async fn dial_accepts_cleartext_to_bracketed_ipv6_loopback() { - // Nothing has to listen: the cleartext guard must pass and the - // failure must come from the connection attempt instead. - let err = dial("ws://[::1]:1", None).await.unwrap_err(); - assert!(!err.contains("cleartext"), "{err}"); - } - - /// The absolute constraint this guards: with a proxy set, a relay is - /// never dialled directly, and the proxy is asked to CONNECT to the - /// relay's *name*. Resolving the name locally would break `.onion` - /// relays (only Tor can resolve them) and would leak the relay's address - /// to the local resolver besides. - #[tokio::test] - async fn a_proxied_relay_dial_asks_the_socks_proxy_for_the_relay_name_and_never_connects_directly() { - // Stands in for the relay: it resolves from `localhost`, so a dial - // that ignored the proxy would land here and be counted. - let (relay, direct_connections) = crate::test_support::counting_listener().await; - - let (proxy, request) = crate::test_support::socks5_proxy().await; - let err = match dial(&format!("ws://localhost:{}", relay.port()), Some(proxy.to_string())).await { - Ok(_) => panic!("the proxy closes after CONNECT, so no ws handshake can complete"), - Err(err) => err, - }; - assert!(!err.starts_with("socks5:"), "the proxy answered, so the dial must not fail in SOCKS5: {err}"); - - let request = tokio::time::timeout(Duration::from_secs(5), request) - .await - .expect("the relay dial never reached the SOCKS5 proxy") - .expect("the SOCKS5 proxy reported its CONNECT"); - assert_eq!( - request.atyp, 3, - "the relay host must be sent as a domain name (the proxy does the DNS lookup, which is what reaches .onion addresses), not as a locally resolved IP" - ); - assert_eq!(request.host, "localhost", "the proxy was asked for the wrong host"); - assert_eq!(request.port, relay.port(), "the proxy was asked for the wrong port"); - assert_eq!( - direct_connections.load(std::sync::atomic::Ordering::SeqCst), - 0, - "the relay was dialled directly even though a proxy is set" - ); - } - - #[tokio::test] - async fn set_proxy_updates_what_the_next_dial_uses_and_closes_every_live_connection() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - assert_eq!(t.current_proxy(), None); - t.ensure_connected(); - - t.subscribe( - a_filter(&phone), - SubCallbacks { - on_event: Rc::new(|_| {}), - on_eose: Rc::new(|| {}), - on_close: Rc::new(|_| {}), - }, - ); - // Real handshake against the mock relay — poll rather than a - // flat sleep so this isn't a race against however long that - // takes on a loaded CI box. - for _ in 0..100 { - if !t.connected_relays().is_empty() { - break; - } - tokio::time::sleep(Duration::from_millis(10)).await; - } - assert!(!t.connected_relays().is_empty()); - - t.set_proxy(Some("127.0.0.1:9050".to_string())); - assert_eq!(t.current_proxy(), Some("127.0.0.1:9050".to_string())); - // The live connection (dialled before the proxy existed) is - // torn down — every existing socket was dialled under the OLD - // setting, so none of them can be trusted to still be routed - // correctly once it changes. - for _ in 0..100 { - if t.connected_relays().is_empty() { - break; - } - tokio::time::sleep(Duration::from_millis(10)).await; - } - assert!(t.connected_relays().is_empty()); - - t.set_proxy(None); - assert_eq!(t.current_proxy(), None); - }) - .await; - } - - /// `set_relays` only touches the diff: a relay dropped from the list is - /// closed deliberately (no `on_close`) and its socket stops holding up - /// the subscriptions it carried, so EOSE the other relays already sent - /// completes them right away. - #[tokio::test] - async fn set_relays_closes_a_removed_relay_and_stops_the_sub_waiting_on_it() { - LocalSet::new() - .run_until(async { - let mut a = mock_relay().await; - let mut b = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { - relays: vec![a.url.clone(), b.url.clone()], - auth: Rc::new(phone.clone()), - proxy: None, - }); - t.ensure_connected(); - wait_until(|| t.connected_relays().len() == 2).await; - let (_sub, seen) = recording_sub(&t, &phone); - assert!(a.next_frame().await.starts_with(r#"["REQ","cd-1""#)); - assert!(b.next_frame().await.starts_with(r#"["REQ","cd-1""#)); - - // B has stored everything it has, A has not: the subscription - // is not complete while A still carries it. The barrier event - // orders the wait behind B's EOSE. - b.push(r#"["EOSE","cd-1"]"#.to_string()); - b.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "barrier"))); - wait_until(|| !seen.events.borrow().is_empty()).await; - assert_eq!(seen.eoses.get(), 0, "A has not EOSEd, so the sub must still wait on it"); - - t.set_relays(std::slice::from_ref(&b.url)); - - assert_eq!(t.connected_relays(), BTreeSet::from([b.url.clone()]), "the removed relay must no longer be connected"); - assert_eq!(seen.eoses.get(), 1, "with A closed, the sub is complete from B alone"); - assert_eq!(seen.closes.get(), 0, "a relay removed on purpose does not close the subscription"); - t.shutdown(); - }) - .await; - } - - /// A relay added by `set_relays` is dialled and every open subscription is - /// REQ'd to it as it comes up, so it starts carrying traffic immediately. - #[tokio::test] - async fn set_relays_dials_and_subscribes_an_added_relay() { - LocalSet::new() - .run_until(async { - let mut a = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&a, &phone); - t.ensure_connected(); - let (_sub, _seen) = recording_sub(&t, &phone); - assert!(a.next_frame().await.starts_with(r#"["REQ","cd-1""#)); - - let mut c = mock_relay().await; - t.set_relays(&[a.url.clone(), c.url.clone()]); - wait_until(|| t.connected_relays().len() == 2).await; - - assert!( - c.next_frame().await.starts_with(r#"["REQ","cd-1""#), - "the added relay must be sent the open subscription's REQ" - ); - assert!(t.connected_relays().contains(&a.url), "the unchanged relay keeps its socket"); - t.shutdown(); - }) - .await; - } - - /// `subscribe_to` pins a subscription to the named relays: only they get - /// the REQ, and EOSE from one of them is enough although another relay is - /// connected and has said nothing. - #[tokio::test] - async fn subscribe_to_opens_the_subscription_only_on_the_named_relays() { - LocalSet::new() - .run_until(async { - let mut a = mock_relay().await; - let mut b = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { - relays: vec![a.url.clone(), b.url.clone()], - auth: Rc::new(phone.clone()), - proxy: None, - }); - t.ensure_connected(); - wait_until(|| t.connected_relays().len() == 2).await; - - let (callbacks, seen_b) = recording_callbacks(); - let _pinned = t.subscribe_to(std::slice::from_ref(&b.url), a_filter(&phone), callbacks); - assert!(b.next_frame().await.starts_with(r#"["REQ","cd-1""#), "the named relay must carry the sub"); - - // A second, ordinary subscription goes everywhere. A's first - // frame must be ITS REQ: had cd-1 been sent to A, that would - // have arrived first. - let (_all, _seen_a) = recording_sub(&t, &phone); - assert!( - a.next_frame().await.starts_with(r#"["REQ","cd-2""#), - "the pinned subscription must never reach an unnamed relay" - ); - assert!(b.next_frame().await.starts_with(r#"["REQ","cd-2""#)); - - // B's EOSE completes the pinned sub although A, connected and - // REQ'd for the other sub only, never answered for this one. - b.push(r#"["EOSE","cd-1"]"#.to_string()); - b.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "barrier"))); - wait_until(|| !seen_b.events.borrow().is_empty()).await; - assert_eq!(seen_b.eoses.get(), 1, "the pinned sub waits only on the relay it named"); - t.shutdown(); - }) - .await; - } - - #[tokio::test] - async fn a_deliberate_shutdown_stops_reporting_the_relay_as_connected_at_once() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - wait_until(|| !t.connected_relays().is_empty()).await; - - t.shutdown(); - // No polling: the closing task never reports its own death, - // so this must be true synchronously. - assert!(t.connected_relays().is_empty()); - }) - .await; - } - - #[tokio::test] - async fn a_superseded_dial_never_touches_its_replacement() { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - let url = mock.url.clone(); - // The live replacement (generation 7), already up. - let (tx, mut rx) = mpsc::channel::(OUTBOUND_QUEUE); - { - let mut st = t.state.borrow_mut(); - st.conns.insert( - url.clone(), - Conn { tx, stop: Rc::new(Stop::default()), heard: Rc::default(), up: true, up_since: None, generation: 7, dialed_at: SystemTime::now() }, - ); - st.router.relay_connected(&url); - } - - // The old dial (generation 3) finishing either way is ignored. - assert!(!t.on_relay_up(&url, 3)); - t.on_relay_dead(&url, 3, "dial failed: stale".into()); - assert!(t.state.borrow().conns.contains_key(&url)); - assert_eq!(t.connected_relays(), BTreeSet::from([url.clone()])); - assert!(rx.try_recv().is_err(), "nothing was replayed onto the replacement"); - - // Its own death still counts. - t.on_relay_dead(&url, 7, "socket closed".into()); - assert!(!t.state.borrow().conns.contains_key(&url)); - assert!(t.connected_relays().is_empty()); - } - - fn fast_timing() -> Timing { - Timing { - ping_every: Duration::from_millis(50), - dead_after: Duration::from_millis(300), - dial: Duration::from_millis(300), - dial_proxied: Duration::from_millis(300), - write: Duration::from_millis(300), - auth: Duration::from_millis(200), - // Long enough that no redial lands inside these tests; the - // redial tests set their own. - retry_base: Duration::from_secs(30), - retry_max: Duration::from_secs(30), - stable_after: Duration::from_secs(60), - } - } - - fn counting_sub(t: &WsTransport, phone: &Keypair) -> (Box, Rc>) { - let closes = Rc::new(RefCell::new(0u32)); - let c = Rc::clone(&closes); - let sub = t.subscribe( - a_filter(phone), - SubCallbacks { - on_event: Rc::new(|_| {}), - on_eose: Rc::new(|| {}), - on_close: Rc::new(move |_| *c.borrow_mut() += 1), - }, - ); - (sub, closes) - } - - #[tokio::test] - async fn a_dial_that_never_completes_its_handshake_times_out() { - LocalSet::new() - .run_until(async { - // Accepts TCP, never answers the WS handshake. - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let url = format!("ws://{}", listener.local_addr().unwrap()); - let held = tokio::spawn(async move { - let (tcp, _) = listener.accept().await.unwrap(); - tokio::time::sleep(Duration::from_secs(30)).await; - drop(tcp); - }); - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { relays: vec![url], auth: Rc::new(phone), proxy: None }); - t.state.borrow_mut().timing = fast_timing(); - t.ensure_connected(); - assert_eq!(t.state.borrow().conns.len(), 1); - // The failed dial frees the entry, so the FSM can redial. - wait_until(|| t.state.borrow().conns.is_empty()).await; - held.abort(); - }) - .await; - } - - #[tokio::test] - async fn a_relay_that_goes_silent_is_dropped_and_reported() { - LocalSet::new() - .run_until(async { - // Completes the handshake, then neither reads nor writes: - // our pings go unanswered. - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let url = format!("ws://{}", listener.local_addr().unwrap()); - let held = tokio::spawn(async move { - let (tcp, _) = listener.accept().await.unwrap(); - let ws = tokio_tungstenite::accept_async(tcp).await.unwrap(); - tokio::time::sleep(Duration::from_secs(30)).await; - drop(ws); - }); - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { - relays: vec![url], - auth: Rc::new(phone.clone()), - proxy: None, - }); - t.state.borrow_mut().timing = fast_timing(); - t.ensure_connected(); - let (_sub, closes) = counting_sub(&t, &phone); - wait_until(|| !t.connected_relays().is_empty()).await; - - wait_until(|| t.connected_relays().is_empty()).await; - assert!(t.state.borrow().conns.is_empty()); - assert_eq!(*closes.borrow(), 1); - held.abort(); - }) - .await; - } - - #[tokio::test] - async fn a_full_outbound_queue_stops_the_connection_as_a_failure() { - let (tx, _rx) = mpsc::channel::(1); - let conn = Conn { tx, stop: Rc::new(Stop::default()), heard: Rc::default(), up: true, up_since: None, generation: 1, dialed_at: SystemTime::now() }; - conn.send("first".into()); - assert_eq!(conn.stop.reason.get(), None); - conn.send("second".into()); - assert_eq!(conn.stop.reason.get(), Some(StopReason::Overflow)); - // A teardown still wins over a pending overflow: no on_close then. - conn.close(); - assert_eq!(conn.stop.wait().await, StopReason::Deliberate); - } - - #[tokio::test] - async fn a_teardown_while_dialing_abandons_the_dial() { - LocalSet::new() - .run_until(async { - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let url = format!("ws://{}", listener.local_addr().unwrap()); - let held = tokio::spawn(async move { - let (tcp, _) = listener.accept().await.unwrap(); - tokio::time::sleep(Duration::from_secs(30)).await; - drop(tcp); - }); - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { - relays: vec![url], - auth: Rc::new(phone.clone()), - proxy: None, - }); - t.ensure_connected(); - let (_sub, closes) = counting_sub(&t, &phone); - tokio::time::sleep(Duration::from_millis(50)).await; - t.shutdown(); - // The abandoned dial is gone and never reports a close. - tokio::time::sleep(Duration::from_millis(200)).await; - assert_eq!(*closes.borrow(), 0); - assert!(t.state.borrow().conns.is_empty()); - held.abort(); - }) - .await; - } - - fn a_filter(phone: &Keypair) -> Filter { - Filter { - kinds: vec![24515], - authors: vec!["a".repeat(64)], - p_tags: vec![phone.pubkey_hex.clone()], - d_tags: vec![], - c_tags: vec![], - since: None, - } - } - - fn signed_note(kind: u16, content: &str) -> String { - let k = generate_keypair(); - nostr::EventBuilder::new(nostr::Kind::Custom(kind), content) - .sign_with_keys(&nostr::Keys::new(k.secret_key.clone())) - .unwrap() - .as_json() - } - - #[tokio::test] - async fn subscribe_sends_req_then_delivers_events_and_eose() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - - let events: Rc>> = Rc::new(RefCell::new(vec![])); - let eosed = Rc::new(RefCell::new(false)); - let (ev_c, eo_c) = (Rc::clone(&events), Rc::clone(&eosed)); - let sub = t.subscribe( - a_filter(&phone), - SubCallbacks { - on_event: Rc::new(move |ev| ev_c.borrow_mut().push(ev.clone())), - on_eose: Rc::new(move || *eo_c.borrow_mut() = true), - on_close: Rc::new(|_| {}), - }, - ); - - let req = mock.next_frame().await; - assert!(req.starts_with(r#"["REQ","cd-1",{"#), "got {req}"); - assert!(req.contains("\"#p\":["), "filter carries #p: {req}"); - - mock.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "hi"))); - mock.push(r#"["EOSE","cd-1"]"#.to_string()); - wait_until(|| events.borrow().len() == 1 && *eosed.borrow()).await; - - assert_eq!(events.borrow().len(), 1); - assert_eq!(events.borrow()[0].kind, 24515); - assert!(*eosed.borrow()); - drop(sub); - }) - .await; - } - - #[tokio::test] - async fn answers_nip42_auth_with_an_identity_signed_event() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - let _sub = t.subscribe( - a_filter(&phone), - SubCallbacks { - on_event: Rc::new(|_| {}), - on_eose: Rc::new(|| {}), - on_close: Rc::new(|_| {}), - }, - ); - let _req = mock.next_frame().await; - - mock.push(r#"["AUTH","chal-42"]"#.to_string()); - let auth = mock.next_frame().await; - let v: Vec = serde_json::from_str(&auth).unwrap(); - assert_eq!(v[0], "AUTH"); - assert_eq!(v[1]["kind"], 22242); - assert_eq!(v[1]["pubkey"], phone.pubkey_hex); - assert!(v[1]["tags"] - .as_array() - .unwrap() - .iter() - .any(|tag| tag[0] == "challenge" && tag[1] == "chal-42")); - }) - .await; - } - - /// A signer that answers only after a while (an app on the phone). - struct SlowSigner(Keypair); - impl AuthSigner for SlowSigner { - fn sign_auth( - &self, - relay: &str, - challenge: &str, - now_ms: u64, - ) -> std::pin::Pin> + '_>> { - let (relay, challenge) = (relay.to_string(), challenge.to_string()); - Box::pin(async move { - tokio::time::sleep(Duration::from_millis(50)).await; - self.0.sign_auth(&relay, &challenge, now_ms).await - }) - } - } - - /// A signer whose first attempt fails (the user dismissed the prompt), - /// then signs. The failure is local: no AUTH frame is sent. - struct FlakySigner { - key: Keypair, - attempts: Cell, - } - impl AuthSigner for FlakySigner { - fn sign_auth( - &self, - relay: &str, - challenge: &str, - now_ms: u64, - ) -> std::pin::Pin> + '_>> { - let attempt = self.attempts.get(); - self.attempts.set(attempt + 1); - let (relay, challenge) = (relay.to_string(), challenge.to_string()); - let key = self.key.clone(); - Box::pin(async move { - if attempt == 0 { - return Err("the user dismissed the prompt".to_string()); - } - key.sign_auth(&relay, &challenge, now_ms).await - }) - } - } - - #[tokio::test] - async fn a_failed_auth_signature_releases_the_sub_and_a_new_challenge_retries() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { - relays: vec![mock.url.clone()], - auth: Rc::new(FlakySigner { key: phone.clone(), attempts: Cell::new(0) }), - proxy: None, - }); - t.ensure_connected(); - let (_sub, seen) = recording_sub(&t, &phone); - let _req = mock.next_frame().await; - - // The relay challenges, the signer fails (no AUTH frame), and - // the relay refuses the REQ for auth: the sub must not wait on - // an answer that will never come. - mock.push(r#"["AUTH","chal-1"]"#.to_string()); - mock.push(r#"["CLOSED","cd-1","auth-required: sign in"]"#.to_string()); - wait_until(|| seen.closes.get() == 1).await; - assert_eq!(seen.eoses.get(), 0); - - // A fresh challenge on the same connection is tried again. - mock.push(r#"["AUTH","chal-2"]"#.to_string()); - let auth = mock.next_frame().await; - assert!(auth.starts_with(r#"["AUTH","#), "{auth}"); - let v: Vec = serde_json::from_str(&auth).unwrap(); - assert!(v[1]["tags"] - .as_array() - .unwrap() - .iter() - .any(|tag| tag[0] == "challenge" && tag[1] == "chal-2")); - t.shutdown(); - }) - .await; - } - - #[tokio::test] - async fn a_slow_auth_signer_still_answers_the_challenge() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { - relays: vec![mock.url.clone()], - auth: Rc::new(SlowSigner(phone.clone())), - proxy: None, - }); - t.ensure_connected(); - let _sub = t.subscribe( - a_filter(&phone), - SubCallbacks { - on_event: Rc::new(|_| {}), - on_eose: Rc::new(|| {}), - on_close: Rc::new(|_| {}), - }, - ); - let _req = mock.next_frame().await; - mock.push(r#"["AUTH","chal-slow"]"#.to_string()); - let auth = mock.next_frame().await; - let v: Vec = serde_json::from_str(&auth).unwrap(); - assert_eq!((v[0].as_str(), v[1]["pubkey"].as_str()), (Some("AUTH"), Some(phone.pubkey_hex.as_str()))); - }) - .await; - } - - #[tokio::test] - async fn publish_confirmed_maps_the_relay_ok_verdict() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - // force the dial to complete - let _sub = t.subscribe( - a_filter(&phone), - SubCallbacks { - on_event: Rc::new(|_| {}), - on_eose: Rc::new(|| {}), - on_close: Rc::new(|_| {}), - }, - ); - let _req = mock.next_frame().await; - - // Any signed event: the transport never looks inside it. - let keys = nostr::Keys::new(phone.secret_key.clone()); - let signed = nostr::EventBuilder::new(nostr::Kind::Custom(4515), "payload") - .sign_with_keys(&keys) - .unwrap(); - let event = SignedEvent::from_nostr(&signed); - - let pt = t.clone(); - let handle = tokio::task::spawn_local(async move { - pt.publish_confirmed(&event, Duration::from_secs(3), 3).await - }); - - let sent = mock.next_frame().await; - let v: Vec = serde_json::from_str(&sent).unwrap(); - assert_eq!(v[0], "EVENT"); - let id = v[1]["id"].as_str().unwrap().to_string(); - mock.push(format!(r#"["OK","{id}",false,"blocked: not allowed"]"#)); - - let result = handle.await.unwrap(); - assert_eq!(result.verdict, PublishVerdict::Rejected); - }) - .await; - } - - #[tokio::test] - async fn a_targeted_publish_skips_connected_relays_outside_its_list() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - let _sub = t.subscribe( - a_filter(&phone), - SubCallbacks { - on_event: Rc::new(|_| {}), - on_eose: Rc::new(|| {}), - on_close: Rc::new(|_| {}), - }, - ); - let _req = mock.next_frame().await; - - let keys = nostr::Keys::new(phone.secret_key.clone()); - let signed = nostr::EventBuilder::new(nostr::Kind::Custom(4515), "payload") - .sign_with_keys(&keys) - .unwrap(); - let event = SignedEvent::from_nostr(&signed); - - let elsewhere = vec!["wss://another-machine.example".to_string()]; - let result = t.publish_confirmed_to(&event, &elsewhere, Duration::from_secs(1), 1).await; - assert_eq!(result.verdict, PublishVerdict::Unreachable); - assert_eq!(mock.frame_within(Duration::from_millis(200)).await, None, "nothing sent to the connected relay"); - - let here = vec![mock.url.clone()]; - let pt = t.clone(); - let handle = tokio::task::spawn_local(async move { - pt.publish_confirmed_to(&event, &here, Duration::from_secs(3), 1).await - }); - let sent = mock.next_frame().await; - let v: Vec = serde_json::from_str(&sent).unwrap(); - assert_eq!(v[0], "EVENT"); - let id = v[1]["id"].as_str().unwrap().to_string(); - mock.push(format!(r#"["OK","{id}",true,""]"#)); - assert_eq!(handle.await.unwrap().verdict, PublishVerdict::Accepted); - }) - .await; - } - - #[tokio::test] - async fn a_socket_drop_fires_on_close_once() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - - let closes = Rc::new(RefCell::new(0u32)); - let c = Rc::clone(&closes); - let _sub = t.subscribe( - a_filter(&phone), - SubCallbacks { - on_event: Rc::new(|_| {}), - on_eose: Rc::new(|| {}), - on_close: Rc::new(move |_| *c.borrow_mut() += 1), - }, - ); - let _req = mock.next_frame().await; - - mock.close(); - wait_until(|| *closes.borrow() == 1).await; - assert_eq!(*closes.borrow(), 1); - }) - .await; - } - - #[tokio::test] - async fn dropping_a_sub_sends_close_and_silences_it() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - - let events: Rc>> = Rc::new(RefCell::new(vec![])); - let ev_c = Rc::clone(&events); - let sub = t.subscribe( - a_filter(&phone), - SubCallbacks { - on_event: Rc::new(move |ev| ev_c.borrow_mut().push(ev.clone())), - on_eose: Rc::new(|| {}), - on_close: Rc::new(|_| {}), - }, - ); - let _req = mock.next_frame().await; - - // A second, live subscription is the barrier that proves the - // late frame was handled: frames arrive over one socket in - // order, so its delivery follows the dropped one. - let live: Rc>> = Rc::new(RefCell::new(vec![])); - let live_c = Rc::clone(&live); - let _live_sub = t.subscribe( - a_filter(&phone), - SubCallbacks { - on_event: Rc::new(move |ev| live_c.borrow_mut().push(ev.clone())), - on_eose: Rc::new(|| {}), - on_close: Rc::new(|_| {}), - }, - ); - assert!(mock.next_frame().await.starts_with(r#"["REQ","cd-2","#)); - - sub.close(); - assert_eq!(mock.next_frame().await, r#"["CLOSE","cd-1"]"#); - - // A late EVENT for the dropped sub must not reach its callback, - // while the next one for the live sub still does. - mock.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "late"))); - mock.push(format!(r#"["EVENT","cd-2",{}]"#, signed_note(24515, "live"))); - wait_until(|| live.borrow().len() == 1).await; - assert!(events.borrow().is_empty()); - }) - .await; - } - - fn quick_redial(t: &WsTransport) { - t.state.borrow_mut().timing = Timing { - retry_base: Duration::from_millis(40), - retry_max: Duration::from_millis(80), - ..TIMING - }; - } - - /// Records every callback of one subscription. - #[derive(Default)] - struct Seen { - events: RefCell>, - eoses: Cell, - closes: Cell, - } - - fn recording_callbacks() -> (SubCallbacks, Rc) { - let seen = Rc::new(Seen::default()); - let (a, b, c) = (Rc::clone(&seen), Rc::clone(&seen), Rc::clone(&seen)); - let callbacks = SubCallbacks { - on_event: Rc::new(move |ev| a.events.borrow_mut().push(ev.id.clone())), - on_eose: Rc::new(move || b.eoses.set(b.eoses.get() + 1)), - on_close: Rc::new(move |_| c.closes.set(c.closes.get() + 1)), - }; - (callbacks, seen) - } - - fn recording_sub(t: &WsTransport, phone: &Keypair) -> (Box, Rc) { - let (callbacks, seen) = recording_callbacks(); - (t.subscribe(a_filter(phone), callbacks), seen) - } - - #[tokio::test] - async fn check_liveness_keeps_relays_that_answer_and_drops_silent_ones() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - // Completes the handshake, then never reads: no Pong. - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let silent = format!("ws://{}", listener.local_addr().unwrap()); - let held = tokio::spawn(async move { - let (tcp, _) = listener.accept().await.unwrap(); - let ws = tokio_tungstenite::accept_async(tcp).await.unwrap(); - tokio::time::sleep(Duration::from_secs(30)).await; - drop(ws); - }); - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { - relays: vec![mock.url.clone(), silent.clone()], - auth: Rc::new(phone.clone()), - proxy: None, - }); - t.ensure_connected(); - let (_sub, seen) = recording_sub(&t, &phone); - let _req = mock.next_frame().await; - wait_until(|| t.connected_relays().len() == 2).await; - - assert_eq!(t.check_liveness(Duration::from_millis(300)).await, 1); - wait_until(|| t.connected_relays().len() == 1).await; - assert!(t.connected_relays().contains(&mock.url)); - assert_eq!(seen.closes.get(), 0, "the answering relay still carries the sub"); - t.shutdown(); - held.abort(); - }) - .await; - } - - #[tokio::test] - async fn the_ping_interval_sets_the_silence_deadline() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - t.set_ping_interval(Duration::from_secs(150)); - let timing = t.state.borrow().timing; - assert_eq!((timing.ping_every, timing.dead_after), (Duration::from_secs(150), Duration::from_secs(315))); - assert!(t.state.borrow().pinger.is_some()); - t.shutdown(); - assert!(t.state.borrow().pinger.is_none()); - assert_eq!(DEAD_AFTER, Duration::from_secs(75)); - }) - .await; - } - - #[test] - fn the_redial_delay_grows_per_failure_up_to_the_cap() { - let delay = |attempt| TIMING.retry_delay(attempt, "wss://r.example"); - // Each step doubles until the cap, and jitter stays in a step's upper - // half, so consecutive steps keep rising while there is room to double. - let mut previous = Duration::ZERO; - for attempt in 1..=8 { - let d = delay(attempt); - assert!(d >= previous, "attempt {attempt}: {d:?} below the previous step {previous:?}"); - previous = d; - } - // Past that the cap holds, and jitter never takes a delay below the - // first step's floor — a failing relay is never hammered. - for attempt in 1..=30 { - let d = delay(attempt); - assert!(d <= RETRY_MAX, "attempt {attempt}: {d:?} past the cap"); - assert!(d >= RETRY_BASE / 2, "attempt {attempt}: {d:?} below the first step's floor"); - } - assert!(delay(30) >= RETRY_MAX / 2, "the delay settles within the cap's jitter band"); - } - - #[tokio::test] - async fn a_failed_relay_is_redialled_on_its_own_and_gets_its_reqs_back() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - quick_redial(&t); - t.ensure_connected(); - let (_sub, seen) = recording_sub(&t, &phone); - assert!(mock.next_frame().await.starts_with(r#"["REQ","cd-1""#)); - - mock.close(); - // No ensure_connected from anyone: the transport comes back - // by itself and replays the REQ on the new socket. - assert!(mock.next_frame().await.starts_with(r#"["REQ","cd-1""#)); - wait_until(|| !t.connected_relays().is_empty()).await; - assert_eq!(seen.closes.get(), 1); - }) - .await; - } - - #[tokio::test] - async fn shutdown_cancels_a_pending_redial() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.state.borrow_mut().timing = Timing { retry_base: Duration::from_millis(150), ..TIMING }; - t.ensure_connected(); - let (_sub, _seen) = recording_sub(&t, &phone); - let _req = mock.next_frame().await; - mock.close(); - wait_until(|| !t.state.borrow().retries.is_empty()).await; - - t.shutdown(); - assert!(t.state.borrow().retries.is_empty()); - assert_eq!(mock.frame_within(Duration::from_millis(400)).await, None, "no redial after shutdown"); - assert!(t.state.borrow().conns.is_empty()); - }) - .await; - } - - /// A listener that accepts TCP and never answers the WS handshake, - /// counting the dials that reach it. - async fn hanging_relay() -> (String, Rc>, tokio::task::JoinHandle<()>) { - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let url = format!("ws://{}", listener.local_addr().unwrap()); - let accepted = Rc::new(Cell::new(0)); - let count = Rc::clone(&accepted); - let held = tokio::task::spawn_local(async move { - let mut held = Vec::new(); - while let Ok((tcp, _)) = listener.accept().await { - count.set(count.get() + 1); - held.push(tcp); - } - }); - (url, accepted, held) - } - - fn generation_of(t: &WsTransport, relay: &str) -> Option { - t.state.borrow().conns.get(relay).map(|c| c.generation) - } - - #[tokio::test] - async fn retry_now_abandons_a_hanging_dial_and_restarts_the_backoff() { - LocalSet::new() - .run_until(async { - let (url, accepted, held) = hanging_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { relays: vec![url.clone()], auth: Rc::new(phone), proxy: None }); - // Not active yet: nothing to retry. - t.retry_now(); - assert!(t.state.borrow().conns.is_empty()); - - t.ensure_connected(); - wait_until(|| accepted.get() == 1).await; - let first = generation_of(&t, &url).unwrap(); - t.state.borrow_mut().retries.insert(url.clone(), Retry { attempt: 9, ..Retry::default() }); - - t.retry_now(); - wait_until(|| accepted.get() == 2).await; - assert!(generation_of(&t, &url).unwrap() > first); - assert!(t.state.borrow().retries.is_empty(), "the backoff starts over"); - held.abort(); - }) - .await; - } - - #[tokio::test] - async fn retry_now_leaves_a_live_socket_alone() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - wait_until(|| !t.connected_relays().is_empty()).await; - let live = generation_of(&t, &mock.url); - - t.retry_now(); - assert_eq!(generation_of(&t, &mock.url), live); - assert!(!t.connected_relays().is_empty()); - }) - .await; - } - - #[tokio::test] - async fn catch_up_redials_what_is_overdue_by_the_wall_clock() { - LocalSet::new() - .run_until(async { - let (url, accepted, held) = hanging_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { relays: vec![url.clone()], auth: Rc::new(phone), proxy: None }); - t.ensure_connected(); - wait_until(|| accepted.get() == 1).await; - let first = generation_of(&t, &url).unwrap(); - - // Still within its deadline: left to run. - t.catch_up(); - assert_eq!(generation_of(&t, &url), Some(first)); - - // The device slept through the deadline. - t.state.borrow_mut().conns.get_mut(&url).unwrap().dialed_at -= DIAL_TIMEOUT; - t.catch_up(); - wait_until(|| accepted.get() == 2).await; - let second = generation_of(&t, &url).unwrap(); - assert!(second > first); - - // A redial whose delay passed while asleep is dialled now; one - // still pending by the wall clock waits. - t.state.borrow_mut().conns.remove(&url); - let future = SystemTime::now() + Duration::from_secs(600); - t.state.borrow_mut().retries.insert(url.clone(), Retry { attempt: 3, timer: None, due_at: Some(future) }); - t.catch_up(); - assert!(generation_of(&t, &url).is_none()); - t.state.borrow_mut().retries.get_mut(&url).unwrap().due_at = Some(SystemTime::now() - Duration::from_secs(1)); - t.catch_up(); - wait_until(|| accepted.get() == 3).await; - assert!(generation_of(&t, &url).unwrap() > second); - assert_eq!(t.state.borrow().retries[&url].attempt, 3, "a caught-up redial keeps its backoff"); - held.abort(); - }) - .await; - } - - #[tokio::test] - async fn twenty_relays_deliver_each_event_once_and_close_only_when_all_are_gone() { - LocalSet::new() - .run_until(async { - let mut mocks = Vec::new(); - for _ in 0..20 { - mocks.push(mock_relay().await); - } - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = WsTransport::new(WsConfig { - relays: mocks.iter().map(|m| m.url.clone()).collect(), - auth: Rc::new(phone.clone()), - proxy: None, - }); - t.ensure_connected(); - let (_sub, seen) = recording_sub(&t, &phone); - for m in &mut mocks { - assert!(m.next_frame().await.starts_with(r#"["REQ","cd-1""#)); - } - assert_eq!(t.connected_relays().len(), 20); - - // Every relay has the same stored event: one delivery. - let note = signed_note(24515, "stored"); - for m in &mocks { - m.push(format!(r#"["EVENT","cd-1",{note}]"#)); - } - for (i, m) in mocks.iter().enumerate() { - assert_eq!(seen.eoses.get(), 0, "EOSE before relay {i} sent its own"); - m.push(r#"["EOSE","cd-1"]"#.to_string()); - tokio::time::sleep(Duration::from_millis(5)).await; - } - wait_until(|| seen.eoses.get() == 1).await; - assert_eq!(seen.events.borrow().len(), 1); - - // A second event, live, from a few relays: still one. Each of - // those relays also sends a distinct barrier event after it; - // once every barrier is delivered, every copy of the live - // event has been through dedup too. - let live = signed_note(24515, "live"); - let live_id = serde_json::from_str::(&live).unwrap()["id"].as_str().unwrap().to_string(); - for (i, m) in mocks.iter().enumerate().take(5) { - m.push(format!(r#"["EVENT","cd-1",{live}]"#)); - m.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, &format!("barrier {i}")))); - } - wait_until(|| seen.events.borrow().len() == 7).await; - assert_eq!( - seen.events.borrow().iter().filter(|id| **id == live_id).count(), - 1, - "the live event was delivered more than once: {:?}", - seen.events.borrow() - ); - - // Nineteen relays dropping is not a close; the last one is. - for m in &mocks[..19] { - m.close(); - } - wait_until(|| t.connected_relays().len() == 1).await; - assert_eq!(seen.closes.get(), 0); - mocks[19].close(); - wait_until(|| seen.closes.get() == 1).await; - t.shutdown(); - }) - .await; - } - - #[tokio::test] - async fn a_req_refused_for_auth_is_resent_only_after_the_auth_ok() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - let (_sub, seen) = recording_sub(&t, &phone); - let _req = mock.next_frame().await; - - mock.push(r#"["AUTH","chal"]"#.to_string()); - let auth: Vec = serde_json::from_str(&mock.next_frame().await).unwrap(); - let auth_id = auth[1]["id"].as_str().unwrap().to_string(); - mock.push(r#"["CLOSED","cd-1","auth-required: sign in first"]"#.to_string()); - assert_eq!(mock.frame_within(Duration::from_millis(200)).await, None, "the REQ waits for the AUTH OK"); - - mock.push(format!(r#"["OK","{auth_id}",true,""]"#)); - assert!(mock.next_frame().await.starts_with(r#"["REQ","cd-1""#)); - - // A second refusal after AUTH is final: no REQ/CLOSED loop. - mock.push(r#"["CLOSED","cd-1","auth-required: still no"]"#.to_string()); - assert_eq!(mock.frame_within(Duration::from_millis(200)).await, None); - assert_eq!(seen.closes.get(), 1); - }) - .await; - } - - #[tokio::test] - async fn a_publish_refused_for_auth_is_accepted_after_the_auth_ok() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - wait_until(|| !t.connected_relays().is_empty()).await; - mock.push(r#"["AUTH","chal"]"#.to_string()); - let auth: Vec = serde_json::from_str(&mock.next_frame().await).unwrap(); - let auth_id = auth[1]["id"].as_str().unwrap().to_string(); - - let keys = nostr::Keys::new(phone.secret_key.clone()); - let signed = nostr::EventBuilder::new(nostr::Kind::Custom(4515), "payload") - .sign_with_keys(&keys) - .unwrap(); - let event = SignedEvent::from_nostr(&signed); - let pt = t.clone(); - let handle = tokio::task::spawn_local(async move { - pt.publish_confirmed(&event, Duration::from_secs(3), 1).await - }); - - let first = mock.next_frame().await; - let id = serde_json::from_str::>(&first).unwrap()[1]["id"].as_str().unwrap().to_string(); - mock.push(format!(r#"["OK","{id}",false,"auth-required: who are you"]"#)); - mock.push(format!(r#"["OK","{auth_id}",true,""]"#)); - assert_eq!(mock.next_frame().await, first, "the same event, re-sent"); - mock.push(format!(r#"["OK","{id}",true,""]"#)); - assert_eq!(handle.await.unwrap().verdict, PublishVerdict::Accepted); - }) - .await; - } - - #[tokio::test] - async fn a_repeated_auth_challenge_costs_one_identity_signature() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.ensure_connected(); - wait_until(|| !t.connected_relays().is_empty()).await; - - mock.push(r#"["AUTH","chal-1"]"#.to_string()); - let first = mock.next_frame().await; - assert!(first.starts_with(r#"["AUTH","#), "{first}"); - - for challenge in ["chal-2", "chal-3"] { - mock.push(format!(r#"["AUTH","{challenge}"]"#)); - } - assert_eq!( - mock.frame_within(Duration::from_millis(300)).await, - None, - "a repeated challenge must not be signed again" - ); - t.shutdown(); - }) - .await; - } - - #[tokio::test] - async fn a_relay_that_never_answers_our_auth_releases_the_sub_at_its_deadline() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let t = transport(&mock, &phone); - t.state.borrow_mut().timing = fast_timing(); - t.ensure_connected(); - let (_sub, seen) = recording_sub(&t, &phone); - let _req = mock.next_frame().await; - - mock.push(r#"["AUTH","chal"]"#.to_string()); - let auth = mock.next_frame().await; - assert!(auth.starts_with(r#"["AUTH","#), "{auth}"); - // The relay refuses the REQ for auth and then never answers the - // AUTH it asked for: the sub must not stay parked on it. - mock.push(r#"["CLOSED","cd-1","auth-required: sign in"]"#.to_string()); - - wait_until(|| seen.closes.get() == 1).await; - assert_eq!(seen.eoses.get(), 0); - t.shutdown(); - }) - .await; - } -} +mod tests; diff --git a/crates/nostr-transport/src/ws/tests.rs b/crates/nostr-transport/src/ws/tests.rs new file mode 100644 index 00000000..91dd7a01 --- /dev/null +++ b/crates/nostr-transport/src/ws/tests.rs @@ -0,0 +1,1235 @@ +use super::*; +use crate::mock::{mock_relay, MockRelay}; +use crate::test_support::wait_until; +use protocol::crypto::{generate_keypair, keypair_from_secret_hex, Keypair}; +use nostr::JsonUtil; +use std::cell::RefCell; +use tokio::task::LocalSet; + +const SEC_PHONE: &str = + "0000000000000000000000000000000000000000000000000000000000000001"; + +fn transport(mock: &MockRelay, phone: &Keypair) -> WsTransport { + WsTransport::new(WsConfig { + relays: vec![mock.url.clone()], + auth: Rc::new(phone.clone()), + proxy: None, + }) +} + +#[tokio::test] +async fn dial_refuses_cleartext_to_a_public_host() { + let err = dial("ws://relay.example.com", None).await.unwrap_err(); + assert!(err.contains("cleartext"), "{err}"); +} + +#[tokio::test] +async fn dial_does_not_refuse_cleartext_to_onion_on_scheme_grounds() { + // A loopback stand-in for Tor: the proxy is asked to CONNECT to the + // onion name (no local resolver sees it), answers success and closes, + // so the dial fails at the WebSocket handshake — never the + // cleartext-rejection this test exists to rule out. + let (proxy, request) = crate::test_support::socks5_proxy().await; + let err = dial("ws://expyuzz4wqqyqhjn.onion:7447", Some(proxy.to_string())).await.unwrap_err(); + assert!(!err.contains("cleartext"), "{err}"); + let request = tokio::time::timeout(Duration::from_secs(5), request) + .await + .expect("the onion dial never reached the proxy") + .expect("the proxy reported its CONNECT"); + assert_eq!(request.host, "expyuzz4wqqyqhjn.onion", "the proxy must resolve the onion name, not this machine"); +} + +#[tokio::test] +async fn dial_does_not_refuse_cleartext_to_loopback() { + let mock = mock_relay().await; + // A real connect+handshake against the mock relay itself, over ws:// + // 127.0.0.1 — proves loopback is genuinely usable, not just "not + // rejected before some other failure". + dial(&mock.url, None).await.unwrap(); +} + +#[test] +fn loopback_includes_the_bracketed_ipv6_form_url_gives() { + for host in ["127.0.0.1", "::1", "[::1]", "localhost"] { + assert!(is_loopback(host), "{host}"); + } + assert!(!is_loopback("192.168.1.20")); + assert!(!is_loopback("[::2]")); +} + +#[tokio::test] +async fn dial_accepts_cleartext_to_bracketed_ipv6_loopback() { + // Nothing has to listen: the cleartext guard must pass and the + // failure must come from the connection attempt instead. + let err = dial("ws://[::1]:1", None).await.unwrap_err(); + assert!(!err.contains("cleartext"), "{err}"); +} + +/// The absolute constraint this guards: with a proxy set, a relay is +/// never dialled directly, and the proxy is asked to CONNECT to the +/// relay's *name*. Resolving the name locally would break `.onion` +/// relays (only Tor can resolve them) and would leak the relay's address +/// to the local resolver besides. +#[tokio::test] +async fn a_proxied_relay_dial_asks_the_socks_proxy_for_the_relay_name_and_never_connects_directly() { + // Stands in for the relay: it resolves from `localhost`, so a dial + // that ignored the proxy would land here and be counted. + let (relay, direct_connections) = crate::test_support::counting_listener().await; + + let (proxy, request) = crate::test_support::socks5_proxy().await; + let err = match dial(&format!("ws://localhost:{}", relay.port()), Some(proxy.to_string())).await { + Ok(_) => panic!("the proxy closes after CONNECT, so no ws handshake can complete"), + Err(err) => err, + }; + assert!(!err.starts_with("socks5:"), "the proxy answered, so the dial must not fail in SOCKS5: {err}"); + + let request = tokio::time::timeout(Duration::from_secs(5), request) + .await + .expect("the relay dial never reached the SOCKS5 proxy") + .expect("the SOCKS5 proxy reported its CONNECT"); + assert_eq!( + request.atyp, 3, + "the relay host must be sent as a domain name (the proxy does the DNS lookup, which is what reaches .onion addresses), not as a locally resolved IP" + ); + assert_eq!(request.host, "localhost", "the proxy was asked for the wrong host"); + assert_eq!(request.port, relay.port(), "the proxy was asked for the wrong port"); + assert_eq!( + direct_connections.load(std::sync::atomic::Ordering::SeqCst), + 0, + "the relay was dialled directly even though a proxy is set" + ); +} + +#[tokio::test] +async fn set_proxy_updates_what_the_next_dial_uses_and_closes_every_live_connection() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + assert_eq!(t.current_proxy(), None); + t.ensure_connected(); + + t.subscribe( + a_filter(&phone), + SubCallbacks { + on_event: Rc::new(|_| {}), + on_eose: Rc::new(|| {}), + on_close: Rc::new(|_| {}), + }, + ); + // Real handshake against the mock relay — poll rather than a + // flat sleep so this isn't a race against however long that + // takes on a loaded CI box. + for _ in 0..100 { + if !t.connected_relays().is_empty() { + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + assert!(!t.connected_relays().is_empty()); + + t.set_proxy(Some("127.0.0.1:9050".to_string())); + assert_eq!(t.current_proxy(), Some("127.0.0.1:9050".to_string())); + // The live connection (dialled before the proxy existed) is + // torn down — every existing socket was dialled under the OLD + // setting, so none of them can be trusted to still be routed + // correctly once it changes. + for _ in 0..100 { + if t.connected_relays().is_empty() { + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + assert!(t.connected_relays().is_empty()); + + t.set_proxy(None); + assert_eq!(t.current_proxy(), None); + }) + .await; +} + +/// `set_relays` only touches the diff: a relay dropped from the list is +/// closed deliberately (no `on_close`) and its socket stops holding up +/// the subscriptions it carried, so EOSE the other relays already sent +/// completes them right away. +#[tokio::test] +async fn set_relays_closes_a_removed_relay_and_stops_the_sub_waiting_on_it() { + LocalSet::new() + .run_until(async { + let mut a = mock_relay().await; + let mut b = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { + relays: vec![a.url.clone(), b.url.clone()], + auth: Rc::new(phone.clone()), + proxy: None, + }); + t.ensure_connected(); + wait_until(|| t.connected_relays().len() == 2).await; + let (_sub, seen) = recording_sub(&t, &phone); + assert!(a.next_frame().await.starts_with(r#"["REQ","cd-1""#)); + assert!(b.next_frame().await.starts_with(r#"["REQ","cd-1""#)); + + // B has stored everything it has, A has not: the subscription + // is not complete while A still carries it. The barrier event + // orders the wait behind B's EOSE. + b.push(r#"["EOSE","cd-1"]"#.to_string()); + b.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "barrier"))); + wait_until(|| !seen.events.borrow().is_empty()).await; + assert_eq!(seen.eoses.get(), 0, "A has not EOSEd, so the sub must still wait on it"); + + t.set_relays(std::slice::from_ref(&b.url)); + + assert_eq!(t.connected_relays(), BTreeSet::from([b.url.clone()]), "the removed relay must no longer be connected"); + assert_eq!(seen.eoses.get(), 1, "with A closed, the sub is complete from B alone"); + assert_eq!(seen.closes.get(), 0, "a relay removed on purpose does not close the subscription"); + t.shutdown(); + }) + .await; +} + +/// A relay added by `set_relays` is dialled and every open subscription is +/// REQ'd to it as it comes up, so it starts carrying traffic immediately. +#[tokio::test] +async fn set_relays_dials_and_subscribes_an_added_relay() { + LocalSet::new() + .run_until(async { + let mut a = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&a, &phone); + t.ensure_connected(); + let (_sub, _seen) = recording_sub(&t, &phone); + assert!(a.next_frame().await.starts_with(r#"["REQ","cd-1""#)); + + let mut c = mock_relay().await; + t.set_relays(&[a.url.clone(), c.url.clone()]); + wait_until(|| t.connected_relays().len() == 2).await; + + assert!( + c.next_frame().await.starts_with(r#"["REQ","cd-1""#), + "the added relay must be sent the open subscription's REQ" + ); + assert!(t.connected_relays().contains(&a.url), "the unchanged relay keeps its socket"); + t.shutdown(); + }) + .await; +} + +/// `subscribe_to` pins a subscription to the named relays: only they get +/// the REQ, and EOSE from one of them is enough although another relay is +/// connected and has said nothing. +#[tokio::test] +async fn subscribe_to_opens_the_subscription_only_on_the_named_relays() { + LocalSet::new() + .run_until(async { + let mut a = mock_relay().await; + let mut b = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { + relays: vec![a.url.clone(), b.url.clone()], + auth: Rc::new(phone.clone()), + proxy: None, + }); + t.ensure_connected(); + wait_until(|| t.connected_relays().len() == 2).await; + + let (callbacks, seen_b) = recording_callbacks(); + let _pinned = t.subscribe_to(std::slice::from_ref(&b.url), a_filter(&phone), callbacks); + assert!(b.next_frame().await.starts_with(r#"["REQ","cd-1""#), "the named relay must carry the sub"); + + // A second, ordinary subscription goes everywhere. A's first + // frame must be ITS REQ: had cd-1 been sent to A, that would + // have arrived first. + let (_all, _seen_a) = recording_sub(&t, &phone); + assert!( + a.next_frame().await.starts_with(r#"["REQ","cd-2""#), + "the pinned subscription must never reach an unnamed relay" + ); + assert!(b.next_frame().await.starts_with(r#"["REQ","cd-2""#)); + + // B's EOSE completes the pinned sub although A, connected and + // REQ'd for the other sub only, never answered for this one. + b.push(r#"["EOSE","cd-1"]"#.to_string()); + b.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "barrier"))); + wait_until(|| !seen_b.events.borrow().is_empty()).await; + assert_eq!(seen_b.eoses.get(), 1, "the pinned sub waits only on the relay it named"); + t.shutdown(); + }) + .await; +} + +#[tokio::test] +async fn a_deliberate_shutdown_stops_reporting_the_relay_as_connected_at_once() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + wait_until(|| !t.connected_relays().is_empty()).await; + + t.shutdown(); + // No polling: the closing task never reports its own death, + // so this must be true synchronously. + assert!(t.connected_relays().is_empty()); + }) + .await; +} + +#[tokio::test] +async fn a_superseded_dial_never_touches_its_replacement() { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + let url = mock.url.clone(); + // The live replacement (generation 7), already up. + let (tx, mut rx) = mpsc::channel::(OUTBOUND_QUEUE); + { + let mut st = t.state.borrow_mut(); + st.conns.insert( + url.clone(), + Conn { tx, stop: Rc::new(Stop::default()), heard: Rc::default(), up: true, up_since: None, generation: 7, dialed_at: SystemTime::now() }, + ); + st.router.relay_connected(&url); + } + + // The old dial (generation 3) finishing either way is ignored. + assert!(!t.on_relay_up(&url, 3)); + t.on_relay_dead(&url, 3, "dial failed: stale".into()); + assert!(t.state.borrow().conns.contains_key(&url)); + assert_eq!(t.connected_relays(), BTreeSet::from([url.clone()])); + assert!(rx.try_recv().is_err(), "nothing was replayed onto the replacement"); + + // Its own death still counts. + t.on_relay_dead(&url, 7, "socket closed".into()); + assert!(!t.state.borrow().conns.contains_key(&url)); + assert!(t.connected_relays().is_empty()); +} + +fn fast_timing() -> Timing { + Timing { + ping_every: Duration::from_millis(50), + dead_after: Duration::from_millis(300), + dial: Duration::from_millis(300), + dial_proxied: Duration::from_millis(300), + write: Duration::from_millis(300), + auth: Duration::from_millis(200), + // Long enough that no redial lands inside these tests; the + // redial tests set their own. + retry_base: Duration::from_secs(30), + retry_max: Duration::from_secs(30), + stable_after: Duration::from_secs(60), + } +} + +fn counting_sub(t: &WsTransport, phone: &Keypair) -> (Box, Rc>) { + let closes = Rc::new(RefCell::new(0u32)); + let c = Rc::clone(&closes); + let sub = t.subscribe( + a_filter(phone), + SubCallbacks { + on_event: Rc::new(|_| {}), + on_eose: Rc::new(|| {}), + on_close: Rc::new(move |_| *c.borrow_mut() += 1), + }, + ); + (sub, closes) +} + +#[tokio::test] +async fn a_dial_that_never_completes_its_handshake_times_out() { + LocalSet::new() + .run_until(async { + // Accepts TCP, never answers the WS handshake. + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}", listener.local_addr().unwrap()); + let held = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + tokio::time::sleep(Duration::from_secs(30)).await; + drop(tcp); + }); + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { relays: vec![url], auth: Rc::new(phone), proxy: None }); + t.state.borrow_mut().timing = fast_timing(); + t.ensure_connected(); + assert_eq!(t.state.borrow().conns.len(), 1); + // The failed dial frees the entry, so the FSM can redial. + wait_until(|| t.state.borrow().conns.is_empty()).await; + held.abort(); + }) + .await; +} + +#[tokio::test] +async fn a_relay_that_goes_silent_is_dropped_and_reported() { + LocalSet::new() + .run_until(async { + // Completes the handshake, then neither reads nor writes: + // our pings go unanswered. + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}", listener.local_addr().unwrap()); + let held = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let ws = tokio_tungstenite::accept_async(tcp).await.unwrap(); + tokio::time::sleep(Duration::from_secs(30)).await; + drop(ws); + }); + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { + relays: vec![url], + auth: Rc::new(phone.clone()), + proxy: None, + }); + t.state.borrow_mut().timing = fast_timing(); + t.ensure_connected(); + let (_sub, closes) = counting_sub(&t, &phone); + wait_until(|| !t.connected_relays().is_empty()).await; + + wait_until(|| t.connected_relays().is_empty()).await; + assert!(t.state.borrow().conns.is_empty()); + assert_eq!(*closes.borrow(), 1); + held.abort(); + }) + .await; +} + +#[tokio::test] +async fn a_full_outbound_queue_stops_the_connection_as_a_failure() { + let (tx, _rx) = mpsc::channel::(1); + let conn = Conn { tx, stop: Rc::new(Stop::default()), heard: Rc::default(), up: true, up_since: None, generation: 1, dialed_at: SystemTime::now() }; + conn.send("first".into()); + assert_eq!(conn.stop.reason.get(), None); + conn.send("second".into()); + assert_eq!(conn.stop.reason.get(), Some(StopReason::Overflow)); + // A teardown still wins over a pending overflow: no on_close then. + conn.close(); + assert_eq!(conn.stop.wait().await, StopReason::Deliberate); +} + +#[tokio::test] +async fn a_teardown_while_dialing_abandons_the_dial() { + LocalSet::new() + .run_until(async { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}", listener.local_addr().unwrap()); + let held = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + tokio::time::sleep(Duration::from_secs(30)).await; + drop(tcp); + }); + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { + relays: vec![url], + auth: Rc::new(phone.clone()), + proxy: None, + }); + t.ensure_connected(); + let (_sub, closes) = counting_sub(&t, &phone); + tokio::time::sleep(Duration::from_millis(50)).await; + t.shutdown(); + // The abandoned dial is gone and never reports a close. + tokio::time::sleep(Duration::from_millis(200)).await; + assert_eq!(*closes.borrow(), 0); + assert!(t.state.borrow().conns.is_empty()); + held.abort(); + }) + .await; +} + +fn a_filter(phone: &Keypair) -> Filter { + Filter { + kinds: vec![24515], + authors: vec!["a".repeat(64)], + p_tags: vec![phone.pubkey_hex.clone()], + d_tags: vec![], + c_tags: vec![], + since: None, + } +} + +fn signed_note(kind: u16, content: &str) -> String { + let k = generate_keypair(); + nostr::EventBuilder::new(nostr::Kind::Custom(kind), content) + .sign_with_keys(&nostr::Keys::new(k.secret_key.clone())) + .unwrap() + .as_json() +} + +#[tokio::test] +async fn subscribe_sends_req_then_delivers_events_and_eose() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + + let events: Rc>> = Rc::new(RefCell::new(vec![])); + let eosed = Rc::new(RefCell::new(false)); + let (ev_c, eo_c) = (Rc::clone(&events), Rc::clone(&eosed)); + let sub = t.subscribe( + a_filter(&phone), + SubCallbacks { + on_event: Rc::new(move |ev| ev_c.borrow_mut().push(ev.clone())), + on_eose: Rc::new(move || *eo_c.borrow_mut() = true), + on_close: Rc::new(|_| {}), + }, + ); + + let req = mock.next_frame().await; + assert!(req.starts_with(r#"["REQ","cd-1",{"#), "got {req}"); + assert!(req.contains("\"#p\":["), "filter carries #p: {req}"); + + mock.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "hi"))); + mock.push(r#"["EOSE","cd-1"]"#.to_string()); + wait_until(|| events.borrow().len() == 1 && *eosed.borrow()).await; + + assert_eq!(events.borrow().len(), 1); + assert_eq!(events.borrow()[0].kind, 24515); + assert!(*eosed.borrow()); + drop(sub); + }) + .await; +} + +#[tokio::test] +async fn answers_nip42_auth_with_an_identity_signed_event() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + let _sub = t.subscribe( + a_filter(&phone), + SubCallbacks { + on_event: Rc::new(|_| {}), + on_eose: Rc::new(|| {}), + on_close: Rc::new(|_| {}), + }, + ); + let _req = mock.next_frame().await; + + mock.push(r#"["AUTH","chal-42"]"#.to_string()); + let auth = mock.next_frame().await; + let v: Vec = serde_json::from_str(&auth).unwrap(); + assert_eq!(v[0], "AUTH"); + assert_eq!(v[1]["kind"], 22242); + assert_eq!(v[1]["pubkey"], phone.pubkey_hex); + assert!(v[1]["tags"] + .as_array() + .unwrap() + .iter() + .any(|tag| tag[0] == "challenge" && tag[1] == "chal-42")); + }) + .await; +} + +/// A signer that answers only after a while (an app on the phone). +struct SlowSigner(Keypair); +impl AuthSigner for SlowSigner { + fn sign_auth( + &self, + relay: &str, + challenge: &str, + now_ms: u64, + ) -> std::pin::Pin> + '_>> { + let (relay, challenge) = (relay.to_string(), challenge.to_string()); + Box::pin(async move { + tokio::time::sleep(Duration::from_millis(50)).await; + self.0.sign_auth(&relay, &challenge, now_ms).await + }) + } +} + +/// A signer whose first attempt fails (the user dismissed the prompt), +/// then signs. The failure is local: no AUTH frame is sent. +struct FlakySigner { + key: Keypair, + attempts: Cell, +} +impl AuthSigner for FlakySigner { + fn sign_auth( + &self, + relay: &str, + challenge: &str, + now_ms: u64, + ) -> std::pin::Pin> + '_>> { + let attempt = self.attempts.get(); + self.attempts.set(attempt + 1); + let (relay, challenge) = (relay.to_string(), challenge.to_string()); + let key = self.key.clone(); + Box::pin(async move { + if attempt == 0 { + return Err("the user dismissed the prompt".to_string()); + } + key.sign_auth(&relay, &challenge, now_ms).await + }) + } +} + +#[tokio::test] +async fn a_failed_auth_signature_releases_the_sub_and_a_new_challenge_retries() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { + relays: vec![mock.url.clone()], + auth: Rc::new(FlakySigner { key: phone.clone(), attempts: Cell::new(0) }), + proxy: None, + }); + t.ensure_connected(); + let (_sub, seen) = recording_sub(&t, &phone); + let _req = mock.next_frame().await; + + // The relay challenges, the signer fails (no AUTH frame), and + // the relay refuses the REQ for auth: the sub must not wait on + // an answer that will never come. + mock.push(r#"["AUTH","chal-1"]"#.to_string()); + mock.push(r#"["CLOSED","cd-1","auth-required: sign in"]"#.to_string()); + wait_until(|| seen.closes.get() == 1).await; + assert_eq!(seen.eoses.get(), 0); + + // A fresh challenge on the same connection is tried again. + mock.push(r#"["AUTH","chal-2"]"#.to_string()); + let auth = mock.next_frame().await; + assert!(auth.starts_with(r#"["AUTH","#), "{auth}"); + let v: Vec = serde_json::from_str(&auth).unwrap(); + assert!(v[1]["tags"] + .as_array() + .unwrap() + .iter() + .any(|tag| tag[0] == "challenge" && tag[1] == "chal-2")); + t.shutdown(); + }) + .await; +} + +#[tokio::test] +async fn a_slow_auth_signer_still_answers_the_challenge() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { + relays: vec![mock.url.clone()], + auth: Rc::new(SlowSigner(phone.clone())), + proxy: None, + }); + t.ensure_connected(); + let _sub = t.subscribe( + a_filter(&phone), + SubCallbacks { + on_event: Rc::new(|_| {}), + on_eose: Rc::new(|| {}), + on_close: Rc::new(|_| {}), + }, + ); + let _req = mock.next_frame().await; + mock.push(r#"["AUTH","chal-slow"]"#.to_string()); + let auth = mock.next_frame().await; + let v: Vec = serde_json::from_str(&auth).unwrap(); + assert_eq!((v[0].as_str(), v[1]["pubkey"].as_str()), (Some("AUTH"), Some(phone.pubkey_hex.as_str()))); + }) + .await; +} + +#[tokio::test] +async fn publish_confirmed_maps_the_relay_ok_verdict() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + // force the dial to complete + let _sub = t.subscribe( + a_filter(&phone), + SubCallbacks { + on_event: Rc::new(|_| {}), + on_eose: Rc::new(|| {}), + on_close: Rc::new(|_| {}), + }, + ); + let _req = mock.next_frame().await; + + // Any signed event: the transport never looks inside it. + let keys = nostr::Keys::new(phone.secret_key.clone()); + let signed = nostr::EventBuilder::new(nostr::Kind::Custom(4515), "payload") + .sign_with_keys(&keys) + .unwrap(); + let event = SignedEvent::from_nostr(&signed); + + let pt = t.clone(); + let handle = tokio::task::spawn_local(async move { + pt.publish_confirmed(&event, Duration::from_secs(3), 3).await + }); + + let sent = mock.next_frame().await; + let v: Vec = serde_json::from_str(&sent).unwrap(); + assert_eq!(v[0], "EVENT"); + let id = v[1]["id"].as_str().unwrap().to_string(); + mock.push(format!(r#"["OK","{id}",false,"blocked: not allowed"]"#)); + + let result = handle.await.unwrap(); + assert_eq!(result.verdict, PublishVerdict::Rejected); + }) + .await; +} + +#[tokio::test] +async fn a_targeted_publish_skips_connected_relays_outside_its_list() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + let _sub = t.subscribe( + a_filter(&phone), + SubCallbacks { + on_event: Rc::new(|_| {}), + on_eose: Rc::new(|| {}), + on_close: Rc::new(|_| {}), + }, + ); + let _req = mock.next_frame().await; + + let keys = nostr::Keys::new(phone.secret_key.clone()); + let signed = nostr::EventBuilder::new(nostr::Kind::Custom(4515), "payload") + .sign_with_keys(&keys) + .unwrap(); + let event = SignedEvent::from_nostr(&signed); + + let elsewhere = vec!["wss://another-machine.example".to_string()]; + let result = t.publish_confirmed_to(&event, &elsewhere, Duration::from_secs(1), 1).await; + assert_eq!(result.verdict, PublishVerdict::Unreachable); + assert_eq!(mock.frame_within(Duration::from_millis(200)).await, None, "nothing sent to the connected relay"); + + let here = vec![mock.url.clone()]; + let pt = t.clone(); + let handle = tokio::task::spawn_local(async move { + pt.publish_confirmed_to(&event, &here, Duration::from_secs(3), 1).await + }); + let sent = mock.next_frame().await; + let v: Vec = serde_json::from_str(&sent).unwrap(); + assert_eq!(v[0], "EVENT"); + let id = v[1]["id"].as_str().unwrap().to_string(); + mock.push(format!(r#"["OK","{id}",true,""]"#)); + assert_eq!(handle.await.unwrap().verdict, PublishVerdict::Accepted); + }) + .await; +} + +#[tokio::test] +async fn a_socket_drop_fires_on_close_once() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + + let closes = Rc::new(RefCell::new(0u32)); + let c = Rc::clone(&closes); + let _sub = t.subscribe( + a_filter(&phone), + SubCallbacks { + on_event: Rc::new(|_| {}), + on_eose: Rc::new(|| {}), + on_close: Rc::new(move |_| *c.borrow_mut() += 1), + }, + ); + let _req = mock.next_frame().await; + + mock.close(); + wait_until(|| *closes.borrow() == 1).await; + assert_eq!(*closes.borrow(), 1); + }) + .await; +} + +#[tokio::test] +async fn dropping_a_sub_sends_close_and_silences_it() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + + let events: Rc>> = Rc::new(RefCell::new(vec![])); + let ev_c = Rc::clone(&events); + let sub = t.subscribe( + a_filter(&phone), + SubCallbacks { + on_event: Rc::new(move |ev| ev_c.borrow_mut().push(ev.clone())), + on_eose: Rc::new(|| {}), + on_close: Rc::new(|_| {}), + }, + ); + let _req = mock.next_frame().await; + + // A second, live subscription is the barrier that proves the + // late frame was handled: frames arrive over one socket in + // order, so its delivery follows the dropped one. + let live: Rc>> = Rc::new(RefCell::new(vec![])); + let live_c = Rc::clone(&live); + let _live_sub = t.subscribe( + a_filter(&phone), + SubCallbacks { + on_event: Rc::new(move |ev| live_c.borrow_mut().push(ev.clone())), + on_eose: Rc::new(|| {}), + on_close: Rc::new(|_| {}), + }, + ); + assert!(mock.next_frame().await.starts_with(r#"["REQ","cd-2","#)); + + sub.close(); + assert_eq!(mock.next_frame().await, r#"["CLOSE","cd-1"]"#); + + // A late EVENT for the dropped sub must not reach its callback, + // while the next one for the live sub still does. + mock.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, "late"))); + mock.push(format!(r#"["EVENT","cd-2",{}]"#, signed_note(24515, "live"))); + wait_until(|| live.borrow().len() == 1).await; + assert!(events.borrow().is_empty()); + }) + .await; +} + +fn quick_redial(t: &WsTransport) { + t.state.borrow_mut().timing = Timing { + retry_base: Duration::from_millis(40), + retry_max: Duration::from_millis(80), + ..TIMING + }; +} + +/// Records every callback of one subscription. +#[derive(Default)] +struct Seen { + events: RefCell>, + eoses: Cell, + closes: Cell, +} + +fn recording_callbacks() -> (SubCallbacks, Rc) { + let seen = Rc::new(Seen::default()); + let (a, b, c) = (Rc::clone(&seen), Rc::clone(&seen), Rc::clone(&seen)); + let callbacks = SubCallbacks { + on_event: Rc::new(move |ev| a.events.borrow_mut().push(ev.id.clone())), + on_eose: Rc::new(move || b.eoses.set(b.eoses.get() + 1)), + on_close: Rc::new(move |_| c.closes.set(c.closes.get() + 1)), + }; + (callbacks, seen) +} + +fn recording_sub(t: &WsTransport, phone: &Keypair) -> (Box, Rc) { + let (callbacks, seen) = recording_callbacks(); + (t.subscribe(a_filter(phone), callbacks), seen) +} + +#[tokio::test] +async fn check_liveness_keeps_relays_that_answer_and_drops_silent_ones() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + // Completes the handshake, then never reads: no Pong. + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let silent = format!("ws://{}", listener.local_addr().unwrap()); + let held = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let ws = tokio_tungstenite::accept_async(tcp).await.unwrap(); + tokio::time::sleep(Duration::from_secs(30)).await; + drop(ws); + }); + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { + relays: vec![mock.url.clone(), silent.clone()], + auth: Rc::new(phone.clone()), + proxy: None, + }); + t.ensure_connected(); + let (_sub, seen) = recording_sub(&t, &phone); + let _req = mock.next_frame().await; + wait_until(|| t.connected_relays().len() == 2).await; + + assert_eq!(t.check_liveness(Duration::from_millis(300)).await, 1); + wait_until(|| t.connected_relays().len() == 1).await; + assert!(t.connected_relays().contains(&mock.url)); + assert_eq!(seen.closes.get(), 0, "the answering relay still carries the sub"); + t.shutdown(); + held.abort(); + }) + .await; +} + +#[tokio::test] +async fn the_ping_interval_sets_the_silence_deadline() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + t.set_ping_interval(Duration::from_secs(150)); + let timing = t.state.borrow().timing; + assert_eq!((timing.ping_every, timing.dead_after), (Duration::from_secs(150), Duration::from_secs(315))); + assert!(t.state.borrow().pinger.is_some()); + t.shutdown(); + assert!(t.state.borrow().pinger.is_none()); + assert_eq!(DEAD_AFTER, Duration::from_secs(75)); + }) + .await; +} + +#[test] +fn the_redial_delay_grows_per_failure_up_to_the_cap() { + let delay = |attempt| TIMING.retry_delay(attempt, "wss://r.example"); + // Each step doubles until the cap, and jitter stays in a step's upper + // half, so consecutive steps keep rising while there is room to double. + let mut previous = Duration::ZERO; + for attempt in 1..=8 { + let d = delay(attempt); + assert!(d >= previous, "attempt {attempt}: {d:?} below the previous step {previous:?}"); + previous = d; + } + // Past that the cap holds, and jitter never takes a delay below the + // first step's floor — a failing relay is never hammered. + for attempt in 1..=30 { + let d = delay(attempt); + assert!(d <= RETRY_MAX, "attempt {attempt}: {d:?} past the cap"); + assert!(d >= RETRY_BASE / 2, "attempt {attempt}: {d:?} below the first step's floor"); + } + assert!(delay(30) >= RETRY_MAX / 2, "the delay settles within the cap's jitter band"); +} + +#[tokio::test] +async fn a_failed_relay_is_redialled_on_its_own_and_gets_its_reqs_back() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + quick_redial(&t); + t.ensure_connected(); + let (_sub, seen) = recording_sub(&t, &phone); + assert!(mock.next_frame().await.starts_with(r#"["REQ","cd-1""#)); + + mock.close(); + // No ensure_connected from anyone: the transport comes back + // by itself and replays the REQ on the new socket. + assert!(mock.next_frame().await.starts_with(r#"["REQ","cd-1""#)); + wait_until(|| !t.connected_relays().is_empty()).await; + assert_eq!(seen.closes.get(), 1); + }) + .await; +} + +#[tokio::test] +async fn shutdown_cancels_a_pending_redial() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.state.borrow_mut().timing = Timing { retry_base: Duration::from_millis(150), ..TIMING }; + t.ensure_connected(); + let (_sub, _seen) = recording_sub(&t, &phone); + let _req = mock.next_frame().await; + mock.close(); + wait_until(|| !t.state.borrow().retries.is_empty()).await; + + t.shutdown(); + assert!(t.state.borrow().retries.is_empty()); + assert_eq!(mock.frame_within(Duration::from_millis(400)).await, None, "no redial after shutdown"); + assert!(t.state.borrow().conns.is_empty()); + }) + .await; +} + +/// A listener that accepts TCP and never answers the WS handshake, +/// counting the dials that reach it. +async fn hanging_relay() -> (String, Rc>, tokio::task::JoinHandle<()>) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}", listener.local_addr().unwrap()); + let accepted = Rc::new(Cell::new(0)); + let count = Rc::clone(&accepted); + let held = tokio::task::spawn_local(async move { + let mut held = Vec::new(); + while let Ok((tcp, _)) = listener.accept().await { + count.set(count.get() + 1); + held.push(tcp); + } + }); + (url, accepted, held) +} + +fn generation_of(t: &WsTransport, relay: &str) -> Option { + t.state.borrow().conns.get(relay).map(|c| c.generation) +} + +#[tokio::test] +async fn retry_now_abandons_a_hanging_dial_and_restarts_the_backoff() { + LocalSet::new() + .run_until(async { + let (url, accepted, held) = hanging_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { relays: vec![url.clone()], auth: Rc::new(phone), proxy: None }); + // Not active yet: nothing to retry. + t.retry_now(); + assert!(t.state.borrow().conns.is_empty()); + + t.ensure_connected(); + wait_until(|| accepted.get() == 1).await; + let first = generation_of(&t, &url).unwrap(); + t.state.borrow_mut().retries.insert(url.clone(), Retry { attempt: 9, ..Retry::default() }); + + t.retry_now(); + wait_until(|| accepted.get() == 2).await; + assert!(generation_of(&t, &url).unwrap() > first); + assert!(t.state.borrow().retries.is_empty(), "the backoff starts over"); + held.abort(); + }) + .await; +} + +#[tokio::test] +async fn retry_now_leaves_a_live_socket_alone() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + wait_until(|| !t.connected_relays().is_empty()).await; + let live = generation_of(&t, &mock.url); + + t.retry_now(); + assert_eq!(generation_of(&t, &mock.url), live); + assert!(!t.connected_relays().is_empty()); + }) + .await; +} + +#[tokio::test] +async fn catch_up_redials_what_is_overdue_by_the_wall_clock() { + LocalSet::new() + .run_until(async { + let (url, accepted, held) = hanging_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { relays: vec![url.clone()], auth: Rc::new(phone), proxy: None }); + t.ensure_connected(); + wait_until(|| accepted.get() == 1).await; + let first = generation_of(&t, &url).unwrap(); + + // Still within its deadline: left to run. + t.catch_up(); + assert_eq!(generation_of(&t, &url), Some(first)); + + // The device slept through the deadline. + t.state.borrow_mut().conns.get_mut(&url).unwrap().dialed_at -= DIAL_TIMEOUT; + t.catch_up(); + wait_until(|| accepted.get() == 2).await; + let second = generation_of(&t, &url).unwrap(); + assert!(second > first); + + // A redial whose delay passed while asleep is dialled now; one + // still pending by the wall clock waits. + t.state.borrow_mut().conns.remove(&url); + let future = SystemTime::now() + Duration::from_secs(600); + t.state.borrow_mut().retries.insert(url.clone(), Retry { attempt: 3, timer: None, due_at: Some(future) }); + t.catch_up(); + assert!(generation_of(&t, &url).is_none()); + t.state.borrow_mut().retries.get_mut(&url).unwrap().due_at = Some(SystemTime::now() - Duration::from_secs(1)); + t.catch_up(); + wait_until(|| accepted.get() == 3).await; + assert!(generation_of(&t, &url).unwrap() > second); + assert_eq!(t.state.borrow().retries[&url].attempt, 3, "a caught-up redial keeps its backoff"); + held.abort(); + }) + .await; +} + +#[tokio::test] +async fn twenty_relays_deliver_each_event_once_and_close_only_when_all_are_gone() { + LocalSet::new() + .run_until(async { + let mut mocks = Vec::new(); + for _ in 0..20 { + mocks.push(mock_relay().await); + } + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = WsTransport::new(WsConfig { + relays: mocks.iter().map(|m| m.url.clone()).collect(), + auth: Rc::new(phone.clone()), + proxy: None, + }); + t.ensure_connected(); + let (_sub, seen) = recording_sub(&t, &phone); + for m in &mut mocks { + assert!(m.next_frame().await.starts_with(r#"["REQ","cd-1""#)); + } + assert_eq!(t.connected_relays().len(), 20); + + // Every relay has the same stored event: one delivery. + let note = signed_note(24515, "stored"); + for m in &mocks { + m.push(format!(r#"["EVENT","cd-1",{note}]"#)); + } + for (i, m) in mocks.iter().enumerate() { + assert_eq!(seen.eoses.get(), 0, "EOSE before relay {i} sent its own"); + m.push(r#"["EOSE","cd-1"]"#.to_string()); + tokio::time::sleep(Duration::from_millis(5)).await; + } + wait_until(|| seen.eoses.get() == 1).await; + assert_eq!(seen.events.borrow().len(), 1); + + // A second event, live, from a few relays: still one. Each of + // those relays also sends a distinct barrier event after it; + // once every barrier is delivered, every copy of the live + // event has been through dedup too. + let live = signed_note(24515, "live"); + let live_id = serde_json::from_str::(&live).unwrap()["id"].as_str().unwrap().to_string(); + for (i, m) in mocks.iter().enumerate().take(5) { + m.push(format!(r#"["EVENT","cd-1",{live}]"#)); + m.push(format!(r#"["EVENT","cd-1",{}]"#, signed_note(24515, &format!("barrier {i}")))); + } + wait_until(|| seen.events.borrow().len() == 7).await; + assert_eq!( + seen.events.borrow().iter().filter(|id| **id == live_id).count(), + 1, + "the live event was delivered more than once: {:?}", + seen.events.borrow() + ); + + // Nineteen relays dropping is not a close; the last one is. + for m in &mocks[..19] { + m.close(); + } + wait_until(|| t.connected_relays().len() == 1).await; + assert_eq!(seen.closes.get(), 0); + mocks[19].close(); + wait_until(|| seen.closes.get() == 1).await; + t.shutdown(); + }) + .await; +} + +#[tokio::test] +async fn a_req_refused_for_auth_is_resent_only_after_the_auth_ok() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + let (_sub, seen) = recording_sub(&t, &phone); + let _req = mock.next_frame().await; + + mock.push(r#"["AUTH","chal"]"#.to_string()); + let auth: Vec = serde_json::from_str(&mock.next_frame().await).unwrap(); + let auth_id = auth[1]["id"].as_str().unwrap().to_string(); + mock.push(r#"["CLOSED","cd-1","auth-required: sign in first"]"#.to_string()); + assert_eq!(mock.frame_within(Duration::from_millis(200)).await, None, "the REQ waits for the AUTH OK"); + + mock.push(format!(r#"["OK","{auth_id}",true,""]"#)); + assert!(mock.next_frame().await.starts_with(r#"["REQ","cd-1""#)); + + // A second refusal after AUTH is final: no REQ/CLOSED loop. + mock.push(r#"["CLOSED","cd-1","auth-required: still no"]"#.to_string()); + assert_eq!(mock.frame_within(Duration::from_millis(200)).await, None); + assert_eq!(seen.closes.get(), 1); + }) + .await; +} + +#[tokio::test] +async fn a_publish_refused_for_auth_is_accepted_after_the_auth_ok() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + wait_until(|| !t.connected_relays().is_empty()).await; + mock.push(r#"["AUTH","chal"]"#.to_string()); + let auth: Vec = serde_json::from_str(&mock.next_frame().await).unwrap(); + let auth_id = auth[1]["id"].as_str().unwrap().to_string(); + + let keys = nostr::Keys::new(phone.secret_key.clone()); + let signed = nostr::EventBuilder::new(nostr::Kind::Custom(4515), "payload") + .sign_with_keys(&keys) + .unwrap(); + let event = SignedEvent::from_nostr(&signed); + let pt = t.clone(); + let handle = tokio::task::spawn_local(async move { + pt.publish_confirmed(&event, Duration::from_secs(3), 1).await + }); + + let first = mock.next_frame().await; + let id = serde_json::from_str::>(&first).unwrap()[1]["id"].as_str().unwrap().to_string(); + mock.push(format!(r#"["OK","{id}",false,"auth-required: who are you"]"#)); + mock.push(format!(r#"["OK","{auth_id}",true,""]"#)); + assert_eq!(mock.next_frame().await, first, "the same event, re-sent"); + mock.push(format!(r#"["OK","{id}",true,""]"#)); + assert_eq!(handle.await.unwrap().verdict, PublishVerdict::Accepted); + }) + .await; +} + +#[tokio::test] +async fn a_repeated_auth_challenge_costs_one_identity_signature() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.ensure_connected(); + wait_until(|| !t.connected_relays().is_empty()).await; + + mock.push(r#"["AUTH","chal-1"]"#.to_string()); + let first = mock.next_frame().await; + assert!(first.starts_with(r#"["AUTH","#), "{first}"); + + for challenge in ["chal-2", "chal-3"] { + mock.push(format!(r#"["AUTH","{challenge}"]"#)); + } + assert_eq!( + mock.frame_within(Duration::from_millis(300)).await, + None, + "a repeated challenge must not be signed again" + ); + t.shutdown(); + }) + .await; +} + +#[tokio::test] +async fn a_relay_that_never_answers_our_auth_releases_the_sub_at_its_deadline() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let t = transport(&mock, &phone); + t.state.borrow_mut().timing = fast_timing(); + t.ensure_connected(); + let (_sub, seen) = recording_sub(&t, &phone); + let _req = mock.next_frame().await; + + mock.push(r#"["AUTH","chal"]"#.to_string()); + let auth = mock.next_frame().await; + assert!(auth.starts_with(r#"["AUTH","#), "{auth}"); + // The relay refuses the REQ for auth and then never answers the + // AUTH it asked for: the sub must not stay parked on it. + mock.push(r#"["CLOSED","cd-1","auth-required: sign in"]"#.to_string()); + + wait_until(|| seen.closes.get() == 1).await; + assert_eq!(seen.eoses.get(), 0); + t.shutdown(); + }) + .await; +} From 549c23437c10d109ee07a0e226acae5ec79368b9 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 09:01:48 +0100 Subject: [PATCH 39/42] refactor(opencode): split the driver by concern Pure move, no behaviour change: driver.ts keeps OpenCodeDriver (server lifecycle, providers, catalog), and the rest moves to sibling files in the same directory. session.ts holds OpenCodeSession and its child-session, part and form types; personas.ts the agent listing and selection helpers; modes.ts the mode constants and permission rules; format.ts the pure helpers (token accounting, model ids, errors, commands, question forms). Importers now name the file each symbol lives in. Co-Authored-By: Claude Code --- .../opencode/__tests__/opencodeDriver.test.ts | 3 +- .../agent-host/src/drivers/opencode/driver.ts | 1673 +---------------- .../agent-host/src/drivers/opencode/format.ts | 242 +++ .../agent-host/src/drivers/opencode/modes.ts | 61 + .../src/drivers/opencode/personas.ts | 72 + .../src/drivers/opencode/session.ts | 1295 +++++++++++++ 6 files changed, 1689 insertions(+), 1657 deletions(-) create mode 100644 packages/agent-host/src/drivers/opencode/format.ts create mode 100644 packages/agent-host/src/drivers/opencode/modes.ts create mode 100644 packages/agent-host/src/drivers/opencode/personas.ts create mode 100644 packages/agent-host/src/drivers/opencode/session.ts diff --git a/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts b/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts index 2e3cf021..e7ffcf87 100644 --- a/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts +++ b/packages/agent-host/src/drivers/opencode/__tests__/opencodeDriver.test.ts @@ -12,7 +12,8 @@ import { tmpdir } from 'node:os'; import * as path from 'node:path'; import { afterEach, describe, it, expect, vi } from 'vitest'; import type { OpenCodeClient } from '@opencode/client'; -import { OpenCodeDriver, openCodeContext, pickDefaultModel, reasoningLevels, toFormAnswer, toQuestionSpecs } from '../driver'; +import { OpenCodeDriver } from '../driver'; +import { openCodeContext, pickDefaultModel, reasoningLevels, toFormAnswer, toQuestionSpecs } from '../format'; import type { StartSession } from '../../../sdk/types'; import { PARTIAL_INTERVAL_MS } from '../../../sdk/partial'; import { recordingContext, type Handlers } from '../../../sdk/__tests__/context'; diff --git a/packages/agent-host/src/drivers/opencode/driver.ts b/packages/agent-host/src/drivers/opencode/driver.ts index 603093da..2eced492 100644 --- a/packages/agent-host/src/drivers/opencode/driver.ts +++ b/packages/agent-host/src/drivers/opencode/driver.ts @@ -25,1660 +25,31 @@ import { existsSync } from 'node:fs'; import { homedir } from 'node:os'; import * as path from 'node:path'; -import { OpenCode, type AgentInfo as OpenCodeAgent, type ModelInfo, type OpenCodeClient, type PermissionAsked, type PermissionRule, type ProviderInfo, type SessionInfo, type V2Event } from '@opencode/client'; -import { parseSlashCommand, slashCommand } from '../../sdk/commands'; -import type { Driver, DriverSession, McpManager, PermissionRuleManager, PluginManager, SessionContext, SessionMcpState } from '../../sdk/driver'; -import { PERMISSION_ALLOW, PERMISSION_ALLOW_ALWAYS, PERMISSION_DENY, toolLocations, unknownToolReporter } from '../../sdk/tools'; -import { opencodeTool } from './tools'; -import { newTranslateContext, type TranslateContext } from '../../sdk/transcript'; -import { PartialCoalescer } from '../../sdk/partial'; -import type { - AgentInfo, - ContextBreakdown, - ContextCategory, - ModelEntry, - OptionChoice, - PermissionOption, - Persona, - ProviderBinding, - QuestionReply, - QuestionSpec, - RefusedProvider, - SessionOption, - SlashCommand, - StartSession, - Subagent, - TaskStatus, - UsageData, -} from '../../sdk/types'; -import { - opencodeEventToEntries, - QUESTION_TOOL, - SHELL_TOOL, - toolCallDiffs, - toolResultText, - type OpenCodeEvent, -} from './adapter'; -import { SESSION_COMMANDS, sessionCommand, sessionSlashCommands } from './sessionCommands'; -import { OpenCodeMcp, openCodeSessionMcp, toggleOpenCodeMcp } from './mcp'; -import { OpenCodePlugins } from './plugins'; -import { OpenCodePermissionRules } from './permissionRules'; -import { OpenCodeAccounts } from './accounts'; -import { type Catalog, type CatalogModel, catalogModel, EMPTY_CATALOG, serverModel, withModels } from './catalog'; -import { fetchModelsDev, MODELS_DEV_TTL_MS, modelsDevSource } from './modelsDev'; -import { readCatalogCache, writeCatalogCache } from './catalogCache'; -import { - NOTHING_SERVED, - type Placement, - placeProfiles, - profileModelGroup, - providersFingerprint, - type Served, - servedBy, - serverSetup, - withoutProfiles, -} from './providers'; -import { - resolveOpenCodePath, - startOpenCodeServer, - type OpenCodeServerHandle, - type StartOpenCodeServerOptions, -} from './server'; +import { OpenCode, type OpenCodeClient } from '@opencode/client'; +import type { Driver, DriverSession, McpManager, PermissionRuleManager, PluginManager, SessionContext } from '../../sdk/driver'; import type { ProviderHttp } from '../../sdk/net'; import { isValidProviderBaseUrl } from '../../sdk/provider'; -import { redactSecrets } from '../../sdk/redact'; import { checkToken, listModels, OPENAI_API } from '../../sdk/providerApi'; import type { EndpointModel } from '../../sdk/providerModels'; +import { redactSecrets } from '../../sdk/redact'; +import type { AgentInfo, ModelEntry, Persona, ProviderBinding, RefusedProvider, StartSession } from '../../sdk/types'; +import { OpenCodeAccounts } from './accounts'; +import { type Catalog, type CatalogModel, catalogModel, EMPTY_CATALOG, serverModel, withModels } from './catalog'; +import { readCatalogCache, writeCatalogCache } from './catalogCache'; +import { formatOpenCodeError, isNotFound, type OpenCodeCatalog, pickDefaultModel, reasoningLevels, splitModelId } from './format'; +import { OpenCodeMcp } from './mcp'; +import { fetchModelsDev, MODELS_DEV_TTL_MS, modelsDevSource } from './modelsDev'; +import { BUILD_AGENT, DEFAULT_MODE, OPENCODE_MODES } from './modes'; +import { OpenCodePermissionRules } from './permissionRules'; +import { listSelectablePersonas, PERSONA_LOAD_MS } from './personas'; +import { OpenCodePlugins } from './plugins'; +import { NOTHING_SERVED, type Placement, placeProfiles, profileModelGroup, providersFingerprint, type Served, servedBy, serverSetup, withoutProfiles } from './providers'; +import { resolveOpenCodePath, startOpenCodeServer, type OpenCodeServerHandle, type StartOpenCodeServerOptions } from './server'; +import { OpenCodeSession } from './session'; import { OPENCODE_STYLE } from './style'; export const OPENCODE_AGENT_ID = 'opencode'; -/** `ask` sends every permission OpenCode asks for to the phone; `default` - * (the auto-approve mode every agent shares) allows them all; `plan` runs - * OpenCode's own plan agent, which may not edit, asking like `ask`. */ -const AUTO_APPROVE_MODE = 'default'; -const DEFAULT_MODE = 'ask'; -const PLAN_MODE = 'plan'; -const OPENCODE_MODES = [ - { id: DEFAULT_MODE, label: 'Ask', description: 'Ask before each tool call' }, - { id: PLAN_MODE, label: 'Plan', description: 'Explore and plan without editing; switch to Ask or YOLO to build it' }, - { id: AUTO_APPROVE_MODE, label: 'YOLO', description: 'Run every tool without asking' }, -]; -/** The OpenCode agent the plan mode runs. In any other mode a prompt names - * none, so OpenCode uses its default agent (`build`, or the operator's). */ -const PLAN_AGENT = 'plan'; -/** The agent every other mode runs. */ -const BUILD_AGENT = 'build'; - -/** The agents OpenCode ships with. A directory's own agents load a moment - * after it is booted, so a list holding only these may still be missing - * them. */ -const BUILTIN_AGENTS = new Set(['build', 'plan', 'general', 'explore', 'compaction', 'title', 'summary']); -/** How long after a directory was booted its own agents are still loading. */ -const PERSONA_LOAD_MS = 1_000; -/** How often a list or a lookup is retried while they load. */ -const PERSONA_POLL_MS = 100; -/** How long a session waits for the persona it was asked for. */ -const PERSONA_WAIT_MS = 2_000; - -function delay(ms: number): Promise { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - -/** Whether an agent OpenCode knows is one a session can run as. `plan` is - * this driver's own Plan mode agent, and a subagent or a hidden one is not - * a configuration a session is run as. */ -export function isPersona(agent: OpenCodeAgent): boolean { - return agent.mode !== 'subagent' && !agent.hidden && agent.id !== PLAN_AGENT; -} - -/** OpenCode's agents as the phone's persona list: the selectable ones, - * `build` first, then OpenCode's own order. */ -export function toPersonas(agents: readonly OpenCodeAgent[]): Persona[] { - const selectable = agents.filter(isPersona).map((agent) => ({ - id: agent.id, - label: agent.name, - ...(agent.description ? { description: agent.description } : {}), - ...(agent.color ? { color: agent.color } : {}), - })); - const build = selectable.filter((p) => p.id === BUILD_AGENT); - return [...build, ...selectable.filter((p) => p.id !== BUILD_AGENT)]; -} - -/** - * The selectable agents OpenCode lists for `location` (its own directory - * when none is named, which is the built-ins plus the global agents). A - * just-booted directory lists its built-ins at once and its own agents a - * moment later, so while only the built-ins are there this keeps asking — - * until `waitUntil`, past which a call never waits. - */ -async function listSelectablePersonas( - client: OpenCodeClient, - location: { directory: string } | undefined, - waitUntil: number, -): Promise { - const listed = async (): Promise => { - try { - return (await client.agent.list(location ? { location } : undefined)).data; - } catch { - return []; - } - }; - let agents = await listed(); - while (Date.now() < waitUntil && agents.every((a) => BUILTIN_AGENTS.has(a.id))) { - await delay(PERSONA_POLL_MS); - agents = await listed(); - } - return toPersonas(agents); -} - -/** - * The permission rules a session runs under, from its mode and the rules of - * the agent it runs. - * - * V2's base policy ALLOWS every tool in the project: left alone, a session - * would never ask, and the phone's approval card would never appear. The ask - * modes therefore state the rule that makes OpenCode ask — the last matching - * rule wins, and `*`/`*` matches everything — while the auto-approve mode - * states the allow it already had, so a mode switch always lands on an - * explicit rule rather than on whatever the operator configured. - * - * The session's rules are matched after the agent's own, so that catch-all - * alone would also override what the agent forbids (the plan agent's - * `edit` deny, a custom agent's `shell` deny). The agent's rules therefore - * follow it again, in their order: a deny stays a deny, and every other - * effect becomes the mode's — the mode decides whether a call asks, the - * agent what it may never do. - */ -export function permissionRules(mode: string, agentRules: readonly PermissionRule[] = []): PermissionRule[] { - const effect = mode === AUTO_APPROVE_MODE ? 'allow' : 'ask'; - return [ - { action: '*', resource: '*', effect }, - ...agentRules.map((rule): PermissionRule => ({ action: rule.action, resource: rule.resource, effect: rule.effect === 'deny' ? rule.effect : effect })), - ]; -} - -/** OpenCode's "always": it saves a rule for the project that every later - * session obeys, across server restarts, until it is removed. The rule is - * the pattern OpenCode offers with the ask (`echo *` for `echo hi`), often - * wider than the call on the card, so the label names it. */ -function allowInProject(save: readonly string[]): PermissionOption { - const what = save.map((pattern) => `"${pattern}"`).join(', '); - return { ...PERMISSION_ALLOW_ALWAYS, label: `Always allow ${what} in this project` }; -} - -type SessionPermissionAsk = PermissionAsked['data']; - -/** A sub-agent's child session, as its `subagent` call described it. */ -interface ChildSession { - callId: string; - title: string; - label?: string; - /** The sub-agent was left running in the background, and has not ended. */ - background: boolean; - /** The user asked it to stop: its end is a stop, not a completion. */ - stopping: boolean; - /** The child session has ended, however it ended. A call that finishes - * after its child already went idle did not leave anything running. */ - ended: boolean; -} - -function subagentOf(child: ChildSession): Subagent { - return { ...(child.label ? { label: child.label } : {}), parentCallId: child.callId }; -} - -/** The stable id of a reply part: its message and the part's ordinal within - * it — the same identity every event of the part (started, each delta, - * ended) carries, so its `partial`s and the `text` entry that replaces them - * agree on the key. */ -function partKey(data: { assistantMessageID: string; ordinal: number }): string { - return `${data.assistantMessageID}:${data.ordinal}`; -} - -/** The tool OpenCode uses to ask the user questions. Its own call is not - * shown as a generic tool row: the form it raises renders as a question - * card instead (see handleForm). */ -const SUBAGENT_TOOL = 'subagent'; - -/** `model/providerID` split — a v2 model reference wants `{providerID, id}`, - * not a single string. A model id with no `/` (or an empty half) has no - * valid split, so nothing is named and OpenCode falls back to its own - * configured default — no fabrication. */ -function splitModelId(model: string | undefined): { providerID: string; id: string } | undefined { - if (!model) return undefined; - const i = model.indexOf('/'); - if (i <= 0 || i === model.length - 1) return undefined; - return { providerID: model.slice(0, i), id: model.slice(i + 1) }; -} - -/** OpenCode's models and the one a session defaults to. */ -export interface OpenCodeCatalog { - models: ModelEntry[]; - defaultModel?: string; - /** Context window per model id, for the models whose provider declares - * one. A provider that declares none reports 0, and OpenCode then has no - * size to compact against (its auto-compaction threshold is derived from - * the window, and is unbounded without one), so such a model has no entry - * here rather than a guessed size. */ - contextLimits?: Record; -} - -/** - * What the context holds after one assistant step: the prompt that step sent - * (its uncached input, what the provider read from its cache and what it - * wrote there) plus the reply it produced (its text and its reasoning), - * which the next step sends back as part of its prompt. - * - * These are a STEP's counters — `session.step.ended`'s. They must never be - * fed the session's own totals (`session.usage.updated`), which only ever - * grow: they count every step, tool call, title and compaction the session - * has ever paid for, so a meter built from them climbs to the window's size - * and stays pinned there while the context itself is nearly empty. - */ -export function contextTokens(tokens: SessionInfo['tokens']): number { - return tokens.input + tokens.output + tokens.reasoning + tokens.cache.read + tokens.cache.write; -} - -/** A provider retry as the phone shows it: why, which attempt, and when - * the next one goes. */ -export function retryText(status: { attempt: number; message: string; next: number }, now: number): string { - const wait = Math.max(0, Math.round((status.next - now) / 1000)); - const why = status.message.trim() || 'the provider failed'; - return `${why} — retrying (attempt ${status.attempt}${wait > 0 ? `, in ${wait} s` : ''})`; -} - -/** - * What the context window held at a session's last step, as far as OpenCode - * counts it: the prompt (the part the provider had cached, and the rest) - * and the reply (its reasoning, its text), which the next step reads as part - * of its prompt. Nothing without the window's size. - */ -export function openCodeContext(tokens: SessionInfo['tokens'], window: number | undefined): ContextBreakdown | undefined { - if (!window || window <= 0) return undefined; - const parts: Array<[string, number]> = [ - ['Cached prompt', tokens.cache.read], - ['New prompt', tokens.input + tokens.cache.write], - ['Reasoning', tokens.reasoning], - ['Reply', tokens.output], - ]; - const whole = (n: number) => Math.min(Math.max(0, Math.round(n)), 0xffff_ffff); - const used = whole(parts.reduce((sum, [, n]) => sum + n, 0)); - const categories: ContextCategory[] = parts - .filter(([, n]) => n > 0) - .map(([name, n]) => ({ name, tokens: whole(n), kind: 'used' as const })); - categories.push({ name: 'Free space', tokens: whole(window - used), kind: 'free' }); - return { usedTokens: used, windowTokens: whole(window), categories }; -} - -/** The provider OpenCode Zen serves under — the one provider every OpenCode - * install has, whose free models need no API key at all. */ -const ZEN_PROVIDER = 'opencode'; - -/** - * The model a session runs when the phone names none, as a - * `/` id from `models`: the model OpenCode's own - * default names, when it is one of them; else a free OpenCode Zen model, - * which works with no credential set up; else the first model that is not - * deprecated. Deprecated models are skipped, and a model the server does not - * offer at all is never named. - */ -export function pickDefaultModel(configured: string | undefined, models: ModelInfo[]): string | undefined { - const usable = models.filter((m) => m.status !== 'deprecated' && m.enabled); - const ids = new Set(usable.map((m) => `${m.providerID}/${m.modelID}`)); - if (configured && ids.has(configured)) return configured; - const free = usable.find((m) => m.providerID === ZEN_PROVIDER && m.cost.every((c) => c.input === 0 && c.output === 0)); - if (free) return `${free.providerID}/${free.modelID}`; - const first = usable[0]; - return first ? `${first.providerID}/${first.modelID}` : undefined; -} - -/** The variant OpenCode reads as "none": the model's own settings. */ -const DEFAULT_VARIANT = 'default'; - -/** How a variant reads in the effort picker. */ -const VARIANT_LABELS: Record = { xhigh: 'Extra high', none: 'None' }; - -/** A model's reasoning levels, from its OpenCode variants (`low`, `high`, - * `max`…, set by OpenCode from what the model is): its default first. A - * model without variants has none. */ -export function reasoningLevels(variants: ModelInfo['variants'] | undefined): OptionChoice[] { - const ids = (variants ?? []).map((v) => v.id).filter((id) => id !== DEFAULT_VARIANT); - if (ids.length === 0) return []; - const label = (id: string) => VARIANT_LABELS[id] ?? id.charAt(0).toUpperCase() + id.slice(1); - return [{ id: DEFAULT_VARIANT, label: 'Default' }, ...ids.map((id) => ({ id, label: label(id) }))]; -} - -/** The name OpenCode gives a session it has not titled yet - * ("New session - "; "Child session - …" for a sub-agent's). */ -const PLACEHOLDER_TITLE = /^(New|Child) session - \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/; - -export function isPlaceholderTitle(title: string): boolean { - return PLACEHOLDER_TITLE.test(title); -} - -export function unsupportedModelReason(model: string, models: ModelEntry[]): string | undefined { - if (models.length === 0 || models.some((m) => m.id === model)) return undefined; - return `OpenCode does not offer the model '${model}' — none of its configured providers serves it; choose one from its model list.`; -} - -/** Best-effort human-readable text out of a v2 structured error. */ -export function formatOpenCodeError(error: unknown): string { - if (error && typeof error === 'object') { - const e = error as { message?: unknown; name?: unknown; data?: { message?: unknown } }; - if (typeof e.message === 'string' && e.message !== '') return e.message; - if (typeof e.data?.message === 'string') return e.data.message; - if (typeof e.name === 'string') return e.name; - } - return 'OpenCode reported an error'; -} - -/** - * The phone-facing description of a permission ask. V2 names the RULE that - * needs approval (`external_directory`, `edit`, `shell`, …), not the tool; - * the tool itself is shown as the card's title (see fromAsk), so this says - * what the approval is actually for. - */ -export function describePermission(permission: string, patterns: string[]): string { - const what = patterns.filter((p) => p.length > 0).join(', '); - switch (permission) { - case 'external_directory': - return what ? `Access outside the project: ${what}` : 'Access outside the project'; - case 'doom_loop': - return 'The same tool call keeps repeating — let it continue?'; - case 'shell': - return what ? `Run: ${what}` : 'Run a shell command'; - case 'edit': - return what ? `Edit: ${what}` : 'Edit files'; - case 'read': - return what ? `Read: ${what}` : 'Read files'; - case 'webfetch': - return what ? `Fetch: ${what}` : 'Fetch a URL'; - case 'websearch': - return what ? `Search the web: ${what}` : 'Search the web'; - default: - return what ? `${permission}: ${what}` : permission; - } -} - -/** OpenCode's commands as the phone's command menu lists them. V2's command - * list carries no argument hints of its own, so a command shows just its - * name and description. */ -export function toSlashCommands(commands: Array<{ name: string; description?: string }>): SlashCommand[] { - return commands.map((c) => slashCommand(c.name, c.description)); -} - -/** A `question` form's fields in the wire's question shape. V2 states a - * question as a form field: a string field carrying the options (with - * `custom` allowing a typed answer), or a multiselect for a question that - * takes more than one. */ -export function toQuestionSpecs(fields: FormFieldLike[]): QuestionSpec[] { - return fields.map((f) => ({ - question: f.title ?? f.description ?? f.key, - ...(f.description && f.title ? { header: f.title } : {}), - options: (f.type === 'multiselect' || f.type === 'string' ? (f.options ?? []) : []).map((o) => ({ - label: o.label, - ...(o.description ? { description: o.description } : {}), - })), - ...(f.type === 'multiselect' ? { multiSelect: true } : {}), - })); -} - -/** The subset of a v2 form field the question card reads. */ -export interface FormFieldLike { - key: string; - type: string; - title?: string; - description?: string; - options?: Array<{ value: string; label: string; description?: string }>; -} - -/** - * The user's answers as the form's value map: v2 keys each field's answer by - * the field itself (`q0`, `q1`…, the key the question tool gave it), and - * takes a chosen entry as its value — a custom (typed) answer is sent as the - * text, since OpenCode reads any value the options do not offer that way. - */ -export function toFormAnswer( - fields: FormFieldLike[], - answers: QuestionReply[], -): Record { - const answer: Record = {}; - fields.forEach((field, i) => { - const reply = answers[i]; - if (reply === undefined) return; - if (reply.type === 'selected') { - answer[field.key] = field.type === 'multiselect' ? reply.labels : (reply.labels[0] ?? ''); - return; - } - if (reply.text !== '') answer[field.key] = field.type === 'multiselect' ? [reply.text] : reply.text; - }); - return answer; -} - -export class OpenCodeSession implements DriverSession { - private readonly cwd: string; - private readonly abortController = new AbortController(); - private readonly translate: TranslateContext; - /** Told a tool name no agent's table lists, which the log then carries. */ - private readonly reportUnknownTool: (toolName: string) => void; - private ended = false; - private mode: string; - /** The persona the session runs as. Plan mode runs OpenCode's own plan - * agent instead, and comes back to this one when it leaves. */ - private persona: string = BUILD_AGENT; - /** When the session became ready. Its directory's own agents are still - * loading for a moment after, so a persona list asked for meanwhile - * waits rather than answering with the built-ins alone. */ - private personasReadyAt?: number; - /** The last mode switch still being applied to the server; the next one - * waits for it. Never rejects. */ - private agentSwitch: Promise = Promise.resolve(); - /** The title last reported for the session. */ - private title: string | undefined; - private model?: { providerID: string; id: string }; - /** The reasoning level ("variant") prompts ask of the model; none = the - * model's default. */ - private variant: string | undefined; - - /** Tool call ids already shown as a row — v2 reports a call's input, its - * start and its end separately, and only one row may come of them. */ - private readonly shownCalls = new Set(); - /** Coalesces the reply's streamed deltas into `partial` events - * (sdk/partial): a few signed, relayed updates a second, not one per - * token. Only the main agent's reply text is streamed — see the `delta` - * case in consumeEvents. */ - private readonly partials: PartialCoalescer; - /** Ask ids (permissions and forms) already handled — an ask could in - * theory refire; a second reply is rejected by the server anyway, but this - * avoids the wasted round trip and a second card. */ - private readonly answeredAsks = new Set(); - /** Sub-agent sessions this session started (OpenCode's `subagent` tool runs - * each in a child session), by session id: the call that started it, the - * sub-agent's kind, and — for one left running in the background — that - * it is a task, and whether the user asked it to stop. */ - private readonly children = new Map(); - /** Background shell commands this session started, by the shell id the - * server gave them — the title a task row shows, the tool call that - * started it, and whether the user asked it to stop. The server names a - * background shell only on this session's own tool result; its later - * `shell.exited` carries no session at all, so the id learned here is - * what ties that exit back to this session's task. */ - private readonly shells = new Map(); - /** Every event the stream carried for this session, one line each, when - * `CODEDECK_DEBUG_EVENTS` is set: which event, for which session, and - * whether this session reads it as a sub-agent's child. A live server is - * the only place v2's real event order can be seen (the fixtures are - * written by hand), so turn and reporting bugs are diagnosed from this - * trace — see `./codedeck live opencode --env CODEDECK_DEBUG_EVENTS=1`. */ - private readonly traceEvents = Boolean(process.env.CODEDECK_DEBUG_EVENTS); - /** The providers' context limits, fetched on the first step that used - * tokens; dropped again when the fetch came back empty, so a server that - * was briefly unreachable is asked again. */ - private contextLimits?: Promise>; - private contextWindow?: number; - private contextPercentage?: number; - /** The turn was reported complete, and no new one has started since. */ - private turnCompleted = false; - /** The error last reported in this turn: OpenCode reports a failed - * turn's error both on its step and on its execution. */ - private lastError?: string; - /** The last main-agent step's own token counts: what the context held - * then. Never the session's running totals, which only grow. */ - private lastTokens?: SessionInfo['tokens']; - /** The model the last main-agent step ran, which is the window its tokens - * are measured against. */ - private lastStepModel?: { providerID: string; id: string }; - /** Names of the session's commands, from the last list fetched: a typed - * `/name` runs as that command only when OpenCode has one by that name. - * Dropped again when the fetch failed, so the next one asks again. */ - private commandNames?: Promise>; - - /** Resolves once the OpenCode session exists server-side. */ - private readonly ready: Promise<{ client: OpenCodeClient; session: SessionInfo }>; - /** Whether the server it runs on is up: until then nothing runs on it. */ - private serverUp = false; - private readonly catalog: () => Promise; - - constructor( - params: StartSession, - private readonly ctx: SessionContext, - clientPromise: Promise, - options: OpenCodeSessionOptions = {}, - ) { - this.cwd = params.cwd; - this.mode = params.mode ?? DEFAULT_MODE; - this.reportUnknownTool = unknownToolReporter((message) => this.ctx.log(message)); - this.translate = newTranslateContext(this.reportUnknownTool); - this.partials = new PartialCoalescer((update) => { - if (!this.ended) this.ctx.emit({ type: 'partial', ...update }); - }); - this.catalog = options.catalog ?? (async () => ({ models: [] })); - this.model = splitModelId(params.model ?? undefined); - // A level the model lacks is ignored by OpenCode: the session starts on - // the model's default rather than failing. - this.variant = params.effort && params.effort !== DEFAULT_VARIANT ? params.effort : undefined; - clientPromise.then( - () => (this.serverUp = true), - () => {}, - ); - this.ready = this.init(clientPromise, params); - // init() reports its own failure as `ended`; nothing else awaits this - // rejection except prompt/interrupt, which catch it themselves. - this.ready.catch(() => {}); - } - - private deliver(event: OpenCodeEvent): void { - if (this.ended) return; - const entries = opencodeEventToEntries(event, this.translate); - if (entries.length > 0) this.ctx.emit({ type: 'entries', entries }); - } - - get isEnded(): boolean { - return this.ended; - } - - /** The server this session runs on was replaced: it ends with the - * reason, which the bridge answers by resuming it. */ - serverReplaced(): void { - this.finish('The OpenCode server restarted to load changed provider profiles.'); - } - - /** The server this session runs on exited: the session ends with the - * reason, so the bridge can resume the conversation on the next one. */ - serverLost(reason: string): void { - this.finish(reason); - } - - private finish(error?: string): void { - if (this.ended) return; - this.ended = true; - this.abortController.abort(); - this.partials.reset(); - // Nothing will report these shells' ends once the stream is gone. - this.shells.clear(); - this.ctx.emit({ type: 'ended', ...(error ? { error } : {}) }); - } - - private async init( - clientPromise: Promise, - params: StartSession, - ): Promise<{ client: OpenCodeClient; session: SessionInfo }> { - try { - // With no model named, the session runs the default the phone was - // shown, set on the session — never whatever OpenCode would pick on - // its own (the last model used, possibly one with no credential). - // A resumed conversation already ran on its model; only a new start is - // checked against what the providers offer. - const catalogRead: Promise = !params.model || !params.resume ? this.catalog() : Promise.resolve({ models: [] }); - // Subscribe BEFORE resolving/creating the session so no event in the gap - // between "session exists" and "we started listening" is missed. The - // stream carries every location, so this session filters by sessionID. - // Opened while the catalog is read, so a new session waits for one - // round trip, not two; a refused model closes it again (the abort in - // `finish`). - const [catalog, client] = await Promise.all([catalogRead, clientPromise]); - const stream = client.event.subscribe({ signal: this.abortController.signal }); - if (params.model && !params.resume) { - const refused = unsupportedModelReason(params.model, catalog.models); - if (refused) throw new Error(refused); - } - // The context meter's window sizes come from the same catalog. - if (catalog.models.length > 0) this.contextLimits ??= Promise.resolve(catalog.contextLimits ?? {}); - const model = params.model ?? catalog.defaultModel; - if (!params.model) this.model = splitModelId(model); - const { session, resumeLost } = await this.resolveSession(client, params); - // Before any prompt can run: which persona the session runs as, and - // the rules of the agent that is actually running — the persona's - // denies must hold from the first tool call, not from the first mode - // switch. - await this.resolvePersona(client, session, params, resumeLost); - // OpenCode runs whatever agent the session recorded: a new one is - // created on build (or plan), and a resumed one may still name a - // persona whose file has since gone, which would fail its next turn. - const running = this.runningAgent(); - // A session that names none runs the one it was created on, or else - // OpenCode's default. - const created = !params.resume || resumeLost; - const recorded = session.agent ?? (created && this.mode === PLAN_MODE ? PLAN_AGENT : BUILD_AGENT); - if (recorded !== running) { - await client.session.switchAgent({ sessionID: session.id, agent: running }).catch(() => {}); - } - await this.statePermissions(client, session.id, running); - // The "memory was lost" notice goes ahead of the session's first line. - if (resumeLost) this.deliver({ type: 'resume-lost' }); - if (!this.ended) { - this.ctx.emit({ type: 'info', nativeSessionId: session.id, ...(model ? { model } : {}), mode: this.mode, persona: this.persona }); - this.reportTitle(session.title); - this.ctx.emit({ type: 'ready' }); - // From here the session's directory has been booted, and its own - // agents are the ones still loading. - this.personasReadyAt = Date.now(); - } - // Consume the event stream for the rest of this session's life. A clean - // end (server closed the connection) ends the session normally; a - // rejection (network drop, server restart) ends it with the error, so - // the bridge's restart policy can bring it back. - void this.consumeEvents(client, stream, session.id).then( - () => this.finish(), - (err) => this.finish(err instanceof Error ? err.message : String(err)), - ); - return { client, session }; - } catch (err) { - const message = err instanceof Error ? err.message : String(err); - this.deliver({ type: 'error', content: message }); - this.finish(message); - throw err; - } - } - - /** `params.resume` names a previously seen OpenCode session id. Verify it - * still exists server-side before trusting it — a session the server no - * longer knows about (deleted, server restarted with no persistence) falls - * through to creating a fresh one rather than failing the whole session. - * `resumeLost` says whether that fallback happened, so the user is told. */ - private async resolveSession( - client: OpenCodeClient, - params: StartSession, - ): Promise<{ session: SessionInfo; resumeLost: boolean }> { - if (params.resume) { - try { - return { session: await client.session.get({ sessionID: params.resume }), resumeLost: false }; - } catch (err) { - // Only "the server does not have this session" is a lost - // conversation. A transport failure says nothing about the session: - // falling through to a fresh one would discard the transcript for a - // server that is merely unreachable, and that server could not - // create the new session either. - if (!isNotFound(err)) { - throw new Error(`OpenCode could not look up session ${params.resume}: ${formatOpenCodeError(err)}`); - } - this.ctx.log(`[opencode] resume ${params.resume} not found server-side (${formatOpenCodeError(err)}) — starting a fresh session instead`); - } - } - return { session: await this.createSessionRemote(client), resumeLost: Boolean(params.resume) }; - } - - /** Created untitled, so OpenCode names it after its first message, on the - * model and agent the session runs from the start — v2 takes both here - * rather than on each prompt. */ - private async createSessionRemote(client: OpenCodeClient): Promise { - try { - return await client.session.create({ - location: { directory: this.cwd }, - ...(this.model ? { model: { ...this.model, ...(this.variant ? { variant: this.variant } : {}) } } : {}), - ...(this.mode === PLAN_MODE ? { agent: PLAN_AGENT } : {}), - permissions: permissionRules(this.mode), - }); - } catch (err) { - throw new Error(`OpenCode session.create failed: ${formatOpenCodeError(err)}`); - } - } - - /** The OpenCode agent the session runs right now: Plan mode runs the plan - * agent, every other mode the persona. */ - private runningAgent(): string { - return this.mode === PLAN_MODE ? PLAN_AGENT : this.persona; - } - - /** - * Which persona the session runs as, decided before it is ready so every - * prompt and every permission rule is stated for it. - * - * A new session is created on the plan agent in Plan mode and `build` - * otherwise; a persona the phone named is waited for — a directory's own - * agents load a moment after it was booted — and must be one a session can - * run as. A resumed session keeps the agent OpenCode recorded, so a - * restart resumes as the session actually ran. Only decides: the caller - * switches OpenCode's session to it. - */ - private async resolvePersona( - client: OpenCodeClient, - session: SessionInfo, - params: StartSession, - resumeLost: boolean, - ): Promise { - if (params.resume && !resumeLost && session.agent && (await this.selectableAgent(client, session.agent))) { - this.persona = session.agent; - return; - } - const requested = params.persona; - if (!requested || requested === BUILD_AGENT) { - this.persona = BUILD_AGENT; - return; - } - if (await this.waitForAgent(client, requested)) { - // Plan mode keeps its own agent; the persona applies when it leaves. - this.persona = requested; - return; - } - this.persona = BUILD_AGENT; - this.deliver({ - type: 'persona-notice', - text: `OpenCode has no agent '${requested}' here, so this session runs as Build.`, - }); - } - - /** OpenCode's record of `id` in this session's directory. `found` is false - * when the agent does not exist (yet), which a caller may wait on. */ - private async agentLookup(client: OpenCodeClient, id: string): Promise<{ found: boolean; persona?: string }> { - try { - const got = await client.agent.get({ agentID: id, location: { directory: this.cwd } }); - return { found: true, ...(isPersona(got.data) ? { persona: got.data.id } : {}) }; - } catch { - return { found: false }; - } - } - - /** The agent under `id`, when it is one a session can run as. */ - private async selectableAgent(client: OpenCodeClient, id: string): Promise { - return (await this.agentLookup(client, id)).persona; - } - - /** Wait for a just-booted directory's agents to load, then answer whether - * `id` is one a session can run as. An agent that exists but is not one - * ends the wait at once: it never becomes one. */ - private async waitForAgent(client: OpenCodeClient, id: string): Promise { - const deadline = Date.now() + PERSONA_WAIT_MS; - for (;;) { - const { found, persona } = await this.agentLookup(client, id); - if (persona) return true; - if (found || Date.now() >= deadline) return false; - await delay(PERSONA_POLL_MS); - } - } - - /** - * Apply the agent the session runs as now and state its rules, through the - * queue: the host runs a second switch while the first is still talking to - * the server, and each queued one reads the mode and persona set last, so - * the session ends on the agent and rules of the final one, never a mix. - */ - private queueAgentSwitch(client: OpenCodeClient, session: SessionInfo): Promise { - const switched = this.agentSwitch.then(async () => { - const agent = this.runningAgent(); - await client.session.switchAgent({ sessionID: session.id, agent }).catch(() => {}); - await this.statePermissions(client, session.id, agent); - }); - this.agentSwitch = switched; - return switched; - } - - /** - * A directory's agent files changed: the persona this session runs as may - * be gone, and the rules of the agent running may have changed. Anything - * for another directory is not this session's news — the directory is - * compared resolved, since the server may spell the same one differently. - */ - private async onAgentUpdated(client: OpenCodeClient, directory: string | undefined): Promise { - if (this.ended) return; - if (directory !== undefined && path.resolve(directory) !== path.resolve(this.cwd)) return; - try { - const { session } = await this.ready; - if (this.ended) return; - const removed = this.persona; - if (removed !== BUILD_AGENT && !(await this.selectableAgent(client, removed))) { - this.persona = BUILD_AGENT; - this.deliver({ - type: 'persona-notice', - text: `OpenCode's agent '${removed}' was removed, so this session runs as Build.`, - }); - if (!this.ended) this.ctx.emit({ type: 'info', persona: BUILD_AGENT }); - } - // The agent running now has its rules restated whatever changed: its - // own file may be the one that was edited. - await this.queueAgentSwitch(client, session); - } catch (err) { - // Nothing here may escape: this runs off the event stream, where a - // rejection has no one to catch it. A session that never came up has - // nothing to restate; anything else is worth the log. - this.ctx.log(`[opencode] could not restate the session after an agent change: ${formatOpenCodeError(err)}`); - } - } - - private async consumeEvents( - client: OpenCodeClient, - stream: AsyncIterable, - sessionId: string, - ): Promise { - for await (const event of stream) { - if (this.ended) break; - const data = event.data as { sessionID?: string } | undefined; - const child = data?.sessionID ? this.children.get(data.sessionID) : undefined; - if (this.traceEvents) { - const parent = (event.data as { parentID?: string } | undefined)?.parentID; - this.ctx.log( - `[opencode] ${event.type} sessionID=${String(data?.sessionID)} parentID=${String(parent)} child=${Boolean(child)} main=${sessionId}`, - ); - } - // Events for other sessions are dropped, except the one that tells this - // session a sub-agent of its own started. - if (data?.sessionID && data.sessionID !== sessionId && !child && !this.noteChild(event, sessionId)) continue; - switch (event.type) { - case 'session.text.delta': { - // Only the main agent's reply text is streamed: a sub-agent's text - // and the model's reasoning are shown collapsed (inside a tool - // group, or as a Thinking step), so a relay round trip for either - // would be traffic for what nobody sees as it arrives. - if (!child && event.data.delta) { - this.partials.delta({ key: partKey(event.data), entryType: 'text', text: event.data.delta }); - } - break; - } - case 'session.text.ended': { - const part = child ? undefined : partKey(event.data); - if (part) this.partials.complete(part); - this.deliver({ - type: 'text', - role: 'assistant', - text: event.data.text, - ...(part ? { part } : {}), - ...(child ? { subagent: subagentOf(child) } : {}), - }); - break; - } - case 'session.reasoning.ended': { - this.deliver({ type: 'reasoning', text: event.data.text, ...(child ? { subagent: subagentOf(child) } : {}) }); - break; - } - case 'session.tool.input.started': { - this.toolCalls.set(event.data.id, { name: event.data.name, input: {} }); - break; - } - case 'session.tool.called': { - const name = this.toolCalls.get(event.data.id)?.name ?? ''; - this.toolCalls.set(event.data.id, { name, input: (event.data.input ?? {}) as Record }); - // Only this session's own calls start children it pairs: a - // sub-agent's own sub-agent is announced to that sub-agent, never - // to us, so its call would wait here forever and the next child - // of ours would be paired with it. - if (name === SUBAGENT_TOOL && !child) this.noteSubagentCall(event.data.id, event.data.input); - this.deliver({ - type: 'tool-start', - callId: event.data.id, - toolName: name, - input: event.data.input, - ...(child ? { subagent: subagentOf(child) } : {}), - }); - break; - } - case 'session.tool.success': { - const diffs = toolCallDiffs(event.data.metadata as Record | undefined); - for (const diff of diffs) this.toolDiffedFiles.add(diff.path); - this.deliver({ - type: 'tool-result', - callId: event.data.id, - text: toolResultText(event.data.content, 'Done.'), - diffs, - ...(child ? { subagent: subagentOf(child) } : {}), - }); - this.finishSubagentCall(event.data.id); - // A sub-agent's own background shell belongs to that sub-agent, and - // its child session ending is what reports the task. - if (!child) this.noteBackgroundShell(event.data.id, event.data.metadata); - break; - } - case 'session.tool.failed': { - this.deliver({ - type: 'tool-result', - callId: event.data.id, - text: toolResultText(event.data.content, formatOpenCodeError(event.data.error)), - isError: true, - diffs: toolCallDiffs(event.data.metadata as Record | undefined), - ...(child ? { subagent: subagentOf(child) } : {}), - }); - this.finishSubagentCall(event.data.id); - break; - } - case 'session.step.started': { - // The step is what names the model; its end carries only the - // tokens, and the context meter needs both. - if (!child) this.lastStepModel = event.data.model; - break; - } - case 'session.step.ended': { - if (!child) { - // The step's own counters, not the session's running totals (see - // contextTokens): what the next step will carry as its prompt. - if (event.data.tokens) this.lastTokens = event.data.tokens; - this.reportStepTokens(); - } - // Files the step changed that no tool call already showed (a shell - // command's writes, say): v2 names them here rather than as a - // session-wide diff. - if (event.data.files && event.data.files.length > 0) void this.reportChangedFiles(client, sessionId, event.data.files); - break; - } - // A compaction rewrote the conversation the model sees. The phone's - // transcript still holds what the summary replaced, and its context - // meter is about to drop, so the compaction is stated rather than - // left to look like the context vanishing on its own. - case 'session.compaction.started': { - if (child) break; - // A manual compaction is the `/compact` built-in, whose own status - // line is already in the transcript; only an automatic one is news. - if (event.data.reason !== 'manual') { - this.deliver({ type: 'status', text: 'OpenCode is compacting the conversation to make room in the context.' }); - } - break; - } - case 'session.compaction.ended': { - if (child) break; - this.deliver({ - type: 'status', - text: - event.data.reason === 'manual' - ? 'Compacted the conversation: OpenCode continues from a summary of it.' - : 'Compacted the conversation to make room: OpenCode continues from a summary of it.', - }); - break; - } - case 'session.compaction.failed': { - if (child) break; - this.reportError(`OpenCode could not compact the conversation: ${formatOpenCodeError(event.data.error)}`); - break; - } - // A child's failure is the sub-agent's own: it ends that sub-agent, - // rather than surfacing on the phone as an error the user cannot - // place. The background task it was left as is what reports it. - case 'session.step.failed': { - if (child) { - this.childEnded(child, 'failed'); - break; - } - if (event.data.error) this.reportError(formatOpenCodeError(event.data.error)); - break; - } - case 'session.execution.failed': { - if (child) { - this.childEnded(child, 'failed'); - break; - } - this.reportError(formatOpenCodeError(event.data.error)); - // The turn is over however it ended: a failure that left the - // session looking like it was still working would strand the phone - // with a spinner and no way back. - this.markTurnComplete(); - break; - } - // A child's execution ending is that sub-agent's own end, exactly as - // its `session.idle` would be: only THIS session's own execution - // ending ends its turn. A child's end read as the session's own marks - // the turn complete while the parent is still working, and — because - // the child is then never recorded as ended — makes the call that - // started it a background task the phone shows as running for good. - case 'session.execution.succeeded': { - if (child) { - this.childEnded(child, 'completed'); - break; - } - this.markTurnComplete(); - break; - } - case 'session.execution.interrupted': { - if (child) { - this.childEnded(child, 'stopped'); - break; - } - this.markTurnComplete(); - break; - } - case 'session.idle': { - if (child) this.childEnded(child, 'completed'); - else this.markTurnComplete(); - break; - } - // A background shell of this session ended: the server's - // `shell.exited` names the shell and its exit, but no session (see - // the `shells` field), so it is matched by the id this session learned - // when the shell was started. A shell of another session's — or one - // already reported — is ignored. - case 'shell.exited': { - this.finishShell(event.data); - break; - } - case 'session.status': { - const status = event.data.status; - if (status.type === 'retry') { - // The provider failed and OpenCode tries again, waiting longer - // each time: said on the phone, rather than a turn that only - // seems slow until the last attempt's error. - this.deliver({ type: 'status', text: retryText(status, Date.now()) }); - break; - } - if (child) break; - if (status.type === 'busy') { - this.turnCompleted = false; - this.lastError = undefined; - } - if (!this.ended) this.ctx.emit({ type: 'turn', state: status.type === 'busy' ? 'running' : 'idle' }); - break; - } - case 'session.renamed': { - if (!child) this.reportTitle(event.data.title); - break; - } - // A directory's agent files changed: the persona this session runs - // as may be gone, and the rules of the agent running may have - // changed. Handled off the stream, since it talks to the server. - case 'agent.updated': { - void this.onAgentUpdated(client, event.location?.directory); - break; - } - case 'permission.asked': { - this.handlePermission(client, event.data, child); - break; - } - case 'form.created': { - if (event.data.form.metadata?.kind !== 'question') break; - this.handleForm(client, event.data.form, child); - break; - } - case 'session.execution.started': { - // A sub-agent asked again works again: it has not ended. - if (child) { - child.ended = false; - break; - } - // V2 starts a turn with this, not with a `busy` status: without it - // the phone would only ever learn a turn had ended. - this.turnCompleted = false; - this.lastError = undefined; - if (!this.ended) this.ctx.emit({ type: 'turn', state: 'running' }); - break; - } - default: - break; - } - } - } - - /** - * What each call is, by its id: v2 names the tool when its input starts and - * sends the input when the call is made, while the events that come after - * — a permission ask, a result — carry only the id. A permission card is - * therefore read from here, since the ask itself says no more than which - * call it is about. - */ - private readonly toolCalls = new Map }>(); - /** Files a completed edit/write/patch call already showed as a diff card. - * A file the step also names as changed is that same edit — dropped - * instead of shown twice — and consumes the entry, so a LATER change to - * the file (a shell command, say) still gets its card. */ - private readonly toolDiffedFiles = new Set(); - - /** Whether this event is this session's own child being announced; the - * child is recorded so its later events are read. OpenCode names the - * parent on the child's own `session.created`, which is what tells a - * sub-agent's session apart from an unrelated one on the same server. */ - private noteChild(event: V2Event, sessionId: string): boolean { - if (event.type !== 'session.created') return false; - const created = event.data; - if (created.parentID !== sessionId || this.children.has(created.sessionID)) return false; - // The call that started it named the sub-agent; the two events arrive - // back to back, the call first. - const call = this.pendingSubagents.shift(); - this.children.set(created.sessionID, { - callId: call?.callId ?? '', - title: call?.title ?? created.title ?? 'Sub-agent', - ...(call?.label ? { label: call.label } : {}), - background: false, - stopping: false, - ended: false, - }); - return true; - } - - /** Sub-agent calls whose child session has not been announced yet: the - * call names the sub-agent, the `session.created` that follows names the - * session it runs in, and the two are paired here. */ - private readonly pendingSubagents: Array<{ callId: string; label?: string; title: string }> = []; - - /** Remember what the `subagent` call asked for; the child session it - * starts is created just after, and claims this. */ - private noteSubagentCall(callId: string, input: Record): void { - const label = typeof input.agent === 'string' && input.agent ? input.agent : undefined; - const title = typeof input.description === 'string' && input.description ? input.description : 'Sub-agent'; - this.pendingSubagents.push({ callId, ...(label ? { label } : {}), title }); - } - - /** The child a sub-agent call started, by that call's id. */ - private childOfCall(callId: string): { id: string; child: ChildSession } | undefined { - for (const [id, child] of this.children) if (child.callId === callId) return { id, child }; - return undefined; - } - - /** A sub-agent call finished while its child session is still going: the - * sub-agent was left to run on its own, which is the background task the - * phone can stop. */ - private finishSubagentCall(callId: string): void { - const found = this.childOfCall(callId); - if (!found) return; - const { id, child } = found; - // A child whose execution already ended ran to completion inside the - // call: it was not left running, so there is no task to report. - if (child.background || child.ended) return; - child.background = true; - this.deliver({ type: 'task', taskId: id, kind: 'agent', title: child.title, status: 'running', callId: child.callId }); - } - - private childIdOf(child: ChildSession): string { - for (const [id, c] of this.children) if (c === child) return id; - return child.callId; - } - - /** A child session ended — its own execution, or a failure: a background - * sub-agent's task ended (stopped, when the user asked for that). */ - private childEnded(child: ChildSession, status: 'completed' | 'failed' | 'stopped'): void { - child.ended = true; - if (!child.background) return; - child.background = false; - this.deliver({ - type: 'task', - taskId: this.childIdOf(child), - kind: 'agent', - title: child.title, - status: child.stopping ? 'stopped' : status, - callId: child.callId, - }); - } - - /** - * A `shell` call OpenCode moved to the background, read from its success - * result. The result names the shell and reports it still `running`; a - * shell that ran to completion carries its output and its exit code - * instead, with no shell id — that difference is what tells a background - * task apart from a call that already finished inside the turn. The shell - * is a server object whose own events (`shell.created`, `shell.exited`) - * name no session, so the id learned here is the only thing that ties its - * exit back to this session's task. - */ - private noteBackgroundShell(callId: string, metadata: unknown): void { - const started = this.toolCalls.get(callId); - if (started?.name !== SHELL_TOOL) return; - const meta = metadata as { shellID?: unknown; status?: unknown } | undefined; - const shellID = typeof meta?.shellID === 'string' ? meta.shellID : ''; - if (shellID === '' || meta?.status !== 'running' || this.shells.has(shellID)) return; - const command = started.input.command; - const title = typeof command === 'string' && command !== '' ? command : 'Background command'; - this.shells.set(shellID, { title, callId, stopping: false }); - this.deliver({ type: 'task', taskId: shellID, kind: 'shell', title, status: 'running', callId }); - } - - /** - * A background shell of this session ended, out of the server's - * `shell.exited` (which names no session — see `shells`). Its exit decides - * how it went; a shell the user stopped, or one the server killed, is - * reported as stopped rather than failed. A shell this session did not - * start is ignored. - */ - private finishShell(data: { id: string; exit?: number; status: string }): void { - const shell = this.shells.get(data.id); - if (!shell) return; - this.shells.delete(data.id); - const status: TaskStatus = - shell.stopping || data.status === 'killed' - ? 'stopped' - : data.status === 'timeout' || (data.exit !== undefined && data.exit !== 0) - ? 'failed' - : 'completed'; - this.deliver({ type: 'task', taskId: data.id, kind: 'shell', title: shell.title, status, callId: shell.callId }); - } - - /** The turn is over: said once, however many of v2's end signals arrive. */ - private markTurnComplete(): void { - // A live row left over is dropped here (nothing will complete it), and no - // part timer outlives the turn. - this.partials.reset(); - if (!this.turnCompleted) this.deliver({ type: 'idle' }); - this.turnCompleted = true; - if (!this.ended) this.ctx.emit({ type: 'turn', state: 'idle' }); - } - - /** Files a step changed that no tool call showed, as diff cards: the - * server's own diff carries the patch for each. */ - private async reportChangedFiles(client: OpenCodeClient, sessionId: string, files: string[]): Promise { - const wanted = files.filter((f) => !this.consumeToolDiff(f)); - if (wanted.length === 0) return; - try { - const diff = await client.session.diff({ sessionID: sessionId }); - const byPath = new Map(diff.map((d) => [d.file, d])); - const shown = wanted.flatMap((f) => { - const found = byPath.get(f) ?? [...byPath.values()].find((d) => d.file.endsWith(`/${f}`) || f.endsWith(`/${d.file}`)); - return found ? [found] : []; - }); - if (shown.length > 0) this.deliver({ type: 'diff', files: shown }); - } catch { - // A diff the server will not give is not worth failing a turn over. - } - } - - /** Whether a tool call already showed `file`'s change (removing that - * record). Tool calls name files by absolute path, the step's list by - * worktree-relative path, so either may be a path-suffix of the other. */ - private consumeToolDiff(file: string): boolean { - const norm = (p: string) => p.replace(/\\/g, '/'); - const target = norm(file); - for (const shown of this.toolDiffedFiles) { - const s = norm(shown); - if (s === target || s.endsWith(`/${target}`) || target.endsWith(`/${s}`)) { - this.toolDiffedFiles.delete(shown); - return true; - } - } - return false; - } - - /** Report how full the context is after a step, over the window of the - * model that ran it — only what changed, and nothing for a model with no - * declared limit. */ - private reportStepTokens(): void { - const providerID = this.lastStepModel?.providerID; - const modelID = this.lastStepModel?.id; - if (!this.lastTokens || !providerID || !modelID) return; - const used = contextTokens(this.lastTokens); - if (used <= 0) return; - this.contextLimits ??= this.catalog().then((c) => { - if (c.models.length === 0) this.contextLimits = undefined; - return c.contextLimits ?? {}; - }); - void this.contextLimits.then((limits) => { - const window = limits[`${providerID}/${modelID}`]; - if (!window || this.ended) return; - const pct = Math.max(0, Math.min(100, Math.round((used / window) * 100))); - const changedPct = pct !== this.contextPercentage; - const changedCw = window !== this.contextWindow; - if (!changedPct && !changedCw) return; - this.contextPercentage = pct; - this.contextWindow = window; - this.ctx.emit({ - type: 'info', - ...(changedPct ? { contextPercentage: pct } : {}), - ...(changedCw ? { contextWindow: window } : {}), - }); - }); - } - - private reportError(content: string): void { - if (content === this.lastError) return; - this.lastError = content; - this.deliver({ type: 'error', content }); - } - - /** - * Answer one OpenCode permission ask: allowed in the auto-approve mode, - * otherwise sent to the phone. Always answered — OpenCode must never wait - * on a reply that is not coming. - */ - private handlePermission(client: OpenCodeClient, ask: SessionPermissionAsk, child?: ChildSession): void { - if (this.answeredAsks.has(ask.id)) return; - this.answeredAsks.add(ask.id); - const reply = (decision: 'once' | 'always' | 'reject'): void => { - // Best effort — the ask then stays pending server-side; there is no - // live connection left to retry over. - void client.permission.reply({ sessionID: ask.sessionID, requestID: ask.id, decision }).catch(() => {}); - }; - - if (this.mode === AUTO_APPROVE_MODE) { - reply('once'); - return; - } - const started = this.toolCalls.get(ask.source?.id ?? ''); - const toolName = started?.name ?? ask.action; - // The question tool asks for permission to ask, and then asks. Its own - // ask is the question: a card for it would make the user allow being - // asked before they are asked, with the answer they give the form the - // only one that says anything. Allowed here, so the form's card is the - // one they see. - if (toolName === QUESTION_TOOL) { - reply('once'); - return; - } - // The ask names the call, not what it will run: the arguments it was - // called with are what the card shows (`read` a file, `shell` a command). - // Its own `metadata` carries them when v2 sends any. - const input = started?.input ?? ((ask.metadata ?? {}) as Record); - const description = ask.message?.trim() || describePermission(ask.action, ask.resources); - const call = opencodeTool(toolName, input, this.reportUnknownTool); - this.ctx - .requestPermission({ - requestId: ask.source?.id ?? ask.id, - toolName, - kind: call.kind, - title: call.title, - description, - locations: toolLocations(input), - options: ask.save && ask.save.length > 0 ? [PERMISSION_ALLOW, allowInProject(ask.save), PERMISSION_DENY] : [PERMISSION_ALLOW, PERMISSION_DENY], - ...(child ? { subagent: subagentOf(child) } : {}), - }) - .then((outcome) => { - const chosen = outcome.outcome === 'selected' ? outcome.optionId : undefined; - reply(chosen === PERMISSION_ALLOW.id ? 'once' : chosen === PERMISSION_ALLOW_ALWAYS.id && ask.save?.length ? 'always' : 'reject'); - }) - .catch(() => reply('reject')); - } - - /** - * One OpenCode form raised by the `question` tool, asked on the phone. - * Keyed by the question tool's call id — the form's own metadata names it — - * so the card groups under that call and the call's result marks the card - * answered. Always answered — a dismissal cancels the form, so OpenCode - * never waits on a reply that is not coming. - */ - private handleForm(client: OpenCodeClient, form: FormInfoLike, child?: ChildSession): void { - if (this.answeredAsks.has(form.id)) return; - this.answeredAsks.add(form.id); - - const tool = (form.metadata as { tool?: { id?: string } } | undefined)?.tool; - const toolUseId = tool?.id ?? form.id; - this.deliver({ type: 'question', toolUseId }); - - const cancel = (): void => { - void client.session.form.cancel({ sessionID: form.sessionID, formID: form.id }).catch(() => {}); - }; - this.ctx - .askQuestion(toolUseId, toQuestionSpecs(form.fields)) - .then((outcome) => { - if (outcome.outcome !== 'answered') return cancel(); - return client.session.form - .reply({ sessionID: form.sessionID, formID: form.id, answer: toFormAnswer(form.fields, outcome.answers) }) - .catch(cancel); - }) - .catch(cancel); - } - - /** - * A typed `/name args` naming one of OpenCode's commands runs through - * `session.command`, which expands the command's template; sent as plain - * text it would reach the model verbatim. Anything else is a prompt. - */ - prompt(text: string): void { - const command = parseSlashCommand(text); - this.turnCompleted = false; - this.lastError = undefined; - this.ready - .then(async ({ client, session }) => { - const listed = command ? await this.knownCommands(client) : new Set(); - if (command && listed.has(command.name)) { - this.runCommand(client, session.id, command.name, command.args); - return; - } - const builtin = command && sessionCommand(command.name, listed); - if (builtin) { - this.runSessionCommand(client, session.id, builtin); - return; - } - try { - await client.session.prompt({ sessionID: session.id, text }); - } catch (err) { - this.deliver({ type: 'error', content: `OpenCode prompt failed: ${formatOpenCodeError(err)}` }); - } - }) - .catch((err) => { - this.deliver({ type: 'error', content: `OpenCode prompt failed: ${err instanceof Error ? err.message : String(err)}` }); - }); - } - - /** One of the commands OpenCode's terminal runs itself; its outcome is a - * status line, its failure an error entry. */ - private runSessionCommand(client: OpenCodeClient, sessionID: string, command: (typeof SESSION_COMMANDS)[number]): void { - command - .run({ - client, - sessionID, - ...(this.model ? { model: { ...this.model, ...(this.variant ? { variant: this.variant } : {}) } } : {}), - status: (text) => this.deliver({ type: 'status', text }), - }) - .catch((err) => this.deliver({ type: 'error', content: err instanceof Error ? err.message : String(err) })); - } - - /** The title OpenCode gave the session, once it has one of its own - * (not the timestamp a new session starts with) — each new one once. */ - private reportTitle(title: string | undefined): void { - if (!title || isPlaceholderTitle(title) || title === this.title || this.ended) return; - this.title = title; - this.ctx.emit({ type: 'info', title }); - } - - /** `session.command` answers only once the whole turn is over, so it is - * not awaited: the turn itself arrives on the event stream. A refusal - * becomes an error entry; a connection given up on while the turn runs - * (the stream still carries it) is only logged. */ - private runCommand(client: OpenCodeClient, sessionID: string, name: string, args: string): void { - client.session - .command({ sessionID, name, text: args }) - .then(() => {}) - .catch((err) => this.ctx.log(`[opencode] /${name}: ${formatOpenCodeError(err)}`)); - } - - private async fetchCommands(client: OpenCodeClient): Promise> { - try { - const listed = await client.command.list({ location: { directory: this.cwd } }); - this.commandNames = Promise.resolve(new Set(listed.data.map((c) => c.name))); - return listed.data; - } catch (err) { - throw new Error(`OpenCode could not list its commands: ${formatOpenCodeError(err)}`); - } - } - - private knownCommands(client: OpenCodeClient): Promise> { - this.commandNames ??= this.fetchCommands(client).then( - (commands) => new Set(commands.map((c) => c.name)), - () => { - this.commandNames = undefined; - return new Set(); - }, - ); - return this.commandNames; - } - - /** The server's commands, then the ones the driver runs itself. */ - async listCommands(): Promise { - const { client } = await this.ready; - const commands = await this.fetchCommands(client); - return [...toSlashCommands(commands), ...sessionSlashCommands(new Set(commands.map((c) => c.name)))]; - } - - /** The personas this session can run as: the selectable agents in its - * directory, waiting a moment for the directory's own agents to load - * after the session became ready. */ - async listPersonas(): Promise { - const { client } = await this.ready; - return listSelectablePersonas(client, { directory: this.cwd }, (this.personasReadyAt ?? Date.now()) + PERSONA_LOAD_MS); - } - - async mcpStatus(): Promise { - const { client } = await this.ready; - return openCodeSessionMcp(client, this.cwd); - } - - async toggleMcp(name: string, enabled: boolean): Promise { - const { client } = await this.ready; - return toggleOpenCodeMcp(client, this.cwd, name, enabled); - } - - async setOption(option: SessionOption, value: string): Promise { - switch (option) { - case 'mode': { - if (!OPENCODE_MODES.some((m) => m.id === value)) throw new Error(`OpenCode has no mode '${value}'`); - this.mode = value; - // V2 runs an agent and a permission ruleset for the whole session, so - // switching the mode switches both: the plan agent (or the session's - // persona), and whether a tool call reaches the phone at all. - const { client, session } = await this.ready; - await this.queueAgentSwitch(client, session); - return; - } - case 'persona': { - // The session runs as the persona outside Plan mode; in Plan mode it - // runs OpenCode's plan agent, and comes back to the persona when it - // leaves. Either way the persona is what a switch is validated - // against, so an agent that is gone is refused before it is set. - const { client, session } = await this.ready; - const found = await this.selectableAgent(client, value); - if (!found) throw new Error(`OpenCode has no agent '${value}' here`); - this.persona = found; - if (this.mode === PLAN_MODE) return; - await this.queueAgentSwitch(client, session); - return; - } - case 'model': { - // V2 keeps the model on the session; the switch applies from the next - // step, which is what a per-prompt model used to mean. - const split = splitModelId(value); - if (!split) throw new Error(`'${value}' is not an OpenCode provider/model id`); - const models = (await this.catalog()).models; - const refused = unsupportedModelReason(value, models); - if (refused) throw new Error(refused); - // A level the new model lacks would be ignored; drop it. - if (this.variant && !models.find((m) => m.id === value)?.efforts?.some((e) => e.id === this.variant)) this.variant = undefined; - this.model = split; - await this.switchModel(); - return; - } - case 'effort': { - // Levels are the model's own (its OpenCode variants). - const { models, defaultModel } = await this.catalog(); - const id = this.model ? `${this.model.providerID}/${this.model.id}` : defaultModel; - const levels = models.find((m) => m.id === id)?.efforts ?? []; - if (!levels.some((e) => e.id === value)) { - throw new Error(levels.length > 0 ? `'${value}' is not a reasoning level of ${id}` : `${id ?? 'This model'} has no reasoning levels`); - } - this.variant = value === DEFAULT_VARIANT ? undefined : value; - await this.switchModel(); - return; - } - } - } - - /** Set the session's rules for its mode and for `agent`'s own rules. An - * agent that cannot be read leaves only the mode's catch-all, which is - * logged: nothing the agent forbids would then be refused. */ - private async statePermissions(client: OpenCodeClient, sessionID: string, agent: string): Promise { - const agentRules = await Promise.resolve() - .then(() => client.agent.get({ agentID: agent, location: { directory: this.cwd } })) - .then( - (got) => got.data.permissions, - (err) => { - this.ctx.log(`[opencode] could not read agent ${agent}'s permissions: ${formatOpenCodeError(err)}`); - return []; - }, - ); - await client.session - .update({ sessionID, permissions: permissionRules(this.mode, agentRules) }) - .catch((err) => this.ctx.log(`[opencode] could not set the session's permissions: ${formatOpenCodeError(err)}`)); - } - - /** Tell the server which model (and reasoning variant) the session runs. */ - private async switchModel(): Promise { - if (!this.model) return; - const { client, session } = await this.ready; - await client.session - .switchModel({ sessionID: session.id, model: { ...this.model, ...(this.variant ? { variant: this.variant } : {}) } }) - .catch(() => {}); - } - - async interrupt(): Promise { - // The interrupted turn's remaining text is dropped rather than left to - // keep arriving; its final entries still carry whatever it produced. - this.partials.reset(); - try { - const { client, session } = await this.ready; - await client.session.interrupt({ sessionID: session.id }); - } catch { - // Best effort — an already-idle OpenCode session can legitimately - // refuse an interrupt. - } - } - - /** - * Stop a task the phone can see: a sub-agent left running in the - * background (its task id is its child session's id), or a background - * shell command (its task id is the shell's). Either may already be gone — - * the task ended between the phone's tap and this call — and a target the - * server no longer knows is the outcome that was wanted, not a failure: - * the task's own end, or the lack of one, is what reports it. - */ - async stopTask(taskId: string): Promise { - const shell = this.shells.get(taskId); - const child = shell ? undefined : this.children.get(taskId); - if (!shell && !child?.background) throw new Error('No such task is running'); - if (shell) shell.stopping = true; - else if (child) child.stopping = true; - const { client } = await this.ready; - try { - // The delete must name the session's own directory: the server scopes - // shells by location, and a delete without one drops its record - // without ending the process, which would leave the command running - // while the phone was told it stopped. - if (shell) await client.shell.remove({ id: taskId, location: { directory: this.cwd } }); - else await client.session.interrupt({ sessionID: taskId }); - } catch (err) { - if (isNotFound(err)) { - // A sub-agent's child session reports its own end, but a shell the - // server has forgotten will send no exit for this session to read: - // report it stopped here, or it would show as running for good. - if (shell) this.finishShell({ id: taskId, status: 'killed' }); - return; - } - throw new Error(`OpenCode could not stop the task: ${formatOpenCodeError(err)}`); - } - } - - /** What the session has cost so far, as OpenCode prices its models (no - * subscription windows: OpenCode has none to report). A free or - * unpriced model's session reports no cost. */ - async getUsage(): Promise { - const { client, session } = await this.ready; - try { - const info = await client.session.get({ sessionID: session.id }); - const context = this.lastTokens ? openCodeContext(this.lastTokens, this.contextWindow) : undefined; - return { - available: true, - windows: [], - ...(info.cost ? { sessionCostUsd: info.cost } : {}), - ...(context ? { context } : {}), - fetchedAt: new Date().toISOString(), - }; - } catch { - return null; - } - } - - async end(): Promise { - if (this.ended) return; - this.ended = true; - this.abortController.abort(); - this.partials.reset(); - // The session is gone from this client: no shell event will arrive for it. - this.shells.clear(); - // A server still to start (waiting for its provider profiles, or being - // installed) runs nothing of this session's, and may not start before - // the host stops waiting on this end. - if (!this.serverUp) return; - try { - const { client, session } = await this.ready; - await client.session.interrupt({ sessionID: session.id }); - } catch { - // Best effort — the session may never have been created, or may - // already be gone server-side. - } - } -} - -/** The parts of a v2 form the question card reads. */ -interface FormInfoLike { - id: string; - sessionID: string; - title: string; - metadata?: Record; - fields: FormFieldLike[]; -} - -export interface OpenCodeSessionOptions { - /** The models a session may choose from, and its default. */ - catalog?: () => Promise; -} - export interface OpenCodeDriverOptions { /** An OpenCode server to connect to. Wins over `autoStart`. */ serverUrl?: string; @@ -2297,13 +668,3 @@ export class OpenCodeDriver implements Driver { await this.server?.close(); } } - -/** Whether a client error is the server's "no such thing" — 404, or the - * declared not-found error it answers a missing session with (whose body's - * `_tag`, e.g. `ShellNotFoundError`, the client sets as the error's name). */ -function isNotFound(err: unknown): boolean { - if (!err || typeof err !== 'object') return false; - const e = err as { name?: unknown; cause?: { status?: unknown } }; - if (typeof e.cause?.status === 'number') return e.cause.status === 404; - return typeof e.name === 'string' && /NotFound/.test(e.name); -} diff --git a/packages/agent-host/src/drivers/opencode/format.ts b/packages/agent-host/src/drivers/opencode/format.ts new file mode 100644 index 00000000..c74e240e --- /dev/null +++ b/packages/agent-host/src/drivers/opencode/format.ts @@ -0,0 +1,242 @@ +/** + * The pure translations between OpenCode's data and the shapes the phone + * reads: its token counters, retries, model ids and defaults, errors, + * permission asks, commands and question forms. Nothing here talks to the + * server, so the session and the driver both use it. + */ +import type { ModelInfo, SessionInfo } from '@opencode/client'; +import { slashCommand } from '../../sdk/commands'; +import type { + ContextBreakdown, + ContextCategory, + ModelEntry, + OptionChoice, + QuestionReply, + QuestionSpec, + SlashCommand, +} from '../../sdk/types'; + +/** `model/providerID` split — a v2 model reference wants `{providerID, id}`, + * not a single string. A model id with no `/` (or an empty half) has no + * valid split, so nothing is named and OpenCode falls back to its own + * configured default — no fabrication. */ +export function splitModelId(model: string | undefined): { providerID: string; id: string } | undefined { + if (!model) return undefined; + const i = model.indexOf('/'); + if (i <= 0 || i === model.length - 1) return undefined; + return { providerID: model.slice(0, i), id: model.slice(i + 1) }; +} + +/** OpenCode's models and the one a session defaults to. */ +export interface OpenCodeCatalog { + models: ModelEntry[]; + defaultModel?: string; + /** Context window per model id, for the models whose provider declares + * one. A provider that declares none reports 0, and OpenCode then has no + * size to compact against (its auto-compaction threshold is derived from + * the window, and is unbounded without one), so such a model has no entry + * here rather than a guessed size. */ + contextLimits?: Record; +} + +/** + * What the context holds after one assistant step: the prompt that step sent + * (its uncached input, what the provider read from its cache and what it + * wrote there) plus the reply it produced (its text and its reasoning), + * which the next step sends back as part of its prompt. + * + * These are a STEP's counters — `session.step.ended`'s. They must never be + * fed the session's own totals (`session.usage.updated`), which only ever + * grow: they count every step, tool call, title and compaction the session + * has ever paid for, so a meter built from them climbs to the window's size + * and stays pinned there while the context itself is nearly empty. + */ +export function contextTokens(tokens: SessionInfo['tokens']): number { + return tokens.input + tokens.output + tokens.reasoning + tokens.cache.read + tokens.cache.write; +} + +/** A provider retry as the phone shows it: why, which attempt, and when + * the next one goes. */ +export function retryText(status: { attempt: number; message: string; next: number }, now: number): string { + const wait = Math.max(0, Math.round((status.next - now) / 1000)); + const why = status.message.trim() || 'the provider failed'; + return `${why} — retrying (attempt ${status.attempt}${wait > 0 ? `, in ${wait} s` : ''})`; +} + +/** + * What the context window held at a session's last step, as far as OpenCode + * counts it: the prompt (the part the provider had cached, and the rest) + * and the reply (its reasoning, its text), which the next step reads as part + * of its prompt. Nothing without the window's size. + */ +export function openCodeContext(tokens: SessionInfo['tokens'], window: number | undefined): ContextBreakdown | undefined { + if (!window || window <= 0) return undefined; + const parts: Array<[string, number]> = [ + ['Cached prompt', tokens.cache.read], + ['New prompt', tokens.input + tokens.cache.write], + ['Reasoning', tokens.reasoning], + ['Reply', tokens.output], + ]; + const whole = (n: number) => Math.min(Math.max(0, Math.round(n)), 0xffff_ffff); + const used = whole(parts.reduce((sum, [, n]) => sum + n, 0)); + const categories: ContextCategory[] = parts + .filter(([, n]) => n > 0) + .map(([name, n]) => ({ name, tokens: whole(n), kind: 'used' as const })); + categories.push({ name: 'Free space', tokens: whole(window - used), kind: 'free' }); + return { usedTokens: used, windowTokens: whole(window), categories }; +} + +/** The provider OpenCode Zen serves under — the one provider every OpenCode + * install has, whose free models need no API key at all. */ +const ZEN_PROVIDER = 'opencode'; + +/** + * The model a session runs when the phone names none, as a + * `/` id from `models`: the model OpenCode's own + * default names, when it is one of them; else a free OpenCode Zen model, + * which works with no credential set up; else the first model that is not + * deprecated. Deprecated models are skipped, and a model the server does not + * offer at all is never named. + */ +export function pickDefaultModel(configured: string | undefined, models: ModelInfo[]): string | undefined { + const usable = models.filter((m) => m.status !== 'deprecated' && m.enabled); + const ids = new Set(usable.map((m) => `${m.providerID}/${m.modelID}`)); + if (configured && ids.has(configured)) return configured; + const free = usable.find((m) => m.providerID === ZEN_PROVIDER && m.cost.every((c) => c.input === 0 && c.output === 0)); + if (free) return `${free.providerID}/${free.modelID}`; + const first = usable[0]; + return first ? `${first.providerID}/${first.modelID}` : undefined; +} + +/** The variant OpenCode reads as "none": the model's own settings. */ +export const DEFAULT_VARIANT = 'default'; + +/** How a variant reads in the effort picker. */ +const VARIANT_LABELS: Record = { xhigh: 'Extra high', none: 'None' }; + +/** A model's reasoning levels, from its OpenCode variants (`low`, `high`, + * `max`…, set by OpenCode from what the model is): its default first. A + * model without variants has none. */ +export function reasoningLevels(variants: ModelInfo['variants'] | undefined): OptionChoice[] { + const ids = (variants ?? []).map((v) => v.id).filter((id) => id !== DEFAULT_VARIANT); + if (ids.length === 0) return []; + const label = (id: string) => VARIANT_LABELS[id] ?? id.charAt(0).toUpperCase() + id.slice(1); + return [{ id: DEFAULT_VARIANT, label: 'Default' }, ...ids.map((id) => ({ id, label: label(id) }))]; +} + +/** The name OpenCode gives a session it has not titled yet + * ("New session - "; "Child session - …" for a sub-agent's). */ +const PLACEHOLDER_TITLE = /^(New|Child) session - \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/; + +export function isPlaceholderTitle(title: string): boolean { + return PLACEHOLDER_TITLE.test(title); +} + +export function unsupportedModelReason(model: string, models: ModelEntry[]): string | undefined { + if (models.length === 0 || models.some((m) => m.id === model)) return undefined; + return `OpenCode does not offer the model '${model}' — none of its configured providers serves it; choose one from its model list.`; +} + +/** Best-effort human-readable text out of a v2 structured error. */ +export function formatOpenCodeError(error: unknown): string { + if (error && typeof error === 'object') { + const e = error as { message?: unknown; name?: unknown; data?: { message?: unknown } }; + if (typeof e.message === 'string' && e.message !== '') return e.message; + if (typeof e.data?.message === 'string') return e.data.message; + if (typeof e.name === 'string') return e.name; + } + return 'OpenCode reported an error'; +} + +/** + * The phone-facing description of a permission ask. V2 names the RULE that + * needs approval (`external_directory`, `edit`, `shell`, …), not the tool; + * the tool itself is shown as the card's title (see fromAsk), so this says + * what the approval is actually for. + */ +export function describePermission(permission: string, patterns: string[]): string { + const what = patterns.filter((p) => p.length > 0).join(', '); + switch (permission) { + case 'external_directory': + return what ? `Access outside the project: ${what}` : 'Access outside the project'; + case 'doom_loop': + return 'The same tool call keeps repeating — let it continue?'; + case 'shell': + return what ? `Run: ${what}` : 'Run a shell command'; + case 'edit': + return what ? `Edit: ${what}` : 'Edit files'; + case 'read': + return what ? `Read: ${what}` : 'Read files'; + case 'webfetch': + return what ? `Fetch: ${what}` : 'Fetch a URL'; + case 'websearch': + return what ? `Search the web: ${what}` : 'Search the web'; + default: + return what ? `${permission}: ${what}` : permission; + } +} + +/** OpenCode's commands as the phone's command menu lists them. V2's command + * list carries no argument hints of its own, so a command shows just its + * name and description. */ +export function toSlashCommands(commands: Array<{ name: string; description?: string }>): SlashCommand[] { + return commands.map((c) => slashCommand(c.name, c.description)); +} + +/** A `question` form's fields in the wire's question shape. V2 states a + * question as a form field: a string field carrying the options (with + * `custom` allowing a typed answer), or a multiselect for a question that + * takes more than one. */ +export function toQuestionSpecs(fields: FormFieldLike[]): QuestionSpec[] { + return fields.map((f) => ({ + question: f.title ?? f.description ?? f.key, + ...(f.description && f.title ? { header: f.title } : {}), + options: (f.type === 'multiselect' || f.type === 'string' ? (f.options ?? []) : []).map((o) => ({ + label: o.label, + ...(o.description ? { description: o.description } : {}), + })), + ...(f.type === 'multiselect' ? { multiSelect: true } : {}), + })); +} + +/** The subset of a v2 form field the question card reads. */ +export interface FormFieldLike { + key: string; + type: string; + title?: string; + description?: string; + options?: Array<{ value: string; label: string; description?: string }>; +} + +/** + * The user's answers as the form's value map: v2 keys each field's answer by + * the field itself (`q0`, `q1`…, the key the question tool gave it), and + * takes a chosen entry as its value — a custom (typed) answer is sent as the + * text, since OpenCode reads any value the options do not offer that way. + */ +export function toFormAnswer( + fields: FormFieldLike[], + answers: QuestionReply[], +): Record { + const answer: Record = {}; + fields.forEach((field, i) => { + const reply = answers[i]; + if (reply === undefined) return; + if (reply.type === 'selected') { + answer[field.key] = field.type === 'multiselect' ? reply.labels : (reply.labels[0] ?? ''); + return; + } + if (reply.text !== '') answer[field.key] = field.type === 'multiselect' ? [reply.text] : reply.text; + }); + return answer; +} + +/** Whether a client error is the server's "no such thing" — 404, or the + * declared not-found error it answers a missing session with (whose body's + * `_tag`, e.g. `ShellNotFoundError`, the client sets as the error's name). */ +export function isNotFound(err: unknown): boolean { + if (!err || typeof err !== 'object') return false; + const e = err as { name?: unknown; cause?: { status?: unknown } }; + if (typeof e.cause?.status === 'number') return e.cause.status === 404; + return typeof e.name === 'string' && /NotFound/.test(e.name); +} diff --git a/packages/agent-host/src/drivers/opencode/modes.ts b/packages/agent-host/src/drivers/opencode/modes.ts new file mode 100644 index 00000000..056012b1 --- /dev/null +++ b/packages/agent-host/src/drivers/opencode/modes.ts @@ -0,0 +1,61 @@ +/** + * The OpenCode session's modes and the permission rules each one runs + * under. A mode names both the agent the session runs (the plan agent in + * Plan mode, the session's persona everywhere else) and whether a tool call + * reaches the phone. + */ +import type { PermissionRule } from '@opencode/client'; +import { PERMISSION_ALLOW_ALWAYS } from '../../sdk/tools'; +import type { PermissionOption } from '../../sdk/types'; + +/** `ask` sends every permission OpenCode asks for to the phone; `default` + * (the auto-approve mode every agent shares) allows them all; `plan` runs + * OpenCode's own plan agent, which may not edit, asking like `ask`. */ +export const AUTO_APPROVE_MODE = 'default'; +export const DEFAULT_MODE = 'ask'; +export const PLAN_MODE = 'plan'; +export const OPENCODE_MODES = [ + { id: DEFAULT_MODE, label: 'Ask', description: 'Ask before each tool call' }, + { id: PLAN_MODE, label: 'Plan', description: 'Explore and plan without editing; switch to Ask or YOLO to build it' }, + { id: AUTO_APPROVE_MODE, label: 'YOLO', description: 'Run every tool without asking' }, +]; +/** The OpenCode agent the plan mode runs. In any other mode a prompt names + * none, so OpenCode uses its default agent (`build`, or the operator's). */ +export const PLAN_AGENT = 'plan'; +/** The agent every other mode runs. */ +export const BUILD_AGENT = 'build'; + +/** + * The permission rules a session runs under, from its mode and the rules of + * the agent it runs. + * + * V2's base policy ALLOWS every tool in the project: left alone, a session + * would never ask, and the phone's approval card would never appear. The ask + * modes therefore state the rule that makes OpenCode ask — the last matching + * rule wins, and `*`/`*` matches everything — while the auto-approve mode + * states the allow it already had, so a mode switch always lands on an + * explicit rule rather than on whatever the operator configured. + * + * The session's rules are matched after the agent's own, so that catch-all + * alone would also override what the agent forbids (the plan agent's + * `edit` deny, a custom agent's `shell` deny). The agent's rules therefore + * follow it again, in their order: a deny stays a deny, and every other + * effect becomes the mode's — the mode decides whether a call asks, the + * agent what it may never do. + */ +export function permissionRules(mode: string, agentRules: readonly PermissionRule[] = []): PermissionRule[] { + const effect = mode === AUTO_APPROVE_MODE ? 'allow' : 'ask'; + return [ + { action: '*', resource: '*', effect }, + ...agentRules.map((rule): PermissionRule => ({ action: rule.action, resource: rule.resource, effect: rule.effect === 'deny' ? rule.effect : effect })), + ]; +} + +/** OpenCode's "always": it saves a rule for the project that every later + * session obeys, across server restarts, until it is removed. The rule is + * the pattern OpenCode offers with the ask (`echo *` for `echo hi`), often + * wider than the call on the card, so the label names it. */ +export function allowInProject(save: readonly string[]): PermissionOption { + const what = save.map((pattern) => `"${pattern}"`).join(', '); + return { ...PERMISSION_ALLOW_ALWAYS, label: `Always allow ${what} in this project` }; +} diff --git a/packages/agent-host/src/drivers/opencode/personas.ts b/packages/agent-host/src/drivers/opencode/personas.ts new file mode 100644 index 00000000..f3ef76e4 --- /dev/null +++ b/packages/agent-host/src/drivers/opencode/personas.ts @@ -0,0 +1,72 @@ +/** + * OpenCode's agents as the phone's personas: which of them a session can run + * as, how they are ordered, and how a call waits for a just-booted + * directory's own agents to load rather than answering with the built-ins + * alone. + */ +import type { AgentInfo as OpenCodeAgent, OpenCodeClient } from '@opencode/client'; +import type { Persona } from '../../sdk/types'; +import { BUILD_AGENT, PLAN_AGENT } from './modes'; + +/** The agents OpenCode ships with. A directory's own agents load a moment + * after it is booted, so a list holding only these may still be missing + * them. */ +const BUILTIN_AGENTS = new Set(['build', 'plan', 'general', 'explore', 'compaction', 'title', 'summary']); +/** How long after a directory was booted its own agents are still loading. */ +export const PERSONA_LOAD_MS = 1_000; +/** How often a list or a lookup is retried while they load. */ +export const PERSONA_POLL_MS = 100; +/** How long a session waits for the persona it was asked for. */ +export const PERSONA_WAIT_MS = 2_000; + +export function delay(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +/** Whether an agent OpenCode knows is one a session can run as. `plan` is + * this driver's own Plan mode agent, and a subagent or a hidden one is not + * a configuration a session is run as. */ +export function isPersona(agent: OpenCodeAgent): boolean { + return agent.mode !== 'subagent' && !agent.hidden && agent.id !== PLAN_AGENT; +} + +/** OpenCode's agents as the phone's persona list: the selectable ones, + * `build` first, then OpenCode's own order. */ +export function toPersonas(agents: readonly OpenCodeAgent[]): Persona[] { + const selectable = agents.filter(isPersona).map((agent) => ({ + id: agent.id, + label: agent.name, + ...(agent.description ? { description: agent.description } : {}), + ...(agent.color ? { color: agent.color } : {}), + })); + const build = selectable.filter((p) => p.id === BUILD_AGENT); + return [...build, ...selectable.filter((p) => p.id !== BUILD_AGENT)]; +} + +/** + * The selectable agents OpenCode lists for `location` (its own directory + * when none is named, which is the built-ins plus the global agents). A + * just-booted directory lists its built-ins at once and its own agents a + * moment later, so while only the built-ins are there this keeps asking — + * until `waitUntil`, past which a call never waits. + */ +export async function listSelectablePersonas( + client: OpenCodeClient, + location: { directory: string } | undefined, + waitUntil: number, +): Promise { + const listed = async (): Promise => { + try { + return (await client.agent.list(location ? { location } : undefined)).data; + } catch { + return []; + } + }; + let agents = await listed(); + while (Date.now() < waitUntil && agents.every((a) => BUILTIN_AGENTS.has(a.id))) { + await delay(PERSONA_POLL_MS); + agents = await listed(); + } + return toPersonas(agents); +} + diff --git a/packages/agent-host/src/drivers/opencode/session.ts b/packages/agent-host/src/drivers/opencode/session.ts new file mode 100644 index 00000000..b7b409c1 --- /dev/null +++ b/packages/agent-host/src/drivers/opencode/session.ts @@ -0,0 +1,1295 @@ +/** + * One OpenCode session, over the server client: it subscribes to the + * server's event stream, normalizes it down to stable `OpenCodeEvent`s + * (adapter.ts translates those into entries), and answers OpenCode's + * permission asks and its forms (the cards a question becomes) through the + * bridge. + */ +import * as path from 'node:path'; +import type { OpenCodeClient, PermissionAsked, SessionInfo, V2Event } from '@opencode/client'; +import { parseSlashCommand } from '../../sdk/commands'; +import type { DriverSession, SessionContext, SessionMcpState } from '../../sdk/driver'; +import { PartialCoalescer } from '../../sdk/partial'; +import { PERMISSION_ALLOW, PERMISSION_ALLOW_ALWAYS, PERMISSION_DENY, toolLocations, unknownToolReporter } from '../../sdk/tools'; +import { newTranslateContext, type TranslateContext } from '../../sdk/transcript'; +import type { Persona, SessionOption, SlashCommand, StartSession, Subagent, TaskStatus, UsageData } from '../../sdk/types'; +import { opencodeEventToEntries, QUESTION_TOOL, SHELL_TOOL, toolCallDiffs, toolResultText, type OpenCodeEvent } from './adapter'; +import { + contextTokens, + DEFAULT_VARIANT, + describePermission, + formatOpenCodeError, + isNotFound, + isPlaceholderTitle, + openCodeContext, + retryText, + splitModelId, + toFormAnswer, + toQuestionSpecs, + toSlashCommands, + unsupportedModelReason, + type FormFieldLike, + type OpenCodeCatalog, +} from './format'; +import { openCodeSessionMcp, toggleOpenCodeMcp } from './mcp'; +import { allowInProject, AUTO_APPROVE_MODE, BUILD_AGENT, DEFAULT_MODE, OPENCODE_MODES, permissionRules, PLAN_AGENT, PLAN_MODE } from './modes'; +import { delay, isPersona, listSelectablePersonas, PERSONA_LOAD_MS, PERSONA_POLL_MS, PERSONA_WAIT_MS } from './personas'; +import { SESSION_COMMANDS, sessionCommand, sessionSlashCommands } from './sessionCommands'; +import { opencodeTool } from './tools'; + +type SessionPermissionAsk = PermissionAsked['data']; + +/** A sub-agent's child session, as its `subagent` call described it. */ +interface ChildSession { + callId: string; + title: string; + label?: string; + /** The sub-agent was left running in the background, and has not ended. */ + background: boolean; + /** The user asked it to stop: its end is a stop, not a completion. */ + stopping: boolean; + /** The child session has ended, however it ended. A call that finishes + * after its child already went idle did not leave anything running. */ + ended: boolean; +} + +function subagentOf(child: ChildSession): Subagent { + return { ...(child.label ? { label: child.label } : {}), parentCallId: child.callId }; +} + +/** The stable id of a reply part: its message and the part's ordinal within + * it — the same identity every event of the part (started, each delta, + * ended) carries, so its `partial`s and the `text` entry that replaces them + * agree on the key. */ +function partKey(data: { assistantMessageID: string; ordinal: number }): string { + return `${data.assistantMessageID}:${data.ordinal}`; +} + +/** The tool OpenCode uses to ask the user questions. Its own call is not + * shown as a generic tool row: the form it raises renders as a question + * card instead (see handleForm). */ +const SUBAGENT_TOOL = 'subagent'; + +export class OpenCodeSession implements DriverSession { + private readonly cwd: string; + private readonly abortController = new AbortController(); + private readonly translate: TranslateContext; + /** Told a tool name no agent's table lists, which the log then carries. */ + private readonly reportUnknownTool: (toolName: string) => void; + private ended = false; + private mode: string; + /** The persona the session runs as. Plan mode runs OpenCode's own plan + * agent instead, and comes back to this one when it leaves. */ + private persona: string = BUILD_AGENT; + /** When the session became ready. Its directory's own agents are still + * loading for a moment after, so a persona list asked for meanwhile + * waits rather than answering with the built-ins alone. */ + private personasReadyAt?: number; + /** The last mode switch still being applied to the server; the next one + * waits for it. Never rejects. */ + private agentSwitch: Promise = Promise.resolve(); + /** The title last reported for the session. */ + private title: string | undefined; + private model?: { providerID: string; id: string }; + /** The reasoning level ("variant") prompts ask of the model; none = the + * model's default. */ + private variant: string | undefined; + + /** Tool call ids already shown as a row — v2 reports a call's input, its + * start and its end separately, and only one row may come of them. */ + private readonly shownCalls = new Set(); + /** Coalesces the reply's streamed deltas into `partial` events + * (sdk/partial): a few signed, relayed updates a second, not one per + * token. Only the main agent's reply text is streamed — see the `delta` + * case in consumeEvents. */ + private readonly partials: PartialCoalescer; + /** Ask ids (permissions and forms) already handled — an ask could in + * theory refire; a second reply is rejected by the server anyway, but this + * avoids the wasted round trip and a second card. */ + private readonly answeredAsks = new Set(); + /** Sub-agent sessions this session started (OpenCode's `subagent` tool runs + * each in a child session), by session id: the call that started it, the + * sub-agent's kind, and — for one left running in the background — that + * it is a task, and whether the user asked it to stop. */ + private readonly children = new Map(); + /** Background shell commands this session started, by the shell id the + * server gave them — the title a task row shows, the tool call that + * started it, and whether the user asked it to stop. The server names a + * background shell only on this session's own tool result; its later + * `shell.exited` carries no session at all, so the id learned here is + * what ties that exit back to this session's task. */ + private readonly shells = new Map(); + /** Every event the stream carried for this session, one line each, when + * `CODEDECK_DEBUG_EVENTS` is set: which event, for which session, and + * whether this session reads it as a sub-agent's child. A live server is + * the only place v2's real event order can be seen (the fixtures are + * written by hand), so turn and reporting bugs are diagnosed from this + * trace — see `./codedeck live opencode --env CODEDECK_DEBUG_EVENTS=1`. */ + private readonly traceEvents = Boolean(process.env.CODEDECK_DEBUG_EVENTS); + /** The providers' context limits, fetched on the first step that used + * tokens; dropped again when the fetch came back empty, so a server that + * was briefly unreachable is asked again. */ + private contextLimits?: Promise>; + private contextWindow?: number; + private contextPercentage?: number; + /** The turn was reported complete, and no new one has started since. */ + private turnCompleted = false; + /** The error last reported in this turn: OpenCode reports a failed + * turn's error both on its step and on its execution. */ + private lastError?: string; + /** The last main-agent step's own token counts: what the context held + * then. Never the session's running totals, which only grow. */ + private lastTokens?: SessionInfo['tokens']; + /** The model the last main-agent step ran, which is the window its tokens + * are measured against. */ + private lastStepModel?: { providerID: string; id: string }; + /** Names of the session's commands, from the last list fetched: a typed + * `/name` runs as that command only when OpenCode has one by that name. + * Dropped again when the fetch failed, so the next one asks again. */ + private commandNames?: Promise>; + + /** Resolves once the OpenCode session exists server-side. */ + private readonly ready: Promise<{ client: OpenCodeClient; session: SessionInfo }>; + /** Whether the server it runs on is up: until then nothing runs on it. */ + private serverUp = false; + private readonly catalog: () => Promise; + + constructor( + params: StartSession, + private readonly ctx: SessionContext, + clientPromise: Promise, + options: OpenCodeSessionOptions = {}, + ) { + this.cwd = params.cwd; + this.mode = params.mode ?? DEFAULT_MODE; + this.reportUnknownTool = unknownToolReporter((message) => this.ctx.log(message)); + this.translate = newTranslateContext(this.reportUnknownTool); + this.partials = new PartialCoalescer((update) => { + if (!this.ended) this.ctx.emit({ type: 'partial', ...update }); + }); + this.catalog = options.catalog ?? (async () => ({ models: [] })); + this.model = splitModelId(params.model ?? undefined); + // A level the model lacks is ignored by OpenCode: the session starts on + // the model's default rather than failing. + this.variant = params.effort && params.effort !== DEFAULT_VARIANT ? params.effort : undefined; + clientPromise.then( + () => (this.serverUp = true), + () => {}, + ); + this.ready = this.init(clientPromise, params); + // init() reports its own failure as `ended`; nothing else awaits this + // rejection except prompt/interrupt, which catch it themselves. + this.ready.catch(() => {}); + } + + private deliver(event: OpenCodeEvent): void { + if (this.ended) return; + const entries = opencodeEventToEntries(event, this.translate); + if (entries.length > 0) this.ctx.emit({ type: 'entries', entries }); + } + + get isEnded(): boolean { + return this.ended; + } + + /** The server this session runs on was replaced: it ends with the + * reason, which the bridge answers by resuming it. */ + serverReplaced(): void { + this.finish('The OpenCode server restarted to load changed provider profiles.'); + } + + /** The server this session runs on exited: the session ends with the + * reason, so the bridge can resume the conversation on the next one. */ + serverLost(reason: string): void { + this.finish(reason); + } + + private finish(error?: string): void { + if (this.ended) return; + this.ended = true; + this.abortController.abort(); + this.partials.reset(); + // Nothing will report these shells' ends once the stream is gone. + this.shells.clear(); + this.ctx.emit({ type: 'ended', ...(error ? { error } : {}) }); + } + + private async init( + clientPromise: Promise, + params: StartSession, + ): Promise<{ client: OpenCodeClient; session: SessionInfo }> { + try { + // With no model named, the session runs the default the phone was + // shown, set on the session — never whatever OpenCode would pick on + // its own (the last model used, possibly one with no credential). + // A resumed conversation already ran on its model; only a new start is + // checked against what the providers offer. + const catalogRead: Promise = !params.model || !params.resume ? this.catalog() : Promise.resolve({ models: [] }); + // Subscribe BEFORE resolving/creating the session so no event in the gap + // between "session exists" and "we started listening" is missed. The + // stream carries every location, so this session filters by sessionID. + // Opened while the catalog is read, so a new session waits for one + // round trip, not two; a refused model closes it again (the abort in + // `finish`). + const [catalog, client] = await Promise.all([catalogRead, clientPromise]); + const stream = client.event.subscribe({ signal: this.abortController.signal }); + if (params.model && !params.resume) { + const refused = unsupportedModelReason(params.model, catalog.models); + if (refused) throw new Error(refused); + } + // The context meter's window sizes come from the same catalog. + if (catalog.models.length > 0) this.contextLimits ??= Promise.resolve(catalog.contextLimits ?? {}); + const model = params.model ?? catalog.defaultModel; + if (!params.model) this.model = splitModelId(model); + const { session, resumeLost } = await this.resolveSession(client, params); + // Before any prompt can run: which persona the session runs as, and + // the rules of the agent that is actually running — the persona's + // denies must hold from the first tool call, not from the first mode + // switch. + await this.resolvePersona(client, session, params, resumeLost); + // OpenCode runs whatever agent the session recorded: a new one is + // created on build (or plan), and a resumed one may still name a + // persona whose file has since gone, which would fail its next turn. + const running = this.runningAgent(); + // A session that names none runs the one it was created on, or else + // OpenCode's default. + const created = !params.resume || resumeLost; + const recorded = session.agent ?? (created && this.mode === PLAN_MODE ? PLAN_AGENT : BUILD_AGENT); + if (recorded !== running) { + await client.session.switchAgent({ sessionID: session.id, agent: running }).catch(() => {}); + } + await this.statePermissions(client, session.id, running); + // The "memory was lost" notice goes ahead of the session's first line. + if (resumeLost) this.deliver({ type: 'resume-lost' }); + if (!this.ended) { + this.ctx.emit({ type: 'info', nativeSessionId: session.id, ...(model ? { model } : {}), mode: this.mode, persona: this.persona }); + this.reportTitle(session.title); + this.ctx.emit({ type: 'ready' }); + // From here the session's directory has been booted, and its own + // agents are the ones still loading. + this.personasReadyAt = Date.now(); + } + // Consume the event stream for the rest of this session's life. A clean + // end (server closed the connection) ends the session normally; a + // rejection (network drop, server restart) ends it with the error, so + // the bridge's restart policy can bring it back. + void this.consumeEvents(client, stream, session.id).then( + () => this.finish(), + (err) => this.finish(err instanceof Error ? err.message : String(err)), + ); + return { client, session }; + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + this.deliver({ type: 'error', content: message }); + this.finish(message); + throw err; + } + } + + /** `params.resume` names a previously seen OpenCode session id. Verify it + * still exists server-side before trusting it — a session the server no + * longer knows about (deleted, server restarted with no persistence) falls + * through to creating a fresh one rather than failing the whole session. + * `resumeLost` says whether that fallback happened, so the user is told. */ + private async resolveSession( + client: OpenCodeClient, + params: StartSession, + ): Promise<{ session: SessionInfo; resumeLost: boolean }> { + if (params.resume) { + try { + return { session: await client.session.get({ sessionID: params.resume }), resumeLost: false }; + } catch (err) { + // Only "the server does not have this session" is a lost + // conversation. A transport failure says nothing about the session: + // falling through to a fresh one would discard the transcript for a + // server that is merely unreachable, and that server could not + // create the new session either. + if (!isNotFound(err)) { + throw new Error(`OpenCode could not look up session ${params.resume}: ${formatOpenCodeError(err)}`); + } + this.ctx.log(`[opencode] resume ${params.resume} not found server-side (${formatOpenCodeError(err)}) — starting a fresh session instead`); + } + } + return { session: await this.createSessionRemote(client), resumeLost: Boolean(params.resume) }; + } + + /** Created untitled, so OpenCode names it after its first message, on the + * model and agent the session runs from the start — v2 takes both here + * rather than on each prompt. */ + private async createSessionRemote(client: OpenCodeClient): Promise { + try { + return await client.session.create({ + location: { directory: this.cwd }, + ...(this.model ? { model: { ...this.model, ...(this.variant ? { variant: this.variant } : {}) } } : {}), + ...(this.mode === PLAN_MODE ? { agent: PLAN_AGENT } : {}), + permissions: permissionRules(this.mode), + }); + } catch (err) { + throw new Error(`OpenCode session.create failed: ${formatOpenCodeError(err)}`); + } + } + + /** The OpenCode agent the session runs right now: Plan mode runs the plan + * agent, every other mode the persona. */ + private runningAgent(): string { + return this.mode === PLAN_MODE ? PLAN_AGENT : this.persona; + } + + /** + * Which persona the session runs as, decided before it is ready so every + * prompt and every permission rule is stated for it. + * + * A new session is created on the plan agent in Plan mode and `build` + * otherwise; a persona the phone named is waited for — a directory's own + * agents load a moment after it was booted — and must be one a session can + * run as. A resumed session keeps the agent OpenCode recorded, so a + * restart resumes as the session actually ran. Only decides: the caller + * switches OpenCode's session to it. + */ + private async resolvePersona( + client: OpenCodeClient, + session: SessionInfo, + params: StartSession, + resumeLost: boolean, + ): Promise { + if (params.resume && !resumeLost && session.agent && (await this.selectableAgent(client, session.agent))) { + this.persona = session.agent; + return; + } + const requested = params.persona; + if (!requested || requested === BUILD_AGENT) { + this.persona = BUILD_AGENT; + return; + } + if (await this.waitForAgent(client, requested)) { + // Plan mode keeps its own agent; the persona applies when it leaves. + this.persona = requested; + return; + } + this.persona = BUILD_AGENT; + this.deliver({ + type: 'persona-notice', + text: `OpenCode has no agent '${requested}' here, so this session runs as Build.`, + }); + } + + /** OpenCode's record of `id` in this session's directory. `found` is false + * when the agent does not exist (yet), which a caller may wait on. */ + private async agentLookup(client: OpenCodeClient, id: string): Promise<{ found: boolean; persona?: string }> { + try { + const got = await client.agent.get({ agentID: id, location: { directory: this.cwd } }); + return { found: true, ...(isPersona(got.data) ? { persona: got.data.id } : {}) }; + } catch { + return { found: false }; + } + } + + /** The agent under `id`, when it is one a session can run as. */ + private async selectableAgent(client: OpenCodeClient, id: string): Promise { + return (await this.agentLookup(client, id)).persona; + } + + /** Wait for a just-booted directory's agents to load, then answer whether + * `id` is one a session can run as. An agent that exists but is not one + * ends the wait at once: it never becomes one. */ + private async waitForAgent(client: OpenCodeClient, id: string): Promise { + const deadline = Date.now() + PERSONA_WAIT_MS; + for (;;) { + const { found, persona } = await this.agentLookup(client, id); + if (persona) return true; + if (found || Date.now() >= deadline) return false; + await delay(PERSONA_POLL_MS); + } + } + + /** + * Apply the agent the session runs as now and state its rules, through the + * queue: the host runs a second switch while the first is still talking to + * the server, and each queued one reads the mode and persona set last, so + * the session ends on the agent and rules of the final one, never a mix. + */ + private queueAgentSwitch(client: OpenCodeClient, session: SessionInfo): Promise { + const switched = this.agentSwitch.then(async () => { + const agent = this.runningAgent(); + await client.session.switchAgent({ sessionID: session.id, agent }).catch(() => {}); + await this.statePermissions(client, session.id, agent); + }); + this.agentSwitch = switched; + return switched; + } + + /** + * A directory's agent files changed: the persona this session runs as may + * be gone, and the rules of the agent running may have changed. Anything + * for another directory is not this session's news — the directory is + * compared resolved, since the server may spell the same one differently. + */ + private async onAgentUpdated(client: OpenCodeClient, directory: string | undefined): Promise { + if (this.ended) return; + if (directory !== undefined && path.resolve(directory) !== path.resolve(this.cwd)) return; + try { + const { session } = await this.ready; + if (this.ended) return; + const removed = this.persona; + if (removed !== BUILD_AGENT && !(await this.selectableAgent(client, removed))) { + this.persona = BUILD_AGENT; + this.deliver({ + type: 'persona-notice', + text: `OpenCode's agent '${removed}' was removed, so this session runs as Build.`, + }); + if (!this.ended) this.ctx.emit({ type: 'info', persona: BUILD_AGENT }); + } + // The agent running now has its rules restated whatever changed: its + // own file may be the one that was edited. + await this.queueAgentSwitch(client, session); + } catch (err) { + // Nothing here may escape: this runs off the event stream, where a + // rejection has no one to catch it. A session that never came up has + // nothing to restate; anything else is worth the log. + this.ctx.log(`[opencode] could not restate the session after an agent change: ${formatOpenCodeError(err)}`); + } + } + + private async consumeEvents( + client: OpenCodeClient, + stream: AsyncIterable, + sessionId: string, + ): Promise { + for await (const event of stream) { + if (this.ended) break; + const data = event.data as { sessionID?: string } | undefined; + const child = data?.sessionID ? this.children.get(data.sessionID) : undefined; + if (this.traceEvents) { + const parent = (event.data as { parentID?: string } | undefined)?.parentID; + this.ctx.log( + `[opencode] ${event.type} sessionID=${String(data?.sessionID)} parentID=${String(parent)} child=${Boolean(child)} main=${sessionId}`, + ); + } + // Events for other sessions are dropped, except the one that tells this + // session a sub-agent of its own started. + if (data?.sessionID && data.sessionID !== sessionId && !child && !this.noteChild(event, sessionId)) continue; + switch (event.type) { + case 'session.text.delta': { + // Only the main agent's reply text is streamed: a sub-agent's text + // and the model's reasoning are shown collapsed (inside a tool + // group, or as a Thinking step), so a relay round trip for either + // would be traffic for what nobody sees as it arrives. + if (!child && event.data.delta) { + this.partials.delta({ key: partKey(event.data), entryType: 'text', text: event.data.delta }); + } + break; + } + case 'session.text.ended': { + const part = child ? undefined : partKey(event.data); + if (part) this.partials.complete(part); + this.deliver({ + type: 'text', + role: 'assistant', + text: event.data.text, + ...(part ? { part } : {}), + ...(child ? { subagent: subagentOf(child) } : {}), + }); + break; + } + case 'session.reasoning.ended': { + this.deliver({ type: 'reasoning', text: event.data.text, ...(child ? { subagent: subagentOf(child) } : {}) }); + break; + } + case 'session.tool.input.started': { + this.toolCalls.set(event.data.id, { name: event.data.name, input: {} }); + break; + } + case 'session.tool.called': { + const name = this.toolCalls.get(event.data.id)?.name ?? ''; + this.toolCalls.set(event.data.id, { name, input: (event.data.input ?? {}) as Record }); + // Only this session's own calls start children it pairs: a + // sub-agent's own sub-agent is announced to that sub-agent, never + // to us, so its call would wait here forever and the next child + // of ours would be paired with it. + if (name === SUBAGENT_TOOL && !child) this.noteSubagentCall(event.data.id, event.data.input); + this.deliver({ + type: 'tool-start', + callId: event.data.id, + toolName: name, + input: event.data.input, + ...(child ? { subagent: subagentOf(child) } : {}), + }); + break; + } + case 'session.tool.success': { + const diffs = toolCallDiffs(event.data.metadata as Record | undefined); + for (const diff of diffs) this.toolDiffedFiles.add(diff.path); + this.deliver({ + type: 'tool-result', + callId: event.data.id, + text: toolResultText(event.data.content, 'Done.'), + diffs, + ...(child ? { subagent: subagentOf(child) } : {}), + }); + this.finishSubagentCall(event.data.id); + // A sub-agent's own background shell belongs to that sub-agent, and + // its child session ending is what reports the task. + if (!child) this.noteBackgroundShell(event.data.id, event.data.metadata); + break; + } + case 'session.tool.failed': { + this.deliver({ + type: 'tool-result', + callId: event.data.id, + text: toolResultText(event.data.content, formatOpenCodeError(event.data.error)), + isError: true, + diffs: toolCallDiffs(event.data.metadata as Record | undefined), + ...(child ? { subagent: subagentOf(child) } : {}), + }); + this.finishSubagentCall(event.data.id); + break; + } + case 'session.step.started': { + // The step is what names the model; its end carries only the + // tokens, and the context meter needs both. + if (!child) this.lastStepModel = event.data.model; + break; + } + case 'session.step.ended': { + if (!child) { + // The step's own counters, not the session's running totals (see + // contextTokens): what the next step will carry as its prompt. + if (event.data.tokens) this.lastTokens = event.data.tokens; + this.reportStepTokens(); + } + // Files the step changed that no tool call already showed (a shell + // command's writes, say): v2 names them here rather than as a + // session-wide diff. + if (event.data.files && event.data.files.length > 0) void this.reportChangedFiles(client, sessionId, event.data.files); + break; + } + // A compaction rewrote the conversation the model sees. The phone's + // transcript still holds what the summary replaced, and its context + // meter is about to drop, so the compaction is stated rather than + // left to look like the context vanishing on its own. + case 'session.compaction.started': { + if (child) break; + // A manual compaction is the `/compact` built-in, whose own status + // line is already in the transcript; only an automatic one is news. + if (event.data.reason !== 'manual') { + this.deliver({ type: 'status', text: 'OpenCode is compacting the conversation to make room in the context.' }); + } + break; + } + case 'session.compaction.ended': { + if (child) break; + this.deliver({ + type: 'status', + text: + event.data.reason === 'manual' + ? 'Compacted the conversation: OpenCode continues from a summary of it.' + : 'Compacted the conversation to make room: OpenCode continues from a summary of it.', + }); + break; + } + case 'session.compaction.failed': { + if (child) break; + this.reportError(`OpenCode could not compact the conversation: ${formatOpenCodeError(event.data.error)}`); + break; + } + // A child's failure is the sub-agent's own: it ends that sub-agent, + // rather than surfacing on the phone as an error the user cannot + // place. The background task it was left as is what reports it. + case 'session.step.failed': { + if (child) { + this.childEnded(child, 'failed'); + break; + } + if (event.data.error) this.reportError(formatOpenCodeError(event.data.error)); + break; + } + case 'session.execution.failed': { + if (child) { + this.childEnded(child, 'failed'); + break; + } + this.reportError(formatOpenCodeError(event.data.error)); + // The turn is over however it ended: a failure that left the + // session looking like it was still working would strand the phone + // with a spinner and no way back. + this.markTurnComplete(); + break; + } + // A child's execution ending is that sub-agent's own end, exactly as + // its `session.idle` would be: only THIS session's own execution + // ending ends its turn. A child's end read as the session's own marks + // the turn complete while the parent is still working, and — because + // the child is then never recorded as ended — makes the call that + // started it a background task the phone shows as running for good. + case 'session.execution.succeeded': { + if (child) { + this.childEnded(child, 'completed'); + break; + } + this.markTurnComplete(); + break; + } + case 'session.execution.interrupted': { + if (child) { + this.childEnded(child, 'stopped'); + break; + } + this.markTurnComplete(); + break; + } + case 'session.idle': { + if (child) this.childEnded(child, 'completed'); + else this.markTurnComplete(); + break; + } + // A background shell of this session ended: the server's + // `shell.exited` names the shell and its exit, but no session (see + // the `shells` field), so it is matched by the id this session learned + // when the shell was started. A shell of another session's — or one + // already reported — is ignored. + case 'shell.exited': { + this.finishShell(event.data); + break; + } + case 'session.status': { + const status = event.data.status; + if (status.type === 'retry') { + // The provider failed and OpenCode tries again, waiting longer + // each time: said on the phone, rather than a turn that only + // seems slow until the last attempt's error. + this.deliver({ type: 'status', text: retryText(status, Date.now()) }); + break; + } + if (child) break; + if (status.type === 'busy') { + this.turnCompleted = false; + this.lastError = undefined; + } + if (!this.ended) this.ctx.emit({ type: 'turn', state: status.type === 'busy' ? 'running' : 'idle' }); + break; + } + case 'session.renamed': { + if (!child) this.reportTitle(event.data.title); + break; + } + // A directory's agent files changed: the persona this session runs + // as may be gone, and the rules of the agent running may have + // changed. Handled off the stream, since it talks to the server. + case 'agent.updated': { + void this.onAgentUpdated(client, event.location?.directory); + break; + } + case 'permission.asked': { + this.handlePermission(client, event.data, child); + break; + } + case 'form.created': { + if (event.data.form.metadata?.kind !== 'question') break; + this.handleForm(client, event.data.form, child); + break; + } + case 'session.execution.started': { + // A sub-agent asked again works again: it has not ended. + if (child) { + child.ended = false; + break; + } + // V2 starts a turn with this, not with a `busy` status: without it + // the phone would only ever learn a turn had ended. + this.turnCompleted = false; + this.lastError = undefined; + if (!this.ended) this.ctx.emit({ type: 'turn', state: 'running' }); + break; + } + default: + break; + } + } + } + + /** + * What each call is, by its id: v2 names the tool when its input starts and + * sends the input when the call is made, while the events that come after + * — a permission ask, a result — carry only the id. A permission card is + * therefore read from here, since the ask itself says no more than which + * call it is about. + */ + private readonly toolCalls = new Map }>(); + /** Files a completed edit/write/patch call already showed as a diff card. + * A file the step also names as changed is that same edit — dropped + * instead of shown twice — and consumes the entry, so a LATER change to + * the file (a shell command, say) still gets its card. */ + private readonly toolDiffedFiles = new Set(); + + /** Whether this event is this session's own child being announced; the + * child is recorded so its later events are read. OpenCode names the + * parent on the child's own `session.created`, which is what tells a + * sub-agent's session apart from an unrelated one on the same server. */ + private noteChild(event: V2Event, sessionId: string): boolean { + if (event.type !== 'session.created') return false; + const created = event.data; + if (created.parentID !== sessionId || this.children.has(created.sessionID)) return false; + // The call that started it named the sub-agent; the two events arrive + // back to back, the call first. + const call = this.pendingSubagents.shift(); + this.children.set(created.sessionID, { + callId: call?.callId ?? '', + title: call?.title ?? created.title ?? 'Sub-agent', + ...(call?.label ? { label: call.label } : {}), + background: false, + stopping: false, + ended: false, + }); + return true; + } + + /** Sub-agent calls whose child session has not been announced yet: the + * call names the sub-agent, the `session.created` that follows names the + * session it runs in, and the two are paired here. */ + private readonly pendingSubagents: Array<{ callId: string; label?: string; title: string }> = []; + + /** Remember what the `subagent` call asked for; the child session it + * starts is created just after, and claims this. */ + private noteSubagentCall(callId: string, input: Record): void { + const label = typeof input.agent === 'string' && input.agent ? input.agent : undefined; + const title = typeof input.description === 'string' && input.description ? input.description : 'Sub-agent'; + this.pendingSubagents.push({ callId, ...(label ? { label } : {}), title }); + } + + /** The child a sub-agent call started, by that call's id. */ + private childOfCall(callId: string): { id: string; child: ChildSession } | undefined { + for (const [id, child] of this.children) if (child.callId === callId) return { id, child }; + return undefined; + } + + /** A sub-agent call finished while its child session is still going: the + * sub-agent was left to run on its own, which is the background task the + * phone can stop. */ + private finishSubagentCall(callId: string): void { + const found = this.childOfCall(callId); + if (!found) return; + const { id, child } = found; + // A child whose execution already ended ran to completion inside the + // call: it was not left running, so there is no task to report. + if (child.background || child.ended) return; + child.background = true; + this.deliver({ type: 'task', taskId: id, kind: 'agent', title: child.title, status: 'running', callId: child.callId }); + } + + private childIdOf(child: ChildSession): string { + for (const [id, c] of this.children) if (c === child) return id; + return child.callId; + } + + /** A child session ended — its own execution, or a failure: a background + * sub-agent's task ended (stopped, when the user asked for that). */ + private childEnded(child: ChildSession, status: 'completed' | 'failed' | 'stopped'): void { + child.ended = true; + if (!child.background) return; + child.background = false; + this.deliver({ + type: 'task', + taskId: this.childIdOf(child), + kind: 'agent', + title: child.title, + status: child.stopping ? 'stopped' : status, + callId: child.callId, + }); + } + + /** + * A `shell` call OpenCode moved to the background, read from its success + * result. The result names the shell and reports it still `running`; a + * shell that ran to completion carries its output and its exit code + * instead, with no shell id — that difference is what tells a background + * task apart from a call that already finished inside the turn. The shell + * is a server object whose own events (`shell.created`, `shell.exited`) + * name no session, so the id learned here is the only thing that ties its + * exit back to this session's task. + */ + private noteBackgroundShell(callId: string, metadata: unknown): void { + const started = this.toolCalls.get(callId); + if (started?.name !== SHELL_TOOL) return; + const meta = metadata as { shellID?: unknown; status?: unknown } | undefined; + const shellID = typeof meta?.shellID === 'string' ? meta.shellID : ''; + if (shellID === '' || meta?.status !== 'running' || this.shells.has(shellID)) return; + const command = started.input.command; + const title = typeof command === 'string' && command !== '' ? command : 'Background command'; + this.shells.set(shellID, { title, callId, stopping: false }); + this.deliver({ type: 'task', taskId: shellID, kind: 'shell', title, status: 'running', callId }); + } + + /** + * A background shell of this session ended, out of the server's + * `shell.exited` (which names no session — see `shells`). Its exit decides + * how it went; a shell the user stopped, or one the server killed, is + * reported as stopped rather than failed. A shell this session did not + * start is ignored. + */ + private finishShell(data: { id: string; exit?: number; status: string }): void { + const shell = this.shells.get(data.id); + if (!shell) return; + this.shells.delete(data.id); + const status: TaskStatus = + shell.stopping || data.status === 'killed' + ? 'stopped' + : data.status === 'timeout' || (data.exit !== undefined && data.exit !== 0) + ? 'failed' + : 'completed'; + this.deliver({ type: 'task', taskId: data.id, kind: 'shell', title: shell.title, status, callId: shell.callId }); + } + + /** The turn is over: said once, however many of v2's end signals arrive. */ + private markTurnComplete(): void { + // A live row left over is dropped here (nothing will complete it), and no + // part timer outlives the turn. + this.partials.reset(); + if (!this.turnCompleted) this.deliver({ type: 'idle' }); + this.turnCompleted = true; + if (!this.ended) this.ctx.emit({ type: 'turn', state: 'idle' }); + } + + /** Files a step changed that no tool call showed, as diff cards: the + * server's own diff carries the patch for each. */ + private async reportChangedFiles(client: OpenCodeClient, sessionId: string, files: string[]): Promise { + const wanted = files.filter((f) => !this.consumeToolDiff(f)); + if (wanted.length === 0) return; + try { + const diff = await client.session.diff({ sessionID: sessionId }); + const byPath = new Map(diff.map((d) => [d.file, d])); + const shown = wanted.flatMap((f) => { + const found = byPath.get(f) ?? [...byPath.values()].find((d) => d.file.endsWith(`/${f}`) || f.endsWith(`/${d.file}`)); + return found ? [found] : []; + }); + if (shown.length > 0) this.deliver({ type: 'diff', files: shown }); + } catch { + // A diff the server will not give is not worth failing a turn over. + } + } + + /** Whether a tool call already showed `file`'s change (removing that + * record). Tool calls name files by absolute path, the step's list by + * worktree-relative path, so either may be a path-suffix of the other. */ + private consumeToolDiff(file: string): boolean { + const norm = (p: string) => p.replace(/\\/g, '/'); + const target = norm(file); + for (const shown of this.toolDiffedFiles) { + const s = norm(shown); + if (s === target || s.endsWith(`/${target}`) || target.endsWith(`/${s}`)) { + this.toolDiffedFiles.delete(shown); + return true; + } + } + return false; + } + + /** Report how full the context is after a step, over the window of the + * model that ran it — only what changed, and nothing for a model with no + * declared limit. */ + private reportStepTokens(): void { + const providerID = this.lastStepModel?.providerID; + const modelID = this.lastStepModel?.id; + if (!this.lastTokens || !providerID || !modelID) return; + const used = contextTokens(this.lastTokens); + if (used <= 0) return; + this.contextLimits ??= this.catalog().then((c) => { + if (c.models.length === 0) this.contextLimits = undefined; + return c.contextLimits ?? {}; + }); + void this.contextLimits.then((limits) => { + const window = limits[`${providerID}/${modelID}`]; + if (!window || this.ended) return; + const pct = Math.max(0, Math.min(100, Math.round((used / window) * 100))); + const changedPct = pct !== this.contextPercentage; + const changedCw = window !== this.contextWindow; + if (!changedPct && !changedCw) return; + this.contextPercentage = pct; + this.contextWindow = window; + this.ctx.emit({ + type: 'info', + ...(changedPct ? { contextPercentage: pct } : {}), + ...(changedCw ? { contextWindow: window } : {}), + }); + }); + } + + private reportError(content: string): void { + if (content === this.lastError) return; + this.lastError = content; + this.deliver({ type: 'error', content }); + } + + /** + * Answer one OpenCode permission ask: allowed in the auto-approve mode, + * otherwise sent to the phone. Always answered — OpenCode must never wait + * on a reply that is not coming. + */ + private handlePermission(client: OpenCodeClient, ask: SessionPermissionAsk, child?: ChildSession): void { + if (this.answeredAsks.has(ask.id)) return; + this.answeredAsks.add(ask.id); + const reply = (decision: 'once' | 'always' | 'reject'): void => { + // Best effort — the ask then stays pending server-side; there is no + // live connection left to retry over. + void client.permission.reply({ sessionID: ask.sessionID, requestID: ask.id, decision }).catch(() => {}); + }; + + if (this.mode === AUTO_APPROVE_MODE) { + reply('once'); + return; + } + const started = this.toolCalls.get(ask.source?.id ?? ''); + const toolName = started?.name ?? ask.action; + // The question tool asks for permission to ask, and then asks. Its own + // ask is the question: a card for it would make the user allow being + // asked before they are asked, with the answer they give the form the + // only one that says anything. Allowed here, so the form's card is the + // one they see. + if (toolName === QUESTION_TOOL) { + reply('once'); + return; + } + // The ask names the call, not what it will run: the arguments it was + // called with are what the card shows (`read` a file, `shell` a command). + // Its own `metadata` carries them when v2 sends any. + const input = started?.input ?? ((ask.metadata ?? {}) as Record); + const description = ask.message?.trim() || describePermission(ask.action, ask.resources); + const call = opencodeTool(toolName, input, this.reportUnknownTool); + this.ctx + .requestPermission({ + requestId: ask.source?.id ?? ask.id, + toolName, + kind: call.kind, + title: call.title, + description, + locations: toolLocations(input), + options: ask.save && ask.save.length > 0 ? [PERMISSION_ALLOW, allowInProject(ask.save), PERMISSION_DENY] : [PERMISSION_ALLOW, PERMISSION_DENY], + ...(child ? { subagent: subagentOf(child) } : {}), + }) + .then((outcome) => { + const chosen = outcome.outcome === 'selected' ? outcome.optionId : undefined; + reply(chosen === PERMISSION_ALLOW.id ? 'once' : chosen === PERMISSION_ALLOW_ALWAYS.id && ask.save?.length ? 'always' : 'reject'); + }) + .catch(() => reply('reject')); + } + + /** + * One OpenCode form raised by the `question` tool, asked on the phone. + * Keyed by the question tool's call id — the form's own metadata names it — + * so the card groups under that call and the call's result marks the card + * answered. Always answered — a dismissal cancels the form, so OpenCode + * never waits on a reply that is not coming. + */ + private handleForm(client: OpenCodeClient, form: FormInfoLike, child?: ChildSession): void { + if (this.answeredAsks.has(form.id)) return; + this.answeredAsks.add(form.id); + + const tool = (form.metadata as { tool?: { id?: string } } | undefined)?.tool; + const toolUseId = tool?.id ?? form.id; + this.deliver({ type: 'question', toolUseId }); + + const cancel = (): void => { + void client.session.form.cancel({ sessionID: form.sessionID, formID: form.id }).catch(() => {}); + }; + this.ctx + .askQuestion(toolUseId, toQuestionSpecs(form.fields)) + .then((outcome) => { + if (outcome.outcome !== 'answered') return cancel(); + return client.session.form + .reply({ sessionID: form.sessionID, formID: form.id, answer: toFormAnswer(form.fields, outcome.answers) }) + .catch(cancel); + }) + .catch(cancel); + } + + /** + * A typed `/name args` naming one of OpenCode's commands runs through + * `session.command`, which expands the command's template; sent as plain + * text it would reach the model verbatim. Anything else is a prompt. + */ + prompt(text: string): void { + const command = parseSlashCommand(text); + this.turnCompleted = false; + this.lastError = undefined; + this.ready + .then(async ({ client, session }) => { + const listed = command ? await this.knownCommands(client) : new Set(); + if (command && listed.has(command.name)) { + this.runCommand(client, session.id, command.name, command.args); + return; + } + const builtin = command && sessionCommand(command.name, listed); + if (builtin) { + this.runSessionCommand(client, session.id, builtin); + return; + } + try { + await client.session.prompt({ sessionID: session.id, text }); + } catch (err) { + this.deliver({ type: 'error', content: `OpenCode prompt failed: ${formatOpenCodeError(err)}` }); + } + }) + .catch((err) => { + this.deliver({ type: 'error', content: `OpenCode prompt failed: ${err instanceof Error ? err.message : String(err)}` }); + }); + } + + /** One of the commands OpenCode's terminal runs itself; its outcome is a + * status line, its failure an error entry. */ + private runSessionCommand(client: OpenCodeClient, sessionID: string, command: (typeof SESSION_COMMANDS)[number]): void { + command + .run({ + client, + sessionID, + ...(this.model ? { model: { ...this.model, ...(this.variant ? { variant: this.variant } : {}) } } : {}), + status: (text) => this.deliver({ type: 'status', text }), + }) + .catch((err) => this.deliver({ type: 'error', content: err instanceof Error ? err.message : String(err) })); + } + + /** The title OpenCode gave the session, once it has one of its own + * (not the timestamp a new session starts with) — each new one once. */ + private reportTitle(title: string | undefined): void { + if (!title || isPlaceholderTitle(title) || title === this.title || this.ended) return; + this.title = title; + this.ctx.emit({ type: 'info', title }); + } + + /** `session.command` answers only once the whole turn is over, so it is + * not awaited: the turn itself arrives on the event stream. A refusal + * becomes an error entry; a connection given up on while the turn runs + * (the stream still carries it) is only logged. */ + private runCommand(client: OpenCodeClient, sessionID: string, name: string, args: string): void { + client.session + .command({ sessionID, name, text: args }) + .then(() => {}) + .catch((err) => this.ctx.log(`[opencode] /${name}: ${formatOpenCodeError(err)}`)); + } + + private async fetchCommands(client: OpenCodeClient): Promise> { + try { + const listed = await client.command.list({ location: { directory: this.cwd } }); + this.commandNames = Promise.resolve(new Set(listed.data.map((c) => c.name))); + return listed.data; + } catch (err) { + throw new Error(`OpenCode could not list its commands: ${formatOpenCodeError(err)}`); + } + } + + private knownCommands(client: OpenCodeClient): Promise> { + this.commandNames ??= this.fetchCommands(client).then( + (commands) => new Set(commands.map((c) => c.name)), + () => { + this.commandNames = undefined; + return new Set(); + }, + ); + return this.commandNames; + } + + /** The server's commands, then the ones the driver runs itself. */ + async listCommands(): Promise { + const { client } = await this.ready; + const commands = await this.fetchCommands(client); + return [...toSlashCommands(commands), ...sessionSlashCommands(new Set(commands.map((c) => c.name)))]; + } + + /** The personas this session can run as: the selectable agents in its + * directory, waiting a moment for the directory's own agents to load + * after the session became ready. */ + async listPersonas(): Promise { + const { client } = await this.ready; + return listSelectablePersonas(client, { directory: this.cwd }, (this.personasReadyAt ?? Date.now()) + PERSONA_LOAD_MS); + } + + async mcpStatus(): Promise { + const { client } = await this.ready; + return openCodeSessionMcp(client, this.cwd); + } + + async toggleMcp(name: string, enabled: boolean): Promise { + const { client } = await this.ready; + return toggleOpenCodeMcp(client, this.cwd, name, enabled); + } + + async setOption(option: SessionOption, value: string): Promise { + switch (option) { + case 'mode': { + if (!OPENCODE_MODES.some((m) => m.id === value)) throw new Error(`OpenCode has no mode '${value}'`); + this.mode = value; + // V2 runs an agent and a permission ruleset for the whole session, so + // switching the mode switches both: the plan agent (or the session's + // persona), and whether a tool call reaches the phone at all. + const { client, session } = await this.ready; + await this.queueAgentSwitch(client, session); + return; + } + case 'persona': { + // The session runs as the persona outside Plan mode; in Plan mode it + // runs OpenCode's plan agent, and comes back to the persona when it + // leaves. Either way the persona is what a switch is validated + // against, so an agent that is gone is refused before it is set. + const { client, session } = await this.ready; + const found = await this.selectableAgent(client, value); + if (!found) throw new Error(`OpenCode has no agent '${value}' here`); + this.persona = found; + if (this.mode === PLAN_MODE) return; + await this.queueAgentSwitch(client, session); + return; + } + case 'model': { + // V2 keeps the model on the session; the switch applies from the next + // step, which is what a per-prompt model used to mean. + const split = splitModelId(value); + if (!split) throw new Error(`'${value}' is not an OpenCode provider/model id`); + const models = (await this.catalog()).models; + const refused = unsupportedModelReason(value, models); + if (refused) throw new Error(refused); + // A level the new model lacks would be ignored; drop it. + if (this.variant && !models.find((m) => m.id === value)?.efforts?.some((e) => e.id === this.variant)) this.variant = undefined; + this.model = split; + await this.switchModel(); + return; + } + case 'effort': { + // Levels are the model's own (its OpenCode variants). + const { models, defaultModel } = await this.catalog(); + const id = this.model ? `${this.model.providerID}/${this.model.id}` : defaultModel; + const levels = models.find((m) => m.id === id)?.efforts ?? []; + if (!levels.some((e) => e.id === value)) { + throw new Error(levels.length > 0 ? `'${value}' is not a reasoning level of ${id}` : `${id ?? 'This model'} has no reasoning levels`); + } + this.variant = value === DEFAULT_VARIANT ? undefined : value; + await this.switchModel(); + return; + } + } + } + + /** Set the session's rules for its mode and for `agent`'s own rules. An + * agent that cannot be read leaves only the mode's catch-all, which is + * logged: nothing the agent forbids would then be refused. */ + private async statePermissions(client: OpenCodeClient, sessionID: string, agent: string): Promise { + const agentRules = await Promise.resolve() + .then(() => client.agent.get({ agentID: agent, location: { directory: this.cwd } })) + .then( + (got) => got.data.permissions, + (err) => { + this.ctx.log(`[opencode] could not read agent ${agent}'s permissions: ${formatOpenCodeError(err)}`); + return []; + }, + ); + await client.session + .update({ sessionID, permissions: permissionRules(this.mode, agentRules) }) + .catch((err) => this.ctx.log(`[opencode] could not set the session's permissions: ${formatOpenCodeError(err)}`)); + } + + /** Tell the server which model (and reasoning variant) the session runs. */ + private async switchModel(): Promise { + if (!this.model) return; + const { client, session } = await this.ready; + await client.session + .switchModel({ sessionID: session.id, model: { ...this.model, ...(this.variant ? { variant: this.variant } : {}) } }) + .catch(() => {}); + } + + async interrupt(): Promise { + // The interrupted turn's remaining text is dropped rather than left to + // keep arriving; its final entries still carry whatever it produced. + this.partials.reset(); + try { + const { client, session } = await this.ready; + await client.session.interrupt({ sessionID: session.id }); + } catch { + // Best effort — an already-idle OpenCode session can legitimately + // refuse an interrupt. + } + } + + /** + * Stop a task the phone can see: a sub-agent left running in the + * background (its task id is its child session's id), or a background + * shell command (its task id is the shell's). Either may already be gone — + * the task ended between the phone's tap and this call — and a target the + * server no longer knows is the outcome that was wanted, not a failure: + * the task's own end, or the lack of one, is what reports it. + */ + async stopTask(taskId: string): Promise { + const shell = this.shells.get(taskId); + const child = shell ? undefined : this.children.get(taskId); + if (!shell && !child?.background) throw new Error('No such task is running'); + if (shell) shell.stopping = true; + else if (child) child.stopping = true; + const { client } = await this.ready; + try { + // The delete must name the session's own directory: the server scopes + // shells by location, and a delete without one drops its record + // without ending the process, which would leave the command running + // while the phone was told it stopped. + if (shell) await client.shell.remove({ id: taskId, location: { directory: this.cwd } }); + else await client.session.interrupt({ sessionID: taskId }); + } catch (err) { + if (isNotFound(err)) { + // A sub-agent's child session reports its own end, but a shell the + // server has forgotten will send no exit for this session to read: + // report it stopped here, or it would show as running for good. + if (shell) this.finishShell({ id: taskId, status: 'killed' }); + return; + } + throw new Error(`OpenCode could not stop the task: ${formatOpenCodeError(err)}`); + } + } + + /** What the session has cost so far, as OpenCode prices its models (no + * subscription windows: OpenCode has none to report). A free or + * unpriced model's session reports no cost. */ + async getUsage(): Promise { + const { client, session } = await this.ready; + try { + const info = await client.session.get({ sessionID: session.id }); + const context = this.lastTokens ? openCodeContext(this.lastTokens, this.contextWindow) : undefined; + return { + available: true, + windows: [], + ...(info.cost ? { sessionCostUsd: info.cost } : {}), + ...(context ? { context } : {}), + fetchedAt: new Date().toISOString(), + }; + } catch { + return null; + } + } + + async end(): Promise { + if (this.ended) return; + this.ended = true; + this.abortController.abort(); + this.partials.reset(); + // The session is gone from this client: no shell event will arrive for it. + this.shells.clear(); + // A server still to start (waiting for its provider profiles, or being + // installed) runs nothing of this session's, and may not start before + // the host stops waiting on this end. + if (!this.serverUp) return; + try { + const { client, session } = await this.ready; + await client.session.interrupt({ sessionID: session.id }); + } catch { + // Best effort — the session may never have been created, or may + // already be gone server-side. + } + } +} + +/** The parts of a v2 form the question card reads. */ +interface FormInfoLike { + id: string; + sessionID: string; + title: string; + metadata?: Record; + fields: FormFieldLike[]; +} + +export interface OpenCodeSessionOptions { + /** The models a session may choose from, and its default. */ + catalog?: () => Promise; +} From c500530807912cbcb1ad0d62583c8f896b9d8649 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 09:07:54 +0100 Subject: [PATCH 40/42] refactor(client-runtime): split the runtime tests by topic Pure move: the 71 runtime tests leave one 4.2k-line file for a directory module. `runtime/tests.rs` keeps the shared helpers (Spy, the fixed clock and entropy, the wait helpers, core_for*, eose_all, the frame pushers and decoders, the key store) and declares the topic modules; each topic file holds its tests and the helpers only it uses. No test body, name or comment changed; the shared helpers are now `pub(super)` so the submodules can reach them. Files: tor.rs, connection.rs, store_layer.rs, machines.rs, session_keys.rs, direct.rs, sync.rs, backup.rs, presence.rs, terminal.rs. Co-Authored-By: Claude Code --- crates/client-runtime/src/runtime/tests.rs | 3977 +---------------- .../src/runtime/tests/backup.rs | 256 ++ .../src/runtime/tests/connection.rs | 778 ++++ .../src/runtime/tests/direct.rs | 109 + .../src/runtime/tests/machines.rs | 437 ++ .../src/runtime/tests/presence.rs | 92 + .../src/runtime/tests/session_keys.rs | 540 +++ .../src/runtime/tests/store_layer.rs | 764 ++++ .../client-runtime/src/runtime/tests/sync.rs | 246 + .../src/runtime/tests/terminal.rs | 282 ++ .../client-runtime/src/runtime/tests/tor.rs | 426 ++ 11 files changed, 3970 insertions(+), 3937 deletions(-) create mode 100644 crates/client-runtime/src/runtime/tests/backup.rs create mode 100644 crates/client-runtime/src/runtime/tests/connection.rs create mode 100644 crates/client-runtime/src/runtime/tests/direct.rs create mode 100644 crates/client-runtime/src/runtime/tests/machines.rs create mode 100644 crates/client-runtime/src/runtime/tests/presence.rs create mode 100644 crates/client-runtime/src/runtime/tests/session_keys.rs create mode 100644 crates/client-runtime/src/runtime/tests/store_layer.rs create mode 100644 crates/client-runtime/src/runtime/tests/sync.rs create mode 100644 crates/client-runtime/src/runtime/tests/terminal.rs create mode 100644 crates/client-runtime/src/runtime/tests/tor.rs diff --git a/crates/client-runtime/src/runtime/tests.rs b/crates/client-runtime/src/runtime/tests.rs index 028041fd..3dd760c7 100644 --- a/crates/client-runtime/src/runtime/tests.rs +++ b/crates/client-runtime/src/runtime/tests.rs @@ -11,17 +11,29 @@ use client_core::stores::session_key::{SessionGrant, SessionKey}; use std::sync::Mutex; use tokio::task::LocalSet; -const SEC_PHONE: &str = +mod tor; +mod connection; +mod store_layer; +mod machines; +mod session_keys; +mod direct; +mod sync; +mod backup; +mod presence; +mod terminal; + +pub(super) const SEC_PHONE: &str = "0000000000000000000000000000000000000000000000000000000000000001"; #[derive(Default)] -struct Spy { +pub(super) struct Spy { statuses: Mutex>, connected_relays: Mutex>>, messages: Mutex>, failures: Mutex>, events: Mutex>, } + impl CoreObserver for Spy { fn connection_changed(&self, status: ConnectionStatus, needs_pairing_check: bool, connected_relays: &[String]) { self.statuses.lock().unwrap().push((status, needs_pairing_check)); @@ -38,20 +50,23 @@ impl CoreObserver for Spy { } } -struct FixedClock(RefCell); +pub(super) struct FixedClock(RefCell); + impl Clock for FixedClock { fn now_ms(&self) -> u64 { *self.0.borrow() } } -struct ZeroEntropy; + +pub(super) struct ZeroEntropy; + impl Entropy for ZeroEntropy { fn unit(&self) -> f64 { 0.0 } } -fn fast_reconnect() -> ReconnectConfig { +pub(super) fn fast_reconnect() -> ReconnectConfig { ReconnectConfig { base_ms: 40, max_ms: 120, @@ -61,23 +76,17 @@ fn fast_reconnect() -> ReconnectConfig { } } -/// The connect deadline the `fast_reconnect` config uses, for advancing the -/// fake clock. -fn connect_deadline() -> u64 { - fast_reconnect().connect_deadline_ms -} - /// A fixed wait, only where no event names what the test needs: a check that /// something did NOT happen (give the loop time to prove the absence), a /// fake-clock step that must land before the test moves the clock on, or a /// private state the views do not expose. -async fn settle() { +pub(super) async fn settle() { tokio::time::sleep(Duration::from_millis(120)).await; } /// Wait for a condition that names the event a test needs, instead of always /// burning the same wall-clock time and hoping it was enough. -async fn wait_for(mut cond: impl FnMut() -> bool) { +pub(super) async fn wait_for(mut cond: impl FnMut() -> bool) { for _ in 0..600 { if cond() { return; @@ -88,7 +97,7 @@ async fn wait_for(mut cond: impl FnMut() -> bool) { } /// Wait until the live FSM reports `want`. -async fn wait_status(core: &Core, want: ConnectionStatus) { +pub(super) async fn wait_status(core: &Core, want: ConnectionStatus) { for _ in 0..600 { if core.connection_status().await.0 == want { return; @@ -98,13 +107,13 @@ async fn wait_status(core: &Core, want: ConnectionStatus) { panic!("the core never reached {want:?}"); } -async fn wait_connected(core: &Core) { +pub(super) async fn wait_connected(core: &Core) { wait_status(core, ConnectionStatus::Connected).await; } /// [`wait_for`] for a probe that has to query a view (answered in order with /// everything the loop had already queued). -async fn wait_for_async(mut probe: F) +pub(super) async fn wait_for_async(mut probe: F) where F: FnMut() -> Fut, Fut: std::future::Future, @@ -118,11 +127,11 @@ where panic!("condition never became true"); } -async fn core_for(mock: &MockRelay, phone: &Keypair, spy: Rc) -> Core { +pub(super) async fn core_for(mock: &MockRelay, phone: &Keypair, spy: Rc) -> Core { core_for_ports(mock, phone, spy, CorePorts::default()).await } -async fn core_for_ports( +pub(super) async fn core_for_ports( mock: &MockRelay, phone: &Keypair, spy: Rc, @@ -131,7 +140,7 @@ async fn core_for_ports( core_for_ports_clock(mock, phone, spy, ports, Rc::new(FixedClock(RefCell::new(1_000_000)))).await } -async fn core_for_ports_clock( +pub(super) async fn core_for_ports_clock( mock: &MockRelay, phone: &Keypair, spy: Rc, @@ -158,554 +167,9 @@ async fn core_for_ports_clock( core } -struct OkHttp; -impl crate::attachments::HttpFetch for OkHttp { - fn put( - &self, - _url: &str, - _headers: Vec<(String, String)>, - _body: Vec, - ) -> crate::ports::LocalBoxFuture<'_, Result> - { - Box::pin(async { - Ok(crate::attachments::HttpResponse { - status: 200, - body: b"{}".to_vec(), - }) - }) - } -} - -/// Every call fails — forces the session-image chunk fallback. -struct FailHttp; -impl crate::attachments::HttpFetch for FailHttp { - fn put( - &self, - _url: &str, - _headers: Vec<(String, String)>, - _body: Vec, - ) -> crate::ports::LocalBoxFuture<'_, Result> - { - Box::pin(async { Err("no server".to_string()) }) - } -} - -/// A Blossom server that accepts the upload and never answers. -struct HangHttp; -impl crate::attachments::HttpFetch for HangHttp { - fn put( - &self, - _url: &str, - _headers: Vec<(String, String)>, - _body: Vec, - ) -> crate::ports::LocalBoxFuture<'_, Result> - { - Box::pin(std::future::pending()) - } -} - -#[tokio::test] -async fn a_stalled_image_upload_does_not_block_the_loop() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let ports = CorePorts { - http: Rc::new(HangHttp), - ..CorePorts::default() - }; - let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.start(); - eose_all(&mut mock).await; - wait_connected(&core).await; - core.dispatch(Intent::SetBlossomServer("https://blossom.example".into())).await; - - let core2 = core.clone(); - let upload = tokio::task::spawn_local(async move { - core2 - .dispatch(Intent::SendSessionFile(SessionFileSend { - machine: "m".into(), - session_id: "s1".into(), - text: String::new(), - data: b"bytes".to_vec(), - filename: "a.jpg".into(), - mime_type: "image/jpeg".into(), - })) - .await; - }); - // Let the upload task reach the hanging `put` before checking the - // loop still answers. - for _ in 0..10 { - tokio::task::yield_now().await; - } - - // The upload is still hanging, yet the loop answers at once. - tokio::time::timeout(Duration::from_secs(1), core.machines_view()) - .await - .expect("the loop stayed blocked behind the upload"); - assert!(!upload.is_finished(), "the intent answers only when the send ends"); - upload.abort(); - }) - .await; -} - -/// Records every `set_proxy` call — used to check the HTTP port's own -/// boot-time proxy wiring, the twin of `WsConfig.proxy` above. -#[derive(Default)] -struct RecordingHttp { - proxy_calls: RefCell>>, -} -impl crate::attachments::HttpFetch for RecordingHttp { - fn put( - &self, - _url: &str, - _headers: Vec<(String, String)>, - _body: Vec, - ) -> crate::ports::LocalBoxFuture<'_, Result> - { - Box::pin(async { Err("not used".to_string()) }) - } - fn set_proxy(&self, proxy: Option<&str>) { - self.proxy_calls.borrow_mut().push(proxy.map(str::to_string)); - } -} - -#[tokio::test] -async fn spawn_applies_the_boot_time_proxy_to_the_http_port_when_tor_is_on() { - LocalSet::new() - .run_until(async { - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let spy = Rc::new(Spy::default()); - let recording = Rc::new(RecordingHttp::default()); - let ports = CorePorts { - http: Rc::clone(&recording) as Rc, - ..CorePorts::default() - }; - let _core = Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone.clone())), - proxy: Some("127.0.0.1:9050".to_string()), - tor: true, - device_label: None, - reconnect: fast_reconnect(), - }, - ports, - spy, - Rc::new(FixedClock(RefCell::new(1_000_000))), - Rc::new(ZeroEntropy), - ) - .await; - - assert_eq!( - recording.proxy_calls.borrow().as_slice(), - [Some("127.0.0.1:9050".to_string())] - ); - }) - .await; -} - -/// A host that starts with Tor off must not have leaked the proxy address -/// to the HTTP port at all — `tor_proxy_address` is remembered for a later -/// `Intent::SetTorEnabled(true)`, but the port itself stays direct until then. -#[tokio::test] -async fn spawn_leaves_the_http_port_direct_when_tor_is_off() { - LocalSet::new() - .run_until(async { - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let spy = Rc::new(Spy::default()); - let recording = Rc::new(RecordingHttp::default()); - let ports = CorePorts { - http: Rc::clone(&recording) as Rc, - ..CorePorts::default() - }; - let _core = Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone.clone())), - proxy: Some("127.0.0.1:9050".to_string()), - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - ports, - spy, - Rc::new(FixedClock(RefCell::new(1_000_000))), - Rc::new(ZeroEntropy), - ) - .await; - - assert!(recording.proxy_calls.borrow().is_empty()); - }) - .await; -} - -/// Spawn a core with the given Tor settings; the caller points it at relays. -/// The `fast_reconnect` override keeps the FSM timers out of the test's way. -async fn core_with_tor( - phone: &Keypair, - spy: Rc, - ports: CorePorts, - proxy: Option, - tor: bool, -) -> Core { - Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone.clone())), - proxy, - tor, - device_label: None, - reconnect: fast_reconnect(), - }, - ports, - spy, - Rc::new(FixedClock(RefCell::new(1_000_000))), - Rc::new(ZeroEntropy), - ) - .await -} - -/// A SOCKS5 proxy on loopback that records every `CONNECT` target and, for a -/// reachable target, forwards the connection. A test asserts through it that -/// traffic expected to be proxied really is — and that traffic expected to be -/// direct never appears here. -struct SocksProxy { - /// `127.0.0.1:` — the proxy address a `CoreConfig` is given. - address: String, - connects: mpsc::UnboundedReceiver<(String, u16)>, -} - -impl SocksProxy { - async fn spawn() -> Self { - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let address = listener.local_addr().unwrap().to_string(); - let (tx, connects) = mpsc::unbounded_channel(); - tokio::task::spawn_local(async move { - while let Ok((socket, _)) = listener.accept().await { - tokio::task::spawn_local(serve_socks5(socket, tx.clone())); - } - }); - Self { address, connects } - } - - /// The next `CONNECT` the proxy saw, or `None` if none came within `within`. - async fn next_connect(&mut self, within: Duration) -> Option<(String, u16)> { - tokio::time::timeout(within, self.connects.recv()).await.ok().flatten() - } - - /// A `CONNECT` the proxy saw and nobody consumed yet. - fn pending_connect(&mut self) -> Option<(String, u16)> { - self.connects.try_recv().ok() - } -} - -/// Serve one SOCKS5 client: no-auth greeting, one `CONNECT`, then forward. -/// A `.onion` name cannot be resolved here, so it is refused the way a Tor -/// proxy refuses a service it cannot reach — the point for the tests is that -/// the request reached the proxy at all. -async fn serve_socks5( - mut client: tokio::net::TcpStream, - connects: mpsc::UnboundedSender<(String, u16)>, -) { - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - - let mut greeting = [0u8; 2]; - if client.read_exact(&mut greeting).await.is_err() || greeting[0] != 5 { - return; - } - let mut methods = vec![0u8; greeting[1] as usize]; - if client.read_exact(&mut methods).await.is_err() { - return; - } - if client.write_all(&[5, 0]).await.is_err() { - return; - } - - let mut request = [0u8; 4]; - if client.read_exact(&mut request).await.is_err() || request[0] != 5 || request[1] != 1 { - return; - } - let host = match request[3] { - 1 => { - let mut ip = [0u8; 4]; - if client.read_exact(&mut ip).await.is_err() { - return; - } - ip.iter().map(|b| b.to_string()).collect::>().join(".") - } - 3 => { - let mut len = [0u8; 1]; - if client.read_exact(&mut len).await.is_err() { - return; - } - let mut name = vec![0u8; len[0] as usize]; - if client.read_exact(&mut name).await.is_err() { - return; - } - String::from_utf8_lossy(&name).to_string() - } - 4 => { - let mut ip = [0u8; 16]; - if client.read_exact(&mut ip).await.is_err() { - return; - } - std::net::Ipv6Addr::from(ip).to_string() - } - _ => return, - }; - let mut port = [0u8; 2]; - if client.read_exact(&mut port).await.is_err() { - return; - } - let port = u16::from_be_bytes(port); - let _ = connects.send((host.clone(), port)); - - if host.ends_with(".onion") { - let _ = client.write_all(&[5, 4, 0, 1, 0, 0, 0, 0, 0, 0]).await; - return; - } - match tokio::net::TcpStream::connect((host.as_str(), port)).await { - Ok(mut upstream) => { - if client.write_all(&[5, 0, 0, 1, 0, 0, 0, 0, 0, 0]).await.is_err() { - return; - } - let _ = tokio::io::copy_bidirectional(&mut client, &mut upstream).await; - } - Err(_) => { - let _ = client.write_all(&[5, 4, 0, 1, 0, 0, 0, 0, 0, 0]).await; - } - } -} - -/// Tor is switched on while the core is already connected: the next relay -/// connection is dialled through the SOCKS5 proxy (no Nostr traffic takes a -/// direct path while Tor is on), Blossom uploads follow, and switching Tor -/// off redials relay traffic directly. -#[tokio::test] -async fn toggling_tor_at_runtime_moves_relay_and_http_traffic_through_the_proxy_and_back() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let spy = Rc::new(Spy::default()); - let recording = Rc::new(RecordingHttp::default()); - let mut proxy = SocksProxy::spawn().await; - let proxy_address = proxy.address.clone(); - - let core = core_with_tor( - &phone, - Rc::clone(&spy), - CorePorts { - http: Rc::clone(&recording) as Rc, - ..CorePorts::default() - }, - Some(proxy_address.clone()), - false, - ) - .await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.set_relays(vec![mock.url.clone()]); - core.start(); - eose_all(&mut mock).await; - while mock.try_accept() {} - assert!( - proxy.pending_connect().is_none(), - "with Tor off the relay connection must not touch the proxy" - ); - assert!(recording.proxy_calls.borrow().is_empty(), "with Tor off Blossom stays direct"); - - core.dispatch(Intent::SetTorEnabled(true)).await; - let relay = mock.url.trim_start_matches("ws://").to_string(); - let (relay_host, relay_port) = relay.split_once(':').unwrap(); - assert_eq!( - proxy.next_connect(Duration::from_secs(5)).await, - Some((relay_host.to_string(), relay_port.parse().unwrap())), - "enabling Tor must redial the relay through the proxy" - ); - assert!( - mock.next_accept(Duration::from_secs(5)).await, - "the proxied relay dial must reach the relay" - ); - assert_eq!( - recording.proxy_calls.borrow().as_slice(), - [Some(proxy_address.clone())], - "Blossom uploads must follow the same switch" - ); - assert!(proxy.pending_connect().is_none(), "one relay, one proxied dial"); - - core.dispatch(Intent::SetTorEnabled(false)).await; - assert!( - mock.next_accept(Duration::from_secs(5)).await, - "disabling Tor must redial the relay" - ); - assert!( - proxy.pending_connect().is_none(), - "a relay dial with Tor off must bypass the proxy" - ); - assert_eq!( - recording.proxy_calls.borrow().as_slice(), - [Some(proxy_address.clone()), None], - "Blossom is direct again once Tor is off" - ); - }) - .await; -} - -/// Enabling Tor at runtime swaps in the Tor reconnect timing: the connect -/// deadline must cover the slower dial the proxy just introduced. With the -/// clearnet deadline still in force, an attempt that is still dialling is -/// abandoned long before Tor's own transport would give up. -#[tokio::test] -async fn enabling_tor_at_runtime_widens_the_connect_deadline_to_tors() { - LocalSet::new() - .run_until(async { - // Accepts the TCP connection but never answers the WebSocket - // handshake, so the attempt stays `Connecting`. - let silent = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let relay = format!("ws://{}", silent.local_addr().unwrap()); - tokio::task::spawn_local(async move { - let mut held = Vec::new(); - while let Ok((socket, _)) = silent.accept().await { - held.push(socket); - } - }); - let proxy = SocksProxy::spawn().await; - let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone)), - proxy: Some(proxy.address.clone()), - tor: false, - device_label: None, - reconnect: DEFAULT_RECONNECT_CONFIG, - }, - CorePorts::default(), - Rc::new(Spy::default()), - Rc::clone(&clock) as Rc, - Rc::new(ZeroEntropy), - ) - .await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.set_relays(vec![relay]); - core.start(); - assert_eq!( - core.connection_status().await.0, - ConnectionStatus::Connecting, - "the attempt starts on the clearnet timing" - ); - - core.dispatch(Intent::SetTorEnabled(true)).await; - *clock.0.borrow_mut() += DEFAULT_RECONNECT_CONFIG.connect_deadline_ms + 1; - core.tick(); - assert_eq!( - core.connection_status().await.0, - ConnectionStatus::Connecting, - "enabling Tor must widen the connect deadline, not keep the clearnet one" - ); - - *clock.0.borrow_mut() = 1_000_000 + TOR_RECONNECT_CONFIG.connect_deadline_ms; - core.tick(); - assert_eq!( - core.connection_status().await.0, - ConnectionStatus::WaitingRetry, - "the Tor deadline still bounds an attempt that never reaches EOSE" - ); - }) - .await; -} - -/// With Tor on, a direct link's `.onion` endpoint goes through the proxy — -/// only Tor reaches it — while a LAN endpoint is dialled directly. A direct -/// link is the user's own bridge on their own network, not Nostr traffic. -#[tokio::test] -async fn with_tor_on_a_direct_link_proxies_its_onion_endpoint_and_dials_lan_directly() { - LocalSet::new() - .run_until(async { - let lan = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let lan_address = lan.local_addr().unwrap(); - let (lan_hit_tx, mut lan_hit_rx) = mpsc::unbounded_channel(); - tokio::task::spawn_local(async move { - let mut held = Vec::new(); - while let Ok((socket, _)) = lan.accept().await { - held.push(socket); - let _ = lan_hit_tx.send(()); - } - }); - let mut proxy = SocksProxy::spawn().await; - - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - state.machines.get_mut(&machine.pubkey_hex).unwrap().direct = Some(protocol::direct::DirectInfo { - endpoints: vec!["ws://abc.onion:7448".into(), format!("ws://{lan_address}")], - cert_sha256: None, - }); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - // No relay is configured: nothing but the direct link may reach - // the proxy, so its CONNECT log is unambiguous. - let core = core_with_tor( - &phone, - Rc::new(Spy::default()), - CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, - Some(proxy.address.clone()), - true, - ) - .await; - core.start(); - - assert_eq!( - proxy.next_connect(Duration::from_secs(5)).await, - Some(("abc.onion".to_string(), 7448)), - "the onion endpoint must be dialled through the proxy" - ); - tokio::time::timeout(Duration::from_secs(5), lan_hit_rx.recv()) - .await - .expect("the LAN endpoint must be dialled") - .expect("the LAN listener is alive"); - assert!( - proxy.pending_connect().is_none(), - "the LAN endpoint must be dialled directly, never through the proxy" - ); - }) - .await; -} - -/// Decrypt + decode a relay `EVENT` frame as a `PhoneToBridge` command from -/// `phone` to `machine` — `None` if the frame isn't an EVENT, isn't tagged -/// for `machine`, or doesn't decrypt/decode as one. -fn decode_command_frame( - frame: &str, - phone_pubkey: &str, - machine: &Keypair, -) -> Option { - let v: Vec = serde_json::from_str(frame).ok()?; - if v.first()? != "EVENT" { - return None; - } - let ev = v.get(1)?; - if ev.get("pubkey")? != &serde_json::json!(phone_pubkey) { - return None; - } - let tagged = ev.get("tags")?.as_array()?.iter().any(|t| { - t.get(0) == Some(&serde_json::json!("p")) - && t.get(1) == Some(&serde_json::json!(machine.pubkey_hex)) - }); - if !tagged { - return None; - } - let content = ev.get("content")?.as_str()?; - let plaintext = - protocol::crypto::decrypt_from(&machine.secret_key, phone_pubkey, content).ok()?; - protocol::codec::decode_phone_to_bridge(&plaintext).ok() -} - /// EOSE the three bridge subscriptions. All are named `cd-N` and replay /// in non-deterministic order. -async fn eose_all(mock: &mut MockRelay) { +pub(super) async fn eose_all(mock: &mut MockRelay) { let mut seen = 0; while seen < 3 { let frame = mock.next_frame().await; @@ -718,1422 +182,12 @@ async fn eose_all(mock: &mut MockRelay) { } } -#[tokio::test] -async fn start_subscribes_and_reports_connected_after_all_eose() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - - eose_all(&mut mock).await; - wait_for(|| spy.statuses.lock().unwrap().last().is_some_and(|(s, _)| *s == ConnectionStatus::Connected)) - .await; - - let statuses = spy.statuses.lock().unwrap().clone(); - assert!( - statuses.iter().any(|(s, _)| *s == ConnectionStatus::Connecting), - "{statuses:?}" - ); - assert_eq!(statuses.last().unwrap().0, ConnectionStatus::Connected); - // The relay that actually opened is what Settings' per-relay - // dot should read — not an empty placeholder. - assert_eq!( - spy.connected_relays.lock().unwrap().last().unwrap(), - &vec![mock.url.clone()], - ); - }) - .await; -} - -/// One relay of two dying leaves overall `ConnectionStatus` untouched -/// (`NostrClient::on_close` only fires once EVERY relay for a -/// subscription is dead), so `dispatch`'s status-transition gate never -/// runs. The transport reports the relay going down itself, so Settings' -/// per-relay dot follows at once rather than at the watchdog's next tick. -#[tokio::test] -async fn a_relay_dying_while_another_survives_is_reported_at_once() { - LocalSet::new() - .run_until(async { - let mut mock1 = mock_relay().await; - let mut mock2 = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone.clone())), - proxy: None, - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - CorePorts::default(), - Rc::clone(&spy) as Rc, - Rc::new(FixedClock(RefCell::new(1_000_000))), - Rc::new(ZeroEntropy), - ) - .await; - core.set_relays(vec![mock1.url.clone(), mock2.url.clone()]); - - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - - eose_all(&mut mock1).await; - eose_all(&mut mock2).await; - wait_for(|| spy.connected_relays.lock().unwrap().last().is_some_and(|r| r.len() == 2)).await; - - let before = spy.connected_relays.lock().unwrap().last().unwrap().clone(); - assert_eq!(before.len(), 2, "{before:?}"); - - // Real time only, far short of the watchdog's 30 s tick. - mock2.close(); - wait_for(|| { - spy.connected_relays - .lock() - .unwrap() - .last() - .is_some_and(|r| r == &vec![mock1.url.clone()]) - }) - .await; - - let after = spy.connected_relays.lock().unwrap().last().unwrap().clone(); - assert_eq!(after, vec![mock1.url.clone()], "{after:?}"); - }) - .await; -} - -#[tokio::test] -async fn a_bridge_message_is_decrypted_decoded_and_delivered() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - - let msg = protocol::codec::decode_bridge_to_phone( - r#"{"v":12,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#, - ) - .unwrap(); - let plaintext = encode_bridge_to_phone(&msg); - let ct = protocol::crypto::encrypt_to( - &machine.secret_key, - &phone.pubkey_hex, - &plaintext, - ) - .unwrap(); - let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) - .tag(recipient(&machine, &phone.pubkey_hex)) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - mock.push(format!( - r#"["EVENT","cd-3",{}]"#, - ::as_json(&event) - )); - wait_for(|| spy.messages.lock().unwrap().len() == 1).await; - - let messages = spy.messages.lock().unwrap(); - assert_eq!(messages.len(), 1, "{messages:?}"); - assert_eq!(messages[0].0, machine.pubkey_hex); - assert!(matches!(messages[0].1, BridgeToPhone::InputAck(_))); - }) - .await; -} - -#[tokio::test] -async fn a_bridge_of_another_version_is_marked_until_it_is_read_again() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - // Paired, so the machines store knows it. - core.dispatch(Intent::BeginManualPairing { - npub: machine.npub.clone(), - token: "tok".into(), - relays: mock.url.clone(), - label: "laptop".into(), - }) - .await; - let sub = drain_traffic_resubscribe(&mut mock).await; - let ack = BridgeToPhone::PairAck(protocol::events::PairAckMsg { - machine: "laptop".into(), - ok: true, - reason: None, - relays: None, - host: None, - }); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &ack); - let key = machine.pubkey_hex.clone(); - wait_for_async(|| { - let core = core.clone(); - let key = key.clone(); - async move { core.machines_view().await.machines.contains_key(&key) } - }) - .await; - let sub = drain_traffic_resubscribe(&mut mock).await; - - let push_raw = |plaintext: &str| { - let ct = protocol::crypto::encrypt_to(&machine.secret_key, &phone.pubkey_hex, plaintext).unwrap(); - let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) - .tag(recipient(&machine, &phone.pubkey_hex)) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - mock.push(format!(r#"["EVENT","{sub}",{}]"#, ::as_json(&event))); - }; - let mark = || async { - core.machines_view().await.machines[&machine.pubkey_hex].incompatible.map(|i| i.theirs) - }; - assert_eq!(mark().await, None); - - push_raw(r#"{"v":13,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#); - let key = machine.pubkey_hex.clone(); - wait_for_async(|| { - let core = core.clone(); - let key = key.clone(); - async move { - core.machines_view().await.machines[&key].incompatible.map(|i| i.theirs) == Some(Some(13)) - } - }) - .await; - assert_eq!(mark().await, Some(Some(13))); - let delivered = spy.messages.lock().unwrap().len(); - - push_raw(r#"{"v":12,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#); - wait_for_async(|| { - let core = core.clone(); - let key = key.clone(); - async move { core.machines_view().await.machines[&key].incompatible.is_none() } - }) - .await; - assert_eq!(mark().await, None); - assert_eq!(spy.messages.lock().unwrap().len(), delivered + 1); - }) - .await; -} - -#[tokio::test] -async fn a_notify_worthy_event_while_backgrounded_fires_the_ping_core_event() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - - // Backgrounded — `decide_ping` wants a chime unconditionally - // once the app isn't visible, for any notify-worthy event. - core.pause(); - - let msg = protocol::codec::decode_bridge_to_phone( - r#"{"v":12,"type":"session-failed","pendingId":"p1","reason":"boom"}"#, - ) - .unwrap(); - let plaintext = encode_bridge_to_phone(&msg); - let ct = protocol::crypto::encrypt_to( - &machine.secret_key, - &phone.pubkey_hex, - &plaintext, - ) - .unwrap(); - let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) - .tag(recipient(&machine, &phone.pubkey_hex)) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - mock.push(format!( - r#"["EVENT","cd-1",{}]"#, - ::as_json(&event) - )); - wait_for(|| spy.events.lock().unwrap().contains(&CoreEvent::Ping)).await; - - let events = spy.events.lock().unwrap(); - assert!(events.contains(&CoreEvent::Ping), "{events:?}"); - }) - .await; -} - -#[tokio::test] -async fn create_folder_round_trips_to_a_matching_folder_ack_core_event() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - - core.dispatch(Intent::CreateFolder { - machine: machine.pubkey_hex.clone(), - path: "sub/dir".into(), - root: None, - request_id: "req-1".into(), - }) - .await; - - // The dispatched command reaches the bridge as a real, signed - // create-folder event — same publish path every other Intent uses. - let frame = mock.next_frame().await; - let v: serde_json::Value = serde_json::from_str(&frame).unwrap(); - assert_eq!(v[0], "EVENT"); - - let msg = protocol::codec::decode_bridge_to_phone( - r#"{"v":12,"type":"folder-ack","requestId":"req-1","success":true,"path":"sub/dir"}"#, - ) - .unwrap(); - let plaintext = encode_bridge_to_phone(&msg); - let ct = protocol::crypto::encrypt_to( - &machine.secret_key, - &phone.pubkey_hex, - &plaintext, - ) - .unwrap(); - let event = nostr::EventBuilder::new(nostr::Kind::Custom(RESPONSE_KIND), ct) - .tag(recipient(&machine, &phone.pubkey_hex)) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - mock.push(format!( - r#"["EVENT","cd-1",{}]"#, - ::as_json(&event) - )); - wait_for(|| { - spy.events.lock().unwrap().iter().any(|e| matches!( - e, - CoreEvent::FolderAck { request_id, success: true, path: Some(p), error: None } - if request_id == "req-1" && p == "sub/dir" - )) - }) - .await; - - let events = spy.events.lock().unwrap(); - assert!( - events.iter().any(|e| matches!( - e, - CoreEvent::FolderAck { request_id, success: true, path: Some(p), error: None } - if request_id == "req-1" && p == "sub/dir" - )), - "{events:?}", - ); - }) - .await; -} - -/// A fresh store asks for stored responses from its first start (less the -/// grace), not the identity's whole history: what earlier installs were -/// sent is under keys this one does not hold. -#[tokio::test] -async fn a_fresh_store_asks_for_stored_responses_from_its_start() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.start(); - let since = loop { - let frame = mock.next_frame().await; - let v: Vec = serde_json::from_str(&frame).unwrap(); - if v[0] == "REQ" && v[2]["kinds"][0] == RESPONSE_KIND { - break v[2]["since"].as_i64(); - } - }; - // The test clock reads 1_000 s. - assert_eq!(since, Some(1_000 - crate::nostr_client::STORED_SINCE_GRACE_SECONDS)); - }) - .await; -} - -/// A stored-kind event (4516/30515) advancing `last_stored_seen` used to -/// update only the in-memory `LoopHost` cursor — a restart re-hydrated -/// from the `Kv` at 0 and re-fetched the peer's ENTIRE stored history -/// instead of resuming from where it left off, on every single restart. -#[tokio::test] -async fn a_stored_kind_event_persists_the_cursor_to_kv() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let kv = Rc::new(MemoryKv::new()); - let ports = CorePorts { kv: Rc::clone(&kv) as Rc, ..CorePorts::default() }; - let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - - assert_eq!(kv.get(LAST_STORED_SEEN_KEY).await, None); - - let msg = protocol::codec::decode_bridge_to_phone( - r#"{"v":12,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#, - ) - .unwrap(); - let plaintext = encode_bridge_to_phone(&msg); - let ct = protocol::crypto::encrypt_to( - &machine.secret_key, - &phone.pubkey_hex, - &plaintext, - ) - .unwrap(); - let event = nostr::EventBuilder::new(nostr::Kind::Custom(RESPONSE_KIND), ct) - .tag(recipient(&machine, &phone.pubkey_hex)) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - let created_at = event.created_at.as_secs(); - // "cd-1" is a real, currently-open subscription id (one of the - // three bridge filters `eose_all` just drained) — an id with no - // matching subscription is silently dropped by the transport, - // same as a real relay addressing a closed sub. - mock.push(format!( - r#"["EVENT","cd-1",{}]"#, - ::as_json(&event) - )); - settle().await; - // Held back by the write debounce... - assert_eq!(kv.get(LAST_STORED_SEEN_KEY).await, None); - // ...until it elapses. - tokio::time::sleep(WRITE_DEBOUNCE + Duration::from_millis(200)).await; - assert_eq!( - kv.get(LAST_STORED_SEEN_KEY).await, - Some(created_at.to_string()), - ); - - // A later event is written the moment the app is backgrounded. - let newer = nostr::EventBuilder::new(nostr::Kind::Custom(RESPONSE_KIND), "x") - .custom_created_at(nostr::Timestamp::from(created_at + 10)) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - mock.push(format!( - r#"["EVENT","cd-1",{}]"#, - ::as_json(&newer) - )); - settle().await; - core.pause(); - settle().await; - assert_eq!( - kv.get(LAST_STORED_SEEN_KEY).await, - Some((created_at + 10).to_string()), - ); - }) - .await; -} - -#[tokio::test] -async fn a_socket_drop_backs_off_and_reconnects() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - wait_connected(&core).await; - - mock.close(); - // fast_reconnect base is 40ms: the backoff and its retry follow. - wait_for(|| { - let statuses = spy.statuses.lock().unwrap(); - statuses.iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry) - && statuses.iter().filter(|(s, _)| *s == ConnectionStatus::Connecting).count() >= 2 - }) - .await; - - let statuses = spy.statuses.lock().unwrap().clone(); - assert!( - statuses.iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry), - "{statuses:?}" - ); - assert!( - statuses.iter().filter(|(s, _)| *s == ConnectionStatus::Connecting).count() >= 2, - "expected a second Connecting after backoff: {statuses:?}" - ); - }) - .await; -} - -#[tokio::test] -async fn a_connect_that_never_reaches_eose_is_abandoned_and_retried() { - LocalSet::new() - .run_until(async { - // The relay accepts the socket and reads the REQs, but never - // sends EOSE — the FSM's `SocketOpen`, and the only thing that - // ends `Connecting`. - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let spy = Rc::new(Spy::default()); - let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); - let core = core_for_ports_clock( - &mock, - &phone, - Rc::clone(&spy), - CorePorts::default(), - Rc::clone(&clock), - ) - .await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.start(); - wait_status(&core, ConnectionStatus::Connecting).await; - assert_eq!(core.connection_status().await.0, ConnectionStatus::Connecting); - - *clock.0.borrow_mut() += connect_deadline() + 1_000; - core.tick(); - // fast_reconnect's 40ms retry has fired by now. - wait_for(|| { - let statuses: Vec = - spy.statuses.lock().unwrap().iter().map(|(s, _)| *s).collect(); - statuses.windows(2).any(|w| w == [ConnectionStatus::WaitingRetry, ConnectionStatus::Connecting]) - }) - .await; - - let statuses: Vec = - spy.statuses.lock().unwrap().iter().map(|(s, _)| *s).collect(); - assert!(statuses.contains(&ConnectionStatus::WaitingRetry), "{statuses:?}"); - assert!( - statuses.windows(2).any(|w| w == [ConnectionStatus::WaitingRetry, ConnectionStatus::Connecting]), - "the abandoned attempt went through backoff and retried: {statuses:?}" - ); - }) - .await; -} - -#[tokio::test] -async fn a_paused_connect_is_not_abandoned_and_a_resume_restarts_its_clock() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let spy = Rc::new(Spy::default()); - let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); - let core = core_for_ports_clock( - &mock, - &phone, - Rc::clone(&spy), - CorePorts::default(), - clock.clone(), - ) - .await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.start(); - wait_status(&core, ConnectionStatus::Connecting).await; - assert_eq!(core.connection_status().await.0, ConnectionStatus::Connecting); - - // Hidden past the 500ms visibility debounce, then dozing: wall - // time moves beyond the deadline while the app runs nothing. - core.pause(); - tokio::time::sleep(Duration::from_millis(600)).await; - *clock.0.borrow_mut() += connect_deadline() + 60_000; - core.tick(); - assert_eq!( - core.connection_status().await.0, - ConnectionStatus::Connecting, - "paused time is not spent against the connect attempt" - ); - - // Resume re-dials while still Connecting: that is a new attempt, - // with a fresh window rather than the abandoned one's leftovers. - // The re-dial runs on the fake clock, so let the loop take it in - // before moving that clock. - core.resume(); - settle().await; - *clock.0.borrow_mut() += connect_deadline() - 1_000; - core.tick(); - assert_eq!( - core.connection_status().await.0, - ConnectionStatus::Connecting, - "the resumed attempt gets its own window" - ); - - *clock.0.borrow_mut() += 2_000; - core.tick(); - wait_for(|| { - spy.statuses.lock().unwrap().iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry) - }) - .await; - assert!( - spy.statuses.lock().unwrap().iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry), - "the resumed attempt is abandoned once its own deadline passes" - ); - }) - .await; -} - -#[tokio::test] -async fn stop_is_terminal_no_reconnect() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.start(); - eose_all(&mut mock).await; - wait_connected(&core).await; - - core.stop(); - wait_status(&core, ConnectionStatus::Stopped).await; - mock.close(); - // Nothing after Stopped, even when the socket closes. - settle().await; - - let statuses = spy.statuses.lock().unwrap().clone(); - assert_eq!(statuses.last().unwrap().0, ConnectionStatus::Stopped); - // nothing after Stopped - let after_stop = statuses - .iter() - .skip_while(|(s, _)| *s != ConnectionStatus::Stopped) - .count(); - assert_eq!(after_stop, 1, "status changed after Stop: {statuses:?}"); - }) - .await; -} - -#[tokio::test] -async fn connection_status_query_reflects_the_live_fsm() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - - assert_eq!(core.connection_status().await.0, ConnectionStatus::Idle); - core.start(); - eose_all(&mut mock).await; - wait_connected(&core).await; - assert_eq!(core.connection_status().await.0, ConnectionStatus::Connected); - core.stop(); - wait_status(&core, ConnectionStatus::Stopped).await; - assert_eq!(core.connection_status().await.0, ConnectionStatus::Stopped); - }) - .await; -} - -/// A machine paired in a PRIOR run is only in the persisted `Kv`, never in -/// `set_machines` — production code never calls that (only a live pairing -/// route/intent populates the subscription author list reactively). A -/// fresh `Core::spawn()` used to leave that list empty for the rest of the -/// process's life unless a NEW pairing happened to run in it: -/// `NostrClient::connect()` treats an empty author list as vacuous and -/// opens zero of the three bridge subscriptions, so a returning phone -/// would never see another heartbeat or session update from a machine it -/// paired before this boot — the root cause behind "the machine dot never -/// leaves orange, and a bridge-confirmed session never appears, except -/// right after pairing". -#[tokio::test] -async fn a_machine_paired_in_a_prior_run_is_resubscribed_on_a_fresh_boot_without_set_machines() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - heard_without_session_keys(&mut state, &machine.pubkey_hex); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let ports = CorePorts { kv: Rc::new(kv), ..CorePorts::default() }; - - let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - // Deliberately no `core.set_machines(...)` — this is the part - // of the boot sequence a real app reopen actually exercises. - core.start(); - - // Hangs (and `next_frame` panics at its 2s budget) if the - // subscription author list is still empty at this point. - eose_all(&mut mock).await; - - // A real heartbeat from that same machine must still reach the - // machines view — proving the subscription actually scopes to - // it, not just that some vacuous socket opened. - let sessions_json = - r#"{"v":12,"type":"sessions","machine":"bridge","sessions":[],"agents":[]}"#; - let msg = protocol::codec::decode_bridge_to_phone(sessions_json).unwrap(); - let plaintext = encode_bridge_to_phone(&msg); - let ct = protocol::crypto::encrypt_to( - &machine.secret_key, - &phone.pubkey_hex, - &plaintext, - ) - .unwrap(); - let event = - nostr::EventBuilder::new(nostr::Kind::Custom(SESSION_LIST_KIND), ct) - .tag(recipient(&machine, &phone.pubkey_hex)) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - mock.push(format!( - r#"["EVENT","cd-1",{}]"#, - ::as_json(&event) - )); - let key = machine.pubkey_hex.clone(); - wait_for_async(|| { - let core = core.clone(); - let key = key.clone(); - async move { - core.machines_view() - .await - .machines - .get(&key) - .is_some_and(|m| m.last_heartbeat_at.is_some()) - } - }) - .await; - - let view = core.machines_view().await; - let m = view.machines.get(&machine.pubkey_hex).expect("machine still known"); - assert!(m.last_heartbeat_at.is_some(), "heartbeat never reached the view"); - }) - .await; -} - -#[tokio::test] -async fn set_relays_repoints_the_transport_to_the_new_relay() { - LocalSet::new() - .run_until(async { - let mut first = mock_relay().await; - let mut second = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let core = core_for(&first, &phone, Rc::new(Spy::default())).await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.start(); - eose_all(&mut first).await; - wait_connected(&core).await; - - core.set_relays(vec![second.url.clone()]); - // the new relay gets the three REQs; drain them - for _ in 0..3 { - let req = second.next_frame().await; - assert!(req.starts_with(r#"["REQ""#), "got {req}"); - } - }) - .await; -} - -#[tokio::test] -async fn keepalive_probes_the_relays_and_keeps_a_healthy_connection() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.start(); - eose_all(&mut mock).await; - wait_connected(&core).await; - - // The mock answers the probe's ping, so the check is quick - // and leaves the connection as it was. - tokio::time::timeout(Duration::from_secs(2), core.keepalive()) - .await - .expect("keepalive resolves once the relay answers"); - let (status, _, connected) = core.connection_status().await; - assert_eq!(status, ConnectionStatus::Connected); - assert_eq!(connected, vec![mock.url.clone()]); - }) - .await; -} - -#[tokio::test] -async fn a_30515_heartbeat_reaches_the_fsm_even_if_it_is_not_ours() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.start(); - eose_all(&mut mock).await; - - // a signed 30515 from a stranger: no decode (not a paired - // machine), but it must not crash and must not deliver. - let stranger = generate_keypair(); - let event = - nostr::EventBuilder::new(nostr::Kind::Custom(SESSION_LIST_KIND), "garbage") - .sign_with_keys(&nostr::Keys::new(stranger.secret_key.clone())) - .unwrap(); - mock.push(format!( - r#"["EVENT","cd-1",{}]"#, - ::as_json(&event) - )); - settle().await; - - assert!(spy.messages.lock().unwrap().is_empty()); - }) - .await; -} - -// --- the composed store layer --- - -#[tokio::test] -async fn connection_view_query_reflects_the_live_status() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - - assert_eq!(core.connection_view().await.unwrap().status, "idle"); - core.start(); - eose_all(&mut mock).await; - wait_connected(&core).await; - assert_eq!(core.connection_view().await.unwrap().status, "connected"); - }) - .await; -} - -#[tokio::test] -async fn an_intent_becomes_a_signed_command_on_the_wire() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - - core.dispatch(Intent::Interrupt { - machine: machine.pubkey_hex.clone(), - session_id: "s1".into(), - }) - .await; - - // among the published EVENTs find the one wrapped for the - // machine. - let mut found = false; - for _ in 0..6 { - let frame = mock.next_frame().await; - let v: Vec = serde_json::from_str(&frame).unwrap(); - if v[0] != "EVENT" { - continue; - } - let ev = &v[1]; - if ev["pubkey"] == serde_json::json!(phone.pubkey_hex) - && ev["tags"] - .as_array() - .unwrap() - .iter() - .any(|t| t[0] == "p" && t[1] == machine.pubkey_hex) - { - found = true; - break; - } - } - assert!(found, "no command EVENT p-tagged to the machine"); - }) - .await; -} - -#[tokio::test] -async fn editing_a_machines_relays_repoints_the_transport() { - LocalSet::new() - .run_until(async { - let mut first = mock_relay().await; - let mut second = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - // A machine paired in an earlier run, reached over `first`. - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[first.url.clone()]); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let spy = Rc::new(Spy::default()); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone.clone())), - proxy: None, - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, - Rc::clone(&spy) as Rc, - Rc::new(FixedClock(RefCell::new(1_000_000))), - Rc::new(ZeroEntropy), - ) - .await; - // Dialled from the stored machine alone: no relay configured. - core.start(); - eose_all(&mut first).await; - wait_connected(&core).await; - - core.dispatch(Intent::SetMachineRelays { - machine: machine.pubkey_hex.clone(), - relays: vec![second.url.clone()], - }) - .await; - for _ in 0..3 { - let req = second.next_frame().await; - assert!(req.starts_with(r#"["REQ""#), "got {req}"); - } - assert!(spy.events.lock().unwrap().contains(&CoreEvent::StateChanged { slice: SliceId::Machines })); - }) - .await; -} - -#[tokio::test] -async fn select_session_intent_updates_the_ui_view_and_emits_a_state_changed_event() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - - core.dispatch(Intent::SelectSession { - machine: "m1".into(), - session_id: Some("s1".into()), - }) - .await; - - let uv = core.ui_view().await; - assert_eq!(uv.selected_machine.as_deref(), Some("m1")); - assert_eq!(uv.selected_session.as_deref(), Some("s1")); - - { - let events = spy.events.lock().unwrap(); - assert!(events.contains(&CoreEvent::StateChanged { slice: SliceId::Ui })); - } - }) - .await; -} - -#[tokio::test] -async fn selecting_a_session_cancels_its_notification_tag() { - // CDX-026c: opening a session the user was notified about clears - // every notification filed under its tag. - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let notifier = RecordingNotifier::new(); - let ports = CorePorts { - notifier: Rc::new(notifier.clone()), - ..CorePorts::default() - }; - let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - - core.dispatch(Intent::SelectSession { - machine: "m1".into(), - session_id: Some("s1".into()), - }) - .await; - - assert_eq!( - notifier.cancelled(), - vec![client_core::notifications::session_notify_tag("m1", "s1")] - ); - }) - .await; -} - -#[tokio::test] -async fn coming_back_to_a_session_cancels_what_was_notified_while_away() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let notifier = RecordingNotifier::new(); - let ports = CorePorts { - notifier: Rc::new(notifier.clone()), - ..CorePorts::default() - }; - let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - core.dispatch(Intent::SelectSession { - machine: "m1".into(), - session_id: Some("s1".into()), - }) - .await; - core.pause(); - core.resume(); - wait_for(|| notifier.cancelled().len() == 2).await; - let tag = client_core::notifications::session_notify_tag("m1", "s1"); - assert_eq!(notifier.cancelled(), [tag.clone(), tag]); - - // Left for the list: coming back clears nothing. - core.dispatch(Intent::SelectSession { - machine: "m1".into(), - session_id: None, - }) - .await; - core.pause(); - core.resume(); - settle().await; - assert_eq!(notifier.cancelled().len(), 2); - }) - .await; -} - -#[tokio::test] -async fn set_plan_approval_choice_intent_updates_the_ui_view() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - - core.dispatch(Intent::SetPlanApprovalChoice { - card_id: "card1".into(), - key: "2".into(), - }) - .await; - - let uv = core.ui_view().await; - assert_eq!(uv.plan_approval_choices.get("card1").map(String::as_str), Some("2")); - }) - .await; -} - -/// Read frames from `mock` until `matches` returns one, ACK-ing every -/// `EVENT` along the way (by its real id) so a concurrent -/// `publish_confirmed` settles immediately instead of riding out the full -/// confirm budget — the image upload awaits its own publish, so the test -/// must answer it while `dispatch` is still in flight. -async fn find_command_frame( - mock: &mut MockRelay, - phone_pubkey: &str, - machine: &Keypair, - matches: impl Fn(&PhoneToBridge) -> bool, -) -> PhoneToBridge { - for _ in 0..16 { - let frame = mock.next_frame().await; - let v: Vec = serde_json::from_str(&frame).unwrap(); - if v[0] == "EVENT" { - if let Some(id) = v[1]["id"].as_str() { - mock.push(format!(r#"["OK","{id}",true,""]"#)); - } - } - if let Some(msg) = decode_command_frame(&frame, phone_pubkey, machine) { - if matches(&msg) { - return msg; - } - } - } - panic!("no matching command reached the machine within 16 frames"); -} - -#[tokio::test] -async fn sending_a_session_image_uploads_to_blossom_and_publishes_the_command() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let ports = CorePorts { - http: Rc::new(OkHttp), - ..CorePorts::default() - }; - let core = - core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - wait_connected(&core).await; - - core.dispatch(Intent::SetBlossomServer("https://blossom.example".into())).await; - - let core2 = core.clone(); - let machine_pubkey = machine.pubkey_hex.clone(); - let dispatched = tokio::task::spawn_local(async move { - core2 - .dispatch(Intent::SendSessionFile(SessionFileSend { - machine: machine_pubkey, - session_id: "s1".into(), - text: "look at this".into(), - data: b"png bytes here".to_vec(), - filename: "photo.png".into(), - mime_type: "image/png".into(), - })) - .await; - }); - - let msg = find_command_frame( - &mut mock, - &phone.pubkey_hex, - &machine, - |m| matches!(m, PhoneToBridge::UploadFile(_)), - ) - .await; - dispatched.await.unwrap(); - - match msg { - PhoneToBridge::UploadFile(UploadFileMsg::Blossom(m)) => { - assert_eq!(m.session_id, "s1"); - assert_eq!(m.text, "look at this"); - assert_eq!(m.filename, "photo.png"); - assert_eq!(m.size_bytes, b"png bytes here".len() as u64); - assert!(!m.hash.is_empty()); - } - other => panic!("Blossom succeeded — should not chunk: {other:?}"), - } - }) - .await; -} - -#[tokio::test] -async fn with_no_blossom_server_a_file_goes_through_the_relays() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let ports = CorePorts { - http: Rc::new(OkHttp), - ..CorePorts::default() - }; - let core = - core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - wait_connected(&core).await; - - let core2 = core.clone(); - let machine_pubkey = machine.pubkey_hex.clone(); - let dispatched = tokio::task::spawn_local(async move { - core2 - .dispatch(Intent::SendSessionFile(SessionFileSend { - machine: machine_pubkey, - session_id: "s1".into(), - text: "look at this".into(), - data: b"%PDF-1.7 bytes".to_vec(), - filename: "notes.pdf".into(), - mime_type: "application/pdf".into(), - })) - .await; - }); - - let msg = find_command_frame( - &mut mock, - &phone.pubkey_hex, - &machine, - |m| matches!(m, PhoneToBridge::UploadFile(_)), - ) - .await; - dispatched.await.unwrap(); - - // The HTTP port would accept an upload: only the missing - // server keeps the file off Blossom. - match msg { - PhoneToBridge::UploadFile(UploadFileMsg::Chunk(m)) => { - assert_eq!((m.filename.as_str(), m.mime_type.as_str()), ("notes.pdf", "application/pdf")); - assert_eq!(m.total_chunks, 1); - } - other => panic!("no Blossom server is set: {other:?}"), - } - }) - .await; -} - -#[tokio::test] -async fn sending_a_session_image_falls_back_to_chunks_when_blossom_is_unreachable() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let ports = CorePorts { - http: Rc::new(FailHttp), - ..CorePorts::default() - }; - let core = - core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - wait_connected(&core).await; - - core.dispatch(Intent::SetBlossomServer("https://blossom.example".into())).await; - - let core2 = core.clone(); - let machine_pubkey = machine.pubkey_hex.clone(); - let dispatched = tokio::task::spawn_local(async move { - core2 - .dispatch(Intent::SendSessionFile(SessionFileSend { - machine: machine_pubkey, - session_id: "s1".into(), - text: "a caption".into(), - data: b"small image bytes".to_vec(), - filename: "photo.jpg".into(), - mime_type: "image/jpeg".into(), - })) - .await; - }); - - // Step through the upload's retry backoff (1 s, then 2 s) - // on paused time; each sleep is only armed once the attempt - // before it has failed, hence one advance per step. - tokio::time::pause(); - for _ in 0..3 { - for _ in 0..20 { - tokio::task::yield_now().await; - } - tokio::time::advance(Duration::from_millis(2_100)).await; - } - tokio::time::resume(); - - let msg = find_command_frame( - &mut mock, - &phone.pubkey_hex, - &machine, - |m| matches!(m, PhoneToBridge::UploadFile(_)), - ) - .await; - dispatched.await.unwrap(); - - match msg { - PhoneToBridge::UploadFile(UploadFileMsg::Chunk(m)) => { - assert_eq!(m.session_id, "s1"); - assert_eq!(m.chunk_index, 0); - assert_eq!(m.total_chunks, 1); // well under 35 KB - assert_eq!(m.text, "a caption"); - } - other => panic!("blossom is unreachable in this test: {other:?}"), - } - }) - .await; -} - -#[tokio::test] -async fn the_connection_status_change_emits_a_state_changed_event() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![generate_keypair().pubkey_hex]); - core.start(); - eose_all(&mut mock).await; - wait_for(|| { - spy.events.lock().unwrap().contains(&CoreEvent::StateChanged { - slice: SliceId::Connection, - }) - }) - .await; - - let events = spy.events.lock().unwrap(); - assert!(events.contains(&CoreEvent::StateChanged { - slice: SliceId::Connection - })); - }) - .await; -} - -#[tokio::test] -async fn a_session_pending_message_populates_the_view_and_emits_a_state_changed_event() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - - let msg = protocol::codec::decode_bridge_to_phone( - r#"{"v":12,"type":"session-pending","pendingId":"p1","machine":"devbox","createdAt":"2026-01-01T00:00:00.000Z"}"#, - ) - .unwrap(); - let plaintext = encode_bridge_to_phone(&msg); - let ct = protocol::crypto::encrypt_to( - &machine.secret_key, - &phone.pubkey_hex, - &plaintext, - ) - .unwrap(); - let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) - .tag(recipient(&machine, &phone.pubkey_hex)) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - mock.push(format!( - r#"["EVENT","cd-3",{}]"#, - ::as_json(&event) - )); - wait_for(|| { - spy.events.lock().unwrap().contains(&CoreEvent::StateChanged { - slice: SliceId::PendingSessions, - }) - }) - .await; - - { - let events = spy.events.lock().unwrap(); - assert!(events.contains(&CoreEvent::StateChanged { - slice: SliceId::PendingSessions - })); - } - - let view = core.pending_sessions_view().await; - let placeholder = view.pending.get("p1").expect("placeholder in the view"); - assert_eq!(placeholder.machine_name, "devbox"); - }) - .await; -} - -#[tokio::test] -async fn dismiss_pending_session_removes_it_and_emits_a_state_changed_event() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - - let msg = protocol::codec::decode_bridge_to_phone( - r#"{"v":12,"type":"session-failed","pendingId":"p1","reason":"boom"}"#, - ) - .unwrap(); - let plaintext = encode_bridge_to_phone(&msg); - let ct = protocol::crypto::encrypt_to( - &machine.secret_key, - &phone.pubkey_hex, - &plaintext, - ) - .unwrap(); - let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) - .tag(recipient(&machine, &phone.pubkey_hex)) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - mock.push(format!( - r#"["EVENT","cd-3",{}]"#, - ::as_json(&event) - )); - wait_for_async(|| { - let core = core.clone(); - async move { core.pending_sessions_view().await.pending.contains_key("p1") } - }) - .await; - assert!(core.pending_sessions_view().await.pending.contains_key("p1")); - - core.dispatch(Intent::DismissPendingSession { - pending_id: "p1".into(), - }) - .await; - - assert!(!core.pending_sessions_view().await.pending.contains_key("p1")); - let events = spy.events.lock().unwrap(); - assert!(events.contains(&CoreEvent::StateChanged { - slice: SliceId::PendingSessions - })); - }) - .await; -} - -#[tokio::test] -async fn an_output_message_populates_the_transcript_view_and_emits_transcript_appended() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - - let msg = protocol::codec::decode_bridge_to_phone( - r#"{"v":12,"type":"output","sessionId":"s1","seq":1,"entries":[{"entryType":"text","role":"agent","text":"hi","timestamp":"t"}]}"#, - ) - .unwrap(); - let plaintext = encode_bridge_to_phone(&msg); - let ct = protocol::crypto::encrypt_to( - &machine.secret_key, - &phone.pubkey_hex, - &plaintext, - ) - .unwrap(); - let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) - .tag(recipient(&machine, &phone.pubkey_hex)) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - mock.push(format!( - r#"["EVENT","cd-3",{}]"#, - ::as_json(&event) - )); - wait_for(|| { - spy.events.lock().unwrap().contains(&CoreEvent::TranscriptAppended { - machine: machine.pubkey_hex.clone(), - session_id: "s1".to_string(), - }) - }) - .await; - - let view = core.transcript_view(machine.pubkey_hex.clone(), "s1".to_string()).await; - assert_eq!(view.rows.len(), 1); - assert_eq!(view.rows[0].seq, 1); - assert_eq!(view.sync.local_high, 1); - - let events = spy.events.lock().unwrap(); - assert!(events.contains(&CoreEvent::TranscriptAppended { - machine: machine.pubkey_hex, - session_id: "s1".to_string(), - })); - }) - .await; -} - -#[tokio::test] -async fn transcript_view_of_an_unknown_session_is_the_honest_empty_default() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - - let view = core.transcript_view("m1".to_string(), "s-never-seen".to_string()).await; - assert!(view.rows.is_empty()); - assert!(view.have_ranges.is_empty()); - assert_eq!(view.sync.local_high, 0); - assert!(view.sync.contiguous); - }) - .await; -} - /// A resubscribe triggered by an authors-filter change (a new pairing /// candidate, a freshly paired machine) re-does the phone's 3 traffic /// filters: this drains exactly 3 REQs (skipping the CLOSE frames for the /// superseded subs along the way) and returns one sub_id the router now /// has open. -async fn drain_traffic_resubscribe(mock: &mut MockRelay) -> String { +pub(super) async fn drain_traffic_resubscribe(mock: &mut MockRelay) -> String { let mut seen = 0; let mut sub_id = String::new(); while seen < 3 { @@ -2156,7 +210,7 @@ async fn drain_traffic_resubscribe(mock: &mut MockRelay) -> String { /// tears down the old subs and opens new ones with new ids, so the caller /// must hand in a sub_id drained from the CURRENT `eose_all` round, not /// one left over from an earlier one. -fn push_bridge_to_phone_event( +pub(super) fn push_bridge_to_phone_event( mock: &MockRelay, machine: &protocol::crypto::Keypair, phone_pubkey_hex: &str, @@ -2168,7 +222,7 @@ fn push_bridge_to_phone_event( /// [`push_bridge_to_phone_event`] as an event of `kind`, written at /// `created_at` (seconds) rather than now. -fn push_bridge_to_phone_event_at( +pub(super) fn push_bridge_to_phone_event_at( mock: &MockRelay, machine: &protocol::crypto::Keypair, phone_pubkey_hex: &str, @@ -2192,451 +246,15 @@ fn push_bridge_to_phone_event_at( } /// The recipient tag of a copy `bridge` encrypted to `key`. -fn recipient(bridge: &protocol::crypto::Keypair, key: impl AsRef) -> nostr::Tag { +pub(super) fn recipient(bridge: &protocol::crypto::Keypair, key: impl AsRef) -> nostr::Tag { let tag = protocol::recipients::recipient_tag(&bridge.pubkey_hex, key.as_ref()); nostr::Tag::custom(nostr::TagKind::custom(protocol::recipients::RECIPIENT_TAG), [tag]) } -/// A fresh phone has no relays at all: the pairing link's relays are the -/// first the transport dials, and the pair-request, sent while they are -/// still connecting, reaches them. -#[tokio::test] -async fn a_phone_with_no_relays_pairs_over_the_relays_its_pairing_names() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone.clone())), - proxy: None, - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - CorePorts::default(), - Rc::new(Spy::default()), - Rc::new(FixedClock(RefCell::new(1_000_000))), - Rc::new(ZeroEntropy), - ) - .await; - core.start(); - - core.dispatch(Intent::BeginManualPairing { - npub: machine.npub.clone(), - token: "tok".into(), - relays: mock.url.clone(), - label: "laptop".into(), - }) - .await; - let mut pair_request = false; - for _ in 0..12 { - let frame = mock.next_frame().await; - let v: Vec = serde_json::from_str(&frame).unwrap(); - match v[0].as_str() { - Some("REQ") => mock.push(format!(r#"["EOSE","{}"]"#, v[1].as_str().unwrap())), - Some("EVENT") => { - let tags = v[1]["tags"].as_array().unwrap(); - if tags.iter().any(|t| t[0] == "p" && t[1] == machine.pubkey_hex) { - pair_request = true; - break; - } - } - _ => {} - } - } - assert!(pair_request, "the pair-request never reached the pairing's relay"); - }) - .await; -} - -#[tokio::test] -async fn remove_machine_intent_drops_it_from_the_view_and_erases_its_transcript() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - // `set_machines` alone (as other tests use it) only steers the - // subscription authors filter — it does not populate - // `stores.machines`, so it is not enough to make `RemoveMachine` - // find anything to forget. Only a completed pairing does that. - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - core.dispatch(Intent::BeginManualPairing { - npub: machine.npub.clone(), - token: "tok".into(), - relays: mock.url.clone(), - label: "laptop".into(), - }) - .await; - // Staging the candidate resubscribes (the ack must pass the - // authors filter) — drain that round to get a sub_id the - // router currently has open. - let sub = drain_traffic_resubscribe(&mut mock).await; - push_bridge_to_phone_event( - &mock, - &machine, - &phone.pubkey_hex, - &sub, - &BridgeToPhone::PairAck(protocol::events::PairAckMsg { - machine: "laptop".into(), - ok: true, - reason: None, - relays: None, - host: None, - }), - ); - let key = machine.pubkey_hex.clone(); - wait_for_async(|| { - let core = core.clone(); - let key = key.clone(); - async move { core.machines_view().await.machines.contains_key(&key) } - }) - .await; - assert!(core - .machines_view() - .await - .machines - .contains_key(&machine.pubkey_hex)); - - // Registering the machine resubscribes again — same reason. - let sub = drain_traffic_resubscribe(&mut mock).await; - - // A session must actually be listed (not just have output - // flowing) for `RemoveMachine` to find it — it gathers the - // sessions to forget from `MachineView.sessions`, exactly - // like the TS `removeMachine` it mirrors. - let sessions_msg = protocol::codec::decode_bridge_to_phone( - r#"{"v":12,"type":"sessions","machine":"laptop","sessions":[ - {"id":"s1","agent":"claude-code","slug":"sl","cwd":"/w","lastActivity":"t","lineCount":0,"title":null,"project":"p"} - ],"agents":[]}"#, - ) - .unwrap(); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &sessions_msg); - let listed_key = machine.pubkey_hex.clone(); - wait_for_async(|| { - let core = core.clone(); - let key = listed_key.clone(); - async move { - core.machines_view() - .await - .machines - .get(&key) - .is_some_and(|m| m.sessions.contains_key("s1")) - } - }) - .await; - - let msg = protocol::codec::decode_bridge_to_phone( - r#"{"v":12,"type":"output","sessionId":"s1","seq":1,"entries":[{"entryType":"text","role":"agent","text":"hi","timestamp":"t"}]}"#, - ) - .unwrap(); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &msg); - wait_for_async(|| { - let core = core.clone(); - let key = machine.pubkey_hex.clone(); - async move { core.transcript_view(key, "s1".to_string()).await.rows.len() == 1 } - }) - .await; - - // Sanity: the transcript is really there before forgetting the machine. - let before = core.transcript_view(machine.pubkey_hex.clone(), "s1".to_string()).await; - assert_eq!(before.rows.len(), 1); - - core.dispatch(Intent::RemoveMachine { - pubkey_hex: machine.pubkey_hex.clone(), - }) - .await; - wait_for_async(|| { - let core = core.clone(); - let key = machine.pubkey_hex.clone(); - async move { core.transcript_view(key, "s1".to_string()).await.rows.is_empty() } - }) - .await; - - assert!(!core - .machines_view() - .await - .machines - .contains_key(&machine.pubkey_hex)); - let after = core.transcript_view(machine.pubkey_hex.clone(), "s1".to_string()).await; - assert!(after.rows.is_empty()); - - let events = spy.events.lock().unwrap(); - assert!(events.contains(&CoreEvent::StateChanged { slice: SliceId::Transcript })); - assert!(events.contains(&CoreEvent::StateChanged { slice: SliceId::Machines })); - }) - .await; -} - -#[tokio::test] -async fn remove_machine_for_a_never_paired_pubkey_is_a_harmless_no_op() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - - core.dispatch(Intent::RemoveMachine { - pubkey_hex: "never-paired".into(), - }) - .await; - - assert!(!core.machines_view().await.machines.contains_key("never-paired")); - }) - .await; -} - -#[tokio::test] -async fn the_undo_toast_clears_itself_when_the_window_expires_without_a_tap() { - // Regression: `on_undo_timer` cleared `stores.ui.undo_toast` correctly - // but emitted `StateChanged(Cards)` instead of `StateChanged(Ui)` — a - // `UiView` consumer (the native adapter) never learned to re-fetch, so - // letting the undo window expire without tapping undo left the toast - // showing forever. Waits out the REAL `UNDO_DELAY_MS` (~4s) rather than - // a shortened one: `Core::spawn` hydrates `StoresConfig::default()` - // unconditionally, so the production delay isn't test-overridable here. - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - core.dispatch(Intent::BeginManualPairing { - npub: machine.npub.clone(), - token: "tok".into(), - relays: mock.url.clone(), - label: "laptop".into(), - }) - .await; - let sub = drain_traffic_resubscribe(&mut mock).await; - push_bridge_to_phone_event( - &mock, - &machine, - &phone.pubkey_hex, - &sub, - &BridgeToPhone::PairAck(protocol::events::PairAckMsg { - machine: "laptop".into(), - ok: true, - reason: None, - relays: None, - host: None, - }), - ); - let key = machine.pubkey_hex.clone(); - wait_for_async(|| { - let core = core.clone(); - let key = key.clone(); - async move { core.machines_view().await.machines.contains_key(&key) } - }) - .await; - - let sub = drain_traffic_resubscribe(&mut mock).await; - let sessions_msg = protocol::codec::decode_bridge_to_phone( - r#"{"v":12,"type":"sessions","machine":"laptop","sessions":[ - {"id":"s1","agent":"claude-code","slug":"sl","cwd":"/w","lastActivity":"t","lineCount":0,"title":null,"project":"p"} - ],"agents":[]}"#, - ) - .unwrap(); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &sessions_msg); - let listed_key = machine.pubkey_hex.clone(); - wait_for_async(|| { - let core = core.clone(); - let key = listed_key.clone(); - async move { - core.machines_view() - .await - .machines - .get(&key) - .is_some_and(|m| m.sessions.contains_key("s1")) - } - }) - .await; - - core.dispatch(Intent::DeleteSession { - machine: machine.pubkey_hex.clone(), - session_id: "s1".into(), - label: None, - }) - .await; - assert!( - core.ui_view().await.undo_toast.is_some(), - "delete should arm the undo toast" - ); - let ui_events_before = spy - .events - .lock() - .unwrap() - .iter() - .filter(|e| matches!(e, CoreEvent::StateChanged { slice: SliceId::Ui })) - .count(); - - // Let the window elapse for real, without ever dispatching - // UndoDelete. - tokio::time::sleep(std::time::Duration::from_millis( - client_core::delete_controller::UNDO_DELAY_MS + 300, - )) - .await; - - assert!(core.ui_view().await.undo_toast.is_none()); - let ui_events_after = spy - .events - .lock() - .unwrap() - .iter() - .filter(|e| matches!(e, CoreEvent::StateChanged { slice: SliceId::Ui })) - .count(); - assert!( - ui_events_after > ui_events_before, - "the timer firing must emit its own StateChanged(Ui) — a \ - UiView consumer has no other way to learn the toast \ - cleared itself" - ); - }) - .await; -} - -/// A heartbeat listing `ids` for the machine of the session-delete test. -fn heartbeat_listing(ids: &[&str]) -> BridgeToPhone { - let sessions: Vec = ids - .iter() - .map(|id| { - serde_json::json!({"id": id, "agent": "claude-code", "slug": id, "cwd": "/w", - "lastActivity": "t", "lineCount": 0, "title": null, "project": "p"}) - }) - .collect(); - protocol::codec::decode_bridge_to_phone( - &serde_json::json!({ - "v": 12, - "type": "sessions", - "machine": "laptop", - "sessions": sessions, - "agents": [], - "v": protocol::capabilities::PROTOCOL_VERSION, - }) - .to_string(), - ) - .unwrap() -} - -/// Deleting sessions one after another: each delete commits the one before -/// it at once (one `close-session` each), a heartbeat sent before the -/// bridge has closed the later ones does not bring them back, and the -/// last one — still in its undo window — is committed as soon as the app -/// is backgrounded rather than lost with a killed process. -#[tokio::test] -async fn deleting_sessions_in_a_row_closes_each_one_exactly_once() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - heard_without_session_keys(&mut state, &machine.pubkey_hex); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let notifier = RecordingNotifier::new(); - let ports = CorePorts { kv: Rc::new(kv), notifier: Rc::new(notifier.clone()), ..CorePorts::default() }; - let spy = Rc::new(Spy::default()); - let core = core_for_ports(&mock, &phone, Rc::clone(&spy), ports).await; - core.start(); - eose_all(&mut mock).await; - let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - - let listed = heartbeat_listing(&["s1", "s2", "s3"]); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &listed); - let key = machine.pubkey_hex.clone(); - wait_for_async(|| { - let core = core.clone(); - let key = key.clone(); - async move { core.machines_view().await.machines[&key].sessions.len() == 3 } - }) - .await; - let sessions = |view: MachinesView| -> Vec { - view.machines[&machine.pubkey_hex].sessions.keys().cloned().collect() - }; - assert_eq!(sessions(core.machines_view().await), ["s1", "s2", "s3"]); - - for id in ["s1", "s2", "s3"] { - core.dispatch(Intent::DeleteSession { - machine: machine.pubkey_hex.clone(), - session_id: id.into(), - label: None, - }) - .await; - } - assert!(sessions(core.machines_view().await).is_empty()); - let toast = core.ui_view().await.undo_toast.expect("the last delete is undoable"); - assert_eq!(toast.session_id, "s3"); - - // s1 and s2 were committed by the delete that followed each. - let closed = |cmd: Option| match cmd { - Some(PhoneToBridge::CloseSession(m)) => m.session_id, - other => panic!("expected a close-session, got {other:?}"), - }; - for id in ["s1", "s2"] { - assert_eq!(closed(next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await), id); - } - - // The bridge has closed s1 only; its next list still has the - // other two, which must stay deleted on the phone. - let after_first = heartbeat_listing(&["s2", "s3"]); - // A heartbeat for a session already deleted still notifies; wait - // for that to prove the frame was folded in before asserting it - // does not resurrect anything. - let machine_changes = || { - spy.events - .lock() - .unwrap() - .iter() - .filter(|e| matches!(e, CoreEvent::StateChanged { slice: SliceId::Machines })) - .count() - }; - let before = machine_changes(); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &after_first); - wait_for(|| machine_changes() > before).await; - assert!(sessions(core.machines_view().await).is_empty()); - - // Backgrounded inside s3's undo window: committed now, well - // before the window would have run out. - core.pause(); - let last = tokio::time::timeout( - std::time::Duration::from_millis(client_core::delete_controller::UNDO_DELAY_MS / 2), - next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine), - ) - .await - .expect("the pending delete is sent when the app is backgrounded"); - assert_eq!(closed(last), "s3"); - assert!(core.ui_view().await.undo_toast.is_none()); - // Each one's notifications went with it — s3's only once it was - // deleted for good. - let tag = |id: &str| client_core::notifications::session_notify_tag(&machine.pubkey_hex, id); - assert_eq!(notifier.cancelled(), [tag("s1"), tag("s2"), tag("s3")]); - - // Nothing is left to undo, and nothing is sent twice. - core.dispatch(Intent::UndoDelete).await; - assert!(sessions(core.machines_view().await).is_empty()); - }) - .await; -} - /// The next command EVENT the identity `phone` signed for `machine`, /// with its payload decrypted as from `payload_key` (the phone's /// identity or its session key). Every EVENT is ACKed on the way. -async fn next_command_via( +pub(super) async fn next_command_via( mock: &mut MockRelay, phone: &Keypair, payload_key: &str, @@ -2661,265 +279,16 @@ async fn next_command_via( /// Mark `machine` as a bridge already heard from that does not honour /// session keys: it is talked to with the identity, as a test of anything /// else expects. -fn heard_without_session_keys(state: &mut client_core::stores::machines::MachinesState, machine: &str) { +pub(super) fn heard_without_session_keys(state: &mut client_core::stores::machines::MachinesState, machine: &str) { let m = state.machines.get_mut(machine).unwrap(); m.capabilities = vec![protocol::capabilities::FILES.to_string()]; m.last_heartbeat_at = Some(1); } -fn heartbeat_with(caps: &[&str]) -> BridgeToPhone { - protocol::codec::decode_bridge_to_phone( - &serde_json::json!({ - "v": 12, - "type": "sessions", - "machine": "laptop", - "sessions": [], - "agents": [], - "credentials": [], - "v": protocol::capabilities::PROTOCOL_VERSION, - "capabilities": caps, - }) - .to_string(), - ) - .unwrap() -} - -/// The whole grant life cycle against a bridge advertising session keys: -/// granted by the identity, confirmed by a message under the key, used -/// for payloads (never for signing), and dropped when the bridge speaks -/// to the identity again. -#[tokio::test] -async fn a_bridge_with_session_keys_gets_payloads_under_the_key_and_signatures_by_the_identity() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let ports = CorePorts { kv: Rc::new(kv), ..CorePorts::default() }; - let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - core.start(); - eose_all(&mut mock).await; - // A machine not heard from is granted before anything else; the - // reconnect's refresh waits on it. - let grant = expect_grant(&mut mock, &phone, &machine).await; - let session = grant.pubkey_hex.clone(); - assert_ne!(session, phone.pubkey_hex); - assert_eq!(grant.replaces, None); - - // Before the bridge confirms, commands wait. - core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; - - // The heartbeat under the key confirms it: what waited goes out - // under the key, without the refresh the heartbeat answered. - let heartbeat = heartbeat_with(&[protocol::capabilities::SESSION_KEYS]); - push_bridge_to_phone_event(&mock, &machine, &session, "cd-1", &heartbeat); - let cmd = next_command_via(&mut mock, &phone, &session, &machine).await; - assert!(matches!(cmd, Some(PhoneToBridge::Interrupt(_))), "{cmd:?}"); - - // A later message to the identity: the bridge lost the key, and - // is granted it again at once. - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat); - let again = expect_grant(&mut mock, &phone, &machine).await; - assert_eq!(again.pubkey_hex, session); - core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; - push_bridge_to_phone_event(&mock, &machine, &session, "cd-1", &heartbeat); - let cmd = next_command_via(&mut mock, &phone, &session, &machine).await; - assert!(matches!(cmd, Some(PhoneToBridge::Interrupt(_))), "{cmd:?}"); - }) - .await; -} - -/// The next command must be a grant, under the identity. -async fn expect_grant( - mock: &mut MockRelay, - phone: &Keypair, - machine: &Keypair, -) -> protocol::commands::SessionKeyGrant { - match next_command_via(mock, phone, &phone.pubkey_hex, machine).await { - Some(PhoneToBridge::SessionKey(m)) => m.session_key, - other => panic!("expected a grant, got {other:?}"), - } -} - -/// An identity signer that counts what it is asked to decrypt. -struct DecryptCounter(crate::signer::LocalSigner, Rc>); -impl IdentitySigner for DecryptCounter { - fn pubkey_hex(&self) -> String { - self.0.pubkey_hex() - } - fn sign_event(&self, event: nostr::UnsignedEvent) -> crate::ports::LocalBoxFuture<'_, Result> { - self.0.sign_event(event) - } - fn nip44_encrypt(&self, peer: &str, plaintext: &str) -> crate::ports::LocalBoxFuture<'_, Result> { - self.0.nip44_encrypt(peer, plaintext) - } - fn nip44_decrypt(&self, peer: &str, ciphertext: &str) -> crate::ports::LocalBoxFuture<'_, Result> { - self.1.set(self.1.get() + 1); - self.0.nip44_decrypt(peer, ciphertext) - } -} - -/// A core whose identity counts its decrypts, with `machine` restored -/// from a backup: known, never heard from, no key of this client. -async fn restored_core(mock: &MockRelay, phone: &Keypair, machine: &Keypair, spy: Rc) -> (Core, Rc>) { - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let decrypts = Rc::new(std::cell::Cell::new(0)); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(DecryptCounter(crate::signer::LocalSigner(phone.clone()), Rc::clone(&decrypts))), - proxy: None, - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, - spy, - Rc::new(FixedClock(RefCell::new(1_000_000))), - Rc::new(ZeroEntropy), - ) - .await; - core.set_relays(vec![mock.url.clone()]); - (core, decrypts) -} - -/// Every grant carries the phone's configured device name — what the bridge -/// shows in its terminal allowlist and in a refusal — and none when the phone -/// has no name to give. `CoreConfig::new` sanitizes, so a whitespace-only -/// setting is stored (and granted) as no name rather than a blank one. -#[tokio::test] -async fn every_grant_carries_the_configured_device_label() { - LocalSet::new() - .run_until(async { - for (configured, expected) in [ - (Some("Pixel 8a".to_string()), Some("Pixel 8a")), - (Some(" ".to_string()), None), - (None, None), - ] { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let core = Core::spawn( - CoreConfig::new(Rc::new(crate::signer::LocalSigner(phone.clone())), None, false, configured.clone()), - CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, - spy, - Rc::new(FixedClock(RefCell::new(1_000_000))), - Rc::new(ZeroEntropy), - ) - .await; - core.set_relays(vec![mock.url.clone()]); - core.start(); - eose_all(&mut mock).await; - let grant = expect_grant(&mut mock, &phone, &machine).await; - assert_eq!(grant.label.as_deref(), expected, "configured {configured:?}"); - } - }) - .await; -} - -/// A restored device costs the identity's signer no decrypt: what the -/// bridge wrote to the identity before the grant is skipped, what it -/// writes while the grant travels is dropped once the key is confirmed, -/// and the sessions the confirming heartbeat lists sync under the key. -#[tokio::test] -async fn a_restored_machine_is_granted_first_and_nothing_to_the_identity_is_decrypted() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let (core, decrypts) = restored_core(&mock, &phone, &machine, Rc::clone(&spy)).await; - core.start(); - eose_all(&mut mock).await; - let grant = expect_grant(&mut mock, &phone, &machine).await; - - // An identity heartbeat from an earlier install, still on the - // relay, and one written while the grant travels. - let heartbeat = heartbeat_with(&[protocol::capabilities::SESSION_KEYS]); - push_bridge_to_phone_event_at(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat, SESSION_LIST_KIND, Some(500)); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat); - settle().await; - assert!(spy.messages.lock().unwrap().is_empty(), "nothing to the identity is read yet"); - - // The heartbeat under the key lists a session: it syncs under the key. - let listed = protocol::codec::decode_bridge_to_phone( - &serde_json::json!({ - "v": protocol::capabilities::PROTOCOL_VERSION, - "type": "sessions", - "machine": "laptop", - "sessions": [{"id": "s1", "agent": "opencode", "slug": "sl", "cwd": "/w", "lastActivity": "t", "lineCount": 1, "title": null, "project": "p", "seqHigh": 3}], - "agents": [], - "credentials": [], - "capabilities": [protocol::capabilities::SESSION_KEYS], - }) - .to_string(), - ) - .unwrap(); - push_bridge_to_phone_event(&mock, &machine, &grant.pubkey_hex, "cd-1", &listed); - let sync = next_command_via(&mut mock, &phone, &grant.pubkey_hex, &machine).await; - assert!(matches!(sync, Some(PhoneToBridge::SyncRequest(_))), "{sync:?}"); - wait_for(|| spy.messages.lock().unwrap().len() == 1).await; - assert_eq!(decrypts.get(), 0, "the identity decrypted nothing"); - assert_eq!(spy.messages.lock().unwrap().len(), 1, "only the heartbeat under the key was read"); - }) - .await; -} - -/// A bridge that does not confirm the grant in time: what waited on it -/// goes on under the identity, as without session keys. -#[tokio::test] -async fn an_unconfirmed_grant_lets_what_waited_go_on_under_the_identity() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let (core, decrypts) = restored_core(&mock, &phone, &machine, Rc::clone(&spy)).await; - core.start(); - eose_all(&mut mock).await; - expect_grant(&mut mock, &phone, &machine).await; - // A heartbeat to the identity, and live output superseded by - // the sync that follows it. - let heartbeat = heartbeat_with(&[]); - push_bridge_to_phone_event_at(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat, SESSION_LIST_KIND, None); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat); - settle().await; - assert_eq!(decrypts.get(), 0); - - tokio::time::pause(); - tokio::time::advance(super::grant::GRANT_WAIT + Duration::from_secs(1)).await; - tokio::time::resume(); - let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - wait_for(|| decrypts.get() == 1).await; - assert_eq!(decrypts.get(), 1, "the held heartbeat is read with the identity"); - assert!(spy.messages.lock().unwrap().iter().any(|(_, m)| matches!(m, BridgeToPhone::Sessions(_)))); - }) - .await; -} - /// A host's secret store for the session keys, in memory. #[derive(Default, Clone)] -struct MemoryKeyStore(Rc>>); +pub(super) struct MemoryKeyStore(Rc>>); + impl SessionKeyStore for MemoryKeyStore { fn load(&self) -> crate::ports::LocalBoxFuture<'_, Option> { let ring = self.0.borrow().clone(); @@ -2930,911 +299,15 @@ impl SessionKeyStore for MemoryKeyStore { Box::pin(async {}) } } + impl MemoryKeyStore { fn ring(&self) -> SessionKeyRing { SessionKeyRing::load(self.0.borrow().as_deref(), 1_000_000).0 } } -#[tokio::test] -async fn with_a_host_key_store_the_kv_holds_no_session_key() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let kv = MemoryKv::seeded([ - (crate::stores::OLD_SESSION_KEY_KEY, generate_keypair().secret_hex()), - (crate::stores::SESSION_KEYS_KEY, "{}".to_string()), - ]); - let store = MemoryKeyStore::default(); - let ports = CorePorts { - kv: Rc::new(kv.clone()), - session_keys: Some(Rc::new(store.clone())), - ..CorePorts::default() - }; - let _core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - let dump = kv.dump(); - assert!(!dump.contains_key(crate::stores::OLD_SESSION_KEY_KEY)); - assert!(!dump.contains_key(crate::stores::SESSION_KEYS_KEY)); - assert!(store.0.borrow().is_some(), "the key went to the host's store"); - let secret = store.ring().current.keypair.secret_hex(); - assert!(dump.values().all(|v| !v.contains(&secret))); - }) - .await; -} - -/// A client a bridge knows by its session key stays present there: after -/// a quiet [`PRESENCE_RENEW_SECS`] it renews with a `presence`, and a stop -/// tells the bridge it is away before it disconnects. -#[tokio::test] -async fn a_quiet_client_renews_its_presence_and_says_when_it_goes_away() { - use client_core::stores::session_key::{SessionGrant, SessionKey}; - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let start_ms: u64 = 1_000_000; - let key = SessionKey { keypair: generate_keypair(), expires_at: start_ms / 1000 + 80 * 24 * 3600 }; - let store = MemoryKeyStore::default(); - *store.0.borrow_mut() = Some(SessionKeyRing::single(key.clone()).encode()); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - state.machines.get_mut(&machine.pubkey_hex).unwrap().session_grant = Some(SessionGrant { - pubkey_hex: key.pubkey_hex().to_string(), - expires_at: key.expires_at, - sent_at: start_ms / 1000 - 100, - }); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let clock = Rc::new(FixedClock(RefCell::new(start_ms))); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone.clone())), - proxy: None, - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - CorePorts { kv: Rc::new(kv), session_keys: Some(Rc::new(store.clone())), ..CorePorts::default() }, - Rc::new(Spy::default()), - Rc::clone(&clock) as Rc, - Rc::new(ZeroEntropy), - ) - .await; - core.set_relays(vec![mock.url.clone()]); - core.start(); - eose_all(&mut mock).await; - // Connecting says it is here already. - let refresh = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - - // Quiet for the renewal period: the next check sends a presence. - *clock.0.borrow_mut() += PRESENCE_RENEW_SECS * 1000; - tokio::time::pause(); - tokio::time::advance(STALE_WATCHDOG_EVERY + Duration::from_secs(1)).await; - tokio::time::resume(); - let renewal = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; - assert!(matches!(renewal, Some(PhoneToBridge::Presence(PresenceMsg { away: false, watching: None, watching_terminal: None }))), "{renewal:?}"); - - core.stop(); - let away = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; - assert!(matches!(away, Some(PhoneToBridge::Presence(PresenceMsg { away: true, .. }))), "{away:?}"); - }) - .await; -} - -/// A machine-settings change the bridge never answers (the machine is -/// offline, a relay dropped the command) stops showing as busy after the -/// phone's own deadline, with the reason, instead of spinning forever. -#[tokio::test] -async fn a_machine_settings_change_nobody_answers_stops_being_busy_at_the_deadline() { - use client_core::stores::machines::SLOW_SETTINGS_ANSWER_TIMEOUT_MS; - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let start_ms: u64 = 1_000_000; - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let clock = Rc::new(FixedClock(RefCell::new(start_ms))); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone.clone())), - proxy: None, - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, - Rc::new(Spy::default()), - Rc::clone(&clock) as Rc, - Rc::new(ZeroEntropy), - ) - .await; - core.set_relays(vec![mock.url.clone()]); - core.start(); - eose_all(&mut mock).await; - - core.dispatch(Intent::PluginAction { - machine: machine.pubkey_hex.clone(), - agent: "claude-code".into(), - action: protocol::common::PluginAction::Install, - target: "b@m".into(), - }) - .await; - let busy = |view: MachinesView| view.machines[&machine.pubkey_hex].plugins["claude-code"].busy.clone(); - assert_eq!(busy(core.machines_view().await), ["b@m".to_string()]); - - // The machine never answers; the phone's clock passes the - // deadline and the timer armed for it fires. - *clock.0.borrow_mut() += SLOW_SETTINGS_ANSWER_TIMEOUT_MS + 1; - tokio::time::pause(); - tokio::time::advance(Duration::from_millis(SLOW_SETTINGS_ANSWER_TIMEOUT_MS + 1)).await; - tokio::time::resume(); - wait_for_async(|| { - let core = core.clone(); - let key = machine.pubkey_hex.clone(); - async move { - let view = core.machines_view().await; - view.machines[&key].plugins["claude-code"].failure.is_some() - } - }) - .await; - - let view = core.machines_view().await; - let plugins = &view.machines[&machine.pubkey_hex].plugins["claude-code"]; - assert!(plugins.busy.is_empty(), "the change is given up on"); - let failure = plugins.failure.as_ref().expect("and says why"); - assert!(failure.error.contains("did not answer"), "{failure:?}"); - }) - .await; -} - -/// A key a month from lapsing is replaced at boot. The bridge on it keeps -/// using it until it confirms the new one, which it is granted under the -/// old one; then the old key is gone from the phone's store. -#[tokio::test] -async fn a_rotated_key_is_granted_and_the_old_one_dropped_once_confirmed() { - use client_core::stores::session_key::{SessionGrant, SessionKey}; - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let now_secs = 1_000; - let old = SessionKey { keypair: generate_keypair(), expires_at: now_secs + 10 * 24 * 3600 }; - let store = MemoryKeyStore::default(); - *store.0.borrow_mut() = Some(SessionKeyRing::single(old.clone()).encode()); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - state.machines.get_mut(&machine.pubkey_hex).unwrap().session_grant = Some(SessionGrant { - pubkey_hex: old.pubkey_hex().to_string(), - expires_at: old.expires_at, - sent_at: now_secs - 100, - }); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let ports = CorePorts { - kv: Rc::new(kv), - session_keys: Some(Rc::new(store.clone())), - ..CorePorts::default() - }; - let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; - let ring = store.ring(); - let new = ring.current.pubkey_hex().to_string(); - assert_ne!(new, old.pubkey_hex(), "replaced at boot"); - assert_eq!(ring.previous.as_ref().map(|k| k.pubkey_hex()), Some(old.pubkey_hex()), "and kept"); - - core.start(); - eose_all(&mut mock).await; - // The bridge still holds only the old key: payloads use it. - let refresh = next_command_via(&mut mock, &phone, old.pubkey_hex(), &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - - // Its heartbeat, under the old key, earns it the new one. - let heartbeat = heartbeat_with(&[protocol::capabilities::SESSION_KEYS]); - push_bridge_to_phone_event(&mock, &machine, old.pubkey_hex(), "cd-1", &heartbeat); - let grant = match next_command_via(&mut mock, &phone, old.pubkey_hex(), &machine).await { - Some(PhoneToBridge::SessionKey(m)) => m.session_key, - other => panic!("expected a grant, got {other:?}"), - }; - assert_eq!(grant.pubkey_hex, new); - assert_eq!(grant.replaces.as_deref(), Some(old.pubkey_hex()), "it moves this client, not adds one"); - assert_eq!(grant.bridge_pubkey_hex, machine.pubkey_hex); - assert_eq!(grant.expires_at, ring.current.expires_at); - - // A heartbeat under the new key confirms it: the old one goes. - push_bridge_to_phone_event(&mock, &machine, &new, "cd-1", &heartbeat); - wait_for(|| store.ring().previous.is_none()).await; - assert!(store.ring().previous.is_none()); - core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; - let cmd = next_command_via(&mut mock, &phone, &new, &machine).await; - assert!(matches!(cmd, Some(PhoneToBridge::Interrupt(_))), "{cmd:?}"); - }) - .await; -} - -/// Coming back to the app with the socket still up asks a machine for -/// its session list only when its heartbeat has not kept it current. -#[tokio::test] -async fn a_resume_refreshes_only_machines_not_heard_from_lately() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - heard_without_session_keys(&mut state, &machine.pubkey_hex); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone.clone())), - proxy: None, - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, - Rc::new(Spy::default()), - clock.clone(), - Rc::new(ZeroEntropy), - ) - .await; - core.set_relays(vec![mock.url.clone()]); - core.start(); - eose_all(&mut mock).await; - let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "the connect refreshes: {refresh:?}"); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat_with(&[])); - for _ in 0..100 { - if core.machines_view().await.machines[&machine.pubkey_hex].last_heartbeat_at.is_some() { - break; - } - tokio::time::sleep(Duration::from_millis(10)).await; - } - - // Heard just now: the resume sends nothing, so the next - // command out is the user's. - core.resume(); - core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; - let cmd = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; - assert!(matches!(cmd, Some(PhoneToBridge::Interrupt(_))), "{cmd:?}"); - - // Silent for longer than a couple of heartbeats: refreshed. - *clock.0.borrow_mut() += RESUME_HEARD_WITHIN_MS; - core.resume(); - let cmd = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; - assert!(matches!(cmd, Some(PhoneToBridge::RefreshSessions(_))), "{cmd:?}"); - }) - .await; -} - -/// A bridge's direct link, scripted: it takes the phone's HELLO, pushes -/// `push` (a message for the phone), then answers every command with an -/// `OK` and hands it to `commands`. -async fn scripted_direct_bridge( - listener: tokio::net::TcpListener, - machine: Keypair, - phone_pubkey: String, - push: BridgeToPhone, - commands: mpsc::UnboundedSender, -) { - use futures_util::{SinkExt, StreamExt}; - use protocol::direct::{check_direct_auth, decode_direct_frame, encode_direct_frame, DirectFrame}; - use tokio_tungstenite::tungstenite::Message; - let (tcp, _) = listener.accept().await.unwrap(); - let mut ws = tokio_tungstenite::accept_async(tcp).await.unwrap(); - let text = |f: &DirectFrame| Message::Text(encode_direct_frame(f)); - ws.send(text(&DirectFrame::Challenge("c1".into()))).await.unwrap(); - let hello = loop { - if let Some(Ok(Message::Text(t))) = ws.next().await { - break decode_direct_frame(&t).unwrap(); - } - }; - let DirectFrame::Hello { auth, .. } = hello else { panic!("expected HELLO") }; - assert_eq!(check_direct_auth(&auth, "c1", auth.created_at).unwrap(), phone_pubkey, "signed by the identity"); - ws.send(text(&DirectFrame::Ready)).await.unwrap(); - - let ct = protocol::crypto::encrypt_to(&machine.secret_key, &phone_pubkey, &encode_bridge_to_phone(&push)).unwrap(); - let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) - .tag(recipient(&machine, &phone_pubkey)) - .tag(nostr::Tag::public_key(nostr::PublicKey::from_hex(&phone_pubkey).unwrap())) - .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) - .unwrap(); - ws.send(text(&DirectFrame::Event(SignedEvent::from_nostr(&event)))).await.unwrap(); - - loop { - if let Some(Ok(Message::Text(t))) = ws.next().await { - if let Ok(DirectFrame::Event(command)) = decode_direct_frame(&t) { - ws.send(text(&DirectFrame::Ok { id: command.id.clone(), accepted: true, message: String::new() })).await.unwrap(); - let plaintext = protocol::crypto::decrypt_from(&machine.secret_key, &phone_pubkey, &command.content).unwrap(); - let _ = commands.send(protocol::codec::decode_phone_to_bridge(&plaintext).unwrap()); - } - } - } -} - -/// A bridge that advertises a direct endpoint gets a link: its messages -/// arrive over it, the machines view says so, and commands go over it. -#[tokio::test] -async fn a_direct_link_carries_messages_and_commands() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let endpoint = format!("ws://{}", listener.local_addr().unwrap()); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - heard_without_session_keys(&mut state, &machine.pubkey_hex); - state.machines.get_mut(&machine.pubkey_hex).unwrap().direct = - Some(protocol::direct::DirectInfo { endpoints: vec![endpoint.clone()], cert_sha256: None }); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let push = protocol::codec::decode_bridge_to_phone(r#"{"v":12,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#).unwrap(); - let (commands_tx, mut commands) = mpsc::unbounded_channel(); - tokio::task::spawn_local(scripted_direct_bridge( - listener, - machine.clone(), - phone.pubkey_hex.clone(), - push, - commands_tx, - )); - - let spy = Rc::new(Spy::default()); - let core = core_for_ports(&mock, &phone, Rc::clone(&spy), CorePorts { kv: Rc::new(kv), ..CorePorts::default() }).await; - core.start(); - eose_all(&mut mock).await; - for _ in 0..100 { - if spy.messages.lock().unwrap().iter().any(|(_, m)| matches!(m, BridgeToPhone::InputAck(_))) { - break; - } - tokio::time::sleep(Duration::from_millis(20)).await; - } - assert!( - spy.messages.lock().unwrap().iter().any(|(m, msg)| *m == machine.pubkey_hex && matches!(msg, BridgeToPhone::InputAck(_))), - "the pushed message arrived over the link" - ); - assert_eq!(core.machines_view().await.direct_up.get(&machine.pubkey_hex), Some(&endpoint)); - - core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; - // Other commands (the session-key grant) may come first. - tokio::time::timeout(Duration::from_secs(5), async { - loop { - match commands.recv().await { - Some(PhoneToBridge::Interrupt(_)) => break, - Some(_) => {} - None => panic!("the link closed"), - } - } - }) - .await - .expect("the interrupt went over the link in time"); - }) - .await; -} - -/// A local identity that counts its signatures. -struct CountingSigner(crate::signer::LocalSigner, Rc>); -impl IdentitySigner for CountingSigner { - fn pubkey_hex(&self) -> String { - self.0.pubkey_hex() - } - fn sign_event(&self, event: nostr::UnsignedEvent) -> crate::ports::LocalBoxFuture<'_, Result> { - self.1.set(self.1.get() + 1); - self.0.sign_event(event) - } - fn nip44_encrypt(&self, peer: &str, plaintext: &str) -> crate::ports::LocalBoxFuture<'_, Result> { - self.0.nip44_encrypt(peer, plaintext) - } - fn nip44_decrypt(&self, peer: &str, ciphertext: &str) -> crate::ports::LocalBoxFuture<'_, Result> { - self.0.nip44_decrypt(peer, ciphertext) - } -} - -/// The identity signs on its own only for a handful of things: a -/// refresh per machine on each (re)connect, the sync requests and acks -/// that follow, and relay AUTH. A sync's chunks arrive back to back; they -/// cost one signed ack, not one each. -#[tokio::test] -async fn a_burst_of_sync_chunks_costs_one_signed_ack() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - heard_without_session_keys(&mut state, &machine.pubkey_hex); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let signs = Rc::new(std::cell::Cell::new(0)); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(CountingSigner(crate::signer::LocalSigner(phone.clone()), Rc::clone(&signs))), - proxy: None, - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, - Rc::new(Spy::default()), - Rc::new(FixedClock(RefCell::new(1_000_000))), - Rc::new(ZeroEntropy), - ) - .await; - core.set_relays(vec![mock.url.clone()]); - core.start(); - eose_all(&mut mock).await; - let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - assert_eq!(signs.get(), 1, "the reconnect's refresh"); - - let decode = |v: serde_json::Value| protocol::codec::decode_bridge_to_phone(&v.to_string()).unwrap(); - let entry = serde_json::json!({ "timestamp": "t", "entryType": "turn_complete" }); - let begin = decode(serde_json::json!({ - "v": 12, - "type": "sync-begin", "sessionId": "s1", "syncId": "y1", "seqHigh": 3, "ranges": [[1, 3]] - })); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &begin); - for seq in 1..=3 { - let chunk = decode(serde_json::json!({ - "v": 12, - "type": "sync-chunk", "sessionId": "s1", "syncId": "y1", "range": [seq, seq], - "entries": [{ "seq": seq, "entry": entry }] - })); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &chunk); - } - let ack = match next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await { - Some(PhoneToBridge::SyncAck(ack)) => ack, - other => panic!("expected a sync-ack, got {other:?}"), - }; - assert_eq!((ack.sync_id.as_str(), ack.ranges), ("y1", vec![(1, 1), (2, 2), (3, 3)])); - assert_eq!(signs.get(), 2, "one signature for the three chunks"); - }) - .await; -} - -/// A `sync-begin` whose `sync-end` never arrives leaves the cycle in flight; -/// without a retry the session's gap would stay unfilled until the next -/// reconnect. The watchdog's tick restarts the cycle. -#[tokio::test] -async fn a_sync_cycle_that_goes_silent_is_re_requested_by_the_watchdog() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - heard_without_session_keys(&mut state, &machine.pubkey_hex); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(crate::signer::LocalSigner(phone.clone())), - proxy: None, - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, - Rc::new(Spy::default()), - clock.clone(), - Rc::new(ZeroEntropy), - ) - .await; - core.set_relays(vec![mock.url.clone()]); - core.start(); - eose_all(&mut mock).await; - let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - - let begin = protocol::codec::decode_bridge_to_phone( - &serde_json::json!({ - "v": 12, - "type": "sync-begin", "sessionId": "s1", "syncId": "y1", "seqHigh": 3, "ranges": [[1, 3]] - }) - .to_string(), - ) - .unwrap(); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &begin); - wait_for_async(|| { - let core = core.clone(); - let key = machine.pubkey_hex.clone(); - async move { core.transcript_view(key, "s1".to_string()).await.sync.target == 3 } - }) - .await; - - *clock.0.borrow_mut() += - client_core::stores::transcript::SYNC_STALL_MS + 1; - core.tick(); - let request = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; - assert!( - matches!(request, Some(PhoneToBridge::SyncRequest(ref m)) if m.session_id == "s1"), - "{request:?}" - ); - }) - .await; -} - -/// A stalled cycle must not be re-requested once the socket is gone: the -/// tick's own heartbeat check may have just moved the FSM to WaitingRetry, -/// and publishing there only burns an attempt and a signature. -#[tokio::test] -async fn a_sync_stall_is_not_re_requested_on_a_dead_connection() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); - heard_without_session_keys(&mut state, &machine.pubkey_hex); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let signs = Rc::new(std::cell::Cell::new(0)); - let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); - let core = Core::spawn( - CoreConfig { - identity: Rc::new(CountingSigner( - crate::signer::LocalSigner(phone.clone()), - Rc::clone(&signs), - )), - proxy: None, - tor: false, - device_label: None, - reconnect: fast_reconnect(), - }, - CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, - Rc::new(Spy::default()), - clock.clone(), - Rc::new(ZeroEntropy), - ) - .await; - core.set_relays(vec![mock.url.clone()]); - core.start(); - eose_all(&mut mock).await; - let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - assert_eq!(signs.get(), 1, "the reconnect's refresh"); - - let begin = protocol::codec::decode_bridge_to_phone( - &serde_json::json!({ - "v": 12, - "type": "sync-begin", "sessionId": "s1", "syncId": "y1", "seqHigh": 3, "ranges": [[1, 3]] - }) - .to_string(), - ) - .unwrap(); - push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &begin); - wait_for_async(|| { - let core = core.clone(); - let key = machine.pubkey_hex.clone(); - async move { core.transcript_view(key, "s1".to_string()).await.sync.target == 3 } - }) - .await; - - core.set_online(false); - *clock.0.borrow_mut() += client_core::stores::transcript::SYNC_STALL_MS + 1; - core.tick(); - // Nothing is published, so there is no event to wait on: give the - // tick's spawned work a moment before asserting the absence. - settle().await; - assert_eq!(signs.get(), 1, "no request is published without a live connection"); - }) - .await; -} - -/// Documents the JSON shape a binding receives — externally tagged, -/// camelCase, matching `Intent`'s own convention. -#[test] -fn core_event_json_shape_is_externally_tagged_camel_case() { - assert_eq!( - serde_json::to_value(CoreEvent::StateChanged { slice: SliceId::Machines }).unwrap(), - serde_json::json!({ "stateChanged": { "slice": "machines" } }), - ); - assert_eq!( - serde_json::to_value(CoreEvent::OutboxSettled { - id: "in-1".into(), - delivered: true, - }) - .unwrap(), - serde_json::json!({ "outboxSettled": { "id": "in-1", "delivered": true } }), - ); - assert_eq!( - serde_json::to_value(CoreEvent::ActionFailed { - kind: ActionFailedKind::PublishRejected, - }) - .unwrap(), - serde_json::json!({ "actionFailed": { "kind": "publishRejected" } }), - ); -} - -/// The next frame of `kind` (`REQ`, `EVENT`, …), skipping the others. -async fn next_frame_of(mock: &mut MockRelay, kind: &str) -> Vec { - for _ in 0..20 { - let frame = mock.next_frame().await; - let v: Vec = serde_json::from_str(&frame).unwrap(); - if v[0] == kind { - return v; - } - } - panic!("no {kind} frame"); -} - -/// The backup look's REQ: answered with `events` then EOSE. Returns the -/// subscription's id. -async fn answer_backup_req(mock: &mut MockRelay, me: &str, events: &[serde_json::Value]) -> String { - loop { - let req = next_frame_of(mock, "REQ").await; - let sub = req[1].as_str().unwrap().to_string(); - if req[2]["#d"].is_array() { - assert_eq!(req[2]["kinds"], serde_json::json!([30078])); - assert_eq!(req[2]["authors"], serde_json::json!([me])); - for e in events { - mock.push(serde_json::json!(["EVENT", sub, e]).to_string()); - } - mock.push(format!(r#"["EOSE","{sub}"]"#)); - return sub; - } - mock.push(format!(r#"["EOSE","{sub}"]"#)); - } -} - -/// Backup turned on with `mock` as its relay, holding nothing yet: the -/// core saves its own, which the relay takes. Returns the backup -/// subscription's id. -async fn backup_on(core: &Core, mock: &mut MockRelay, me: &str) -> String { - core.dispatch(Intent::SetBackupRelay(mock.url.clone())).await; - let sub = answer_backup_req(mock, me, &[]).await; - let event = next_frame_of(mock, "EVENT").await[1].clone(); - mock.push(serde_json::json!(["OK", event["id"], true, ""]).to_string()); - wait_for_async(|| { - let core = core.clone(); - async move { core.settings_view().await.unwrap().backup.saved_at.is_some() } - }) - .await; - sub -} - -#[tokio::test] -async fn the_backup_relay_hears_only_the_backup_and_the_bridge_relays_never_do() { - LocalSet::new() - .run_until(async { - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let mut bridge_relay = mock_relay().await; - let mut backup_relay = mock_relay().await; - let mut state = client_core::stores::machines::MachinesState::default(); - state.register_machine(&machine.pubkey_hex, "laptop", None, None, &[bridge_relay.url.clone()]); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let ports = CorePorts { kv: Rc::new(kv), ..CorePorts::default() }; - let core = core_for_ports(&bridge_relay, &phone, Rc::new(Spy::default()), ports).await; - core.start(); - core.dispatch(Intent::SetBackupRelay(backup_relay.url.clone())).await; - let req = next_frame_of(&mut backup_relay, "REQ").await; - assert!(req[2]["#d"].is_array(), "the backup relay is asked for the backup: {req:?}"); - let sub = req[1].as_str().unwrap(); - backup_relay.push(format!(r#"["EOSE","{sub}"]"#)); - while let Some(frame) = backup_relay.frame_within(Duration::from_millis(400)).await { - assert!(!frame.starts_with(r#"["REQ""#), "the backup relay sees what the phone follows: {frame}"); - } - while let Some(frame) = bridge_relay.frame_within(Duration::from_millis(200)).await { - let v: Vec = serde_json::from_str(&frame).unwrap(); - assert!(v[0] != "REQ" || v[2].get("#d").is_none(), "a bridge relay sees the backup's REQ: {frame}"); - } - }) - .await; -} - -#[tokio::test] -async fn a_deletion_on_another_device_turns_backup_off_here() { - LocalSet::new() - .run_until(async { - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let mut mock = mock_relay().await; - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - core.start(); - let sub = backup_on(&core, &mut mock, &phone.pubkey_hex).await; - assert!(settings_backup(&core).await.relay.is_some()); - // Another device deletes it: the empty version takes its place. - let signer = crate::signer::LocalSigner(phone.clone()); - let removed = crate::backup::seal_removed(&signer, 5_000).await.unwrap(); - mock.push(serde_json::json!(["EVENT", sub, removed]).to_string()); - wait_for_async(|| { - let core = core.clone(); - async move { - matches!( - core.settings_view().await.unwrap().backup.status, - crate::backup::BackupStatus::Failed { .. } - ) - } - }) - .await; - let backup = settings_backup(&core).await; - assert_eq!(backup.relay, None); - assert!(matches!(backup.status, crate::backup::BackupStatus::Failed { reason } if reason.contains("another device"))); - }) - .await; -} - -#[tokio::test] -async fn a_device_joining_after_a_deletion_saves_its_own() { - LocalSet::new() - .run_until(async { - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let mut mock = mock_relay().await; - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - core.start(); - let signer = crate::signer::LocalSigner(phone.clone()); - let removed = crate::backup::seal_removed(&signer, 900).await.unwrap(); - core.dispatch(Intent::SetBackupRelay(mock.url.clone())).await; - answer_backup_req(&mut mock, &phone.pubkey_hex, &[serde_json::to_value(&removed).unwrap()]).await; - let saved = next_frame_of(&mut mock, "EVENT").await[1].clone(); - assert_eq!(saved["kind"], 30078); - assert!(!saved["content"].as_str().unwrap().is_empty()); - assert!(saved["created_at"].as_u64().unwrap() > 900, "dated past the empty version"); - assert!(settings_backup(&core).await.relay.is_some()); - }) - .await; -} - -#[tokio::test] -async fn a_backup_relay_ending_the_subscription_shows_as_a_failure() { - LocalSet::new() - .run_until(async { - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let mut mock = mock_relay().await; - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - core.start(); - let sub = backup_on(&core, &mut mock, &phone.pubkey_hex).await; - mock.push(serde_json::json!(["CLOSED", sub, "restricted: members only"]).to_string()); - wait_for_async(|| { - let core = core.clone(); - async move { - matches!( - core.settings_view().await.unwrap().backup.status, - crate::backup::BackupStatus::Failed { .. } - ) - } - }) - .await; - let backup = settings_backup(&core).await; - assert!(backup.relay.is_some(), "backup stays on, and is asked for again later"); - assert!(matches!(backup.status, crate::backup::BackupStatus::Failed { reason } if reason.contains("members only"))); - }) - .await; -} - -async fn settings_backup(core: &Core) -> crate::view::BackupView { - core.settings_view().await.unwrap().backup -} - -#[tokio::test] -async fn a_backup_saved_by_one_phone_restores_the_machines_on_another() { - LocalSet::new() - .run_until(async { - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - - // The old phone, paired with a machine, turns the backup on. - let mut first = mock_relay().await; - let mut state = client_core::stores::machines::MachinesState::default(); - // Names with spaces: base64 ciphertext can never contain them by chance. - state.register_machine(&machine.pubkey_hex, "my laptop", Some("Work lab".into()), None, &[first.url.clone()]); - let kv = MemoryKv::seeded([( - crate::stores::MACHINES_KEY, - client_core::stores::machines::serialize_machines(&state.machines), - )]); - let old_keys = MemoryKeyStore::default(); - let ports = CorePorts { kv: Rc::new(kv), session_keys: Some(Rc::new(old_keys.clone())), ..CorePorts::default() }; - let old = core_for_ports(&first, &phone, Rc::new(Spy::default()), ports).await; - old.start(); - old.dispatch(Intent::SetBackupRelay(first.url.clone())).await; - // Nothing on the relay yet: this phone's is saved. - answer_backup_req(&mut first, &phone.pubkey_hex, &[]).await; - let event = next_frame_of(&mut first, "EVENT").await[1].clone(); - assert_eq!(event["kind"], 30078); - let wire = event.to_string(); - let old_secret = old_keys.ring().current.keypair.secret_hex(); - assert!(!wire.contains("my laptop") && !wire.contains("Work lab") && !wire.contains(&machine.pubkey_hex)); - assert!(!wire.contains(&old_secret)); - first.push(serde_json::json!(["OK", event["id"], true, ""]).to_string()); - wait_for_async(|| { - let old = old.clone(); - async move { old.settings_view().await.unwrap().backup.saved_at.is_some() } - }) - .await; - let saved = settings_backup(&old).await; - assert_eq!(saved.relay.as_deref(), Some(first.url.as_str())); - assert!(saved.saved_at.is_some()); - - // Another device with the same identity turns backup on: the - // machine is merged in at once, with no question asked. - let mut second = mock_relay().await; - let new_keys = MemoryKeyStore::default(); - let ports = CorePorts { session_keys: Some(Rc::new(new_keys.clone())), ..CorePorts::default() }; - let new = core_for_ports(&second, &phone, Rc::new(Spy::default()), ports).await; - new.start(); - new.dispatch(Intent::SetBackupRelay(second.url.clone())).await; - answer_backup_req(&mut second, &phone.pubkey_hex, &[event]).await; - // The backup's "the relay answered" flag is private, and both the - // merged machine and an Idle status can be observed while its EOSE - // is still in flight, so no view names the point where a save is - // allowed to run. Give the answer time to be folded in. - settle().await; - let machines = new.machines_view().await.machines; - let restored = &machines[&machine.pubkey_hex]; - assert_eq!((restored.label.as_deref(), restored.relays.clone()), (Some("Work lab"), vec![first.url.clone()])); - assert!(matches!(settings_backup(&new).await.status, crate::backup::BackupStatus::Idle)); - // Its session keys stay its own: it is a client of its own. - assert_ne!(new_keys.ring().current.keypair.secret_hex(), old_secret); - - // Its own edit goes into what it saves, the merged machine with it. - new.dispatch(Intent::SetDirectEndpoints { - machine: machine.pubkey_hex.clone(), - endpoints: vec!["wss://laptop.lan:7447".into()], - }) - .await; - new.dispatch(Intent::BackupNow).await; - let saved = next_frame_of(&mut second, "EVENT").await[1].clone(); - let plain = protocol::crypto::decrypt_from(&phone.secret_key, &phone.pubkey_hex, saved["content"].as_str().unwrap()).unwrap(); - let doc = client_core::stores::backup::decode_backup(&plain).unwrap(); - assert_eq!(doc.machines.len(), 1); - assert_eq!(doc.machines[0].direct_endpoints, ["wss://laptop.lan:7447"]); - assert!(!plain.contains(&old_secret) && !plain.contains("secretHex"), "no session keys in the backup"); - }) - .await; -} - -#[tokio::test] -async fn a_relay_address_the_phone_may_not_dial_is_refused() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; - core.dispatch(Intent::SetBackupRelay("ws://plain.example".into())).await; - let backup = settings_backup(&core).await; - assert_eq!(backup.relay, None); - assert!(matches!(backup.status, crate::backup::BackupStatus::Failed { .. })); - }) - .await; -} - -// --- what the client watches: the bridge sends live text only for it --- - /// A started core that `machine` knows by the session key `key`. -async fn core_watched_by(mock: &MockRelay, phone: &Keypair, key: &SessionKey, machine: &Keypair) -> Core { +pub(super) async fn core_watched_by(mock: &MockRelay, phone: &Keypair, key: &SessionKey, machine: &Keypair) -> Core { let store = MemoryKeyStore::default(); *store.0.borrow_mut() = Some(SessionKeyRing::single(key.clone()).encode()); let mut state = client_core::stores::machines::MachinesState::default(); @@ -3860,7 +333,7 @@ async fn core_watched_by(mock: &MockRelay, phone: &Keypair, key: &SessionKey, ma } /// Every `presence` this client sends `machine` within `wait`. -async fn presences_within( +pub(super) async fn presences_within( mock: &mut MockRelay, payload_key: &str, machine: &Keypair, @@ -3886,373 +359,3 @@ async fn presences_within( } sent } - -/// Opening a session tells its bridge to send that session's live text, and -/// leaving it tells the bridge to stop; both after the coalescing window. -#[tokio::test] -async fn the_session_on_screen_is_what_the_bridge_is_told() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; - let core = core_watched_by(&mock, &phone, &key, &machine).await; - eose_all(&mut mock).await; - let refresh = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - - core.dispatch(Intent::SelectSession { machine: machine.pubkey_hex.clone(), session_id: Some("s1".into()) }).await; - let opened = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; - assert_eq!(opened.len(), 1, "{opened:?}"); - assert!(!opened[0].away); - assert_eq!(opened[0].watching.as_deref(), Some("s1"), "the session on screen"); - - core.dispatch(Intent::SelectSession { machine: machine.pubkey_hex.clone(), session_id: None }).await; - let left = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; - assert_eq!(left.len(), 1, "{left:?}"); - assert_eq!(left[0].watching, None, "nothing is on screen any more"); - }) - .await; -} - -/// Backgrounding the app stops its live text: the bridge is told nothing is -/// on screen, and told again which session when the app comes back. -#[tokio::test] -async fn backgrounding_the_app_stops_the_live_text() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; - let core = core_watched_by(&mock, &phone, &key, &machine).await; - eose_all(&mut mock).await; - let refresh = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - core.dispatch(Intent::SelectSession { machine: machine.pubkey_hex.clone(), session_id: Some("s1".into()) }).await; - assert_eq!(presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await.len(), 1); - - core.pause(); - // Hiding is debounced (VISIBILITY_DEBOUNCE_MS), and the change is - // then coalesced (WATCH_SETTLE_MS) before it is sent. - let hidden = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(1400)).await; - assert_eq!(hidden.len(), 1, "{hidden:?}"); - assert_eq!(hidden[0].watching, None, "a backgrounded app shows nothing"); - - core.resume(); - let back = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; - assert_eq!(back.len(), 1, "{back:?}"); - assert_eq!(back[0].watching.as_deref(), Some("s1"), "the session is on screen again"); - }) - .await; -} - -/// Flicking through sessions is one `presence`, for the one left on screen. -#[tokio::test] -async fn switching_sessions_quickly_sends_one_presence() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; - let core = core_watched_by(&mock, &phone, &key, &machine).await; - eose_all(&mut mock).await; - let refresh = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; - assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); - - for session in ["s1", "s2", "s3"] { - core.dispatch(Intent::SelectSession { - machine: machine.pubkey_hex.clone(), - session_id: Some(session.into()), - }) - .await; - } - let sent = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; - assert_eq!(sent.len(), 1, "one presence for the whole burst: {sent:?}"); - assert_eq!(sent[0].watching.as_deref(), Some("s3"), "the one left on screen"); - }) - .await; -} - -/// Every command this client sends `machine` within `wait`. -async fn commands_within( - mock: &mut MockRelay, - payload_key: &str, - machine: &Keypair, - wait: Duration, -) -> Vec { - let deadline = std::time::Instant::now() + wait; - let mut sent = Vec::new(); - loop { - let left = deadline.saturating_duration_since(std::time::Instant::now()); - if left.is_zero() { - break; - } - let Some(frame) = mock.frame_within(left).await else { break }; - let Ok(v) = serde_json::from_str::>(&frame) else { continue }; - if v[0] != "EVENT" { - continue; - } - let Ok(ev) = serde_json::from_value::(v[1].clone()) else { continue }; - let Ok(plain) = protocol::crypto::decrypt_from(&machine.secret_key, payload_key, &ev.content) else { continue }; - if let Ok(msg) = protocol::codec::decode_phone_to_bridge(&plain) { - sent.push(msg); - } - } - sent -} - -/// Every `terminal-input` this client sends `machine` within `wait`. -async fn terminal_inputs_within( - mock: &mut MockRelay, - payload_key: &str, - machine: &Keypair, - wait: Duration, -) -> Vec { - commands_within(mock, payload_key, machine, wait) - .await - .into_iter() - .filter_map(|m| match m { - PhoneToBridge::TerminalInput(m) => Some(m), - _ => None, - }) - .collect() -} - -/// The terminal on screen is what the bridge is told to send output for: -/// attaching names it in the presence, and detaching stops it. Nothing is -/// watched otherwise, so a terminal costs a backgrounded phone nothing. -#[tokio::test] -async fn the_terminal_on_screen_is_what_the_bridge_is_told() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; - let core = core_watched_by(&mock, &phone, &key, &machine).await; - eose_all(&mut mock).await; - let _ = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; - - core.dispatch(Intent::AttachTerminal { machine: machine.pubkey_hex.clone(), term: "t1".into() }) - .await; - let sent = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; - assert!( - sent.iter().any(|p| p.watching_terminal.as_deref() == Some("t1")), - "the bridge must be told which terminal is on screen: {sent:?}" - ); - - core.dispatch(Intent::DetachTerminal { machine: machine.pubkey_hex.clone() }).await; - let sent = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; - assert!( - sent.iter().any(|p| p.watching_terminal.is_none() && p.watching.is_none()), - "detaching must stop the output: {sent:?}" - ); - }) - .await; -} - -/// Typing is paced by the core, not sent per keystroke: printable bytes wait -/// and go with the key a program acts on, as one sequenced command. -#[tokio::test] -async fn typing_into_a_terminal_goes_out_as_one_batched_command() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; - let core = core_watched_by(&mock, &phone, &key, &machine).await; - eose_all(&mut mock).await; - let _ = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; - - core.dispatch(Intent::AttachTerminal { machine: machine.pubkey_hex.clone(), term: "t1".into() }) - .await; - core.dispatch(Intent::TerminalInput { - machine: machine.pubkey_hex.clone(), - term: "t1".into(), - bytes: b"git che".to_vec(), - alternate_screen: false, - }) - .await; - // Nothing yet: the bytes are waiting for a key a program acts on. - assert!(terminal_inputs_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(120)) - .await - .is_empty()); - - core.dispatch(Intent::TerminalInput { - machine: machine.pubkey_hex.clone(), - term: "t1".into(), - bytes: b"\t".to_vec(), - alternate_screen: false, - }) - .await; - let sent = terminal_inputs_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; - assert_eq!(sent.len(), 1, "one command for the whole line: {sent:?}"); - assert_eq!(sent[0].seq, 1); - assert_eq!(sent[0].data, "Z2l0IGNoZQk=", "git che plus the Tab"); - }) - .await; -} - -/// A key held back for the next batch redraws the app, whose predicted echo -/// shows it at once; the send that empties the batch redraws it again. -#[tokio::test] -async fn keys_held_back_redraw_the_predicted_echo() { - LocalSet::new() - .run_until(async { - let mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - - core.dispatch(Intent::AttachTerminal { machine: machine.pubkey_hex.clone(), term: "t1".into() }) - .await; - wait_for_async(|| { - let core = core.clone(); - async move { core.ui_view().await.terminals.attached.is_some() } - }) - .await; - let ui_changes = || { - spy.events - .lock() - .unwrap() - .iter() - .filter(|e| matches!(e, CoreEvent::StateChanged { slice: SliceId::Ui })) - .count() - }; - let before = ui_changes(); - core.dispatch(Intent::TerminalInput { - machine: machine.pubkey_hex.clone(), - term: "t1".into(), - bytes: b"l".to_vec(), - alternate_screen: false, - }) - .await; - wait_for_async(|| { - let core = core.clone(); - async move { - core.ui_view().await.terminals.attached.unwrap().waiting_input == b"l".to_vec() - } - }) - .await; - assert_eq!(core.ui_view().await.terminals.attached.unwrap().waiting_input, b"l".to_vec()); - assert!(ui_changes() > before, "a held key must redraw the app"); - - let before = ui_changes(); - core.dispatch(Intent::TerminalInput { - machine: machine.pubkey_hex.clone(), - term: "t1".into(), - bytes: b"\r".to_vec(), - alternate_screen: false, - }) - .await; - wait_for(|| ui_changes() > before).await; - assert!(core.ui_view().await.terminals.attached.unwrap().waiting_input.is_empty()); - assert!(ui_changes() > before, "the send must clear the prediction"); - }) - .await; -} - -/// Terminal output never goes through a view: it arrives as its own event, -/// already base64-decoded, with the offset it starts at. -#[tokio::test] -async fn terminal_output_reaches_the_host_on_its_own_event() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let spy = Rc::new(Spy::default()); - let core = core_for(&mock, &phone, Rc::clone(&spy)).await; - core.set_machines(vec![machine.pubkey_hex.clone()]); - core.start(); - eose_all(&mut mock).await; - - core.dispatch(Intent::AttachTerminal { machine: machine.pubkey_hex.clone(), term: "t1".into() }) - .await; - wait_for_async(|| { - let core = core.clone(); - async move { core.ui_view().await.terminals.attached.is_some() } - }) - .await; - - // "hi" in base64, the shell's first output at offset 0. - push_bridge_to_phone_event_at( - &mock, - &machine, - &phone.pubkey_hex, - "cd-1", - &BridgeToPhone::TerminalOutput(protocol::events::TerminalOutputMsg { - term: "t1".into(), - offset: 0, - data: "aGk=".into(), - replay: None, - }), - LIVE_KIND, - None, - ); - wait_for(|| { - spy.events.lock().unwrap().iter().any(|e| matches!( - e, - CoreEvent::TerminalOutput { term, offset: 0, bytes, .. } - if term == "t1" && bytes == b"hi" - )) - }) - .await; - - { - let events = spy.events.lock().unwrap(); - assert!( - events.iter().any(|e| matches!( - e, - CoreEvent::TerminalOutput { term, offset: 0, bytes, .. } - if term == "t1" && bytes == b"hi" - )), - "the frame must arrive as its own event: {events:?}" - ); - } - let view = core.ui_view().await; - let attached = view.terminals.attached.expect("a terminal on screen"); - assert_eq!(attached.expected_offset, 2, "the screen advanced past what it drew"); - }) - .await; -} - -/// A bridge that does not advertise terminals never opens one: the command -/// would wait on an answer that never comes. -#[tokio::test] -async fn opening_a_terminal_needs_the_bridge_to_advertise_it() { - LocalSet::new() - .run_until(async { - let mut mock = mock_relay().await; - let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); - let machine = generate_keypair(); - let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; - // `core_watched_by` registers a machine with no capabilities. - let core = core_watched_by(&mock, &phone, &key, &machine).await; - eose_all(&mut mock).await; - let _ = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; - - core.dispatch(Intent::OpenTerminal { - machine: machine.pubkey_hex.clone(), - session: None, - cols: 80, - rows: 24, - }) - .await; - - assert!(core.ui_view().await.terminals.attached.is_none(), "nothing on screen"); - let sent = commands_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(400)).await; - assert!( - !sent.iter().any(|m| matches!(m, PhoneToBridge::TerminalOpen(_))), - "no open went out: {sent:?}" - ); - }) - .await; -} diff --git a/crates/client-runtime/src/runtime/tests/backup.rs b/crates/client-runtime/src/runtime/tests/backup.rs new file mode 100644 index 00000000..9cfc3424 --- /dev/null +++ b/crates/client-runtime/src/runtime/tests/backup.rs @@ -0,0 +1,256 @@ +use super::*; + +/// The next frame of `kind` (`REQ`, `EVENT`, …), skipping the others. +async fn next_frame_of(mock: &mut MockRelay, kind: &str) -> Vec { + for _ in 0..20 { + let frame = mock.next_frame().await; + let v: Vec = serde_json::from_str(&frame).unwrap(); + if v[0] == kind { + return v; + } + } + panic!("no {kind} frame"); +} + +/// The backup look's REQ: answered with `events` then EOSE. Returns the +/// subscription's id. +async fn answer_backup_req(mock: &mut MockRelay, me: &str, events: &[serde_json::Value]) -> String { + loop { + let req = next_frame_of(mock, "REQ").await; + let sub = req[1].as_str().unwrap().to_string(); + if req[2]["#d"].is_array() { + assert_eq!(req[2]["kinds"], serde_json::json!([30078])); + assert_eq!(req[2]["authors"], serde_json::json!([me])); + for e in events { + mock.push(serde_json::json!(["EVENT", sub, e]).to_string()); + } + mock.push(format!(r#"["EOSE","{sub}"]"#)); + return sub; + } + mock.push(format!(r#"["EOSE","{sub}"]"#)); + } +} + +/// Backup turned on with `mock` as its relay, holding nothing yet: the +/// core saves its own, which the relay takes. Returns the backup +/// subscription's id. +async fn backup_on(core: &Core, mock: &mut MockRelay, me: &str) -> String { + core.dispatch(Intent::SetBackupRelay(mock.url.clone())).await; + let sub = answer_backup_req(mock, me, &[]).await; + let event = next_frame_of(mock, "EVENT").await[1].clone(); + mock.push(serde_json::json!(["OK", event["id"], true, ""]).to_string()); + wait_for_async(|| { + let core = core.clone(); + async move { core.settings_view().await.unwrap().backup.saved_at.is_some() } + }) + .await; + sub +} + +#[tokio::test] +async fn the_backup_relay_hears_only_the_backup_and_the_bridge_relays_never_do() { + LocalSet::new() + .run_until(async { + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let mut bridge_relay = mock_relay().await; + let mut backup_relay = mock_relay().await; + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "laptop", None, None, &[bridge_relay.url.clone()]); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let ports = CorePorts { kv: Rc::new(kv), ..CorePorts::default() }; + let core = core_for_ports(&bridge_relay, &phone, Rc::new(Spy::default()), ports).await; + core.start(); + core.dispatch(Intent::SetBackupRelay(backup_relay.url.clone())).await; + let req = next_frame_of(&mut backup_relay, "REQ").await; + assert!(req[2]["#d"].is_array(), "the backup relay is asked for the backup: {req:?}"); + let sub = req[1].as_str().unwrap(); + backup_relay.push(format!(r#"["EOSE","{sub}"]"#)); + while let Some(frame) = backup_relay.frame_within(Duration::from_millis(400)).await { + assert!(!frame.starts_with(r#"["REQ""#), "the backup relay sees what the phone follows: {frame}"); + } + while let Some(frame) = bridge_relay.frame_within(Duration::from_millis(200)).await { + let v: Vec = serde_json::from_str(&frame).unwrap(); + assert!(v[0] != "REQ" || v[2].get("#d").is_none(), "a bridge relay sees the backup's REQ: {frame}"); + } + }) + .await; +} + +#[tokio::test] +async fn a_deletion_on_another_device_turns_backup_off_here() { + LocalSet::new() + .run_until(async { + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let mut mock = mock_relay().await; + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + core.start(); + let sub = backup_on(&core, &mut mock, &phone.pubkey_hex).await; + assert!(settings_backup(&core).await.relay.is_some()); + // Another device deletes it: the empty version takes its place. + let signer = crate::signer::LocalSigner(phone.clone()); + let removed = crate::backup::seal_removed(&signer, 5_000).await.unwrap(); + mock.push(serde_json::json!(["EVENT", sub, removed]).to_string()); + wait_for_async(|| { + let core = core.clone(); + async move { + matches!( + core.settings_view().await.unwrap().backup.status, + crate::backup::BackupStatus::Failed { .. } + ) + } + }) + .await; + let backup = settings_backup(&core).await; + assert_eq!(backup.relay, None); + assert!(matches!(backup.status, crate::backup::BackupStatus::Failed { reason } if reason.contains("another device"))); + }) + .await; +} + +#[tokio::test] +async fn a_device_joining_after_a_deletion_saves_its_own() { + LocalSet::new() + .run_until(async { + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let mut mock = mock_relay().await; + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + core.start(); + let signer = crate::signer::LocalSigner(phone.clone()); + let removed = crate::backup::seal_removed(&signer, 900).await.unwrap(); + core.dispatch(Intent::SetBackupRelay(mock.url.clone())).await; + answer_backup_req(&mut mock, &phone.pubkey_hex, &[serde_json::to_value(&removed).unwrap()]).await; + let saved = next_frame_of(&mut mock, "EVENT").await[1].clone(); + assert_eq!(saved["kind"], 30078); + assert!(!saved["content"].as_str().unwrap().is_empty()); + assert!(saved["created_at"].as_u64().unwrap() > 900, "dated past the empty version"); + assert!(settings_backup(&core).await.relay.is_some()); + }) + .await; +} + +#[tokio::test] +async fn a_backup_relay_ending_the_subscription_shows_as_a_failure() { + LocalSet::new() + .run_until(async { + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let mut mock = mock_relay().await; + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + core.start(); + let sub = backup_on(&core, &mut mock, &phone.pubkey_hex).await; + mock.push(serde_json::json!(["CLOSED", sub, "restricted: members only"]).to_string()); + wait_for_async(|| { + let core = core.clone(); + async move { + matches!( + core.settings_view().await.unwrap().backup.status, + crate::backup::BackupStatus::Failed { .. } + ) + } + }) + .await; + let backup = settings_backup(&core).await; + assert!(backup.relay.is_some(), "backup stays on, and is asked for again later"); + assert!(matches!(backup.status, crate::backup::BackupStatus::Failed { reason } if reason.contains("members only"))); + }) + .await; +} + +async fn settings_backup(core: &Core) -> crate::view::BackupView { + core.settings_view().await.unwrap().backup +} + +#[tokio::test] +async fn a_backup_saved_by_one_phone_restores_the_machines_on_another() { + LocalSet::new() + .run_until(async { + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + + // The old phone, paired with a machine, turns the backup on. + let mut first = mock_relay().await; + let mut state = client_core::stores::machines::MachinesState::default(); + // Names with spaces: base64 ciphertext can never contain them by chance. + state.register_machine(&machine.pubkey_hex, "my laptop", Some("Work lab".into()), None, &[first.url.clone()]); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let old_keys = MemoryKeyStore::default(); + let ports = CorePorts { kv: Rc::new(kv), session_keys: Some(Rc::new(old_keys.clone())), ..CorePorts::default() }; + let old = core_for_ports(&first, &phone, Rc::new(Spy::default()), ports).await; + old.start(); + old.dispatch(Intent::SetBackupRelay(first.url.clone())).await; + // Nothing on the relay yet: this phone's is saved. + answer_backup_req(&mut first, &phone.pubkey_hex, &[]).await; + let event = next_frame_of(&mut first, "EVENT").await[1].clone(); + assert_eq!(event["kind"], 30078); + let wire = event.to_string(); + let old_secret = old_keys.ring().current.keypair.secret_hex(); + assert!(!wire.contains("my laptop") && !wire.contains("Work lab") && !wire.contains(&machine.pubkey_hex)); + assert!(!wire.contains(&old_secret)); + first.push(serde_json::json!(["OK", event["id"], true, ""]).to_string()); + wait_for_async(|| { + let old = old.clone(); + async move { old.settings_view().await.unwrap().backup.saved_at.is_some() } + }) + .await; + let saved = settings_backup(&old).await; + assert_eq!(saved.relay.as_deref(), Some(first.url.as_str())); + assert!(saved.saved_at.is_some()); + + // Another device with the same identity turns backup on: the + // machine is merged in at once, with no question asked. + let mut second = mock_relay().await; + let new_keys = MemoryKeyStore::default(); + let ports = CorePorts { session_keys: Some(Rc::new(new_keys.clone())), ..CorePorts::default() }; + let new = core_for_ports(&second, &phone, Rc::new(Spy::default()), ports).await; + new.start(); + new.dispatch(Intent::SetBackupRelay(second.url.clone())).await; + answer_backup_req(&mut second, &phone.pubkey_hex, &[event]).await; + // The backup's "the relay answered" flag is private, and both the + // merged machine and an Idle status can be observed while its EOSE + // is still in flight, so no view names the point where a save is + // allowed to run. Give the answer time to be folded in. + settle().await; + let machines = new.machines_view().await.machines; + let restored = &machines[&machine.pubkey_hex]; + assert_eq!((restored.label.as_deref(), restored.relays.clone()), (Some("Work lab"), vec![first.url.clone()])); + assert!(matches!(settings_backup(&new).await.status, crate::backup::BackupStatus::Idle)); + // Its session keys stay its own: it is a client of its own. + assert_ne!(new_keys.ring().current.keypair.secret_hex(), old_secret); + + // Its own edit goes into what it saves, the merged machine with it. + new.dispatch(Intent::SetDirectEndpoints { + machine: machine.pubkey_hex.clone(), + endpoints: vec!["wss://laptop.lan:7447".into()], + }) + .await; + new.dispatch(Intent::BackupNow).await; + let saved = next_frame_of(&mut second, "EVENT").await[1].clone(); + let plain = protocol::crypto::decrypt_from(&phone.secret_key, &phone.pubkey_hex, saved["content"].as_str().unwrap()).unwrap(); + let doc = client_core::stores::backup::decode_backup(&plain).unwrap(); + assert_eq!(doc.machines.len(), 1); + assert_eq!(doc.machines[0].direct_endpoints, ["wss://laptop.lan:7447"]); + assert!(!plain.contains(&old_secret) && !plain.contains("secretHex"), "no session keys in the backup"); + }) + .await; +} + +#[tokio::test] +async fn a_relay_address_the_phone_may_not_dial_is_refused() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + core.dispatch(Intent::SetBackupRelay("ws://plain.example".into())).await; + let backup = settings_backup(&core).await; + assert_eq!(backup.relay, None); + assert!(matches!(backup.status, crate::backup::BackupStatus::Failed { .. })); + }) + .await; +} diff --git a/crates/client-runtime/src/runtime/tests/connection.rs b/crates/client-runtime/src/runtime/tests/connection.rs new file mode 100644 index 00000000..be6de2c1 --- /dev/null +++ b/crates/client-runtime/src/runtime/tests/connection.rs @@ -0,0 +1,778 @@ +use super::*; + +/// The connect deadline the `fast_reconnect` config uses, for advancing the +/// fake clock. +fn connect_deadline() -> u64 { + fast_reconnect().connect_deadline_ms +} + +#[tokio::test] +async fn start_subscribes_and_reports_connected_after_all_eose() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + + eose_all(&mut mock).await; + wait_for(|| spy.statuses.lock().unwrap().last().is_some_and(|(s, _)| *s == ConnectionStatus::Connected)) + .await; + + let statuses = spy.statuses.lock().unwrap().clone(); + assert!( + statuses.iter().any(|(s, _)| *s == ConnectionStatus::Connecting), + "{statuses:?}" + ); + assert_eq!(statuses.last().unwrap().0, ConnectionStatus::Connected); + // The relay that actually opened is what Settings' per-relay + // dot should read — not an empty placeholder. + assert_eq!( + spy.connected_relays.lock().unwrap().last().unwrap(), + &vec![mock.url.clone()], + ); + }) + .await; +} + +/// One relay of two dying leaves overall `ConnectionStatus` untouched +/// (`NostrClient::on_close` only fires once EVERY relay for a +/// subscription is dead), so `dispatch`'s status-transition gate never +/// runs. The transport reports the relay going down itself, so Settings' +/// per-relay dot follows at once rather than at the watchdog's next tick. +#[tokio::test] +async fn a_relay_dying_while_another_survives_is_reported_at_once() { + LocalSet::new() + .run_until(async { + let mut mock1 = mock_relay().await; + let mut mock2 = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy: None, + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + CorePorts::default(), + Rc::clone(&spy) as Rc, + Rc::new(FixedClock(RefCell::new(1_000_000))), + Rc::new(ZeroEntropy), + ) + .await; + core.set_relays(vec![mock1.url.clone(), mock2.url.clone()]); + + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + + eose_all(&mut mock1).await; + eose_all(&mut mock2).await; + wait_for(|| spy.connected_relays.lock().unwrap().last().is_some_and(|r| r.len() == 2)).await; + + let before = spy.connected_relays.lock().unwrap().last().unwrap().clone(); + assert_eq!(before.len(), 2, "{before:?}"); + + // Real time only, far short of the watchdog's 30 s tick. + mock2.close(); + wait_for(|| { + spy.connected_relays + .lock() + .unwrap() + .last() + .is_some_and(|r| r == &vec![mock1.url.clone()]) + }) + .await; + + let after = spy.connected_relays.lock().unwrap().last().unwrap().clone(); + assert_eq!(after, vec![mock1.url.clone()], "{after:?}"); + }) + .await; +} + +#[tokio::test] +async fn a_bridge_message_is_decrypted_decoded_and_delivered() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + + let msg = protocol::codec::decode_bridge_to_phone( + r#"{"v":12,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#, + ) + .unwrap(); + let plaintext = encode_bridge_to_phone(&msg); + let ct = protocol::crypto::encrypt_to( + &machine.secret_key, + &phone.pubkey_hex, + &plaintext, + ) + .unwrap(); + let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) + .tag(recipient(&machine, &phone.pubkey_hex)) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + mock.push(format!( + r#"["EVENT","cd-3",{}]"#, + ::as_json(&event) + )); + wait_for(|| spy.messages.lock().unwrap().len() == 1).await; + + let messages = spy.messages.lock().unwrap(); + assert_eq!(messages.len(), 1, "{messages:?}"); + assert_eq!(messages[0].0, machine.pubkey_hex); + assert!(matches!(messages[0].1, BridgeToPhone::InputAck(_))); + }) + .await; +} + +#[tokio::test] +async fn a_bridge_of_another_version_is_marked_until_it_is_read_again() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + // Paired, so the machines store knows it. + core.dispatch(Intent::BeginManualPairing { + npub: machine.npub.clone(), + token: "tok".into(), + relays: mock.url.clone(), + label: "laptop".into(), + }) + .await; + let sub = drain_traffic_resubscribe(&mut mock).await; + let ack = BridgeToPhone::PairAck(protocol::events::PairAckMsg { + machine: "laptop".into(), + ok: true, + reason: None, + relays: None, + host: None, + }); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &ack); + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { core.machines_view().await.machines.contains_key(&key) } + }) + .await; + let sub = drain_traffic_resubscribe(&mut mock).await; + + let push_raw = |plaintext: &str| { + let ct = protocol::crypto::encrypt_to(&machine.secret_key, &phone.pubkey_hex, plaintext).unwrap(); + let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) + .tag(recipient(&machine, &phone.pubkey_hex)) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + mock.push(format!(r#"["EVENT","{sub}",{}]"#, ::as_json(&event))); + }; + let mark = || async { + core.machines_view().await.machines[&machine.pubkey_hex].incompatible.map(|i| i.theirs) + }; + assert_eq!(mark().await, None); + + push_raw(r#"{"v":13,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#); + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { + core.machines_view().await.machines[&key].incompatible.map(|i| i.theirs) == Some(Some(13)) + } + }) + .await; + assert_eq!(mark().await, Some(Some(13))); + let delivered = spy.messages.lock().unwrap().len(); + + push_raw(r#"{"v":12,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { core.machines_view().await.machines[&key].incompatible.is_none() } + }) + .await; + assert_eq!(mark().await, None); + assert_eq!(spy.messages.lock().unwrap().len(), delivered + 1); + }) + .await; +} + +#[tokio::test] +async fn a_notify_worthy_event_while_backgrounded_fires_the_ping_core_event() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + + // Backgrounded — `decide_ping` wants a chime unconditionally + // once the app isn't visible, for any notify-worthy event. + core.pause(); + + let msg = protocol::codec::decode_bridge_to_phone( + r#"{"v":12,"type":"session-failed","pendingId":"p1","reason":"boom"}"#, + ) + .unwrap(); + let plaintext = encode_bridge_to_phone(&msg); + let ct = protocol::crypto::encrypt_to( + &machine.secret_key, + &phone.pubkey_hex, + &plaintext, + ) + .unwrap(); + let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) + .tag(recipient(&machine, &phone.pubkey_hex)) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + mock.push(format!( + r#"["EVENT","cd-1",{}]"#, + ::as_json(&event) + )); + wait_for(|| spy.events.lock().unwrap().contains(&CoreEvent::Ping)).await; + + let events = spy.events.lock().unwrap(); + assert!(events.contains(&CoreEvent::Ping), "{events:?}"); + }) + .await; +} + +#[tokio::test] +async fn create_folder_round_trips_to_a_matching_folder_ack_core_event() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + + core.dispatch(Intent::CreateFolder { + machine: machine.pubkey_hex.clone(), + path: "sub/dir".into(), + root: None, + request_id: "req-1".into(), + }) + .await; + + // The dispatched command reaches the bridge as a real, signed + // create-folder event — same publish path every other Intent uses. + let frame = mock.next_frame().await; + let v: serde_json::Value = serde_json::from_str(&frame).unwrap(); + assert_eq!(v[0], "EVENT"); + + let msg = protocol::codec::decode_bridge_to_phone( + r#"{"v":12,"type":"folder-ack","requestId":"req-1","success":true,"path":"sub/dir"}"#, + ) + .unwrap(); + let plaintext = encode_bridge_to_phone(&msg); + let ct = protocol::crypto::encrypt_to( + &machine.secret_key, + &phone.pubkey_hex, + &plaintext, + ) + .unwrap(); + let event = nostr::EventBuilder::new(nostr::Kind::Custom(RESPONSE_KIND), ct) + .tag(recipient(&machine, &phone.pubkey_hex)) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + mock.push(format!( + r#"["EVENT","cd-1",{}]"#, + ::as_json(&event) + )); + wait_for(|| { + spy.events.lock().unwrap().iter().any(|e| matches!( + e, + CoreEvent::FolderAck { request_id, success: true, path: Some(p), error: None } + if request_id == "req-1" && p == "sub/dir" + )) + }) + .await; + + let events = spy.events.lock().unwrap(); + assert!( + events.iter().any(|e| matches!( + e, + CoreEvent::FolderAck { request_id, success: true, path: Some(p), error: None } + if request_id == "req-1" && p == "sub/dir" + )), + "{events:?}", + ); + }) + .await; +} + +/// A fresh store asks for stored responses from its first start (less the +/// grace), not the identity's whole history: what earlier installs were +/// sent is under keys this one does not hold. +#[tokio::test] +async fn a_fresh_store_asks_for_stored_responses_from_its_start() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.start(); + let since = loop { + let frame = mock.next_frame().await; + let v: Vec = serde_json::from_str(&frame).unwrap(); + if v[0] == "REQ" && v[2]["kinds"][0] == RESPONSE_KIND { + break v[2]["since"].as_i64(); + } + }; + // The test clock reads 1_000 s. + assert_eq!(since, Some(1_000 - crate::nostr_client::STORED_SINCE_GRACE_SECONDS)); + }) + .await; +} + +/// A stored-kind event (4516/30515) advancing `last_stored_seen` used to +/// update only the in-memory `LoopHost` cursor — a restart re-hydrated +/// from the `Kv` at 0 and re-fetched the peer's ENTIRE stored history +/// instead of resuming from where it left off, on every single restart. +#[tokio::test] +async fn a_stored_kind_event_persists_the_cursor_to_kv() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let kv = Rc::new(MemoryKv::new()); + let ports = CorePorts { kv: Rc::clone(&kv) as Rc, ..CorePorts::default() }; + let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + + assert_eq!(kv.get(LAST_STORED_SEEN_KEY).await, None); + + let msg = protocol::codec::decode_bridge_to_phone( + r#"{"v":12,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#, + ) + .unwrap(); + let plaintext = encode_bridge_to_phone(&msg); + let ct = protocol::crypto::encrypt_to( + &machine.secret_key, + &phone.pubkey_hex, + &plaintext, + ) + .unwrap(); + let event = nostr::EventBuilder::new(nostr::Kind::Custom(RESPONSE_KIND), ct) + .tag(recipient(&machine, &phone.pubkey_hex)) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + let created_at = event.created_at.as_secs(); + // "cd-1" is a real, currently-open subscription id (one of the + // three bridge filters `eose_all` just drained) — an id with no + // matching subscription is silently dropped by the transport, + // same as a real relay addressing a closed sub. + mock.push(format!( + r#"["EVENT","cd-1",{}]"#, + ::as_json(&event) + )); + settle().await; + // Held back by the write debounce... + assert_eq!(kv.get(LAST_STORED_SEEN_KEY).await, None); + // ...until it elapses. + tokio::time::sleep(WRITE_DEBOUNCE + Duration::from_millis(200)).await; + assert_eq!( + kv.get(LAST_STORED_SEEN_KEY).await, + Some(created_at.to_string()), + ); + + // A later event is written the moment the app is backgrounded. + let newer = nostr::EventBuilder::new(nostr::Kind::Custom(RESPONSE_KIND), "x") + .custom_created_at(nostr::Timestamp::from(created_at + 10)) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + mock.push(format!( + r#"["EVENT","cd-1",{}]"#, + ::as_json(&newer) + )); + settle().await; + core.pause(); + settle().await; + assert_eq!( + kv.get(LAST_STORED_SEEN_KEY).await, + Some((created_at + 10).to_string()), + ); + }) + .await; +} + +#[tokio::test] +async fn a_socket_drop_backs_off_and_reconnects() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + wait_connected(&core).await; + + mock.close(); + // fast_reconnect base is 40ms: the backoff and its retry follow. + wait_for(|| { + let statuses = spy.statuses.lock().unwrap(); + statuses.iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry) + && statuses.iter().filter(|(s, _)| *s == ConnectionStatus::Connecting).count() >= 2 + }) + .await; + + let statuses = spy.statuses.lock().unwrap().clone(); + assert!( + statuses.iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry), + "{statuses:?}" + ); + assert!( + statuses.iter().filter(|(s, _)| *s == ConnectionStatus::Connecting).count() >= 2, + "expected a second Connecting after backoff: {statuses:?}" + ); + }) + .await; +} + +#[tokio::test] +async fn a_connect_that_never_reaches_eose_is_abandoned_and_retried() { + LocalSet::new() + .run_until(async { + // The relay accepts the socket and reads the REQs, but never + // sends EOSE — the FSM's `SocketOpen`, and the only thing that + // ends `Connecting`. + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let spy = Rc::new(Spy::default()); + let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); + let core = core_for_ports_clock( + &mock, + &phone, + Rc::clone(&spy), + CorePorts::default(), + Rc::clone(&clock), + ) + .await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.start(); + wait_status(&core, ConnectionStatus::Connecting).await; + assert_eq!(core.connection_status().await.0, ConnectionStatus::Connecting); + + *clock.0.borrow_mut() += connect_deadline() + 1_000; + core.tick(); + // fast_reconnect's 40ms retry has fired by now. + wait_for(|| { + let statuses: Vec = + spy.statuses.lock().unwrap().iter().map(|(s, _)| *s).collect(); + statuses.windows(2).any(|w| w == [ConnectionStatus::WaitingRetry, ConnectionStatus::Connecting]) + }) + .await; + + let statuses: Vec = + spy.statuses.lock().unwrap().iter().map(|(s, _)| *s).collect(); + assert!(statuses.contains(&ConnectionStatus::WaitingRetry), "{statuses:?}"); + assert!( + statuses.windows(2).any(|w| w == [ConnectionStatus::WaitingRetry, ConnectionStatus::Connecting]), + "the abandoned attempt went through backoff and retried: {statuses:?}" + ); + }) + .await; +} + +#[tokio::test] +async fn a_paused_connect_is_not_abandoned_and_a_resume_restarts_its_clock() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let spy = Rc::new(Spy::default()); + let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); + let core = core_for_ports_clock( + &mock, + &phone, + Rc::clone(&spy), + CorePorts::default(), + clock.clone(), + ) + .await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.start(); + wait_status(&core, ConnectionStatus::Connecting).await; + assert_eq!(core.connection_status().await.0, ConnectionStatus::Connecting); + + // Hidden past the 500ms visibility debounce, then dozing: wall + // time moves beyond the deadline while the app runs nothing. + core.pause(); + tokio::time::sleep(Duration::from_millis(600)).await; + *clock.0.borrow_mut() += connect_deadline() + 60_000; + core.tick(); + assert_eq!( + core.connection_status().await.0, + ConnectionStatus::Connecting, + "paused time is not spent against the connect attempt" + ); + + // Resume re-dials while still Connecting: that is a new attempt, + // with a fresh window rather than the abandoned one's leftovers. + // The re-dial runs on the fake clock, so let the loop take it in + // before moving that clock. + core.resume(); + settle().await; + *clock.0.borrow_mut() += connect_deadline() - 1_000; + core.tick(); + assert_eq!( + core.connection_status().await.0, + ConnectionStatus::Connecting, + "the resumed attempt gets its own window" + ); + + *clock.0.borrow_mut() += 2_000; + core.tick(); + wait_for(|| { + spy.statuses.lock().unwrap().iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry) + }) + .await; + assert!( + spy.statuses.lock().unwrap().iter().any(|(s, _)| *s == ConnectionStatus::WaitingRetry), + "the resumed attempt is abandoned once its own deadline passes" + ); + }) + .await; +} + +#[tokio::test] +async fn stop_is_terminal_no_reconnect() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.start(); + eose_all(&mut mock).await; + wait_connected(&core).await; + + core.stop(); + wait_status(&core, ConnectionStatus::Stopped).await; + mock.close(); + // Nothing after Stopped, even when the socket closes. + settle().await; + + let statuses = spy.statuses.lock().unwrap().clone(); + assert_eq!(statuses.last().unwrap().0, ConnectionStatus::Stopped); + // nothing after Stopped + let after_stop = statuses + .iter() + .skip_while(|(s, _)| *s != ConnectionStatus::Stopped) + .count(); + assert_eq!(after_stop, 1, "status changed after Stop: {statuses:?}"); + }) + .await; +} + +#[tokio::test] +async fn connection_status_query_reflects_the_live_fsm() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + + assert_eq!(core.connection_status().await.0, ConnectionStatus::Idle); + core.start(); + eose_all(&mut mock).await; + wait_connected(&core).await; + assert_eq!(core.connection_status().await.0, ConnectionStatus::Connected); + core.stop(); + wait_status(&core, ConnectionStatus::Stopped).await; + assert_eq!(core.connection_status().await.0, ConnectionStatus::Stopped); + }) + .await; +} + +/// A machine paired in a PRIOR run is only in the persisted `Kv`, never in +/// `set_machines` — production code never calls that (only a live pairing +/// route/intent populates the subscription author list reactively). A +/// fresh `Core::spawn()` used to leave that list empty for the rest of the +/// process's life unless a NEW pairing happened to run in it: +/// `NostrClient::connect()` treats an empty author list as vacuous and +/// opens zero of the three bridge subscriptions, so a returning phone +/// would never see another heartbeat or session update from a machine it +/// paired before this boot — the root cause behind "the machine dot never +/// leaves orange, and a bridge-confirmed session never appears, except +/// right after pairing". +#[tokio::test] +async fn a_machine_paired_in_a_prior_run_is_resubscribed_on_a_fresh_boot_without_set_machines() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + heard_without_session_keys(&mut state, &machine.pubkey_hex); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let ports = CorePorts { kv: Rc::new(kv), ..CorePorts::default() }; + + let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + // Deliberately no `core.set_machines(...)` — this is the part + // of the boot sequence a real app reopen actually exercises. + core.start(); + + // Hangs (and `next_frame` panics at its 2s budget) if the + // subscription author list is still empty at this point. + eose_all(&mut mock).await; + + // A real heartbeat from that same machine must still reach the + // machines view — proving the subscription actually scopes to + // it, not just that some vacuous socket opened. + let sessions_json = + r#"{"v":12,"type":"sessions","machine":"bridge","sessions":[],"agents":[]}"#; + let msg = protocol::codec::decode_bridge_to_phone(sessions_json).unwrap(); + let plaintext = encode_bridge_to_phone(&msg); + let ct = protocol::crypto::encrypt_to( + &machine.secret_key, + &phone.pubkey_hex, + &plaintext, + ) + .unwrap(); + let event = + nostr::EventBuilder::new(nostr::Kind::Custom(SESSION_LIST_KIND), ct) + .tag(recipient(&machine, &phone.pubkey_hex)) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + mock.push(format!( + r#"["EVENT","cd-1",{}]"#, + ::as_json(&event) + )); + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { + core.machines_view() + .await + .machines + .get(&key) + .is_some_and(|m| m.last_heartbeat_at.is_some()) + } + }) + .await; + + let view = core.machines_view().await; + let m = view.machines.get(&machine.pubkey_hex).expect("machine still known"); + assert!(m.last_heartbeat_at.is_some(), "heartbeat never reached the view"); + }) + .await; +} + +#[tokio::test] +async fn set_relays_repoints_the_transport_to_the_new_relay() { + LocalSet::new() + .run_until(async { + let mut first = mock_relay().await; + let mut second = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = core_for(&first, &phone, Rc::new(Spy::default())).await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.start(); + eose_all(&mut first).await; + wait_connected(&core).await; + + core.set_relays(vec![second.url.clone()]); + // the new relay gets the three REQs; drain them + for _ in 0..3 { + let req = second.next_frame().await; + assert!(req.starts_with(r#"["REQ""#), "got {req}"); + } + }) + .await; +} + +#[tokio::test] +async fn keepalive_probes_the_relays_and_keeps_a_healthy_connection() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.start(); + eose_all(&mut mock).await; + wait_connected(&core).await; + + // The mock answers the probe's ping, so the check is quick + // and leaves the connection as it was. + tokio::time::timeout(Duration::from_secs(2), core.keepalive()) + .await + .expect("keepalive resolves once the relay answers"); + let (status, _, connected) = core.connection_status().await; + assert_eq!(status, ConnectionStatus::Connected); + assert_eq!(connected, vec![mock.url.clone()]); + }) + .await; +} + +#[tokio::test] +async fn a_30515_heartbeat_reaches_the_fsm_even_if_it_is_not_ours() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.start(); + eose_all(&mut mock).await; + + // a signed 30515 from a stranger: no decode (not a paired + // machine), but it must not crash and must not deliver. + let stranger = generate_keypair(); + let event = + nostr::EventBuilder::new(nostr::Kind::Custom(SESSION_LIST_KIND), "garbage") + .sign_with_keys(&nostr::Keys::new(stranger.secret_key.clone())) + .unwrap(); + mock.push(format!( + r#"["EVENT","cd-1",{}]"#, + ::as_json(&event) + )); + settle().await; + + assert!(spy.messages.lock().unwrap().is_empty()); + }) + .await; +} diff --git a/crates/client-runtime/src/runtime/tests/direct.rs b/crates/client-runtime/src/runtime/tests/direct.rs new file mode 100644 index 00000000..d470a799 --- /dev/null +++ b/crates/client-runtime/src/runtime/tests/direct.rs @@ -0,0 +1,109 @@ +use super::*; + +/// A bridge's direct link, scripted: it takes the phone's HELLO, pushes +/// `push` (a message for the phone), then answers every command with an +/// `OK` and hands it to `commands`. +async fn scripted_direct_bridge( + listener: tokio::net::TcpListener, + machine: Keypair, + phone_pubkey: String, + push: BridgeToPhone, + commands: mpsc::UnboundedSender, +) { + use futures_util::{SinkExt, StreamExt}; + use protocol::direct::{check_direct_auth, decode_direct_frame, encode_direct_frame, DirectFrame}; + use tokio_tungstenite::tungstenite::Message; + let (tcp, _) = listener.accept().await.unwrap(); + let mut ws = tokio_tungstenite::accept_async(tcp).await.unwrap(); + let text = |f: &DirectFrame| Message::Text(encode_direct_frame(f)); + ws.send(text(&DirectFrame::Challenge("c1".into()))).await.unwrap(); + let hello = loop { + if let Some(Ok(Message::Text(t))) = ws.next().await { + break decode_direct_frame(&t).unwrap(); + } + }; + let DirectFrame::Hello { auth, .. } = hello else { panic!("expected HELLO") }; + assert_eq!(check_direct_auth(&auth, "c1", auth.created_at).unwrap(), phone_pubkey, "signed by the identity"); + ws.send(text(&DirectFrame::Ready)).await.unwrap(); + + let ct = protocol::crypto::encrypt_to(&machine.secret_key, &phone_pubkey, &encode_bridge_to_phone(&push)).unwrap(); + let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) + .tag(recipient(&machine, &phone_pubkey)) + .tag(nostr::Tag::public_key(nostr::PublicKey::from_hex(&phone_pubkey).unwrap())) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + ws.send(text(&DirectFrame::Event(SignedEvent::from_nostr(&event)))).await.unwrap(); + + loop { + if let Some(Ok(Message::Text(t))) = ws.next().await { + if let Ok(DirectFrame::Event(command)) = decode_direct_frame(&t) { + ws.send(text(&DirectFrame::Ok { id: command.id.clone(), accepted: true, message: String::new() })).await.unwrap(); + let plaintext = protocol::crypto::decrypt_from(&machine.secret_key, &phone_pubkey, &command.content).unwrap(); + let _ = commands.send(protocol::codec::decode_phone_to_bridge(&plaintext).unwrap()); + } + } + } +} + +/// A bridge that advertises a direct endpoint gets a link: its messages +/// arrive over it, the machines view says so, and commands go over it. +#[tokio::test] +async fn a_direct_link_carries_messages_and_commands() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let endpoint = format!("ws://{}", listener.local_addr().unwrap()); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + heard_without_session_keys(&mut state, &machine.pubkey_hex); + state.machines.get_mut(&machine.pubkey_hex).unwrap().direct = + Some(protocol::direct::DirectInfo { endpoints: vec![endpoint.clone()], cert_sha256: None }); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let push = protocol::codec::decode_bridge_to_phone(r#"{"v":12,"type":"input-ack","sessionId":"s1","inputId":"i1"}"#).unwrap(); + let (commands_tx, mut commands) = mpsc::unbounded_channel(); + tokio::task::spawn_local(scripted_direct_bridge( + listener, + machine.clone(), + phone.pubkey_hex.clone(), + push, + commands_tx, + )); + + let spy = Rc::new(Spy::default()); + let core = core_for_ports(&mock, &phone, Rc::clone(&spy), CorePorts { kv: Rc::new(kv), ..CorePorts::default() }).await; + core.start(); + eose_all(&mut mock).await; + for _ in 0..100 { + if spy.messages.lock().unwrap().iter().any(|(_, m)| matches!(m, BridgeToPhone::InputAck(_))) { + break; + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + assert!( + spy.messages.lock().unwrap().iter().any(|(m, msg)| *m == machine.pubkey_hex && matches!(msg, BridgeToPhone::InputAck(_))), + "the pushed message arrived over the link" + ); + assert_eq!(core.machines_view().await.direct_up.get(&machine.pubkey_hex), Some(&endpoint)); + + core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; + // Other commands (the session-key grant) may come first. + tokio::time::timeout(Duration::from_secs(5), async { + loop { + match commands.recv().await { + Some(PhoneToBridge::Interrupt(_)) => break, + Some(_) => {} + None => panic!("the link closed"), + } + } + }) + .await + .expect("the interrupt went over the link in time"); + }) + .await; +} diff --git a/crates/client-runtime/src/runtime/tests/machines.rs b/crates/client-runtime/src/runtime/tests/machines.rs new file mode 100644 index 00000000..d6f052cc --- /dev/null +++ b/crates/client-runtime/src/runtime/tests/machines.rs @@ -0,0 +1,437 @@ +use super::*; + +/// A fresh phone has no relays at all: the pairing link's relays are the +/// first the transport dials, and the pair-request, sent while they are +/// still connecting, reaches them. +#[tokio::test] +async fn a_phone_with_no_relays_pairs_over_the_relays_its_pairing_names() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy: None, + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + CorePorts::default(), + Rc::new(Spy::default()), + Rc::new(FixedClock(RefCell::new(1_000_000))), + Rc::new(ZeroEntropy), + ) + .await; + core.start(); + + core.dispatch(Intent::BeginManualPairing { + npub: machine.npub.clone(), + token: "tok".into(), + relays: mock.url.clone(), + label: "laptop".into(), + }) + .await; + let mut pair_request = false; + for _ in 0..12 { + let frame = mock.next_frame().await; + let v: Vec = serde_json::from_str(&frame).unwrap(); + match v[0].as_str() { + Some("REQ") => mock.push(format!(r#"["EOSE","{}"]"#, v[1].as_str().unwrap())), + Some("EVENT") => { + let tags = v[1]["tags"].as_array().unwrap(); + if tags.iter().any(|t| t[0] == "p" && t[1] == machine.pubkey_hex) { + pair_request = true; + break; + } + } + _ => {} + } + } + assert!(pair_request, "the pair-request never reached the pairing's relay"); + }) + .await; +} + +#[tokio::test] +async fn remove_machine_intent_drops_it_from_the_view_and_erases_its_transcript() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + // `set_machines` alone (as other tests use it) only steers the + // subscription authors filter — it does not populate + // `stores.machines`, so it is not enough to make `RemoveMachine` + // find anything to forget. Only a completed pairing does that. + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + core.dispatch(Intent::BeginManualPairing { + npub: machine.npub.clone(), + token: "tok".into(), + relays: mock.url.clone(), + label: "laptop".into(), + }) + .await; + // Staging the candidate resubscribes (the ack must pass the + // authors filter) — drain that round to get a sub_id the + // router currently has open. + let sub = drain_traffic_resubscribe(&mut mock).await; + push_bridge_to_phone_event( + &mock, + &machine, + &phone.pubkey_hex, + &sub, + &BridgeToPhone::PairAck(protocol::events::PairAckMsg { + machine: "laptop".into(), + ok: true, + reason: None, + relays: None, + host: None, + }), + ); + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { core.machines_view().await.machines.contains_key(&key) } + }) + .await; + assert!(core + .machines_view() + .await + .machines + .contains_key(&machine.pubkey_hex)); + + // Registering the machine resubscribes again — same reason. + let sub = drain_traffic_resubscribe(&mut mock).await; + + // A session must actually be listed (not just have output + // flowing) for `RemoveMachine` to find it — it gathers the + // sessions to forget from `MachineView.sessions`, exactly + // like the TS `removeMachine` it mirrors. + let sessions_msg = protocol::codec::decode_bridge_to_phone( + r#"{"v":12,"type":"sessions","machine":"laptop","sessions":[ + {"id":"s1","agent":"claude-code","slug":"sl","cwd":"/w","lastActivity":"t","lineCount":0,"title":null,"project":"p"} + ],"agents":[]}"#, + ) + .unwrap(); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &sessions_msg); + let listed_key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = listed_key.clone(); + async move { + core.machines_view() + .await + .machines + .get(&key) + .is_some_and(|m| m.sessions.contains_key("s1")) + } + }) + .await; + + let msg = protocol::codec::decode_bridge_to_phone( + r#"{"v":12,"type":"output","sessionId":"s1","seq":1,"entries":[{"entryType":"text","role":"agent","text":"hi","timestamp":"t"}]}"#, + ) + .unwrap(); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &msg); + wait_for_async(|| { + let core = core.clone(); + let key = machine.pubkey_hex.clone(); + async move { core.transcript_view(key, "s1".to_string()).await.rows.len() == 1 } + }) + .await; + + // Sanity: the transcript is really there before forgetting the machine. + let before = core.transcript_view(machine.pubkey_hex.clone(), "s1".to_string()).await; + assert_eq!(before.rows.len(), 1); + + core.dispatch(Intent::RemoveMachine { + pubkey_hex: machine.pubkey_hex.clone(), + }) + .await; + wait_for_async(|| { + let core = core.clone(); + let key = machine.pubkey_hex.clone(); + async move { core.transcript_view(key, "s1".to_string()).await.rows.is_empty() } + }) + .await; + + assert!(!core + .machines_view() + .await + .machines + .contains_key(&machine.pubkey_hex)); + let after = core.transcript_view(machine.pubkey_hex.clone(), "s1".to_string()).await; + assert!(after.rows.is_empty()); + + let events = spy.events.lock().unwrap(); + assert!(events.contains(&CoreEvent::StateChanged { slice: SliceId::Transcript })); + assert!(events.contains(&CoreEvent::StateChanged { slice: SliceId::Machines })); + }) + .await; +} + +#[tokio::test] +async fn remove_machine_for_a_never_paired_pubkey_is_a_harmless_no_op() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + + core.dispatch(Intent::RemoveMachine { + pubkey_hex: "never-paired".into(), + }) + .await; + + assert!(!core.machines_view().await.machines.contains_key("never-paired")); + }) + .await; +} + +#[tokio::test] +async fn the_undo_toast_clears_itself_when_the_window_expires_without_a_tap() { + // Regression: `on_undo_timer` cleared `stores.ui.undo_toast` correctly + // but emitted `StateChanged(Cards)` instead of `StateChanged(Ui)` — a + // `UiView` consumer (the native adapter) never learned to re-fetch, so + // letting the undo window expire without tapping undo left the toast + // showing forever. Waits out the REAL `UNDO_DELAY_MS` (~4s) rather than + // a shortened one: `Core::spawn` hydrates `StoresConfig::default()` + // unconditionally, so the production delay isn't test-overridable here. + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + core.dispatch(Intent::BeginManualPairing { + npub: machine.npub.clone(), + token: "tok".into(), + relays: mock.url.clone(), + label: "laptop".into(), + }) + .await; + let sub = drain_traffic_resubscribe(&mut mock).await; + push_bridge_to_phone_event( + &mock, + &machine, + &phone.pubkey_hex, + &sub, + &BridgeToPhone::PairAck(protocol::events::PairAckMsg { + machine: "laptop".into(), + ok: true, + reason: None, + relays: None, + host: None, + }), + ); + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { core.machines_view().await.machines.contains_key(&key) } + }) + .await; + + let sub = drain_traffic_resubscribe(&mut mock).await; + let sessions_msg = protocol::codec::decode_bridge_to_phone( + r#"{"v":12,"type":"sessions","machine":"laptop","sessions":[ + {"id":"s1","agent":"claude-code","slug":"sl","cwd":"/w","lastActivity":"t","lineCount":0,"title":null,"project":"p"} + ],"agents":[]}"#, + ) + .unwrap(); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, &sub, &sessions_msg); + let listed_key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = listed_key.clone(); + async move { + core.machines_view() + .await + .machines + .get(&key) + .is_some_and(|m| m.sessions.contains_key("s1")) + } + }) + .await; + + core.dispatch(Intent::DeleteSession { + machine: machine.pubkey_hex.clone(), + session_id: "s1".into(), + label: None, + }) + .await; + assert!( + core.ui_view().await.undo_toast.is_some(), + "delete should arm the undo toast" + ); + let ui_events_before = spy + .events + .lock() + .unwrap() + .iter() + .filter(|e| matches!(e, CoreEvent::StateChanged { slice: SliceId::Ui })) + .count(); + + // Let the window elapse for real, without ever dispatching + // UndoDelete. + tokio::time::sleep(std::time::Duration::from_millis( + client_core::delete_controller::UNDO_DELAY_MS + 300, + )) + .await; + + assert!(core.ui_view().await.undo_toast.is_none()); + let ui_events_after = spy + .events + .lock() + .unwrap() + .iter() + .filter(|e| matches!(e, CoreEvent::StateChanged { slice: SliceId::Ui })) + .count(); + assert!( + ui_events_after > ui_events_before, + "the timer firing must emit its own StateChanged(Ui) — a \ + UiView consumer has no other way to learn the toast \ + cleared itself" + ); + }) + .await; +} + +/// A heartbeat listing `ids` for the machine of the session-delete test. +fn heartbeat_listing(ids: &[&str]) -> BridgeToPhone { + let sessions: Vec = ids + .iter() + .map(|id| { + serde_json::json!({"id": id, "agent": "claude-code", "slug": id, "cwd": "/w", + "lastActivity": "t", "lineCount": 0, "title": null, "project": "p"}) + }) + .collect(); + protocol::codec::decode_bridge_to_phone( + &serde_json::json!({ + "v": 12, + "type": "sessions", + "machine": "laptop", + "sessions": sessions, + "agents": [], + "v": protocol::capabilities::PROTOCOL_VERSION, + }) + .to_string(), + ) + .unwrap() +} + +/// Deleting sessions one after another: each delete commits the one before +/// it at once (one `close-session` each), a heartbeat sent before the +/// bridge has closed the later ones does not bring them back, and the +/// last one — still in its undo window — is committed as soon as the app +/// is backgrounded rather than lost with a killed process. +#[tokio::test] +async fn deleting_sessions_in_a_row_closes_each_one_exactly_once() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + heard_without_session_keys(&mut state, &machine.pubkey_hex); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let notifier = RecordingNotifier::new(); + let ports = CorePorts { kv: Rc::new(kv), notifier: Rc::new(notifier.clone()), ..CorePorts::default() }; + let spy = Rc::new(Spy::default()); + let core = core_for_ports(&mock, &phone, Rc::clone(&spy), ports).await; + core.start(); + eose_all(&mut mock).await; + let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); + + let listed = heartbeat_listing(&["s1", "s2", "s3"]); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &listed); + let key = machine.pubkey_hex.clone(); + wait_for_async(|| { + let core = core.clone(); + let key = key.clone(); + async move { core.machines_view().await.machines[&key].sessions.len() == 3 } + }) + .await; + let sessions = |view: MachinesView| -> Vec { + view.machines[&machine.pubkey_hex].sessions.keys().cloned().collect() + }; + assert_eq!(sessions(core.machines_view().await), ["s1", "s2", "s3"]); + + for id in ["s1", "s2", "s3"] { + core.dispatch(Intent::DeleteSession { + machine: machine.pubkey_hex.clone(), + session_id: id.into(), + label: None, + }) + .await; + } + assert!(sessions(core.machines_view().await).is_empty()); + let toast = core.ui_view().await.undo_toast.expect("the last delete is undoable"); + assert_eq!(toast.session_id, "s3"); + + // s1 and s2 were committed by the delete that followed each. + let closed = |cmd: Option| match cmd { + Some(PhoneToBridge::CloseSession(m)) => m.session_id, + other => panic!("expected a close-session, got {other:?}"), + }; + for id in ["s1", "s2"] { + assert_eq!(closed(next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await), id); + } + + // The bridge has closed s1 only; its next list still has the + // other two, which must stay deleted on the phone. + let after_first = heartbeat_listing(&["s2", "s3"]); + // A heartbeat for a session already deleted still notifies; wait + // for that to prove the frame was folded in before asserting it + // does not resurrect anything. + let machine_changes = || { + spy.events + .lock() + .unwrap() + .iter() + .filter(|e| matches!(e, CoreEvent::StateChanged { slice: SliceId::Machines })) + .count() + }; + let before = machine_changes(); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &after_first); + wait_for(|| machine_changes() > before).await; + assert!(sessions(core.machines_view().await).is_empty()); + + // Backgrounded inside s3's undo window: committed now, well + // before the window would have run out. + core.pause(); + let last = tokio::time::timeout( + std::time::Duration::from_millis(client_core::delete_controller::UNDO_DELAY_MS / 2), + next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine), + ) + .await + .expect("the pending delete is sent when the app is backgrounded"); + assert_eq!(closed(last), "s3"); + assert!(core.ui_view().await.undo_toast.is_none()); + // Each one's notifications went with it — s3's only once it was + // deleted for good. + let tag = |id: &str| client_core::notifications::session_notify_tag(&machine.pubkey_hex, id); + assert_eq!(notifier.cancelled(), [tag("s1"), tag("s2"), tag("s3")]); + + // Nothing is left to undo, and nothing is sent twice. + core.dispatch(Intent::UndoDelete).await; + assert!(sessions(core.machines_view().await).is_empty()); + }) + .await; +} diff --git a/crates/client-runtime/src/runtime/tests/presence.rs b/crates/client-runtime/src/runtime/tests/presence.rs new file mode 100644 index 00000000..7ef20174 --- /dev/null +++ b/crates/client-runtime/src/runtime/tests/presence.rs @@ -0,0 +1,92 @@ +use super::*; + +// --- what the client watches: the bridge sends live text only for it --- + +/// Opening a session tells its bridge to send that session's live text, and +/// leaving it tells the bridge to stop; both after the coalescing window. +#[tokio::test] +async fn the_session_on_screen_is_what_the_bridge_is_told() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; + let core = core_watched_by(&mock, &phone, &key, &machine).await; + eose_all(&mut mock).await; + let refresh = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); + + core.dispatch(Intent::SelectSession { machine: machine.pubkey_hex.clone(), session_id: Some("s1".into()) }).await; + let opened = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; + assert_eq!(opened.len(), 1, "{opened:?}"); + assert!(!opened[0].away); + assert_eq!(opened[0].watching.as_deref(), Some("s1"), "the session on screen"); + + core.dispatch(Intent::SelectSession { machine: machine.pubkey_hex.clone(), session_id: None }).await; + let left = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; + assert_eq!(left.len(), 1, "{left:?}"); + assert_eq!(left[0].watching, None, "nothing is on screen any more"); + }) + .await; +} + +/// Backgrounding the app stops its live text: the bridge is told nothing is +/// on screen, and told again which session when the app comes back. +#[tokio::test] +async fn backgrounding_the_app_stops_the_live_text() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; + let core = core_watched_by(&mock, &phone, &key, &machine).await; + eose_all(&mut mock).await; + let refresh = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); + core.dispatch(Intent::SelectSession { machine: machine.pubkey_hex.clone(), session_id: Some("s1".into()) }).await; + assert_eq!(presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await.len(), 1); + + core.pause(); + // Hiding is debounced (VISIBILITY_DEBOUNCE_MS), and the change is + // then coalesced (WATCH_SETTLE_MS) before it is sent. + let hidden = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(1400)).await; + assert_eq!(hidden.len(), 1, "{hidden:?}"); + assert_eq!(hidden[0].watching, None, "a backgrounded app shows nothing"); + + core.resume(); + let back = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; + assert_eq!(back.len(), 1, "{back:?}"); + assert_eq!(back[0].watching.as_deref(), Some("s1"), "the session is on screen again"); + }) + .await; +} + +/// Flicking through sessions is one `presence`, for the one left on screen. +#[tokio::test] +async fn switching_sessions_quickly_sends_one_presence() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; + let core = core_watched_by(&mock, &phone, &key, &machine).await; + eose_all(&mut mock).await; + let refresh = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); + + for session in ["s1", "s2", "s3"] { + core.dispatch(Intent::SelectSession { + machine: machine.pubkey_hex.clone(), + session_id: Some(session.into()), + }) + .await; + } + let sent = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; + assert_eq!(sent.len(), 1, "one presence for the whole burst: {sent:?}"); + assert_eq!(sent[0].watching.as_deref(), Some("s3"), "the one left on screen"); + }) + .await; +} diff --git a/crates/client-runtime/src/runtime/tests/session_keys.rs b/crates/client-runtime/src/runtime/tests/session_keys.rs new file mode 100644 index 00000000..aad5382d --- /dev/null +++ b/crates/client-runtime/src/runtime/tests/session_keys.rs @@ -0,0 +1,540 @@ +use super::*; + +fn heartbeat_with(caps: &[&str]) -> BridgeToPhone { + protocol::codec::decode_bridge_to_phone( + &serde_json::json!({ + "v": 12, + "type": "sessions", + "machine": "laptop", + "sessions": [], + "agents": [], + "credentials": [], + "v": protocol::capabilities::PROTOCOL_VERSION, + "capabilities": caps, + }) + .to_string(), + ) + .unwrap() +} + +/// The whole grant life cycle against a bridge advertising session keys: +/// granted by the identity, confirmed by a message under the key, used +/// for payloads (never for signing), and dropped when the bridge speaks +/// to the identity again. +#[tokio::test] +async fn a_bridge_with_session_keys_gets_payloads_under_the_key_and_signatures_by_the_identity() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let ports = CorePorts { kv: Rc::new(kv), ..CorePorts::default() }; + let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + core.start(); + eose_all(&mut mock).await; + // A machine not heard from is granted before anything else; the + // reconnect's refresh waits on it. + let grant = expect_grant(&mut mock, &phone, &machine).await; + let session = grant.pubkey_hex.clone(); + assert_ne!(session, phone.pubkey_hex); + assert_eq!(grant.replaces, None); + + // Before the bridge confirms, commands wait. + core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; + + // The heartbeat under the key confirms it: what waited goes out + // under the key, without the refresh the heartbeat answered. + let heartbeat = heartbeat_with(&[protocol::capabilities::SESSION_KEYS]); + push_bridge_to_phone_event(&mock, &machine, &session, "cd-1", &heartbeat); + let cmd = next_command_via(&mut mock, &phone, &session, &machine).await; + assert!(matches!(cmd, Some(PhoneToBridge::Interrupt(_))), "{cmd:?}"); + + // A later message to the identity: the bridge lost the key, and + // is granted it again at once. + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat); + let again = expect_grant(&mut mock, &phone, &machine).await; + assert_eq!(again.pubkey_hex, session); + core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; + push_bridge_to_phone_event(&mock, &machine, &session, "cd-1", &heartbeat); + let cmd = next_command_via(&mut mock, &phone, &session, &machine).await; + assert!(matches!(cmd, Some(PhoneToBridge::Interrupt(_))), "{cmd:?}"); + }) + .await; +} + +/// The next command must be a grant, under the identity. +async fn expect_grant( + mock: &mut MockRelay, + phone: &Keypair, + machine: &Keypair, +) -> protocol::commands::SessionKeyGrant { + match next_command_via(mock, phone, &phone.pubkey_hex, machine).await { + Some(PhoneToBridge::SessionKey(m)) => m.session_key, + other => panic!("expected a grant, got {other:?}"), + } +} + +/// An identity signer that counts what it is asked to decrypt. +struct DecryptCounter(crate::signer::LocalSigner, Rc>); + +impl IdentitySigner for DecryptCounter { + fn pubkey_hex(&self) -> String { + self.0.pubkey_hex() + } + fn sign_event(&self, event: nostr::UnsignedEvent) -> crate::ports::LocalBoxFuture<'_, Result> { + self.0.sign_event(event) + } + fn nip44_encrypt(&self, peer: &str, plaintext: &str) -> crate::ports::LocalBoxFuture<'_, Result> { + self.0.nip44_encrypt(peer, plaintext) + } + fn nip44_decrypt(&self, peer: &str, ciphertext: &str) -> crate::ports::LocalBoxFuture<'_, Result> { + self.1.set(self.1.get() + 1); + self.0.nip44_decrypt(peer, ciphertext) + } +} + +/// A core whose identity counts its decrypts, with `machine` restored +/// from a backup: known, never heard from, no key of this client. +async fn restored_core(mock: &MockRelay, phone: &Keypair, machine: &Keypair, spy: Rc) -> (Core, Rc>) { + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let decrypts = Rc::new(std::cell::Cell::new(0)); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(DecryptCounter(crate::signer::LocalSigner(phone.clone()), Rc::clone(&decrypts))), + proxy: None, + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, + spy, + Rc::new(FixedClock(RefCell::new(1_000_000))), + Rc::new(ZeroEntropy), + ) + .await; + core.set_relays(vec![mock.url.clone()]); + (core, decrypts) +} + +/// Every grant carries the phone's configured device name — what the bridge +/// shows in its terminal allowlist and in a refusal — and none when the phone +/// has no name to give. `CoreConfig::new` sanitizes, so a whitespace-only +/// setting is stored (and granted) as no name rather than a blank one. +#[tokio::test] +async fn every_grant_carries_the_configured_device_label() { + LocalSet::new() + .run_until(async { + for (configured, expected) in [ + (Some("Pixel 8a".to_string()), Some("Pixel 8a")), + (Some(" ".to_string()), None), + (None, None), + ] { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let core = Core::spawn( + CoreConfig::new(Rc::new(crate::signer::LocalSigner(phone.clone())), None, false, configured.clone()), + CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, + spy, + Rc::new(FixedClock(RefCell::new(1_000_000))), + Rc::new(ZeroEntropy), + ) + .await; + core.set_relays(vec![mock.url.clone()]); + core.start(); + eose_all(&mut mock).await; + let grant = expect_grant(&mut mock, &phone, &machine).await; + assert_eq!(grant.label.as_deref(), expected, "configured {configured:?}"); + } + }) + .await; +} + +/// A restored device costs the identity's signer no decrypt: what the +/// bridge wrote to the identity before the grant is skipped, what it +/// writes while the grant travels is dropped once the key is confirmed, +/// and the sessions the confirming heartbeat lists sync under the key. +#[tokio::test] +async fn a_restored_machine_is_granted_first_and_nothing_to_the_identity_is_decrypted() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let (core, decrypts) = restored_core(&mock, &phone, &machine, Rc::clone(&spy)).await; + core.start(); + eose_all(&mut mock).await; + let grant = expect_grant(&mut mock, &phone, &machine).await; + + // An identity heartbeat from an earlier install, still on the + // relay, and one written while the grant travels. + let heartbeat = heartbeat_with(&[protocol::capabilities::SESSION_KEYS]); + push_bridge_to_phone_event_at(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat, SESSION_LIST_KIND, Some(500)); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat); + settle().await; + assert!(spy.messages.lock().unwrap().is_empty(), "nothing to the identity is read yet"); + + // The heartbeat under the key lists a session: it syncs under the key. + let listed = protocol::codec::decode_bridge_to_phone( + &serde_json::json!({ + "v": protocol::capabilities::PROTOCOL_VERSION, + "type": "sessions", + "machine": "laptop", + "sessions": [{"id": "s1", "agent": "opencode", "slug": "sl", "cwd": "/w", "lastActivity": "t", "lineCount": 1, "title": null, "project": "p", "seqHigh": 3}], + "agents": [], + "credentials": [], + "capabilities": [protocol::capabilities::SESSION_KEYS], + }) + .to_string(), + ) + .unwrap(); + push_bridge_to_phone_event(&mock, &machine, &grant.pubkey_hex, "cd-1", &listed); + let sync = next_command_via(&mut mock, &phone, &grant.pubkey_hex, &machine).await; + assert!(matches!(sync, Some(PhoneToBridge::SyncRequest(_))), "{sync:?}"); + wait_for(|| spy.messages.lock().unwrap().len() == 1).await; + assert_eq!(decrypts.get(), 0, "the identity decrypted nothing"); + assert_eq!(spy.messages.lock().unwrap().len(), 1, "only the heartbeat under the key was read"); + }) + .await; +} + +/// A bridge that does not confirm the grant in time: what waited on it +/// goes on under the identity, as without session keys. +#[tokio::test] +async fn an_unconfirmed_grant_lets_what_waited_go_on_under_the_identity() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let (core, decrypts) = restored_core(&mock, &phone, &machine, Rc::clone(&spy)).await; + core.start(); + eose_all(&mut mock).await; + expect_grant(&mut mock, &phone, &machine).await; + // A heartbeat to the identity, and live output superseded by + // the sync that follows it. + let heartbeat = heartbeat_with(&[]); + push_bridge_to_phone_event_at(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat, SESSION_LIST_KIND, None); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat); + settle().await; + assert_eq!(decrypts.get(), 0); + + tokio::time::pause(); + tokio::time::advance(super::grant::GRANT_WAIT + Duration::from_secs(1)).await; + tokio::time::resume(); + let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); + wait_for(|| decrypts.get() == 1).await; + assert_eq!(decrypts.get(), 1, "the held heartbeat is read with the identity"); + assert!(spy.messages.lock().unwrap().iter().any(|(_, m)| matches!(m, BridgeToPhone::Sessions(_)))); + }) + .await; +} + +#[tokio::test] +async fn with_a_host_key_store_the_kv_holds_no_session_key() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let kv = MemoryKv::seeded([ + (crate::stores::OLD_SESSION_KEY_KEY, generate_keypair().secret_hex()), + (crate::stores::SESSION_KEYS_KEY, "{}".to_string()), + ]); + let store = MemoryKeyStore::default(); + let ports = CorePorts { + kv: Rc::new(kv.clone()), + session_keys: Some(Rc::new(store.clone())), + ..CorePorts::default() + }; + let _core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + let dump = kv.dump(); + assert!(!dump.contains_key(crate::stores::OLD_SESSION_KEY_KEY)); + assert!(!dump.contains_key(crate::stores::SESSION_KEYS_KEY)); + assert!(store.0.borrow().is_some(), "the key went to the host's store"); + let secret = store.ring().current.keypair.secret_hex(); + assert!(dump.values().all(|v| !v.contains(&secret))); + }) + .await; +} + +/// A client a bridge knows by its session key stays present there: after +/// a quiet [`PRESENCE_RENEW_SECS`] it renews with a `presence`, and a stop +/// tells the bridge it is away before it disconnects. +#[tokio::test] +async fn a_quiet_client_renews_its_presence_and_says_when_it_goes_away() { + use client_core::stores::session_key::{SessionGrant, SessionKey}; + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let start_ms: u64 = 1_000_000; + let key = SessionKey { keypair: generate_keypair(), expires_at: start_ms / 1000 + 80 * 24 * 3600 }; + let store = MemoryKeyStore::default(); + *store.0.borrow_mut() = Some(SessionKeyRing::single(key.clone()).encode()); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + state.machines.get_mut(&machine.pubkey_hex).unwrap().session_grant = Some(SessionGrant { + pubkey_hex: key.pubkey_hex().to_string(), + expires_at: key.expires_at, + sent_at: start_ms / 1000 - 100, + }); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let clock = Rc::new(FixedClock(RefCell::new(start_ms))); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy: None, + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + CorePorts { kv: Rc::new(kv), session_keys: Some(Rc::new(store.clone())), ..CorePorts::default() }, + Rc::new(Spy::default()), + Rc::clone(&clock) as Rc, + Rc::new(ZeroEntropy), + ) + .await; + core.set_relays(vec![mock.url.clone()]); + core.start(); + eose_all(&mut mock).await; + // Connecting says it is here already. + let refresh = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); + + // Quiet for the renewal period: the next check sends a presence. + *clock.0.borrow_mut() += PRESENCE_RENEW_SECS * 1000; + tokio::time::pause(); + tokio::time::advance(STALE_WATCHDOG_EVERY + Duration::from_secs(1)).await; + tokio::time::resume(); + let renewal = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; + assert!(matches!(renewal, Some(PhoneToBridge::Presence(PresenceMsg { away: false, watching: None, watching_terminal: None }))), "{renewal:?}"); + + core.stop(); + let away = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; + assert!(matches!(away, Some(PhoneToBridge::Presence(PresenceMsg { away: true, .. }))), "{away:?}"); + }) + .await; +} + +/// A machine-settings change the bridge never answers (the machine is +/// offline, a relay dropped the command) stops showing as busy after the +/// phone's own deadline, with the reason, instead of spinning forever. +#[tokio::test] +async fn a_machine_settings_change_nobody_answers_stops_being_busy_at_the_deadline() { + use client_core::stores::machines::SLOW_SETTINGS_ANSWER_TIMEOUT_MS; + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let start_ms: u64 = 1_000_000; + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let clock = Rc::new(FixedClock(RefCell::new(start_ms))); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy: None, + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, + Rc::new(Spy::default()), + Rc::clone(&clock) as Rc, + Rc::new(ZeroEntropy), + ) + .await; + core.set_relays(vec![mock.url.clone()]); + core.start(); + eose_all(&mut mock).await; + + core.dispatch(Intent::PluginAction { + machine: machine.pubkey_hex.clone(), + agent: "claude-code".into(), + action: protocol::common::PluginAction::Install, + target: "b@m".into(), + }) + .await; + let busy = |view: MachinesView| view.machines[&machine.pubkey_hex].plugins["claude-code"].busy.clone(); + assert_eq!(busy(core.machines_view().await), ["b@m".to_string()]); + + // The machine never answers; the phone's clock passes the + // deadline and the timer armed for it fires. + *clock.0.borrow_mut() += SLOW_SETTINGS_ANSWER_TIMEOUT_MS + 1; + tokio::time::pause(); + tokio::time::advance(Duration::from_millis(SLOW_SETTINGS_ANSWER_TIMEOUT_MS + 1)).await; + tokio::time::resume(); + wait_for_async(|| { + let core = core.clone(); + let key = machine.pubkey_hex.clone(); + async move { + let view = core.machines_view().await; + view.machines[&key].plugins["claude-code"].failure.is_some() + } + }) + .await; + + let view = core.machines_view().await; + let plugins = &view.machines[&machine.pubkey_hex].plugins["claude-code"]; + assert!(plugins.busy.is_empty(), "the change is given up on"); + let failure = plugins.failure.as_ref().expect("and says why"); + assert!(failure.error.contains("did not answer"), "{failure:?}"); + }) + .await; +} + +/// A key a month from lapsing is replaced at boot. The bridge on it keeps +/// using it until it confirms the new one, which it is granted under the +/// old one; then the old key is gone from the phone's store. +#[tokio::test] +async fn a_rotated_key_is_granted_and_the_old_one_dropped_once_confirmed() { + use client_core::stores::session_key::{SessionGrant, SessionKey}; + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let now_secs = 1_000; + let old = SessionKey { keypair: generate_keypair(), expires_at: now_secs + 10 * 24 * 3600 }; + let store = MemoryKeyStore::default(); + *store.0.borrow_mut() = Some(SessionKeyRing::single(old.clone()).encode()); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + state.machines.get_mut(&machine.pubkey_hex).unwrap().session_grant = Some(SessionGrant { + pubkey_hex: old.pubkey_hex().to_string(), + expires_at: old.expires_at, + sent_at: now_secs - 100, + }); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let ports = CorePorts { + kv: Rc::new(kv), + session_keys: Some(Rc::new(store.clone())), + ..CorePorts::default() + }; + let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + let ring = store.ring(); + let new = ring.current.pubkey_hex().to_string(); + assert_ne!(new, old.pubkey_hex(), "replaced at boot"); + assert_eq!(ring.previous.as_ref().map(|k| k.pubkey_hex()), Some(old.pubkey_hex()), "and kept"); + + core.start(); + eose_all(&mut mock).await; + // The bridge still holds only the old key: payloads use it. + let refresh = next_command_via(&mut mock, &phone, old.pubkey_hex(), &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); + + // Its heartbeat, under the old key, earns it the new one. + let heartbeat = heartbeat_with(&[protocol::capabilities::SESSION_KEYS]); + push_bridge_to_phone_event(&mock, &machine, old.pubkey_hex(), "cd-1", &heartbeat); + let grant = match next_command_via(&mut mock, &phone, old.pubkey_hex(), &machine).await { + Some(PhoneToBridge::SessionKey(m)) => m.session_key, + other => panic!("expected a grant, got {other:?}"), + }; + assert_eq!(grant.pubkey_hex, new); + assert_eq!(grant.replaces.as_deref(), Some(old.pubkey_hex()), "it moves this client, not adds one"); + assert_eq!(grant.bridge_pubkey_hex, machine.pubkey_hex); + assert_eq!(grant.expires_at, ring.current.expires_at); + + // A heartbeat under the new key confirms it: the old one goes. + push_bridge_to_phone_event(&mock, &machine, &new, "cd-1", &heartbeat); + wait_for(|| store.ring().previous.is_none()).await; + assert!(store.ring().previous.is_none()); + core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; + let cmd = next_command_via(&mut mock, &phone, &new, &machine).await; + assert!(matches!(cmd, Some(PhoneToBridge::Interrupt(_))), "{cmd:?}"); + }) + .await; +} + +/// Coming back to the app with the socket still up asks a machine for +/// its session list only when its heartbeat has not kept it current. +#[tokio::test] +async fn a_resume_refreshes_only_machines_not_heard_from_lately() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + heard_without_session_keys(&mut state, &machine.pubkey_hex); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy: None, + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, + Rc::new(Spy::default()), + clock.clone(), + Rc::new(ZeroEntropy), + ) + .await; + core.set_relays(vec![mock.url.clone()]); + core.start(); + eose_all(&mut mock).await; + let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "the connect refreshes: {refresh:?}"); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &heartbeat_with(&[])); + for _ in 0..100 { + if core.machines_view().await.machines[&machine.pubkey_hex].last_heartbeat_at.is_some() { + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + + // Heard just now: the resume sends nothing, so the next + // command out is the user's. + core.resume(); + core.dispatch(Intent::Interrupt { machine: machine.pubkey_hex.clone(), session_id: "s1".into() }).await; + let cmd = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; + assert!(matches!(cmd, Some(PhoneToBridge::Interrupt(_))), "{cmd:?}"); + + // Silent for longer than a couple of heartbeats: refreshed. + *clock.0.borrow_mut() += RESUME_HEARD_WITHIN_MS; + core.resume(); + let cmd = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; + assert!(matches!(cmd, Some(PhoneToBridge::RefreshSessions(_))), "{cmd:?}"); + }) + .await; +} diff --git a/crates/client-runtime/src/runtime/tests/store_layer.rs b/crates/client-runtime/src/runtime/tests/store_layer.rs new file mode 100644 index 00000000..9ddf3d5e --- /dev/null +++ b/crates/client-runtime/src/runtime/tests/store_layer.rs @@ -0,0 +1,764 @@ +use super::*; + +// --- the composed store layer --- + +struct OkHttp; + +impl crate::attachments::HttpFetch for OkHttp { + fn put( + &self, + _url: &str, + _headers: Vec<(String, String)>, + _body: Vec, + ) -> crate::ports::LocalBoxFuture<'_, Result> + { + Box::pin(async { + Ok(crate::attachments::HttpResponse { + status: 200, + body: b"{}".to_vec(), + }) + }) + } +} + +/// Every call fails — forces the session-image chunk fallback. +struct FailHttp; + +impl crate::attachments::HttpFetch for FailHttp { + fn put( + &self, + _url: &str, + _headers: Vec<(String, String)>, + _body: Vec, + ) -> crate::ports::LocalBoxFuture<'_, Result> + { + Box::pin(async { Err("no server".to_string()) }) + } +} + +/// A Blossom server that accepts the upload and never answers. +struct HangHttp; + +impl crate::attachments::HttpFetch for HangHttp { + fn put( + &self, + _url: &str, + _headers: Vec<(String, String)>, + _body: Vec, + ) -> crate::ports::LocalBoxFuture<'_, Result> + { + Box::pin(std::future::pending()) + } +} + +#[tokio::test] +async fn a_stalled_image_upload_does_not_block_the_loop() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let ports = CorePorts { + http: Rc::new(HangHttp), + ..CorePorts::default() + }; + let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.start(); + eose_all(&mut mock).await; + wait_connected(&core).await; + core.dispatch(Intent::SetBlossomServer("https://blossom.example".into())).await; + + let core2 = core.clone(); + let upload = tokio::task::spawn_local(async move { + core2 + .dispatch(Intent::SendSessionFile(SessionFileSend { + machine: "m".into(), + session_id: "s1".into(), + text: String::new(), + data: b"bytes".to_vec(), + filename: "a.jpg".into(), + mime_type: "image/jpeg".into(), + })) + .await; + }); + // Let the upload task reach the hanging `put` before checking the + // loop still answers. + for _ in 0..10 { + tokio::task::yield_now().await; + } + + // The upload is still hanging, yet the loop answers at once. + tokio::time::timeout(Duration::from_secs(1), core.machines_view()) + .await + .expect("the loop stayed blocked behind the upload"); + assert!(!upload.is_finished(), "the intent answers only when the send ends"); + upload.abort(); + }) + .await; +} + +/// Decrypt + decode a relay `EVENT` frame as a `PhoneToBridge` command from +/// `phone` to `machine` — `None` if the frame isn't an EVENT, isn't tagged +/// for `machine`, or doesn't decrypt/decode as one. +fn decode_command_frame( + frame: &str, + phone_pubkey: &str, + machine: &Keypair, +) -> Option { + let v: Vec = serde_json::from_str(frame).ok()?; + if v.first()? != "EVENT" { + return None; + } + let ev = v.get(1)?; + if ev.get("pubkey")? != &serde_json::json!(phone_pubkey) { + return None; + } + let tagged = ev.get("tags")?.as_array()?.iter().any(|t| { + t.get(0) == Some(&serde_json::json!("p")) + && t.get(1) == Some(&serde_json::json!(machine.pubkey_hex)) + }); + if !tagged { + return None; + } + let content = ev.get("content")?.as_str()?; + let plaintext = + protocol::crypto::decrypt_from(&machine.secret_key, phone_pubkey, content).ok()?; + protocol::codec::decode_phone_to_bridge(&plaintext).ok() +} + +#[tokio::test] +async fn connection_view_query_reflects_the_live_status() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + + assert_eq!(core.connection_view().await.unwrap().status, "idle"); + core.start(); + eose_all(&mut mock).await; + wait_connected(&core).await; + assert_eq!(core.connection_view().await.unwrap().status, "connected"); + }) + .await; +} + +#[tokio::test] +async fn an_intent_becomes_a_signed_command_on_the_wire() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + + core.dispatch(Intent::Interrupt { + machine: machine.pubkey_hex.clone(), + session_id: "s1".into(), + }) + .await; + + // among the published EVENTs find the one wrapped for the + // machine. + let mut found = false; + for _ in 0..6 { + let frame = mock.next_frame().await; + let v: Vec = serde_json::from_str(&frame).unwrap(); + if v[0] != "EVENT" { + continue; + } + let ev = &v[1]; + if ev["pubkey"] == serde_json::json!(phone.pubkey_hex) + && ev["tags"] + .as_array() + .unwrap() + .iter() + .any(|t| t[0] == "p" && t[1] == machine.pubkey_hex) + { + found = true; + break; + } + } + assert!(found, "no command EVENT p-tagged to the machine"); + }) + .await; +} + +#[tokio::test] +async fn editing_a_machines_relays_repoints_the_transport() { + LocalSet::new() + .run_until(async { + let mut first = mock_relay().await; + let mut second = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + // A machine paired in an earlier run, reached over `first`. + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[first.url.clone()]); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let spy = Rc::new(Spy::default()); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy: None, + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, + Rc::clone(&spy) as Rc, + Rc::new(FixedClock(RefCell::new(1_000_000))), + Rc::new(ZeroEntropy), + ) + .await; + // Dialled from the stored machine alone: no relay configured. + core.start(); + eose_all(&mut first).await; + wait_connected(&core).await; + + core.dispatch(Intent::SetMachineRelays { + machine: machine.pubkey_hex.clone(), + relays: vec![second.url.clone()], + }) + .await; + for _ in 0..3 { + let req = second.next_frame().await; + assert!(req.starts_with(r#"["REQ""#), "got {req}"); + } + assert!(spy.events.lock().unwrap().contains(&CoreEvent::StateChanged { slice: SliceId::Machines })); + }) + .await; +} + +#[tokio::test] +async fn select_session_intent_updates_the_ui_view_and_emits_a_state_changed_event() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + + core.dispatch(Intent::SelectSession { + machine: "m1".into(), + session_id: Some("s1".into()), + }) + .await; + + let uv = core.ui_view().await; + assert_eq!(uv.selected_machine.as_deref(), Some("m1")); + assert_eq!(uv.selected_session.as_deref(), Some("s1")); + + { + let events = spy.events.lock().unwrap(); + assert!(events.contains(&CoreEvent::StateChanged { slice: SliceId::Ui })); + } + }) + .await; +} + +#[tokio::test] +async fn selecting_a_session_cancels_its_notification_tag() { + // CDX-026c: opening a session the user was notified about clears + // every notification filed under its tag. + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let notifier = RecordingNotifier::new(); + let ports = CorePorts { + notifier: Rc::new(notifier.clone()), + ..CorePorts::default() + }; + let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + + core.dispatch(Intent::SelectSession { + machine: "m1".into(), + session_id: Some("s1".into()), + }) + .await; + + assert_eq!( + notifier.cancelled(), + vec![client_core::notifications::session_notify_tag("m1", "s1")] + ); + }) + .await; +} + +#[tokio::test] +async fn coming_back_to_a_session_cancels_what_was_notified_while_away() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let notifier = RecordingNotifier::new(); + let ports = CorePorts { + notifier: Rc::new(notifier.clone()), + ..CorePorts::default() + }; + let core = core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + core.dispatch(Intent::SelectSession { + machine: "m1".into(), + session_id: Some("s1".into()), + }) + .await; + core.pause(); + core.resume(); + wait_for(|| notifier.cancelled().len() == 2).await; + let tag = client_core::notifications::session_notify_tag("m1", "s1"); + assert_eq!(notifier.cancelled(), [tag.clone(), tag]); + + // Left for the list: coming back clears nothing. + core.dispatch(Intent::SelectSession { + machine: "m1".into(), + session_id: None, + }) + .await; + core.pause(); + core.resume(); + settle().await; + assert_eq!(notifier.cancelled().len(), 2); + }) + .await; +} + +#[tokio::test] +async fn set_plan_approval_choice_intent_updates_the_ui_view() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + + core.dispatch(Intent::SetPlanApprovalChoice { + card_id: "card1".into(), + key: "2".into(), + }) + .await; + + let uv = core.ui_view().await; + assert_eq!(uv.plan_approval_choices.get("card1").map(String::as_str), Some("2")); + }) + .await; +} + +/// Read frames from `mock` until `matches` returns one, ACK-ing every +/// `EVENT` along the way (by its real id) so a concurrent +/// `publish_confirmed` settles immediately instead of riding out the full +/// confirm budget — the image upload awaits its own publish, so the test +/// must answer it while `dispatch` is still in flight. +async fn find_command_frame( + mock: &mut MockRelay, + phone_pubkey: &str, + machine: &Keypair, + matches: impl Fn(&PhoneToBridge) -> bool, +) -> PhoneToBridge { + for _ in 0..16 { + let frame = mock.next_frame().await; + let v: Vec = serde_json::from_str(&frame).unwrap(); + if v[0] == "EVENT" { + if let Some(id) = v[1]["id"].as_str() { + mock.push(format!(r#"["OK","{id}",true,""]"#)); + } + } + if let Some(msg) = decode_command_frame(&frame, phone_pubkey, machine) { + if matches(&msg) { + return msg; + } + } + } + panic!("no matching command reached the machine within 16 frames"); +} + +#[tokio::test] +async fn sending_a_session_image_uploads_to_blossom_and_publishes_the_command() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let ports = CorePorts { + http: Rc::new(OkHttp), + ..CorePorts::default() + }; + let core = + core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + wait_connected(&core).await; + + core.dispatch(Intent::SetBlossomServer("https://blossom.example".into())).await; + + let core2 = core.clone(); + let machine_pubkey = machine.pubkey_hex.clone(); + let dispatched = tokio::task::spawn_local(async move { + core2 + .dispatch(Intent::SendSessionFile(SessionFileSend { + machine: machine_pubkey, + session_id: "s1".into(), + text: "look at this".into(), + data: b"png bytes here".to_vec(), + filename: "photo.png".into(), + mime_type: "image/png".into(), + })) + .await; + }); + + let msg = find_command_frame( + &mut mock, + &phone.pubkey_hex, + &machine, + |m| matches!(m, PhoneToBridge::UploadFile(_)), + ) + .await; + dispatched.await.unwrap(); + + match msg { + PhoneToBridge::UploadFile(UploadFileMsg::Blossom(m)) => { + assert_eq!(m.session_id, "s1"); + assert_eq!(m.text, "look at this"); + assert_eq!(m.filename, "photo.png"); + assert_eq!(m.size_bytes, b"png bytes here".len() as u64); + assert!(!m.hash.is_empty()); + } + other => panic!("Blossom succeeded — should not chunk: {other:?}"), + } + }) + .await; +} + +#[tokio::test] +async fn with_no_blossom_server_a_file_goes_through_the_relays() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let ports = CorePorts { + http: Rc::new(OkHttp), + ..CorePorts::default() + }; + let core = + core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + wait_connected(&core).await; + + let core2 = core.clone(); + let machine_pubkey = machine.pubkey_hex.clone(); + let dispatched = tokio::task::spawn_local(async move { + core2 + .dispatch(Intent::SendSessionFile(SessionFileSend { + machine: machine_pubkey, + session_id: "s1".into(), + text: "look at this".into(), + data: b"%PDF-1.7 bytes".to_vec(), + filename: "notes.pdf".into(), + mime_type: "application/pdf".into(), + })) + .await; + }); + + let msg = find_command_frame( + &mut mock, + &phone.pubkey_hex, + &machine, + |m| matches!(m, PhoneToBridge::UploadFile(_)), + ) + .await; + dispatched.await.unwrap(); + + // The HTTP port would accept an upload: only the missing + // server keeps the file off Blossom. + match msg { + PhoneToBridge::UploadFile(UploadFileMsg::Chunk(m)) => { + assert_eq!((m.filename.as_str(), m.mime_type.as_str()), ("notes.pdf", "application/pdf")); + assert_eq!(m.total_chunks, 1); + } + other => panic!("no Blossom server is set: {other:?}"), + } + }) + .await; +} + +#[tokio::test] +async fn sending_a_session_image_falls_back_to_chunks_when_blossom_is_unreachable() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let ports = CorePorts { + http: Rc::new(FailHttp), + ..CorePorts::default() + }; + let core = + core_for_ports(&mock, &phone, Rc::new(Spy::default()), ports).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + wait_connected(&core).await; + + core.dispatch(Intent::SetBlossomServer("https://blossom.example".into())).await; + + let core2 = core.clone(); + let machine_pubkey = machine.pubkey_hex.clone(); + let dispatched = tokio::task::spawn_local(async move { + core2 + .dispatch(Intent::SendSessionFile(SessionFileSend { + machine: machine_pubkey, + session_id: "s1".into(), + text: "a caption".into(), + data: b"small image bytes".to_vec(), + filename: "photo.jpg".into(), + mime_type: "image/jpeg".into(), + })) + .await; + }); + + // Step through the upload's retry backoff (1 s, then 2 s) + // on paused time; each sleep is only armed once the attempt + // before it has failed, hence one advance per step. + tokio::time::pause(); + for _ in 0..3 { + for _ in 0..20 { + tokio::task::yield_now().await; + } + tokio::time::advance(Duration::from_millis(2_100)).await; + } + tokio::time::resume(); + + let msg = find_command_frame( + &mut mock, + &phone.pubkey_hex, + &machine, + |m| matches!(m, PhoneToBridge::UploadFile(_)), + ) + .await; + dispatched.await.unwrap(); + + match msg { + PhoneToBridge::UploadFile(UploadFileMsg::Chunk(m)) => { + assert_eq!(m.session_id, "s1"); + assert_eq!(m.chunk_index, 0); + assert_eq!(m.total_chunks, 1); // well under 35 KB + assert_eq!(m.text, "a caption"); + } + other => panic!("blossom is unreachable in this test: {other:?}"), + } + }) + .await; +} + +#[tokio::test] +async fn the_connection_status_change_emits_a_state_changed_event() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.start(); + eose_all(&mut mock).await; + wait_for(|| { + spy.events.lock().unwrap().contains(&CoreEvent::StateChanged { + slice: SliceId::Connection, + }) + }) + .await; + + let events = spy.events.lock().unwrap(); + assert!(events.contains(&CoreEvent::StateChanged { + slice: SliceId::Connection + })); + }) + .await; +} + +#[tokio::test] +async fn a_session_pending_message_populates_the_view_and_emits_a_state_changed_event() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + + let msg = protocol::codec::decode_bridge_to_phone( + r#"{"v":12,"type":"session-pending","pendingId":"p1","machine":"devbox","createdAt":"2026-01-01T00:00:00.000Z"}"#, + ) + .unwrap(); + let plaintext = encode_bridge_to_phone(&msg); + let ct = protocol::crypto::encrypt_to( + &machine.secret_key, + &phone.pubkey_hex, + &plaintext, + ) + .unwrap(); + let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) + .tag(recipient(&machine, &phone.pubkey_hex)) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + mock.push(format!( + r#"["EVENT","cd-3",{}]"#, + ::as_json(&event) + )); + wait_for(|| { + spy.events.lock().unwrap().contains(&CoreEvent::StateChanged { + slice: SliceId::PendingSessions, + }) + }) + .await; + + { + let events = spy.events.lock().unwrap(); + assert!(events.contains(&CoreEvent::StateChanged { + slice: SliceId::PendingSessions + })); + } + + let view = core.pending_sessions_view().await; + let placeholder = view.pending.get("p1").expect("placeholder in the view"); + assert_eq!(placeholder.machine_name, "devbox"); + }) + .await; +} + +#[tokio::test] +async fn dismiss_pending_session_removes_it_and_emits_a_state_changed_event() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + + let msg = protocol::codec::decode_bridge_to_phone( + r#"{"v":12,"type":"session-failed","pendingId":"p1","reason":"boom"}"#, + ) + .unwrap(); + let plaintext = encode_bridge_to_phone(&msg); + let ct = protocol::crypto::encrypt_to( + &machine.secret_key, + &phone.pubkey_hex, + &plaintext, + ) + .unwrap(); + let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) + .tag(recipient(&machine, &phone.pubkey_hex)) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + mock.push(format!( + r#"["EVENT","cd-3",{}]"#, + ::as_json(&event) + )); + wait_for_async(|| { + let core = core.clone(); + async move { core.pending_sessions_view().await.pending.contains_key("p1") } + }) + .await; + assert!(core.pending_sessions_view().await.pending.contains_key("p1")); + + core.dispatch(Intent::DismissPendingSession { + pending_id: "p1".into(), + }) + .await; + + assert!(!core.pending_sessions_view().await.pending.contains_key("p1")); + let events = spy.events.lock().unwrap(); + assert!(events.contains(&CoreEvent::StateChanged { + slice: SliceId::PendingSessions + })); + }) + .await; +} + +#[tokio::test] +async fn an_output_message_populates_the_transcript_view_and_emits_transcript_appended() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + + let msg = protocol::codec::decode_bridge_to_phone( + r#"{"v":12,"type":"output","sessionId":"s1","seq":1,"entries":[{"entryType":"text","role":"agent","text":"hi","timestamp":"t"}]}"#, + ) + .unwrap(); + let plaintext = encode_bridge_to_phone(&msg); + let ct = protocol::crypto::encrypt_to( + &machine.secret_key, + &phone.pubkey_hex, + &plaintext, + ) + .unwrap(); + let event = nostr::EventBuilder::new(nostr::Kind::Custom(LIVE_KIND), ct) + .tag(recipient(&machine, &phone.pubkey_hex)) + .sign_with_keys(&nostr::Keys::new(machine.secret_key.clone())) + .unwrap(); + mock.push(format!( + r#"["EVENT","cd-3",{}]"#, + ::as_json(&event) + )); + wait_for(|| { + spy.events.lock().unwrap().contains(&CoreEvent::TranscriptAppended { + machine: machine.pubkey_hex.clone(), + session_id: "s1".to_string(), + }) + }) + .await; + + let view = core.transcript_view(machine.pubkey_hex.clone(), "s1".to_string()).await; + assert_eq!(view.rows.len(), 1); + assert_eq!(view.rows[0].seq, 1); + assert_eq!(view.sync.local_high, 1); + + let events = spy.events.lock().unwrap(); + assert!(events.contains(&CoreEvent::TranscriptAppended { + machine: machine.pubkey_hex, + session_id: "s1".to_string(), + })); + }) + .await; +} + +#[tokio::test] +async fn transcript_view_of_an_unknown_session_is_the_honest_empty_default() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = core_for(&mock, &phone, Rc::new(Spy::default())).await; + + let view = core.transcript_view("m1".to_string(), "s-never-seen".to_string()).await; + assert!(view.rows.is_empty()); + assert!(view.have_ranges.is_empty()); + assert_eq!(view.sync.local_high, 0); + assert!(view.sync.contiguous); + }) + .await; +} diff --git a/crates/client-runtime/src/runtime/tests/sync.rs b/crates/client-runtime/src/runtime/tests/sync.rs new file mode 100644 index 00000000..560db27a --- /dev/null +++ b/crates/client-runtime/src/runtime/tests/sync.rs @@ -0,0 +1,246 @@ +use super::*; + +/// A local identity that counts its signatures. +struct CountingSigner(crate::signer::LocalSigner, Rc>); + +impl IdentitySigner for CountingSigner { + fn pubkey_hex(&self) -> String { + self.0.pubkey_hex() + } + fn sign_event(&self, event: nostr::UnsignedEvent) -> crate::ports::LocalBoxFuture<'_, Result> { + self.1.set(self.1.get() + 1); + self.0.sign_event(event) + } + fn nip44_encrypt(&self, peer: &str, plaintext: &str) -> crate::ports::LocalBoxFuture<'_, Result> { + self.0.nip44_encrypt(peer, plaintext) + } + fn nip44_decrypt(&self, peer: &str, ciphertext: &str) -> crate::ports::LocalBoxFuture<'_, Result> { + self.0.nip44_decrypt(peer, ciphertext) + } +} + +/// The identity signs on its own only for a handful of things: a +/// refresh per machine on each (re)connect, the sync requests and acks +/// that follow, and relay AUTH. A sync's chunks arrive back to back; they +/// cost one signed ack, not one each. +#[tokio::test] +async fn a_burst_of_sync_chunks_costs_one_signed_ack() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + heard_without_session_keys(&mut state, &machine.pubkey_hex); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let signs = Rc::new(std::cell::Cell::new(0)); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(CountingSigner(crate::signer::LocalSigner(phone.clone()), Rc::clone(&signs))), + proxy: None, + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, + Rc::new(Spy::default()), + Rc::new(FixedClock(RefCell::new(1_000_000))), + Rc::new(ZeroEntropy), + ) + .await; + core.set_relays(vec![mock.url.clone()]); + core.start(); + eose_all(&mut mock).await; + let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); + assert_eq!(signs.get(), 1, "the reconnect's refresh"); + + let decode = |v: serde_json::Value| protocol::codec::decode_bridge_to_phone(&v.to_string()).unwrap(); + let entry = serde_json::json!({ "timestamp": "t", "entryType": "turn_complete" }); + let begin = decode(serde_json::json!({ + "v": 12, + "type": "sync-begin", "sessionId": "s1", "syncId": "y1", "seqHigh": 3, "ranges": [[1, 3]] + })); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &begin); + for seq in 1..=3 { + let chunk = decode(serde_json::json!({ + "v": 12, + "type": "sync-chunk", "sessionId": "s1", "syncId": "y1", "range": [seq, seq], + "entries": [{ "seq": seq, "entry": entry }] + })); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &chunk); + } + let ack = match next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await { + Some(PhoneToBridge::SyncAck(ack)) => ack, + other => panic!("expected a sync-ack, got {other:?}"), + }; + assert_eq!((ack.sync_id.as_str(), ack.ranges), ("y1", vec![(1, 1), (2, 2), (3, 3)])); + assert_eq!(signs.get(), 2, "one signature for the three chunks"); + }) + .await; +} + +/// A `sync-begin` whose `sync-end` never arrives leaves the cycle in flight; +/// without a retry the session's gap would stay unfilled until the next +/// reconnect. The watchdog's tick restarts the cycle. +#[tokio::test] +async fn a_sync_cycle_that_goes_silent_is_re_requested_by_the_watchdog() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + heard_without_session_keys(&mut state, &machine.pubkey_hex); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy: None, + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, + Rc::new(Spy::default()), + clock.clone(), + Rc::new(ZeroEntropy), + ) + .await; + core.set_relays(vec![mock.url.clone()]); + core.start(); + eose_all(&mut mock).await; + let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); + + let begin = protocol::codec::decode_bridge_to_phone( + &serde_json::json!({ + "v": 12, + "type": "sync-begin", "sessionId": "s1", "syncId": "y1", "seqHigh": 3, "ranges": [[1, 3]] + }) + .to_string(), + ) + .unwrap(); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &begin); + wait_for_async(|| { + let core = core.clone(); + let key = machine.pubkey_hex.clone(); + async move { core.transcript_view(key, "s1".to_string()).await.sync.target == 3 } + }) + .await; + + *clock.0.borrow_mut() += + client_core::stores::transcript::SYNC_STALL_MS + 1; + core.tick(); + let request = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; + assert!( + matches!(request, Some(PhoneToBridge::SyncRequest(ref m)) if m.session_id == "s1"), + "{request:?}" + ); + }) + .await; +} + +/// A stalled cycle must not be re-requested once the socket is gone: the +/// tick's own heartbeat check may have just moved the FSM to WaitingRetry, +/// and publishing there only burns an attempt and a signature. +#[tokio::test] +async fn a_sync_stall_is_not_re_requested_on_a_dead_connection() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + heard_without_session_keys(&mut state, &machine.pubkey_hex); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + let signs = Rc::new(std::cell::Cell::new(0)); + let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(CountingSigner( + crate::signer::LocalSigner(phone.clone()), + Rc::clone(&signs), + )), + proxy: None, + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, + Rc::new(Spy::default()), + clock.clone(), + Rc::new(ZeroEntropy), + ) + .await; + core.set_relays(vec![mock.url.clone()]); + core.start(); + eose_all(&mut mock).await; + let refresh = next_command_via(&mut mock, &phone, &phone.pubkey_hex, &machine).await; + assert!(matches!(refresh, Some(PhoneToBridge::RefreshSessions(_))), "{refresh:?}"); + assert_eq!(signs.get(), 1, "the reconnect's refresh"); + + let begin = protocol::codec::decode_bridge_to_phone( + &serde_json::json!({ + "v": 12, + "type": "sync-begin", "sessionId": "s1", "syncId": "y1", "seqHigh": 3, "ranges": [[1, 3]] + }) + .to_string(), + ) + .unwrap(); + push_bridge_to_phone_event(&mock, &machine, &phone.pubkey_hex, "cd-1", &begin); + wait_for_async(|| { + let core = core.clone(); + let key = machine.pubkey_hex.clone(); + async move { core.transcript_view(key, "s1".to_string()).await.sync.target == 3 } + }) + .await; + + core.set_online(false); + *clock.0.borrow_mut() += client_core::stores::transcript::SYNC_STALL_MS + 1; + core.tick(); + // Nothing is published, so there is no event to wait on: give the + // tick's spawned work a moment before asserting the absence. + settle().await; + assert_eq!(signs.get(), 1, "no request is published without a live connection"); + }) + .await; +} + +/// Documents the JSON shape a binding receives — externally tagged, +/// camelCase, matching `Intent`'s own convention. +#[test] +fn core_event_json_shape_is_externally_tagged_camel_case() { + assert_eq!( + serde_json::to_value(CoreEvent::StateChanged { slice: SliceId::Machines }).unwrap(), + serde_json::json!({ "stateChanged": { "slice": "machines" } }), + ); + assert_eq!( + serde_json::to_value(CoreEvent::OutboxSettled { + id: "in-1".into(), + delivered: true, + }) + .unwrap(), + serde_json::json!({ "outboxSettled": { "id": "in-1", "delivered": true } }), + ); + assert_eq!( + serde_json::to_value(CoreEvent::ActionFailed { + kind: ActionFailedKind::PublishRejected, + }) + .unwrap(), + serde_json::json!({ "actionFailed": { "kind": "publishRejected" } }), + ); +} diff --git a/crates/client-runtime/src/runtime/tests/terminal.rs b/crates/client-runtime/src/runtime/tests/terminal.rs new file mode 100644 index 00000000..09b2e015 --- /dev/null +++ b/crates/client-runtime/src/runtime/tests/terminal.rs @@ -0,0 +1,282 @@ +use super::*; + +/// Every command this client sends `machine` within `wait`. +async fn commands_within( + mock: &mut MockRelay, + payload_key: &str, + machine: &Keypair, + wait: Duration, +) -> Vec { + let deadline = std::time::Instant::now() + wait; + let mut sent = Vec::new(); + loop { + let left = deadline.saturating_duration_since(std::time::Instant::now()); + if left.is_zero() { + break; + } + let Some(frame) = mock.frame_within(left).await else { break }; + let Ok(v) = serde_json::from_str::>(&frame) else { continue }; + if v[0] != "EVENT" { + continue; + } + let Ok(ev) = serde_json::from_value::(v[1].clone()) else { continue }; + let Ok(plain) = protocol::crypto::decrypt_from(&machine.secret_key, payload_key, &ev.content) else { continue }; + if let Ok(msg) = protocol::codec::decode_phone_to_bridge(&plain) { + sent.push(msg); + } + } + sent +} + +/// Every `terminal-input` this client sends `machine` within `wait`. +async fn terminal_inputs_within( + mock: &mut MockRelay, + payload_key: &str, + machine: &Keypair, + wait: Duration, +) -> Vec { + commands_within(mock, payload_key, machine, wait) + .await + .into_iter() + .filter_map(|m| match m { + PhoneToBridge::TerminalInput(m) => Some(m), + _ => None, + }) + .collect() +} + +/// The terminal on screen is what the bridge is told to send output for: +/// attaching names it in the presence, and detaching stops it. Nothing is +/// watched otherwise, so a terminal costs a backgrounded phone nothing. +#[tokio::test] +async fn the_terminal_on_screen_is_what_the_bridge_is_told() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; + let core = core_watched_by(&mock, &phone, &key, &machine).await; + eose_all(&mut mock).await; + let _ = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; + + core.dispatch(Intent::AttachTerminal { machine: machine.pubkey_hex.clone(), term: "t1".into() }) + .await; + let sent = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; + assert!( + sent.iter().any(|p| p.watching_terminal.as_deref() == Some("t1")), + "the bridge must be told which terminal is on screen: {sent:?}" + ); + + core.dispatch(Intent::DetachTerminal { machine: machine.pubkey_hex.clone() }).await; + let sent = presences_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; + assert!( + sent.iter().any(|p| p.watching_terminal.is_none() && p.watching.is_none()), + "detaching must stop the output: {sent:?}" + ); + }) + .await; +} + +/// Typing is paced by the core, not sent per keystroke: printable bytes wait +/// and go with the key a program acts on, as one sequenced command. +#[tokio::test] +async fn typing_into_a_terminal_goes_out_as_one_batched_command() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; + let core = core_watched_by(&mock, &phone, &key, &machine).await; + eose_all(&mut mock).await; + let _ = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; + + core.dispatch(Intent::AttachTerminal { machine: machine.pubkey_hex.clone(), term: "t1".into() }) + .await; + core.dispatch(Intent::TerminalInput { + machine: machine.pubkey_hex.clone(), + term: "t1".into(), + bytes: b"git che".to_vec(), + alternate_screen: false, + }) + .await; + // Nothing yet: the bytes are waiting for a key a program acts on. + assert!(terminal_inputs_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(120)) + .await + .is_empty()); + + core.dispatch(Intent::TerminalInput { + machine: machine.pubkey_hex.clone(), + term: "t1".into(), + bytes: b"\t".to_vec(), + alternate_screen: false, + }) + .await; + let sent = terminal_inputs_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(700)).await; + assert_eq!(sent.len(), 1, "one command for the whole line: {sent:?}"); + assert_eq!(sent[0].seq, 1); + assert_eq!(sent[0].data, "Z2l0IGNoZQk=", "git che plus the Tab"); + }) + .await; +} + +/// A key held back for the next batch redraws the app, whose predicted echo +/// shows it at once; the send that empties the batch redraws it again. +#[tokio::test] +async fn keys_held_back_redraw_the_predicted_echo() { + LocalSet::new() + .run_until(async { + let mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + + core.dispatch(Intent::AttachTerminal { machine: machine.pubkey_hex.clone(), term: "t1".into() }) + .await; + wait_for_async(|| { + let core = core.clone(); + async move { core.ui_view().await.terminals.attached.is_some() } + }) + .await; + let ui_changes = || { + spy.events + .lock() + .unwrap() + .iter() + .filter(|e| matches!(e, CoreEvent::StateChanged { slice: SliceId::Ui })) + .count() + }; + let before = ui_changes(); + core.dispatch(Intent::TerminalInput { + machine: machine.pubkey_hex.clone(), + term: "t1".into(), + bytes: b"l".to_vec(), + alternate_screen: false, + }) + .await; + wait_for_async(|| { + let core = core.clone(); + async move { + core.ui_view().await.terminals.attached.unwrap().waiting_input == b"l".to_vec() + } + }) + .await; + assert_eq!(core.ui_view().await.terminals.attached.unwrap().waiting_input, b"l".to_vec()); + assert!(ui_changes() > before, "a held key must redraw the app"); + + let before = ui_changes(); + core.dispatch(Intent::TerminalInput { + machine: machine.pubkey_hex.clone(), + term: "t1".into(), + bytes: b"\r".to_vec(), + alternate_screen: false, + }) + .await; + wait_for(|| ui_changes() > before).await; + assert!(core.ui_view().await.terminals.attached.unwrap().waiting_input.is_empty()); + assert!(ui_changes() > before, "the send must clear the prediction"); + }) + .await; +} + +/// Terminal output never goes through a view: it arrives as its own event, +/// already base64-decoded, with the offset it starts at. +#[tokio::test] +async fn terminal_output_reaches_the_host_on_its_own_event() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let spy = Rc::new(Spy::default()); + let core = core_for(&mock, &phone, Rc::clone(&spy)).await; + core.set_machines(vec![machine.pubkey_hex.clone()]); + core.start(); + eose_all(&mut mock).await; + + core.dispatch(Intent::AttachTerminal { machine: machine.pubkey_hex.clone(), term: "t1".into() }) + .await; + wait_for_async(|| { + let core = core.clone(); + async move { core.ui_view().await.terminals.attached.is_some() } + }) + .await; + + // "hi" in base64, the shell's first output at offset 0. + push_bridge_to_phone_event_at( + &mock, + &machine, + &phone.pubkey_hex, + "cd-1", + &BridgeToPhone::TerminalOutput(protocol::events::TerminalOutputMsg { + term: "t1".into(), + offset: 0, + data: "aGk=".into(), + replay: None, + }), + LIVE_KIND, + None, + ); + wait_for(|| { + spy.events.lock().unwrap().iter().any(|e| matches!( + e, + CoreEvent::TerminalOutput { term, offset: 0, bytes, .. } + if term == "t1" && bytes == b"hi" + )) + }) + .await; + + { + let events = spy.events.lock().unwrap(); + assert!( + events.iter().any(|e| matches!( + e, + CoreEvent::TerminalOutput { term, offset: 0, bytes, .. } + if term == "t1" && bytes == b"hi" + )), + "the frame must arrive as its own event: {events:?}" + ); + } + let view = core.ui_view().await; + let attached = view.terminals.attached.expect("a terminal on screen"); + assert_eq!(attached.expected_offset, 2, "the screen advanced past what it drew"); + }) + .await; +} + +/// A bridge that does not advertise terminals never opens one: the command +/// would wait on an answer that never comes. +#[tokio::test] +async fn opening_a_terminal_needs_the_bridge_to_advertise_it() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let key = SessionKey { keypair: generate_keypair(), expires_at: 1_000 + 80 * 24 * 3600 }; + // `core_watched_by` registers a machine with no capabilities. + let core = core_watched_by(&mock, &phone, &key, &machine).await; + eose_all(&mut mock).await; + let _ = next_command_via(&mut mock, &phone, key.pubkey_hex(), &machine).await; + + core.dispatch(Intent::OpenTerminal { + machine: machine.pubkey_hex.clone(), + session: None, + cols: 80, + rows: 24, + }) + .await; + + assert!(core.ui_view().await.terminals.attached.is_none(), "nothing on screen"); + let sent = commands_within(&mut mock, key.pubkey_hex(), &machine, Duration::from_millis(400)).await; + assert!( + !sent.iter().any(|m| matches!(m, PhoneToBridge::TerminalOpen(_))), + "no open went out: {sent:?}" + ); + }) + .await; +} diff --git a/crates/client-runtime/src/runtime/tests/tor.rs b/crates/client-runtime/src/runtime/tests/tor.rs new file mode 100644 index 00000000..9f303285 --- /dev/null +++ b/crates/client-runtime/src/runtime/tests/tor.rs @@ -0,0 +1,426 @@ +use super::*; + +/// Records every `set_proxy` call — used to check the HTTP port's own +/// boot-time proxy wiring, the twin of `WsConfig.proxy` above. +#[derive(Default)] +struct RecordingHttp { + proxy_calls: RefCell>>, +} + +impl crate::attachments::HttpFetch for RecordingHttp { + fn put( + &self, + _url: &str, + _headers: Vec<(String, String)>, + _body: Vec, + ) -> crate::ports::LocalBoxFuture<'_, Result> + { + Box::pin(async { Err("not used".to_string()) }) + } + fn set_proxy(&self, proxy: Option<&str>) { + self.proxy_calls.borrow_mut().push(proxy.map(str::to_string)); + } +} + +#[tokio::test] +async fn spawn_applies_the_boot_time_proxy_to_the_http_port_when_tor_is_on() { + LocalSet::new() + .run_until(async { + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let spy = Rc::new(Spy::default()); + let recording = Rc::new(RecordingHttp::default()); + let ports = CorePorts { + http: Rc::clone(&recording) as Rc, + ..CorePorts::default() + }; + let _core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy: Some("127.0.0.1:9050".to_string()), + tor: true, + device_label: None, + reconnect: fast_reconnect(), + }, + ports, + spy, + Rc::new(FixedClock(RefCell::new(1_000_000))), + Rc::new(ZeroEntropy), + ) + .await; + + assert_eq!( + recording.proxy_calls.borrow().as_slice(), + [Some("127.0.0.1:9050".to_string())] + ); + }) + .await; +} + +/// A host that starts with Tor off must not have leaked the proxy address +/// to the HTTP port at all — `tor_proxy_address` is remembered for a later +/// `Intent::SetTorEnabled(true)`, but the port itself stays direct until then. +#[tokio::test] +async fn spawn_leaves_the_http_port_direct_when_tor_is_off() { + LocalSet::new() + .run_until(async { + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let spy = Rc::new(Spy::default()); + let recording = Rc::new(RecordingHttp::default()); + let ports = CorePorts { + http: Rc::clone(&recording) as Rc, + ..CorePorts::default() + }; + let _core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy: Some("127.0.0.1:9050".to_string()), + tor: false, + device_label: None, + reconnect: fast_reconnect(), + }, + ports, + spy, + Rc::new(FixedClock(RefCell::new(1_000_000))), + Rc::new(ZeroEntropy), + ) + .await; + + assert!(recording.proxy_calls.borrow().is_empty()); + }) + .await; +} + +/// Spawn a core with the given Tor settings; the caller points it at relays. +/// The `fast_reconnect` override keeps the FSM timers out of the test's way. +async fn core_with_tor( + phone: &Keypair, + spy: Rc, + ports: CorePorts, + proxy: Option, + tor: bool, +) -> Core { + Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone.clone())), + proxy, + tor, + device_label: None, + reconnect: fast_reconnect(), + }, + ports, + spy, + Rc::new(FixedClock(RefCell::new(1_000_000))), + Rc::new(ZeroEntropy), + ) + .await +} + +/// A SOCKS5 proxy on loopback that records every `CONNECT` target and, for a +/// reachable target, forwards the connection. A test asserts through it that +/// traffic expected to be proxied really is — and that traffic expected to be +/// direct never appears here. +struct SocksProxy { + /// `127.0.0.1:` — the proxy address a `CoreConfig` is given. + address: String, + connects: mpsc::UnboundedReceiver<(String, u16)>, +} + +impl SocksProxy { + async fn spawn() -> Self { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap().to_string(); + let (tx, connects) = mpsc::unbounded_channel(); + tokio::task::spawn_local(async move { + while let Ok((socket, _)) = listener.accept().await { + tokio::task::spawn_local(serve_socks5(socket, tx.clone())); + } + }); + Self { address, connects } + } + + /// The next `CONNECT` the proxy saw, or `None` if none came within `within`. + async fn next_connect(&mut self, within: Duration) -> Option<(String, u16)> { + tokio::time::timeout(within, self.connects.recv()).await.ok().flatten() + } + + /// A `CONNECT` the proxy saw and nobody consumed yet. + fn pending_connect(&mut self) -> Option<(String, u16)> { + self.connects.try_recv().ok() + } +} + +/// Serve one SOCKS5 client: no-auth greeting, one `CONNECT`, then forward. +/// A `.onion` name cannot be resolved here, so it is refused the way a Tor +/// proxy refuses a service it cannot reach — the point for the tests is that +/// the request reached the proxy at all. +async fn serve_socks5( + mut client: tokio::net::TcpStream, + connects: mpsc::UnboundedSender<(String, u16)>, +) { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + let mut greeting = [0u8; 2]; + if client.read_exact(&mut greeting).await.is_err() || greeting[0] != 5 { + return; + } + let mut methods = vec![0u8; greeting[1] as usize]; + if client.read_exact(&mut methods).await.is_err() { + return; + } + if client.write_all(&[5, 0]).await.is_err() { + return; + } + + let mut request = [0u8; 4]; + if client.read_exact(&mut request).await.is_err() || request[0] != 5 || request[1] != 1 { + return; + } + let host = match request[3] { + 1 => { + let mut ip = [0u8; 4]; + if client.read_exact(&mut ip).await.is_err() { + return; + } + ip.iter().map(|b| b.to_string()).collect::>().join(".") + } + 3 => { + let mut len = [0u8; 1]; + if client.read_exact(&mut len).await.is_err() { + return; + } + let mut name = vec![0u8; len[0] as usize]; + if client.read_exact(&mut name).await.is_err() { + return; + } + String::from_utf8_lossy(&name).to_string() + } + 4 => { + let mut ip = [0u8; 16]; + if client.read_exact(&mut ip).await.is_err() { + return; + } + std::net::Ipv6Addr::from(ip).to_string() + } + _ => return, + }; + let mut port = [0u8; 2]; + if client.read_exact(&mut port).await.is_err() { + return; + } + let port = u16::from_be_bytes(port); + let _ = connects.send((host.clone(), port)); + + if host.ends_with(".onion") { + let _ = client.write_all(&[5, 4, 0, 1, 0, 0, 0, 0, 0, 0]).await; + return; + } + match tokio::net::TcpStream::connect((host.as_str(), port)).await { + Ok(mut upstream) => { + if client.write_all(&[5, 0, 0, 1, 0, 0, 0, 0, 0, 0]).await.is_err() { + return; + } + let _ = tokio::io::copy_bidirectional(&mut client, &mut upstream).await; + } + Err(_) => { + let _ = client.write_all(&[5, 4, 0, 1, 0, 0, 0, 0, 0, 0]).await; + } + } +} + +/// Tor is switched on while the core is already connected: the next relay +/// connection is dialled through the SOCKS5 proxy (no Nostr traffic takes a +/// direct path while Tor is on), Blossom uploads follow, and switching Tor +/// off redials relay traffic directly. +#[tokio::test] +async fn toggling_tor_at_runtime_moves_relay_and_http_traffic_through_the_proxy_and_back() { + LocalSet::new() + .run_until(async { + let mut mock = mock_relay().await; + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let spy = Rc::new(Spy::default()); + let recording = Rc::new(RecordingHttp::default()); + let mut proxy = SocksProxy::spawn().await; + let proxy_address = proxy.address.clone(); + + let core = core_with_tor( + &phone, + Rc::clone(&spy), + CorePorts { + http: Rc::clone(&recording) as Rc, + ..CorePorts::default() + }, + Some(proxy_address.clone()), + false, + ) + .await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.set_relays(vec![mock.url.clone()]); + core.start(); + eose_all(&mut mock).await; + while mock.try_accept() {} + assert!( + proxy.pending_connect().is_none(), + "with Tor off the relay connection must not touch the proxy" + ); + assert!(recording.proxy_calls.borrow().is_empty(), "with Tor off Blossom stays direct"); + + core.dispatch(Intent::SetTorEnabled(true)).await; + let relay = mock.url.trim_start_matches("ws://").to_string(); + let (relay_host, relay_port) = relay.split_once(':').unwrap(); + assert_eq!( + proxy.next_connect(Duration::from_secs(5)).await, + Some((relay_host.to_string(), relay_port.parse().unwrap())), + "enabling Tor must redial the relay through the proxy" + ); + assert!( + mock.next_accept(Duration::from_secs(5)).await, + "the proxied relay dial must reach the relay" + ); + assert_eq!( + recording.proxy_calls.borrow().as_slice(), + [Some(proxy_address.clone())], + "Blossom uploads must follow the same switch" + ); + assert!(proxy.pending_connect().is_none(), "one relay, one proxied dial"); + + core.dispatch(Intent::SetTorEnabled(false)).await; + assert!( + mock.next_accept(Duration::from_secs(5)).await, + "disabling Tor must redial the relay" + ); + assert!( + proxy.pending_connect().is_none(), + "a relay dial with Tor off must bypass the proxy" + ); + assert_eq!( + recording.proxy_calls.borrow().as_slice(), + [Some(proxy_address.clone()), None], + "Blossom is direct again once Tor is off" + ); + }) + .await; +} + +/// Enabling Tor at runtime swaps in the Tor reconnect timing: the connect +/// deadline must cover the slower dial the proxy just introduced. With the +/// clearnet deadline still in force, an attempt that is still dialling is +/// abandoned long before Tor's own transport would give up. +#[tokio::test] +async fn enabling_tor_at_runtime_widens_the_connect_deadline_to_tors() { + LocalSet::new() + .run_until(async { + // Accepts the TCP connection but never answers the WebSocket + // handshake, so the attempt stays `Connecting`. + let silent = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let relay = format!("ws://{}", silent.local_addr().unwrap()); + tokio::task::spawn_local(async move { + let mut held = Vec::new(); + while let Ok((socket, _)) = silent.accept().await { + held.push(socket); + } + }); + let proxy = SocksProxy::spawn().await; + let clock = Rc::new(FixedClock(RefCell::new(1_000_000))); + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let core = Core::spawn( + CoreConfig { + identity: Rc::new(crate::signer::LocalSigner(phone)), + proxy: Some(proxy.address.clone()), + tor: false, + device_label: None, + reconnect: DEFAULT_RECONNECT_CONFIG, + }, + CorePorts::default(), + Rc::new(Spy::default()), + Rc::clone(&clock) as Rc, + Rc::new(ZeroEntropy), + ) + .await; + core.set_machines(vec![generate_keypair().pubkey_hex]); + core.set_relays(vec![relay]); + core.start(); + assert_eq!( + core.connection_status().await.0, + ConnectionStatus::Connecting, + "the attempt starts on the clearnet timing" + ); + + core.dispatch(Intent::SetTorEnabled(true)).await; + *clock.0.borrow_mut() += DEFAULT_RECONNECT_CONFIG.connect_deadline_ms + 1; + core.tick(); + assert_eq!( + core.connection_status().await.0, + ConnectionStatus::Connecting, + "enabling Tor must widen the connect deadline, not keep the clearnet one" + ); + + *clock.0.borrow_mut() = 1_000_000 + TOR_RECONNECT_CONFIG.connect_deadline_ms; + core.tick(); + assert_eq!( + core.connection_status().await.0, + ConnectionStatus::WaitingRetry, + "the Tor deadline still bounds an attempt that never reaches EOSE" + ); + }) + .await; +} + +/// With Tor on, a direct link's `.onion` endpoint goes through the proxy — +/// only Tor reaches it — while a LAN endpoint is dialled directly. A direct +/// link is the user's own bridge on their own network, not Nostr traffic. +#[tokio::test] +async fn with_tor_on_a_direct_link_proxies_its_onion_endpoint_and_dials_lan_directly() { + LocalSet::new() + .run_until(async { + let lan = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let lan_address = lan.local_addr().unwrap(); + let (lan_hit_tx, mut lan_hit_rx) = mpsc::unbounded_channel(); + tokio::task::spawn_local(async move { + let mut held = Vec::new(); + while let Ok((socket, _)) = lan.accept().await { + held.push(socket); + let _ = lan_hit_tx.send(()); + } + }); + let mut proxy = SocksProxy::spawn().await; + + let phone = keypair_from_secret_hex(SEC_PHONE).unwrap(); + let machine = generate_keypair(); + let mut state = client_core::stores::machines::MachinesState::default(); + state.register_machine(&machine.pubkey_hex, "bridge", None, None, &[]); + state.machines.get_mut(&machine.pubkey_hex).unwrap().direct = Some(protocol::direct::DirectInfo { + endpoints: vec!["ws://abc.onion:7448".into(), format!("ws://{lan_address}")], + cert_sha256: None, + }); + let kv = MemoryKv::seeded([( + crate::stores::MACHINES_KEY, + client_core::stores::machines::serialize_machines(&state.machines), + )]); + // No relay is configured: nothing but the direct link may reach + // the proxy, so its CONNECT log is unambiguous. + let core = core_with_tor( + &phone, + Rc::new(Spy::default()), + CorePorts { kv: Rc::new(kv), ..CorePorts::default() }, + Some(proxy.address.clone()), + true, + ) + .await; + core.start(); + + assert_eq!( + proxy.next_connect(Duration::from_secs(5)).await, + Some(("abc.onion".to_string(), 7448)), + "the onion endpoint must be dialled through the proxy" + ); + tokio::time::timeout(Duration::from_secs(5), lan_hit_rx.recv()) + .await + .expect("the LAN endpoint must be dialled") + .expect("the LAN listener is alive"); + assert!( + proxy.pending_connect().is_none(), + "the LAN endpoint must be dialled directly, never through the proxy" + ); + }) + .await; +} From 509b053e4aa2e9ca673f9846ab337206d297de39 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 09:11:26 +0100 Subject: [PATCH 41/42] build(codedeck): give the Rust check the Android corpus copy it reads client-ffi's display corpus test include_str!s the Android copy of the corpus to prove the two match, so ./codedeck check failed to compile it in its container, which held only crates/ and the root manifests. Add that file to the pack list. Co-Authored-By: Claude Code --- codedeck | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/codedeck b/codedeck index 9f0dcf07..8911546a 100755 --- a/codedeck +++ b/codedeck @@ -451,9 +451,11 @@ check_rust() { -v codedeck-cargo-registry:/usr/local/cargo/registry \ -v codedeck-check-target:/repo/target - # The pack list must carry every repository-root file the workspace reads - # at compile time: bridge-runtime's tests include_str! config.example.json. - pack_repo_into "$name" /repo crates Cargo.toml Cargo.lock config.example.json + # The pack list must carry every file outside crates/ the workspace reads + # at compile time: bridge-runtime's tests include_str! config.example.json, + # and client-ffi's corpus test the Android copy of the display corpus. + pack_repo_into "$name" /repo crates Cargo.toml Cargo.lock config.example.json \ + apps/android/app/src/test/resources/display_entries_corpus.json case "$1" in typecheck) dexec -w /repo "$name" cargo clippy --locked --workspace --all-targets -- -D warnings ;; test) dexec -w /repo "$name" cargo test --locked --workspace ;; From a38f4b81914392a5f860387d56e09dd202cb09bd Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Tue, 27 Oct 2026 09:21:13 +0100 Subject: [PATCH 42/42] fix(agent-host): redact a secret whole when another secret is part of it Redaction replaced each secret's forms in turn. When one secret was part of another (a token, and a header value built from it) and came first, the longer one was cut apart and its tail stayed in the text that is logged or sent to the phone. Every form of every secret is now replaced longest first across all of them. A secret carried in an address is escaped, so its percent-encoded form is removed as well. Co-Authored-By: Claude Code --- .../src/sdk/__tests__/redact.test.ts | 22 +++++++++++++++++++ packages/agent-host/src/sdk/redact.ts | 19 +++++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 packages/agent-host/src/sdk/__tests__/redact.test.ts diff --git a/packages/agent-host/src/sdk/__tests__/redact.test.ts b/packages/agent-host/src/sdk/__tests__/redact.test.ts new file mode 100644 index 00000000..dc496d6d --- /dev/null +++ b/packages/agent-host/src/sdk/__tests__/redact.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from 'vitest'; +import { redactSecrets } from '../redact'; +import { expectNoSecretLeaks } from './secrets'; + +describe('redactSecrets', () => { + it('removes a secret whole even when a shorter secret is part of it', () => { + // A profile's token and a header built from it: the short one listed + // first must not cut the long one apart and leave its tail behind. + const short = 'sk-cdx-7Qm2Lp9'; + const long = `${short}Rt4Vb8Nz1Kx5Jr3Hd`; + const text = redactSecrets(`refused key ${long}`, [short, long]); + expectNoSecretLeaks({ logs: [text] }, [long]); + }); + + it('removes a secret that travelled percent-encoded in an address', () => { + // A key in a query string is escaped, so its raw spelling never occurs. + const key = 'ant+cdx/3Vn8Kq2Ws6Ye1=Pu4Tz9Mo7Lb'; + const text = redactSecrets(`GET https://example.test/models?key=${encodeURIComponent(key)} failed`, [key]); + expect(text).not.toContain(encodeURIComponent(key)); + expect(text).toContain('[redacted]'); + }); +}); diff --git a/packages/agent-host/src/sdk/redact.ts b/packages/agent-host/src/sdk/redact.ts index 3d889618..d86d9428 100644 --- a/packages/agent-host/src/sdk/redact.ts +++ b/packages/agent-host/src/sdk/redact.ts @@ -17,25 +17,32 @@ export const REDACTED = '[redacted]'; /** Below this length a value is not treated as a credential. */ const MIN_SECRET_LENGTH = 8; -/** The forms one secret may appear in, longest first. */ +/** The forms one secret may appear in. */ function formsOf(secret: string): string[] { const bytes = Buffer.from(secret, 'utf8'); - const forms = new Set([ + return [ secret, `Bearer ${secret}`, + // A key in an address (a query string, a pasted callback) is escaped. + encodeURIComponent(secret), bytes.toString('base64'), bytes.toString('base64').replace(/=+$/, ''), bytes.toString('base64url'), - ]); - return [...forms].filter((form) => form.length >= MIN_SECRET_LENGTH).sort((a, b) => b.length - a.length); + ]; } /** `text` with every occurrence of each secret (and its encodings) removed. */ export function redactSecrets(text: string, secrets: Iterable): string { - let out = text; + const forms = new Set(); for (const secret of secrets) { if (typeof secret !== 'string' || secret.length < MIN_SECRET_LENGTH) continue; - for (const form of formsOf(secret)) out = out.split(form).join(REDACTED); + for (const form of formsOf(secret)) if (form.length >= MIN_SECRET_LENGTH) forms.add(form); } + // Longest first across every secret, not per secret: one secret can be + // part of another (a token, and a header value built from it), and + // removing the shorter first would cut the longer apart and leave the rest + // of it in the text. + let out = text; + for (const form of [...forms].sort((a, b) => b.length - a.length)) out = out.split(form).join(REDACTED); return out; }