Skip to main content

vtcode_core/tools/registry/
harness.rs

1//! Harness context for tool execution tracking.
2//!
3//! This module provides the context wrapper for tracking sessions and tasks
4//! during tool execution.
5
6use arc_swap::{ArcSwap, ArcSwapOption};
7use std::sync::Arc;
8use std::time::SystemTime;
9
10use super::execution_history::HarnessContextSnapshot;
11
12/// Thread-safe context for harness execution.
13///
14/// Tracks session and task IDs across tool invocations.
15#[derive(Debug, Clone)]
16pub struct HarnessContext {
17    session_id: Arc<ArcSwap<String>>,
18    task_id: Arc<ArcSwapOption<String>>,
19}
20
21impl Default for HarnessContext {
22    fn default() -> Self {
23        let session_id = SystemTime::now()
24            .duration_since(SystemTime::UNIX_EPOCH)
25            .map(|d| format!("session-{}", d.as_millis()))
26            .unwrap_or_else(|_| "session-unknown".to_string());
27
28        Self {
29            session_id: Arc::new(ArcSwap::from_pointee(session_id)),
30            task_id: Arc::new(ArcSwapOption::empty()),
31        }
32    }
33}
34
35impl HarnessContext {
36    /// Create a new harness context with a specific session ID.
37    pub fn with_session(session_id: impl Into<String>) -> Self {
38        Self {
39            session_id: Arc::new(ArcSwap::from_pointee(session_id.into())),
40            task_id: Arc::new(ArcSwapOption::empty()),
41        }
42    }
43
44    /// Set the session ID.
45    pub fn set_session_id(&self, session_id: impl Into<String>) {
46        self.session_id.store(Arc::new(session_id.into()));
47    }
48
49    /// Set the task ID.
50    pub fn set_task_id(&self, task_id: Option<String>) {
51        self.task_id.store(task_id.map(Arc::new));
52    }
53
54    /// Get the current session ID.
55    pub fn session_id(&self) -> String {
56        self.session_id.load().as_ref().clone()
57    }
58
59    /// Get the current task ID.
60    pub fn task_id(&self) -> Option<String> {
61        self.task_id.load_full().map(|task_id| (*task_id).clone())
62    }
63
64    /// Create a snapshot of the current context.
65    pub fn snapshot(&self) -> HarnessContextSnapshot {
66        HarnessContextSnapshot::new(self.session_id(), self.task_id())
67    }
68}