Skip to main content

flint_lean/
sentinel.rs

1//! The boundary verbs: `.flint/publish` and `.flint/sync` (plan §2.1,
2//! §2.2 — D1, D2, D3, D3.1, D12).
3//!
4//! The agent touches a file; the syncer consumes it, honors it with a
5//! real barrier (or a real sync), and answers with an ack. The
6//! discipline is `consume_inbox`'s, mirrored — exactly-once via
7//! integrate → persist → drop, idempotent between — with rename in
8//! place of CAS:
9//!
10//! 1. **Consume** — the sentinel is renamed out of the agent's reach
11//!    into the state dir. Type-checked first (`S_ISREG` only: a FIFO
12//!    would block the body read forever), body bounded at 64 KiB.
13//!    **Settle-before-consume:** the poll never consumes while a
14//!    pending record stands — a surviving pending must be honored,
15//!    acked and retired first, or the consume would clobber it and
16//!    orphan its nonces forever. Coalescing happens INSIDE the pending
17//!    record: touches arriving during the min-interval wait append
18//!    their nonces to the standing record.
19//! 2. **Honor** — run one full fused barrier (which claims the publish
20//!    fence for its commit section only) or one sync.
21//! 3. **Ack** — written atomically AFTER the barrier's manifest CAS and
22//!    baseline rewrite, carrying the FULL covered-nonce set: under
23//!    coalescing an agent whose nonce rode behind a later touch would
24//!    otherwise never see it and would re-touch in a loop, feeding the
25//!    storm the rate limit exists to prevent.
26//! 4. **Retire** — the pending record is removed after the ack rename.
27//!
28//! **The uniform crash rule (D2, replacing the draft's per-crash-point
29//! matrix).** Pending-present-and-no-matching-ack is the SAME
30//! observable state for "crashed before the manifest CAS" and "crashed
31//! after step 7" — the baseline is rewritten only at step 7 — so acking
32//! from persisted state would assert publication of writes that never
33//! uploaded. On restart: pending + no matching ack ⇒ ALWAYS run a full
34//! barrier (idempotent; AdoptOwn/recent-uuids covers a half-published
35//! set), then ack with THAT barrier's installed seq. Pending + matching
36//! ack ⇒ retire only; the ack already names a real install.
37//!
38//! **A fence is a retry (design 2026-09-13 §4).** The lease is held per
39//! barrier, so a commit section deposed mid-way abandons THAT barrier:
40//! nothing was installed, the pending record stands, and the next tick
41//! honors it with a fresh claim. There is no refused-fenced ack and no
42//! fenced marker any more — the life-long lease they answered for is
43//! gone.
44
45use std::path::{Path, PathBuf};
46
47use serde::{Deserialize, Serialize};
48
49use super::control::{self, write_atomic};
50use super::state::ConflictRecord;
51use super::{now_unix, LeanError, LeanResult, Syncer};
52
53/// Bound on a sentinel body read. A larger file is truncated and the
54/// remainder ignored — never a wedge.
55const MAX_BODY: u64 = 64 * 1024;
56
57/// Bound on the covered-nonce set carried in one ack (oldest dropped).
58const MAX_NONCES: usize = 32;
59
60/// Bound on a scoped sync's entry list (§2.2 write containment).
61pub const MAX_SCOPE_ENTRIES: usize = 64;
62pub const MAX_SCOPE_ENTRY_LEN: usize = 1024;
63
64const BUDGET: &str = "sentinel-budget.json";
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub enum Verb {
68    Publish,
69    Sync,
70}
71
72impl Verb {
73    pub fn as_str(&self) -> &'static str {
74        match self {
75            Verb::Publish => "publish",
76            Verb::Sync => "sync",
77        }
78    }
79    pub fn sentinel_name(&self) -> &'static str {
80        match self {
81            Verb::Publish => control::PUBLISH,
82            Verb::Sync => control::SYNC,
83        }
84    }
85    fn ack_name(&self) -> &'static str {
86        match self {
87            Verb::Publish => control::PUBLISH_ACK,
88            Verb::Sync => control::SYNC_ACK,
89        }
90    }
91    /// The standing pending record. `pub` because `flint-sync status`
92    /// reports on it and MUST NOT spell it a second time: it did, it
93    /// spelled it wrong ("pending-publish.json"), and the field that
94    /// answers "is my agent blocked on an ack?" therefore answered no
95    /// forever.
96    pub fn pending_name(&self) -> &'static str {
97        match self {
98            Verb::Publish => "publish.pending.json",
99            Verb::Sync => "sync.pending.json",
100        }
101    }
102    /// The consume staging name. A crash between the rename (which
103    /// removes the sentinel) and the pending write would otherwise lose
104    /// the touch: on restart the staging file is recovered into the
105    /// pending record instead.
106    fn staging_name(&self) -> &'static str {
107        match self {
108            Verb::Publish => "publish.consumed",
109            Verb::Sync => "sync.consumed",
110        }
111    }
112}
113
114/// The agent's optional JSON body.
115#[derive(Debug, Clone, Default, Deserialize)]
116struct SentinelBody {
117    #[serde(default)]
118    nonce: Option<String>,
119    #[serde(default)]
120    note: Option<String>,
121    /// `sync` only: path prefixes and exact paths (D4).
122    #[serde(default)]
123    scope: Option<Vec<String>>,
124}
125
126/// A consumed sentinel awaiting honor (`.flint-sync/<verb>.pending.json`).
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct PendingSentinel {
129    pub verb: String,
130    /// The latest covered touch, in nanoseconds — bare-touch agents
131    /// match their boundary on this.
132    pub consumed_mtime_unix_ns: u128,
133    pub consumed_at: u64,
134    /// EVERY coalesced nonce, oldest dropped past `MAX_NONCES`.
135    pub nonces: Vec<String>,
136    #[serde(default)]
137    pub note: Option<String>,
138    #[serde(default)]
139    pub scope: Option<Vec<String>>,
140    /// An unparsable or oversize body was honored as a bare touch — a
141    /// warning conflict record names it. Never a wedge, never a silent
142    /// drop.
143    #[serde(default)]
144    pub torn: bool,
145    /// A coalesced touch asked for the WHOLE tree (no `scope`); later
146    /// scoped touches must not narrow it (review 2026-09-12, inbox-7).
147    #[serde(default)]
148    pub whole_tree: bool,
149}
150
151/// The ack document (`.flint/<verb>.ack`).
152#[derive(Debug, Clone, Serialize, Deserialize)]
153pub struct Ack {
154    /// "ok" | "partial" | "refused-scope" | "refused-read-only".
155    ///
156    /// `partial` is the honest answer when the boundary installed but a
157    /// path the agent declared is not in it (D1) —
158    /// `report.dropped` names them. An agent that treats it as failure
159    /// and re-touches is behaving correctly.
160    pub status: String,
161    pub nonces: Vec<String>,
162    pub sentinel_mtime_unix_ns: u128,
163    #[serde(skip_serializing_if = "Option::is_none")]
164    pub seq: Option<u64>,
165    #[serde(skip_serializing_if = "Option::is_none")]
166    pub manifest_etag: Option<String>,
167    /// "sentinel" | "sentinel-deferred" | "drain" | "recovered".
168    pub boundary: String,
169    pub completed_unix: u64,
170    /// Set on a refusal (`refused-scope`, `refused-read-only`): what was
171    /// wrong with the request, or why it cannot be done here, in words.
172    #[serde(default, skip_serializing_if = "Option::is_none")]
173    pub reason: Option<String>,
174    pub report: AckReport,
175}
176
177#[derive(Debug, Clone, Default, Serialize, Deserialize)]
178pub struct AckReport {
179    pub uploaded: usize,
180    pub deleted: usize,
181    pub parked: usize,
182    pub consumed: usize,
183    pub no_change: bool,
184    /// `sync` only: the applied/deleted/conflict transport (§2.2 — the
185    /// conflict report rides the ack in FULL: "never a silent winner"
186    /// must survive the file transport).
187    #[serde(default, skip_serializing_if = "Vec::is_empty")]
188    pub applied: Vec<String>,
189    #[serde(default, skip_serializing_if = "Vec::is_empty")]
190    pub conflicts: Vec<ConflictRecord>,
191    #[serde(default, skip_serializing_if = "Option::is_none")]
192    pub scope: Option<Vec<String>>,
193    /// Foreign changes seen but deferred to the inbox flow (D4).
194    #[serde(default)]
195    pub out_of_scope_foreign: usize,
196    /// Declared paths the boundary does NOT carry (a standing park, a
197    /// delete another writer's edit outranked, or an upload that
198    /// published nothing).
199    /// Non-empty ⇒ `status: "partial"`: the §2.2 rule — the
200    /// conflict report rides the ack in full, never a silent loser —
201    /// generalized from `sync` to the publish verb.
202    #[serde(default, skip_serializing_if = "Vec::is_empty")]
203    pub dropped: Vec<String>,
204}
205
206/// The work meter (D3.1 — the hot-loops no-regression rule).
207///
208/// A counted budget charges a hot 2 GiB checkpoint the same one unit as
209/// a 4 KiB file, so a storming agent could drive `dirty_bytes × 60/hour`
210/// of extra upload while staying inside a green budget — a regression on
211/// the one performance sub-axis lean currently maxes (a tight local
212/// loop's republish is coalesced by cadence; sentinels un-coalesce it on
213/// demand). Metering work instead bounds sentinel-driven published bytes
214/// at `budget × whole_put_max` per hour independent of dirty-set size,
215/// and a workspace at the cap degrades to exactly today's cadence
216/// behavior — the definition of no regression.
217#[derive(Debug, Clone, Default, Serialize, Deserialize)]
218pub struct SentinelBudget {
219    /// (unix, units) charges inside the rolling hour.
220    pub charges: Vec<(u64, u64)>,
221    /// The last sentinel-honoring barrier, for the min-interval.
222    pub last_honor_unix: u64,
223}
224
225impl SentinelBudget {
226    fn prune(&mut self, now: u64) {
227        self.charges.retain(|(at, _)| now.saturating_sub(*at) < 3600);
228    }
229    pub fn spent(&self, now: u64) -> u64 {
230        self.charges
231            .iter()
232            .filter(|(at, _)| now.saturating_sub(*at) < 3600)
233            .map(|(_, u)| *u)
234            .sum()
235    }
236    pub fn remaining(&self, now: u64, budget: u64) -> u64 {
237        budget.saturating_sub(self.spent(now))
238    }
239}
240
241/// Why a standing pending sentinel is not being honored right now.
242/// The records a sync produced, given how many the log held before it.
243///
244/// `skip(before)` alone is only sound while the log GREW. A rotation
245/// during the sync can SHORTEN it, and skipping past a shorter list
246/// reports NO conflicts for a sync that produced some — the one answer
247/// this ack must never give. Over-reporting the survivors is the safe
248/// direction: an ack naming a conflict that predates the sync costs a
249/// reader a second look, an ack naming none costs them the conflict.
250pub(crate) fn conflicts_since(
251    before: usize,
252    after: Vec<ConflictRecord>,
253) -> Vec<ConflictRecord> {
254    if after.len() >= before {
255        after[before..].to_vec()
256    } else {
257        after
258    }
259}
260
261#[derive(Debug, Clone, Copy, PartialEq, Eq)]
262pub enum Due {
263    /// Honor it on this tick.
264    Ready,
265    /// Inside `sentinel_min_interval_secs` of the last honor: wait, and
266    /// let further touches coalesce.
267    MinInterval,
268    /// The hourly budget is exhausted: the boundary is still honored,
269    /// by the next FLOOR tick, and its ack is stamped
270    /// `sentinel-deferred`. Contents are never thinned (D1's corollary)
271    /// — cost is bounded by deferring the barrier, never by publishing
272    /// less than the boundary covers.
273    BudgetDeferred,
274}
275
276fn mtime_ns(m: &std::fs::Metadata) -> u128 {
277    m.modified()
278        .ok()
279        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
280        .map(|d| d.as_nanos())
281        .unwrap_or(0)
282}
283
284/// Read the sentinel body, bounded, and WITHOUT ever blocking on the
285/// open.
286///
287/// The type check in `consume_sentinel` is an `lstat`, and this is a
288/// second path resolution — so on its own the check is a TOCTOU: swap a
289/// FIFO in between and a plain `File::open` blocks forever waiting for a
290/// writer, wedging the poll arm and, behind it, every boundary this
291/// syncer owes (review: U23).
292///
293/// `O_NONBLOCK` closes it at the syscall rather than by winning a race:
294/// opening a writer-less FIFO returns immediately instead of blocking,
295/// and `O_NOFOLLOW` refuses a symlink swapped in for the same purpose.
296/// Both are no-ops on the regular file this is supposed to be reading.
297pub(crate) fn read_bounded(path: &Path) -> std::io::Result<(Vec<u8>, bool)> {
298    use std::io::Read;
299    use std::os::unix::fs::OpenOptionsExt;
300    let f = std::fs::File::options()
301        .read(true)
302        .custom_flags(libc::O_NONBLOCK | libc::O_NOFOLLOW)
303        .open(path)?;
304    // A FIFO that a writer DOES hold open reads EAGAIN rather than
305    // blocking; treat it as an empty body, which the torn-body rule
306    // already handles as a bare touch.
307    let meta = f.metadata()?;
308    if !meta.is_file() {
309        return Err(std::io::Error::new(
310            std::io::ErrorKind::InvalidInput,
311            "sentinel is not a regular file",
312        ));
313    }
314    let mut buf = Vec::new();
315    let mut take = f.take(MAX_BODY + 1);
316    take.read_to_end(&mut buf)?;
317    let oversize = buf.len() as u64 > MAX_BODY;
318    buf.truncate(MAX_BODY as usize);
319    Ok((buf, oversize))
320}
321
322impl Syncer {
323    fn pending_path(&self, verb: Verb) -> PathBuf {
324        self.cfg.state_dir().join(verb.pending_name())
325    }
326    fn staging_path(&self, verb: Verb) -> PathBuf {
327        self.cfg.state_dir().join(verb.staging_name())
328    }
329    fn budget_path(&self) -> PathBuf {
330        self.cfg.state_dir().join(BUDGET)
331    }
332
333    pub fn load_pending(&self, verb: Verb) -> LeanResult<Option<PendingSentinel>> {
334        let p = self.pending_path(verb);
335        if !p.exists() {
336            return Ok(None);
337        }
338        let bytes = std::fs::read(&p)?;
339        match serde_json::from_slice(&bytes) {
340            Ok(v) => Ok(Some(v)),
341            // Our own record is torn (a crash mid-rewrite): honor it as
342            // a bare touch rather than wedging on it.
343            Err(_) => Ok(Some(PendingSentinel {
344                verb: verb.as_str().into(),
345                consumed_mtime_unix_ns: 0,
346                consumed_at: now_unix(),
347                nonces: vec![],
348                note: None,
349                scope: None,
350                torn: true,
351                whole_tree: false,
352            })),
353        }
354    }
355
356    fn save_pending(&self, verb: Verb, p: &PendingSentinel) -> LeanResult<()> {
357        let bytes = serde_json::to_vec_pretty(p)
358            .map_err(|e| LeanError::State(format!("pending: {e}")))?;
359        write_atomic(&self.pending_path(verb), &bytes)
360    }
361
362    fn retire_pending(&self, verb: Verb) -> LeanResult<()> {
363        let p = self.pending_path(verb);
364        if p.exists() {
365            std::fs::remove_file(&p)?;
366        }
367        Ok(())
368    }
369
370    pub fn load_budget(&self) -> LeanResult<SentinelBudget> {
371        let p = self.budget_path();
372        if !p.exists() {
373            return Ok(SentinelBudget::default());
374        }
375        let bytes = std::fs::read(&p)?;
376        Ok(serde_json::from_slice(&bytes).unwrap_or_default())
377    }
378
379    fn save_budget(&self, b: &SentinelBudget) -> LeanResult<()> {
380        let bytes =
381            serde_json::to_vec_pretty(b).map_err(|e| LeanError::State(format!("budget: {e}")))?;
382        write_atomic(&self.budget_path(), &bytes)
383    }
384
385    /// D3.1: charge the meter for a completed sentinel honor.
386    /// `published_bytes == 0` (a no-diff honor) costs NOTHING — the
387    /// budget exists to bound work and a no-diff honor does none; the
388    /// min-interval remains its only bound.
389    pub fn charge_budget(&self, published_bytes: u64) -> LeanResult<u64> {
390        let now = now_unix();
391        let mut b = self.load_budget()?;
392        b.prune(now);
393        let units = if published_bytes == 0 {
394            0
395        } else {
396            published_bytes.div_ceil(self.cfg.whole_put_max.max(1)).max(1)
397        };
398        if units > 0 {
399            b.charges.push((now, units));
400        }
401        b.last_honor_unix = now;
402        self.save_budget(&b)?;
403        Ok(units)
404    }
405
406    /// Whether a standing pending sentinel may be honored on this tick.
407    pub fn sentinel_due(&self) -> LeanResult<Due> {
408        let now = now_unix();
409        let b = self.load_budget()?;
410        if b.last_honor_unix > 0
411            && now.saturating_sub(b.last_honor_unix) < self.cfg.sentinel_min_interval_secs
412        {
413            return Ok(Due::MinInterval);
414        }
415        if b.remaining(now, self.cfg.sentinel_hourly_budget) == 0 {
416            return Ok(Due::BudgetDeferred);
417        }
418        Ok(Due::Ready)
419    }
420
421    /// One poll tick: `lstat` exactly two fixed paths — no inotify
422    /// dependency, no directory scan, ~2 lstats/s when idle.
423    ///
424    /// Returns the verbs whose sentinel was consumed (or coalesced) on
425    /// this tick.
426    pub fn poll_sentinels(&mut self) -> LeanResult<Vec<Verb>> {
427        let mut consumed = vec![];
428        for verb in [Verb::Publish, Verb::Sync] {
429            if self.consume_sentinel(verb)? {
430                consumed.push(verb);
431            }
432        }
433        Ok(consumed)
434    }
435
436    /// Recover a consume that crashed between the rename and the
437    /// pending write. Called at startup, before the first poll.
438    pub fn recover_consume_staging(&mut self) -> LeanResult<()> {
439        for verb in [Verb::Publish, Verb::Sync] {
440            let staging = self.staging_path(verb);
441            if staging.exists() {
442                let meta = std::fs::metadata(&staging)?;
443                let (body, oversize) = read_bounded(&staging)?;
444                self.fold_into_pending(verb, mtime_ns(&meta), &body, oversize)?;
445                std::fs::remove_file(&staging)?;
446            }
447        }
448        Ok(())
449    }
450
451    pub(crate) fn consume_sentinel(&mut self, verb: Verb) -> LeanResult<bool> {
452        let path = self.control_path(verb.sentinel_name());
453        let meta = match std::fs::symlink_metadata(&path) {
454            Ok(m) => m,
455            Err(_) => return Ok(false),
456        };
457        // Type check FIRST: a FIFO would block the body read forever;
458        // a directory, socket or symlink at the sentinel path is not a
459        // touch this syncer will act on.
460        if !meta.is_file() {
461            // ONCE per process, not once per poll tick. A FIFO or dir
462            // parked at the sentinel path is a standing condition, and
463            // appending a record every 10 s grows a file that
464            // `load_conflicts` parses WHOLE — twice per sync honor, and
465            // again per status read and per scrape (review: U23). The
466            // condition is worth recording; the repetition is not.
467            let slot = format!("{}/{}", super::CONTROL_DIR, verb.sentinel_name());
468            if self.noted_not_regular.insert(slot.clone()) {
469                self.state.append_conflict(&ConflictRecord {
470                    path: slot,
471                    foreign_etag: String::new(),
472                    preserved_key: None,
473                    kind: "sentinel-not-regular-file".into(),
474                    at_unix: now_unix(),
475                })?;
476            }
477            return Ok(false);
478        }
479        let ns = mtime_ns(&meta);
480        let (body, oversize) = match read_bounded(&path) {
481            Ok(v) => v,
482            Err(_) => (Vec::new(), false),
483        };
484        // The consume act: rename the sentinel out of the agent's reach.
485        // Staging first, pending second — a crash between is recovered
486        // at startup from the staging file, so a touch is never lost
487        // ACROSS A RESTART.
488        //
489        // Scope worth naming (review: U37): recovery runs ONLY at
490        // startup (`settle_pending_at_startup`, called once before the
491        // run loop). A *transient* fold failure mid-run therefore leaves
492        // a consumed touch stranded in the staging file until the
493        // process restarts, and the next consume's rename clobbers it,
494        // orphaning its nonce. Recovering in the poll arm as well would
495        // close it; nothing does today.
496        self.noted_not_regular.remove(&format!("{}/{}", super::CONTROL_DIR, verb.sentinel_name()));
497        let staging = self.staging_path(verb);
498        std::fs::rename(&path, &staging)?;
499        self.fold_into_pending(verb, ns, &body, oversize)?;
500        let _ = std::fs::remove_file(&staging);
501        Ok(true)
502    }
503
504    /// Build or coalesce into the standing pending record.
505    ///
506    /// Settle-before-consume in its positive form: an existing pending
507    /// record is never overwritten. Its nonce set grows, its covered
508    /// mtime advances to the latest touch, and its scope (sync) unions —
509    /// so the honor that eventually runs covers every coalesced touch,
510    /// and every one of their nonces appears in the ack.
511    fn fold_into_pending(
512        &mut self,
513        verb: Verb,
514        ns: u128,
515        body: &[u8],
516        oversize: bool,
517    ) -> LeanResult<()> {
518        let trimmed = body.iter().all(|b| b.is_ascii_whitespace());
519        let (parsed, torn) = if trimmed {
520            (SentinelBody::default(), false) // bare touch
521        } else if oversize {
522            (SentinelBody::default(), true)
523        } else {
524            match serde_json::from_slice::<SentinelBody>(body) {
525                Ok(v) => (v, false),
526                Err(_) => (SentinelBody::default(), true),
527            }
528        };
529        if torn {
530            self.state.append_conflict(&ConflictRecord {
531                path: format!("{}/{}", super::CONTROL_DIR, verb.sentinel_name()),
532                foreign_etag: String::new(),
533                preserved_key: None,
534                kind: "sentinel-torn-body".into(),
535                at_unix: now_unix(),
536            })?;
537        }
538        let mut pending = self.load_pending(verb)?.unwrap_or(PendingSentinel {
539            verb: verb.as_str().into(),
540            consumed_mtime_unix_ns: 0,
541            consumed_at: now_unix(),
542            nonces: vec![],
543            note: None,
544            scope: None,
545            torn: false,
546            whole_tree: false,
547        });
548        pending.consumed_mtime_unix_ns = pending.consumed_mtime_unix_ns.max(ns);
549        pending.consumed_at = now_unix();
550        pending.torn |= torn;
551        if let Some(n) = parsed.nonce {
552            let n: String = n.chars().take(128).collect();
553            if !pending.nonces.contains(&n) {
554                pending.nonces.push(n);
555            }
556            let excess = pending.nonces.len().saturating_sub(MAX_NONCES);
557            if excess > 0 {
558                pending.nonces.drain(..excess);
559            }
560        }
561        if let Some(note) = parsed.note {
562            pending.note = Some(note.chars().take(4096).collect());
563        }
564        // Scope folding (review 2026-09-12, inbox-7 / inbox-10): an
565        // UNSCOPED sync touch means the whole tree and outranks every
566        // scoped one it coalesces with, in either order — the whole-tree
567        // agent matches its nonce on this ack and must get what it asked
568        // for. Scoped touches union, and the union is NOT truncated
569        // here: an oversize or all-invalid scope is answered at the
570        // honor as `refused-scope`, never silently narrowed.
571        match parsed.scope {
572            None if matches!(verb, Verb::Sync) => {
573                pending.scope = None;
574                pending.whole_tree = true;
575            }
576            Some(scope) if !pending.whole_tree => {
577                let mut merged = pending.scope.take().unwrap_or_default();
578                for e in scope {
579                    if e.len() > MAX_SCOPE_ENTRY_LEN {
580                        continue;
581                    }
582                    if !merged.contains(&e) {
583                        merged.push(e);
584                    }
585                }
586                pending.scope = Some(merged);
587            }
588            _ => {}
589        }
590        self.save_pending(verb, &pending)?;
591        Ok(())
592    }
593
594    /// A boundary asked for through a door that is NOT the file
595    /// protocol — the gateway's inbox field (§2.5) or the UDS socket.
596    ///
597    /// It folds into the same pending record a `.flint/publish` touch
598    /// would, which is the whole design of the layered doors: the extra
599    /// doors are sugar over one consume path, so min-interval,
600    /// coalescing, the work-metered budget, the covered-nonce ack and
601    /// the crash rules apply to them without a second implementation
602    /// that could disagree with the first.
603    pub fn request_boundary(&mut self, nonce: &str, note: Option<String>) -> LeanResult<()> {
604        let body = serde_json::json!({ "nonce": nonce, "note": note });
605        let ns = std::time::SystemTime::now()
606            .duration_since(std::time::UNIX_EPOCH)
607            .map(|d| d.as_nanos())
608            .unwrap_or(0);
609        self.fold_into_pending(Verb::Publish, ns, body.to_string().as_bytes(), false)
610    }
611
612    fn ack_path(&self, verb: Verb) -> PathBuf {
613        self.control_path(verb.ack_name())
614    }
615
616    pub fn read_ack(&self, verb: Verb) -> Option<Ack> {
617        let bytes = std::fs::read(self.ack_path(verb)).ok()?;
618        serde_json::from_slice(&bytes).ok()
619    }
620
621    fn write_ack(&self, verb: Verb, ack: &Ack) -> LeanResult<()> {
622        let bytes =
623            serde_json::to_vec_pretty(ack).map_err(|e| LeanError::State(format!("ack: {e}")))?;
624        write_atomic(&self.ack_path(verb), &bytes)?;
625        self.trace("ack", serde_json::json!({"verb": format!("{verb:?}"), "nonces": ack.nonces, "status": ack.status,
626            "seq": ack.seq, "dropped": ack.report.dropped, "boundary": ack.boundary}));
627        Ok(())
628    }
629
630    /// Does a standing ack already answer this pending record? Used by
631    /// the restart rule: matching ⇒ retire only (the ack names a real
632    /// install); not matching ⇒ run a full barrier and ack from THAT.
633    pub(crate) fn ack_matches(&self, verb: Verb, pending: &PendingSentinel) -> bool {
634        let Some(ack) = self.read_ack(verb) else { return false };
635        // Compare values the SYNCER minted (review: U22). The ack is an
636        // ordinary file in `.flint/`, writable by every process sharing
637        // the mount, and `sentinel_mtime_unix_ns` is the agent's own
638        // file mtime — which is not monotone even without an adversary:
639        // `touch -t`, a clock step, a restored file and a tar extract
640        // all move it backwards. Deciding "has this boundary already
641        // run?" from it lets a STALE ack retire a FRESH request, and
642        // the request is then retired having never run.
643        //
644        // `consumed_at` and `completed_unix` are both minted here, in
645        // that order, so their ordering answers the question the mtime
646        // was being asked.
647        if ack.completed_unix < pending.consumed_at {
648            return false;
649        }
650        if ack.sentinel_mtime_unix_ns < pending.consumed_mtime_unix_ns {
651            return false;
652        }
653        // A bare touch carries no nonce, so `all()` over an empty set is
654        // vacuously TRUE and the timestamps become the entire test.
655        // Require the ack to name this exact touch instead.
656        if pending.nonces.is_empty() {
657            return ack.sentinel_mtime_unix_ns == pending.consumed_mtime_unix_ns;
658        }
659        pending.nonces.iter().all(|n| ack.nonces.contains(n))
660    }
661
662    /// Honor a standing pending sentinel, if one stands and is due.
663    ///
664    /// `forced` = this is a floor tick picking up a budget-deferred
665    /// boundary; the ack is stamped `sentinel-deferred`. `Ok(None)`
666    /// means nothing was owed (or it is not due yet).
667    pub async fn honor_pending(&mut self, verb: Verb, forced: bool) -> LeanResult<Option<Ack>> {
668        self.honor_pending_as(verb, forced, None).await
669    }
670
671    /// `honor_pending` under an explicit provenance stamp. The drain
672    /// needs it: it rewrites the ack to `drain`, and a manifest still
673    /// stamped `sentinel-deferred` would have the bucket and the ack
674    /// naming two different clocks for one boundary.
675    pub async fn honor_pending_as(
676        &mut self,
677        verb: Verb,
678        forced: bool,
679        source: Option<&str>,
680    ) -> LeanResult<Option<Ack>> {
681        let Some(pending) = self.load_pending(verb)? else { return Ok(None) };
682        if !forced {
683            match self.sentinel_due()? {
684                Due::Ready => {}
685                // Not honored on this tick; the floor tick will (D3).
686                Due::MinInterval | Due::BudgetDeferred => return Ok(None),
687            }
688        }
689        // The restart rule: a matching ack already names a real
690        // install — retire, never re-run.
691        if self.ack_matches(verb, &pending) {
692            self.retire_pending(verb)?;
693            return Ok(None);
694        }
695
696        let ack = match verb {
697            Verb::Publish => self.honor_publish(&pending, forced, source).await,
698            Verb::Sync => self.honor_sync(&pending, forced).await,
699        };
700        match ack {
701            Ok(ack) => {
702                self.write_ack(verb, &ack)?;
703                self.retire_pending(verb)?;
704                Ok(Some(ack))
705            }
706            Err(e) => Err(e),
707        }
708    }
709
710    async fn honor_publish(
711        &mut self,
712        pending: &PendingSentinel,
713        forced: bool,
714        source: Option<&str>,
715    ) -> LeanResult<Ack> {
716        // A reader answers — "nothing is silent" — and publishes nothing.
717        // No budget charge: nothing was published, and a refusal must
718        // not hold back the next sync behind the min-interval.
719        if self.cfg.access.is_read() {
720            return Ok(Ack {
721                status: "refused-read-only".into(),
722                nonces: pending.nonces.clone(),
723                sentinel_mtime_unix_ns: pending.consumed_mtime_unix_ns,
724                seq: None,
725                manifest_etag: None,
726                boundary: if forced { "sentinel-deferred".into() } else { "sentinel".into() },
727                completed_unix: now_unix(),
728                reason: Some(
729                    "this pod has read-only access to the workspace (capabilities `access: read`): \
730                     nothing in its tree is published. `sync` still integrates the bucket's latest \
731                     boundary"
732                        .into(),
733                ),
734                report: AckReport::default(),
735            });
736        }
737        // The DECLARED form (D1): a delete the agent made before the
738        // touch is part of the coherent point it declared, so this
739        // barrier confirms first-absence paths instead of acking a
740        // boundary that withholds them to the next floor tick.
741        // The ack and the manifest must agree on which clock published:
742        // a budget-deferred boundary reads `sentinel-deferred` in both.
743        let before = self.state.load_conflicts()?.len();
744        let report = self
745            .declared_barrier_as(
746                source.unwrap_or(if forced { "sentinel-deferred" } else { "sentinel" }),
747            )
748            .await?;
749        // Review 2026-09-12, ack-3: the boundary's own records (a
750        // `consume-dirty` the agent won, a refused removal) ride the
751        // publish ack as they ride the sync ack — AGENTS.md says so.
752        let conflicts = conflicts_since(before, self.state.load_conflicts()?);
753        let units = self.charge_budget(report.published_bytes)?;
754        let _ = units;
755        let baseline = self.state.load_baseline()?;
756        // Review 2026-09-12, inbox-1: a boundary with a standing park does
757        // NOT carry the agent's file — `ok` promised "the boundary is in
758        // the bucket". It is `partial`, and `report.dropped` names the
759        // paths. Neither is a delete another writer's edit outranked (the
760        // seq still cites the file) nor an upload that published nothing
761        // (a transfer that drifted or was swept).
762        let dropped: Vec<String> =
763            report.parked.iter().chain(&report.outranked).chain(&report.deferred).cloned().collect();
764        Ok(Ack {
765            status: if dropped.is_empty() { "ok".into() } else { "partial".into() },
766            nonces: pending.nonces.clone(),
767            sentinel_mtime_unix_ns: pending.consumed_mtime_unix_ns,
768            seq: report.seq,
769            manifest_etag: baseline.manifest_etag.clone(),
770            boundary: if forced { "sentinel-deferred".into() } else { "sentinel".into() },
771            completed_unix: now_unix(),
772        reason: None,
773            report: AckReport {
774                uploaded: report.uploaded.len(),
775                deleted: report.deleted.len(),
776                parked: report.parked.len(),
777                consumed: report.consumed,
778                no_change: report.no_change,
779                conflicts,
780                dropped,
781                ..Default::default()
782            },
783        })
784    }
785
786    async fn honor_sync(&mut self, pending: &PendingSentinel, forced: bool) -> LeanResult<Ack> {
787        if let Some(reason) = Self::scope_refusal(&pending.scope) {
788            // Review 2026-09-12, ack-1 / inbox-3: the refusal used to be
789            // an ERROR the loop retried every tick — never acked, never
790            // retired, and returned ahead of the publish honor and the
791            // cadence barrier, so one `{"scope":[]}` stopped every
792            // boundary for the life of the workspace. An invalid request
793            // is the agent's to fix: answered, retired, and nothing else
794            // waits on it.
795            return Ok(Ack {
796                status: "refused-scope".into(),
797                nonces: pending.nonces.clone(),
798                sentinel_mtime_unix_ns: pending.consumed_mtime_unix_ns,
799                seq: None,
800                manifest_etag: None,
801                boundary: if forced { "sentinel-deferred".into() } else { "sentinel".into() },
802                completed_unix: now_unix(),
803                    reason: Some(reason),
804                report: AckReport { scope: pending.scope.clone(), ..Default::default() },
805            });
806        }
807        let before = self.state.load_conflicts()?.len();
808        let report = self.sync_scoped(pending.scope.clone()).await?;
809        let conflicts = conflicts_since(before, self.state.load_conflicts()?);
810        // A sync publishes no bytes: it costs no budget units, only the
811        // min-interval (which it shares with publish).
812        self.charge_budget(0)?;
813        Ok(Ack {
814            status: "ok".into(),
815            nonces: pending.nonces.clone(),
816            sentinel_mtime_unix_ns: pending.consumed_mtime_unix_ns,
817            seq: Some(report.seq),
818            manifest_etag: None,
819            boundary: if forced { "sentinel-deferred".into() } else { "sentinel".into() },
820            completed_unix: now_unix(),
821        reason: None,
822            report: AckReport {
823                consumed: report.applied.len(),
824                deleted: report.deleted.len(),
825                applied: report.applied.clone(),
826                conflicts,
827                scope: pending.scope.clone(),
828                out_of_scope_foreign: report.out_of_scope_foreign,
829                ..Default::default()
830            },
831        })
832    }
833
834    /// Review 2026-09-12, ack-1: an invalid sync scope is the AGENT's
835    /// error and is answered as one (`refused-scope`), never retried.
836    /// `None` = the scope is acceptable (or absent: the whole tree).
837    fn scope_refusal(scope: &Option<Vec<String>>) -> Option<String> {
838        let raw = scope.as_ref()?;
839        if raw.len() > MAX_SCOPE_ENTRIES {
840            return Some(format!(
841                "scope names {} entries; at most {MAX_SCOPE_ENTRIES} are allowed per sync",
842                raw.len()
843            ));
844        }
845        if super::sync::Scope::new(raw).is_empty() {
846            return Some(format!(
847                "scope names {} entr{} and none is valid: each must be a relative path inside the \
848                 workspace with no `.` or `..` component (an empty scope is refused, not widened \
849                 to the whole tree)",
850                raw.len(),
851                if raw.len() == 1 { "y" } else { "ies" }
852            ));
853        }
854        None
855    }
856
857    /// The startup settle (the uniform crash rule). Runs before the
858    /// first poll arms: a surviving pending must be honored, acked and
859    /// retired before any fresh sentinel may be consumed.
860    pub async fn settle_pending_at_startup(&mut self) -> LeanResult<()> {
861        self.recover_consume_staging()?;
862        for verb in [Verb::Publish, Verb::Sync] {
863            let Some(pending) = self.load_pending(verb)? else { continue };
864            if self.ack_matches(verb, &pending) {
865                // Crashed after the ack, before the retire.
866                self.retire_pending(verb)?;
867                continue;
868            }
869            // Pending + no matching ack is the same observable state
870            // for crash-before-CAS and crash-after-step-7: run a full
871            // barrier and ack from ITS install, never from persisted
872            // state.
873            self.honor_pending(verb, true).await?;
874        }
875        Ok(())
876    }
877}
878
879/// What a floor tick did.
880#[derive(Debug, Default)]
881pub struct FloorOutcome {
882    pub seq: Option<u64>,
883    pub no_change: bool,
884    /// Acks written because a budget-deferred (or min-interval-held)
885    /// pending sentinel was picked up by this floor tick.
886    pub acks: Vec<Ack>,
887    pub uploaded: usize,
888    pub deleted: usize,
889    pub consumed: usize,
890    /// Why visibility is withheld right now, straight off the gauges —
891    /// so the per-tick stderr line is greppable and structured, which
892    /// until Phase 6 is the ONLY signal surface an operator has.
893    pub withheld_reason: Option<String>,
894    /// A foreign 412 parked at least one path on this tick.
895    pub parked: usize,
896    /// Carried out of the barrier only to feed the news ticker.
897    observed_etag: Option<String>,
898}
899
900impl Syncer {
901    /// Move the news ticker from what the barrier already learned (D5).
902    /// Never issues a request of its own.
903    fn ticker_from(&self, seq: Option<u64>, etag: Option<String>) -> LeanResult<()> {
904        let baseline_seq = self.state.load_baseline()?.seq;
905        // A merge that carried another writer's change into the manifest
906        // moves the baseline to that document while the change waits in
907        // this writer's queue (or, after a restart, its intent journal) for
908        // the next consume. Until then the tree is integrated only through
909        // what the ticker said while nothing waited.
910        let waiting = !self.state.load_foreign_queue()?.is_empty()
911            || !self.state.load_intent()?.installed_foreign.is_empty();
912        let integrated = if waiting {
913            baseline_seq.min(self.load_remote_seq().integrated_seq)
914        } else {
915            baseline_seq
916        };
917        self.touch_remote_seq(seq, etag, integrated)
918    }
919
920    /// One poll tick of the sentinel arm: consume what is there, then
921    /// honor if the min-interval and the work budget allow.
922    ///
923    /// In `cadence` mode the arm still consumes and honors — a sentinel
924    /// triggers a fused barrier there too; the mode's meaning is that
925    /// there is no separate citation lane, not that the verbs are dead.
926    pub async fn sentinel_tick(&mut self) -> LeanResult<Vec<Ack>> {
927        let posture = self.load_posture()?;
928        if posture.as_ref().map(|p| !p.enabled).unwrap_or(false) {
929            return Ok(vec![]);
930        }
931        self.poll_sentinels()?;
932        let mut acks = vec![];
933        // Sync before publish: a coalesced pair means "pull, then
934        // publish my coherent point" — the other order would publish
935        // against a tree the agent expected to have refreshed.
936        for verb in [Verb::Sync, Verb::Publish] {
937            match self.honor_pending(verb, false).await {
938                Ok(Some(a)) => acks.push(a),
939                Ok(None) => {}
940                Err(e) => {
941                    // Review 2026-09-12, ack-1: a non-fence honor error is
942                    // THIS verb's problem. Returning here left the other
943                    // verb unhonoured for as long as the error recurred —
944                    // for a deterministic one, forever. The pending stands
945                    // and is retried; the tick goes on.
946                    eprintln!("flint-sync: {verb:?} honor failed (pending kept, retrying): {e}");
947                }
948            }
949        }
950        if !acks.is_empty() {
951            let last = acks.last().and_then(|a| a.seq);
952            self.ticker_from(last, None)?;
953        }
954        Ok(acks)
955    }
956
957    /// One floor tick: either honor a standing
958    /// pending sentinel that the budget or min-interval held back — the
959    /// boundary is honored by a REAL barrier, its ack stamped
960    /// `sentinel-deferred` — or run the ordinary cadence barrier.
961    ///
962    /// This is the arm B16 regresses on: the interval rewrite must not
963    /// let the poll arm starve cadence.
964    pub async fn floor_tick(&mut self) -> LeanResult<FloorOutcome> {
965        let mut out = FloorOutcome::default();
966        // A held-back sync pending is settled first, for the same
967        // reason as in the poll arm.
968        //
969        // Only a PUBLISH honor discharges the floor's own barrier: a
970        // sync ack carries a seq (the manifest it synced against) but
971        // publishes nothing, so treating it as "the floor ran" would
972        // skip cadence for a tick — a silent RPO regression.
973        let mut published = false;
974        for verb in [Verb::Sync, Verb::Publish] {
975            if self.load_pending(verb)?.is_some() {
976                match self.honor_pending(verb, true).await {
977                    Ok(Some(a)) => {
978                        out.seq = a.seq.or(out.seq);
979                        if verb == Verb::Publish && a.status == "ok" {
980                            published = true;
981                            out.uploaded = a.report.uploaded;
982                            out.deleted = a.report.deleted;
983                            out.consumed = a.report.consumed;
984                            out.no_change = a.report.no_change;
985                        }
986                        out.acks.push(a);
987                    }
988                    Ok(None) => {}
989                    Err(e) => {
990                        // Review 2026-09-12, ack-1: same rule as the poll
991                        // arm, and here the stake is the cadence barrier
992                        // below, which used to go unrun behind the error.
993                        eprintln!("flint-sync: {verb:?} honor failed on the floor (pending kept): {e}");
994                    }
995                }
996            }
997        }
998        // A reader's floor is its pull (`reader.rs`): no barrier, ever.
999        if self.cfg.access.is_read() {
1000            return match self.reader_pull().await {
1001                Ok(p) => {
1002                    let _ = self.clear_auth_pause();
1003                    match &p.sync {
1004                        Some(r) => {
1005                            out.seq = Some(r.seq);
1006                            out.consumed = r.applied.len();
1007                            out.deleted = r.deleted.len();
1008                        }
1009                        None => {
1010                            out.no_change = true;
1011                            out.seq = Some(self.state.load_baseline()?.seq);
1012                        }
1013                    }
1014                    self.ticker_from(p.observed_seq, p.observed_etag)?;
1015                    out.withheld_reason = self.write_gauges(None)?.withheld_reason;
1016                    Ok(out)
1017                }
1018                Err(e) => {
1019                    if e.is_auth() {
1020                        let _ = self.note_auth_pause();
1021                    }
1022                    Err(e)
1023                }
1024            };
1025        }
1026        // A publish sentinel honored on this tick already ran the fused
1027        // barrier the floor owed; running a second one would be pure
1028        // churn.
1029        if !published {
1030            let ran = self.cadence_barrier().await.map(|r| {
1031                out.seq = r.seq;
1032                out.no_change = r.no_change;
1033                out.uploaded = r.uploaded.len();
1034                out.deleted = r.deleted.len();
1035                out.consumed = r.consumed;
1036                out.parked = r.parked.len();
1037                out.observed_etag = r.observed_etag.clone();
1038                r.observed_seq
1039            });
1040            match ran {
1041                Ok(observed) => {
1042                    // The floor tick is the one scheduled request every
1043                    // writer makes, idle or not, so it is where a
1044                    // credential pause is seen to END — and, below, to
1045                    // begin. A claim sees both too, but an idle writer
1046                    // never claims. Best-effort: a gauge that failed to
1047                    // write must not fail a tick that succeeded.
1048                    let _ = self.clear_auth_pause();
1049                    let etag = out.observed_etag.take();
1050                    self.ticker_from(observed, etag)?;
1051                    // A foreign 412 outranks "waiting for a boundary":
1052                    // those paths are not ours to publish at all, and
1053                    // saying "quiesce-pending" would send an operator
1054                    // looking for a clock instead of a conflict record.
1055                    let forced = (out.parked > 0).then_some(super::gauges::Withheld::Parked412);
1056                    out.withheld_reason = self.write_gauges(forced)?.withheld_reason;
1057                    return Ok(out);
1058                }
1059                Err(e) => {
1060                    if e.is_auth() {
1061                        let _ = self.note_auth_pause();
1062                    }
1063                    return Err(e);
1064                }
1065            }
1066        }
1067        self.ticker_from(out.seq, None)?;
1068        out.withheld_reason = self.write_gauges(None)?.withheld_reason;
1069        Ok(out)
1070    }
1071
1072    /// The preStop drain (D10 rule 1): settle every owed ack BEFORE the
1073    /// lease release. A pending sentinel at SIGTERM is answered by the
1074    /// drain itself — the container-restart case, where the emptyDir and
1075    /// the agent both survive, must not strand a waiting agent.
1076    pub async fn drain(&mut self) -> LeanResult<Vec<Ack>> {
1077        let mut acks = vec![];
1078        // Only an ok PUBLISH ack means a boundary already ran. A sync
1079        // ack carries a seq too — the manifest it synced AGAINST — while
1080        // publishing nothing, so asking "did any ack carry a seq?" lets
1081        // a pending `.flint/sync` at SIGTERM cancel the drain's own
1082        // cite-everything pass and forfeit every byte since the last
1083        // boundary. The floor arm's `sync_ack_is_not_a_floor` comment
1084        // names this exact trap; the drain repeated it.
1085        let mut published = false;
1086        for verb in [Verb::Sync, Verb::Publish] {
1087            let is_publish = matches!(verb, Verb::Publish);
1088            if self.load_pending(verb)?.is_some() {
1089                match self.honor_pending_as(verb, true, Some("drain")).await {
1090                    Ok(Some(mut a)) => {
1091                        a.boundary = "drain".into();
1092                        // Re-write with the drain stamp so the agent can
1093                        // tell a drained boundary from a live one.
1094                        let bytes = serde_json::to_vec_pretty(&a)
1095                            .map_err(|e| LeanError::State(format!("ack: {e}")))?;
1096                        write_atomic(&self.ack_path(verb), &bytes)?;
1097                        published |= is_publish && a.status == "ok";
1098                        acks.push(a);
1099                    }
1100                    Ok(None) => {}
1101                    Err(e) => return Err(e),
1102                }
1103            }
1104        }
1105        // A reader has nothing to drain: what its tree holds is either in
1106        // the bucket already or, by its access, never going there.
1107        if !published && !self.cfg.access.is_read() {
1108            // D10: the drain publishes EVERYTHING it can, as one declared barrier.
1109                let r = self.declared_barrier_as("drain").await?;
1110                self.ticker_from(r.observed_seq, r.observed_etag.clone())?;
1111                // Review 2026-09-12, inbox-1: a drain that leaves a path
1112                // parked has not published the tree; attesting it let the
1113                // node remove the agent's only copy. So is one whose upload
1114                // published nothing, and one whose delete another writer's
1115                // edit outranked: the retry publishes both.
1116                if !r.parked.is_empty() {
1117                    return Err(LeanError::State(format!(
1118                        "drain: {} path(s) could not be published (parked on a foreign version whose \
1119                         preserve failed): {:?} — not attesting",
1120                        r.parked.len(),
1121                        r.parked
1122                    )));
1123                }
1124                if !r.deferred.is_empty() || !r.outranked.is_empty() {
1125                    return Err(LeanError::State(format!(
1126                        "drain: not in this boundary — uploads that published nothing {:?}, deletes \
1127                         another writer's change outranked {:?} — not attesting",
1128                        r.deferred, r.outranked
1129                    )));
1130                }
1131        }
1132        Ok(acks)
1133    }
1134}