lean-ctx-cloud 3.12.7

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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// SPDX-License-Identifier: Apache-2.0
//! `task.event` frames (C0 2.2 §10.1): Cloud tells every engine of a vault
//! when one of its tasks changed. The engine outdates its task cache (the
//! next `ctx_tasks` read fetches the task again) and hands the work to its
//! own agents on the local bus, so agents orchestrate tasks themselves:
//!
//! - `opened`, `released`, `rejected`: the task is open (again): every agent
//!   of the project may claim it;
//! - `submitted`: a result waits for a second agent's check: every agent of
//!   the project except the one that did the work (Cloud refuses a
//!   self-verify);
//! - everything else changes only the cache.
//!
//! The frame carries no task text, only its number, so nothing from it is
//! shown as an instruction. The frame is not stored by Cloud: no replay. A
//! frame missed while the relay was away or still subscribing is caught up
//! after each hello (`catch_up`): the open and submitted tasks, read with
//! `GET …/tasks?state=`, go through the same `apply`. The relay state
//! keeps the last change applied per task ([`TasksSeen`]), so neither path
//! announces a change twice or announces an older one after a newer.
//!
//! The frame types and [`notices`] are always built; `apply` and `catch_up`
//! run only in the relay's WebSocket session (`bus::ws`, feature
//! `http-server`), so they and what only they use are compiled with it.

use std::collections::BTreeMap;
#[cfg(feature = "http-server")]
use std::path::Path;

use serde::{Deserialize, Serialize};
use serde_json::Value;

#[cfg(feature = "http-server")]
use super::address::Address;
#[cfg(feature = "http-server")]
use crate::tasks::ListFilter;
#[cfg(feature = "http-server")]
use crate::vault::VaultMap;
#[cfg(feature = "http-server")]
use lean_ctx::core::a2a::message::{MessagePriority, PrivacyLevel};
#[cfg(feature = "http-server")]
use lean_ctx::core::agents::AgentRegistry;
use lean_ctx_protocol::cloud::tasks::TaskState;

/// What happened to the task.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TaskEventKind {
    Opened,
    Claimed,
    Released,
    Submitted,
    Verified,
    Rejected,
    Cancelled,
    Failed,
    #[serde(other)]
    Unknown,
}

/// Who acted: a worker and its engine, or Cloud itself (`system`).
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
pub struct TaskEventBy {
    #[serde(default)]
    pub worker_id: Option<String>,
    #[serde(default)]
    pub instance_id: Option<String>,
    #[serde(default)]
    pub system: bool,
}

/// One `task.event` frame.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct TaskEvent {
    pub vault: String,
    pub number: u64,
    pub event: TaskEventKind,
    pub state: TaskState,
    pub by: TaskEventBy,
    pub at: u64,
}

impl TaskEvent {
    pub fn parse(frame: &Value) -> Option<Self> {
        serde_json::from_value(frame.clone())
            .ok()
            .filter(|e: &Self| e.number > 0)
    }
}

/// The local posts an event asks for: `(to, text)`, `None` = every agent
/// of the project. `local` are the project's active agents.
pub fn notices(event: &TaskEvent, local: &[String]) -> Vec<(Option<String>, String)> {
    let n = event.number;
    match event.event {
        TaskEventKind::Opened | TaskEventKind::Released | TaskEventKind::Rejected => {
            let why = match event.event {
                TaskEventKind::Opened => "is open",
                TaskEventKind::Released => "is open again (its claim ended)",
                _ => "is open again (a check rejected its result)",
            };
            vec![(
                None,
                format!(
                    "LeanCTX Cloud task #{n} {why}: read it with ctx_tasks action=show number={n}, claim it with action=claim."
                ),
            )]
        }
        TaskEventKind::Submitted => {
            let text = format!(
                "LeanCTX Cloud task #{n} has a result to check: read it with ctx_tasks action=show number={n}, then action=verify verdict=accept|reject (another agent than the one that did the work)."
            );
            // `by.worker_id` is the agent id the claim and result named
            // (Cloud compares the same string for self_verify).
            let worker = event.by.worker_id.as_deref().unwrap_or_default();
            if !local.iter().any(|agent| agent == worker) {
                // The result came from another agent: any agent here may check it.
                return vec![(None, text)];
            }
            local
                .iter()
                .filter(|agent| agent.as_str() != worker)
                .map(|agent| (Some(agent.clone()), text.clone()))
                .collect()
        }
        _ => Vec::new(),
    }
}

/// The last change applied to one task.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaskSeen {
    pub state: TaskState,
    /// Cloud's time of the change (unix ms); a catch-up has it to the second.
    pub at: u64,
    /// Recorded by a catch-up, not by a frame.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub caught_up: bool,
}

/// Vault address (`#ws/vault`) → task number → the last change applied.
pub type TasksSeen = BTreeMap<String, BTreeMap<u64, TaskSeen>>;

/// Tasks remembered per vault (the oldest changes drop first).
#[cfg(any(test, feature = "http-server"))]
const MAX_SEEN: usize = 1000;

/// Where an event came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Origin {
    /// A `task.event` frame.
    Frame,
    /// A task list read after a hello (`catch_up`).
    CatchUp,
}

/// Record `event` in `seen`; whether its notices are due. An event older
/// than the change already applied is dropped. A catch-up announces only a
/// task whose state changed since; a frame repeats nothing a catch-up of
/// the same change (same state, same second) already announced.
#[cfg(any(test, feature = "http-server"))]
fn admit(seen: &mut TasksSeen, event: &TaskEvent, origin: Origin) -> bool {
    let tasks = seen.entry(event.vault.clone()).or_default();
    let notify = match tasks.get(&event.number) {
        None => true,
        Some(last) if event.at < last.at => return false,
        Some(last) => match origin {
            Origin::Frame => {
                !(last.caught_up && last.state == event.state && event.at < last.at + 1000)
            }
            Origin::CatchUp if event.at > last.at && last.state != event.state => true,
            Origin::CatchUp => return false,
        },
    };
    tasks.insert(
        event.number,
        TaskSeen {
            state: event.state,
            at: event.at,
            caught_up: origin == Origin::CatchUp,
        },
    );
    while tasks.len() > MAX_SEEN {
        let Some(oldest) = tasks.iter().min_by_key(|(_, s)| s.at).map(|(n, _)| *n) else {
            break;
        };
        tasks.remove(&oldest);
    }
    notify
}

/// Apply one event: outdate the vault's task cache and post its notices
/// into the project's local bus, unless `seen` already holds this change
/// or a newer one. An unknown vault changes nothing.
#[cfg(feature = "http-server")]
pub(crate) fn apply(
    registry: &mut AgentRegistry,
    map: &VaultMap,
    data_dir: &Path,
    seen: &mut TasksSeen,
    event: &TaskEvent,
    origin: Origin,
) -> Result<(), String> {
    let Ok(Address::Vault { workspace, vault }) = Address::parse(Some(&event.vault)) else {
        return Ok(());
    };
    let Some(record) = map
        .vaults
        .values()
        .find(|r| r.ns == workspace && r.name == vault)
    else {
        return Ok(());
    };
    crate::tasks::cache::outdate(data_dir, &record.vault_id);
    if !admit(seen, event, origin) {
        return Ok(());
    }
    let Some(root) = record.roots.iter().find(|root| Path::new(root).is_dir()) else {
        return Ok(());
    };
    let local: Vec<String> = registry
        .list_active(Some(root))
        .into_iter()
        .map(|a| a.agent_id.clone())
        .collect();
    let source = match origin {
        Origin::Frame => "",
        Origin::CatchUp => "caught_up:",
    };
    let key = format!(
        "{}task:{}:{}:{:?}:{source}{}",
        super::relay_v2::INBOUND_KEY_PREFIX,
        event.vault,
        event.number,
        event.event,
        event.at
    );
    for (i, (to, text)) in notices(event, &local).into_iter().enumerate() {
        registry.post_message_keyed(
            Some(root),
            "cloud",
            to.as_deref(),
            "status",
            &text,
            PrivacyLevel::Team,
            MessagePriority::Normal,
            None,
            Some(&format!("{key}:{i}")),
        )?;
    }
    Ok(())
}

/// The open and submitted tasks of the `subscribed` vaults as events, for
/// `apply` with [`Origin::CatchUp`], and what could not be read. Blocking
/// (HTTP); the relay runs it off its session loop after each hello.
#[cfg(feature = "http-server")]
pub(crate) fn catch_up(map: &VaultMap, subscribed: &[String]) -> (Vec<TaskEvent>, Vec<String>) {
    let (mut events, mut errors) = (Vec::new(), Vec::new());
    for record in map.vaults.values() {
        let vault = super::relay_v2::address_of(record);
        if !subscribed.contains(&vault) {
            continue;
        }
        let Some(root) = record.roots.iter().find(|root| Path::new(root).is_dir()) else {
            continue;
        };
        let root = Path::new(root);
        for (state, kind) in [
            (TaskState::Open, TaskEventKind::Opened),
            (TaskState::Submitted, TaskEventKind::Submitted),
        ] {
            let filter = ListFilter {
                state: Some(state),
                ..ListFilter::default()
            };
            // Only a current list: tasks of a stale one (429, Cloud not
            // reachable) may since be claimed or closed and would wake
            // Queens as "opened" (review 5!246).
            let list = match crate::tasks::list_read(root, &filter) {
                Ok(read) if read.stale.is_none() => read.value,
                Ok(read) => {
                    let note = read.stale.map(|s| s.note()).unwrap_or_default();
                    errors.push(format!("{vault}: {note}"));
                    continue;
                }
                Err(error) => {
                    errors.push(format!("{vault}: {error}"));
                    continue;
                }
            };
            for task in list
                .list
                .tasks
                .into_iter()
                .filter(|task| task.state == state)
            {
                // A result's worker gets no request to check its own work.
                let worker_id = (kind == TaskEventKind::Submitted)
                    .then(|| crate::tasks::show(root, task.number).ok())
                    .flatten()
                    .and_then(|view| view.task.result)
                    .and_then(|result| result.by.agent);
                events.push(TaskEvent {
                    vault: vault.clone(),
                    number: task.number,
                    event: kind,
                    state,
                    by: TaskEventBy {
                        worker_id,
                        ..TaskEventBy::default()
                    },
                    at: unix_ms(task.updated_at.as_str()),
                });
            }
        }
    }
    (events, errors)
}

/// `YYYY-MM-DDTHH:MM:SSZ` as unix ms; 0 (older than any change) if invalid.
#[cfg(any(test, feature = "http-server"))]
fn unix_ms(timestamp: &str) -> u64 {
    lean_ctx_time::OffsetDateTime::parse_from_rfc3339(timestamp)
        .ok()
        .and_then(|t| u64::try_from(t.timestamp_millis()).ok())
        .unwrap_or_default()
}

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

    const FIXTURES: &str = concat!(
        concat!(env!("CARGO_MANIFEST_DIR"), "/../.."),
        "/crates/lean-ctx-protocol/tests/fixtures/cloud-v2/bus"
    );

    fn frame(name: &str) -> TaskEvent {
        let text =
            std::fs::read_to_string(format!("{FIXTURES}/frame.task_event.{name}.json")).unwrap();
        let value: Value = serde_json::from_str(&text).unwrap();
        let event = TaskEvent::parse(&value).unwrap_or_else(|| panic!("{name}"));
        assert_eq!(
            super::super::v2::parse_frame(&text).unwrap(),
            super::super::v2::Inbound::TaskEvent(Box::new(event.clone())),
            "{name}"
        );
        event
    }

    #[test]
    fn the_contract_frames_parse() {
        for (name, kind, state) in [
            ("opened", TaskEventKind::Opened, TaskState::Open),
            ("released", TaskEventKind::Released, TaskState::Open),
            ("submitted", TaskEventKind::Submitted, TaskState::Submitted),
            ("verified", TaskEventKind::Verified, TaskState::Done),
        ] {
            let event = frame(name);
            assert_eq!((event.event, event.state), (kind, state), "{name}");
            assert_eq!(event.vault, "#acme/payments");
        }
    }

    #[test]
    fn open_tasks_go_to_every_agent_and_results_to_the_others() {
        let opened = frame("opened");
        let out = notices(&opened, &["a1".to_string()]);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].0, None);
        assert!(
            out[0].1.starts_with("LeanCTX Cloud task #7 is open:"),
            "{}",
            out[0].1
        );

        let submitted = frame("submitted");
        let worker = submitted.by.worker_id.clone().unwrap();
        let others = vec!["a1".to_string(), "a3".to_string()];
        // Another agent's result: every agent here may check it.
        assert_eq!(notices(&submitted, &others)[0].0, None);
        // This machine's agent did the work: never that agent (no self-verify).
        let local = vec!["a1".to_string(), worker, "a3".to_string()];
        let to: Vec<_> = notices(&submitted, &local)
            .into_iter()
            .map(|(to, text)| {
                assert!(text.contains("action=verify"), "{text}");
                to.unwrap()
            })
            .collect();
        assert_eq!(to, ["a1", "a3"]);

        assert!(notices(&frame("verified"), &local).is_empty());
    }

    /// Task 7 of the fixture vault in `state` at `at` (unix ms).
    fn change(event: TaskEventKind, state: TaskState, at: u64) -> TaskEvent {
        TaskEvent {
            event,
            state,
            at,
            ..frame("opened")
        }
    }

    #[test]
    fn a_task_opened_while_subscribing_is_announced_once_by_the_catch_up() {
        let mut seen = TasksSeen::new();
        // The frame went by before the hello: only the catch-up sees it.
        let listed = change(TaskEventKind::Opened, TaskState::Open, 1_792_800_020_000);
        assert!(admit(&mut seen, &listed, Origin::CatchUp));
        // The same change as a frame (same second, ms precision): no repeat.
        let late = change(TaskEventKind::Opened, TaskState::Open, 1_792_800_020_431);
        assert!(!admit(&mut seen, &late, Origin::Frame));
        // A second catch-up of the unchanged task: still nothing.
        assert!(!admit(&mut seen, &listed, Origin::CatchUp));
    }

    #[test]
    fn a_catch_up_never_repeats_or_rewinds_what_a_frame_announced() {
        let mut seen = TasksSeen::new();
        let opened = change(TaskEventKind::Opened, TaskState::Open, 1_792_800_020_431);
        assert!(admit(&mut seen, &opened, Origin::Frame));
        // The list read the same change (to the second): no repeat.
        let listed = change(TaskEventKind::Opened, TaskState::Open, 1_792_800_020_000);
        assert!(!admit(&mut seen, &listed, Origin::CatchUp));
        // The task is done; a list read before that is stale and says nothing.
        let done = change(TaskEventKind::Verified, TaskState::Done, 1_792_800_030_000);
        assert!(admit(&mut seen, &done, Origin::Frame));
        assert!(!admit(&mut seen, &listed, Origin::CatchUp));
        assert_eq!(seen["#acme/payments"][&7].state, TaskState::Done);
    }

    #[test]
    fn a_task_open_again_is_announced_even_when_its_claim_was_missed() {
        let mut seen = TasksSeen::new();
        let opened = change(TaskEventKind::Opened, TaskState::Open, 1_000);
        assert!(admit(&mut seen, &opened, Origin::Frame));
        // `claimed` never arrived; the release still says "open again".
        let released = change(TaskEventKind::Released, TaskState::Open, 9_000);
        assert!(admit(&mut seen, &released, Origin::Frame));
        // Reopened while the relay was away: the catch-up announces it.
        let claimed = change(TaskEventKind::Claimed, TaskState::Claimed, 10_000);
        assert!(admit(&mut seen, &claimed, Origin::Frame));
        let reopened = change(TaskEventKind::Opened, TaskState::Open, 20_000);
        assert!(admit(&mut seen, &reopened, Origin::CatchUp));
        // A frame older than what was applied is dropped.
        assert!(!admit(&mut seen, &claimed, Origin::Frame));
        assert_eq!(seen["#acme/payments"][&7].state, TaskState::Open);
    }

    #[test]
    fn the_oldest_changes_drop_first() {
        let mut seen = TasksSeen::new();
        for number in 1..=(MAX_SEEN as u64 + 1) {
            let event = TaskEvent {
                number,
                ..change(TaskEventKind::Opened, TaskState::Open, number)
            };
            assert!(admit(&mut seen, &event, Origin::Frame));
        }
        let tasks = &seen["#acme/payments"];
        assert_eq!(tasks.len(), MAX_SEEN);
        assert!(!tasks.contains_key(&1));
    }

    #[test]
    fn listed_times_are_unix_ms() {
        assert_eq!(unix_ms("2026-10-15T08:00:20Z"), 1_792_051_220_000);
        assert_eq!(unix_ms("not a time"), 0);
    }
}