Skip to main content

lean_ctx/core/
task_spine.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Canonical task lineage with immutable async/blocking execution scopes.
3
4use chrono::Utc;
5use lean_ctx_protocol::{
6    AgentId, ProjectId, SessionId, TaskComplexity, TaskEnvelopeV1, TaskId, TaskProfileV1, TenantId,
7    TraceId,
8};
9use std::cell::RefCell;
10use std::collections::HashMap;
11use std::sync::{Arc, Mutex, OnceLock};
12
13pub type TaskProfileLocal = TaskProfileV1;
14
15thread_local! {
16    static CURRENT: RefCell<Option<TaskEnvelopeV1>> = const { RefCell::new(None) };
17}
18
19tokio::task_local! {
20    // Explicit None masks stale legacy thread state in unbound worker scopes.
21    static SCOPED: Option<TaskEnvelopeV1>;
22}
23
24/// Parent task id retained for the lifetime of each active MCP session.
25type SessionLineage = (String, String);
26type SessionLineages = Arc<Mutex<HashMap<(ProjectId, Option<TenantId>, String), SessionLineage>>>;
27static SESSION_LINEAGES: OnceLock<SessionLineages> = OnceLock::new();
28
29/// Logical identities supplied by an admitting host, never by tool arguments.
30#[derive(Debug, Clone, PartialEq, Eq, Hash)]
31pub(crate) struct AdmittedTaskIdentity {
32    pub(crate) project_id: ProjectId,
33    pub(crate) tenant_id: TenantId,
34}
35
36#[derive(Debug, Clone, Default)]
37/// Maintains task-envelope lineage for the current execution thread.
38pub struct TaskSpine {
39    /// Root task of the current session, absent when creating that root task.
40    pub parent_id: Option<String>,
41    /// Stable trace id shared by the root task and every true child in the session.
42    pub trace_id: String,
43}
44
45impl TaskSpine {
46    pub fn create_envelope(query: &str, session_id: &str, agent_id: &str) -> TaskEnvelopeV1 {
47        Self::create_envelope_in_project(query, session_id, agent_id, None)
48    }
49
50    /// Only the admitting host supplies workspace identity. The latest saved
51    /// session belongs to no particular caller and must never supply this scope.
52    pub(crate) fn create_envelope_in_project(
53        query: &str,
54        session_id: &str,
55        agent_id: &str,
56        project_root: Option<&str>,
57    ) -> TaskEnvelopeV1 {
58        Self::create_envelope_with_identity(query, session_id, agent_id, project_root, None)
59    }
60
61    pub(crate) fn create_envelope_with_identity(
62        query: &str,
63        session_id: &str,
64        agent_id: &str,
65        project_root: Option<&str>,
66        identity: Option<&AdmittedTaskIdentity>,
67    ) -> TaskEnvelopeV1 {
68        let project_id = identity
69            .map(|identity| identity.project_id.clone())
70            .or_else(|| project_root.and_then(|project| ProjectId::try_from(project).ok()))
71            .unwrap_or_else(|| {
72                ProjectId::try_from("unknown-project").expect("fallback project id is valid")
73            });
74        let task_id = TaskId::try_from(format!("mcp-task-{}", uuid::Uuid::new_v4()))
75            .expect("generated task id is valid");
76        let initial_trace_id = format!("trace-{}", uuid::Uuid::new_v4());
77        let tenant_id = identity.map(|identity| identity.tenant_id.clone());
78        let spine = Self::for_session(
79            &project_id,
80            tenant_id.as_ref(),
81            session_id,
82            task_id.as_str(),
83            &initial_trace_id,
84        );
85        let envelope = TaskEnvelopeV1 {
86            schema_version: TaskEnvelopeV1::SCHEMA_VERSION,
87            task_id,
88            trace_id: TraceId::try_from(spine.trace_id.clone())
89                .expect("generated trace id is valid"),
90            project_id,
91            session_id: SessionId::try_from(session_id.to_owned())
92                .expect("MCP session id is valid"),
93            agent_id: AgentId::try_from(agent_id.to_owned()).expect("MCP agent id is valid"),
94            complexity: TaskComplexity::Unknown,
95            created_at: Utc::now().to_rfc3339(),
96            parent_task_id: spine
97                .parent_id
98                .clone()
99                .map(TaskId::try_from)
100                .transpose()
101                .expect("stored parent task id is valid"),
102            tenant_id,
103            intent: (!query.trim().is_empty()).then(|| query.to_owned()),
104            task_class: None,
105            risk_class: None,
106            quality_requirement_milli: None,
107            cost_budget_micros: None,
108            latency_budget_ms: None,
109            data_classification: None,
110            region_policy_ref: None,
111            model_policy_ref: None,
112            context_state_ref: None,
113            outcome_contract_ref: None,
114            extensions: Default::default(),
115        };
116        Self::set(envelope.clone());
117        envelope
118    }
119
120    fn for_session(
121        project_id: &ProjectId,
122        tenant_id: Option<&TenantId>,
123        session_id: &str,
124        task_id: &str,
125        initial_trace_id: &str,
126    ) -> Self {
127        if session_id.trim().is_empty() {
128            return Self {
129                parent_id: None,
130                trace_id: initial_trace_id.to_owned(),
131            };
132        }
133
134        let mut lineages = Self::session_lineages()
135            .lock()
136            .unwrap_or_else(std::sync::PoisonError::into_inner);
137        let (parent_id, trace_id) = match lineages.entry((
138            project_id.clone(),
139            tenant_id.cloned(),
140            session_id.to_owned(),
141        )) {
142            std::collections::hash_map::Entry::Occupied(parent) => {
143                let (root_task_id, trace_id) = parent.get();
144                (Some(root_task_id.clone()), trace_id.clone())
145            }
146            std::collections::hash_map::Entry::Vacant(session) => {
147                session.insert((task_id.to_owned(), initial_trace_id.to_owned()));
148                (None, initial_trace_id.to_owned())
149            }
150        };
151        Self {
152            parent_id,
153            trace_id,
154        }
155    }
156
157    fn session_lineages() -> &'static SessionLineages {
158        SESSION_LINEAGES.get_or_init(|| Arc::new(Mutex::new(HashMap::new())))
159    }
160
161    pub fn enrich_from_triage(envelope: &mut TaskEnvelopeV1, profile: &TaskProfileLocal) {
162        envelope.intent = Some(profile.primary_intent.clone());
163        envelope.task_class = Some(profile.task_class.clone());
164        envelope.complexity = profile.complexity;
165        envelope.risk_class = Some(profile.risk_signal);
166        Self::set(envelope.clone());
167    }
168
169    pub fn task_id() -> Option<String> {
170        Self::current().map(|envelope| envelope.task_id.as_str().to_owned())
171    }
172
173    pub fn current() -> Option<TaskEnvelopeV1> {
174        SCOPED
175            .try_with(Clone::clone)
176            .unwrap_or_else(|_| CURRENT.with(|current| current.borrow().clone()))
177    }
178
179    /// Only an explicitly bound lifecycle may supply canonical execution identity.
180    pub(crate) fn scoped_current() -> Option<TaskEnvelopeV1> {
181        SCOPED.try_with(Clone::clone).ok().flatten()
182    }
183
184    /// Legacy synchronous callers may set a fallback, never overwrite a bound task.
185    pub fn set(envelope: TaskEnvelopeV1) {
186        if SCOPED.try_with(|_| ()).is_err() {
187            CURRENT.with(|current| *current.borrow_mut() = Some(envelope));
188        }
189    }
190
191    pub(crate) async fn scope<F: std::future::Future>(
192        envelope: Option<TaskEnvelopeV1>,
193        future: F,
194    ) -> F::Output {
195        SCOPED.scope(envelope, future).await
196    }
197
198    /// Callers must capture the owning async scope before spawning a blocking job.
199    pub(crate) fn sync_scope<T>(
200        envelope: Option<TaskEnvelopeV1>,
201        operation: impl FnOnce() -> T,
202    ) -> T {
203        SCOPED.sync_scope(envelope, operation)
204    }
205
206    /// Preserve the owner of a detached read/telemetry worker, including no owner.
207    pub(crate) fn spawn_thread<T: Send + 'static>(
208        operation: impl FnOnce() -> T + Send + 'static,
209    ) -> std::thread::JoinHandle<T> {
210        let envelope = Self::current();
211        let project = crate::core::policy::runtime::REQUEST_PROJECT
212            .try_with(|slot| slot.borrow().clone())
213            .ok();
214        let source_view = crate::core::policy::runtime::inherited_source_view();
215        // Admissions made by the worker belong to the call that spawned it.
216        let admissions = crate::core::context_admission::capture::current();
217        std::thread::spawn(move || {
218            crate::core::context_admission::capture::ADMISSIONS.sync_scope(admissions, || {
219                match project {
220                    Some(project) => crate::core::policy::runtime::REQUEST_PROJECT.sync_scope(
221                        std::cell::RefCell::new(project),
222                        || {
223                            crate::core::policy::runtime::with_inherited_source_view(
224                                source_view,
225                                || Self::sync_scope(envelope, operation),
226                            )
227                        },
228                    ),
229                    None => crate::core::policy::runtime::with_inherited_source_view(
230                        source_view,
231                        || Self::sync_scope(envelope, operation),
232                    ),
233                }
234            })
235        })
236    }
237}
238
239#[cfg(test)]
240mod tests {
241    use super::*;
242
243    #[test]
244    fn detached_read_workers_isolate_overlapping_policy_projects() {
245        use crate::core::policy::runtime::REQUEST_PROJECT;
246        let barrier = std::sync::Arc::new(std::sync::Barrier::new(3));
247        let workers = ["first-request-project", "second-request-project"].map(|name| {
248            let root = std::path::PathBuf::from(name);
249            let worker_barrier = barrier.clone();
250            let worker =
251                REQUEST_PROJECT.sync_scope(std::cell::RefCell::new(Some(root.clone())), || {
252                    TaskSpine::spawn_thread(move || {
253                        worker_barrier.wait();
254                        REQUEST_PROJECT.with(|slot| slot.borrow().clone())
255                    })
256                });
257            (root, worker)
258        });
259        barrier.wait();
260        for (root, worker) in workers {
261            assert_eq!(worker.join().unwrap(), Some(root));
262        }
263    }
264
265    #[test]
266    fn detached_worker_inherits_pinned_source_policy() {
267        use crate::core::policy::runtime::{self, REQUEST_PROJECT};
268        use std::fs;
269
270        let _override_guard = runtime::lock_test_override();
271        let _data = crate::core::data_dir::isolated_data_dir();
272        let root = tempfile::tempdir().expect("create project");
273        let policy_dir = root.path().join(".lean-ctx");
274        fs::create_dir_all(&policy_dir).expect("create policy directory");
275        let policy_path = policy_dir.join("policy.toml");
276        fs::write(
277            &policy_path,
278            "name = \"thread-a\"\nversion = \"1.0.0\"\ndescription = \"thread test\"\n",
279        )
280        .expect("write policy A");
281
282        let result =
283            REQUEST_PROJECT.sync_scope(RefCell::new(Some(root.path().to_path_buf())), || {
284                runtime::with_source_view(|| {
285                    let worker = TaskSpine::spawn_thread(|| {
286                        runtime::active().map(|policy| policy.resolved.name.clone())
287                    });
288                    fs::write(
289                        &policy_path,
290                        "name = \"thread-b\"\nversion = \"1.0.0\"\ndescription = \"thread test\"\n",
291                    )
292                    .expect("write policy B");
293                    assert_eq!(worker.join().unwrap().as_deref(), Some("thread-a"));
294                })
295            });
296
297        assert!(result.is_err());
298    }
299
300    #[test]
301    fn detached_threads_preserve_bound_and_explicitly_unbound_identity() {
302        let task = TaskSpine::create_envelope("thread", "scoped-thread", "agent");
303        let worker = TaskSpine::sync_scope(Some(task.clone()), || {
304            TaskSpine::spawn_thread(TaskSpine::current)
305        });
306        assert_eq!(worker.join().unwrap(), Some(task));
307        let unbound = TaskSpine::sync_scope(None, || {
308            TaskSpine::spawn_thread(|| {
309                TaskSpine::create_envelope("decoy", "detached-decoy", "agent");
310                TaskSpine::current()
311            })
312        });
313        assert!(unbound.join().unwrap().is_none());
314    }
315
316    #[tokio::test(flavor = "current_thread")]
317    async fn async_scopes_isolate_interleaved_tasks_and_ignore_legacy_setters() {
318        let first = TaskSpine::create_envelope("first", "scoped-a", "agent-a");
319        let second = TaskSpine::create_envelope("second", "scoped-b", "agent-b");
320        let legacy = TaskSpine::current();
321        let check = |expected: TaskEnvelopeV1, other: TaskEnvelopeV1| async move {
322            TaskSpine::scope(Some(expected.clone()), async {
323                for _ in 0..5 {
324                    TaskSpine::set(other.clone());
325                    tokio::task::yield_now().await;
326                    assert_eq!(TaskSpine::current(), Some(expected.clone()));
327                }
328                TaskSpine::scope(None, async {
329                    assert!(TaskSpine::current().is_none());
330                    TaskSpine::set(other);
331                    assert!(TaskSpine::current().is_none());
332                })
333                .await;
334                assert_eq!(TaskSpine::current(), Some(expected));
335            })
336            .await;
337        };
338        tokio::join!(check(first.clone(), second.clone()), check(second, first));
339        assert_eq!(TaskSpine::current(), legacy);
340    }
341
342    #[test]
343    fn sync_scope_restores_identity_after_panic_and_masks_unbound_workers() {
344        let legacy = TaskSpine::create_envelope("legacy", "legacy-worker", "agent");
345        let scoped = TaskSpine::create_envelope("scoped", "scoped-worker", "agent");
346        TaskSpine::set(legacy.clone());
347        assert!(TaskSpine::scoped_current().is_none());
348        let panic = std::panic::catch_unwind(|| {
349            TaskSpine::sync_scope(Some(scoped.clone()), || {
350                assert_eq!(TaskSpine::scoped_current(), Some(scoped.clone()));
351                assert_eq!(TaskSpine::current(), Some(scoped));
352                panic!("fixture unwind");
353            })
354        });
355        assert!(panic.is_err());
356        assert_eq!(TaskSpine::current(), Some(legacy.clone()));
357        TaskSpine::sync_scope(None, || assert!(TaskSpine::current().is_none()));
358        assert_eq!(TaskSpine::current(), Some(legacy));
359    }
360
361    #[tokio::test(flavor = "current_thread")]
362    async fn cancelled_scope_does_not_replace_parent_identity() {
363        let parent = TaskSpine::create_envelope("parent", "cancel-parent", "agent");
364        let child = TaskSpine::create_envelope("child", "cancel-child", "agent");
365        TaskSpine::scope(Some(parent.clone()), async {
366            let task = TaskSpine::scope(Some(child), async {
367                std::future::pending::<()>().await;
368            });
369            assert!(
370                tokio::time::timeout(std::time::Duration::from_millis(10), task)
371                    .await
372                    .is_err()
373            );
374            assert_eq!(TaskSpine::current(), Some(parent));
375        })
376        .await;
377    }
378
379    #[test]
380    fn test_envelope_created_on_dispatch() {
381        let envelope =
382            TaskSpine::create_envelope("query", "session-task-spine", "agent-task-spine");
383        assert_eq!(
384            TaskSpine::task_id().as_deref(),
385            Some(envelope.task_id.as_str())
386        );
387    }
388
389    #[test]
390    fn test_envelope_has_valid_task_id() {
391        let first = TaskSpine::create_envelope("one", "session-task-spine", "agent-task-spine");
392        let second = TaskSpine::create_envelope("two", "session-task-spine", "agent-task-spine");
393        assert!(!first.task_id.as_str().is_empty());
394        assert_ne!(first.task_id, second.task_id);
395    }
396
397    #[test]
398    fn test_enrich_from_triage() {
399        let mut envelope =
400            TaskSpine::create_envelope("query", "session-task-spine", "agent-task-spine");
401        let profile = TaskProfileV1 {
402            primary_intent: "implement".into(),
403            task_class: "coding".into(),
404            complexity: TaskComplexity::High,
405            scope: Default::default(),
406            context_need_milli: 0,
407            reasoning_need_milli: 0,
408            risk_signal: lean_ctx_protocol::RiskClass::High,
409            confidence_milli: 0,
410            capability_id: None,
411            capability_version: None,
412            keywords: vec![],
413            language_hints: vec![],
414        };
415        TaskSpine::enrich_from_triage(&mut envelope, &profile);
416        assert_eq!(envelope.intent.as_deref(), Some("implement"));
417        assert_eq!(envelope.task_class.as_deref(), Some("coding"));
418        assert_eq!(envelope.complexity, TaskComplexity::High);
419        assert_eq!(
420            envelope.risk_class,
421            Some(lean_ctx_protocol::RiskClass::High)
422        );
423    }
424
425    #[test]
426    fn first_session_task_is_parent_of_subsequent_tasks() {
427        let session_id = format!("lineage-session-{}", uuid::Uuid::new_v4());
428        let parent = TaskSpine::create_envelope("first", &session_id, "agent");
429        let child = TaskSpine::create_envelope("second", &session_id, "agent");
430
431        assert!(parent.parent_task_id.is_none());
432        assert_eq!(child.trace_id, parent.trace_id);
433        child.validate_child_of(&parent).unwrap();
434        assert_eq!(
435            child
436                .parent_task_id
437                .as_ref()
438                .map(lean_ctx_protocol::TaskId::as_str),
439            Some(parent.task_id.as_str())
440        );
441        assert_eq!(
442            TaskSpine::current()
443                .as_ref()
444                .map(|envelope| envelope.task_id.as_str()),
445            Some(child.task_id.as_str())
446        );
447    }
448
449    #[test]
450    fn session_traces_are_isolated_and_shared_by_all_children() {
451        let first_session = format!("lineage-first-{}", uuid::Uuid::new_v4());
452        let second_session = format!("lineage-second-{}", uuid::Uuid::new_v4());
453        let first = TaskSpine::create_envelope("root", &first_session, "agent");
454        let second = TaskSpine::create_envelope("root", &second_session, "agent");
455        assert_ne!(first.trace_id, second.trace_id);
456        assert!(second.parent_task_id.is_none());
457        for _ in 0..3 {
458            let child = TaskSpine::create_envelope("child", &first_session, "agent");
459            child.validate_child_of(&first).unwrap();
460            assert_ne!(child.task_id, first.task_id);
461        }
462    }
463}