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]>
198 lines
10 KiB
Markdown
198 lines
10 KiB
Markdown
# CLAUDE.md — rules for all agents working on mwb-mac
|
||
|
||
mwb-mac is a macOS port of PowerToys "Mouse Without Borders" (MWB): one
|
||
keyboard and mouse shared across up to four machines on a LAN, with
|
||
clipboard sharing, that **interoperates on the wire with the unmodified
|
||
Windows MWB**. Read `README.md` for architecture, `docs/topo.md` for the
|
||
dependency map and `TODO.md` for the backlog before doing anything. The
|
||
reference implementation is checked out at
|
||
`/Users/guoliangchen/repo/PowerToys/src/modules/MouseWithoutBorders/`
|
||
(commit `3104d5c`, tag v0.101.2572.0) — read it, cite it, never edit it.
|
||
|
||
## Hard invariants (never break these)
|
||
|
||
1. **Wire compatibility is a contract, not a goal.** Packet layout, the
|
||
package-type numbering, the key derivation, the cipher mode and the
|
||
handshake are dictated by the Windows source (`docs/protocol.md`
|
||
is our transcription, with `file:line` citations). Any difference is
|
||
a bug on our side unless the Windows source changed. Every codec
|
||
change ships with golden-byte tests whose expected bytes were
|
||
produced by the Windows algorithm, not by our own encoder.
|
||
2. **Never act on an unauthenticated packet.** No input is injected, no
|
||
clipboard is written, no file is created from any packet that did
|
||
not decrypt and validate under the configured security key. There
|
||
is no "no key" mode. The key and decrypted keystrokes are never
|
||
logged, at any log level.
|
||
3. **The local machine is always recoverable.** Whatever happens on the
|
||
network, the user can regain their own keyboard and mouse: the
|
||
escape hotkey works while control is remote, control returns local
|
||
on any disconnect or on key-tap loss, and a
|
||
`kCGEventTapDisabledByTimeout` / `ByUserInput` tap is re-enabled
|
||
immediately. A build that can leave a Mac with a dead keyboard does
|
||
not ship.
|
||
4. **The event-tap callback never blocks.** No network, disk, locking on
|
||
contended locks, or allocation-heavy work inside the
|
||
`CGEventTap` callback; it classifies, enqueues and returns. Budget is
|
||
measured (`docs/perf/README.md`), not asserted.
|
||
5. **Synthetic input is opt-in.** Any test or tool that posts a
|
||
`CGEvent`, writes the real pasteboard or moves the real cursor runs
|
||
only with `MWB_ALLOW_INPUT_TESTS=1` set explicitly and only inside
|
||
the loopback harness. Never set that variable in CI config, a
|
||
script default or a `Makefile` target. This machine is the user's
|
||
daily driver: an agent test that hijacks the mouse is a P0 finding.
|
||
|
||
## Simplicity first
|
||
|
||
- Prefer the boring, obvious implementation. If you're building an
|
||
abstraction for a second user that doesn't exist yet, stop.
|
||
- Apple frameworks first (Foundation, Network, AppKit, CoreGraphics,
|
||
CryptoKit/CommonCrypto). Adding a Swift package dependency requires
|
||
a written justification in the feature README.
|
||
- Small functions, descriptive names, shallow nesting. Code should be
|
||
readable top-to-bottom without jumping around.
|
||
- Match the style of the surrounding code (comment density, naming,
|
||
module layout).
|
||
|
||
## File and function size
|
||
|
||
Files and functions are sized for a human reader, not a compiler.
|
||
|
||
- **Target 300–600 production lines per file.** At 800, consider
|
||
splitting. Above 1,200, an explicit reason not to split goes in the
|
||
feature README. Test lines do not count.
|
||
- **Functions: 10–50 lines.** Investigate anything over 80; anything
|
||
over 150 needs a reason in a comment (a `switch` over `PackageType`
|
||
is the usual one).
|
||
- **The real test is one sentence.** A human should be able to say what
|
||
a file is responsible for in one sentence and predict which parts a
|
||
change touches. Several "and" clauses mean the file wants to be
|
||
several modules. The Windows source fails this test in
|
||
`Common.cs`, `SocketStuff.cs` and `MachineStuff.cs`; do not port
|
||
their shape, port their behaviour.
|
||
- A split is a **pure move**: public names stay the same, behaviour
|
||
stays byte-identical on the wire, the test count does not drop.
|
||
|
||
## Swift standards
|
||
|
||
- Swift 6 language mode, strict concurrency on. `swift-format lint
|
||
--strict` clean, `swift build -Xswiftc -warnings-as-errors` clean,
|
||
`swift test` green — all three before declaring any work finished.
|
||
- No force-unwrap (`!`), `try!`, `fatalError` or `precondition` in
|
||
production code paths; return `Result`/throw. Tests and `main.swift`
|
||
argument handling may use them.
|
||
- Public items get a one-line doc comment. Doc comments say what/why,
|
||
not how.
|
||
- Layering is enforced by SwiftPM targets, not by discipline: the
|
||
`MWBCore` target imports Foundation only (no AppKit, no CoreGraphics,
|
||
no Network) so the protocol, crypto, matrix and settings logic is
|
||
testable without a display or a network. Platform code lives in
|
||
`MWBMac`; the executable `mwb` wires them together.
|
||
- C APIs (`CGEventTap`, `CommonCrypto`) are wrapped once, in one file
|
||
each, behind a Swift type; nothing else touches the raw API.
|
||
|
||
## Document-based development
|
||
|
||
Every feature lives in `docs/features/<feature-name>/README.md`. That
|
||
README is the single source of truth for the feature and must contain
|
||
these sections:
|
||
|
||
```markdown
|
||
# <feature name>
|
||
## Goal — one paragraph, what and why
|
||
## Assumptions — everything you assumed that isn't enforced by code
|
||
## Handshakes — interfaces/contracts with other modules: what you
|
||
consume from them, what you promise to them
|
||
## Status / TODO — checklist; keep current as you work
|
||
## Work log — dated entries by the dev agent: what was done, key
|
||
decisions, deviations from plan
|
||
## QA log — dated entries by the QA agent: what was verified,
|
||
how, and findings
|
||
## Review — reserved for the lead; do not write here
|
||
```
|
||
|
||
Rules:
|
||
|
||
- **Dev agents**: create/update the feature README *as you work*, not at
|
||
the end. Finished TODOs get checked off in the same README. If you
|
||
change a handshake another module depends on, say so explicitly in
|
||
Handshakes and in the work log. Every fact taken from the Windows
|
||
source is cited as `App/<path>.cs:<line>`.
|
||
- **QA agents**: you verify, you don't redesign. Run the checks, try to
|
||
break the feature, record everything in the QA log. File concrete
|
||
findings (file:line, reproduction) — not opinions. Small unambiguous
|
||
fixes (typos, missing test assertion) you may fix directly and note it.
|
||
- **Review section** is written only by the lead agent after examining
|
||
the work. Respond to review comments by fixing and logging in the work
|
||
log, not by editing the Review section.
|
||
- `docs/protocol.md` is the one place the wire format is written down.
|
||
A feature that learns something new about the protocol updates
|
||
`docs/protocol.md` in the same change, with the citation.
|
||
|
||
## Workflow
|
||
|
||
- Do **not** run `git commit` or `git push` — the lead reviews and
|
||
commits all work.
|
||
- Do not touch files outside your feature's stated scope. If you need a
|
||
change in another module, write it as a request in your README's
|
||
Handshakes section and stop.
|
||
- New behavior needs tests in the same change. Every codec or crypto
|
||
change must keep the golden-byte suite green
|
||
(`swift test --filter MWBCoreTests`).
|
||
- Update `TODO.md`: check items your feature completes (leave the item
|
||
text intact).
|
||
- Never grant, request or script macOS permissions (Accessibility,
|
||
Input Monitoring, Screen Recording) on the agent's own initiative.
|
||
Report what is missing; the user grants it in System Settings.
|
||
|
||
## Runaway guard (never run a suite or sweep bare)
|
||
|
||
macOS enforces no memory cap, so the cap is ours:
|
||
`tools/guard.sh [-m GB] [-t SECONDS] -- <command>` kills a command's
|
||
process tree when its resident set or wall clock exceeds the limits
|
||
(defaults 8 GB / 30 min).
|
||
|
||
- **Every `swift test`, every fuzzer, every soak test and every
|
||
two-process loopback run goes through `tools/guard.sh`** — dev, QA
|
||
and lead alike, foreground or background.
|
||
- A guard kill is a finding: report the command and the limit it hit;
|
||
do not raise the limit to make it pass.
|
||
|
||
## Parallel lanes (the 2-agent loop)
|
||
|
||
Up to **two dev/QA lanes may run concurrently** when — and only when —
|
||
their features do not collide in `docs/topo.md`: no shared edge between
|
||
them (no `──▶`, no `◀──▶`, and neither touching the same open handshake
|
||
in §2.9), and disjoint file sets. `docs/topo.md` is the authority; if the
|
||
collision question is not answerable from it, the features are not
|
||
proven independent and run serially. Rules for a parallel wave:
|
||
|
||
- **The lead plans the lanes** before anything is dispatched: one brief
|
||
per lane with an explicit file-ownership list. That list is the merge
|
||
contract — a lane touches nothing outside it. A needed change to a
|
||
file the other lane owns (or to any unlisted file) is a Handshake
|
||
request in the lane's README plus a flag in its report, never a
|
||
silent edit; the lead rules on it at merge.
|
||
- **Each lane works in its own git worktree** branched from the same
|
||
trunk commit. Never two agents in one tree. Trunk stays untouched
|
||
while lanes are in flight; the lead merges each lane back only after
|
||
that lane's QA verdict and lead review, and re-runs the full gates on
|
||
the merged tree before pushing.
|
||
- **Roles per lane are unchanged**: one dev (Opus), then one QA
|
||
(Sonnet) in the same worktree, then the lead's review — so at most
|
||
2 devs + 2 QA agents exist at once, and a lane's QA starts only when
|
||
its own dev is done. A QA rework loops inside its lane without
|
||
blocking the other lane.
|
||
- **Tracking lives in `TODO.md`**: the lead writes the wave's section
|
||
(one subsection per lane, each item naming its files) on trunk before
|
||
branching; each lane ticks only its own lines. Shared-by-nature files
|
||
(`TODO.md`, `docs/protocol.md`, `docs/perf/README.md`) may be appended
|
||
to by both lanes in their own sections — the lead resolves the
|
||
trivially-parallel merge; anything worse than trivially-parallel means
|
||
the lanes were not actually independent, which is a planning failure
|
||
to record.
|
||
- After merging, the lead updates `docs/topo.md` for any edge a lane
|
||
added or amended, so the next wave plans against reality.
|
||
- **Only one lane may hold the real input devices.** A lane whose QA
|
||
needs the loopback harness with `MWB_ALLOW_INPUT_TESTS=1` runs that
|
||
step serially, with the user warned in the report.
|