brainwires-core 0.8.0

Core types, traits, and error handling for the Brainwires Agent Framework
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
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

/// Specifies which contexts can invoke a tool.
/// Implements Anthropic's `allowed_callers` pattern for programmatic tool calling.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum ToolCaller {
    /// Tool can be called directly by the AI
    #[default]
    Direct,
    /// Tool can only be called from within code/script execution
    CodeExecution,
}

/// A tool that can be used by the AI agent
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Tool {
    /// Name of the tool
    #[serde(default)]
    pub name: String,
    /// Description of what the tool does
    #[serde(default)]
    pub description: String,
    /// Input schema (JSON Schema)
    #[serde(default)]
    pub input_schema: ToolInputSchema,
    /// Whether this tool requires user approval before execution
    #[serde(default)]
    pub requires_approval: bool,
    /// Whether this tool should be deferred from initial context loading.
    #[serde(default)]
    pub defer_loading: bool,
    /// Specifies which contexts can call this tool.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub allowed_callers: Vec<ToolCaller>,
    /// Example inputs that teach the AI proper parameter usage.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub input_examples: Vec<Value>,
}

/// JSON Schema for tool input
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolInputSchema {
    /// Schema type (typically "object").
    #[serde(rename = "type", default = "default_schema_type")]
    pub schema_type: String,
    /// Property definitions mapping name to JSON Schema.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub properties: Option<HashMap<String, Value>>,
    /// List of required property names.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<Vec<String>>,
}

fn default_schema_type() -> String {
    "object".to_string()
}

impl Default for ToolInputSchema {
    fn default() -> Self {
        Self {
            schema_type: "object".to_string(),
            properties: None,
            required: None,
        }
    }
}

impl ToolInputSchema {
    /// Create a new object schema
    pub fn object(properties: HashMap<String, Value>, required: Vec<String>) -> Self {
        Self {
            schema_type: "object".to_string(),
            properties: Some(properties),
            required: Some(required),
        }
    }
}

/// A tool use request from the AI
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolUse {
    /// Unique ID for this tool use
    pub id: String,
    /// Name of the tool to use
    pub name: String,
    /// Input parameters for the tool
    pub input: Value,
}

/// Result of a tool execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
    /// ID of the tool use this is a result for
    pub tool_use_id: String,
    /// Result content
    pub content: String,
    /// Whether this is an error result
    #[serde(default)]
    pub is_error: bool,
}

impl ToolResult {
    /// Create a successful tool result
    pub fn success<S: Into<String>>(tool_use_id: S, content: S) -> Self {
        Self {
            tool_use_id: tool_use_id.into(),
            content: content.into(),
            is_error: false,
        }
    }

    /// Create an error tool result
    pub fn error<S: Into<String>>(tool_use_id: S, error: S) -> Self {
        Self {
            tool_use_id: tool_use_id.into(),
            content: error.into(),
            is_error: true,
        }
    }
}

// ── Idempotency registry ─────────────────────────────────────────────────────

/// Record of a completed idempotent write operation.
#[derive(Debug, Clone)]
pub struct IdempotencyRecord {
    /// Unix timestamp of first execution.
    pub executed_at: i64,
    /// The success message returned on first execution (returned verbatim on retries).
    pub cached_result: String,
}

/// Shared registry that deduplicates mutating file-system tool calls within a run.
///
/// Create one per agent run and attach it to `ToolContext` via
/// `ToolContext::with_idempotency_registry`.  All clones of the `ToolContext`
/// share the same underlying map so that idempotency is enforced across the
/// entire run regardless of how many times the context is cloned.
#[derive(Debug, Clone, Default)]
pub struct IdempotencyRegistry(Arc<Mutex<HashMap<String, IdempotencyRecord>>>);

impl IdempotencyRegistry {
    /// Create a new, empty registry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Return the cached result for `key`, or `None` if not yet executed.
    pub fn get(&self, key: &str) -> Option<IdempotencyRecord> {
        self.0
            .lock()
            .expect("idempotency registry lock poisoned")
            .get(key)
            .cloned()
    }

    /// Record that `key` produced `result`.
    ///
    /// If `key` was already recorded (concurrent retry), the first result wins.
    pub fn record(&self, key: String, result: String) {
        let mut map = self.0.lock().expect("idempotency registry lock poisoned");
        map.entry(key).or_insert_with(|| {
            use chrono::Utc;
            IdempotencyRecord {
                executed_at: Utc::now().timestamp(),
                cached_result: result,
            }
        });
    }

    /// Number of recorded operations.
    pub fn len(&self) -> usize {
        self.0
            .lock()
            .expect("idempotency registry lock poisoned")
            .len()
    }

    /// Returns `true` if no operations have been recorded yet.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

// ── Side-effect staging (two-phase commit) ────────────────────────────────────

/// A single write operation that has been staged but not yet committed.
#[derive(Debug, Clone)]
pub struct StagedWrite {
    /// Content-addressed key — used to deduplicate identical staged writes.
    pub key: String,
    /// The absolute target path on the filesystem.
    pub target_path: PathBuf,
    /// UTF-8 content to write on commit.
    pub content: String,
}

/// Result returned by a successful [`StagingBackend::commit`].
#[derive(Debug, Clone)]
pub struct CommitResult {
    /// Number of writes successfully committed to disk.
    pub committed: usize,
    /// The target paths that were written.
    pub paths: Vec<PathBuf>,
}

/// Trait for staging write operations before committing to the filesystem.
///
/// Defined in `brainwires-core` so that [`ToolContext`] can hold an
/// `Arc<dyn StagingBackend>` without depending on `brainwires-tool-system`,
/// which would create a circular crate dependency.
///
/// The concrete implementation lives in `brainwires-tool-system::transaction::TransactionManager`.
pub trait StagingBackend: std::fmt::Debug + Send + Sync {
    /// Stage a write operation.
    ///
    /// Returns `true` if newly staged, `false` if `key` was already present
    /// (idempotent — same key staged twice is a no-op).
    fn stage(&self, write: StagedWrite) -> bool;

    /// Commit all staged writes to the filesystem.
    ///
    /// Each staged file is moved (or copied) to its target path atomically.
    /// On success the staging queue is cleared.
    fn commit(&self) -> anyhow::Result<CommitResult>;

    /// Discard all staged writes without touching the filesystem.
    fn rollback(&self);

    /// Return the number of pending staged writes.
    fn pending_count(&self) -> usize;
}

// ── ToolContext ───────────────────────────────────────────────────────────────

/// Execution context for a tool.
///
/// Provides the working directory, optional metadata, and permission capabilities
/// to tool implementations.
#[derive(Debug, Clone)]
pub struct ToolContext {
    /// Current working directory for resolving relative paths
    pub working_directory: String,
    /// User ID (if authenticated)
    pub user_id: Option<String>,
    /// Additional context data (application-specific key-value pairs)
    pub metadata: HashMap<String, String>,
    /// Agent capabilities for permission checks (serialized as JSON value).
    ///
    /// Consumers should serialize their concrete capability types into this field
    /// and deserialize when reading. This keeps the core crate free of capability
    /// type definitions.
    pub capabilities: Option<Value>,
    /// Per-run idempotency registry for mutating file operations.
    ///
    /// When `Some`, write/delete/edit operations derive a content-addressed key
    /// and skip re-execution if the same key has already been processed in this
    /// run.  `None` disables idempotency tracking (useful for tests or simple
    /// single-call use cases).
    pub idempotency_registry: Option<IdempotencyRegistry>,
    /// Optional two-phase commit staging backend.
    ///
    /// When `Some`, mutating file operations (`write_file`, `edit_file`,
    /// `patch_file`) stage their writes instead of applying them immediately.
    /// The caller is responsible for calling `backend.commit()` to finalize the
    /// writes, or `backend.rollback()` to discard them.
    ///
    /// Staging is checked *after* the idempotency registry: if the same
    /// operation key is already cached, the cached result is returned without
    /// staging again.
    pub staging_backend: Option<Arc<dyn StagingBackend>>,
}

impl ToolContext {
    /// Attach a fresh idempotency registry to this context (builder pattern).
    pub fn with_idempotency_registry(mut self) -> Self {
        self.idempotency_registry = Some(IdempotencyRegistry::new());
        self
    }

    /// Attach a staging backend for two-phase commit file writes (builder pattern).
    pub fn with_staging_backend(mut self, backend: Arc<dyn StagingBackend>) -> Self {
        self.staging_backend = Some(backend);
        self
    }
}

impl Default for ToolContext {
    fn default() -> Self {
        Self {
            working_directory: std::env::current_dir()
                .ok()
                .and_then(|p| p.to_str().map(|s| s.to_string()))
                .unwrap_or_else(|| ".".to_string()),
            user_id: None,
            metadata: HashMap::new(),
            capabilities: None,
            idempotency_registry: None,
            staging_backend: None,
        }
    }
}

/// Tool selection mode
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum ToolMode {
    /// All tools from registry
    Full,
    /// User-selected specific tools (stores tool names)
    Explicit(Vec<String>),
    /// Smart routing based on query analysis (default)
    #[default]
    Smart,
    /// Core tools only
    Core,
    /// No tools enabled
    None,
}

impl ToolMode {
    /// Get a display name for the mode
    pub fn display_name(&self) -> &'static str {
        match self {
            ToolMode::Full => "full",
            ToolMode::Explicit(_) => "explicit",
            ToolMode::Smart => "smart",
            ToolMode::Core => "core",
            ToolMode::None => "none",
        }
    }
}

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

    #[test]
    fn test_tool_result_success() {
        let result = ToolResult::success("tool-1", "Success!");
        assert!(!result.is_error);
    }

    #[test]
    fn test_tool_result_error() {
        let result = ToolResult::error("tool-2", "Failed!");
        assert!(result.is_error);
    }

    #[test]
    fn test_tool_input_schema_object() {
        let mut props = HashMap::new();
        props.insert("name".to_string(), json!({"type": "string"}));
        let schema = ToolInputSchema::object(props, vec!["name".to_string()]);
        assert_eq!(schema.schema_type, "object");
        assert!(schema.properties.is_some());
    }

    #[test]
    fn test_idempotency_registry_basic() {
        let registry = IdempotencyRegistry::new();
        assert!(registry.is_empty());

        registry.record("key-1".to_string(), "result-1".to_string());
        assert_eq!(registry.len(), 1);

        let record = registry.get("key-1").unwrap();
        assert_eq!(record.cached_result, "result-1");
        assert!(record.executed_at > 0);

        // Second record call with same key is a no-op (first result wins)
        registry.record("key-1".to_string(), "result-DIFFERENT".to_string());
        assert_eq!(registry.get("key-1").unwrap().cached_result, "result-1");
        assert_eq!(registry.len(), 1);
    }

    #[test]
    fn test_idempotency_registry_clone_shares_state() {
        let registry = IdempotencyRegistry::new();
        let clone = registry.clone();

        registry.record("k".to_string(), "v".to_string());
        // Clone sees the same entry because it shares the Arc<Mutex<...>>
        assert!(clone.get("k").is_some());
    }

    #[test]
    fn test_tool_context_default_has_no_registry() {
        let ctx = ToolContext::default();
        assert!(ctx.idempotency_registry.is_none());
    }

    #[test]
    fn test_tool_context_with_registry() {
        let ctx = ToolContext::default().with_idempotency_registry();
        assert!(ctx.idempotency_registry.is_some());
        assert!(ctx.idempotency_registry.unwrap().is_empty());
    }
}