Skip to main content

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//! | [`signature`] | not an endpoint: how a device signs every request |
27//! | [`validate`] | the rules both halves enforce |
28//!
29//! # Frozen surface
30//!
31//! **These JSON shapes are frozen.** The deployed Node server speaks them,
32//! its SQLite rows were written against them, and during any migration a
33//! machine on the old client and one on the new binary talk to the same
34//! deployment. Field names and ordering here are compatibility surface, not
35//! style.
36//!
37//! That includes timestamps, which every `updated_at`, `checked_at` and
38//! `last_*_at` field carries as JavaScript's `Date.toISOString()` —
39//! millisecond precision with a `Z` suffix, e.g. `2026-09-03T21:49:55.191Z`.
40//! Rows already in the database are in that shape. `recall_server::now`
41//! produces it.
42//!
43//! The full reference, including status codes and worked `curl` examples,
44//! is in `docs/reference/api.md`.
45
46#![deny(missing_docs)]
47
48pub mod admin;
49pub mod audit;
50pub mod devices;
51pub mod discovery;
52pub mod health;
53pub mod jobs;
54pub mod signature;
55pub mod sync;
56pub mod validate;
57
58pub use admin::{AdminStats, AdminTotals, ProjectStats};
59pub use audit::{AuditCapability, AuditCheckpoint, AuditConsistencyResponse, AuditEntriesResponse};
60pub use devices::{
61    ApproveRequest, Authkey, AuthkeyCreated, AuthkeyList, AuthkeyRequest, AuthkeyRevokeRequest,
62    DenyRequest, DenyResponse, Device, DeviceIdentity, DeviceList, DevicesCapability,
63    EnrollApproved, EnrollPending, EnrollPollRequest, EnrollPollResponse, EnrollRequest,
64    PendingEnrollment,
65};
66pub use discovery::{Discovery, DISCOVERY_PATH, PROTOCOL, PROTOCOL_HEADER};
67pub use health::{ClaudeCliStatus, Health, MergeError, MergeStatus, QueueStatus, WorkerStatus};
68pub use jobs::{
69    ClaimRequest, ClaimResponse, ClaudeCliReport, Job, JobList, JobSummary, MergeInput,
70    MergeResult, MergeSide, ResultRequest, ResultResponse,
71};
72/// The hash a push names its base by: SHA-256 of the file's exact bytes, as
73/// lowercase hex.
74///
75/// Computed on both sides of the wire — by the client over what it last
76/// synced, by the server over what it has stored — so it has to be one
77/// function in one place.
78pub fn content_sha256(content: &str) -> String {
79    use sha2::{Digest, Sha256};
80    let digest = Sha256::digest(content.as_bytes());
81    digest.iter().map(|b| format!("{b:02x}")).collect()
82}
83
84#[cfg(test)]
85mod hash_tests {
86    use super::content_sha256;
87
88    /// Known SHA-256 vectors, so the two sides of the wire cannot drift into
89    /// computing something else under the same name.
90    #[test]
91    fn content_sha256_is_plain_sha256_as_lowercase_hex() {
92        assert_eq!(
93            content_sha256(""),
94            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
95        );
96        assert_eq!(
97            content_sha256("abc"),
98            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
99        );
100    }
101
102    /// Exact bytes: a trailing newline is part of the content, and so part of
103    /// the hash.
104    #[test]
105    fn a_trailing_newline_changes_the_hash() {
106        assert_ne!(content_sha256("a"), content_sha256("a\n"));
107    }
108}
109
110pub use sync::{File, PushRequest, PushResponse, SyncResponse};
111pub use validate::{
112    validate_base_sha256, validate_file_path, validate_project_key, ValidationError,
113    MAX_FILE_PATH_BYTES, MAX_PROJECT_KEY_BYTES,
114};
115
116use serde::{Deserialize, Serialize};
117
118/// Body returned for any non-2xx, on every endpoint.
119///
120/// It lives at the root rather than in an endpoint module because it belongs
121/// to all of them: whatever a request was trying to do, this is the shape of
122/// being told no.
123#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
124pub struct ErrorResponse {
125    /// A human-readable reason, safe to show a user.
126    pub error: String,
127}