Jump to content

MindAttic.Relay/SPEC

From MindAttic Docs

MindAttic.Relay — Product specification

The complete product, written down before it is built (owner, 2026-11-30, MAT-T360). Every epic, feature and story the finished product will have is listed here, built or not. The ticket tree mirrors this file one to one: every ID below is a ticket. Nothing gets built unless it is here, and anything taken out of here is closed wont-do in the tree. docs/RELEASE.md says which part of this the current milestone delivers. The architecture behind every line is docs/design/relay.md (accepted 2026-10-27).

1. Product definition

In the ecosystem (docs/ECOSYSTEM.md at the workspace root): Platform: owns app-to-app messages (one local bus, later one hub across members' machines).

Relay is the one way MindAttic apps send each other messages, on one machine or across many: a NATS + JetStream server on every machine, one Protocol Buffers contracts package, one client library, an outbox package and the relay CLI. It replaces named-pipe AppBus, shared-database polling and hand-built sockets. Finished, it carries the hub-and-spoke Orchestrator: members' machines joined into one Orchestrator, with presence, terminals, tickets, leases and pacing policy shared across machines, while a lone machine behaves exactly as it does today.

2. Scope boundary

2.1 In the product

  • Relay server: pinned nats-server + JetStream as a Windows service, loopback TLS, credentials from day one.
  • relay CLI: install, status, uninstall, credentials, diagnostics, join/leave, backup.
  • MindAttic.Relay.Contracts: .proto contracts, envelope headers, subject builders.
  • MindAttic.Relay.Client: publish/subscribe with durable consumers, request/reply services, key-value state.
  • MindAttic.Relay.Outbox: a database change and its event saved together.
  • Consumers: Orchestrator.Desktop and Tickets.Desktop (AppBus), KdpPublish and Prose (published books).
  • Multi-machine: hub and leaf links, JetStream domains, device credentials, auth callout.
  • Hub-and-spoke Orchestrator: hub service, org presence and meters, terminal streams with grants and consent, ticket replica and leases, central pacing policy, opt-in remote dispatch, hub operations.

2.2 Never in the product

  • Replacing an app's database or git. The database is the record; a message is the news (RLY-LAW-2).
  • Exactly-once delivery. Delivery is at least once and handlers are idempotent.
  • Running anyone's code. Relay carries messages; apps decide what to do with them.
  • Redis + gRPC as the bus. Rejected in design §2; a gRPC endpoint is added only for an outside system that can't use a NATS client.
  • Browser or phone clients. No consumer needs one; NATS WebSocket keeps the door open if that changes.
  • Open-ended bug hunts as tickets (old M.RLY-T78). A real bug gets its own fix ticket.

3. Users and roles

  • The owner installs and operates Relay, issues and revokes credentials, joins machines and signs off releases.
  • An app author (any MindAttic repo) publishes events and serves commands through the client, never NATS itself.
  • Consuming apps: Orchestrator.Desktop, Tickets.Desktop, KdpPublish, Prose Hub/Writer, the Orchestrator daemon.
  • A member of the organisation signs in to the hub and enrolls their devices.
  • A viewer watches or drives a terminal on another machine under a grant.

4. Requirements

Status is built (closed ticket, with evidence) or MS-n: the milestone in §7 that delivers it. Every unbuilt story is approved and prioritised in build order; the planner splits a story into tasks when the queue reaches it. In MS-8 the leaves are already level:task tickets.

E0. Architecture design — M.RLY-T96 — built

F0.1 Accepted design — M.RLY-T97 — built

  • S0.1.1 Relay design — M.RLY-T1 — built As the owner, I have one accepted architecture for cross-app messaging, so every later piece builds on one plan. Acceptance: Given docs/design/relay.md, then it covers server, subjects, envelope, security, delivery rules, versioning and the hub-and-spoke mapping. Evidence: ba9744e, 3891ecd (M.RLY-T31).

E1. A local message bus runs — M.RLY-T80 — MS-8

A pinned, credentialed, loopback NATS + JetStream service installed and operated by the relay CLI.

F1.1 Install and run server — M.RLY-T83 — MS-8

  • S1.1.1 Solution scaffold — M.RLY-T91 — MS-8 As the owner, MindAttic.Relay.slnx holds the relay CLI (net10.0, Spectre.Console.Cli), an NUnit test project and run.ps1 that deploys to R:\MindAttic\Apps\MindAttic.Relay. Acceptance: Given a clean clone, when I build, then relay --help runs.
  • S1.1.2 Pinned server download — M.RLY-T39 — MS-8 As the owner, relay install downloads the pinned nats-server, verifies its SHA-256 and installs an auto-start Windows service. Acceptance: Given a tampered download, then install refuses it.
  • S1.1.3 Server config and TLS — M.RLY-T40 — MS-8 As the owner, install generates a loopback-only, TLS config with the JetStream store under %ProgramData%\MindAttic\Relay, a local CA and bootstrap credentials; re-running is safe.
  • S1.1.4 Status and uninstall — M.RLY-T41 — MS-8 As the owner, relay status shows running, version, JetStream usage and connections; relay uninstall keeps data unless --purge. Acceptance: unauthenticated and non-TLS connections are refused.

F1.2 Per-app credentials — M.RLY-T84 — MS-8

  • S1.2.1 creds issue and list — M.RLY-T69 — MS-8 As the owner, relay creds issue <app> writes a per-app credential with its subject permissions (orch, tix, prose, kdp, relay-admin) into MindAttic.Vault and relay creds list shows them. Acceptance: an app can't publish outside its permissions.

F1.3 Diagnostics CLI — M.RLY-T85 — MS-8

  • S1.3.1 Relay CLI monitoring — M.RLY-T47 — MS-8 As the owner, relay tail <subject>, relay streams (sizes, consumer lag) and relay dead-letters [--replay] read the localhost monitoring endpoint.

E2. Apps exchange typed messages — M.RLY-T81 — MS-8

F2.1 Contracts package — M.RLY-T86 — MS-8

  • S2.1.1 Envelope and headers — M.RLY-T51 — MS-8 As an app author, RelayHeaders and RelayEnvelope read and write the six envelope headers, and a consumer skips an unknown type or newer version and counts it. Packaged to the workspace NuGet feed.
  • S2.1.2 Proto definitions — M.RLY-T49 — MS-8 As an app author, per-app .proto files (orch, tix, relay) generate C# types for ticket changed, open-ticket and show-worker. Acceptance: round-trip tests pass.
  • S2.1.3 Subjects class — M.RLY-T50 — MS-8 As an app author, subject builders and validators enforce <app>.<noun>.<event>[.<key>], <app>.cmd.<verb> and term.<device>.<session>.<channel>, rejecting bad tokens and email-like values.

F2.2 Client library — M.RLY-T87 — MS-8

  • S2.2.1 Client connection and DI — M.RLY-T42 — MS-8 As an app author, AddMindAtticRelay(appName) connects with the app's Vault credential, reconnects with backoff and never blocks the app when Relay is down.
  • S2.2.2 Publish and subscribe — M.RLY-T67 — MS-8 As an app author, PublishAsync sends with envelope headers and SubscribeAsync uses a durable consumer that acks after the handler, redelivers a bounded number of times, then dead-letters; a relay_processed helper makes handlers idempotent.
  • S2.2.3 Request and serve — M.RLY-T68 — MS-8 As an app author, RequestAsync times out with a plain-words failure and ServeAsync registers a NATS service with stats. Acceptance: works again after a server restart.

E3. Orchestrator and Tickets talk over Relay — M.RLY-T82 — MS-8

F3.1 Orchestrator side — M.RLY-T88 — MS-8

  • S3.1.1 Orchestrator on Relay — M.RLY-T44 — MS-8 As the owner, Orchestrator publishes tix.ticket.changed.<id> and serves orch.cmd.show-worker over Relay, with the named-pipe AppBus kept as a fallback behind a setting.

F3.2 Tickets.Desktop side — M.RLY-T89 — MS-8

  • S3.2.1 Tickets Desktop on Relay — M.RLY-T45 — MS-8 As the owner, Tickets.Desktop hears ticket changes, serves tix.cmd.open-ticket and sends orch.cmd.show-worker over Relay, with the pipe fallback.

F3.3 Relay standard and docs (polish and release 1.0) — M.RLY-T90 — MS-8

  • S3.3.1 Standard and tests — M.RLY-T71 — MS-8 As an app author, the standard "new cross-app messages go through Relay" is documented, with tests on both sides against a throwaway server.
  • S3.3.2 Ship Relay 1.0 — M.RLY-T92 — MS-8 As the owner, Contracts and Client 1.0.0 are on the workspace feed, relay.exe is deployed and installed on the main PC, and docs/BIBLE verified state is current. Acceptance: MS-8's sign-off check.

E4. Producers never lose messages — M.RLY-T98 — MS-113

F4.1 Outbox package — M.RLY-T99 — MS-113

  • S4.1.1 Transactional outbox — M.RLY-T100 — MS-113 As an app author, a database change and its event are saved in one EF Core transaction and a dispatcher publishes it with Nats-Msg-Id = the row id, so a crash can neither lose nor double it.

F4.2 KdpPublish announces books — M.RLY-T101 — MS-113

  • S4.2.1 Book published event — M.RLY-T102 — MS-113 As KdpPublish, I announce prose.book.published (slug, ASIN, Amazon URL, KDP title id) through the outbox in the transaction that writes the ASIN.
  • S4.2.2 Prose refreshes on publish — M.RLY-T103 — MS-113 As the owner, Prose Hub and Writer show a new book's ASIN and link as soon as the event arrives.

F4.3 Key-value state — M.RLY-T104 — MS-113

  • S4.3.1 Key-value buckets — M.RLY-T105 — MS-113 As an app author, I get, put, delete and watch KV keys with revision-conditional updates.

F4.4 Orchestrator Relay page — M.RLY-T106 — MS-113

  • S4.4.1 Relay health page — M.RLY-T107 — MS-113 As the owner, Orchestrator.Desktop shows server, streams, consumer lag, dead letters and a plain-words health line.

F4.5 Polish and release 1.1 — M.RLY-T108 — MS-113

  • S4.5.1 Ship Relay 1.1 — M.RLY-T109 — MS-113 As the owner, Contracts, Client and Outbox 1.1.0 are published, relay.exe redeployed, docs current, and I sign off a book publish reaching Prose over Relay.

F5.1 Spokes survive going offline — M.RLY-T111 — MS-114

  • S5.1.1 Offline prototype — M.RLY-T112 — MS-114 As the architect, an automated throwaway hub/leaf test keeps a spoke offline an hour with events on both sides and shows nothing lost or doubled and the KV mirror served offline, before anything depends on it.

F5.2 Hub and spoke config — M.RLY-T113 — MS-114

  • S5.2.1 Hub config — M.RLY-T114 — MS-114 As the owner, relay install --hub generates the leaf listener, certificate, hub domain and sourced spoke streams.
  • S5.2.2 Spoke config — M.RLY-T115 — MS-114 As the owner, a spoke has one remote with the pinned hub key, its own domain, mirrored hub KV and compression off on terminal links.

F5.3 Join and leave — M.RLY-T116 — MS-114

  • S5.3.1 relay join and leave — M.RLY-T117 — MS-114 As the owner, relay join <hub> / relay leave connect this machine over the tailnet or WebSocket 443, and relay status shows link state, last seen and lag.

E6. Device identity and security — M.RLY-T118 — MS-114

F6.1 Operator key custody — M.RLY-T119 — MS-114

  • S6.1.1 Operator key backup — M.RLY-T120 — MS-114 As the owner, the operator key has an offline backup and a restore runbook, and each organisation has its own account.

F6.2 Revocation — M.RLY-T121 — MS-114

  • S6.2.1 relay creds revoke — M.RLY-T122 — MS-114 As the owner, revoking a user drops its live connections; tests prove permission limits, no credentials on disk and non-TLS refused.

F6.3 Device credentials — M.RLY-T123 — MS-114

  • S6.3.1 Device key — M.RLY-T124 — MS-114 As the architect, device identity (NKey in Vault vs CNG-signed challenge) is decided, recorded and implemented.
  • S6.3.2 Enrollment API — M.RLY-T125 — MS-114 As a member, enrolling a device binds its key to me and issues self-refreshing, expiring credentials; the owner can revoke it.
  • S6.3.3 Auth callout — M.RLY-T126 — MS-114 As the hub, an auth callout grants each connection expiring permissions from hub state; tests cover a revoked device, an expired grant and the callout being down.

F6.4 Polish and release 2.0 — M.RLY-T127 — MS-114

  • S6.4.1 Ship Relay 2.0 — M.RLY-T128 — MS-114 As the owner, two machines are joined to one hub, a spoke survives an hour offline without losing events, runbooks are current and 2.0.0 is published.

E7. One Orchestrator spans machines — M.RLY-T129 — MS-115

F7.1 Orchestrator hub seams — M.RLY-T130 — MS-115

  • S7.1.1 Seam interfaces — M.RLY-T131 — MS-115 As the architect, Orchestrator Core calls IClaims, IPolicySource, IMeterSink, IOrgDirectory and ISessionDirectory.
  • S7.1.2 Local implementations — M.RLY-T132 — MS-115 As the owner, Local* implementations keep single-machine behaviour identical.
  • S7.1.3 Hub settings — M.RLY-T133 — MS-115 As the owner, OrchestratorSettings.Hub defaults to Off and Off builds only the Local seams.

F7.2 Hub service — M.RLY-T134 — MS-115

  • S7.2.1 Hub service — M.RLY-T135 — MS-115 As a member, I sign in to the hub with MindAttic.Authentication (password, TOTP); it serves Relay commands.
  • S7.2.2 Device enrollment pages — M.RLY-T136 — MS-115 As a member, I enroll, refresh and revoke my devices from the hub's pages.

F7.3 Joining from a spoke — M.RLY-T137 — MS-115

  • S7.3.1 Spoke link supervision — M.RLY-T138 — MS-115 As the owner, the daemon supervises the leaf link and publishes presence each tick, surviving offline.
  • S7.3.2 Hub CLI and chip — M.RLY-T139 — MS-115 As the owner, orchestrator hub host|join|status|leave|devices work and the dashboard shows a hub chip.

E8. Org presence and terminals — M.RLY-T140 — MS-115

F8.1 Presence and meters — M.RLY-T141 — MS-115

  • S8.1.1 Presence and meters — M.RLY-T142 — MS-115 As the owner, spokes publish roster, masked sessions, meters and agent summaries; the hub merges meters per login.
  • S8.1.2 Org view across machines — M.RLY-T143 — MS-115 As the owner, the org view and /org usage cover every joined machine, remote people read-only.

F8.2 Terminal streams — M.RLY-T144 — MS-115

  • S8.2.1 Terminal frames over Relay — M.RLY-T145 — MS-115 As a viewer, I attach to a remote session: masked frames on term.*.out, replay by command, input only for interact.
  • S8.2.2 Grants by auth callout — M.RLY-T146 — MS-115 As the owner, grants and audit live at the hub, enforced by expiring callout permissions and re-checked by the spoke.
  • S8.2.3 Consent for interact — M.RLY-T147 — MS-115 As a session owner, the first interact under a grant asks me; no answer in 30 s means watch-only.
  • S8.2.4 Team Wall through the hub — M.RLY-T148 — MS-115 As the owner, Team Wall and TermViewer list org sessions from the presence bucket.

E9. Tickets and policy across machines — M.RLY-T149 — MS-115

F9.1 Ticket replica — M.RLY-T150 — MS-115

  • S9.1.1 Ticket event stream — M.RLY-T151 — MS-115 As the hub, every ticket change in the hub's TicketsDb (the store of record) is a tix.ticket.changed event.
  • S9.1.2 Spoke replica — M.RLY-T152 — MS-115 As a spoke, a durable consumer keeps data/Tickets.db as a read replica; local writes are refused while joined.
  • S9.1.3 Ticket commands — M.RLY-T153 — MS-115 As a member, notes, status, assign and approve are tix.cmd.* commands authorised by role and org.
  • S9.1.4 Tools write to the hub — M.RLY-T154 — MS-115 As a member, the tickets CLI and Desktop write through the hub and say plainly when they're read-only.

F9.2 Leases — M.RLY-T155 — MS-115

  • S9.2.1 Claim and lease — M.RLY-T156 — MS-115 As the hub, tix.cmd.claim runs Allocate and writes the lease into the leases KV bucket.
  • S9.2.2 Renewal and fencing — M.RLY-T157 — MS-115 As the hub, renewals and completions are revision-conditional; a lease expires after 3 ticks and requeues after the offline grace.
  • S9.2.3 Join-mode next --claim — M.RLY-T158 — MS-115 As an agent, orchestrator next --claim works through the hub with a 10 s timeout and a plain message.

F9.3 Central policy — M.RLY-T159 — MS-115

  • S9.3.1 Central pacing policy — M.RLY-T160 — MS-115 As the owner, cutoffs, allowances, pace target, routing and weekly reserve live in the hub's policy bucket, mirrored to every spoke.
  • S9.3.2 Allowance split per device — M.RLY-T161 — MS-115 As the owner, a login live on several machines has its allowance split across them.
  • S9.3.3 Opt-in remote dispatch — M.RLY-T162 — MS-115 As a machine owner, my spoke takes dispatched work only when I set AcceptDispatch.

E10. The hub is operable — M.RLY-T163 — MS-115

F10.1 Hub operations — M.RLY-T164 — MS-115

  • S10.1.1 Hub deploy — M.RLY-T165 — MS-115 As the owner, run.ps1 publishes the hub service and hub server, hub first.
  • S10.1.2 Backups and restore — M.RLY-T166 — MS-115 As the owner, relay backup saves the hub DB and JetStream/KV state, with a tested restore drill.
  • S10.1.3 Key rotation and limits — M.RLY-T167 — MS-115 As the owner, keys and the hub certificate rotate without re-enrolling; device revocation has a UI; sign-in and enrollment are rate-limited; a hub-move runbook exists.
  • S10.1.4 Skew and chaos — M.RLY-T168 — MS-115 As the architect, N-1 vs N contracts and chaos cases (hub killed, spoke asleep, clock skew) are covered by tests.

F10.2 Polish and release 3.0 — M.RLY-T169 — MS-115

  • S10.2.1 Ship Relay 3.0 — M.RLY-T170 — MS-115 As the owner, members' machines run as one Orchestrator over Relay and a lone machine is unchanged; docs and runbooks are current; I sign it off.

5. Non-functional requirements

  • Platform: Windows 11, .NET 10; nats-server pinned by version and SHA-256 (RLY-LAW-9).
  • Security: TLS on every listener, loopback included; credentials required from the first connection; secrets in MindAttic.Vault, never on disk in the repo; monitoring endpoint on localhost only; compression off on terminal links.
  • Delivery: events at least once, ordered per subject, replayable; handlers idempotent; commands time out loudly; poison messages dead-lettered (design §8).
  • Resilience: nothing depends on Relay to keep working; the named-pipe AppBus stays as a fallback; spokes keep working offline in their own JetStream domain.
  • Versioning: contracts change by adding fields; a breaking change is a new Relay-Version; the hub supports N and N-1.
  • Tests: NUnit; integration tests run against a throwaway nats-server, never the installed service.

6. Interfaces and dependencies

Platform repos used, by concern: credentials (per-app Relay credentials are stored in Vault): MindAttic.Vault; its consumers: MindAttic.Orchestrator, MindAttic.Tickets, KdpPublish; logging: MindAttic.Log.

  • Uses: NATS.Net, Grpc.Tools / Google.Protobuf, Spectre.Console.Cli, MindAttic.Vault (credentials), MindAttic.Authentication (hub sign-in, MS-115).
  • Used by: MindAttic.Orchestrator (daemon and Desktop), MindAttic.Tickets (Desktop and CLI), KdpPublish, Prose. The live named-pipe bus it replaces: MindAttic.Orchestrator.Core/Status/AppBus.cs, AppBusHub.cs and MindAttic.Tickets.Desktop/Bus/AppBus.cs, AppBusClient.cs.
  • Contract: subjects (design §4), envelope headers (§6), delivery rules (§8), versioning (§9).

7. Release sequence

  1. MS-8 — Relay 1.0 single-machine bus (active, docs/RELEASE.md): E1, E2, E3. Ends with F3.3.
  2. MS-113 — Relay 1.1 durable producers: E4. Ends with F4.5.
  3. MS-114 — Relay 2.0 across machines: E5, E6. Ends with F6.4.
  4. MS-115 — Relay 3.0 hub-and-spoke Orchestrator: E7, E8, E9, E10. Ends with F10.2. The finished product.

Queue priorities descend across the whole sequence: E1 1000, E2 900, E3 800, E4 700, E5 600, E6 500, E7 400, E8 300, E9 200, E10 100; each feature takes its epic's band plus 90, 80 ...

8. Change log

Date Change to scope Ticket
2026-11-30 Specification written from README, BIBLE, design, code and the ticket history. The multi-machine and hub-and-spoke work the reset deferred (old M.RLY-T7..T30, T43, T46, T48, T52..T76) is now planned as MS-113..MS-115 under new story tickets that cite the old IDs. The hub-and-spoke stories change Orchestrator and Tickets code; they live here because design §10 moved that plan into Relay. MAT-T372
2026-11-30 MAT-T462 single-source pass: 8 renamed (epics as outcomes); 3 requires added chaining the milestones (MS-113 waits on MS-8 T90, MS-114 on MS-113 polish T108, MS-115 on MS-114 polish T127). Note: features F3.1/F3.2 and F4.2 change Orchestrator, Tickets.Desktop and KdpPublish code; by the one-owner law those adoptions belong to the consumer repos and should move there when worked MAT-T464
From MindAttic.Relay/docs/SPEC.md via MindAttic.Docs; synced automatically, so edit the repo, not this page.