lean-ctx-cloud 3.12.11

The LeanCTX 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
// SPDX-License-Identifier: Apache-2.0
//! Process v1 on submit (E47) and verify (E48), decided in the Instance
//! before anything is sent:
//!
//! - **Submit of a process task** (a task view with `process`): G6 runs
//!   here ([`super::gate`]): the locked-path check of the judged commit
//!   and the handed-in worktree, then the gate's own rerun of the judged
//!   commit. The Instance does not read CI (§5.1), so what it sends is its
//!   part of the record, `blocked` on `ci_pipeline` only, which Cloud
//!   completes with `ci`; a red record, or a view that lacks what G6 needs,
//!   refuses the submit and nothing is sent.
//! - **Verify, every task: checker ≠ author** by the per-session identity
//!   (§3.1). The checker is a **live bus presence of this Instance**
//!   (the local registry, never a free string, never the machine's shared
//!   worker id), held by the calling process (MCP) or by a process the
//!   session that started the presence's MCP server is an ancestor of (CLI
//!   from that session's own shell, by process ancestry; the environment,
//!   `LEAN_CTX_SESSION_ID` included, never binds). A result submitted on this Instance is compared with the
//!   session that claimed and the one that submitted it
//!   ([`claims::WorkTrace`]): the same agent, app session or registering
//!   process never checks it. Without a per-session identity on either
//!   side the verify is refused. For a process task the checker also has
//!   a verifying role (tester, code reviewer, security reviewer).
//! - **Verify of a process task:** Cloud's G6 record for the judged commit
//!   must be green, `ci` included, and the locked paths are checked again
//!   here, commit against `spec_commit`, in a private copy.
//!
//! **Known gap, server missing:** Cloud does not yet check the verifier
//! against a live `bus_presence` row of the token's Instance in the gate's
//! role (§3.1 E48), nor add `ci` to G6 (§5.1). An agent running as the same
//! Unix user can still edit this Instance's files (the claims file, the bus
//! registry) or run a command under another session's process tree; only
//! Cloud's per-session identity (task #21) closes that. Every refusal says
//! why and sends nothing.

use std::path::Path;

use lean_ctx_protocol::cloud::gate::{CloudG6RecordV1, G6RecordV1, GateResult, NotGreen};
use lean_ctx_protocol::cloud::tasks::{
    EvidenceKind, EvidenceV1, TaskActorKind, TaskOutcome, TaskV1,
};

use super::claims::{SessionIdentity, WorkTrace};
use super::gate::{self, Missing};
use crate::CloudError;

/// The roles that may verify a process task (§3.1: the gate's role; the
/// old id `reviewer` is the code reviewer).
pub(crate) const VERIFYING_ROLES: [&str; 4] =
    ["tester", "code_reviewer", "reviewer", "security_reviewer"];

/// A live bus presence of this Instance, as the local registry has it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Presence {
    pub agent: String,
    /// The role id it registered with.
    pub role: Option<String>,
    /// The app session of the process that registered it.
    pub app_session: Option<String>,
    /// The registering process, `<pid>@<start marker>`.
    pub process: Option<String>,
    /// Whether the calling process may act as it ([`binds_caller`]): it
    /// registered the presence, or it runs under the session that started
    /// the presence's MCP server.
    pub bound_here: bool,
}

impl Presence {
    pub(crate) fn identity(&self) -> SessionIdentity {
        SessionIdentity {
            agent: self.agent.clone(),
            session: self.app_session.clone(),
            process: self.process.clone(),
        }
    }
}

/// Where a bus agent's live presence is looked up.
pub(crate) type PresenceLookup<'a> = &'a (dyn Fn(&str) -> Option<Presence> + Sync);

fn process_key(pid: u32, identity: &lean_ctx::ipc::process::ProcessIdentity) -> String {
    format!("{pid}@{}", identity.start_marker)
}

/// The live presence of `agent` in this machine's bus registry: listed,
/// not finished, and its registering process still the one that
/// registered it.
pub(crate) fn local_presence(agent: &str) -> Option<Presence> {
    use lean_ctx::core::agents::{AgentRegistry, AgentStatus};
    let registry = AgentRegistry::load()?;
    let entry = registry
        .agents
        .iter()
        .find(|a| a.agent_id == agent && a.status != AgentStatus::Finished)?;
    let identity = entry.process_identity.as_ref()?;
    if !lean_ctx::ipc::process::matches_identity(entry.pid, identity) {
        return None;
    }
    let bound_here = binds_caller(registry.held_by_this_process(agent), entry.pid);
    Some(Presence {
        agent: agent.to_owned(),
        role: entry
            .role
            .as_deref()
            .and_then(lean_ctx::core::agents::role_id),
        app_session: entry.app_session_id.clone(),
        process: Some(process_key(entry.pid, identity)),
        bound_here,
    })
}

/// The parent of process `pid` (Linux: `/proc/<pid>/stat`; other Unix:
/// `ps`); `None` when it cannot be read.
fn parent_of(pid: u32) -> Option<u32> {
    #[cfg(target_os = "linux")]
    {
        let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
        // The command name may hold spaces and parentheses: read after it.
        stat.rsplit_once(')')?
            .1
            .split_whitespace()
            .nth(1)?
            .parse()
            .ok()
    }
    #[cfg(all(unix, not(target_os = "linux")))]
    {
        let out = std::process::Command::new("/bin/ps")
            .args(["-o", "ppid=", "-p", &pid.to_string()])
            .env_clear()
            .stdin(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .output()
            .ok()?;
        String::from_utf8_lossy(&out.stdout).trim().parse().ok()
    }
    #[cfg(not(unix))]
    {
        let _ = pid;
        None
    }
}

/// Whether the host that started the process `registered` (its parent:
/// the agent session that spawned that MCP server) is this process or one
/// of its ancestors: a CLI run from that session's own shell.
///
/// Follow-up: an MCP server orphaned to a subreaper (`systemd --user`)
/// makes that subreaper its host, and every process below it a match;
/// storing the parent's `pid@start` in the registry at registration and
/// refusing a host that differs would close that.
pub(crate) fn hosts_caller(registered: u32) -> bool {
    let Some(host) = parent_of(registered).filter(|host| *host > 1) else {
        return false;
    };
    let mut pid = std::process::id();
    for _ in 0..64 {
        if pid == host {
            return true;
        }
        match parent_of(pid) {
            Some(parent) if parent > 1 => pid = parent,
            _ => return false,
        }
    }
    false
}

/// Whether the calling process may act as a presence registered by the
/// process `registered`: it holds the presence itself (MCP), or it runs
/// under the session that started it (CLI, by process ancestry). The
/// environment (`LEAN_CTX_SESSION_ID`) never binds: any process can set it.
pub(crate) fn binds_caller(held_here: bool, registered: u32) -> bool {
    held_here || hosts_caller(registered)
}

/// The identity of the session acting as `agent` here: its live presence's,
/// else this process with its app session.
pub(crate) fn acting_identity(agent: &str, presence: PresenceLookup<'_>) -> SessionIdentity {
    if let Some(found) = presence(agent) {
        return found.identity();
    }
    let pid = std::process::id();
    SessionIdentity {
        agent: agent.to_owned(),
        session: crate::policy_sessions::current_session(),
        process: lean_ctx::ipc::process::identity(pid).map(|id| process_key(pid, &id)),
    }
}

fn refused(text: String) -> CloudError {
    CloudError::Refused(text)
}

fn short(sha: &str) -> &str {
    &sha[..12.min(sha.len())]
}

fn missing_text(number: u64, missing: &Missing) -> String {
    match missing {
        Missing::Cloud(what) => format!(
            "not supported by the Cloud yet: this Cloud server's view of process task #{number} carries no {what}, which G6 needs; nothing was sent"
        ),
        Missing::Local(what) => format!(
            "task #{number}: this Instance has no record of {what} (claim the task again from this checkout); nothing was sent"
        ),
    }
}

/// The one commit a result names (its `commit` evidence).
fn result_commit(number: u64, evidence: &[EvidenceV1]) -> Result<String, CloudError> {
    let mut commits: Vec<&str> = evidence
        .iter()
        .filter(|e| e.kind == EvidenceKind::Commit)
        .filter_map(|e| e.reference.as_deref())
        .collect();
    commits.sort_unstable();
    commits.dedup();
    match commits.as_slice() {
        [one] => Ok((*one).to_owned()),
        [] => Err(refused(format!(
            "task #{number} is a process task: its result names the judged commit (evidence kind commit); nothing was sent"
        ))),
        _ => Err(refused(format!(
            "task #{number} is a process task: its result names exactly one commit, the one G6 judges; nothing was sent"
        ))),
    }
}

fn result_name(result: GateResult) -> &'static str {
    match result {
        GateResult::Pass => "pass",
        GateResult::Fail => "fail",
        GateResult::Blocked => "blocked",
    }
}

fn record_text(record: &G6RecordV1) -> String {
    let mut text = format!(
        "G6 {} for {}",
        result_name(record.result),
        short(&record.artifact_commit)
    );
    if let Some(rule) = &record.rule {
        text.push_str(&format!(" (rule {rule})"));
    }
    if let Some(reason) = &record.reason {
        text.push_str(": ");
        text.push_str(reason);
    }
    text
}

/// G6 before a result of `task` leaves the worktree `root`, worked out in
/// the gate's directories `dirs`: `None` for a task that is not a
/// process task (or a failed result, which G6 does not judge), the record
/// to send with it (everything the Instance checks holds), or the refusal.
pub(crate) fn submit_gate(
    task: &TaskV1,
    root: &Path,
    dirs: &gate::GateDirs,
    evidence: &[EvidenceV1],
    outcome: TaskOutcome,
    trace: Option<&WorkTrace>,
) -> Result<Option<G6RecordV1>, CloudError> {
    let Some(process) = task.process.as_ref() else {
        return Ok(None);
    };
    if outcome == TaskOutcome::Failed {
        return Ok(None);
    }
    let number = task.number;
    let artifact = result_commit(number, evidence)?;
    let artifact = gate::resolve_commit(root, &artifact)
        .map_err(|e| refused(format!("task #{number}: {e}; nothing was sent")))?;
    if let Some(candidate) = process.candidate_commit.as_deref()
        && candidate != artifact
    {
        return Err(refused(format!(
            "task #{number}: G6 judges the commit G4 passed ({}), not {}; nothing was sent",
            short(candidate),
            short(&artifact)
        )));
    }
    let start = trace.and_then(|t| t.start_commit.as_deref());
    let inputs = gate::inputs_for(process, task.process_gate.as_ref(), start)
        .map_err(|m| refused(missing_text(number, &m)))?;
    let record = gate::g6_record(root, dirs, &artifact, &inputs)
        .map_err(|e| refused(format!("task #{number}: {e}; nothing was sent")))?;
    if !record.ready_for_cloud() {
        return Err(refused(format!(
            "task #{number}: {}; the result was not sent",
            record_text(&record)
        )));
    }
    Ok(Some(record))
}

/// Who checks: the live bus presence `agent` of this Instance, held by the
/// calling process or its app session, and for a process task in a
/// verifying role. The machine's shared worker id (`wkr_…`), an id no live
/// presence has, or one another process holds is no per-session identity.
pub(crate) fn checker_identity(
    agent: &str,
    presence: Option<Presence>,
    process_task: bool,
) -> Result<SessionIdentity, CloudError> {
    let agent = agent.trim();
    if agent.is_empty() || crate::bus::address::is_worker_id(agent) {
        return Err(refused(
            "no per-session identity: verify as a session's bus agent (ctx_agent action=register), not as the machine's worker; checker ≠ author cannot be shown otherwise".into(),
        ));
    }
    let Some(presence) = presence.filter(|p| p.agent == agent) else {
        return Err(refused(format!(
            "no per-session identity: {agent} is no live bus agent of this Instance (register it with ctx_agent in its own session); nothing was sent"
        )));
    };
    if !presence.bound_here {
        return Err(refused(format!(
            "no per-session identity: this process does not act for {agent} (it neither registered it nor runs in its app session); verify from that agent's own session; nothing was sent"
        )));
    }
    if process_task
        && !presence
            .role
            .as_deref()
            .is_some_and(|role| VERIFYING_ROLES.contains(&role))
    {
        return Err(refused(format!(
            "not_permitted: {agent} has role {}; a process task is verified by a tester or reviewer (§3.1); nothing was sent",
            presence.role.as_deref().unwrap_or("none")
        )));
    }
    Ok(presence.identity())
}

/// Checker ≠ author (§3.1) for a verify of `task` by `checker`: a result of
/// another Instance or a person is someone else's; a result of this
/// Instance is compared with the session that submitted it, and the
/// session that claimed the task never checks it either.
pub(crate) fn checker_not_author(
    task: &TaskV1,
    checker: &SessionIdentity,
    trace: Option<&WorkTrace>,
) -> Result<(), CloudError> {
    let number = task.number;
    let Some(result) = task.result.as_ref() else {
        return Err(refused(format!(
            "task #{number} carries no submitted result to verify"
        )));
    };
    match result.by.kind {
        TaskActorKind::Person => Ok(()),
        TaskActorKind::Instance if !result.by.mine => Ok(()),
        TaskActorKind::Instance => {
            let author = trace.and_then(|t| t.submitted_by.as_ref()).ok_or_else(|| {
                refused(format!(
                    "no per-session identity for the author of task #{number}: it was submitted on this Instance, which keeps no record of the session that did it, so checker ≠ author cannot be shown; another Instance or a person verifies it"
                ))
            })?;
            let claimed_here = trace.is_some_and(|t| {
                t.claimed_by == checker.agent
                    || t.claimed_session
                        .as_ref()
                        .is_some_and(|claimant| claimant.same_as(checker))
            });
            if author.same_as(checker) || claimed_here {
                return Err(refused(format!(
                    "self_check: task #{number} was claimed or submitted by this session; another session verifies it"
                )));
            }
            Ok(())
        }
        TaskActorKind::Unknown => Err(refused(format!(
            "task #{number}: its result's author is of a kind this engine does not know, so checker ≠ author cannot be shown"
        ))),
    }
}

/// The judged commit of a submitted process task: the one G4 passed, else
/// the result's commit evidence.
fn judged_commit(task: &TaskV1) -> Option<String> {
    let process = task.process.as_ref()?;
    if let Some(candidate) = &process.candidate_commit {
        return Some(candidate.clone());
    }
    let commits: Vec<&str> = task
        .result
        .as_ref()?
        .evidence
        .iter()
        .filter(|e| e.kind == EvidenceKind::Commit)
        .filter_map(|e| e.reference.as_deref())
        .collect();
    match commits.as_slice() {
        [one] => Some((*one).to_owned()),
        _ => None,
    }
}

fn cloud_record_text(record: &CloudG6RecordV1, why: &str) -> String {
    format!(
        "G6 {} for {}: {why}",
        result_name(record.result),
        short(record.artifact_commit())
    )
}

/// G6 before a verify of a process task (E48): Cloud's G6 record for the
/// judged commit is green, `ci` included, and the locked paths are still
/// unchanged in it (checked here, from a private copy of `repo` in the
/// Instance's G6 directory `work`).
pub(crate) fn verify_gate(task: &TaskV1, repo: &Path, work: &Path) -> Result<(), CloudError> {
    if task.process.is_none() {
        return Ok(());
    }
    let number = task.number;
    let record = task.process_gate.as_ref().and_then(|g| g.g6.as_ref()).ok_or_else(|| {
        refused(format!(
            "not supported by the Cloud yet: this Cloud server serves no G6 record for process task #{number}, and a verify needs a green one; nothing was sent"
        ))
    })?;
    match record.green() {
        Ok(()) => {}
        Err(NotGreen::NoCi) => {
            return Err(refused(format!(
                "not supported by the Cloud yet: the Cloud's G6 record for process task #{number} carries no ci, and a pass needs a green pipeline for the judged commit (§5.1); nothing was sent"
            )));
        }
        Err(NotGreen::Red(why)) => {
            return Err(refused(format!(
                "task #{number}: verify needs a green G6 record; the Cloud's is {}; nothing was sent",
                cloud_record_text(record, &why)
            )));
        }
    }
    let artifact = judged_commit(task).ok_or_else(|| {
        refused(format!(
            "task #{number}: the judged commit is unknown (no candidate commit, not exactly one commit in the result); nothing was sent"
        ))
    })?;
    let artifact = gate::resolve_commit(repo, &artifact)
        .map_err(|e| refused(format!("task #{number}: {e}; nothing was sent")))?;
    if !record.judges(&artifact) {
        return Err(refused(format!(
            "task #{number}: the G6 record judged {}, not the submitted commit {}; nothing was sent",
            short(record.artifact_commit()),
            short(&artifact)
        )));
    }
    let check = gate::recheck_commit(
        repo,
        work,
        &artifact,
        &record.locked_paths.spec_commit,
        &record.locked_paths.checked,
    )
    .map_err(|e| refused(format!("task #{number}: {e}; nothing was sent")))?;
    if !check.ok {
        return Err(refused(format!(
            "task #{number}: {}; nothing was sent",
            gate::changed_reason(&check)
        )));
    }
    Ok(())
}

#[cfg(test)]
#[path = "process_tests.rs"]
pub(crate) mod tests;