app-engine: rework work log, the two answered requests, no stale counts

Records the rework round item by item, moves session-matrix request 1 and
input-capture request 1 to "answered", brings the Assumptions section
into line with the three fixes (0600 re-applied after the replace,
`releaseAll()` in the tail of `perform(_ actions:)`, `ByeBye` on the
signal path) and replaces the stale "Engine.swift 598" with prose —
quoting a line count in a work log guarantees it is wrong by the next
commit, and the judgement it supported is unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 03:58:57 -07:00
1 parent 76d1053aaf
commit 9be0923243
1 file changed
+116 -23
+116 -23
View File
@@ -50,8 +50,11 @@ the open requests lanes 3, 4 and 5 left for "app-shell".
`destination` and `swallowKeys`/`swallowMouse` = remote and not ALL
mode (input-capture request 2); `channel.markControlled(peerID:)` on
**every** switch including back to local (control-channel request 2);
`injector.releaseAll()` on every switch, lock and mode change
(`MachineStuff.cs:1047`, `InitAndCleanup.cs:214-243`); `.persistMatrix`
`injector.releaseAll()` on every switch that lands control back on this
Mac — the tail of `perform(_ actions:)`, so the `ByeBye`, edge-crossing
and `NextMachine` paths are covered as well as the hotkeys
(`Helper.cs:120-123`, `MachineStuff.cs:1047`,
`InitAndCleanup.cs:214-243`) — and on every lock and mode change; `.persistMatrix`
/ `.persistMachinePool` → `SettingsStore.update`.
- **Hotkeys**: `.switchToSlot(n)` → `releaseAll()` then
`switcher.switchToMachine` (`InputHook.cs:626-655`); `.lockMachine` →
@@ -84,9 +87,13 @@ the open requests lanes 3, 4 and 5 left for "app-shell".
`DispatchSourceSignal` for SIGTERM/SIGINT/SIGHUP, all of which call
`Cursor.restoreEverything()` and `Injector.releaseAll()`
**synchronously, without the actor** — both are documented safe from
any thread by their own lanes. `Engine.stop()` sends `ByeBye`, waits
150 ms for it to reach the wire (`InitAndCleanup.cs:192-215`) and then
closes.
any thread by their own lanes. The three signal handlers then call
`EmergencyRelease.sayGoodbye()`, which queues `ByeBye` on the channel
and waits 150 ms for it, so contracts C2's last row holds on that path
too: `ControlChannel` is `Sendable` and hops to its own queue, so it is
the one piece of Engine-owned state a handler may touch.
`Engine.stop()` does the same thing on the graceful path, waiting
150 ms for the wire (`InitAndCleanup.cs:192-215`) before it closes.
- **Reopen countdown** (control-channel request 1): a 1 s timer on the
actor; while nothing is connected, `channel.reopenAll()` every **30 s**,
which is both Windows' cadence (`frmScreen.cs:425-427`, `count % 300`
@@ -100,7 +107,9 @@ the open requests lanes 3, 4 and 5 left for "app-shell".
PowerToys names, `{"value": …}` boxing, `HotkeySettings` shape and
defaults (protocol §10.6 note 5). Stored at
`~/Library/Application Support/mwb-mac/settings.json`, mode 0600,
written to a temp file and renamed. First run generates `SecurityKey`
written to a temp file and renamed, with the mode set again on the
final path afterwards so a file that arrived from elsewhere at 0644
does not stay there. First run generates `SecurityKey`
(`SecurityKey.random()`), `MachineID` (`Int32.random(in: 1...max)`,
`Setting.cs:1048-1070`) and `DeviceID` (UUID). `PackageID`, `LastX`,
`LastY`, the pool and the matrix are written on exit. A
@@ -252,29 +261,33 @@ Rules the consuming lanes must honour:
- **`Permissions.request()` may be called from exactly one place**, the
menu item the user clicks (CLAUDE.md, Workflow).
### Requested of session-matrix (open)
### Requested of session-matrix (answered 2026-09-16)
1. **`PacketSequencer` is used from two queues by design and is not
1. **`PacketSequencer` is used from two queues by design and was not
thread-safe.** Contracts C1 says the process has one sequencer and
that the app stamps application packets; the Engine therefore calls
`sequencer.next()` on the actor while each `Session` calls it on the
channel's queue for its own `Handshake`/heartbeat/`Hi`/`ByeBye`
(`Session.swift:402-403`). `next()` is `value = value &+ 1` with no
synchronisation, so a lost update can hand two packets the same `Id`
and the receiver's replay ring will drop the second. It cannot be
fixed from this lane's files: please make `PacketSequencer` hold its
counter in an `OSAllocatedUnfairLock` (or `Atomic<Int32>`) and
declare it `Sendable`. `ReplayFilter` is only ever touched on the
channel's queue and is fine as it is.
(`Session.swift:402-403`). `next()` was `value = value &+ 1` with no
synchronisation, so a lost update could hand two packets the same `Id`
and the receiver's replay ring would drop the second.
**Done**, as a cross-lane edit the lead authorised: `next()` and
`current` hold an `NSLock` (Foundation only, so MWBCore's layering
holds) and `PacketSequencer` is `@unchecked Sendable`
(`Sources/MWBCore/Sequencing.swift`). The session-matrix Handshakes
section says so. `ReplayFilter` is only ever touched on the channel's
queue and is unchanged.
### Requested of input-capture (open)
### Requested of input-capture (1 answered 2026-09-16, 2 open)
1. **`PermissionState` has no public initialiser.** It declares
1. **`PermissionState` had no public initialiser.** It declared
`public let listen` / `public let post` and no `init`, so the
memberwise one is `internal` and no other target can build a value —
`EngineSnapshot`'s default has to call `Permissions.preflight()`,
which is a TCC query. Please add
`public init(listen: Bool, post: Bool)`.
memberwise one was `internal` and no other target could build a value —
`EngineSnapshot`'s default had to call `Permissions.preflight()`,
which is a TCC query. **Done**, as a cross-lane edit the lead
authorised: `public init(listen: Bool, post: Bool)`
(`Sources/MWBMac/Permissions.swift`), and `EngineSnapshot.permissions`
now defaults to `PermissionState(listen: false, post: false)`.
2. **`Cursor` publishes no "where is the pointer now".** The relative-mode
`NextMachine` path needs `GetCursorPos` on the *receiving* side
(`Receiver.cs:154`, protocol §10.6 note 3), so `Engine.pointerPosition()`
@@ -423,8 +436,10 @@ Rules the consuming lanes must honour:
first start, so the settings file is self-explanatory. Windows shows
its setup form instead; a peer's name still has to be added by hand
(or by app-ui) before anything is dialled.
- **`Settings.swift` is 628 lines and `Engine.swift` 598** — over the
300–600 target, under the 800 "consider splitting" line. Splitting
- **`Settings.swift` and `Engine.swift` are both a little over 600
lines** — over the 300–600 target, well under the 800 "consider
splitting" line. (No count is quoted here on purpose: the last one
went stale within a commit. `wc -l` is the authority.) Splitting
either would mean a file outside this lane's list.
*Verification.* `tools/gates.sh` green (lint --strict, MWBCore
@@ -436,6 +451,84 @@ Rules the consuming lanes must honour:
`NSApplication.run()` inwards — the status item, the tap, the channel,
every C2 row — is unexercised and is CP1's job.
- **2026-09-16 (dev, rework)** — the QA verdict's three findings, the two
cross-lane requests the lead authorised, and the stale number. Six
commits, one per item; gates green on the last.
1. **Finding 1, `settings.json` mode.** `Settings.save(to:)` now passes
`.usingNewMetadataOnly` to `replaceItemAt` *and* sets
`.posixPermissions: 0o600` on the path it actually wrote, with the
reason in a comment. QA was right about the mechanism: without the
option `replaceItemAt` copies the original item's metadata onto the
replacement, so a file copied in from a PC (README D3) kept its mode
for the life of the install, `SecurityKey` and all. The belt-and-
braces `setAttributes` afterwards means the guarantee no longer
depends on which of the two branches wrote the file, nor on
Foundation's documented behaviour staying documented.
(`Sources/MWBCore/Settings.swift`.)
2. **Finding 2, `releaseAll()` on the other switch-to-local paths.**
Fixed rather than documented. `perform(_ actions:)` remembers
`desMachineID`/`isBroadcasting` before running the actions and calls
`injector.releaseAll()` afterwards when the destination moved and
the new destination is this Mac — which is what
`Helper.HelperThread` does (`Helper.cs:120-123`), since its event is
set by `SwitchToMachine` (`MachineStuff.cs:1024`) *and*
`PrepareToSwitchToMachine` (`Event.cs:203`). That covers the
received `ByeBye`, the edge crossing and the `NextMachine` packet.
The one deliberate narrowing, in the doc comment: it fires only when
the destination actually moved. Our `releaseAll()` releases the keys
*this Mac injected*, so an unconditional call on a switch that left
control local — a matrix edit while a peer is driving us — would
drop that peer's modifiers mid-keystroke. Windows has no equivalent
hazard because its release is physical-key based.
(`Sources/mwb/EngineActions.swift`.)
3. **Finding 3, `ByeBye` on the signal path.** Implemented, not
documented away, and it is the few lines the lead hoped for:
`EmergencyRelease` gains `arm(channel:)` and `sayGoodbye()`, the
Engine arms it in `startChannel()` and disarms it in `stop()`, and
the SIGTERM/SIGINT/SIGHUP handler calls `run()` then `sayGoodbye()`
then exits. Nothing here wakes the actor: `ControlChannel` is
`Sendable` and every method hops to its own serial queue, which is
precisely the property the rest of the Engine's state lacks. The
150 ms wait is the same grace `Engine.stop()` gives the packet
(`InitAndCleanup.cs:192-215`); keys and pointer are already back
before it starts, so invariant 3 is not traded for it. Best effort
by construction — a stuck channel queue loses the packet and the
peer falls back to socket-level detection
(`SocketStuff.cs:1561-1563`). `docs/contracts.md` needed no
amendment. (`Sources/mwb/AppLifecycle.swift`, `Engine.swift`.)
4. **Request 1, `PacketSequencer`.** Cross-lane edit, authorised: the
counter is under an `NSLock` and the class is `@unchecked Sendable`
(`Sources/MWBCore/Sequencing.swift`). `NSLock` is Foundation, so
MWBCore's layering gate is untouched. The session-matrix README's
Handshakes signature block and its "one sequencer per process" rule
both now say it is thread-safe, and why (C1: one sequencer, two
queues).
5. **Request 2, `PermissionState.init`.** Cross-lane edit, authorised:
`public init(listen:post:)` added (`Sources/MWBMac/Permissions.swift`),
and the workaround removed — `EngineSnapshot.permissions` defaults to
`PermissionState(listen: false, post: false)` instead of running a
TCC query to obtain a value. The input-capture README's signature
line carries the new init.
6. **The stale "598 lines".** Replaced with prose and no number at all;
quoting a count in a work log guarantees it is wrong by the next
commit, and the judgement it supported (both files under 800, no
split) is unchanged. Today's counts, for the record only:
`Settings.swift` 644, `Engine.swift` 611.
*Verification.* `xcrun swift-format format -i --recursive Sources
Package.swift` then `tools/gates.sh` green (lint --strict, MWBCore
layering, build with warnings-as-errors). `.build/debug/mwb --version`
still prints its line. **The app was not launched** — unchanged from
the first pass, and for the same reason (CLAUDE.md invariant 5): every
path touched here (the signal handlers, the switch tail, the channel
arming) posts events or opens sockets when exercised, so it is CP1's
job with the user present. `Settings.save` was not exercised against a
real file either; the reasoning is Foundation's documented
`replaceItemAt` behaviour plus QA's own empirical check, and the
`setAttributes` line is there so the result does not depend on that
reasoning being right.
## QA log
- **2026-09-16 (QA)** — reviewed against CLAUDE.md, `docs/contracts.md` C1–C5,