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
//! Structured telemetry a tool reports ALONGSIDE its rendered text (UI Phase 1).
//!
//! Why: the retrieval tools already know things the UI needs and the event
//! stream throws away. `search_code` knows which trusty-search lane actually
//! served a query, how many hits came back, and what it cost;
//! `recall_session` knows every result's score and which ones its token
//! budget dropped. Today all of that is flattened into prose and then
//! TRUNCATED into an `args_preview`/`result_preview` string. This module is
//! the escape hatch: a tool returns structured data attached to its
//! [`crate::tools::ToolResult`], and the agent loop — the one layer that
//! knows WHICH agent is running and owns the `ToolEventSink` — forwards it.
//!
//! The alternative (handing every tool a sink plus its own agent name at
//! construction) was rejected: it would make each tool's attribution an
//! independent copy of the loop's, free to drift, and would force the
//! fail-open retrieval tools to grow a second reason to fail. Tools stay
//! pure functions of their arguments — unit-testable with no sink, no
//! session, and no bus — and attribution keeps ONE source of truth.
//! What: [`ToolTelemetry`] is the tool -> loop payload, one variant per
//! structured event kind. It is deliberately NOT the wire type: the wire
//! types live in `crate::events` and are built from this by the sink, which
//! is where `session_id` and `agent` get stamped on.
//! Test: `crate::agent_loop::tests::sink_events::sink_receives_tool_telemetry_with_agent`
//! (forwarding), plus each tool's own `telemetry_*` tests (production).
use crate;
/// Structured data a tool reports about what it actually did (UI Phase 1).
///
/// Why: see the module docs — this is the seam that lets a tool's internal
/// knowledge (real search lane, per-memory scores, what entered context)
/// reach the event stream as DATA instead of dying inside its rendered text.
/// What: one variant per structured event kind; `None` on a `ToolResult`
/// means "this tool has nothing structured to say", which is every tool but
/// the two retrieval tools.
/// Test: `tools::traits::tests::tool_result_carries_optional_telemetry`.
/// What a `search_code` call actually did (UI Phase 1).
///
/// Why: the UI traces a change back to the searches that drove it, which
/// needs the REAL lane rather than the mode the model requested — those
/// differ whenever a not-yet-built index forces a lexical retry (#2783).
/// What: `lane` is the lane that served the results; `hit_count` is `None`
/// when the result shape could not be counted (never a misleading `0`);
/// `hits` carries each result's path + score from that SAME parse (DOC-39
/// Slice B — `[]` in the uncountable case); `latency_ms` covers the whole
/// call including any retry.
/// Test: `tools::trusty_search_tests::resolved_lane_reports_lexical_when_the_retry_served_it`.
/// What a `recall_session` call recalled, and what reached the model
/// (UI Phase 1).
///
/// Why: `injected` is the differentiating signal — see
/// [`crate::events::Event::MemoryRecalled`].
/// What: `results` stays in the daemon's score-sorted (highest-first) order.
/// Test: `tools::recall_session::tests::telemetry_marks_budget_dropped_results_held_back`.