memstead_base/ops/transport.rs
1//! Wire types for the three transport ops `Engine::fetch`,
2//! `Engine::pull`, `Engine::push`.
3//!
4//! `branch_reset` lives next to these in the transport surface;
5//! this file pins the success-path payloads each transport op
6//! returns.
7
8use serde::{Deserialize, Serialize};
9
10/// Outcome of `Engine::fetch`. Updates remote-tracking refs without
11/// moving the local branch pointer.
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
13pub struct FetchOutcome {
14 /// Remote name the fetch targeted (verbatim from input).
15 pub remote: String,
16 /// Refspecs that were fetched. Empty in the response means "the
17 /// remote's configured defaults"; otherwise echoes the caller's
18 /// list.
19 pub refspecs: Vec<String>,
20 /// Per-ref tip the fetch landed on, keyed by remote-tracking ref
21 /// (e.g. `refs/remotes/origin/specs` → new SHA). Only refs that
22 /// actually moved appear here; unchanged refs are omitted.
23 pub updated_refs: Vec<UpdatedRef>,
24 /// Check-ledger rows imported from the remote's `__MEMSTEAD_CHECKS` ref
25 /// for the mem, into the workspace ledger; `0` when the remote
26 /// carries none, or none new.
27 #[serde(default)]
28 pub checks_imported: usize,
29 /// Check-ledger rows the import refused, each named: a row whose time
30 /// of the act lies more than five minutes ahead of this engine's
31 /// clock is refused as a local record with such a time is.
32 #[serde(default, skip_serializing_if = "Vec::is_empty")]
33 pub checks_refused: Vec<crate::check::RefusedCheckRow>,
34}
35
36/// One ref's transition recorded by a successful fetch / pull.
37#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
38pub struct UpdatedRef {
39 /// Ref name (e.g. `refs/remotes/origin/specs`).
40 pub ref_name: String,
41 /// SHA the ref pointed at before this op, when known. Empty
42 /// string when the ref did not exist locally before the op.
43 pub previous_sha: String,
44 /// SHA the ref points at after this op.
45 pub new_sha: String,
46}
47
48/// Outcome of `Engine::pull`. Fast-forwards the local branch when
49/// possible; refuses with `LOCAL_DIVERGENCE` on a diverged local
50/// branch.
51#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
52pub struct PullOutcome {
53 /// Mem whose branch was advanced.
54 pub mem: String,
55 /// Remote-tracking ref the fast-forward consumed (e.g.
56 /// `refs/remotes/origin/specs`).
57 pub source_ref: String,
58 /// Local branch ref that was moved (e.g. `refs/heads/specs`).
59 pub branch_ref: String,
60 /// SHA the local branch pointed at before the pull. Empty for a
61 /// fresh branch that did not yet exist locally.
62 pub previous_sha: String,
63 /// SHA the local branch points at after the pull.
64 pub new_sha: String,
65 /// Updated remote-tracking refs the underlying fetch produced.
66 pub updated_refs: Vec<UpdatedRef>,
67 /// Check-ledger rows imported from the remote's `__MEMSTEAD_CHECKS` ref
68 /// for the mem, into the workspace ledger.
69 #[serde(default)]
70 pub checks_imported: usize,
71 /// Check-ledger rows the import refused, each named: a row whose time
72 /// of the act lies more than five minutes ahead of this engine's
73 /// clock is refused as a local record with such a time is.
74 #[serde(default, skip_serializing_if = "Vec::is_empty")]
75 pub checks_refused: Vec<crate::check::RefusedCheckRow>,
76 /// What the pull could not finish in this process: today only a
77 /// store reload that failed after the branch moved (the ref move
78 /// stands; the next operation reloads again). Empty on a clean pull.
79 #[serde(default, skip_serializing_if = "Vec::is_empty")]
80 pub notices: Vec<String>,
81}
82
83/// Outcome of `Engine::push`. The remote's view of the mem's
84/// branch has moved to `new_sha` after the operation.
85#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
86pub struct PushOutcome {
87 /// Mem whose branch was pushed.
88 pub mem: String,
89 /// Remote name the push targeted.
90 pub remote: String,
91 /// Local branch ref that was pushed (e.g. `refs/heads/specs`).
92 pub branch_ref: String,
93 /// SHA the remote acknowledged after the push.
94 pub new_sha: String,
95 /// `true` when the push was a force update (the caller passed
96 /// `force: true` and the underlying ref move was not a
97 /// fast-forward). Consumers warn on this in their UI.
98 pub forced: bool,
99 /// The `__MEMSTEAD_CHECKS` commit the mem's ledger rows were published
100 /// in, when the push moved that ref; `None` when the remote already
101 /// held the same rows.
102 #[serde(default)]
103 pub checks_published: Option<String>,
104 /// Rows of the remote's transport ref this ledger refused on the
105 /// push's import (a time of the act beyond the clock skew ahead of
106 /// this engine's clock), each named; they stay on the remote.
107 #[serde(default, skip_serializing_if = "Vec::is_empty")]
108 pub checks_refused: Vec<crate::check::RefusedCheckRow>,
109}
110
111/// Outcome of `Engine::push_all`: every mounted git-branch mem's
112/// declared branch plus the `__MEMSTEAD` ref of each mem-repo,
113/// pushed fast-forward only. The run never stops at the first
114/// refusal — a ref that cannot move lands in `refused` and the
115/// remaining refs are still attempted, so one diverged branch never
116/// holds back the publication of the others. A ref already at the
117/// remote's SHA is recorded in `in_sync` and not pushed.
118#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
119pub struct PushAllOutcome {
120 /// Remote name the run targeted.
121 pub remote: String,
122 /// Refs the remote now carries at a new SHA, in push order.
123 pub pushed: Vec<PushedRef>,
124 /// Refs whose local and remote SHA already agreed — nothing was
125 /// sent for them.
126 pub in_sync: Vec<String>,
127 /// Refs the run could not move, each with its typed code.
128 pub refused: Vec<RefusedRef>,
129 /// Check-ledger rows the run's import refused, each named; they stay
130 /// on the remote.
131 #[serde(default, skip_serializing_if = "Vec::is_empty")]
132 pub checks_refused: Vec<crate::check::RefusedCheckRow>,
133}
134
135/// One ref `Engine::push_all` moved on the remote.
136#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
137pub struct PushedRef {
138 /// Full ref name (e.g. `refs/heads/specs`, `refs/heads/__MEMSTEAD`).
139 pub ref_name: String,
140 /// Mem the ref belongs to; `None` for the `__MEMSTEAD` ref.
141 pub mem: Option<String>,
142 /// SHA the remote held before the push. Empty when the remote
143 /// did not carry the ref yet.
144 pub previous_sha: String,
145 /// SHA the remote acknowledged after the push.
146 pub new_sha: String,
147}
148
149/// One ref `Engine::push_all` could not move.
150#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
151pub struct RefusedRef {
152 /// Full ref name the refusal is about.
153 pub ref_name: String,
154 /// Mem the ref belongs to; `None` for the `__MEMSTEAD` ref.
155 pub mem: Option<String>,
156 /// Typed refusal code (`NON_FAST_FORWARD`, `LOCAL_INVALID_STATE`,
157 /// `UNKNOWN_REF`, …) — the same vocabulary the single-mem push
158 /// returns as an error.
159 pub code: String,
160 /// The refusal's human message.
161 pub message: String,
162}
163
164/// Outcome of `Engine::pull_all`, the inverse of
165/// [`PushAllOutcome`]: the `__MEMSTEAD` ref of every mem-repo the
166/// workspace mounts from, then every mounted git-branch mem's declared
167/// branch, fast-forwarded to the remote's state. A ref absent locally
168/// counts as a fast-forward (it is created). The run never stops at the
169/// first refusal: a ref that cannot fast-forward lands in `refused`
170/// and the remaining refs are still attempted.
171#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
172pub struct PullAllOutcome {
173 /// Remote name the run targeted.
174 pub remote: String,
175 /// Local refs the run moved, in pull order (the schema ref first).
176 pub pulled: Vec<PulledRef>,
177 /// Refs whose local and remote SHA already agreed.
178 pub in_sync: Vec<String>,
179 /// Refs that already hold the remote's commit plus local commits
180 /// not yet pushed; left alone, never a refusal.
181 pub local_ahead: Vec<String>,
182 /// Refs the run could not move, each with its typed code
183 /// (`LOCAL_DIVERGENCE`, `SCHEMA_VIOLATION_IN_FETCH`,
184 /// `SCHEMA_NOT_FOUND`, …).
185 pub refused: Vec<RefusedRef>,
186 /// Mems that were quarantined because their schema pin did not
187 /// resolve and serve again because the pulled schema ref carries it.
188 pub returned_to_service: Vec<String>,
189 /// Mems still quarantined after the run, each with its reason.
190 /// A mem whose quarantine has another cause is never pulled.
191 pub quarantined: Vec<QuarantinedAfterPull>,
192 /// Facts that are not failures: a mounted branch the remote does
193 /// not carry, a mem-repo whose remote has no schema ref.
194 pub notices: Vec<String>,
195 /// Mems the remote carries (a branch with a config on the pulled
196 /// schema ref) that no mount here names. Never mounted by the run:
197 /// mounting is an explicit `memstead mem init <name> --reattach`.
198 pub unmounted_on_remote: Vec<UnmountedRemoteMem>,
199 /// Check-ledger rows imported for the mounted mems.
200 pub checks_imported: usize,
201 /// Check-ledger rows the import refused, each named: a row whose time
202 /// of the act lies more than five minutes ahead of this engine's
203 /// clock is refused as a local record with such a time is.
204 #[serde(default, skip_serializing_if = "Vec::is_empty")]
205 pub checks_refused: Vec<crate::check::RefusedCheckRow>,
206}
207
208/// A mem on the remote that this workspace does not mount.
209#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
210pub struct UnmountedRemoteMem {
211 /// The branch's name below `refs/heads/`, which is the mem's name
212 /// for a mem created with `memstead mem init`.
213 pub branch: String,
214 /// The schema pin its config names, when it names one.
215 pub schema: Option<String>,
216}
217
218/// One local ref `Engine::pull_all` moved.
219#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
220pub struct PulledRef {
221 /// Full local ref name (e.g. `refs/heads/specs`, `refs/heads/__MEMSTEAD`).
222 pub ref_name: String,
223 /// Mem the ref belongs to; `None` for the `__MEMSTEAD` ref.
224 pub mem: Option<String>,
225 /// SHA the local ref held before the pull. Empty when it did not
226 /// exist locally.
227 pub previous_sha: String,
228 /// SHA the local ref points at after the pull.
229 pub new_sha: String,
230}
231
232/// A mem `Engine::pull_all` left quarantined, with the reason the
233/// engine's quarantine roster carries for it.
234#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
235pub struct QuarantinedAfterPull {
236 /// The quarantined mem.
237 pub mem: String,
238 /// Typed reason code (e.g. `SCHEMA_NOT_FOUND`, `MOUNT_UNBACKED`).
239 pub code: String,
240 /// The reason's message, repair included.
241 pub message: String,
242}
243
244/// Outcome of `Engine::remote_add`. Configures a named remote on the
245/// workspace's mem-repo so `fetch` / `pull` / `push` have somewhere to
246/// go — upsert semantics (re-pointing an existing remote is not an
247/// error; `updated` says which happened).
248#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
249pub struct RemoteAddOutcome {
250 /// Remote name (verbatim from input).
251 pub remote: String,
252 /// URL the remote now points at.
253 pub url: String,
254 /// `true` when the remote already existed and its URL was
255 /// re-pointed; `false` when it was newly added.
256 pub updated: bool,
257}
258
259/// The standing of one remote ref (or one mounted branch the remote lacks)
260/// on `memstead status --remote`.
261#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
262#[serde(rename_all = "snake_case")]
263pub enum RemoteRefState {
264 /// Local and remote heads agree.
265 InSync,
266 /// The remote head is an ancestor of the local head: unpushed local
267 /// work, never staleness.
268 LocalAhead,
269 /// The local head is an ancestor of the remote head: the remote
270 /// carries commits this clone has not seen. Staleness.
271 Behind,
272 /// Neither head is an ancestor of the other. Staleness.
273 Forked,
274 /// The remote head is not in the local object store: the remote
275 /// carries commits this clone has never fetched, so it is behind or
276 /// forked and only a `memstead fetch` can say which. Staleness.
277 Unfetched,
278 /// The remote carries a branch this workspace mounts (or the schemas
279 /// ref) and the local gitdir has no such ref. Staleness.
280 MissingLocal,
281 /// A remote branch nothing mounts (whether or not a local ref of that
282 /// name exists): a probable leftover of a retired or re-homed mem, or
283 /// a branch that is not graph state at all. A notice, never staleness;
284 /// deleting a remote branch is a human decision.
285 UnmountedRemote,
286 /// A mounted branch the remote does not carry yet: never pushed. A
287 /// notice, never staleness.
288 NotOnRemote,
289}
290
291impl RemoteRefState {
292 /// The stable wire label.
293 pub fn as_wire(self) -> &'static str {
294 match self {
295 Self::InSync => "in_sync",
296 Self::LocalAhead => "local_ahead",
297 Self::Behind => "behind",
298 Self::Forked => "forked",
299 Self::Unfetched => "unfetched",
300 Self::MissingLocal => "missing_local",
301 Self::UnmountedRemote => "unmounted_remote",
302 Self::NotOnRemote => "not_on_remote",
303 }
304 }
305
306 /// Whether this state means the local graph lags the remote.
307 pub fn is_stale(self) -> bool {
308 matches!(
309 self,
310 Self::Behind | Self::Forked | Self::Unfetched | Self::MissingLocal
311 )
312 }
313}
314
315/// One ref on the remote-status report.
316#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
317pub struct RemoteRefStatus {
318 /// The full ref name (`refs/heads/<branch>`).
319 pub ref_name: String,
320 /// The mem the branch is mounted as, or `None` for the schemas ref
321 /// and for an unmounted remote branch.
322 #[serde(skip_serializing_if = "Option::is_none")]
323 pub mem: Option<String>,
324 /// `true` for the `__MEMSTEAD` schemas ref.
325 pub schemas_ref: bool,
326 pub state: RemoteRefState,
327 #[serde(skip_serializing_if = "Option::is_none")]
328 pub remote_sha: Option<String>,
329 #[serde(skip_serializing_if = "Option::is_none")]
330 pub local_sha: Option<String>,
331}
332
333/// What `memstead status --remote` reports: every mounted git-branch mem
334/// and the schemas ref compared against the named remote by a read-only
335/// `ls-remote`, plus the notices of what could not be compared. Never a
336/// refusal: a workspace without a remote, or one it cannot reach, reports
337/// the fact as a notice and stands as not stale (fail open).
338#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
339pub struct RemoteStatusOutcome {
340 /// Remote name the comparison targeted.
341 pub remote: String,
342 /// Every ref compared, sorted by name.
343 pub refs: Vec<RemoteRefStatus>,
344 /// Why some or all of the comparison could not run, one line each.
345 pub notices: Vec<String>,
346 /// Counts by state, in wire-label order.
347 pub counts: std::collections::BTreeMap<String, usize>,
348 /// `true` when any ref is behind, forked or missing locally.
349 pub stale: bool,
350}
351
352/// A remote comparison whose refs lag: behind, forked or missing locally.
353/// The refusal a caller gating on staleness raises, after the report.
354#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
355#[error("the local graph lags remote `{remote}`: {}", stale.join(", "))]
356pub struct RemoteStale {
357 pub remote: String,
358 /// Each stale ref as `<ref> (<state>)`.
359 pub stale: Vec<String>,
360 pub counts: std::collections::BTreeMap<String, usize>,
361}
362
363impl RemoteStale {
364 pub fn code(&self) -> &'static str {
365 "REMOTE_STALE"
366 }
367
368 pub fn details(&self) -> serde_json::Value {
369 serde_json::json!({
370 "remote": self.remote,
371 "stale": self.stale,
372 "counts": self.counts,
373 })
374 }
375}
376
377impl RemoteStatusOutcome {
378 /// The staleness refusal, when any ref lags.
379 pub fn stale_refusal(&self) -> Option<RemoteStale> {
380 if !self.stale {
381 return None;
382 }
383 Some(RemoteStale {
384 remote: self.remote.clone(),
385 stale: self
386 .refs
387 .iter()
388 .filter(|x| x.state.is_stale())
389 .map(|x| format!("{} ({})", x.ref_name, x.state.as_wire()))
390 .collect(),
391 counts: self.counts.clone(),
392 })
393 }
394}