Repository Public
1<!--2MacLustr Storage Node3File: docs/ARCHITECTURE.md4Author: Simon-Pierre Boucher5Contact: contact@spboucher.ai6-->7# Architecture — MacLustr Distributed Storage Fabric, Mobile Extension89## 1. What was found in MacLustr (audit of 2026-12-01)1011| Subject | Finding | Consequence |12|---|---|---|13| Node registration | Macs and servers are registered by `mld` (SSH, markers) and by the Command Center agents (`mcc-agent`, signed HTTPS requests). An iPhone can run neither. | Mobile nodes get their own enrolment: pairing code + device key (§3). |14| Earlier mobile work | `maclustr-mobile` (M4M64a:9320) makes iPhones pull compute jobs with a shared token. The Command Center brief forbids reusing it, and a shared token is not an identity. | Not reused. Not touched. |15| Dashboard | `clustr.spboucher.ai` = MacLustr Command Center (Go control-api + Next.js, M4M64c). Passkey sessions, roles, audit. | The "Mobile Storage Nodes" section is added there; it reads the coordinator through its admin API. |16| Storage of Macs and NAS | No distributed storage engine exists in MacLustr: each app keeps its data on its node's SSD, backups are per-app rsync. No NAS is attached to the cluster (the Ugreen unit is not connected). | The stable store of this fabric is new and lives on the coordinator's node. Nothing existing was replaced. |17| Tunnel, domains, certificates | Public exposure = DNS `A → 51.161.112.61` (BHS64) + Caddy route posted by `mld deploy` + WireGuard to the node. Certificates by Caddy (ACME). | `storage.spboucher.ai` uses exactly this. No new mechanism. |18| Local Network Privacy (macOS 27) | A third-party binary under PM2 cannot open connections **to** LAN peers; listening works, Apple binaries work. | The coordinator only listens; Bonjour is announced by Apple's `dns-sd`. The dashboard reaches the coordinator through its public URL. |1920## 2. Components2122```text23MacLustr cluster24 │25Storage Coordinator ── stable fragment store (HOT, SSD of m4mn) ── catalog (SQLite)26 │ ▲ every fragment always has its copy here27Mobile Storage Gateway (MSP/1: storage.spboucher.ai + LAN listener :8871)28 │ connections opened by the devices only29 ├── iPhone storage node (mobile_opportunistic)30 ├── iPhone storage node31 └── iPad storage node32```3334| Component | Where | Code |35|---|---|---|36| Storage coordinator + mobile gateway | mld app `maclustr-storage`, node **m4mn**, PM2 `storage-coordinator` (8870 gateway, 8871 LAN) and `storage-backup` | `server/` — Node ≥ 24, TypeScript run natively, no dependency |37| iOS app | App Store Connect `6841553137`, bundle `ai.spboucher.maclustr.storage` | `MacLustrStorageNode/` — Swift, SwiftUI, no third-party code |38| Dashboard section | Command Center, `clustr.spboucher.ai` | repository `maclustr-command-center` |39| CLI | `server/bin/mlstore.ts` | same admin API as the dashboard |4041## 3. Storage classes4243| Class | Hardware | Role in this fabric |44|---|---|---|45| HOT | Mac Studio / Mac mini | stable store: authoritative copy of every fragment (today: m4mn) |46| WARM | NAS / servers | not attached yet; a second stable replica belongs here (see §8) |47| OPPORTUNISTIC (`mobile_opportunistic`) | iPhone / iPad | redundant copies only; may vanish at any time |4849**Rule enforced by the coordinator: no fragment needed to rebuild an object ever exists only on an iOS device.** Concretely50(`server/src/coordinator.ts`): objects are written to the stable store before anything is offered to a device; the scheduler51only places fragments whose stable copy is `ok`; a drain deletes a fragment from a device only after re-checking the stable52copy, and pulls the fragment back first if that copy is missing; the overview exposes `fragments_only_on_mobile`, which the53tests require to be 0.5455The mobile copies are still useful, not decorative: when a stable copy is lost or corrupt (found on read or by the scrub),56the coordinator restores it from a mobile node that holds the fragment. This was exercised on the live system (see57TEST-REPORT.md).5859## 4. Data path60611. **Ingest** (`PUT /admin/v1/objects`): the stream is cut into fragments of `MSC_FRAGMENT_MIB` (8 MiB), each chunk is62 encrypted with AES-256-GCM, the fragment (`ciphertext ‖ 16-byte tag`) is written atomically to the stable store under63 its SHA-256, then the catalog transaction commits.642. **Encryption happens on the cluster.** Key and nonce derive from the cluster master key and the SHA-256 of the plaintext65 chunk (HMAC-SHA256). Identical chunks therefore give identical fragments — deduplication — while nothing can be derived66 or confirmed without the master key; each (key, nonce) pair encrypts exactly one plaintext. Devices only ever see67 ciphertext and never receive a key.683. **Placement** (every 3 s): for each online node that accepts writes, fragments with fewer than `MSC_MOBILE_REPLICAS` (2)69 mobile copies are assigned, fewest copies first, within `min(quota − committed, space the device reports writable)`,70 at most 12 queued per node. Offline nodes keep their placements: opportunistic nodes sleep constantly, and re-replicating71 each time would only churn flash and bandwidth.724. **Verification**: a possession proof at the end of each store, then every 6 h per fragment; the device also scrubs its73 own fragments (full re-hash) and reports corruption; the coordinator scrubs the stable store continuously.745. **Read** (`GET …/content`): from the stable store, every fragment checked against its id, the whole object against its75 digest. A missing stable fragment gives `503` and schedules its recovery from a mobile node.7677## 5. iOS app7879```text80MacLustrStorageNode/81├── App/ entry point, root view, theme82├── Core/83│ ├── Storage/ MacLustr Mobile Storage Engine: FragmentStore (actor), FragmentIndex (SQLite), CapacityMonitor84│ ├── Networking/ MSPClient (signed requests, pinning), TransferService (URLSessions), LANDiscovery (Bonjour)85│ ├── Security/ DeviceIdentity (Secure Enclave key, rotation), Keychain86│ ├── Synchronization/ NodeEngine (actor): policy, heartbeat, work, reconciliation, drain87│ ├── Background/ BackgroundScheduler (BGAppRefreshTask, BGProcessingTask)88│ └── Diagnostics/ DeviceConditions (network, power, thermal), Benchmark89├── Features/ Onboarding, Dashboard (+ station mode), Allocation, Activity, Settings90├── Models/ value types91└── Services/ NodeController (bridge engine → SwiftUI)92MacLustrStorageNodeTests/ unit tests of the engine and protocol helpers93```9495### MacLustr Mobile Storage Engine9697- Fragments live in `Application Support/MobileStorageEngine/fragments/<aa>/<sha256>` — never in Caches (purgeable), excluded98 from iCloud and Finder backups (`isExcludedFromBackup`), protection class "until first unlock" so that work continues99 while the screen is locked.100- **Commit** is the only way in: size check → streamed SHA-256 must equal the id → `fsync` → journal row → atomic rename →101 index row and journal removal in one transaction. A fragment already held is not written twice.102- **Recovery** at every launch: interrupted operations from the journal are finished or undone; index rows without a file103 are dropped; files without a row are adopted if their content matches their name, removed otherwise; partial downloads104 nobody owns are cleared. It is idempotent.105- **Quota**: `writable = min(quota − used, free − reserve)`, reserve = larger of 20 GB and 10 % of the device (configurable,106 never under 5 GB). Nothing is pre-allocated. When free space reaches the reserve the node reports `accepting: false`107 (`low_space`) and refuses store tasks; an arriving fragment that would cut into the reserve is discarded.108- Hashing is streamed 1 MiB at a time: memory does not grow with the fragment size.109110### Node engine111112One actor owns the state. Loops (foreground): heartbeat, long-poll for work, rate meter, maintenance (scrub, daily113inventory, key rotation). The **policy** function decides from live conditions whether the node may transfer at all and114whether it accepts new fragments, and how many transfers run in parallel (4 in station mode on power, 3 normally, 2 when115warm or in the background, 1 on cellular or when hot). Every pause has a reason shown to the user and sent to the cluster.116117## 6. Security model (summary — see SECURITY.md)118119Zero trust in both directions: the coordinator authenticates each request by the device's signature; the device accepts120only four operations and verifies every byte it stores; the LAN endpoint is trusted only through a pinned certificate; a121device holds ciphertext without keys; revocation is immediate.122123## 7. Erasure coding (Reed-Solomon): studied, not adopted in 1.0124125| | Replication (current) | Reed-Solomon k+m across mobile nodes |126|---|---|---|127| Purpose in this fabric | extra copies of fragments that already have a stable copy | same: the stable copy stays mandatory by rule |128| Mobile space for one extra loss tolerance | 1× per copy | m/k × (e.g. 0.5× for 4+2) |129| Repair of a stable fragment | one transfer from one device | k transfers from k distinct devices, all online at the same time |130| Device-side work | none beyond hashing | none (coding would stay on the cluster) |131| Coordinator complexity | low | stripes, generations, partial-stripe repair, GF(256) codec to write and test |132133With mobile nodes the binding constraint is not space but **simultaneous availability**: a k-of-n read needs k devices134reachable at once, and iPhones are reachable a fraction of the time. With the fleet in view (a handful of devices), the135probability of finding k of them awake together is low exactly when a repair is needed, whereas a plain replica needs one.136Reed-Solomon becomes worth its complexity when (a) there are enough nodes for n ≥ 6 distinct devices per stripe, and137(b) measured availability is high (dedicated, plugged-in devices in station mode), or when WARM storage joins and stripes138can span NAS + mobile. The catalog already separates objects, fragments and placements, so stripes can be added as a new139placement policy without changing MSP/1 or the app: a parity fragment is just another opaque fragment.140141## 8. Known limits of 1.0142143- **One stable copy.** The stable store and the catalog are on one node (m4mn). The catalog is backed up daily on the same144 disk (`storage-backup`). Losing that node's disk loses the catalog and the master key, and with them the ability to read145 the fragments the phones hold. A second stable replica (another Mac, or the NAS once attached) and an off-node copy of146 `~/.config/maclustr-storage` are the first things to add; `server/scripts/backup.sh` is the hook.147- **No client of the fabric yet.** Objects enter through the admin API / `mlstore put`. No MacLustr app writes to it.148- **No silent push.** The app is not woken by the cluster; background work depends on iOS scheduling (IOS-LIMITS.md).149- The dashboard's node health is derived from heartbeats; there is no alerting rule on it yet.150