From aef8e2af174710b165e4a327f5426e13146c7c38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=A4user?= Date: Thu, 5 Nov 2026 15:01:54 +0100 Subject: [PATCH] Add AGENTS.md and handoff notes for agents Distill the session findings (BlueZ RSSI dedupe, beacon pauses, MSRV pinning, scan-only decision) so future agents and humans don't have to rediscover them. AGENTS.md carries the rules, docs/HANDOFF.md the history and open threads. --- AGENTS.md | 66 +++++++++++++++++++++++++++++++++++++++++++++++ docs/HANDOFF.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 AGENTS.md create mode 100644 docs/HANDOFF.md diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..de49f22 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,66 @@ +# AGENTS.md + +Guidance for AI coding agents working in this repository. Read this before +changing code; see `docs/HANDOFF.md` for the history behind the gotchas. + +## What this is + +`btle-proximity` is a Linux daemon (Rust, tokio, zbus) that locks the +desktop session when a paired phone leaves BLE range. It watches +advertisement updates from BlueZ over D-Bus, runs a small state machine +(Bootstrapping → Present → Suspect → Locked) and locks via +`loginctl lock-session`. There is no unlock: the phone returning only +rearms monitoring. See README.md for the full design. + +## Commands + +- `cargo build` / `cargo test` — build; run the policy state-machine tests +- `cargo install --path .` — install to `~/.cargo/bin` +- `systemctl --user restart btle-proximity` — restart the dev service +- `journalctl --user -u btle-proximity -f` — follow the daemon log +- `RUST_LOG=debug btle-proximity --dry-run` — run manually, no locking + +## Toolchain constraints + +- MSRV is `rust-version = "1.85"` in Cargo.toml (Ubuntu 24.04's rustc). + The resolver is MSRV-aware; do not bump dependencies past what 1.85 + supports. `Cargo.lock` is committed — keep it in sync. +- The dev machine has no rustup; distro `rustc`/`cargo` only. + +## Code map + +- `src/main.rs` — CLI entry, config loading, sd-notify readiness +- `src/config.rs` — TOML config, defaults, validation +- `src/policy.rs` — presence state machine: pure logic, unit-tested, + no I/O; timing/Duty decisions live here +- `src/scanner/bluez.rs` — BlueZ D-Bus integration: discovery duty + toggling, PropertiesChanged/InterfacesAdded handling, pre-lock connect + probe +- `src/locker.rs` — screen locking (logind or `lock_command`) +- `systemd/btle-proximity.service` — user service, `Type=notify` +- `config.toml.example` — shipped config sample with tuning guidance + +## Domain gotchas (learned the hard way — see docs/HANDOFF.md) + +- **BlueZ dedupes identical RSSI values**: `PropertiesChanged` is not + emitted when RSSI repeats, so a stationary phone goes silent in the + event stream. Silence is NOT evidence of absence. The duty escalation + in Present and the 10s connect probe before locking exist because of + this; do not "simplify" them away. +- **Beacon apps pause advertising** while the phone sits still (doze, + battery optimization). `missing_after_secs` must outlast those pauses; + during a pause the phone is both undetectable and unconnectable. +- `DuplicateData = true` in the discovery filter does not bypass the + RSSI dedupe at the `Device1` property level. +- `Device1.Connect` is the only reliable "is it actually there" oracle, + and it works whenever the phone advertises. Scan-based checks cannot + distinguish "silent but present" from "gone". +- The long-term fix for all of the above is the AdvertisementMonitor + roadmap (controller-offloaded patterns, BlueZ ≥ 5.65 + Experimental). + +## Conventions + +- Comments explain *why* (BlueZ quirks, design rationale), not *what*. +- State-machine changes belong in `policy.rs` with unit tests; + `bluez.rs` is the I/O shell around it. Keep the split. +- Commits: short imperative subject; body explains the why. diff --git a/docs/HANDOFF.md b/docs/HANDOFF.md new file mode 100644 index 0000000..ed22661 --- /dev/null +++ b/docs/HANDOFF.md @@ -0,0 +1,68 @@ +# Handoff notes + +Dated notes for picking this project back up: what happened, what was +learned, and which threads are still open. AGENTS.md carries the distilled +rules; this file carries the history behind them. + +## 2026-11-05 — false-lock debugging session + +**Symptoms.** The daemon flipped Present⇄Suspect every minute or so and +locked the screen three times in ~20 minutes while the phone sat on the +desk at -44..-65 dBm (rssi_lock is -80). + +**Root causes, in order of discovery:** + +1. *Duty math*: at the Present duty cycle (100ms window / 5s interval, + ~2% radio), the ~1 Hz beacon was only caught every ~25-50s — at or + beyond `presence_timeout_secs` (30). Journal showed Present stretches + of 46s/71s/45s, then a Suspect flip the moment a 30s gap appeared. +2. *BlueZ RSSI dedupe*: `device_set_rssi()` early-returns when the value + is unchanged, so a stationary phone's event stream goes fully silent + (with `DuplicateData = true` and regardless of duty cycle). Silence + for `missing_after_secs` (60) → lock, e.g. 14:36:56 and 14:39:07, both + exactly 60s after the last event. +3. *Beacon pauses*: the phone's beacon app stops advertising entirely for + 60-90s+ stretches (doze / battery optimization). During a pause the + phone is undetectable *and* unconnectable — this is why the first 3s + connect probe failed at 14:49:56 while the phone was in range. + +**Fixes shipped:** + +- `policy.rs`: Present escalates to the Suspect duty once the last + sighting ages past half of `presence_timeout_secs`, without leaving + Present (kills the flapping; freshness restored within seconds). +- `bluez.rs`: pre-lock connect probe (`probe_reachable`), now 10s; a + successful connection cancels the lock and returns to Present. + Probe outcomes are logged at INFO/WARN — every lock decision is now + explainable from the journal. +- Config guidance: `missing_after_secs = 120` (sample config and local + config) so a lock requires the phone to be silent *and* unconnectable + for 130s straight. + +**Decision: stay scan-only.** The alternative — a persistent LE +connection held by the daemon (presence = link, immune to advertising +pauses, ~zero desktop scanning while connected) — was tested and is +viable: a manual connection held 60s+ through a silent stretch and the +daemon stayed Present with no scanning. It remains the guaranteed fix if +false locks recur. Watch out if adopting it: connecting to a dual-mode +phone registers the PC as an audio device on the phone (A2DP SEPs show +up), and the phone battery profile changes (probably better than 1 Hz +advertising, but unverified). + +**Verification notes.** `bluetoothctl connect ` is a quick manual +oracle for "is the phone connectable right now". Probe behaviour at INFO: +`probe: no connection within 10s, treating phone as out of range` vs +`probe: connect rejected: ...`; success logs `connect probe: phone in +range, not locking`. + +**Open threads:** + +- If false locks recur despite 120s + 10s probe: check the journal for + the probe outcome line, then reconsider the persistent connection. +- AdvertisementMonitor (README roadmap) removes the RSSI-dedupe class of + problems at the controller level; needs BlueZ ≥ 5.65 and + `Experimental = true`. +- v2 unlock (GATT + biometric-signed nonce, see README) — the probe and + the persistent-connection findings are groundwork for that design. +- MSRV/toolchain: project pins `rust-version = "1.85"` so Ubuntu 24.04 + and 26.04 build identically; Cargo.lock is committed.