lean-ctx-cloud 3.12.9

The LeanCTX Cloud connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// SPDX-License-Identifier: Apache-2.0
//! Durable outbox: everything bound for Cloud is written here first and
//! sent later by the scheduler (or `lean-ctx cloud sync`), so Cloud never
//! blocks a tool call and nothing is lost while offline.
//!
//! `<data_dir>/cloud/outbox/<channel>/<id>.json`, one 0600 file per item,
//! ids sort by creation time and double as `Idempotency-Key`. At most
//! 10 000 items / 50 MB: when full, the oldest metrics give way; knowledge
//! is never dropped silently (enqueueing fails instead). An item Cloud
//! refuses for good moves to `outbox/failed/<channel>/` and stays visible
//! in `cloud status`; each channel keeps the newest [`MAX_FAILED`] there and
//! the newest [`MAX_DAMAGED`] damaged files (RB8 M14).

use std::path::{Path, PathBuf};

use lean_ctx_time::{Duration, OffsetDateTime, UtcDateTime};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::CloudError;
use super::state::write_private;

pub const CHANNEL_KNOWLEDGE: &str = "knowledge";
pub const CHANNEL_METRICS: &str = "metrics";
pub const CHANNEL_RECEIPTS: &str = "receipts";
/// Task proposals (C0 1.9 ยง1): agent tasks and contradictions.
pub const CHANNEL_TASKS: &str = "tasks";
const CHANNELS: [&str; 4] = [
    CHANNEL_KNOWLEDGE,
    CHANNEL_METRICS,
    CHANNEL_RECEIPTS,
    CHANNEL_TASKS,
];

const MAX_ITEMS: usize = 10_000;
const MAX_BYTES: u64 = 50 * 1024 * 1024;
/// Refused items kept per channel under `failed/`, newest first.
pub const MAX_FAILED: usize = 500;
/// Damaged item files (`*.damaged`) kept per channel, newest first.
pub const MAX_DAMAGED: usize = 20;
const BACKOFF_START_SECONDS: i64 = 30;
const BACKOFF_MAX_SECONDS: i64 = 3600;
/// A `Retry-After` beyond this is a broken or hostile answer: waiting is
/// capped, never computed past chrono's range (which panics).
const RETRY_AFTER_MAX_SECONDS: i64 = 7 * 24 * 3600;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OutboxItem {
    pub id: String,
    pub channel: String,
    pub created_at: String,
    #[serde(default)]
    pub attempts: u32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub next_attempt_at: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_error: Option<String>,
    /// The wait is a policy refusal's (`policy_denied`): a newer policy in
    /// force ends it at once ([`Outbox::release_policy_holds`]).
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub policy_hold: bool,
    pub payload: Value,
}

impl OutboxItem {
    /// Whether the item may be sent at `now`.
    pub fn due(&self, now: UtcDateTime) -> bool {
        self.next_attempt_at
            .as_deref()
            .and_then(|t| OffsetDateTime::parse_from_rfc3339(t).ok())
            .is_none_or(|at| at.to_utc() <= now)
    }
}

/// Counts for `cloud status`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct OutboxDepth {
    pub pending: usize,
    pub failed: usize,
}

pub struct Outbox {
    root: PathBuf,
}

fn valid_channel(channel: &str) -> bool {
    matches!(
        channel,
        CHANNEL_KNOWLEDGE | CHANNEL_METRICS | CHANNEL_RECEIPTS | CHANNEL_TASKS
    )
}

fn stamp(t: UtcDateTime) -> String {
    t.to_rfc3339_opts(lean_ctx_time::SecondsFormat::Millis, true)
}

impl Outbox {
    pub fn at(data_dir: &Path) -> Self {
        Self {
            root: data_dir.join("outbox"),
        }
    }

    fn dir(&self, channel: &str) -> PathBuf {
        self.root.join(channel)
    }

    fn failed_dir(&self, channel: &str) -> PathBuf {
        self.root.join("failed").join(channel)
    }

    /// Queue `payload` on `channel`; returns the item id.
    pub fn enqueue(
        &self,
        channel: &str,
        payload: Value,
        now: UtcDateTime,
    ) -> Result<String, CloudError> {
        if !valid_channel(channel) {
            return Err(CloudError::Local(format!(
                "unknown outbox channel {channel:?}"
            )));
        }
        let mut random = [0u8; 8];
        getrandom::fill(&mut random).map_err(|e| CloudError::Local(format!("random: {e}")))?;
        let id = format!(
            "{:013}{}",
            now.timestamp_millis().max(0),
            hex::encode(random)
        );
        let item = OutboxItem {
            id: id.clone(),
            channel: channel.to_string(),
            created_at: stamp(now),
            attempts: 0,
            next_attempt_at: None,
            last_error: None,
            policy_hold: false,
            payload,
        };
        let bytes = serde_json::to_vec(&item).map_err(|e| CloudError::Local(e.to_string()))?;
        self.make_room(channel, bytes.len() as u64)?;
        write_private(&self.dir(channel), &format!("{id}.json"), &bytes)?;
        Ok(id)
    }

    fn files(dir: &Path) -> Vec<(PathBuf, u64)> {
        let mut files: Vec<(PathBuf, u64)> = std::fs::read_dir(dir)
            .into_iter()
            .flatten()
            .filter_map(Result::ok)
            .filter(|e| e.file_name().to_string_lossy().ends_with(".json"))
            .map(|e| (e.path(), e.metadata().map_or(0, |m| m.len())))
            .collect();
        files.sort();
        files
    }

    fn make_room(&self, channel: &str, incoming: u64) -> Result<(), CloudError> {
        let channels = CHANNELS;
        let mut count: usize = channels
            .iter()
            .map(|c| Self::files(&self.dir(c)).len())
            .sum();
        let mut bytes: u64 = channels
            .iter()
            .flat_map(|c| Self::files(&self.dir(c)))
            .map(|(_, size)| size)
            .sum();
        let mut metrics = Self::files(&self.dir(CHANNEL_METRICS)).into_iter();
        while count + 1 > MAX_ITEMS || bytes + incoming > MAX_BYTES {
            let Some((oldest, size)) = metrics.next() else {
                return Err(CloudError::Local(format!(
                    "the LeanCTX Cloud outbox is full; {channel} is kept back until LeanCTX Cloud is reachable again"
                )));
            };
            let _ = std::fs::remove_file(oldest);
            count -= 1;
            bytes = bytes.saturating_sub(size);
        }
        Ok(())
    }

    /// Pending items of `channel`, oldest first. Damaged files are moved aside.
    pub fn list(&self, channel: &str) -> Vec<OutboxItem> {
        Self::files(&self.dir(channel))
            .into_iter()
            .filter_map(|(path, _)| {
                let parsed = std::fs::read(&path)
                    .ok()
                    .and_then(|b| serde_json::from_slice::<OutboxItem>(&b).ok());
                if parsed.is_none() {
                    let _ = std::fs::rename(&path, path.with_extension("damaged"));
                    keep_newest(&self.dir(channel), ".damaged", MAX_DAMAGED);
                }
                parsed
            })
            .collect()
    }

    pub fn remove(&self, item: &OutboxItem) -> Result<(), CloudError> {
        match std::fs::remove_file(self.dir(&item.channel).join(format!("{}.json", item.id))) {
            Ok(()) => Ok(()),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Err(e) => Err(CloudError::Local(format!("outbox: {e}"))),
        }
    }

    /// Replace an item (e.g. after a rebase) keeping its id.
    pub fn update(&self, item: &OutboxItem) -> Result<(), CloudError> {
        let bytes = serde_json::to_vec(item).map_err(|e| CloudError::Local(e.to_string()))?;
        write_private(
            &self.dir(&item.channel),
            &format!("{}.json", item.id),
            &bytes,
        )
    }

    /// Record a transient failure: exponential backoff 30 s โ†’ 1 h with
    /// jitter, or Cloud' `Retry-After` when given.
    pub fn retry_later(
        &self,
        item: &mut OutboxItem,
        error: &str,
        retry_after: Option<u64>,
        now: UtcDateTime,
    ) -> Result<(), CloudError> {
        item.policy_hold = false;
        self.wait(item, error, retry_after, now)
    }

    /// [`Self::retry_later`] for a policy refusal: the item waits
    /// `retry_after` (or the backoff), or until a newer policy is in force.
    pub fn hold_for_policy(
        &self,
        item: &mut OutboxItem,
        error: &str,
        retry_after: Option<u64>,
        now: UtcDateTime,
    ) -> Result<(), CloudError> {
        item.policy_hold = true;
        self.wait(item, error, retry_after, now)
    }

    /// A newer policy is in force: every item waiting on a policy refusal
    /// is due again, on every channel; other waits stay. Returns how many.
    pub fn release_policy_holds(&self) -> usize {
        let mut released = 0;
        for channel in CHANNELS {
            for mut item in self.list(channel).into_iter().filter(|i| i.policy_hold) {
                item.policy_hold = false;
                item.next_attempt_at = None;
                if self.update(&item).is_ok() {
                    released += 1;
                }
            }
        }
        released
    }

    fn wait(
        &self,
        item: &mut OutboxItem,
        error: &str,
        retry_after: Option<u64>,
        now: UtcDateTime,
    ) -> Result<(), CloudError> {
        item.attempts = item.attempts.saturating_add(1);
        let exponent = item.attempts.saturating_sub(1).min(7);
        let backoff = (BACKOFF_START_SECONDS << exponent).min(BACKOFF_MAX_SECONDS);
        let mut jitter = [0u8; 2];
        let _ = getrandom::fill(&mut jitter);
        let jitter = i64::from(u16::from_le_bytes(jitter)) % (backoff / 4 + 1);
        let wait = retry_after
            .map_or(backoff + jitter, |s| i64::try_from(s).unwrap_or(i64::MAX))
            .clamp(0, RETRY_AFTER_MAX_SECONDS);
        item.next_attempt_at = Some(stamp(now + Duration::seconds(wait)));
        item.last_error = Some(error.chars().take(300).collect());
        self.update(item)
    }

    /// Cloud refused the item for good: keep it visible under `failed/`.
    pub fn fail(&self, item: &mut OutboxItem, error: &str) -> Result<(), CloudError> {
        item.last_error = Some(error.chars().take(300).collect());
        let bytes = serde_json::to_vec(item).map_err(|e| CloudError::Local(e.to_string()))?;
        write_private(
            &self.failed_dir(&item.channel),
            &format!("{}.json", item.id),
            &bytes,
        )?;
        keep_newest(&self.failed_dir(&item.channel), ".json", MAX_FAILED);
        self.remove(item)
    }

    pub fn failed(&self, channel: &str) -> Vec<OutboxItem> {
        Self::files(&self.failed_dir(channel))
            .into_iter()
            .filter_map(|(path, _)| {
                std::fs::read(&path)
                    .ok()
                    .and_then(|b| serde_json::from_slice(&b).ok())
            })
            .collect()
    }

    pub fn depth(&self, channel: &str) -> OutboxDepth {
        OutboxDepth {
            pending: Self::files(&self.dir(channel)).len(),
            failed: Self::files(&self.failed_dir(channel)).len(),
        }
    }
}

/// Remove the oldest files of `dir` ending in `suffix` beyond `keep` (ids,
/// and so names, sort by creation time).
fn keep_newest(dir: &Path, suffix: &str, keep: usize) {
    let mut names: Vec<PathBuf> = std::fs::read_dir(dir)
        .into_iter()
        .flatten()
        .filter_map(Result::ok)
        .filter(|e| e.file_name().to_string_lossy().ends_with(suffix))
        .map(|e| e.path())
        .collect();
    names.sort();
    let excess = names.len().saturating_sub(keep);
    for old in names.drain(..excess) {
        let _ = std::fs::remove_file(old);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn now() -> UtcDateTime {
        OffsetDateTime::parse_from_rfc3339("2026-10-20T12:00:00Z")
            .unwrap()
            .to_utc()
    }

    #[test]
    fn items_survive_reopen_in_order_and_keep_their_ids() {
        let dir = tempfile::tempdir().unwrap();
        let outbox = Outbox::at(dir.path());
        let first = outbox
            .enqueue(CHANNEL_KNOWLEDGE, json!({"n": 1}), now())
            .unwrap();
        let second = outbox
            .enqueue(
                CHANNEL_KNOWLEDGE,
                json!({"n": 2}),
                now() + Duration::milliseconds(5),
            )
            .unwrap();
        let items = Outbox::at(dir.path()).list(CHANNEL_KNOWLEDGE);
        assert_eq!(
            items.iter().map(|i| i.id.clone()).collect::<Vec<_>>(),
            vec![first, second]
        );
        outbox.remove(&items[0]).unwrap();
        assert_eq!(
            outbox.depth(CHANNEL_KNOWLEDGE),
            OutboxDepth {
                pending: 1,
                failed: 0
            }
        );
    }

    #[test]
    fn backoff_grows_and_failures_stay_visible() {
        let dir = tempfile::tempdir().unwrap();
        let outbox = Outbox::at(dir.path());
        outbox.enqueue(CHANNEL_KNOWLEDGE, json!({}), now()).unwrap();
        let mut item = outbox.list(CHANNEL_KNOWLEDGE).remove(0);
        outbox
            .retry_later(&mut item, "offline", None, now())
            .unwrap();
        assert!(!item.due(now() + Duration::seconds(29)));
        assert!(item.due(now() + Duration::seconds(38)));
        outbox
            .retry_later(&mut item, "offline", Some(600), now())
            .unwrap();
        assert!(!item.due(now() + Duration::seconds(599)));
        outbox.fail(&mut item, "invalid_request").unwrap();
        assert_eq!(
            outbox.depth(CHANNEL_KNOWLEDGE),
            OutboxDepth {
                pending: 0,
                failed: 1
            }
        );
        assert_eq!(
            outbox.failed(CHANNEL_KNOWLEDGE)[0].last_error.as_deref(),
            Some("invalid_request")
        );
    }

    #[test]
    fn a_huge_retry_after_is_capped_not_a_panic() {
        let dir = tempfile::tempdir().unwrap();
        let outbox = Outbox::at(dir.path());
        outbox.enqueue(CHANNEL_KNOWLEDGE, json!({}), now()).unwrap();
        let mut item = outbox.list(CHANNEL_KNOWLEDGE).remove(0);
        outbox
            .retry_later(&mut item, "busy", Some(u64::MAX), now())
            .unwrap();
        assert!(!item.due(now() + Duration::days(6)));
        assert!(item.due(now() + Duration::days(7) + Duration::seconds(1)));
    }

    #[test]
    fn unknown_channels_are_refused() {
        let dir = tempfile::tempdir().unwrap();
        assert!(
            Outbox::at(dir.path())
                .enqueue("../x", json!({}), now())
                .is_err()
        );
    }
}