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
//! Optional tool-event observation hook for `AgentLoop` (#2056).
//!
//! Why: #2055 built the session event taxonomy and `SessionRegistry::record_tool_*`
//! emission plumbing, but `AgentLoop` had no seam to call it from — its tool
//! dispatch (`dispatch_all`) was a closed loop over `ToolRegistry::dispatch_gated`
//! with no observer. Rather than fork the loop for daemon-driven runs, this adds
//! ONE optional hook: a handler invoked around every tool dispatch. `run_task`'s
//! existing CLI path (which never sets a sink) is completely unaffected — the
//! hook defaults to `None` and costs nothing when absent.
//! What: [`ToolEventSink`] is the trait; `crate::task::SessionToolEventSink`
//! (#2056) is the concrete implementation that forwards to
//! `session::registry::SessionRegistry::record_tool_*`. Kept in `agent_loop`
//! (not `session`) so this crate's lower-level engine module has no dependency
//! on the higher-level session/daemon layer — `session` depends on
//! `agent_loop`, never the reverse.
//! Test: exercised via `agent_loop::tests` (a recording sink asserts hook call
//! order) and `crate::task` tests (the real `SessionToolEventSink`).
use async_trait;
use crateToolTelemetry;
/// One turn's working-context budget measurement, handed to
/// [`ToolEventSink::context_budget`].
///
/// Why: `cadence::CadenceOutcome` is the engine's internal result type and
/// carries no notion of the model's context window — but the Infinite Sessions
/// guarantee a consumer wants to render ("working context >= 60%") is a RATIO
/// against that window. This bundles the outcome with the window and cap the
/// caller resolved, so the sink receives everything a budget indicator needs
/// and re-derives nothing.
/// What: `overhead_tokens` is the measured model-facing transcript size;
/// `overhead_cap_tokens` the hard cap; `context_window` the model's real
/// window; `working_context_pct`/`overhead_pct` the two derived percentages
/// (summing to 100); `within_budget`, `fired`, and `rounds` mirror
/// `CadenceOutcome`.
/// Test: `crate::task::sink::tests::forwards_context_budget`.
/// Observes tool dispatch lifecycle events from an `AgentLoop` run.
///
/// Why: The only way to make an agent loop's tool activity observable to an
/// external subscriber (a `session.attach`ed client) without forking the loop
/// or the tool registry.
/// What: Four hooks; `call_id` is the `ToolCall::id` the model assigned,
/// correlating every hook for the same invocation. All are `&self` (not
/// `&mut self`) so a sink can be shared as `Arc<dyn ToolEventSink>` across a
/// PM loop and every delegated sub-agent loop.
///
/// (UI Phase 1) Every hook takes `agent` — the name of the agent whose loop
/// is dispatching. It is a PARAMETER rather than sink state precisely BECAUSE
/// one sink instance is shared by the PM's loop and every delegated
/// sub-agent's loop (see `task::executor::run_and_record`, which clones one
/// `Arc` into both): a sink that stored its own agent name could only ever
/// report one of them. The calling `AgentLoop` is the only layer that knows
/// which agent it is, so it passes its identity per call — see
/// `AgentLoop::with_agent`.
/// (DOC-39 AC-13.1/13.2) Every hook ALSO takes `agent_id` — the dispatching
/// loop's STABLE per-spawn id (see `AgentLoop::with_agent_id`). `agent`
/// alone cannot distinguish two concurrently-delegated sub-agents that
/// share one name (e.g. two `python-engineer` delegations): both attribute
/// as `"python-engineer"` on this stream, and prior to `agent_id` a client's
/// only recourse was fragile `AgentSpawned`/`AgentDone` stream-ordering
/// inference. `agent_id` is minted once per delegation
/// (`runner::in_process::InProcessAgentRunner::run_pipeline`) and threaded
/// through unchanged for that whole sub-agent run.
/// Test: `crate::task::sink::tests::*`.