Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
7eaff88
product and engineering specifications for simplexmq
evgeny-simplex Mar 2, 2026
c7ff637
update CODE.md
evgeny-simplex Mar 9, 2026
db6096a
Merge branch 'rcv-services' into ep/spec-2
epoberezkin Mar 9, 2026
40875e3
update
evgeny-simplex Mar 9, 2026
a68ab40
Merge branch 'rcv-services' into ep/spec-2
evgeny-simplex Mar 9, 2026
e3d9942
Merge branch 'rcv-services' into ep/spec-2
evgeny-simplex Mar 10, 2026
8622908
Merge branch 'rcv-services' into ep/spec-2
evgeny-simplex Mar 10, 2026
0bba2ef
add rcv services spec
evgeny-simplex Mar 10, 2026
ea2a62a
more specs
evgeny-simplex Mar 11, 2026
66d7efa
some modules documented
evgeny-simplex Mar 11, 2026
e5dbe97
spec references in code
evgeny-simplex Mar 11, 2026
7ece87f
encoding notes
evgeny-simplex Mar 11, 2026
844b5ad
cryptography modules specs
evgeny-simplex Mar 11, 2026
326d6cc
code comments
evgeny-simplex Mar 11, 2026
9e3b47a
code refs, additional specs
evgeny-simplex Mar 11, 2026
35d4065
specs for transport
evgeny-simplex Mar 11, 2026
09d55de
protocol and client specs
evgeny-simplex Mar 11, 2026
260ffb1
SMP router specs
evgeny-simplex Mar 12, 2026
8b39610
Merge branch 'rcv-services' into ep/spec-2
epoberezkin Mar 12, 2026
f7be449
SMP router specs
evgeny-simplex Mar 12, 2026
c8f2edc
spec for agent protocol
evgeny-simplex Mar 13, 2026
3a756f9
agent client spec
evgeny-simplex Mar 13, 2026
541b3f9
agent spec
evgeny-simplex Mar 13, 2026
c940f16
update agent specs
evgeny-simplex Mar 13, 2026
8557d2a
agent util specs
evgeny-simplex Mar 13, 2026
bde9050
agent store and notifications specs
evgeny-simplex Mar 13, 2026
546ee1a
update specs
evgeny-simplex Mar 13, 2026
f131531
xftp specs
evgeny-simplex Mar 13, 2026
ceeeeec
more topics
evgeny-simplex Mar 13, 2026
b928854
xrcp specs
evgeny-simplex Mar 13, 2026
3bde77d
update terms
evgeny-simplex Mar 13, 2026
1cc4d98
terms 2
evgeny-simplex Mar 13, 2026
fc5b601
notes
evgeny-simplex Mar 13, 2026
388b13d
docs
evgeny-simplex Mar 13, 2026
13e8b7b
links
evgeny-simplex Mar 13, 2026
cbf32a3
ntf
evgeny-simplex Mar 13, 2026
ca847b1
update
evgeny-simplex Mar 13, 2026
a7c6dde
router diagrams
evgeny-simplex Mar 14, 2026
abcc6da
fixes
evgeny-simplex Mar 14, 2026
4df501e
client diagrams
evgeny-simplex Mar 14, 2026
1db93b9
corrections
evgeny-simplex Mar 14, 2026
8e294cb
agent diagrams
evgeny-simplex Mar 14, 2026
7410ceb
update agent diagram
evgeny-simplex Mar 14, 2026
1354918
improve diagram
evgeny-simplex Mar 14, 2026
021d929
titles
evgeny-simplex Mar 14, 2026
958f030
improve diagram
evgeny-simplex Mar 14, 2026
1ed3444
improve diagram 2
evgeny-simplex Mar 14, 2026
0582429
more diagram
evgeny-simplex Mar 14, 2026
1ddb165
sequence diagrams layout
evgeny-simplex Mar 14, 2026
b62a224
agent topics
evgeny-simplex Mar 14, 2026
7aefcbf
agent connection topic
evgeny-simplex Mar 14, 2026
eafc84f
fix doc
evgeny-simplex Mar 14, 2026
c8de008
fixes
evgeny-simplex Mar 14, 2026
38fa104
subscriptions
evgeny-simplex Mar 14, 2026
7d6e319
client services
evgeny-simplex Mar 14, 2026
9b15cdc
notifications spec
evgeny-simplex Mar 14, 2026
259e950
transport
evgeny-simplex Mar 15, 2026
31158ab
update
evgeny-simplex Mar 15, 2026
73d12aa
patterns
evgeny-simplex Mar 15, 2026
c069881
xftp topic
evgeny-simplex Mar 15, 2026
f56b594
remove old topic stubs
evgeny-simplex Mar 15, 2026
bb72e1a
update
evgeny-simplex Mar 15, 2026
8dfb59b
design notes for main spec files
evgeny-simplex Mar 15, 2026
486d325
update readme
evgeny-simplex Mar 15, 2026
ae57fef
encryption schemes
evgeny-simplex Mar 15, 2026
152c30c
rcv-services issues
evgeny-simplex Mar 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions CODE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# simplexmq — LLM Navigation Guide

This file is the entry point for LLMs working on simplexmq. Read it before making any code changes.

## Three-Layer Architecture

simplexmq maintains three documentation layers alongside source code:

| Layer | Directory | Answers | Audience |
|-------|-----------|---------|----------|
| **Product** | `product/` | What does this do? Who uses it? What must never break? | Anyone reasoning about behavior, privacy, security |
| **Spec** | `spec/` | How does the code work? What does each function do? What are the security invariants? | LLMs and developers modifying code |
| **Protocol** | `protocol/` | What is the wire protocol? What are the message formats and state machines? | Protocol implementors, formal verification |

Additionally:
- `rfcs/` — Protocol evolution: each RFC describes a delta to a protocol spec
- `product/threat-model.md` — Comprehensive threat model across all protocols
- `spec/security-invariants.md` — Every security invariant with enforcement and test coverage

## Navigation Workflow

When modifying code, follow this sequence:

1. **Identify scope** — Find the relevant component in `product/concepts.md`
2. **Load product context** — Read the component file in `product/components/` to understand what users depend on
3. **Load spec context** — Read the relevant `spec/` file(s) for implementation details and call graphs
4. **Check security** — Read `spec/security-invariants.md` for any invariants enforced by the code you're changing
5. **Load source** — Read the actual source files referenced in spec/
6. **Identify impact** — Trace the call graph to understand what your change affects
7. **Implement** — Make the change
8. **Update all layers** — Update spec/, product/, and protocol/ (if wire protocol changed) to stay coherent

## Protocol Specifications

Consolidated protocol specs live in `protocol/`. These describe the wire protocols as originally specified. Code has advanced beyond these versions — Phase 2 of this project will synchronize them.

| File | Protocol | Spec version | Code version |
|------|----------|-------------|--------------|
| `simplex-messaging.md` | SMP (simplex messaging) | v9 | SMP relay v18, SMP client v4 |
| `agent-protocol.md` | Agent (duplex connections) | v5 | Agent v7 |
| `xftp.md` | XFTP (file transfer) | v2 | XFTP v3 |
| `xrcp.md` | XRCP (remote control) | v1 | RCP v1 |
| `push-notifications.md` | Push notifications | v2 | NTF v3 |
| `pqdr.md` | PQDR (post-quantum double ratchet) | v1 | E2E v3 |
| `overview-tjr.md` | Cross-protocol overview | — | — |

Note: SMP has multiple version axes — `VersionSMP` (relay/transport, currently 18), `VersionSMPC` (client protocol, currently 4), and `VersionSMPA` (agent, currently 7). These are negotiated independently.

Protocol specs are amended in place when implementation changes. RFCs in `rfcs/` track the evolution history.

## Source Structure

```
src/Simplex/
Messaging/
Protocol.hs, Protocol/Types.hs — SMP wire protocol types + encoding
Client.hs — SMP client (protocol operations, proxy relay)
Client/Agent.hs — Low-level async SMP agent
Server.hs — SMP server request handling
Server/Env/STM.hs — Server environment + STM state
Server/Main.hs, Server/Main/Init.hs — Server CLI + initialization
Server/QueueStore/ — Queue storage (STM, Postgres)
Server/MsgStore/ — Message storage (STM, Journal, Postgres)
Server/MsgStore/Journal.hs — Journal message store (1000 lines)
Server/StoreLog/ — Store log (append-only write, read-compact-rewrite restore)
Server/NtfStore.hs — Message notification store
Server/Control.hs, Server/CLI.hs — Control protocol + CLI utilities
Server/Stats.hs, Server/Prometheus.hs — Metrics
Server/Information.hs — Server public information / metadata
Agent.hs — SMP agent: duplex connections, queue rotation
Agent/Client.hs — Agent's SMP/XFTP/NTF client management
Agent/Protocol.hs — Agent wire protocol types + encoding (2200 lines)
Agent/Store.hs — Agent storage types (queues, connections, messages)
Agent/Store/AgentStore.hs — Agent storage implementation (3500 lines)
Agent/Store/ — Agent storage backends (SQLite, Postgres)
Agent/Env/SQLite.hs — Agent environment + configuration
Agent/NtfSubSupervisor.hs — Notification subscription management
Agent/TSessionSubs.hs — Transport session subscriptions
Agent/Stats.hs — Agent statistics
Agent/RetryInterval.hs — Retry interval logic
Agent/Lock.hs — Named locks
Agent/QueryString.hs — Query string parsing
Transport.hs — TLS transport abstraction + handshake
Transport/Client.hs, Transport/Server.hs — TLS client + server
Transport/HTTP2.hs — HTTP/2 transport setup
Transport/HTTP2/Client.hs — HTTP/2 client
Transport/HTTP2/Server.hs — HTTP/2 server
Transport/HTTP2/File.hs — HTTP/2 file streaming
Transport/WebSockets.hs — WebSocket adapter
Transport/Buffer.hs — Transport buffering
Transport/KeepAlive.hs — TCP keepalive
Transport/Shared.hs — Certificate chain validation
Transport/Credentials.hs — TLS credential generation
Crypto.hs — All cryptographic primitives
Crypto/File.hs — File encryption (NaCl secret box + lazy)
Crypto/Lazy.hs — Lazy hashing + encryption
Crypto/Ratchet.hs — Double ratchet + PQDR
Crypto/ShortLink.hs — Short link key derivation
Crypto/SNTRUP761.hs — Post-quantum KEM hybrid secret
Crypto/SNTRUP761/Bindings.hs — sntrup761 C FFI bindings
Notifications/Protocol.hs — NTF wire protocol types + encoding
Notifications/Types.hs — NTF agent types (tokens, subscriptions)
Notifications/Transport.hs — NTF transport handshake
Notifications/Client.hs — NTF client operations
Notifications/Server.hs — NTF server
Notifications/Server/Env.hs — NTF server environment + config
Notifications/Server/Store.hs — NTF server storage (STM)
Notifications/Server/Store/Postgres.hs — NTF server storage (Postgres)
Notifications/Server/Push/APNS.hs — Apple push notification integration
Notifications/Server/Push/APNS/Internal.hs — APNS HTTP/2 client
Notifications/Server/Main.hs — NTF server CLI
Notifications/Server/Stats.hs — NTF server metrics
Notifications/Server/Prometheus.hs — NTF Prometheus metrics
Notifications/Server/Control.hs — NTF server control
Encoding.hs, Encoding/String.hs — Binary + string encoding
Version.hs, Version/Internal.hs — Version ranges + negotiation
Util.hs — Utilities (error handling, STM, grouping)
Parsers.hs — Attoparsec parser combinators
TMap.hs — Transactional map (STM)
Compression.hs — Zstd compression
ServiceScheme.hs — Service scheme + server location types
Session.hs — Session variables (TVar-based)
SystemTime.hs — Rounded system time types
FileTransfer/
Protocol.hs — XFTP wire protocol types + encoding
Client.hs — XFTP client operations
Client/Agent.hs — XFTP client agent (connection pooling)
Client/Main.hs — XFTP CLI client implementation
Client/Presets.hs — Default XFTP servers
Server.hs — XFTP server request handling
Server/Env.hs — XFTP server environment + config
Server/Store.hs — XFTP server storage
Server/StoreLog.hs — XFTP server store log
Server/Main.hs — XFTP server CLI
Server/Stats.hs — XFTP server metrics
Server/Prometheus.hs — XFTP Prometheus metrics
Server/Control.hs — XFTP server control
Agent.hs — XFTP agent operations
Description.hs — File description format
Transport.hs — XFTP transport
Crypto.hs — File encryption for transfer
Types.hs — File transfer types
Chunks.hs — Chunk sizing
RemoteControl/
Client.hs — XRCP client (ctrl device)
Invitation.hs — XRCP invitation handling
Discovery.hs — Local network discovery
Discovery/Multicast.hsc — Multicast discovery (C FFI)
Types.hs — XRCP types + version

apps/
smp-server/Main.hs — SMP server executable
smp-server/web/Static.hs — SMP server web static files
xftp-server/Main.hs — XFTP server executable
xftp/Main.hs — XFTP CLI executable
ntf-server/Main.hs — Notification server executable
smp-agent/Main.hs — SMP agent (experimental, not in cabal)
```

## Linking Conventions

### spec → src
Fully qualified exported function names inline in prose: `Simplex.Messaging.Client.connectSMPProxiedRelay`. Use Grep/Glob to locate in source. For app targets: `xftp/Main.main`.

### src → spec
Comment above function:
```haskell
-- spec/crypto-tls.md#certificate-chain-validation
-- Validates relay certificate chain to prevent proxy MITM (SI-XX)
connectSMPProxiedRelay :: ...
```

### spec ↔ spec
Named markdown heading anchors: `spec/crypto.md#ed25519-signing`

### spec ↔ product
Cross-references: `product/rules.md#pr-05`, `spec/security-invariants.md#si-01`

### protocol/ references
`protocol/simplex-messaging.md` with section name

## Build Flags

simplexmq builds with several flag combinations:

| Flag | Effect |
|------|--------|
| (none) | Default: SQLite storage, all executables |
| `-fserver_postgres` | Postgres backend for SMP server |
| `-fclient_postgres` | Postgres backend for agent storage |
| `-fclient_library` | Library-only build (no server executables) |
| `-fswift` | Swift JSON format for mobile bindings |
| `-fuse_crypton` | Use crypton in cryptostore |

All flag combinations must compile with `--enable-tests`. Verify with:
```
cabal build all --ghc-options="-O0" [-flags] [--enable-tests]
```

## Change Protocol

Every code change must maintain coherence across all three layers:

1. **Code change** — Implement in src/
2. **Spec update** — Update the relevant spec/ file(s): types, call graphs, security notes
3. **Product update** — If user-visible behavior changed, update product/ files
4. **Protocol update** — If wire protocol changed, amend protocol/ spec (requires user approval)
5. **Security check** — If the change touches a trust boundary, update spec/security-invariants.md

Protocol spec amendments require explicit user approval before committing.
Loading
Loading