MindAttic.Orchestrator/MESH ARCHITECTURE
Mesh architecture: every machine a peer
Design record for epic M.ORCH-T2540, written under M.ORCH-T2543 (2026-12-02). The owner signs it off by approving the code tickets under M.ORCH-T2540. Nothing here is built until then.
Summary: every decision on one screen
- Every machine is a full Orchestrator. Each runs its own daemon, CLIs, gateway and dashboard, and can always work on its own. Joining the mesh adds abilities and takes none away. The rig never gives up anything.
- One machine is the authority. It is the only writer of tickets, claims, cluster pacing and (later) placement. The first machine in the mesh is the authority, which means the rig. Every other machine is a worker and a viewer and asks the authority through the gateway. Nothing is shared between machines by copying databases.
- Claims are leases. The authority grants a ticket to exactly one (machine, CLI) with a lease that the claimant's daemon keeps renewing. If the lease lapses, the ticket comes back only after a grace period. Every close carries the lease's generation number, so a stale holder can never close or re-claim over a newer one.
- No automatic failover with two machines. Two machines cannot hold a majority vote, so automatic failover would risk both of them acting as the authority at once. If the rig is gone, the laptop keeps its running CLIs and hand-driven work but takes no new claims. The owner can move the authority with one fenced command. Automatic failover arrives only once three or more machines can vote.
- Peers trust each other through a peer key, not the owner key. Each peer gets its own revocable key. The gateway seals it and uses it to mint renewing tokens, the same way
machine connecthandles the owner key today, so the link never lapses. - Joining takes a one-time code. An existing peer shows a short code (
orchestrator mesh invite). The new machine runsorchestrator mesh join <peer> <code>. The gateway also checks through Tailscale whois that the caller belongs to the owner's Tailscale user. Being on the tailnet is not enough to join. - Interact works everywhere, and one machine holds the keyboard. Every wall surface (tile, phone card, pop-out) can type. Whoever typed last holds the keyboard. Another machine takes it once the holder has been idle for a short gap, or at once with "Take keyboard". Every viewer sees who is typing. The owner's own machines need no consent prompts. Member consent rules are unchanged.
- Pacing is per login across the whole cluster. Each machine publishes each login's burn and running subagents to the authority. The authority computes one pace per login and splits the subagent allowance between the machines using that login. A machine that cannot reach the authority falls back to a stricter local pace. No login passes its 5-hour or weekly cutoff, wherever its CLIs run.
- Placement, the end state, scores (machine × signed-in login) pairs. The score weighs login headroom (unused window that would otherwise expire is spent first), the machine's free RAM against one CLI's footprint, and repo readiness. The authority places work until every login reaches its cutoffs, and never past them. Milestone 1 does not build this: each machine's own /allocate draws from the authority, and pacing caps it.
- Git stays the arbiter for code. Each machine has its own checkouts on main and pushes with fetch, merge, push. The commit gate stays per checkout. Exactly-once claims keep two machines off one ticket. Only the authority commits
data/orchestrator.db. - Version skew is guarded. The mesh protocol carries a version number. A peer on another protocol version can still watch and type, but cannot claim or pace until it is updated, and every dashboard says so.
- Migration is opt-in and leaves the old roles alone. Standalone acts as a mesh of one. The host and client roles and
machine connectkeep working for the VM case. Mesh peers appear as gateway remotes, so the Team Wall merges them without new plumbing. - Milestone 1 is rig plus laptop working completely (definition in the Build plan). Failover and the placement scorer come in milestone 2.
a. Topology and authority
The decision. The mesh has one authority and N peers. Every machine, the authority included, is a worker (runs CLIs) and a viewer (a dashboard showing every machine's CLIs). The authority is the single writer of:
- the ticket database (MindAttic.Tickets) and the Orchestrator's store of record (MindAttic.Orchestrator). Both are on the rig's full SQL Server instance today (docs/DATA_STORES.md:9, :32);
- claims (assignments): today
NamedClaim.Claimdoes its check and write as one step under the local assignments lock (src/MindAttic.Orchestrator.Core/Scheduling/NamedClaim.cs:24-28). In the mesh that one step runs on the authority, behind a gateway endpoint; - the membership record (who is in the mesh, their pins and key fingerprints, the authority's epoch);
- cluster pacing (d) and placement (e).
Each peer keeps writing its own machine-local state, just as it does today: its CLIs, TermHost sessions, rl-status.json per Claude home (src/MindAttic.Orchestrator.Core/Sessions/AccountDirs.cs:25) and machine.json. Peers reach the authority only through the gateway. The /tickets endpoint already runs the tickets CLI on the machine that "owns the ticket database" for the owner's own client (src/MindAttic.Orchestrator.Gateway/GatewayApp.cs:209-224). The mesh widens that to peer keys and adds claim, lease, pacing and membership endpoints next to it.
The founder of a mesh, the machine where the first mesh invite runs, becomes the authority at epoch 1. For milestone 1 that is the rig.
Why. The codebase already has single-writer semantics: the host role "is the one writer" and the client "never" writes the committed databases (docs/REMOTE_HOST.md:12-16). The claim check-and-write is already atomic under one lock. Moving that lock to one machine keeps every existing guarantee and reuses an endpoint shape (/tickets) that is already authenticated and audited. Exactly-once claims then follow from one serialised writer, which is the simplest thing that can be proven correct.
Rejected alternatives.
- Fully peer-to-peer with distributed locks. Each claim would need consensus across machines that sleep and roam. Two-node lock protocols cannot tell "peer asleep" from "network split", so they either block or double-claim. Raft or Paxos inside the Orchestrator is a large, permanent cost for a mesh of two to four machines.
- Each machine claims locally and syncs databases. Two machines would claim one ticket during the sync gap, and merging two SQL stores is a problem class of its own. This is the bug the epic exists to prevent.
- SQL Server opened to the tailnet so peers write it directly. Peers could then claim without the one lock, and a database port would be exposed. It also ties every peer to SQL Server, which blocks a future Linux peer (M.ORCH-T2538).
- Keep host/client. It turns the second machine into a viewer and stops its daemon (src/MindAttic.Orchestrator.Core/Setup/MachineSetup.cs:56-63, :453), which is the opposite of the goal.
b. Identity, trust and auth over Tailscale
The decision.
- A peer key of its own. At admission each side mints a random peer key for the other. The receiving machine stores it sealed for its Windows user, as
GatewayRemote.SealedOwnerKeyis today (src/MindAttic.Orchestrator.Core/Settings/GatewayRemote.cs:34).GatewayClient.MintIfDueAsyncgains a third key kind next to the owner and member keys (src/MindAttic.Orchestrator.Core/Sessions/GatewayClient.cs:140-160). It mints a 60-minute interact token whenever fewer than 10 minutes remain, so the link renews itself and never lapses. A peer token's subject is the owner, with scopepeer. That grants list, watch, interact and start-CLI on that machine, plus the mesh endpoints (claims, pacing, membership, ticket forwarding to the authority). It does not grant/branches(the host model's work-branch queue, GatewayApp.cs:240) or the owner-key-only administration. - Admission by one-time code plus Tailscale identity.
orchestrator mesh inviteon any current peer prints a code: single use, 128 bits written as words, lasting 10 minutes (a Vault setting).orchestrator mesh join <peer> <code>on the new machine connects over Tailscale. The inviting gateway asks the local Tailscale daemon's whois API who the caller is, and accepts only a node owned by the same Tailscale user as itself. Then both sides exchange certificate pins and peer keys over that channel. The code authenticates the pins, so neither side trusts on first use. The new peer is recorded with the authority and announced to every peer. - Gateway exposure. On a mesh peer the gateway binds to the machine's Tailscale 100.x address (plus loopback), never to every network. The default for non-mesh machines stays
127.0.0.1(src/MindAttic.Orchestrator.Core/Settings/GatewaySettings.cs:14). - Revocation.
orchestrator mesh remove <peer>on any peer: the authority drops the peer and every peer forgets its key, so a lost laptop is cut off without rotating the rig's owner key. - Claude logins stay per machine. They remain DPAPI-sealed per Windows user per machine and signed in with
orchestrator login(docs/REMOTE_HOST.md:165). The mesh never moves credentials. M.ORCH-T2562 confirms this.
Why. A 60-minute --token remote (src/MindAttic.Orchestrator.Gateway/GatewayOptions.cs:31) would drop every link within an hour. The self-renewing pattern already exists and is proven for the owner and member keys. A key per peer makes revocation local and leaves the owner key's power where it is.
Rejected alternatives.
- Share the owner key with peers. Copies of the owner key would live on every laptop. It unlocks
/tickets,/branchesand admin on every machine, and revoking one lost machine would mean rotating it everywhere. - Tailnet membership alone. Anything ever added to the tailnet (a phone, a shared node, a compromised device) could drive CLIs that hold commit rights. That would be an open door.
- An invite link or file. A link has to be carried between machines, usually over channels less private than the screen. A code read off one screen and typed into the other covers the same need.
- Tailscale whois as the only check. Whois tells who owns a node, not that the owner meant to admit it. Used as a second factor it costs nothing.
c. Interact semantics
The decision.
- Every surface can type. The inline tile stream and the phone card stop being watch-only (src/MindAttic.Orchestrator.Wpf/TeamWallWindow.cs:191 connects with
watch: true, :427 pops out withinteract: false). Each opens with interact when the gateway lists the session withaccess=interact, which every session does for a peer token. - One keyboard per CLI. TermHost tracks a keyboard holder per session. A viewer's keystrokes are delivered when that viewer holds the keyboard, or when the holder has been idle for
InputHandoffSeconds(a Vault setting, default 2). The keyboard then passes to the new typist. Keystrokes from a non-holder inside that window are dropped. Every viewer's frame shows "Typing: RIG" (the holder's machine name). "Take keyboard" takes it at once, and the previous holder sees "Keyboard taken by LAPTOP". Resize follows the keyboard holder. - No consent between the owner's own peers. HubPolicy and TermHubPolicy govern org members' CLIs and Windows accounts on a host (src/MindAttic.Orchestrator.Core/Setup/HubPolicy.cs:8, src/MindAttic.Orchestrator.Term/TermHubPolicy.cs:5). A peer token's subject is the owner, so those rules never fire for it. Member grants and consents work unchanged on every peer.
Why. It is one person on several screens, so the hazard is interleaved keystrokes, not someone else typing. Last-typist-wins with an idle gap matches how the owner moves between machines, and nobody has to remember a "take control" step.
Rejected alternatives.
- Free-for-all (every viewer's input delivered). Two half-typed lines interleave into one prompt sent to an agent with commit rights.
- A strict lock released by hand. The owner walks away from the rig and finds the laptop locked out.
- Watch-only tiles plus pop-out to type. This is today's behaviour, and the owner asked for the opposite.
d. Cluster-wide pacing
What is per machine today. The level of a window is already account-wide. ClaudeMeterProbe asks Claude's own usage endpoint, the one /usage shows (src/MindAttic.Orchestrator.Core/Budget/ClaudeMeterProbe.cs, class summary), so spending on another machine shows up there. Three things are only local:
- the burn rate estimated from this machine's transcripts;
- the subagent allowance computed from this machine's subagent marks (src/MindAttic.Orchestrator.Core/Budget/AccountPacer.cs:18-22);
- the time between meter readings.
With one login on two machines, each machine grants the full allowance and predicts from half the burn. Their combined spend can run past the cutoff between readings.
The decision.
- Every peer's daemon publishes, for each login it runs, the newest meter reading with its timestamp, its tokens per minute and its running subagents. It does this on its existing pace tick, and it is M.ORCH-T2570.
- The authority computes one pace per login (M.ORCH-T2571) with the existing math:
Pacerand, for the Ultra login that has no 5-hour window,Pacer.WeeklyPace(src/MindAttic.Orchestrator.Core/Budget/Pacer.cs:518, called at AccountPacer.cs:275). The inputs are the newest meter reading from any machine and the sum of burn and subagents across machines. It then splits the subagent allowance between the machines running that login, in proportion to their demand, and returns each machine its share. - Each peer writes its share into that login's
rl-status.json, so the pace gates and hooks are unchanged. - Cutoffs stay exactly as configured: 98% of the 5-hour window and 90% of the week by default, per member through
/org limit. They are enforced on the cluster sum. - If the authority is unreachable, a peer paces each login locally with the allowance divided by the number of machines last seen running that login, and stops new work at the cutoff minus
MeshOfflinePaceMarginPercent(a Vault setting, default 3 points). It does this until it has heard from the authority again withinMeshPaceStaleSeconds(a Vault setting). - The dashboard's usage shows each login's cluster total and every machine's share of it (M.ORCH-T2572).
Why. It reuses the pacer whole and changes only its inputs. Because the meter is already account-wide, the only new data is burn and subagent counts, which are small and summable. The offline fallback errs toward stopping early, never toward passing a cutoff.
Rejected alternatives.
- Each machine paces alone with a fixed split (for example 50/50). That wastes the window whenever one machine is idle.
- Pin each login to one machine. That defeats "any CLI on any login on any machine".
- Each machine reads the meter more often. It narrows the overshoot but cannot close it. Burn between readings is still invisible to the other machine, and the endpoint is not built for hammering.
e. Placement and scheduling
The decision (end state). The authority places each approved, unclaimed ticket on the best (machine, login) pair. Candidate pairs are the logins signed in on that machine. A pair is eligible only when:
- the login's cluster pace (d) allows new work,
- the machine's free RAM, after the MemoryGuard's own floor (src/MindAttic.Orchestrator.Core/Activity/MemoryGuard.cs:69), covers one more CLI tree at
CliFootprintGb(a Vault setting, default 1.3; measured at about 1.24 GB on 2026-11-17), - the machine is on the current protocol version, and
- the machine has the repo's checkout, or can clone it.
Eligible pairs are scored, most important first:
- login headroom that expires soonest. Unused 5-hour or weekly headroom that resets sooner is spent first ("use it or lose it");
- machine RAM and CPU headroom;
- repo readiness, which prefers a machine already working in that repo's checkout.
The authority then tells the chosen machine to start the CLI on that login and claim the ticket (M.ORCH-T2573 to M.ORCH-T2575). It keeps placing until every login sits at its cutoffs or no machine has room (M.ORCH-T2576). Logins and limits disappear from the owner's view. He approves tickets and the cluster spends what it has, never more.
Milestone 1 builds none of the scorer. Each machine runs /allocate as today. Every claim goes through the authority, so it is exactly-once, and cluster pacing caps every login. That already makes the rig and the laptop "code tickets until thresholds".
Why. Placement needs claims, leases, cluster pacing and machine headroom to be right first. Building the scorer on milestone 1's foundations puts every rule in one place, the authority.
Rejected alternatives.
- Work-stealing, where each machine grabs when free. That is milestone 1's behaviour and it is correct, but it cannot prefer an expiring window or keep RAM-poor machines (the 16 GB laptop holds about 6 CLIs) from overcommitting.
- A login pinned per machine. See d.
f. Failure modes
The decision.
- Claim leases. A claim carries machine, CLI, session, login and a lease generation. The claimant's daemon renews it every
ClaimLeaseMinutes / 3(ClaimLeaseMinutesis a Vault setting, default 10). When renewals stop, the claim becomes orphaned, and every dashboard shows it as "LAPTOP unreachable since 14:02". The ticket returns to the queue only after a furtherClaimOrphanGraceMinutes(a Vault setting, default 30). A sleeping laptop often still holds uncommitted work in its own checkout, and a short grace would double the work. - The sleeper returns. If its lease is still the newest generation, it renews and carries on. If the ticket was re-granted meanwhile, its daemon tells that CLI "this ticket moved to RIG; commit or drop your work". Its close is refused, because a stale generation is fenced, and it can still push commits. Git sorts out any overlap at merge.
- A CLI dies mid-ticket. Same as today (NamedClaim.cs, class summary): the claim is released when the CLI is gone. The difference is that the claimant's daemon reports it to the authority.
- Lost peer on every dashboard (M.ORCH-T2567). Its tiles grey out, they are labelled with the last time it was seen, and they come back without a manual refresh.
- The authority goes down. Peers keep every running CLI, keep interact among the reachable machines, keep committing and pushing, and pace on the offline fallback (d). They grant no new claims.
ticketsreads are served from a read-only snapshot.orchestrator mesh take-authorityon a surviving peer moves the authority. It needs the owner at that machine, it bumps the epoch, and a returning old authority sees the higher epoch and demotes itself, so two authorities can never both act. The new authority needs the store of record. In milestone 2 that means the ticket and Orchestrator databases restored from the old authority's latest replica (M.ORCH-T2566). Automatic failover is enabled only when three or more peers can vote, and then by majority. - Version skew (M.ORCH-T2568).
/healthalready returns the build version (GatewayApp.cs:156). The mesh adds an integer protocol version. A peer on a different protocol keeps watch and interact but is refused claims, pacing and placement, and every dashboard shows "LAPTOP needs update". - Many machines on main (M.ORCH-T2569). Each machine works in its own checkouts on main, with no branches and no worktrees. The commit gate stays per checkout (mindattic-agent-standard/shared/hooks/commit-gate.py, header): it guards agents sharing one checkout, and no checkout is shared across machines. Across machines, exactly-once claims keep two agents off one ticket, and the existing landing rule (fetch, merge, push, retry when rejected) handles different tickets in one repo. The gate's "close only from the claim's holder" check asks the authority instead of the local store.
data/orchestrator.db, which git carries between machines (docs/REMOTE_HOST.md:8-9), is committed only by the authority. Peers never stage it, so the binary file cannot conflict.
Why. Every failure falls back to doing less, never to doing something twice. With two machines, automatic election is not safe (see "No automatic failover" in the summary). An explicit, fenced handover is.
Rejected alternatives.
- A short lease with immediate requeue. Every laptop lid-close would start the ticket again on the rig.
- Automatic failover between two machines. Split brain gives two authorities and double claims.
- Peers claim locally while cut off and reconcile later. Reconciliation discovers the double claim only after both machines have done the work.
g. Migration
The decision.
- Standalone is a mesh of one. Nothing changes on a machine until
mesh inviteormesh joinruns there. The rig becomes the authority by founding the mesh. Its daemon, CLIs and dashboard keep running throughout, so it never loses local capability. - The host and client roles stay as they are (src/MindAttic.Orchestrator.Core/Setup/MachineRole.cs:7-18, MachineTopology.cs:39) for the remote-VM case in docs/REMOTE_HOST.md. A machine in the client role cannot join a mesh, because the client role stops its daemon. It must first go back to standalone.
MachineRole.HostRemoteName(MachineRole.cs:32, one host) is untouched. Mesh peers are a separate list. - Mesh peers are gateway remotes with kind
peer. They are stored inGatewaySettings.Remoteswith a sealed peer key.TeamWallWindow.RefreshAsync(TeamWallWindow.cs:209), which mergesLocalSource(:239) with every remote, shows them with no new plumbing. Existinggateway remote addremotes keep working (M.ORCH-T936's Team Wall). - Membership lives next to
machine.json, in a per-machinemesh.jsonunder%LOCALAPPDATA%. It is never inorchestrator.db(M.ORCH-T2563). - The tickets CLI on a peer forwards to the authority (M.ORCH-T2564), so agents keep typing
tickets ...unchanged. - Once milestone 1 is accepted, docs/REMOTE_HOST.md gains a pointer here. The host and client roles are retired only by a later ticket, and only if the owner wants it.
Why. Nothing existing breaks: every new path is opt-in and additive, and reusing gateway remotes means the wall, TermViewer and RemoteViewer work unchanged.
Rejected alternatives.
- Build the mesh on
machine connect. It forces the client role and hands off the daemon (MachineSetup.cs:88-114). - Convert the rig to the host role. The host role is meant for a remote VM, and it would make the laptop a client.
h. Verification
Agents never run test suites unasked. The owner runs /test on the repo after each feature lands. Each feature adds NUnit tests:
| Feature | NUnit tests it adds |
|---|---|
| Type into a peer's CLI | peer-key minting renews before expiry; inline tile and phone card open with interact when access=interact; keyboard holder handoff after InputHandoffSeconds, drop of a non-holder's keys, "Take keyboard"
|
| Join with one command | invite code single use and expiry; whois owner mismatch refused; pin and key exchange both ways; remove revokes on every peer |
| One authority | claim on two peers at once (a race test) grants exactly one; lease renewal, orphaning, grace requeue; stale generation close refused; epoch fencing demotes a returning authority; protocol skew refuses claims but allows interact |
| Cluster-wide pacing | sum of two machines' burn gates at 98% / 90%; allowance split by demand; offline fallback margin; Ultra weekly-only path |
| Placement | eligibility (RAM floor, pace, version, checkout); score order, with an expiring window first; stop at thresholds |
Two-machine acceptance (M.ORCH-T2577), run by the owner on the rig and the laptop:
- Fresh laptop with Tailscale:
mesh inviteon the rig,mesh joinon the laptop. Both walls show both machines' CLIs. - Type into a laptop CLI from the rig, and a rig CLI from the laptop. Typing on both at once shows a single holder.
- Start /allocate on both machines in one repo. Every ticket is claimed once, and both machines land commits on main.
- Run one login on both machines near its 5-hour cutoff. The combined spend stops at 98% (Ultra: the weekly line).
- Close the laptop lid mid-ticket. The rig shows it as orphaned, and the ticket is not re-granted before the grace period ends. Reopen the lid: the laptop renews and finishes.
- Stop the rig's Orchestrator. The laptop keeps its CLIs and refuses new claims. Restart the rig and everything resumes.
- The rig alone (laptop off) works exactly as a standalone machine did before the mesh.
Build plan
Milestone 1, "rig and laptop work completely": both machines remain standalone-capable, each types into the other's CLIs, claims are exactly-once with leases, pacing is correct across both, a lost peer shows on every dashboard, skew is guarded and both commit to main safely. The rig is the authority. There is no automatic or manual failover yet, and no scorer.
| Decision | Feature / story | Tasks | Milestone |
|---|---|---|---|
| c, b (link) | Type into a peer's CLI M.ORCH-T2544 (under M.ORCH-T2539) | T2556 self-renewing peer link, T2557 interact from every wall surface, T2558 input ownership | 1 |
| b | Plug a machine into the mesh M.ORCH-T2546 (under M.ORCH-T2545) | T2559 serve this machine, T2560 join both ways, T2561 find peers, T2562 confirm per-machine logins | 1 |
| a | Exactly-once claims M.ORCH-T2548 (under M.ORCH-T2547) | T2563 membership record, T2564 tickets reach the authority, T2565 claim leases | 1 |
| f | The mesh survives a lost machine M.ORCH-T2549 | T2567 lost peer, T2568 version skew, T2569 many machines on main | 1 |
| f | (same story) | T2566 authority failover | 2 |
| d | No login over its limits M.ORCH-T2551 (under M.ORCH-T2550) | T2570 publish login burn, T2571 pace on cluster burn, T2572 show cluster usage | 1 |
| e | Tickets go to the best machine and login M.ORCH-T2553 (under M.ORCH-T2552) | T2573 machine headroom, T2574 scorer, T2575 dispatch, T2576 work until thresholds | 2 |
| h | Rig and laptop work as one M.ORCH-T2555 (under M.ORCH-T2554) | T2577 acceptance checklist | 1 (checklist steps 1-7), extended in 2 |
Tickets this design changes.
- M.ORCH-T2566 Authority failover. Narrowed to a fenced, owner-run
mesh take-authorityplus epoch demotion, with automatic majority failover only once three or more peers exist. It moves to milestone 2. - M.ORCH-T2573 to M.ORCH-T2576. Milestone 2. Milestone 1 relies on per-machine /allocate through the authority.
- MAT-T530 "Add the laptop as a gateway remote" (Agency). Superseded by M.ORCH-T2560, which does the same with a renewing peer key.
Open questions for the owner
- A second store of record for failover. Manual failover to the laptop needs the ticket and Orchestrator databases to exist there: either SQL Server Express on the laptop with a replica, or SQLite on the laptop as an authority-capable store. It is not needed for milestone 1. Which one, when milestone 2 comes?
- Logins on the laptop. Which logins should be signed in on the laptop: all Team Premium seats and the Ultra login, or a subset? Placement only uses logins that are signed in on a machine.
- The defaults. Are these right to start with? Lease 10 minutes, orphan grace 30 minutes, keyboard handoff 2 seconds, offline pace margin 3 points. All of them are Vault settings that can be changed later.