causa-runtime 0.0.1

Causa runtime — optional components: execution stack (turn loop, dispatch, streaming, pause/resume, session)
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Tool batch dispatch — dedup-then-parallel execution with panic isolation,
//! call-deadline backstop, and token-limit truncation with artifact spill.
//! Also the tool-catalog composition point: static Rust tools
//! and dynamic sources (`DynamicToolSource`, e.g. MCP servers) merge into
//! one dispatch path; the driver only consumes the assembled surface.

use crate::budget::TokenCounter;
use crate::composition::ToolBridge;
use crate::config::ToolOutputLimits;
use causa_kernel::CallControl;
use causa_kernel::ToolCallPayload;
use causa_kernel::{
    ArtifactHint, ArtifactStore, DynamicToolSource, Tool, ToolCallContext, ToolDefinition,
    ToolSurface,
};
use causa_kernel::{
    ArtifactKind, ArtifactRef, ToolOutput, ToolOutputMeta, ToolResultPayload, ToolResultStatus,
    Truncation,
};
use futures_util::FutureExt;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::Instrument;

/// One registered dynamic source plus its executor-side listing cache.
/// The cache is keyed by the source's own change signal
/// ([`DynamicToolSource::version`]) so a stable catalog costs no
/// round-trip on surface assembly.
struct DynamicEntry {
    source: Arc<dyn DynamicToolSource>,
    cache: std::sync::Mutex<(u64, Vec<ToolDefinition>)>,
}

/// Dynamic-source registry failures.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ToolRegistryError {
    /// A dynamic source with this id is already registered.
    #[error("dynamic source already registered: {0}")]
    DuplicateSource(String),
    /// No dynamic source registered under this id.
    #[error("no dynamic source registered: {0}")]
    UnknownSource(String),
}

/// The tool dispatcher: static tools plus registered dynamic sources
/// behind one execution path — parallel batch dispatch with per-call
/// panic isolation, a call-deadline backstop, and token-limit truncation
/// with artifact spill.
pub struct ToolExecutor {
    tools: HashMap<String, Arc<dyn Tool>>,
    /// Registered dynamic sources; interior-mutable because the executor
    /// lives in an `Arc` shared with the driver.
    dynamic: std::sync::RwLock<Vec<DynamicEntry>>,
}

impl std::fmt::Debug for ToolExecutor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ToolExecutor")
            .field("tools", &self.tools.keys().collect::<Vec<_>>())
            .field(
                "dynamic",
                &self
                    .dynamic
                    .read()
                    .expect("dynamic lock")
                    .iter()
                    .map(|e| e.source.id().to_string())
                    .collect::<Vec<_>>(),
            )
            .finish()
    }
}

impl ToolExecutor {
    /// Build from static tools, keyed by each tool's `definition().name`.
    pub fn from_vec(tools: Vec<Arc<dyn Tool>>) -> Self {
        let mut map = HashMap::new();
        for t in tools {
            map.insert(t.definition().name.clone(), t);
        }
        Self {
            tools: map,
            dynamic: std::sync::RwLock::new(Vec::new()),
        }
    }

    /// Build from an explicit name → tool map; the keys are the caller's
    /// responsibility.
    pub fn from_map(map: HashMap<String, Arc<dyn Tool>>) -> Self {
        Self {
            tools: map,
            dynamic: std::sync::RwLock::new(Vec::new()),
        }
    }

    /// Register a dynamic tool source. Its current listing
    /// enters [`ToolExecutor::tool_surface`], and dispatch routes exactly
    /// the names that listing advertised (listing membership — naming is
    /// the source's own business); a name already present in the static
    /// map keeps winning.
    pub fn register_dynamic(
        &self,
        source: Arc<dyn DynamicToolSource>,
    ) -> Result<(), ToolRegistryError> {
        let mut dynamic = self.dynamic.write().expect("dynamic lock");
        if dynamic.iter().any(|e| e.source.id() == source.id()) {
            return Err(ToolRegistryError::DuplicateSource(source.id().to_string()));
        }
        dynamic.push(DynamicEntry {
            source,
            cache: std::sync::Mutex::new((u64::MAX, Vec::new())),
        });
        Ok(())
    }

    /// Remove a dynamic source; returns it for reconnect-and-reregister
    /// flows.
    pub fn unregister_dynamic(
        &self,
        id: &str,
    ) -> Result<Arc<dyn DynamicToolSource>, ToolRegistryError> {
        let mut dynamic = self.dynamic.write().expect("dynamic lock");
        let pos = dynamic
            .iter()
            .position(|e| e.source.id() == id)
            .ok_or_else(|| ToolRegistryError::UnknownSource(id.to_string()))?;
        Ok(dynamic.remove(pos).source)
    }

    /// Ids of the registered dynamic sources.
    pub fn dynamic_ids(&self) -> Vec<String> {
        self.dynamic
            .read()
            .expect("dynamic lock")
            .iter()
            .map(|e| e.source.id().to_string())
            .collect()
    }

    /// Assemble the model-facing tool surface: every static tool plus the
    /// current listing of every reachable dynamic source. A source that
    /// fails to list keeps serving its last good listing (with a warning);
    /// a source with no cached listing yet is skipped — either way the
    /// turn proceeds and the host can unregister/reconnect. Static names
    /// win collisions, exactly as dispatch does.
    ///
    /// Lock discipline: registry and cache guards are never held across
    /// an `await` — each source is snapshot before the `list()` call and
    /// re-locked to publish the result (clippy `await_holding_lock`).
    pub async fn tool_surface(&self) -> ToolSurface {
        let mut definitions: Vec<ToolDefinition> =
            self.tools.values().map(|t| t.definition()).collect();

        // Snapshot the dynamic registry so no `RwLockReadGuard` crosses an
        // await. Each entry is cloned as `(source, cached_version, cached_defs)`.
        let snapshot: Vec<(Arc<dyn DynamicToolSource>, u64, Vec<ToolDefinition>)> = {
            let dynamic = self.dynamic.read().expect("dynamic lock");
            dynamic
                .iter()
                .map(|entry| {
                    let cache = entry.cache.lock().expect("source cache lock");
                    (entry.source.clone(), cache.0, cache.1.clone())
                })
                .collect()
        };

        for (source, cached_version, cached_defs) in snapshot {
            let observed = source.version();
            let listing = if observed == cached_version && cached_version != u64::MAX {
                Some(cached_defs)
            } else {
                match source.list().await {
                    Ok(fresh) => {
                        // Publish the refreshed listing back into the cache
                        // (re-lock by id — the source is still registered).
                        let dynamic = self.dynamic.read().expect("dynamic lock");
                        if let Some(entry) = dynamic.iter().find(|e| e.source.id() == source.id()) {
                            let mut cache = entry.cache.lock().expect("source cache lock");
                            // Another concurrent surface assembly may have
                            // already refreshed; keep the newest.
                            if cache.0 != observed {
                                cache.0 = observed;
                                cache.1 = fresh.clone();
                            }
                        }
                        Some(fresh)
                    }
                    Err(e) => {
                        tracing::warn!(
                            source = source.id(),
                            error = %e,
                            "dynamic tool source failed to list; serving its last good listing"
                        );
                        (!cached_defs.is_empty()).then_some(cached_defs)
                    }
                }
            };
            if let Some(defs) = listing {
                // Surface must match dispatch: a dynamic name that collides
                // with a static tool is not advertised (the static tool
                // keeps winning there too).
                definitions.extend(
                    defs.into_iter()
                        .filter(|d| !self.tools.contains_key(&d.name)),
                );
            }
        }
        ToolSurface::from_definitions(definitions)
    }

    /// Find the dynamic source owning `tool_name` — routing is by listing
    /// membership: a source is dispatched exactly the names it advertised
    /// in [`ToolExecutor::tool_surface`] (the same cache), so naming stays
    /// the implementor's business and the model can only call what the
    /// surface showed. Returns the source and its advertised definition
    /// for the bridge. The registry/cache locks never cross an await.
    fn find_dynamic(
        &self,
        tool_name: &str,
    ) -> Option<(Arc<dyn DynamicToolSource>, ToolDefinition)> {
        let dynamic = self.dynamic.read().expect("dynamic lock");
        dynamic.iter().find_map(|entry| {
            let cache = entry.cache.lock().expect("source cache lock");
            cache
                .1
                .iter()
                .find(|d| d.name == tool_name)
                .map(|d| (entry.source.clone(), d.clone()))
        })
    }

    /// Execute a single ToolCallPayload with panic isolation, a call-deadline
    /// backstop, and token-limit truncation. Returns the recorded result
    /// only — the caller owns the unknown-outcome action (in the reference
    /// driver that is the turn policy, resolved by the executed tool name
    /// before dispatch). The `effective_limits` argument is the already
    /// chosen per-call limit: the runner resolves the fallback /
    /// per-tool-name override by the executed name before dispatch; a
    /// standalone caller passes the limit it wants.
    pub async fn execute_with_limits(
        &self,
        payload: ToolCallPayload,
        control: CallControl,
        store: Option<Arc<dyn ArtifactStore>>,
        token_counter: Option<Arc<dyn TokenCounter>>,
        effective_limits: ToolOutputLimits,
    ) -> ToolResultPayload {
        // One `agent.tool` span per dispatch, name and id only. Entered
        // per poll via `Instrument`, so the future stays `Send`.
        let span = tracing::info_span!(
            "agent.tool",
            tool_name = %payload.tool_name,
            call_id = %payload.call_id.0
        );
        self.execute_with_limits_inner(payload, control, store, token_counter, effective_limits)
            .instrument(span)
            .await
    }

    async fn execute_with_limits_inner(
        &self,
        payload: ToolCallPayload,
        control: CallControl,
        store: Option<Arc<dyn ArtifactStore>>,
        token_counter: Option<Arc<dyn TokenCounter>>,
        effective_limits: ToolOutputLimits,
    ) -> ToolResultPayload {
        // Route: the static map first, then dynamic sources by listing
        // membership. A dynamic call is wrapped in a [`ToolBridge`] and
        // runs the exact static path below — panic isolation, call-deadline
        // backstop, truncation — one code path, one error mapping (the
        // bridge's).
        let tool: Option<Arc<dyn Tool>> = match self.tools.get(&payload.tool_name) {
            Some(tool) => Some(tool.clone()),
            None => self
                .find_dynamic(&payload.tool_name)
                .map(|(source, definition)| {
                    Arc::new(ToolBridge::new(source, definition)) as Arc<dyn Tool>
                }),
        };
        let Some(tool) = tool else {
            return ToolResultPayload {
                call_id: payload.call_id.clone(),
                status: ToolResultStatus::Rejected,
                output: ToolOutput::new(
                    serde_json::json!({"error": format!("unknown tool: {}", payload.tool_name)}),
                ),
                media: Vec::new(),
            };
        };

        let ctx = ToolCallContext {
            call_id: payload.call_id.clone(),
            tool_name: payload.tool_name.clone(),
            arguments: payload.arguments.clone(),
        };
        let store_ref: Option<&dyn ArtifactStore> =
            store.as_deref().map(|s| s as &dyn ArtifactStore);

        // Panic isolation plus call-deadline backstop: a tool that
        // neither returns nor observes CallControl still yields a structured
        // UnknownOutcome instead of hanging the turn.
        let fut = {
            let tool = tool.clone();
            let control = control.clone();
            std::panic::AssertUnwindSafe(async move {
                tool.execute_with_store(&ctx, &control, store_ref).await
            })
            .catch_unwind()
        };
        let mut result = match control.deadline() {
            Some(deadline) => match tokio::time::timeout_at(deadline.into(), fut).await {
                Ok(Ok(r)) => r,
                Ok(Err(_)) => Self::panicked_outcome(&payload),
                Err(_) => Self::deadline_backstop_outcome(&payload),
            },
            None => match fut.await {
                Ok(r) => r,
                Err(_) => Self::panicked_outcome(&payload),
            },
        };

        // Token-limit truncation (middle truncation + artifact spill).
        // Shape note: on truncation `content` is REPLACED by a JSON string
        // (head + notice + tail) regardless of the original `Value` shape —
        // an object or array observation becomes a string on the wire. The
        // `truncation: Middle` marker and the artifact ref are how consumers
        // detect this.
        //
        // Budget note: the retained head+tail is sized against the DECLARED
        // limit, not the original size — the notice and the JSON-string
        // wrapping are part of the measured output. Every candidate is
        // re-estimated with the wired estimator and the data budget shrinks
        // until the result fits, so the committed content re-estimates at or
        // under `max_tokens` — with one defined floor: when the limit is
        // smaller than the notice's own estimate, the notice alone is
        // emitted (nothing smaller is representable).
        let effective_limit = effective_limits.max_tokens;

        let estimate = |value: &serde_json::Value| -> usize {
            if let Some(counter) = &token_counter {
                counter.estimate_value(value)
            } else {
                // The driver's fallback opinion — single home in `defaults`.
                crate::defaults::placeholder_token_estimate_value(value)
            }
        };
        let estimated = estimate(&result.output.content);

        if estimated > effective_limit {
            let content_str = serde_json::to_string(&result.output.content)
                .unwrap_or_else(|_| result.output.content.to_string());
            let data_bytes = serde_json::to_vec(&result.output.content)
                .unwrap_or_else(|_| content_str.clone().into_bytes());

            let artifact: Option<ArtifactRef> = if let Some(store_arc) = &store {
                let hint = ArtifactHint {
                    tool_name: payload.tool_name.clone(),
                    call_id: payload.call_id.clone(),
                    kind: ArtifactKind::FullOutput,
                };
                store_arc.persist(&data_bytes, hint).await.ok()
            } else {
                None
            };

            let notice = if let Some(ref a) = artifact {
                format!(
                    "\n...[truncated: original {} tokens, artifact:{}]...\n",
                    estimated, a.id
                )
            } else {
                format!(
                    "\n...[truncated: original {} tokens, showing head+tail]...\n",
                    estimated
                )
            };

            // Initial data budget under the chars/4 heuristic with the
            // notice's own footprint reserved; the loop re-measures every
            // candidate with the wired estimator, so any counter converges.
            // The cut floors at budget/8 (geometric convergence) and 1
            // (termination); budget 0 leaves the notice as the floor.
            let mut data_budget = effective_limit
                .saturating_sub(estimate(&serde_json::Value::String(notice.clone())))
                .saturating_mul(4);
            let mut preview_value;
            loop {
                let (head, tail) = split_head_tail(&content_str, data_budget);
                let candidate = serde_json::Value::String(format!("{head}{notice}{tail}"));
                let candidate_tokens = estimate(&candidate);
                let fits = candidate_tokens <= effective_limit;
                preview_value = candidate;
                if fits || data_budget == 0 {
                    break;
                }
                let cut = (candidate_tokens - effective_limit)
                    .saturating_mul(4)
                    .max(data_budget / 8)
                    .max(1);
                data_budget = data_budget.saturating_sub(cut);
            }

            result.output.content = preview_value;
            result.output.truncation = Truncation::Middle;
            result.output.artifact = artifact;
            let prev_meta = result.output.meta.take();
            result.output.meta = Some(ToolOutputMeta {
                duration_ms: prev_meta.as_ref().and_then(|m| m.duration_ms),
                original_tokens: Some(estimated),
                extra: prev_meta.as_ref().and_then(|m| m.extra.clone()),
            });
        }

        result
    }

    fn panicked_outcome(payload: &ToolCallPayload) -> ToolResultPayload {
        ToolResultPayload {
            call_id: payload.call_id.clone(),
            status: ToolResultStatus::Failed,
            output: ToolOutput::new(serde_json::json!({"error": "tool panicked"})),
            media: Vec::new(),
        }
    }

    fn deadline_backstop_outcome(payload: &ToolCallPayload) -> ToolResultPayload {
        ToolResultPayload {
            call_id: payload.call_id.clone(),
            status: ToolResultStatus::UnknownOutcome,
            output: ToolOutput {
                content: serde_json::json!({"error": "tool did not return before call deadline"}),
                truncation: Truncation::None,
                meta: Some(ToolOutputMeta {
                    duration_ms: None,
                    original_tokens: None,
                    extra: Some(serde_json::json!({"reason": "call_deadline_backstop"})),
                }),
                artifact: None,
            },
            media: Vec::new(),
        }
    }
}

/// Head+tail preview split of `s` totalling at most `budget` bytes
/// (60% head / 40% tail), each cut at a char boundary and never
/// overlapping — the retained amount follows the budget, never the
/// original size.
fn split_head_tail(s: &str, budget: usize) -> (&str, &str) {
    let kept = budget.min(s.len());
    let head_end = floor_char_boundary(s, kept * 3 / 5);
    let tail_start = ceil_char_boundary(s, s.len() - (kept - head_end));
    (&s[..head_end], &s[tail_start..])
}

fn floor_char_boundary(s: &str, mut idx: usize) -> usize {
    if idx >= s.len() {
        return s.len();
    }
    while idx > 0 && !s.is_char_boundary(idx) {
        idx -= 1;
    }
    idx
}
fn ceil_char_boundary(s: &str, mut idx: usize) -> usize {
    if idx >= s.len() {
        return s.len();
    }
    while idx < s.len() && !s.is_char_boundary(idx) {
        idx += 1;
    }
    idx
}