Skip to main content

trusty_code/tools/
telemetry.rs

1//! Structured telemetry a tool reports ALONGSIDE its rendered text (UI Phase 1).
2//!
3//! Why: the retrieval tools already know things the UI needs and the event
4//! stream throws away. `search_code` knows which trusty-search lane actually
5//! served a query, how many hits came back, and what it cost;
6//! `recall_session` knows every result's score and which ones its token
7//! budget dropped. Today all of that is flattened into prose and then
8//! TRUNCATED into an `args_preview`/`result_preview` string. This module is
9//! the escape hatch: a tool returns structured data attached to its
10//! [`crate::tools::ToolResult`], and the agent loop — the one layer that
11//! knows WHICH agent is running and owns the `ToolEventSink` — forwards it.
12//!
13//! The alternative (handing every tool a sink plus its own agent name at
14//! construction) was rejected: it would make each tool's attribution an
15//! independent copy of the loop's, free to drift, and would force the
16//! fail-open retrieval tools to grow a second reason to fail. Tools stay
17//! pure functions of their arguments — unit-testable with no sink, no
18//! session, and no bus — and attribution keeps ONE source of truth.
19//! What: [`ToolTelemetry`] is the tool -> loop payload, one variant per
20//! structured event kind. It is deliberately NOT the wire type: the wire
21//! types live in `crate::events` and are built from this by the sink, which
22//! is where `session_id` and `agent` get stamped on.
23//! Test: `crate::agent_loop::tests::sink_events::sink_receives_tool_telemetry_with_agent`
24//! (forwarding), plus each tool's own `telemetry_*` tests (production).
25
26use crate::events::{RecalledMemory, SearchHit};
27
28/// Structured data a tool reports about what it actually did (UI Phase 1).
29///
30/// Why: see the module docs — this is the seam that lets a tool's internal
31/// knowledge (real search lane, per-memory scores, what entered context)
32/// reach the event stream as DATA instead of dying inside its rendered text.
33/// What: one variant per structured event kind; `None` on a `ToolResult`
34/// means "this tool has nothing structured to say", which is every tool but
35/// the two retrieval tools.
36/// Test: `tools::traits::tests::tool_result_carries_optional_telemetry`.
37#[derive(Debug, Clone, PartialEq)]
38pub enum ToolTelemetry {
39    /// A `search_code` call — becomes `Event::SearchPerformed`.
40    Search(SearchTelemetry),
41    /// A `recall_session` call — becomes `Event::MemoryRecalled`.
42    Recall(RecallTelemetry),
43}
44
45/// What a `search_code` call actually did (UI Phase 1).
46///
47/// Why: the UI traces a change back to the searches that drove it, which
48/// needs the REAL lane rather than the mode the model requested — those
49/// differ whenever a not-yet-built index forces a lexical retry (#2783).
50/// What: `lane` is the lane that served the results; `hit_count` is `None`
51/// when the result shape could not be counted (never a misleading `0`);
52/// `hits` carries each result's path + score from that SAME parse (DOC-39
53/// Slice B — `[]` in the uncountable case); `latency_ms` covers the whole
54/// call including any retry.
55/// Test: `tools::trusty_search_tests::resolved_lane_reports_lexical_when_the_retry_served_it`.
56#[derive(Debug, Clone, PartialEq)]
57pub struct SearchTelemetry {
58    /// The trusty-search lane that actually served this query.
59    pub lane: String,
60    /// The query as the model asked it.
61    pub query: String,
62    /// Hits returned, or `None` when uncountable.
63    pub hit_count: Option<usize>,
64    /// Per-hit path + score, in the order the lane returned them (DOC-39
65    /// Slice B). Empty when `hit_count` is `None` (uncountable payload).
66    pub hits: Vec<SearchHit>,
67    /// Wall-clock duration of the whole tool call.
68    pub latency_ms: u64,
69}
70
71/// What a `recall_session` call recalled, and what reached the model
72/// (UI Phase 1).
73///
74/// Why: `injected` is the differentiating signal — see
75/// [`crate::events::Event::MemoryRecalled`].
76/// What: `results` stays in the daemon's score-sorted (highest-first) order.
77/// Test: `tools::recall_session::tests::telemetry_marks_budget_dropped_results_held_back`.
78#[derive(Debug, Clone, PartialEq)]
79pub struct RecallTelemetry {
80    /// The query as the model asked it.
81    pub query: String,
82    /// Every recalled result, score-sorted highest-first, each flagged with
83    /// whether it entered context.
84    pub results: Vec<RecalledMemory>,
85}