From d141754ed95be100ef3e7df5500e82a71daacaf0 Mon Sep 17 00:00:00 2001 From: Dmitriy Gajewski Date: Sat, 7 Nov 2026 01:51:23 +0300 Subject: [PATCH] fix(auth): require live Bluetooth transport health for panel readiness --- crates/sy-auth/src/syauth.rs | 52 ++++++++++++++ crates/sy-auth/tests/standalone.rs | 31 ++++++++- docs/how-to/troubleshoot-syauth.md | 28 +++++--- llms-full.txt | 28 +++++--- specs/bugs/BUG-20261107-0012.md | 105 +++++++++++++++++++++++++++++ 5 files changed, 223 insertions(+), 21 deletions(-) create mode 100644 specs/bugs/BUG-20261107-0012.md diff --git a/crates/sy-auth/src/syauth.rs b/crates/sy-auth/src/syauth.rs index 90ddd57..57a9c56 100644 --- a/crates/sy-auth/src/syauth.rs +++ b/crates/sy-auth/src/syauth.rs @@ -62,6 +62,8 @@ const SCHEMA_VERSION: u32 = 1; #[derive(Debug, Clone)] pub struct StatusSummary { pub state: String, + /// Controller and advertisement health reported by the daemon. + pub transport_ready: Option, pub peer_id: Option, pub last_connect_ms_ago: Option, pub last_unlock_outcome: Option, @@ -105,8 +107,12 @@ pub fn parse_status_json(src: &str) -> Result let in_flight_challenges = first .and_then(|p| p.get("in_flight_challenges")) .and_then(|n| n.as_u64()); + let transport_ready = daemon + .get("transport_ready") + .and_then(serde_json::Value::as_bool); Ok(StatusSummary { state, + transport_ready, peer_id, last_connect_ms_ago, last_unlock_outcome: None, @@ -138,6 +144,11 @@ pub fn render_status_line(s: &StatusSummary) -> String { return format!("daemon {}", s.state); } let mut parts: Vec = vec!["daemon up".to_string()]; + match s.transport_ready { + Some(false) => parts.push("Bluetooth transport unavailable".to_string()), + None => parts.push("transport health unavailable".to_string()), + Some(true) => {} + } if let Some(ms) = s.last_connect_ms_ago { let mut chunk = format!("last connect {} ago", humanize_ms_ago(ms)); if let Some(outcome) = &s.last_unlock_outcome { @@ -240,8 +251,18 @@ pub fn render_pill( }); let (class, headline) = if !status.is_daemon_up() { ("degraded", "daemon down".to_string()) + } else if status.transport_ready == Some(false) { + ( + "degraded", + "Bluetooth transport unavailable · recovering".to_string(), + ) } else if !status.has_peer() { ("unpaired", "no phone bonded".to_string()) + } else if status.transport_ready.is_none() { + ( + "degraded", + "transport health unavailable · update syauth".to_string(), + ) } else if let Some(o) = recent_denied { let reason = match &o.kind { OutcomeKind::Denied(r) => denied_reason_text(r), @@ -1604,6 +1625,7 @@ mod tests { "daemon_socket": "/run/user/1000/syauth/auth.sock", "daemon": { "state": "up", + "transport_ready": true, "started_at": 1779192091, "peers": [ { @@ -1679,6 +1701,36 @@ mod tests { } } + #[test] + fn status_line_reports_transport_failure() { + let mut status = parse_status_json(FIXTURE_BONDED).expect("fixture"); + status.transport_ready = Some(false); + assert!(render_status_line(&status).contains("Bluetooth transport unavailable")); + } + + #[test] + fn waybar_requires_transport_health_before_reporting_ready() { + let mut fixture: serde_json::Value = serde_json::from_str(FIXTURE_BONDED).expect("fixture"); + fixture["daemon"] + .as_object_mut() + .expect("daemon") + .remove("transport_ready"); + let status = parse_status_json(&fixture.to_string()).expect("status"); + let pill = render_pill(&status, 0, &doctor_unknown(), None, 0, "fedora"); + assert_eq!(pill.class, "degraded"); + assert!(pill.tooltip.contains("transport health unavailable")); + } + + #[test] + fn waybar_reports_failed_transport_even_while_authenticating() { + let mut fixture: serde_json::Value = serde_json::from_str(FIXTURE_BONDED).expect("fixture"); + fixture["daemon"]["transport_ready"] = false.into(); + let status = parse_status_json(&fixture.to_string()).expect("status"); + let pill = render_pill(&status, 1, &doctor_unknown(), None, 0, "fedora"); + assert_eq!(pill.class, "degraded"); + assert!(pill.tooltip.contains("Bluetooth transport unavailable")); + } + #[test] fn waybar_renders_ok_class_when_bonded_idle() { let s = parse_status_json(FIXTURE_BONDED).expect("fixture parses"); diff --git a/crates/sy-auth/tests/standalone.rs b/crates/sy-auth/tests/standalone.rs index 7a108a6..c2d4aae 100644 --- a/crates/sy-auth/tests/standalone.rs +++ b/crates/sy-auth/tests/standalone.rs @@ -38,7 +38,7 @@ fn unanswered_phone_approval_keeps_the_applet_ready() { let upstream = root.path().join("syauth"); fs::write( &upstream, - "#!/bin/sh\nprintf '%s\\n' '{\"daemon\":{\"state\":\"up\",\"peers\":[{\"peer_id\":\"phone\",\"in_flight_challenges\":0}]},\"bluez_adapter\":\"unknown\"}'\n", + "#!/bin/sh\nprintf '%s\\n' '{\"daemon\":{\"state\":\"up\",\"transport_ready\":true,\"peers\":[{\"peer_id\":\"phone\",\"in_flight_challenges\":0}]},\"bluez_adapter\":\"unknown\"}'\n", ) .unwrap(); fs::set_permissions(&upstream, fs::Permissions::from_mode(0o700)).unwrap(); @@ -70,3 +70,32 @@ fn unanswered_phone_approval_keeps_the_applet_ready() { assert!(!tooltip.contains("reconnecting")); assert!(!tooltip.contains("phone reachable")); } + +#[test] +fn broken_transport_overrides_daemon_up_and_pending_authentication() { + use std::{fs, os::unix::fs::PermissionsExt}; + let root = tempfile::tempdir().unwrap(); + let upstream = root.path().join("syauth"); + fs::write(&upstream, r#"#!/bin/sh +printf '%s\n' '{"daemon":{"state":"up","transport_ready":false,"peers":[{"peer_id":"phone","in_flight_challenges":1}]},"bluez_adapter":"unknown"}' +"#).unwrap(); + fs::set_permissions(&upstream, fs::Permissions::from_mode(0o700)).unwrap(); + let output = Command::new(env!("CARGO_BIN_EXE_sy-system-auth")) + .args(["syauth", "--waybar"]) + .env("SY_SYAUTH_BIN", &upstream) + .env("SY_SYAUTH_AUDIT_LOG", root.path().join("missing.log")) + .env("XDG_RUNTIME_DIR", root.path()) + .env("XDG_STATE_HOME", root.path()) + .env("DBUS_SESSION_BUS_ADDRESS", "unix:path=/nonexistent") + .output() + .unwrap(); + assert!(output.status.success()); + let pill: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap(); + assert_eq!(pill["class"], "degraded"); + assert!( + pill["tooltip"] + .as_str() + .unwrap() + .contains("Bluetooth transport unavailable") + ); +} diff --git a/docs/how-to/troubleshoot-syauth.md b/docs/how-to/troubleshoot-syauth.md index 6f28f07..5498ce5 100644 --- a/docs/how-to/troubleshoot-syauth.md +++ b/docs/how-to/troubleshoot-syauth.md @@ -25,14 +25,18 @@ working state. ### A request times out while the phone is away An unanswered approval records a timeout for that request. The key stays cyan -while the daemon runs and has a phone bond. Hover over the key to see +while the daemon reports healthy Bluetooth transport and has a phone bond. +Hover over the key to see `approval timed out (phone may be away)` in the request history. Transport errors also appear in that history. -The `daemon ready` message describes the local service. That status doesn't confirm +The `daemon ready` message describes the local service and its advertisement. +That status doesn't confirm the phone's availability. A successful approval confirms the connection for -that request. Red marks a stopped daemon, a missing bond, or an authorization -error. A new request shows `authenticating` even after an earlier timeout. +that request. Red marks a stopped daemon, failed Bluetooth transport, a missing +bond, or an authorization error. Older daemons without transport health reporting show +`transport health unavailable · update syauth`. A new request shows +`authenticating` even after an earlier timeout. Keep the existing pairing and service running when an approval expires. ### Bluetooth is on but the phone can't discover the desktop @@ -51,12 +55,16 @@ reinitialization. Older daemons hardcode `hci0`, so restarting the same build can leave only its RPC socket running with `orchestrator not started` in the journal. A daemon status of `up` alone does not establish Bluetooth readiness. -The adapter-selection repair uses BlueZ's available default controller at -startup. After installing that repair, restart `syauth-presenced.service` and -verify advertising and a fresh phone subscription. Keep the existing bond; -pairing again does not repair an absent desktop adapter. See the -[adapter investigation](../../specs/bugs/BUG-20261031-1221.md) for validation -and deployment status. +The daemon checks controller and advertisement health every two seconds. +Controller loss or a failed advertisement clears `daemon.transport_ready` in +`syauth status --json`. The managed service restarts and rebuilds GATT and +advertising on the available default controller, including a controller with a +new name. It keeps retrying while Bluetooth is unavailable. The panel shows +`Bluetooth transport unavailable · recovering` or `daemon down` during recovery. + +Keep the existing bond. Pairing again does not repair an absent desktop +adapter. After recovery, verify a fresh phone subscription and approval. +See the [adapter recovery investigation](../../specs/bugs/BUG-20261107-0012.md). ### Desktop apps skip phone and FIDO authentication diff --git a/llms-full.txt b/llms-full.txt index 5cc7e9c..d6408f0 100644 --- a/llms-full.txt +++ b/llms-full.txt @@ -1879,14 +1879,18 @@ working state. #### A request times out while the phone is away An unanswered approval records a timeout for that request. The key stays cyan -while the daemon runs and has a phone bond. Hover over the key to see +while the daemon reports healthy Bluetooth transport and has a phone bond. +Hover over the key to see `approval timed out (phone may be away)` in the request history. Transport errors also appear in that history. -The `daemon ready` message describes the local service. That status doesn't confirm +The `daemon ready` message describes the local service and its advertisement. +That status doesn't confirm the phone's availability. A successful approval confirms the connection for -that request. Red marks a stopped daemon, a missing bond, or an authorization -error. A new request shows `authenticating` even after an earlier timeout. +that request. Red marks a stopped daemon, failed Bluetooth transport, a missing +bond, or an authorization error. Older daemons without transport health reporting show +`transport health unavailable · update syauth`. A new request shows +`authenticating` even after an earlier timeout. Keep the existing pairing and service running when an approval expires. #### Bluetooth is on but the phone can't discover the desktop @@ -1905,12 +1909,16 @@ reinitialization. Older daemons hardcode `hci0`, so restarting the same build can leave only its RPC socket running with `orchestrator not started` in the journal. A daemon status of `up` alone does not establish Bluetooth readiness. -The adapter-selection repair uses BlueZ's available default controller at -startup. After installing that repair, restart `syauth-presenced.service` and -verify advertising and a fresh phone subscription. Keep the existing bond; -pairing again does not repair an absent desktop adapter. See the -[adapter investigation](../../specs/bugs/BUG-20261031-1221.md) for validation -and deployment status. +The daemon checks controller and advertisement health every two seconds. +Controller loss or a failed advertisement clears `daemon.transport_ready` in +`syauth status --json`. The managed service restarts and rebuilds GATT and +advertising on the available default controller, including a controller with a +new name. It keeps retrying while Bluetooth is unavailable. The panel shows +`Bluetooth transport unavailable · recovering` or `daemon down` during recovery. + +Keep the existing bond. Pairing again does not repair an absent desktop +adapter. After recovery, verify a fresh phone subscription and approval. +See the [adapter recovery investigation](../../specs/bugs/BUG-20261107-0012.md). #### Desktop apps skip phone and FIDO authentication diff --git a/specs/bugs/BUG-20261107-0012.md b/specs/bugs/BUG-20261107-0012.md new file mode 100644 index 0000000..7133fe0 --- /dev/null +++ b/specs/bugs/BUG-20261107-0012.md @@ -0,0 +1,105 @@ +# BUG-20261107-0012: Adapter loss leaves a live daemon and a blue key + +## Summary + +syauth remains running with an obsolete Bluetooth adapter handle, while its +panel reports daemon readiness and authentication cannot reach the phone. + +## Reproduction + +- Installed packages: syauth 0.1.0-5.fc44, sy-system-auth 0.1.0-23.fc44. +- The process started November 1 and retained its transport until November 7. +- The journal first reports advertisement rotation failures on November 4 at + 17:53, with `target object was either not present or removed`. +- Kernel logs separately record a MediaTek `hci0` coredump on November 6 at + 20:09:46 and initialization of `hci1` at 20:09:53. This later reset does not + establish the initial cause of the November 4 failure. +- At investigation time BlueZ exposed `hci1`, powered on, with zero active + advertisements. Authentication requests timed out. The RPC still reported + `state: up`; the panel emitted `class: ok` and unknown phone availability. + +## Expected + +Adapter loss invalidates readiness. The daemon reconstructs transport using +the available controller, and the panel distinguishes transport failure from +an ordinary unanswered approval or an absent phone. + +## Actual + +The rotation loop logs backend errors indefinitely. Startup adapter selection +does not reconstruct a peripheral after its controller disappears. The panel +has only process readiness and authentication history, so it misses this fault. + +## Root Cause + +The previous repair selects BlueZ's default adapter only at construction. +PersistentPeripheral retains that adapter handle for its lifetime; the +orchestrator logs failed advertisement updates without rebuilding transport. +The status API does not report advertising or transport health independently +of daemon liveness. + +## Recovery and Verification + +Using the previously authorized service restart, only syauth-presenced was +restarted at 00:12:20 on November 7. The existing bond registered on the current +controller, advertising resumed, and the phone subscribed at 00:12:22. +The challenge at 00:14:07.930 completed with `outcome=ok` at 00:14:10.549. +`timeout 25 sudo -k /usr/bin/true` returned zero through phone approval. + +## Fix + +The daemon now latches controller removal and probes controller power and active +advertising every two seconds. A failed advertisement clears readiness. A lost +transport or failed Bluetooth startup exits with failure, and the managed service +retries every two seconds without a restart limit. Each restart selects the +available default controller and reconstructs GATT and advertising with the +existing bond. Health probes and orchestrator shutdown have bounded waits. + +Status RPC and CLI JSON now report optional `daemon.transport_ready`. The panel +requires reported healthy transport before showing ready, reports known failure +as `Bluetooth transport unavailable · recovering`, and identifies older daemons +that cannot report transport health. An unanswered approval remains request +history when transport is healthy. + +## Automated Verification + +- A failing startup regression proved that unavailable BlueZ previously left a + running socket-only daemon. It now exits for supervised recovery. +- A private D-Bus integration test runs the real daemon with BlueZ interfaces, + removes hci0, verifies failure exit and socket cleanup, and relaunches on hci1. + Recovery reloads the existing peer and preserves bond and key bytes. +- Controller event tests cover name reuse, unrelated removal, and stream loss. +- Readiness tests cover no-phone advertising, failed rotation, and controller + loss before the next minute rotation. RPC tests cover legacy wire decoding. +- CLI and applet tests distinguish daemon liveness from transport health. + The real applet executable shows degraded during a pending challenge when + transport is broken, while unanswered approval stays normal with healthy + transport. Missing health reporting cannot produce a ready indicator. + +## Deployment + +Both native RPM builds passed. Reinstalled syauth 0.1.0-5.fc44 and +sy-system-auth 0.1.0-23.fc44 from the tested source and reloaded the managed +unit. The new daemon started November 7 at 01:36:09, advertised at 01:36:11, +and logged a phone subscription at 01:36:17. Status reports +`transport_ready: true`; BlueZ reports one active advertisement. + +A fresh `timeout 25 sudo -k /usr/bin/true` returned zero through phone approval. +The daemon recorded `outcome=ok` for 01:36:33.173–01:36:35.308, taking 2.135 +seconds. Package verification passes. SHA-256, ownership, and private modes +match the pre-install snapshot for all seven bond and key files. + +`make lint` and `make test` pass in both working trees: sy has 1,751 passing +test results with 12 ignored; syauth has 486 passing results with 10 ignored. +The Spark lint gate and all 18 bridge tests pass. The isolated PR worktree +also passes lint and all 1,670 tests, plus its 17 Spark bridge tests. The syauth dependency checks +include patched `ctutils` and Tokio and the existing informational UniFFI parser +exception. Physical controller removal was exercised on an isolated D-Bus, +while the live host check verified normal advertising, subscription, and approval. + +## Traceability + +- Prior startup-only repair: BUG-20261031-1221. +- Ordinary timeout display repair: BUG-20261101-0319. +- Transport: sibling syauth PersistentPeripheral and orchestrator rotation. +- Display: sy-auth status parser and render_pill.