control-channel: the Awake test moves into Session, where the beat is

Round-2 QA finding: the channel reset beatInputEventCount on its own
clock, out of phase with every connection's heartbeat instant, so a
session could send Heartbeat (20) where Windows sends Awake (21) — and
Awake is what drives the peer's Common.HumanBeingDetected()
(Receiver.cs:245-248), the default-on BlockScreenSaver mechanism.

Windows's beat is the send: SendAwakeBeat() compares
lastRealInputEventCount against Event.RealInputEventCount and re-records
it in the same call (Common.cs:542-559). So the comparison belongs at the
send. Session.tick(now:userActive:) becomes
Session.tick(now:inputEventCount:), taking the raw process-wide counter
(Event.cs:51-55, InputHook.cs:223, 345) and keeping its own
beatInputEventCount — Common.lastRealInputEventCount, Common.cs:543 —
recorded on both branches (:558).

ControlChannel loses lastBeatAt and beatInputEventCount entirely;
noteUserActivity() only increments the counter and tick() hands the same
value to every connection. No userActive-shaped name is left in Sources.

Sources/MWBCore/Session.swift is session-matrix's file, edited for this
fix with the lead's authorisation, as is that lane's Handshakes section.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 01:57:03 -07:00
1 parent 23eddb42b9
commit c03b8cdbcf
5 files changed
+158 -40

No files matched your search

+17 -20
View File
@@ -57,8 +57,6 @@ public final class ControlChannel: @unchecked Sendable {
private var blocked: Set<String> = []
private var dialDeadline: 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
@@ -194,10 +192,13 @@ 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-558`). This is our
/// `Event.RealInputEventCount++` (`InputHook.cs:223, 345`): the count is
/// what matters, not when it happened.
/// Tells the channel the user just did something, which is what turns a
/// connection's next `Heartbeat` into an `Awake` (`Common.cs:545-559`).
///
/// This is exactly our `Event.RealInputEventCount++` (`Event.cs:51-55`,
/// `InputHook.cs:223, 345`) and nothing more: the channel only counts, and
/// each `Session` decides `Awake` against the count it saw at its own
/// previous beat.
public func noteUserActivity() {
queue.async { [self] in inputEventCount &+= 1 }
}
@@ -447,25 +448,21 @@ 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`).
/// The tick hands every connection the raw real-input counter and decides
/// nothing about it. `SendAwakeBeat`'s `lastRealInputEventCount !=
/// Event.RealInputEventCount` test is a comparison against the counter *as of
/// the previous beat*, and the beat is the send (`Common.cs:545-559`,
/// `frmScreen.cs:565-575`), so the comparison belongs where the send is
/// scheduled: inside each `Session`, which owns its own heartbeat instant.
/// A boundary kept here instead would be out of phase with every connection's
/// own beat and could swallow the input a peer needs to stay awake.
private func tick() {
guard running else { return }
let now = monotonicNow()
let interval = config.blockScreenSaver ? Session.fastHeartbeat : Session.slowHeartbeat
let userActive = inputEventCount != beatInputEventCount
if now - lastBeatAt >= interval {
lastBeatAt = now
beatInputEventCount = inputEventCount
}
let count = inputEventCount
for connection in connections {
connection.isControlled = connection.peerID != nil && connection.peerID == controlledPeerID
connection.tick(now: now, userActive: userActive)
connection.tick(now: now, inputEventCount: count)
}
updateClients(now: now)
}
+4 -3
View File
@@ -315,10 +315,11 @@ final class PeerConnection: @unchecked Sendable {
// MARK: - Time
/// Heartbeats, and the C2 rule that a controlled peer which has gone quiet
/// for 30 s while we are feeding it input is gone.
func tick(now: TimeInterval, userActive: Bool) {
/// for 30 s while we are feeding it input is gone. `inputEventCount` is the
/// channel's raw real-input counter, passed straight through to the session.
func tick(now: TimeInterval, inputEventCount: UInt64) {
guard !finished, closing == nil else { return }
session.tick(now: now, userActive: userActive)
session.tick(now: now, inputEventCount: inputEventCount)
pump()
guard status == .connected, isControlled, lastForward > lastReceive else { return }