LAN game discovery previously dropped IPv4 broadcasts at TUN ingress. Carry limited and subnet-directed UDP broadcasts to authenticated, opted-in members of the source network, including destinations reached through mesh relays. Preserve the original IP/UDP bytes and deliver received broadcasts only to the local TUN; never reflood them or expose another pair's plaintext at transit. Build immutable recipient snapshots on address and participation changes. The origin sends one ordinary end-to-end encrypted copy per recipient; the existing fast, bounded-hop transport router remains unchanged. Validate UDP framing, source ownership and destination admission without game-specific port rules. Keep network domains isolated and refuse implicit gateways to physical LANs. The separate broadcast policy/domain layer is the extension point for future authorized subnet exports; physical capture, bridging and LAN deduplication are deliberately not implemented yet. Persist default-on participation independently for each local network. Add join --no-broadcast/--broadcast and network broadcast <id> [on|off], including live updates and authenticated announcements. Joining without a flag preserves the saved choice. Opt-out stops local origination and delivery, while opaque unicast transit for other members keeps working. Migrate SQLite schema 3 to 4 without replacing identities or signed state. Use control ALPN 3 and local IPC protocol 14 for the new announcement/request shapes; update peers and restart running agents together. The data ALPN 4 envelope remains unchanged. No release version bump, tag or push is included. Document agent-owned commits in AGENTS.md: short English subjects, explanatory bodies, scoped staging, honest validation, and repository-local fallback author AB <ab@hexor.cy> only when an effective name/email is missing. Release actions remain the user's responsibility. Validation on Windows: cargo fmt --all -- --check; cargo check --locked --workspace --all-targets; cargo clippy --locked --workspace --all-targets -- -D warnings; release workspace/all-target tests: 313 passed. The two existing SQLite wipe failures (a_wipe_removes_everything_and_the_next_start_is_a_stranger and wiping_twice_is_as_ordinary_as_wiping_once) were explicitly skipped; the public-DHT smoke test and forwarding benchmark remain ignored by default. New coverage exercises real iroh/WireGuard multihop fanout, single delivery, runtime opt-out, unicast replies, domain isolation, malformed input and schema migration. TUNs are in-memory; actual games and OS adapter selection were not tested.
12 KiB
AGENTS.md
Rules for anyone — human or automated — changing this repository. Read this before touching the code. Do not restate the full design here; follow the links.
What this project is
A proof-of-concept agent library for small private mesh networks. A user supplies a network name and one shared secret; agents derive the same network space independently, find each other, prove membership and exchange control messages. Scope and non-scope are in README.md.
There is no central server, no owner, no registration and no majority vote. Design accordingly: a majority is not a root of trust.
Architectural boundaries
Keep these separate. Crossing them is the main thing to review for.
- Control plane vs data plane. The separation is logical, not physical.
The control protocol in
crates/tsunagi/src/proto/knows nothing about packets, and a protocol crate knows nothing about the control protocol; either can be replaced on its own. Both may ride on iroh — refusing to would throw away iroh's NAT traversal and force the data plane to reimplement it. They use different ALPNs and different connections, so a busy or broken data plane cannot disturb control traffic. - One agent, one interface. It belongs to the system level, along with the addresses on it and the decision of whose packet is whose. Several protocols may be carrying traffic at once and none of them owns the thing they carry it for. A protocol is handed a routed packet and hands back a decrypted one; it never creates an interface and never picks an address.
- One state directory, one identity, one live agent — many networks. A network is added to the agent that is already running, never by starting a second one on the same directory. A second agent is a second identity and is isolated: its own directories, its own interface, its own runtime. Do not add anything that lets two agents share a directory, and do not make a network's lifetime depend on the process that happened to start it.
- A protocol is a separate crate with its own version. The version peers compare is the wire version, never the software version: two peers on different releases work together for as long as the bytes between them have not changed. Nothing negotiated may be derived from anything that moves with a release.
- A protocol never sees the network secret. It proves who is at the other end of a tunnel; proving membership of a network stays in the core, which is why the authenticated transport does too.
- A relay carries bytes, never packets. A datagram passed on for two other peers goes link in, link out: it is never written to the middle's interface or decrypted there. Multihop routing has a bounded hop limit; equal-cost next hops are chosen per flow. Reachability is first-hand and volatile: each authenticated member advertises its own protocol-specific links. Build routing tables on topology changes, never per packet. Transit must not acquire a routing mutex or wait for a protocol/TUN reader.
- Broadcast fanout belongs at local IP ingress. Participation is local to each network, enabled by default, persisted and advertised to authenticated peers. Resolve one source domain and target only its willing members. Remote delivery never triggers another fanout. Physical LAN exports must extend explicit ingress and source/destination admission policies; never bypass ownership checks or add broadcast flooding to encrypted transit.
- Plugins never learn reachability. An
IpPluginis handed aPacketLinkper peer and moves datagrams over it. Addresses, hole punching and relays belong tocrates/tsunagi/src/dataplane/transport/. A plugin announcement says who, never where. - The core never parses a plugin payload. See
crates/tsunagi/src/dataplane/mod.rs. Onlycrates/tsunagi-wg-quic/src/announcement.rsinterpretswg-quicpayloads, and only after bounding every field. A data plane failure must never stop the control plane. - Derived, not claimed. A peer's overlay address is derived from its public key. Outbound packets are routed to the owner of the destination address; inbound packets are dropped unless their source is the address derived for the peer that sent them. Never trust an address a peer announces.
- Signed state is the only durable agreement. A fact that must survive a
participant being away goes in
crates/tsunagi/src/state/as a record signed by its author, never in a session. Merging is deterministic, an older version never rolls back a newer one, and absence from a snapshot is not deletion. Never add a vote or a quorum: a majority is not a trust root here, and it would stall with one peer online. - A record and the author's own version counter commit together, in one transaction, before the record is announced.
- Plugins own their system objects. A plugin creates and removes its own interface and nothing else. Never touch routing, DNS or firewall settings.
- Device identity vs network identity. The iroh endpoint id is the device's
public key.
NetworkIdis derived from name + secret only. Never conflate them, and never let one change the other. - Discovery vs authentication. Discovery returns unverified candidates. It never authenticates, never carries control messages and never mutates agent state. Membership is decided only by the handshake.
- Mandatory state vs disposable cache.
state.sqliteis never silently reset: damage is a hard error.cache.sqlitemay be deleted at any time and is recreated. A stale cache must never bypass identity or network authentication. - Candidate vs observed address vs verified path. Report these as three
different things. A missing value is
None, never invented.
Rules that are not negotiable
- Tests come first. Every substantial change ships with tests for the normal path and the important failures. See docs/testing.md. Do not add tests for getters or to move a coverage number.
- Never change
IDENTITY_SCHEME, the derivation labels, or the transcript encoding incrates/tsunagi/src/identity/network.rsandcrates/tsunagi/src/proto/handshake.rswithout treating it as an incompatible protocol change. Bumping the crate version or the control protocol version must not change an existingNetworkId. - Secrets never leak. Not into logs, not into
Debug, not into status output, not into anything exported to the network.NetworkSecretand derived keys redact themselves and zeroize; keep it that way. - No panics on untrusted input. No
unwrap,expectorpanicon anything that came off the network. Clippy enforces this at warn level in the library; tests opt out explicitly at the top of each file. - Bounds before allocation. Frame lengths are checked against
Limits::max_frame_lenbefore a buffer is allocated. Every string, list and queue has a limit incrates/tsunagi/src/config.rs. - Failure is contained. A bad signature, wrong secret, malformed packet or unknown version rejects one message or one session. It never stops another network and never stops the agent. There is no irreversible global error flag. A data plane failure must never stop the control plane.
- The library owns no globals. No global tokio runtime, no global tracing
subscriber, no signal handlers, no
fork, noprocess::exit, no global mutable state. Several agents must run in one test process. - SQLite never blocks the executor. All database work goes through
spawn_blocking. Never hold a transaction or a database lock across a networkawait. When signed records land, writing the record and bumping the author's own counter must be one transaction, committed before publishing to the network. - Shutdown is bounded. A peer that stops reading must not be able to hold up shutdown. Wind tasks down with a grace period and then abort.
- Nothing from a remote announcement becomes a shell command, a filesystem path or an OS setting. The agent never touches interfaces or OS settings it did not create.
Where things live
| path | responsibility |
|---|---|
crates/tsunagi/src/identity/ |
device identity; deterministic network space identity and derived keys |
crates/tsunagi/src/storage/ |
state.sqlite, cache.sqlite, directory ownership lock |
crates/tsunagi/src/discovery.rs |
candidate sources; test and static backends |
crates/tsunagi/src/proto/ |
framing, message formats, membership handshake |
crates/tsunagi/src/net.rs |
iroh endpoint adapter and observability snapshots |
crates/tsunagi/src/agent/ |
agent lifecycle, per-network runtimes, sessions, events, status |
crates/tsunagi/src/state/ |
signed records that outlive a session, their merge rules and address allocation |
crates/tsunagi/src/overlay/ |
the one interface an agent owns: provisioning, the TUN, routing, source checks |
crates/tsunagi/src/dataplane/ |
the protocol contract and the authenticated packet transport |
crates/tsunagi/src/dns/ |
the DNS view of a network: zone, server, resolver publication |
crates/tsunagi-wg-quic/ |
the wg-quic protocol: its keys, its announcement, its tunnels |
crates/tsunagi-cli/ |
the command line agent; the only place that owns a runtime, a logger and signals |
tests/ |
integration tests; tests/common/ is the shared harness |
Add abstractions only at real substitution or testing boundaries. Do not add a trait per struct.
The system level and its plugins are separate crates, so that boundary is
checked by the compiler and not by discipline: a plugin can reach only what
tsunagi makes public, and carries its own version. Everything else stays
one crate with clear modules — do not split further without a reason of that
kind.
Testing rules
- Use real iroh endpoints on loopback, real handshakes, real SQLite in temporary directories, and independent agent instances.
- Discovery may be substituted. iroh, authentication, message passing and persistent storage may not be.
- No arbitrary multi-second
sleepas the primary synchronisation. Wait for a specific event or condition under one overall deadline (wait_event,wait_until).settle()exists only for asserting that something did not happen. - The default suite must pass with no internet, no DHT, no public relay, no administrator rights and no changes to OS network settings. Anything needing the internet or root stays out of the default set.
- The WireGuard packet interface may be substituted (
MemoryTunFactory). Its key handling, announcements, derived addressing, the WireGuard protocol itself and address-ownership enforcement may not — the default suite runs real handshakes and real encryption. - Running several library instances in one process is not a test of several system processes; do not describe it as one.
Commit every completed change
Agents must create a Git commit before finishing any task that changes this repository. Do not leave the user to make the commit. Inspect recent commit messages and follow their style: a short English subject stating the change, then a useful body explaining the problem, final behavior, architectural decisions, compatibility/migration effects, and checks actually performed. Write enough context for a future maintainer or LLM to understand why the code exists. Record failed or skipped checks honestly; never claim unrun tests.
Before committing, inspect the diff and stage only this task's changes. Preserve
unrelated user changes and never commit secrets or generated build artifacts.
If Git has no effective author name or email, set only the missing repository-
local values to AB and ab@hexor.cy. Do not override an existing identity or
change global Git configuration. Report the resulting commit hash.
Version bumps, release tags, and pushing commits/tags belong to the user unless they explicitly ask the agent to do them. Read-only tasks need no empty commit.
Required validation
cargo fmt --all -- --check
cargo clippy --locked --workspace --all-targets -- -D warnings
cargo test --locked --workspace --all-targets
CI runs these on Linux, macOS and Windows. A CI config existing is not evidence that tests ran on every OS — say what you actually ran.
Check the real API of the iroh version in Cargo.lock before using it. Do not
invent methods from memory.