Files
btle-proximity/AGENTS.md
T
Bastian Mäuser 4e4bf28508 Document missing_after_secs default as fallback only
The code default stays 30 s for configs that omit the key, but 30 s
false-locks with the companion beacon (event gaps of 25-60 s are
routine). Docs now say explicitly that shipped configs must set 60+.
2026-11-06 15:12:18 +01:00

4.5 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 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
  • android/ — companion beacon app (Kotlin/Compose, separate Gradle build; see android/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 by cargo deb (see [package.metadata.deb] in Cargo.toml).
  • MSRV gotcha: cargo-deb 3.x does not build on rustc 1.85 (let-chains); pin cargo 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 it assembleRelease produces an unsigned APK.

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.
  • 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_secs must be well above that (30 s false-locked twice; 60 s is the sane floor). The code default stays 30 as a conservative fallback for configs that omit the key — shipped configs (config.toml.example) must set 60 explicitly.
  • Device1.Connect is 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.rs with unit tests; bluez.rs is the I/O shell around it. Keep the split.
  • Commits: short imperative subject; body explains the why.