control-channel: userActive is the SendAwakeBeat test, not a time window

QA finding 3. `tick` computed `userActive` as "input within the last
heartbeat interval". `SendAwakeBeat` instead sends `Awake` when
`lastRealInputEventCount != Event.RealInputEventCount` — real input since
the previous heartbeat — and re-records the counter every time it runs
(Common.cs:545-558). Keep the same two counters: `noteUserActivity()` is
`Event.RealInputEventCount++` (InputHook.cs:223, 345) and the channel
re-records on its own heartbeat boundary, which is the one global beat
Windows's 100 ms timer runs for every socket (frmScreen.cs:565-575).

Also fixes the stale citation for this rule in docs/protocol.md §3.3,
which pointed at SendHeartBeat/screen-capture code (Common.cs:499-513,
662-676) instead of SendAwakeBeat.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 01:41:41 -07:00
1 parent c9ea89ac50
commit a892af6ca3
3 files changed
+33 -8

No files matched your search

+19 -4
View File
@@ -56,7 +56,9 @@ public final class ControlChannel: @unchecked Sendable {
private var failures: [String: Int] = [:]
private var blocked: Set<String> = []
private var dialDeadline: TimeInterval = 0
private var lastUserActivity: TimeInterval = 0
private var inputEventCount: UInt64 = 0
private var beatInputEventCount: UInt64 = 0
private var lastBeatAt: TimeInterval = 0
private var controlledPeerID: UInt32?
private var lastPathStatus: NWPath.Status?
private var connectionCount = 0
@@ -193,9 +195,11 @@ public final class ControlChannel: @unchecked Sendable {
}
/// Tells the channel the user just did something, which is what turns the
/// next `Heartbeat` into an `Awake` (`Common.cs:545-552`).
/// next `Heartbeat` into an `Awake` (`Common.cs:545-558`). This is our
/// `Event.RealInputEventCount++` (`InputHook.cs:223, 345`): the count is
/// what matters, not when it happened.
public func noteUserActivity() {
queue.async { [self] in lastUserActivity = monotonicNow() }
queue.async { [self] in inputEventCount &+= 1 }
}
/// Names the peer currently receiving our input, or `nil` when control is
@@ -443,11 +447,22 @@ public final class ControlChannel: @unchecked Sendable {
source.resume()
}
/// `Awake` or `Heartbeat` is decided by whether the real-input counter moved
/// since the *previous* heartbeat, not by a window around `now`:
/// `SendAwakeBeat` sends `Awake` when `lastRealInputEventCount !=
/// Event.RealInputEventCount` and records the counter again on its way out
/// (`Common.cs:545-558`). The two counters here are that pair, and the beat
/// they are recorded on is the one global heartbeat Windows's 100 ms timer
/// runs for every socket at once (`frmScreen.cs:565-575`).
private func tick() {
guard running else { return }
let now = monotonicNow()
let interval = config.blockScreenSaver ? Session.fastHeartbeat : Session.slowHeartbeat
let userActive = lastUserActivity > 0 && now - lastUserActivity <= interval
let userActive = inputEventCount != beatInputEventCount
if now - lastBeatAt >= interval {
lastBeatAt = now
beatInputEventCount = inputEventCount
}
for connection in connections {
connection.isControlled = connection.peerID != nil && connection.peerID == controlledPeerID
connection.tick(now: now, userActive: userActive)
+5 -1
View File
@@ -89,7 +89,11 @@ No packet is interpreted here.
budget expired is actually dialled.
- **Ticks**: the channel runs one 100 ms `DispatchSourceTimer` that
calls `Session.tick(now:userActive:)` on every connection (the app
reports `userActive` via `noteUserActivity()`), drains outboxes and
reports input via `noteUserActivity()`, which is our
`Event.RealInputEventCount++`, `InputHook.cs:223, 345`; `userActive` is
then "the counter moved since the previous heartbeat", the test
`SendAwakeBeat` makes and re-records every time it runs,
`Common.cs:545-558` — not a sliding window around `now`), drains outboxes and
events, and detects the C2 "30 s silence while we are sending input"
stall: if the connection the sink marked as `controlled` receives
nothing for 30 s while packets are being forwarded to it, close it.
+9 -3
View File
@@ -215,10 +215,16 @@ Status colours are in `doc/devdocs/modules/mousewithoutborders.md`.
### 3.3 Liveness
- Heartbeat from the 100 ms UI timer (`frmScreen.cs:285-288, 566-575`):
- Heartbeat from the 100 ms UI timer (`frmScreen.cs:285-288, 565-575`):
every 60 s when `BlockScreenSaver` is on, else every 300 s.
`Awake (21)` if the user was recently active, else `Heartbeat (20)`
(`Common.cs:499-513, 662-676`).
`Awake (21)` if real input happened since the previous heartbeat, else
`Heartbeat (20)` — `SendAwakeBeat` compares `lastRealInputEventCount !=
Event.RealInputEventCount` and re-records the counter every time it runs
(`Common.cs:542-559`), so this is a since-the-last-beat test, not a
sliding window. The counter is bumped by the input hook
(`Event.cs:51-55`, `InputHook.cs:223, 345`). `Awake` also requires
`BlockScreenSaver`, `IsMyDesktopActive()` and neither the logon nor the
screen-saver desktop (`Common.cs:547-548`).
- Key propagation chain during pairing: `Heartbeat_ex (51)` → receiver
adopts key, replies `Heartbeat_ex_l2 (52)` → `Heartbeat_ex_l3 (53)`
(`Receiver.cs:210-243`). *Port decision: not implemented in M2; the