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)