Plan the macOS port of Mouse Without Borders

Planning documents only: CLAUDE.md rules, README with decisions and
architecture, docs/protocol.md transcribed from the Windows source with
citations, docs/topo.md, TODO.md milestones and the first-wave feature
briefs (bootstrap, packet-codec, cipher-stream), plus tools/guard.sh.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
lucioandClaude Fable 5.1 committed 2026-09-15 23:33:27 -07:00
commit 69c0f7ab53
11 files changed
+1536

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
# bootstrap
## Goal
Turn the empty repository into a building, testing SwiftPM package with
the three-target layering from README D2, the `done` gate script and the
`.app` bundler — and, first of all, establish whether `swift build`
works on this machine at all. Nothing in later lanes can start until
`tools/gates.sh` passes on an empty skeleton.
## Assumptions
- **The SwiftPM breakage is the toolchain, not the manifest.** Measured
2026-09-15: Command Line Tools only (`/Library/Developer/CommandLineTools`,
Swift 6.4, macOS 27.0, no Xcode.app). A trivial manifest fails to link
with `swift-tools-version` 5.9 and 6.0 alike; `libPackageDescription.dylib`
exports `Package.init(… swiftLanguageVersions: [SwiftLanguageMode]? …)`
while the compiled manifest references `[SwiftVersion]?`. Purging
`~/Library/Caches/org.swift.swiftpm` did not help. `swiftc` compiles and
links an AppKit + CryptoKit program fine. The CLT ships Swift Testing
but no `XCTest.framework`.
- **Installing Xcode.app fixes both.** It carries its own SwiftPM and
XCTest. If the user prefers not to, the fallback is a `Makefile`
driving `swiftc` per target plus Swift Testing (`import Testing`) run
via `swift-testing`'s console runner — recorded here only if needed.
- **Minimum macOS 14.** `CGPreflightListenEventAccess` and friends exist
since 10.15; 14 is chosen for Swift 6 concurrency and `NWListener`
maturity, not for a specific API.
- **Bundle id** `com.luciochen.mwb-mac`; `LSUIElement = true` (menu-bar
app, no Dock icon). A self-signed "mwb-mac dev" code-signing
certificate is created once by the user via Keychain Access;
`tools/bundle.sh` reads its name from `MWB_SIGN_IDENTITY` and refuses
to sign ad-hoc unless `MWB_ADHOC=1` is set explicitly (D5).
## Handshakes
### Promised
- `Package.swift` target names `MWBCore`, `MWBNet`, `MWBMac`, `mwb`,
`MWBCoreTests`, `MWBNetTests` are frozen; later lanes add files, not
targets, without a handshake here.
- `tools/gates.sh` exit status is the definition of "done" for every
lane: 0 means format-clean, warning-free, tests green under the guard.
- `MWBCoreTests/LayeringTests.swift` greps `Sources/MWBCore` for
`import (AppKit|CoreGraphics|Network|Cocoa)` and fails on any hit
(topo.md §1 standing promise).
### Consumed
- `tools/guard.sh` (already present; copied from lucisv, header adapted).
## Status / TODO
- [x] Report the toolchain state to the user and wait for Xcode.
Xcode 27.0 (27A266a) installed 2026-09-15; `xcode-select -p` is
`/Applications/Xcode.app/Contents/Developer`.
- [ ] `Package.swift`, empty targets with one placeholder type each,
`Tests/MWBCoreTests/LayeringTests.swift`.
- [ ] `tools/gates.sh`, `.swift-format`.
- [ ] `tools/bundle.sh` + `Resources/Info.plist`.
- [ ] `mwb --version`, `mwb --check-permissions`.
- [ ] `tools/gates.sh` green; record Swift/Xcode versions in the work log.
## Work log
- 2026-09-15 (lead, planning): toolchain probed; findings in Assumptions.
No code written.
- 2026-09-15 (lead): after the user installed Xcode 27.0, the same smoke
package (executable importing AppKit + CryptoKit + CommonCrypto, one
XCTest case, `swift-tools-version:6.0`) built in 8 s and the test
passed under `swift test`. The Assumptions above stay as the record of
why Xcode is a requirement; the fallback Makefile is not needed.
## QA log
## Review
+78
View File
@@ -0,0 +1,78 @@
# cipher-stream
## Goal
The two cryptographic primitives the wire needs, in `MWBCore` with
CommonCrypto and CryptoKit only: the magic-number hash of the security
key (`docs/protocol.md` §2.1) and the per-direction AES-256-CBC stream
with its cleartext salt+IV header and sacrificial first block (§2.2–2.3).
Streaming, allocation-light, and never able to leak key material into a
log. Wire compatibility with the Windows `Encryption.cs` is the whole
point; there is no "better" cipher choice to make here.
## Assumptions
- **CryptoKit cannot do CBC**, so AES goes through `CCCryptorCreate`
with `options = 0` (no padding, CBC) and `CCCryptorUpdate` per chunk.
Zero padding on the Windows side never emits bytes because every write
is a multiple of 16; we therefore do *no* padding at all and treat a
non-multiple-of-16 total as a stream error at close.
- **PBKDF2 goes through `CCKeyDerivationPBKDF`** with
`kCCPRFHmacAlgSHA512`, 100 000 rounds, 32 bytes. Password bytes are
UTF-8 of the key string — `Rfc2898DeriveBytes.Pbkdf2(string, …)` uses
UTF-8 (documented .NET behaviour). Pinned by a Python
`hashlib.pbkdf2_hmac('sha512', …)` vector.
- **The magic hash truncates each UTF-16 code unit to a byte**
(`(byte)st[i]`, `Encryption.cs:184`) and hashes a fixed 32-byte
buffer; keys longer than 32 characters contribute only their first 32.
The `<< 23` overlap is reproduced with `UInt32` wrapping arithmetic in
the same order as the C# expression.
- **Salt and IV are fresh per direction and never reused**; Windows
tests pin uniqueness (`EncryptionTests.cs:59-68`) and so do ours.
- **The sacrificial block is part of the cipher type's contract**: the
writer emits it right after the header, the reader discards the first
16 plaintext bytes. Callers never see it (topo.md handshake 2).
- A wrong key is not detectable here — CBC has no authentication. The
reader returns garbage plaintext and the framing layer's magic/checksum
rejects it, matching Windows (`InvalidKey` after >10 bad packets).
## Handshakes
### Promised (topo.md handshake 2, to control-channel and clipboard-bulk)
```swift
public enum MagicHash { public static func magicNumber(for key: String) -> UInt32 }
public struct CipherWriter {
public init(key: String, rng: RandomBytes = .system) throws // derives, builds header + sacrificial block
public mutating func header() -> Data // 32 bytes cleartext + 16 bytes ciphertext, once
public mutating func write(_ plaintext: Data) throws -> Data // ciphertext for the full blocks now available
}
public struct CipherReader {
public init(key: String)
public mutating func read(_ ciphertext: Data) throws -> Data // consumes header on first bytes; returns plaintext after the sacrificial block
}
```
`RandomBytes` is injectable so tests can pin whole streams.
### Consumed
- Nothing from other lanes.
## Status / TODO
- [ ] `Sources/MWBCore/MagicHash.swift` + `Tests/MWBCoreTests/MagicHashTests.swift`
with `Tests/Fixtures/magic_reference.py` (run once; vector committed).
- [ ] `Sources/MWBCore/CipherStream.swift`: key derivation, writer, reader.
- [ ] `Tests/MWBCoreTests/CipherStreamTests.swift`: PBKDF2 vector;
round-trip; byte-at-a-time reader equals all-at-once; unique
header per instance; wrong key ⇒ garbage without throwing;
descriptions omit secrets.
- [ ] Note in `docs/perf/README.md`: PBKDF2 at 100 000 rounds SHA-512
costs ~N ms per connection open (measure; it is per direction).
## Work log
## QA log
## Review
+84
View File
@@ -0,0 +1,84 @@
# packet-codec
## Goal
The `DATA` packet and its framing as Swift value types in `MWBCore`,
byte-exact with `docs/protocol.md` §1: the 32/64-byte layout, the
`PackageType` numbering and big/small rule, the ASCII machine name, and
the magic + checksum framing that overwrites bytes 1–3. This is the
foundation every other lane reads bytes through; it has no I/O and no
crypto.
## Assumptions
- **Little-endian, natural alignment, no padding**, because the Windows
side marshals an explicit-layout struct (`DATA.cs:135-149`). All
offsets are taken from `docs/protocol.md` §1.1, not recomputed.
- **`Type` is one byte on the wire.** The Swift type stores `PackageType`
as `UInt8` at offset 0; `Framing.open` zeroes bytes 1–3 exactly as
`ProcessReceivedDataEx` does, so a decoded packet's bytes 1–3 are
always 0 and re-sealing is deterministic.
- **The checksum covers bytes 2..31 only, even on 64-byte packets.** We
reproduce this, and pin it with a test that corrupts byte 40 of a
64-byte packet and asserts it is *not* detected — the encryption
layer, not the checksum, is what protects the name half.
- **Unknown type values decode as `.unknown(UInt8)`** rather than
throwing, so a newer Windows build cannot desynchronise our framing.
Size for an unknown value follows the big/small rule (`0x80` bit ⇒ 64,
else 32), which is what Windows would do.
- **Machine names are ASCII, ≤ 32, space-padded.** Non-ASCII input is an
error at construction time; on decode, bytes are trimmed of trailing
spaces and NULs (Windows trims spaces; NUL tolerance costs nothing).
- The magic number is a parameter of `Framing`, not computed here
(that is cipher-stream's `magicNumber(for:)`).
## Handshakes
### Promised (topo.md handshake 1, to session and every later consumer)
```swift
public enum PackageType: RawRepresentable { case hi, hello, … , unknown(UInt8); var rawValue: UInt8; var isBig: Bool }
public struct Packet {
public var type: PackageType; public var id: Int32; public var src: UInt32; public var des: UInt32
public var mouse: MouseData // X, Y, wheelDelta, flags at 16..31
public var keyboard: KeyboardData// dateTime at 16, vk at 24, flags at 28
public var nonce: (UInt32, UInt32, UInt32, UInt32)
public var postAction: UInt32
public var machineName: String? // nil on small packets
public var payload48: Data // bytes 16..63 for clipboard chunks
public init(type:) // zeroed buffer of the right size
public init?(bytes: Data) // 32 or 64 bytes, no framing check
public var bytes: Data // 32 or 64
}
public enum Framing {
public static func seal(_ p: Packet, magic: UInt32) -> Data
public static func open(_ bytes: Data, magic: UInt32) -> Packet // type == .invalid on bad framing
}
```
Exact spelling may change during the lane; the README is updated in the
same change and session codes against the README.
### Consumed
- Nothing. Foundation only.
## Status / TODO
- [ ] `Sources/MWBCore/Packet.swift`: `PackageType` (every value in
protocol §1.2), `isBig`, `Packet` with union views.
- [ ] `Sources/MWBCore/Framing.swift`: `seal` / `open` per §1.3.
- [ ] `Tests/MWBCoreTests/PacketTests.swift`: offsets pinned per field,
big/small over all 256 values, name padding/trim, non-ASCII refused.
- [ ] `Tests/MWBCoreTests/FramingTests.swift`: hand-assembled golden
bytes for Handshake / Mouse / Keyboard / Matrix(slot 4, both
flags) / ClipboardText chunk; round-trip property test; bytes
2..31 corruption detected; byte 40 corruption *not* detected.
- [ ] `docs/protocol.md`: add any offset or rule learned; keep
`[transcribed]` marks until interop-vectors.
## Work log
## QA log
## Review