recall_wire/lib.rs
1//! The contract between Recall's client and server: request and response
2//! shapes, and the validation rules that apply to both.
3//!
4//! Keeping this in one crate is the point of a workspace rather than two
5//! programs. Before the implementations were unified, `file_path`
6//! validation and the tombstone/empty-file distinction existed twice — in
7//! JavaScript on the server and in bash on the client — with nothing
8//! keeping them in agreement, so a drift between them would only surface in
9//! production.
10//!
11//! # Layout
12//!
13//! One module per endpoint, since that is the unit that has to stay
14//! compatible. Every type is also re-exported at the crate root, so callers
15//! can write `recall_wire::PushRequest` and never think about which endpoint
16//! a type belongs to.
17//!
18//! | Module | Endpoint |
19//! |---|---|
20//! | [`sync`] | `POST /sync`, `GET /sync` — memory files in both directions |
21//! | [`health`] | `GET /health` — unauthenticated liveness and merge status |
22//! | [`admin`] | `GET /admin/stats` — what is stored, per project |
23//! | [`discovery`] | `GET /.well-known/recall` — what the server is and speaks |
24//! | [`devices`] | `/v1/devices`, `/v1/authkeys` — enrolling and managing devices |
25//! | [`jobs`] | `/v1/jobs` — the merge queue a worker drains |
26//! | [`audit`] | `/v1/audit/*`: the log's checkpoint, leaves and proofs, the tree they hash to, and checking an export offline |
27//! | [`signature`] | not an endpoint: how a device signs every request |
28//! | [`validate`] | the rules both halves enforce |
29//!
30//! # Frozen surface
31//!
32//! **These JSON shapes are frozen.** The deployed Node server speaks them,
33//! its SQLite rows were written against them, and during any migration a
34//! machine on the old client and one on the new binary talk to the same
35//! deployment. Field names and ordering here are compatibility surface, not
36//! style.
37//!
38//! That includes timestamps, which every `updated_at`, `checked_at` and
39//! `last_*_at` field carries as JavaScript's `Date.toISOString()` —
40//! millisecond precision with a `Z` suffix, e.g. `2026-09-03T21:49:55.191Z`.
41//! Rows already in the database are in that shape. `recall_server::now`
42//! produces it.
43//!
44//! The full reference, including status codes and worked `curl` examples,
45//! is in `docs/reference/api.md`.
46
47#![deny(missing_docs)]
48
49pub mod admin;
50pub mod audit;
51pub mod devices;
52pub mod discovery;
53pub mod health;
54pub mod jobs;
55pub mod signature;
56pub mod sync;
57pub mod validate;
58
59pub use admin::{AdminStats, AdminTotals, ProjectStats};
60pub use audit::{AuditCapability, AuditCheckpoint, AuditConsistencyResponse, AuditEntriesResponse};
61pub use devices::{
62 ApproveRequest, Authkey, AuthkeyCreated, AuthkeyList, AuthkeyRequest, AuthkeyRevokeRequest,
63 DenyRequest, DenyResponse, Device, DeviceIdentity, DeviceList, DevicesCapability,
64 EnrollApproved, EnrollPending, EnrollPollRequest, EnrollPollResponse, EnrollRequest,
65 PendingEnrollment,
66};
67pub use discovery::{Discovery, DISCOVERY_PATH, PROTOCOL, PROTOCOL_HEADER};
68pub use health::{ClaudeCliStatus, Health, MergeError, MergeStatus, QueueStatus, WorkerStatus};
69pub use jobs::{
70 ClaimRequest, ClaimResponse, ClaudeCliReport, Job, JobList, JobSummary, MergeInput,
71 MergeResult, MergeSide, ResultRequest, ResultResponse,
72};
73/// The hash a push names its base by: SHA-256 of the file's exact bytes, as
74/// lowercase hex.
75///
76/// Computed on both sides of the wire — by the client over what it last
77/// synced, by the server over what it has stored — so it has to be one
78/// function in one place.
79pub fn content_sha256(content: &str) -> String {
80 use sha2::{Digest, Sha256};
81 let digest = Sha256::digest(content.as_bytes());
82 digest.iter().map(|b| format!("{b:02x}")).collect()
83}
84
85#[cfg(test)]
86mod hash_tests {
87 use super::content_sha256;
88
89 /// Known SHA-256 vectors, so the two sides of the wire cannot drift into
90 /// computing something else under the same name.
91 #[test]
92 fn content_sha256_is_plain_sha256_as_lowercase_hex() {
93 assert_eq!(
94 content_sha256(""),
95 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
96 );
97 assert_eq!(
98 content_sha256("abc"),
99 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
100 );
101 }
102
103 /// Exact bytes: a trailing newline is part of the content, and so part of
104 /// the hash.
105 #[test]
106 fn a_trailing_newline_changes_the_hash() {
107 assert_ne!(content_sha256("a"), content_sha256("a\n"));
108 }
109}
110
111pub use sync::{File, PushRequest, PushResponse, SyncResponse};
112pub use validate::{
113 validate_base_sha256, validate_file_path, validate_project_key, ValidationError,
114 MAX_FILE_PATH_BYTES, MAX_PROJECT_KEY_BYTES,
115};
116
117use serde::{Deserialize, Serialize};
118
119/// Body returned for any non-2xx, on every endpoint.
120///
121/// It lives at the root rather than in an endpoint module because it belongs
122/// to all of them: whatever a request was trying to do, this is the shape of
123/// being told no.
124#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
125pub struct ErrorResponse {
126 /// A human-readable reason, safe to show a user.
127 pub error: String,
128}