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
//! RTK subprocess delegation + the async compression wrapper.
//!
//! Why: When the user has installed RTK (https://github.com/rtk-ai/rtk),
//! delegating to it gets us the upstream implementation for free. When `rtk`
//! is absent we fall back to the native filter chain.
//! What: `compress_via_rtk` (subprocess), `which` (PATH probe), and the
//! `compress_tool_output_async` wrapper that prefers RTK then falls back.
//! Test: `compress_via_rtk_returns_none_when_binary_absent`,
//! `compress_tool_output_async_falls_back_when_rtk_absent` in `tool_output::tests`.
use compress_tool_output;
/// Which code path produced a compressed tool output.
///
/// Why: Issue #1956's `tm compress` stats logging needs to distinguish an
/// `rtk`-binary compression from the always-available native fallback chain
/// so aggregate savings can be broken down by path, matching the spike doc's
/// "compression path: native fallback chain (rtk NOT on PATH)" framing
/// (`docs/specs/tool-output-interception-seam.md`).
/// What: `RtkBinary` when the external `rtk` subprocess produced the result;
/// `NativeFallback` when the in-tree filter chain (`compress_tool_output`)
/// did, either because `rtk` is absent from `PATH` or its invocation failed.
/// Test: `compress_tool_output_async_with_path_reports_native_fallback_when_rtk_absent`.
/// Pipe `output` through the `rtk` CLI subprocess if installed.
///
/// Why: When the user has installed RTK (https://github.com/rtk-ai/rtk),
/// delegating to it gets us the upstream implementation for free, with
/// updates from the source project. When `rtk` is not on `PATH` we fall
/// back to the native filter.
/// What: Spawns `rtk <tool_name>`, writes `output` to stdin, returns stdout.
/// Returns `None` on any failure (missing binary, non-zero exit, stdin/stdout
/// IO error, decode error) so the caller can fall back gracefully.
/// Test: Covered by integration tests when `rtk` is available; unit tests
/// only verify the `None` path when the binary is absent.
pub async
/// Look up an executable on `PATH`. Returns the absolute path if found.
///
/// Why: Avoids depending on the `which` crate while letting us short-circuit
/// when the binary is absent.
/// What: Splits `$PATH` (or `;`-separated on Windows), checks `dir/name`
/// (and `name.exe` on Windows). Returns the first existing match.
/// Compress a tool's output, trying the RTK subprocess first and falling
/// back to the native filter chain.
///
/// Why: Most users won't have RTK installed; the native filters are always
/// available. When RTK is present we delegate so we stay aligned with upstream.
/// What: Async wrapper — calls `compress_via_rtk`, falls back to
/// `compress_tool_output` (synchronous, native) on `None`. Thin wrapper over
/// [`compress_tool_output_async_with_path`] that discards the path signal,
/// kept as the original stable signature so existing callers (`trusty-agents`'s
/// `llm::tool_loop`) are unaffected by the #1959 hoist.
/// Test: `compress_tool_output_async_falls_back_when_rtk_absent`.
pub async
/// Compress a tool's output like [`compress_tool_output_async`], additionally
/// reporting which code path produced the result.
///
/// Why: Issue #1956's `tm compress` subcommand needs a `compression_path`
/// field for its structured stats log so compression effectiveness can be
/// broken down by rtk-binary vs. native-fallback — see [`CompressionPath`].
/// Splitting this out (rather than changing `compress_tool_output_async`'s
/// signature) keeps every existing call site source-compatible.
/// What: Tries `compress_via_rtk` first; returns `(text, RtkBinary)` on
/// success, else `(compress_tool_output(..), NativeFallback)`.
/// Test: `compress_tool_output_async_with_path_reports_native_fallback_when_rtk_absent`.
pub async