7.2 KiB
contracts.md — cross-module behaviour every lane must honour
Purpose. Feature READMEs specify one module each; this file specifies the behaviour that spans modules — sequencing, recovery, concurrency and who owns shared constants. It was written 2026-09-16 after the session-matrix review found these under-specified. A lane that cannot satisfy a contract here files a Handshake request; it does not reinterpret the contract.
C1. Sequence once, distribute unchanged
Windows stamps one Id per logical packet and writes the same bytes to
every matching socket (Common.cs:906, SocketStuff.cs TcpSend);
the receiver's 50-entry replay ring (Receiver.cs:40-82) then drops the
second delivery when a peer pair has two connections. We do the same:
PacketSequencerandReplayFilterare process singletons owned by the Engine (C3); noSessioncreates its own.- The app stamps
Id(andSrc) once and hands the identicalPacketto every target session viaSession.forward(_:).Sessionnever re-stamps a packet it forwards. Sessionstamps ids itself only for packets it originates (Handshake,HandshakeAck, heartbeats,Hi,ByeBye); those are per-connection by nature on Windows too.- Input (
Mouse,Keyboard),HideMouse,MachineSwitched,NextMachine,Matrixand clipboard chunks are always app-stamped and forwarded. ASessionAPI that lets a caller send an unstamped application packet to one connection is a defect.
C2. Local recovery — who does what
Invariant 3 (CLAUDE.md) in concrete tasks. "Route local" means: the
Switcher's desMachineID becomes the local id, the tap stops swallowing,
the cursor is re-associated and moved to the last local position, and
every key and mouse button we believe is down is released locally with
synthetic up events (Windows ReleaseAllKeys, InitAndCleanup.cs:214-243).
| trigger | detected by | action |
|---|---|---|
Escape shortcut while control is remote: the switch-machine hotkey naming the local slot (⌃⌥F, InputHook.cs:611-658), and additionally ⌃⌥⌘Escape (mac-only hard escape, always local, never forwarded) |
EventTap, synchronously in the callback | route local immediately (the tap flips the routing snapshot itself, then notifies the Engine); the Engine releases keys and sends HideMouse/MachineSwitched housekeeping afterwards |
Controlled peer disconnects (ByeBye, socket close, read error, .invalidKey) |
PeerConnection → Engine | route local, release keys |
| Controlled peer stops answering: no packet for 30 s while we are sending it input (tighter than the 25 min pool liveness, which is for the UI) | Engine timer | close that connection, route local |
Outbound stall: a send on the controlled peer does not complete within 500 ms (SocketStuff.cs:1286 SendTimeout) or its bounded queue overflows (C3) |
PeerConnection | close the connection → the disconnect row |
Event tap disabled by timeout or by user input (kCGEventTapDisabledByTimeout / ByUserInput) |
EventTap callback | re-enable the tap and route local — a tap that was disabled has lost events, so held-key state is unknown |
Tap cannot be created or permission revoked at runtime (CGPreflightListenEventAccess turns false) |
EventTap health check every 5 s | route local, show the permissions state in the menu bar |
| App quits or crashes | atexit/signal handler in mwb |
best effort: re-associate the cursor and release keys; sockets send ByeBye |
Recovery must not depend on the network: every action in the table is local, and none waits on a queue that could be stuck.
C3. Concurrency: the Engine, the routing snapshot, and bounded queues
- The Engine is one Swift
actorinmwb(app-shell lane) that owns all mutable shared state:Switcher(matrix, pool,desMachineID),PacketSequencer,ReplayFilter, the set ofPeerConnections, theInjector's held-key state. Everything that changes that state is anawait engine.…call.MWBCoretypes stay plain values/classes with no locking; the actor is the lock. - The routing snapshot is the one piece of state read outside the
actor: a tiny value (
destination: local | remote(peerID),swallowKeys,swallowMouse,escapeHotkeys) held in anOSAllocatedUnfairLock<RoutingSnapshot>(osmodule, macOS 13+), written by the Engine on every switch and read by the tap callback so it can decide synchronously whether to swallow an event. The lock is uncontended and held for a copy; that is within invariant 4. - Capture queue: the tap callback appends a
CapturedInputvalue to a bounded ring (capacity 4096) and returns. The Engine drains it on its own task. Overflow policy: mouse motion is coalesced (only the latest position per drain is kept; wheel deltas are summed); key and button transitions are never dropped — if a transition cannot be enqueued, the tap routes local (C2) and stops swallowing, because losing a key-up on a remote machine is worse than losing control. - Outbound queue: per connection, bounded to 512 framed packets
(Windows: a 320 000-byte socket buffer,
SocketStuff.cs:1283-1284). Sends are serialised on the connection's own queue with a 500 ms completion timeout. Overflow or timeout closes the connection (C2). - Inbound: each connection reads on its own queue, decrypts with its
CipherReader, feedsSession, and forwardsSessionEvents to the Engine. Only the Engine dispatches.packetevents to the injector, pasteboard or switcher. - The
CGEventTapcallback runs on a dedicatedCFRunLoopthread created for it, never on the main thread, so a busy UI cannot stall it.
C4. Shared constants and configuration are defined before first use
- Injected-event tag (
CGEventField.eventSourceUserDatavalue the tap must pass through untouched): defined inMWBCore/InputTypes.swiftby the input-capture lane, which is also whereCapturedInput,RoutingSnapshotand the escape-hotkey definitions live.input-injectreads it from there. - Configuration: lanes before app-shell take lane-specific config
structs (
SessionConfig,EdgeConfig,ChannelConfig,CaptureConfig); none of them import aSettingstype. app-shell definesSettingsand maps it onto those structs. Changing a behaviour flag at runtime = building a new config struct and handing it to the owner via the Engine. - Status colours and names (
SocketStatus):MWBCore(session-matrix); the UI maps them to colours fromdoc/devdocs/modules/mousewithoutborders.md.
C5. Manual acceptance checkpoints
The user tests; the schedule says when. Each checkpoint means: the
lead runs tools/bundle.sh, hands over dist/mwb.app and a checklist,
and the findings become TODO.md lines for the next lane.
| after lane | checkpoint |
|---|---|
| 7 app-ui | CP1 — menu bar, settings window, keyboard and mouse across two Macs and Mac ↔ Windows, every recovery row in C2 |
| 8 clipboard | CP2 — text/RTF/HTML/image/file both ways, in-band and bulk |
| 9 extras | CP3 — screensaver blocking, fullscreen rule, drawn cursor, corners, relative mode |
| 10 polish | continuous, one checkpoint per batch of fixes |