Two false locks with the phone present: missing_after=30 sat below the realistic worst-case event gap (beacon counter rotation every 10s times 2-20% duty sampling, with frozen-RSSI dedupe collapsing catches inside one window), and the connect probe ran while our own discovery duty-cycling was active, where attempts stall or abort with le-connection-abort-by-local. Probe now stops discovery first and retries (2x6s); sample config and handoff document the sampling math. Local config additionally raises rssi_lock to -85, where nearby-phone readings were pinning.
4.4 KiB
4.4 KiB
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 testscargo install --path .— install to~/.cargo/binsystemctl --user restart btle-proximity— restart the dev servicejournalctl --user -u btle-proximity -f— follow the daemon logRUST_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.lockis committed — keep it in sync. - The dev machine has no rustup; distro
rustc/cargoonly.
Code map
src/main.rs— CLI entry, config loading, sd-notify readinesssrc/config.rs— TOML config, defaults, validationsrc/policy.rs— presence state machine: pure logic, unit-tested, no I/O; timing/Duty decisions live heresrc/scanner/bluez.rs— BlueZ D-Bus integration: discovery duty toggling, PropertiesChanged/InterfacesAdded handling, pre-lock connect probesrc/locker.rs— screen locking (logind orlock_command)systemd/btle-proximity.service— user service,Type=notifyconfig.toml.example— shipped config sample with tuning guidanceandroid/— companion beacon app (Kotlin/Compose, separate Gradle build; seeandroid/README.md). Counter-rotating ServiceData payload defeats the BlueZ RSSI dedupe from the phone side.packaging/— Debian packaging: user unit variant for the packaged binary and the man page; consumed bycargo deb(see[package.metadata.deb]in Cargo.toml).- MSRV gotcha:
cargo-deb3.x does not build on rustc 1.85 (let-chains); pincargo install cargo-deb --version 2.12.1. - arm64 cross-build recipe (isolated rustup + static musl + rust-lld as
linker,
cargo-deb --no-strip) lives in README under "arm64"; the rustup env is ephemeral — re-run the recipe to rebuild. - Android release signing:
android/keystore.properties(gitignored) points at the keystore in~/.config/btle-proximity/; without itassembleReleaseproduces an unsigned APK.
Domain gotchas (learned the hard way — see docs/HANDOFF.md)
- BlueZ dedupes identical RSSI values:
PropertiesChangedis 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_secsmust outlast those pauses; during a pause the phone is both undetectable and unconnectable. DuplicateData = truein the discovery filter does not bypass the RSSI dedupe at theDevice1property level.- Sampling math: the beacon rotates its counter every 10 s, but the
daemon only hears it a fraction of the time (2-20% duty), and frozen
RSSI dedupes catches within one rotation window — event gaps of
25-60 s are routine.
missing_after_secsmust be well above that (30 s false-locked twice; 60 s is the sane floor). Device1.Connectis the only reliable "is it actually there" oracle, and it works whenever the phone advertises — but connection attempts stall or abort (le-connection-abort-by-local) while our own discovery duty-cycling is active. Stop discovery before probing and retry; do not run a single long attempt under scan contention.- 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.rswith unit tests;bluez.rsis the I/O shell around it. Keep the split. - Commits: short imperative subject; body explains the why.