Skip to main content

locode_tools/
ctx.rs

1//! The small per-call tool context (ADR-0003).
2
3use std::path::PathBuf;
4
5use tokio_util::sync::CancellationToken;
6
7/// The context handed to a tool for one call.
8///
9/// Deliberately small (ADR-0003 rejects a god-object context): just the working
10/// directory, the id of the `tool_use` being answered, the workspace jail root,
11/// and a cancellation handle. The loop builds one of these per tool call, setting
12/// [`ToolCtx::call_id`] to the `tool_use` id so the produced `tool_result` pairs
13/// back to it (ADR-0004).
14#[derive(Debug, Clone)]
15pub struct ToolCtx {
16    /// The directory the tool should treat as "current" for this call.
17    pub cwd: PathBuf,
18    /// The id of the `tool_use` block this call answers (the pairing link).
19    pub call_id: String,
20    /// The path jail root; the host resolves every path under this (ADR-0008).
21    pub workspace_root: PathBuf,
22    /// Cooperative cancellation: a running tool should observe this and bail out
23    /// cleanly (kill its subprocess, stop reading) when it fires.
24    pub cancel: CancellationToken,
25}
26
27impl ToolCtx {
28    /// Build a context for one tool call.
29    #[must_use]
30    pub fn new(
31        cwd: PathBuf,
32        call_id: String,
33        workspace_root: PathBuf,
34        cancel: CancellationToken,
35    ) -> Self {
36        Self {
37            cwd,
38            call_id,
39            workspace_root,
40            cancel,
41        }
42    }
43}