causa-kernel 0.3.0

Causa kernel — primitive facts (ContextBlock) and behavior contracts (ports)
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
//! Tool behavior — the `Tool` trait and the `ArtifactStore` port, plus the
//! execution vocabulary they share with drivers: the bound surface and call
//! context. Recorded facts (results, outputs, artifacts) live in
//! `crate::context::tool_data`; batch dispatch lives in `causa-runtime`'s
//! executor. A tool returns its recorded result and nothing else —
//! handling unknown outcomes and retaining output belong to execution
//! consumers, not tool declarations.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;

use crate::context::block::{TextPayload, ToolCallPayload};
use crate::context::ids::BlockId;
use crate::context::tool_data::{ArtifactKind, ArtifactRef, ToolResultPayload};
use crate::context::tools::ToolDefinition;
use crate::ports::control::CallControl;

/// The tool definitions bound for one invocation: what the executor can
/// dispatch this round. A driver records a difference from the context's
/// tool state as a [`crate::ToolChange`] block, and renderers read the tools
/// from the frame, never from this value.
///
/// Serializable for logs and tests; the serialized form is not part of the
/// stable set and may change in any release.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ToolSurface {
    /// Model-facing [`ToolDefinition`]s, in offer order.
    pub definitions: Vec<ToolDefinition>,
}

impl ToolSurface {
    /// A surface offering no tools.
    pub fn empty() -> Self {
        Self {
            definitions: Vec::new(),
        }
    }

    /// Builds a surface from the given definitions.
    pub fn from_definitions(definitions: Vec<ToolDefinition>) -> Self {
        Self { definitions }
    }
}

/// Non-persisted, bind-time typed metadata carried beside [`ToolDefinition`].
///
/// Hosts and tool sources store arbitrary typed values keyed by [`TypeId`].
/// Causa carries metadata from tool binding to [`crate::ProcessorContext`] and
/// never inspects, interprets, or serializes it: metadata never enters
/// [`crate::Context`], [`crate::ToolChange`], or model wire requests, and a
/// change in metadata alone records no [`crate::ToolChange`].
///
/// Cloning is O(1) per entry because each value is reference-counted. An empty
/// `ToolMetadata` allocates nothing.
#[derive(Clone, Default)]
pub struct ToolMetadata {
    entries: HashMap<TypeId, Arc<dyn Any + Send + Sync>>,
}

impl ToolMetadata {
    /// An empty metadata bag.
    pub fn new() -> Self {
        Self::default()
    }

    /// Inserts `value` and returns `self` for chained construction. Replaces
    /// any previous value of type `T`.
    pub fn with<T: Any + Send + Sync>(mut self, value: T) -> Self {
        self.insert(value);
        self
    }

    /// Inserts `value` under `TypeId::of::<T>()`, returning any previous value
    /// of type `T`.
    pub fn insert<T: Any + Send + Sync>(&mut self, value: T) -> Option<Arc<T>> {
        self.entries
            .insert(TypeId::of::<T>(), Arc::new(value))
            .and_then(|prev| prev.downcast::<T>().ok())
    }

    /// Borrows the value stored for type `T`, or `None` if no value of that
    /// exact type is present.
    pub fn get<T: Any>(&self) -> Option<&T> {
        self.entries
            .get(&TypeId::of::<T>())
            .and_then(|arc| (&**arc as &dyn Any).downcast_ref::<T>())
    }

    /// Returns `true` when a value of exact type `T` is stored.
    pub fn contains<T: Any>(&self) -> bool {
        self.entries.contains_key(&TypeId::of::<T>())
    }

    /// Returns the number of stored metadata entries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns `true` when no metadata entries are stored.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

impl fmt::Debug for ToolMetadata {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ToolMetadata")
            .field("len", &self.entries.len())
            .finish_non_exhaustive()
    }
}

/// One tool as its catalog describes it: the model-facing definition and
/// non-persisted [`ToolMetadata`].
///
/// A [`crate::DynamicToolSource`] lists these from
/// [`crate::DynamicToolSource::describe`], and tool processors see the bound
/// set through [`crate::ProcessorContext::tools`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ToolDescription {
    /// The model-facing definition.
    pub definition: ToolDefinition,
    /// Non-persisted metadata declared beside the definition.
    pub metadata: ToolMetadata,
}

impl ToolDescription {
    /// A description with empty metadata.
    pub fn new(definition: ToolDefinition) -> Self {
        Self {
            definition,
            metadata: ToolMetadata::default(),
        }
    }

    /// Replaces the tool metadata.
    pub fn with_metadata(mut self, metadata: ToolMetadata) -> Self {
        self.metadata = metadata;
        self
    }

    /// Inserts a single metadata value of type `T`, replacing any prior value
    /// of the same type.
    pub fn with_meta<T: Any + Send + Sync>(mut self, value: T) -> Self {
        self.metadata.insert(value);
        self
    }
}

/// Non-persisted, call-scoped typed attachments handed from before-processors
/// to tool execution.
///
/// Each entry is stored behind an [`Arc`] keyed by [`TypeId`], keeping
/// [`ToolCallContext::clone`] O(1) per entry during executor dispatch. An
/// empty `CallAttachments` allocates nothing. Attachments are never persisted
/// in [`crate::Context`], never included in [`ToolResultPayload`], and never
/// sent to model protocols; [`crate::ToolBatch::resolve_at`] clears an entry's
/// attachments as soon as the call resolves.
#[derive(Clone, Default)]
pub struct CallAttachments {
    entries: HashMap<TypeId, Arc<dyn Any + Send + Sync>>,
}

impl CallAttachments {
    /// An empty attachment bag.
    pub fn new() -> Self {
        Self::default()
    }

    /// Inserts `value` and returns `self` for chained construction. Replaces
    /// any previous attachment of type `T`.
    pub fn with<T: Any + Send + Sync>(mut self, value: T) -> Self {
        self.insert(value);
        self
    }

    /// Inserts `value` under `TypeId::of::<T>()`, returning any previous
    /// attachment of type `T`.
    pub fn insert<T: Any + Send + Sync>(&mut self, value: T) -> Option<Arc<T>> {
        self.entries
            .insert(TypeId::of::<T>(), Arc::new(value))
            .and_then(|prev| prev.downcast::<T>().ok())
    }

    /// Borrows the attachment stored for type `T`, or `None` if no attachment
    /// of that exact type is present.
    pub fn get<T: Any>(&self) -> Option<&T> {
        self.entries
            .get(&TypeId::of::<T>())
            .and_then(|arc| (&**arc as &dyn Any).downcast_ref::<T>())
    }

    /// Returns `true` when an attachment of exact type `T` is stored.
    pub fn contains<T: Any>(&self) -> bool {
        self.entries.contains_key(&TypeId::of::<T>())
    }

    /// Removes and returns the attachment stored for type `T`, if present.
    pub fn remove<T: Any + Send + Sync>(&mut self) -> Option<Arc<T>> {
        self.entries
            .remove(&TypeId::of::<T>())
            .and_then(|prev| prev.downcast::<T>().ok())
    }

    /// Drops all stored attachments.
    pub fn clear(&mut self) {
        self.entries.clear();
    }

    /// Returns the number of stored attachments.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns `true` when no attachments are stored.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

impl fmt::Debug for CallAttachments {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CallAttachments")
            .field("len", &self.entries.len())
            .finish_non_exhaustive()
    }
}

/// Identity of one tool dispatch, handed to [`Tool::execute`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ToolCallContext {
    /// The declaration block whose result this dispatch produces.
    pub call_block_id: BlockId,
    /// The effective input after any pre-processing edits.
    pub input: ToolCallPayload,
    /// Notes added before execution. `ToolBatch::resolve_at` prepends these
    /// to the returned result's notes; tool implementations should return
    /// only notes they add and must not copy this field into the result.
    pub result_notes: Vec<TextPayload>,
    pub(crate) attachments: CallAttachments,
}
impl ToolCallContext {
    /// A call context with no result notes and no attachments.
    pub fn new(call_block_id: BlockId, input: ToolCallPayload) -> Self {
        Self {
            call_block_id,
            input,
            result_notes: Vec::new(),
            attachments: CallAttachments::default(),
        }
    }

    /// Builds execution material from a committed declaration.
    pub fn from_declaration(call_block_id: BlockId, declaration: &ToolCallPayload) -> Self {
        Self {
            call_block_id,
            input: declaration.clone(),
            result_notes: Vec::new(),
            attachments: CallAttachments::default(),
        }
    }

    /// Borrows the call-scoped attachment of exact type `T`, if present.
    pub fn attachment<T: Any>(&self) -> Option<&T> {
        self.attachments.get::<T>()
    }

    /// Borrows the full call-scoped attachment bag.
    pub fn attachments(&self) -> &CallAttachments {
        &self.attachments
    }

    /// Attaches a value of type `T`, replacing any prior attachment of the same
    /// type. Useful for unit-testing [`Tool::execute`] directly without a
    /// [`crate::ToolBatch`].
    pub fn with_attachment<T: Any + Send + Sync>(mut self, value: T) -> Self {
        self.attachments.insert(value);
        self
    }
}

/// Provenance a caller attaches to bytes handed to [`ArtifactStore::persist`],
/// so the store can label what it receives.
#[non_exhaustive]
pub struct ArtifactHint {
    /// The tool whose output produced the bytes.
    pub tool_name: String,
    /// The tool call the bytes belong to.
    pub call_block_id: BlockId,
    /// The [`ArtifactKind`] classification of the bytes.
    pub kind: ArtifactKind,
}
impl ArtifactHint {
    /// A hint for the artifact one call produced.
    pub fn new(tool_name: impl Into<String>, call_block_id: BlockId, kind: ArtifactKind) -> Self {
        Self {
            tool_name: tool_name.into(),
            call_block_id,
            kind,
        }
    }
}

/// Error surface of the [`ArtifactStore`] port.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum StoreError {
    /// Persisting the artifact failed; carries the human-readable cause.
    #[error("persist failed: {0}")]
    Persist(String),
    /// Reading an artifact back failed; carries the human-readable cause.
    #[error("read failed: {0}")]
    Read(String),
}

/// Persistence port for tool artifacts — where oversized tool outputs are
/// spilled and read back. Callers persist raw bytes plus an [`ArtifactHint`]
/// and receive an [`ArtifactRef`] to embed in the result; concrete stores
/// are host concerns.
#[async_trait]
pub trait ArtifactStore: Send + Sync {
    /// Stores `data`, tagged with `hint`, and returns the [`ArtifactRef`]
    /// describing the persisted artifact.
    async fn persist(&self, data: &[u8], hint: ArtifactHint) -> Result<ArtifactRef, StoreError>;
    /// Reads back the bytes stored under `id`; an optional byte range bounds
    /// the read.
    async fn read(
        &self,
        id: &str,
        range: Option<std::ops::Range<u64>>,
    ) -> Result<Vec<u8>, StoreError>;
}

/// The tool port: one callable tool the model can invoke. Implementations
/// live outside the kernel; the driver's executor dispatches them. A tool
/// returns only its recorded result. Handling unknown outcomes and retaining
/// output belong to the execution consumer, not tool declarations.
///
/// # Requirements on `execute`
///
/// An executor may poll every call of one batch on a single task, with
/// nothing spawned. An implementation therefore:
///
/// - must not block the thread. Blocking or CPU-heavy work runs on a
///   blocking thread (for example `tokio::task::spawn_blocking`) and the
///   tool awaits it;
/// - must watch its [`CallControl`] itself. A turn stop — cancellation or
///   the turn deadline — drops the call future: the executor stops polling,
///   a started call settles as
///   [`ToolResultStatus::UnknownOutcome`](crate::context::tool_data::ToolResultStatus::UnknownOutcome).
///   Once the executor observes the stop it polls the call no further, so a
///   value returned after that is not recorded. Returning promptly on
///   cancellation releases the resources the future still owns; it does not
///   make the result observed;
/// - must return, cleanup included, by its effective call deadline to
///   report
///   [`ToolResultStatus::TimedOut`](crate::context::tool_data::ToolResultStatus::TimedOut)
///   or
///   [`ToolResultStatus::Cancelled`](crate::context::tool_data::ToolResultStatus::Cancelled).
///   The executor enforces a call deadline with `timeout_at`, which polls the
///   call before its own timer and drops the future once the deadline has
///   expired. A tool that races the same deadline and returns without
///   awaiting anything further is therefore recorded. A tool that awaits
///   cleanup after it stops has to stop early enough for that cleanup to
///   finish before the deadline; otherwise the future is dropped mid-cleanup
///   and the call settles as `UnknownOutcome`.
///
/// Dropping the future runs on the executor's task, so [`Drop`] must not
/// block and must be idempotent. A future that starts external work — a
/// spawned task, a child process, a remote operation — cleans it up in
/// [`Drop`].
///
/// For a tool that meets these, a stop takes effect at its next yield point.
/// A tool that holds the thread also holds its sibling calls and the
/// executor's own stop and timeout checks, so nothing is guaranteed for it:
/// not cancellation, not the deadline, not a per-call timeout.
///
/// Calls of one batch are concurrent, not parallel, and their number is not
/// bounded. A tool that needs a limit shares a semaphore across its calls.
#[async_trait]
pub trait Tool: Send + Sync {
    /// The model-facing [`ToolDefinition`] for this tool.
    fn definition(&self) -> ToolDefinition;
    /// Non-persisted metadata carried beside [`Tool::definition`]. An executor
    /// reads it once when binding the tool and exposes it to tool processors
    /// through [`crate::ProcessorContext::tool`] and
    /// [`crate::ProcessorContext::tool_metadata`]. The default is empty.
    fn metadata(&self) -> ToolMetadata {
        ToolMetadata::default()
    }
    /// Runs one call under the call's [`CallControl`], returning the
    /// recorded result. `store` is the host's [`ArtifactStore`] when one is
    /// configured: a tool that produces media or oversized output persists
    /// the bytes there and returns a reference; a tool that does not need
    /// storage ignores it.
    async fn execute(
        &self,
        ctx: &ToolCallContext,
        control: &CallControl,
        store: Option<&dyn ArtifactStore>,
    ) -> ToolResultPayload;
}