返回 CodeWhale
tool_result_retrieval.rs
根目录 / crates / tui / src / tools / tool_result_retrieval.rs
1 //! `retrieve_tool_result` - selective retrieval for spilled tool outputs.
2 //!
3 //! Exact tool evidence is retained under its origin session. Historical
4 //! payloads in the global `tool_outputs/` compatibility directory are readable
5 //! only when a digest-bound ownership sidecar proves they belong to the active
6 //! session.
7
8 use std::fs;
9 use std::path::PathBuf;
10
11 use async_trait::async_trait;
12 use serde_json::{Value, json};
13
14 use super::spec::{
15 ToolCapability, ToolContext, ToolError, ToolResult, ToolSpec, optional_str, optional_u64,
16 required_str,
17 };
18
19 const DEFAULT_MAX_BYTES: usize = 8 * 1024;
20 const HARD_MAX_BYTES: usize = 128 * 1024;
21 const DEFAULT_LINE_COUNT: usize = 40;
22 const HARD_LINE_COUNT: usize = 500;
23 const DEFAULT_MAX_MATCHES: usize = 20;
24 const HARD_MAX_MATCHES: usize = 100;
25 const DEFAULT_CONTEXT_LINES: usize = 1;
26 const HARD_CONTEXT_LINES: usize = 5;
27
28 /// Retrieve summaries or slices of a prior spilled tool result.
29 pub struct RetrieveToolResultTool;
30
31 #[async_trait]
32 impl ToolSpec for RetrieveToolResultTool {
33 fn name(&self) -> &'static str {
34 "retrieve_tool_result"
35 }
36
37 fn description(&self) -> &'static str {
38 "Inspect retained tool evidence with strict session ownership and bounds. Accepts an artifact id, validated session-relative path, or an ownership-proven legacy call/SHA reference. Unowned legacy-global evidence fails closed. Modes: metadata, summary, head, tail, lines, query, bytes. bytes returns a bounded base64 slice for exact text or binary recovery."
39 }
40
41 fn input_schema(&self) -> Value {
42 json!({
43 "type": "object",
44 "properties": {
45 "ref": {
46 "type": "string",
47 "description": "Session-owned artifact id (`art_<id>`) or validated artifact-relative path. Legacy call-id/SHA references work only when origin-session ownership was recorded."
48 },
49 "mode": {
50 "type": "string",
51 "enum": ["metadata", "summary", "head", "tail", "lines", "query", "bytes"],
52 "description": "Retrieval mode. Defaults to summary."
53 },
54 "query": {
55 "type": "string",
56 "description": "Case-insensitive substring to search for when mode=query."
57 },
58 "lines": {
59 "type": "string",
60 "description": "Line selector for mode=lines, e.g. \"10\" or \"10-40\"."
61 },
62 "start_line": {
63 "type": "integer",
64 "description": "1-based first line for mode=lines."
65 },
66 "end_line": {
67 "type": "integer",
68 "description": "1-based final line for mode=lines."
69 },
70 "line_count": {
71 "type": "integer",
72 "description": "Number of lines for head/tail modes. Default 40, hard cap 500."
73 },
74 "max_bytes": {
75 "type": "integer",
76 "description": "Maximum bytes of excerpt text returned. Default 8192, hard cap 131072."
77 },
78 "max_matches": {
79 "type": "integer",
80 "description": "Maximum query matches or signal lines returned. Default 20, hard cap 100."
81 },
82 "context_lines": {
83 "type": "integer",
84 "description": "Extra lines around each query match. Default 1, hard cap 5."
85 },
86 "generation": {
87 "type": "integer",
88 "minimum": 1,
89 "description": "Optional expected evidence generation; mismatches fail closed."
90 },
91 "offset": {
92 "type": "integer",
93 "minimum": 0,
94 "description": "Zero-based byte offset for mode=bytes."
95 },
96 "length": {
97 "type": "integer",
98 "minimum": 1,
99 "description": "Byte count for mode=bytes, capped at max_bytes."
100 }
101 },
102 "required": ["ref"]
103 })
104 }
105
106 fn capabilities(&self) -> Vec<ToolCapability> {
107 vec![ToolCapability::ReadOnly]
108 }
109
110 fn supports_parallel(&self) -> bool {
111 true
112 }
113
114 async fn execute(&self, input: Value, context: &ToolContext) -> Result<ToolResult, ToolError> {
115 let reference = required_str(&input, "ref")?.trim();
116 if reference.is_empty() {
117 return Err(ToolError::invalid_input("ref cannot be empty"));
118 }
119
120 let mode = optional_str(&input, "mode")?
121 .unwrap_or("summary")
122 .trim()
123 .to_ascii_lowercase();
124 let max_bytes = clamp_u64(
125 optional_u64(&input, "max_bytes", DEFAULT_MAX_BYTES as u64)?,
126 1,
127 HARD_MAX_BYTES,
128 );
129 let resolved = resolve_spillover_reference(reference, &context.state_namespace)?;
130 let legacy_ownership = if resolved.kind == ResolvedReferenceKind::LegacyGlobal {
131 Some(authorize_legacy_spillover(
132 &resolved.path,
133 &context.state_namespace,
134 )?)
135 } else {
136 None
137 };
138 let bytes = fs::read(&resolved.path).map_err(|_| {
139 ToolError::execution_failed("evidence is missing or no longer retained")
140 })?;
141 if let Some(ownership) = legacy_ownership {
142 let size = u64::try_from(bytes.len()).unwrap_or(u64::MAX);
143 if ownership.size_bytes != size
144 || ownership.digest != crate::hashing::sha256_hex(&bytes)
145 {
146 return Err(ToolError::execution_failed(
147 "legacy evidence content is corrupt",
148 ));
149 }
150 }
151 let evidence = validate_evidence_if_present(
152 reference,
153 &resolved.path,
154 &bytes,
155 &context.state_namespace,
156 &input,
157 )?;
158 if mode == "metadata" {
159 return ToolResult::json(&json!({
160 "ref": reference,
161 "available": true,
162 "total_bytes": bytes.len(),
163 "evidence": evidence,
164 }))
165 .map_err(|err| ToolError::execution_failed(err.to_string()));
166 }
167 if mode == "bytes" {
168 use base64::Engine as _;
169 let offset = input.get("offset").and_then(Value::as_u64).unwrap_or(0) as usize;
170 let requested = input
171 .get("length")
172 .and_then(Value::as_u64)
173 .unwrap_or(max_bytes as u64) as usize;
174 let end = offset
175 .saturating_add(requested.min(max_bytes))
176 .min(bytes.len());
177 let slice = bytes.get(offset.min(bytes.len())..end).unwrap_or_default();
178 return ToolResult::json(&json!({
179 "ref": reference,
180 "mode": "bytes",
181 "offset": offset,
182 "returned_bytes": slice.len(),
183 "total_bytes": bytes.len(),
184 "encoding": "base64",
185 "data": base64::engine::general_purpose::STANDARD.encode(slice),
186 }))
187 .map_err(|err| ToolError::execution_failed(err.to_string()));
188 }
189 let content = String::from_utf8(bytes).map_err(|_| {
190 ToolError::execution_failed(
191 "evidence encoding is binary; bounded text inspection is unavailable",
192 )
193 })?;
194
195 let lines: Vec<&str> = content.lines().collect();
196 let payload = match mode.as_str() {
197 "summary" => build_summary_payload(reference, &content, &lines, &input, max_bytes)?,
198 "head" => build_head_tail_payload(reference, "head", &lines, &input, max_bytes)?,
199 "tail" => build_head_tail_payload(reference, "tail", &lines, &input, max_bytes)?,
200 "lines" => build_lines_payload(reference, &lines, &input, max_bytes)?,
201 "query" => build_query_payload(reference, &lines, &input, max_bytes)?,
202 other => {
203 return Err(ToolError::invalid_input(format!(
204 "unsupported mode `{other}` (expected metadata, summary, head, tail, lines, query, or bytes)"
205 )));
206 }
207 };
208
209 ToolResult::json(&payload).map_err(|err| {
210 ToolError::execution_failed(format!("failed to serialize result: {err}"))
211 })
212 }
213 }
214
215 fn validate_evidence_if_present(
216 reference: &str,
217 path: &std::path::Path,
218 bytes: &[u8],
219 session_id: &str,
220 input: &Value,
221 ) -> Result<Option<crate::tools::large_output_router::EvidenceArtifact>, ToolError> {
222 let handle = path
223 .file_stem()
224 .and_then(|stem| stem.to_str())
225 .filter(|stem| stem.starts_with("art_"))
226 .or_else(|| {
227 reference
228 .trim()
229 .starts_with("art_")
230 .then(|| reference.trim())
231 });
232 let Some(handle) = handle else {
233 return Ok(None);
234 };
235 let metadata =
236 match crate::tools::large_output_router::read_evidence_metadata(session_id, handle) {
237 Ok(metadata) => metadata,
238 Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
239 Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => {
240 return Err(ToolError::permission_denied(
241 "evidence belongs to another session",
242 ));
243 }
244 Err(_) => return Err(ToolError::execution_failed("evidence metadata is corrupt")),
245 };
246 if metadata.origin_session != session_id || metadata.handle != handle {
247 return Err(ToolError::permission_denied(
248 "evidence belongs to another session",
249 ));
250 }
251 if metadata.redacted {
252 return Err(ToolError::permission_denied("evidence has been redacted"));
253 }
254 if crate::tools::large_output_router::evidence_is_expired(
255 &metadata,
256 crate::tools::large_output_router::unix_millis_now(),
257 ) {
258 return Err(ToolError::execution_failed(
259 "evidence retention has expired",
260 ));
261 }
262 if input
263 .get("generation")
264 .and_then(Value::as_u64)
265 .is_some_and(|generation| generation != u64::from(metadata.generation))
266 {
267 return Err(ToolError::execution_failed(
268 "evidence generation does not match",
269 ));
270 }
271 if metadata.size_bytes != u64::try_from(bytes.len()).unwrap_or(u64::MAX)
272 || metadata.digest != crate::hashing::sha256_hex(bytes)
273 {
274 return Err(ToolError::execution_failed("evidence content is corrupt"));
275 }
276 Ok(Some(metadata))
277 }
278
279 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
280 enum ResolvedReferenceKind {
281 ActiveSession,
282 LegacyGlobal,
283 }
284
285 #[derive(Debug, Clone, PartialEq, Eq)]
286 struct ResolvedSpilloverReference {
287 path: PathBuf,
288 kind: ResolvedReferenceKind,
289 }
290
291 fn authorize_legacy_spillover(
292 path: &std::path::Path,
293 session_id: &str,
294 ) -> Result<crate::tools::truncate::LegacySpilloverOwnership, ToolError> {
295 if session_id.trim().is_empty() {
296 return Err(ToolError::permission_denied(
297 "legacy evidence has no verifiable session owner",
298 ));
299 }
300 let ownership = match crate::tools::truncate::read_legacy_spillover_ownership(path) {
301 Ok(ownership) => ownership,
302 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
303 return Err(ToolError::permission_denied(
304 "legacy evidence has no verifiable session owner",
305 ));
306 }
307 Err(error) if error.kind() == std::io::ErrorKind::PermissionDenied => {
308 return Err(ToolError::permission_denied(
309 "legacy evidence ownership proof is invalid",
310 ));
311 }
312 Err(_) => {
313 return Err(ToolError::execution_failed(
314 "legacy evidence ownership metadata is corrupt",
315 ));
316 }
317 };
318 if ownership.origin_session != session_id {
319 return Err(ToolError::permission_denied(
320 "legacy evidence belongs to another session",
321 ));
322 }
323 Ok(ownership)
324 }
325
326 /// Resolve a tool-result ref without weakening its ownership boundary.
327 ///
328 /// Current-session artifacts always win over same-named global compatibility
329 /// files. A legacy call-id, SHA, relative path, or absolute path can resolve,
330 /// but the caller must validate its ownership sidecar before reading bytes.
331 fn resolve_spillover_reference(
332 reference: &str,
333 session_id: &str,
334 ) -> Result<ResolvedSpilloverReference, ToolError> {
335 let root = crate::tools::truncate::spillover_root()
336 .ok_or_else(|| ToolError::execution_failed("retained evidence storage is unavailable"))?;
337 let root_canonical = root.canonicalize().ok();
338
339 // Resolve the session's `artifacts/` directory.
340 // `session_artifact_absolute_path(sid, p)` returns
341 // `~/.codewhale/sessions/<sid>/<p>` — so passing the literal
342 // `ARTIFACTS_DIR_NAME` ("artifacts") gets us the real artifacts
343 // root. An earlier draft passed `Path::new(".")` and took
344 // `.parent()`, which landed one directory too high (`<sid>` instead
345 // of `<sid>/artifacts`) and silently broke every bare `art_<id>`
346 // ref — only the legacy-spillover fallback survived. The test
347 // `resolves_art_prefix_to_legacy_spillover_id` masked it because
348 // it ONLY wrote a legacy spillover file. The new test
349 // `resolves_art_prefix_via_session_artifacts` exercises the real
350 // path.
351 let session_artifacts_root = if !session_id.is_empty() {
352 crate::artifacts::session_artifact_absolute_path(
353 session_id,
354 std::path::Path::new(crate::artifacts::ARTIFACTS_DIR_NAME),
355 )
356 } else {
357 None
358 };
359 let session_artifacts_root_canonical = session_artifacts_root
360 .as_ref()
361 .and_then(|p| p.canonicalize().ok());
362
363 let trimmed = reference.trim();
364 let stripped = trimmed
365 .strip_prefix("tool_result:")
366 .unwrap_or(trimmed)
367 .trim();
368
369 let mut tried = 0_usize;
370 let try_path = |candidate: PathBuf, tried: &mut usize| -> Option<ResolvedSpilloverReference> {
371 *tried = (*tried).saturating_add(1);
372
373 // Reject symlinks at the leaf BEFORE canonicalizing so an
374 // attacker who can write under `<sid>/artifacts/` cannot
375 // plant a symlink to `/etc/passwd` and read it back through
376 // `retrieve_tool_result`. canonicalize() would happily
377 // follow such a link and then pass the `starts_with(root)`
378 // check because of the resolved-then-compare order. Both session and
379 // compatibility roots reject leaf symlinks. Legacy
380 // compatibility files also need a separate ownership sidecar below.
381 if let Ok(meta) = std::fs::symlink_metadata(&candidate)
382 && meta.file_type().is_symlink()
383 {
384 return None;
385 }
386
387 let canonical = candidate.canonicalize().ok()?;
388 if !canonical.is_file() {
389 return None;
390 }
391 let inside_legacy = root_canonical
392 .as_ref()
393 .is_some_and(|root| canonical.starts_with(root));
394 let inside_session = session_artifacts_root_canonical
395 .as_ref()
396 .is_some_and(|root| canonical.starts_with(root));
397 if inside_session {
398 Some(ResolvedSpilloverReference {
399 path: canonical,
400 kind: ResolvedReferenceKind::ActiveSession,
401 })
402 } else if inside_legacy {
403 Some(ResolvedSpilloverReference {
404 path: canonical,
405 kind: ResolvedReferenceKind::LegacyGlobal,
406 })
407 } else {
408 None
409 }
410 };
411
412 // Form 1/3: absolute path. Validate it lives under one of the allowed roots.
413 let raw_path = PathBuf::from(stripped);
414 if raw_path.is_absolute() {
415 if let Some(found) = try_path(raw_path, &mut tried) {
416 return Ok(found);
417 }
418 return Err(ToolError::permission_denied(
419 "evidence path is not owned by the active session",
420 ));
421 }
422
423 // Session artifact paths take priority over legacy-global lookups.
424 let looks_like_path = stripped.ends_with(".txt")
425 || stripped.contains('/')
426 || (std::path::MAIN_SEPARATOR != '/' && stripped.contains(std::path::MAIN_SEPARATOR));
427 if looks_like_path {
428 if let Some(sa_root) = session_artifacts_root.as_ref() {
429 let rel = stripped.strip_prefix("artifacts/").unwrap_or(stripped);
430 if let Some(found) = try_path(sa_root.join(rel), &mut tried) {
431 return Ok(found);
432 }
433 }
434 if let Some(found) = try_path(root.join(stripped), &mut tried) {
435 return Ok(found);
436 }
437 return Err(not_found(reference, tried));
438 }
439
440 if let Some(sa_root) = session_artifacts_root.as_ref() {
441 let file_name = if stripped.starts_with("art_") {
442 format!("{stripped}.txt")
443 } else {
444 format!("art_{stripped}.txt")
445 };
446 if let Some(found) = try_path(sa_root.join(file_name), &mut tried) {
447 return Ok(found);
448 }
449 }
450
451 // `sha:<hex>` or bare 64-hex resolves only as legacy-global evidence and
452 // therefore still requires an ownership sidecar in the caller.
453 let sha_candidate = stripped
454 .strip_prefix("sha:")
455 .or_else(|| stripped.strip_prefix("sha_"))
456 .unwrap_or(stripped)
457 .trim();
458 if crate::tools::truncate::is_valid_sha256(&sha_candidate.to_ascii_lowercase())
459 && let Some(p) = crate::tools::truncate::sha_spillover_path(sha_candidate)
460 && let Some(found) = try_path(p, &mut tried)
461 {
462 return Ok(found);
463 }
464
465 // Compatibility lookup: `art_<id>` may name the historical `<id>.txt`.
466 if let Some(stripped_art) = stripped.strip_prefix("art_")
467 && let Some(p) = crate::tools::truncate::spillover_path(stripped_art)
468 && let Some(found) = try_path(p, &mut tried)
469 {
470 return Ok(found);
471 }
472
473 if let Some(path) = crate::tools::truncate::spillover_path(stripped)
474 && let Some(found) = try_path(path, &mut tried)
475 {
476 return Ok(found);
477 }
478
479 Err(not_found(reference, tried))
480 }
481
482 /// Missing evidence is distinct without revealing storage roots or generated
483 /// session identifiers to the model.
484 fn not_found(reference: &str, tried: usize) -> ToolError {
485 ToolError::execution_failed(format!(
486 "retained evidence `{reference}` was not found for the active session \
487 ({tried} bounded candidate forms checked). Use the session-owned \
488 `art_<id>` handle from the original receipt."
489 ))
490 }
491
492 fn build_summary_payload(
493 reference: &str,
494 content: &str,
495 lines: &[&str],
496 input: &Value,
497 max_bytes: usize,
498 ) -> Result<Value, ToolError> {
499 let max_matches = clamp_u64(
500 optional_u64(input, "max_matches", DEFAULT_MAX_MATCHES as u64)?,
501 1,
502 HARD_MAX_MATCHES,
503 );
504 let signal_lines = collect_signal_lines(lines, max_matches);
505 let head_count = DEFAULT_LINE_COUNT.min(lines.len());
506 let tail_count = DEFAULT_LINE_COUNT.min(lines.len());
507 let head = render_numbered_lines(
508 lines
509 .iter()
510 .take(head_count)
511 .enumerate()
512 .map(|(idx, line)| (idx + 1, *line)),
513 max_bytes / 2,
514 );
515 let tail_start = lines.len().saturating_sub(tail_count);
516 let tail = render_numbered_lines(
517 lines
518 .iter()
519 .enumerate()
520 .skip(tail_start)
521 .map(|(idx, line)| (idx + 1, *line)),
522 max_bytes / 2,
523 );
524
525 Ok(json!({
526 "ref": reference,
527 "mode": "summary",
528 "total_bytes": content.len(),
529 "total_lines": lines.len(),
530 "non_empty_lines": lines.iter().filter(|line| !line.trim().is_empty()).count(),
531 "signal_lines": signal_lines,
532 "head": head,
533 "tail": tail,
534 "hint": "Use mode=head, tail, lines, or query to retrieve a narrower slice."
535 }))
536 }
537
538 fn build_head_tail_payload(
539 reference: &str,
540 mode: &str,
541 lines: &[&str],
542 input: &Value,
543 max_bytes: usize,
544 ) -> Result<Value, ToolError> {
545 let count = clamp_u64(
546 optional_u64(input, "line_count", DEFAULT_LINE_COUNT as u64)?,
547 1,
548 HARD_LINE_COUNT,
549 );
550 let selected: Vec<(usize, &str)> = if mode == "head" {
551 lines
552 .iter()
553 .take(count)
554 .enumerate()
555 .map(|(idx, line)| (idx + 1, *line))
556 .collect()
557 } else {
558 let start = lines.len().saturating_sub(count);
559 lines
560 .iter()
561 .enumerate()
562 .skip(start)
563 .map(|(idx, line)| (idx + 1, *line))
564 .collect()
565 };
566 let excerpt = render_numbered_lines(selected.iter().copied(), max_bytes);
567
568 Ok(json!({
569 "ref": reference,
570 "mode": mode,
571 "total_lines": lines.len(),
572 "line_count": count,
573 "excerpt": excerpt,
574 }))
575 }
576
577 fn build_lines_payload(
578 reference: &str,
579 lines: &[&str],
580 input: &Value,
581 max_bytes: usize,
582 ) -> Result<Value, ToolError> {
583 let (start, end) = parse_line_selector(input)?;
584 let excerpt = if start > lines.len() {
585 String::new()
586 } else {
587 let end = end.min(lines.len());
588 render_numbered_lines(
589 lines
590 .iter()
591 .enumerate()
592 .skip(start - 1)
593 .take(end.saturating_sub(start) + 1)
594 .map(|(idx, line)| (idx + 1, *line)),
595 max_bytes,
596 )
597 };
598
599 Ok(json!({
600 "ref": reference,
601 "mode": "lines",
602 "total_lines": lines.len(),
603 "start_line": start,
604 "end_line": end.min(lines.len()),
605 "excerpt": excerpt,
606 }))
607 }
608
609 fn build_query_payload(
610 reference: &str,
611 lines: &[&str],
612 input: &Value,
613 max_bytes: usize,
614 ) -> Result<Value, ToolError> {
615 let query = optional_str(input, "query")?
616 .map(str::trim)
617 .filter(|q| !q.is_empty())
618 .ok_or_else(|| ToolError::invalid_input("query is required when mode=query"))?;
619 let query_lower = query.to_lowercase();
620 let max_matches = clamp_u64(
621 optional_u64(input, "max_matches", DEFAULT_MAX_MATCHES as u64)?,
622 1,
623 HARD_MAX_MATCHES,
624 );
625 let context_lines = clamp_u64(
626 optional_u64(input, "context_lines", DEFAULT_CONTEXT_LINES as u64)?,
627 0,
628 HARD_CONTEXT_LINES,
629 );
630
631 let mut matched_lines = 0usize;
632 let mut results = Vec::new();
633 for (idx, line) in lines.iter().enumerate() {
634 if !line.to_lowercase().contains(&query_lower) {
635 continue;
636 }
637 matched_lines += 1;
638 if results.len() >= max_matches {
639 continue;
640 }
641 let start = idx.saturating_sub(context_lines);
642 let end = (idx + context_lines).min(lines.len().saturating_sub(1));
643 let excerpt = render_numbered_lines(
644 lines
645 .iter()
646 .enumerate()
647 .skip(start)
648 .take(end.saturating_sub(start) + 1)
649 .map(|(line_idx, text)| (line_idx + 1, *text)),
650 max_bytes / max_matches.max(1),
651 );
652 results.push(json!({
653 "line": idx + 1,
654 "excerpt": excerpt,
655 }));
656 }
657
658 Ok(json!({
659 "ref": reference,
660 "mode": "query",
661 "query": query,
662 "total_lines": lines.len(),
663 "matched_lines": matched_lines,
664 "matches_returned": results.len(),
665 "results": results,
666 }))
667 }
668
669 fn parse_line_selector(input: &Value) -> Result<(usize, usize), ToolError> {
670 let explicit_start = input.get("start_line").and_then(Value::as_u64);
671 let explicit_end = input.get("end_line").and_then(Value::as_u64);
672 if explicit_start.is_some() || explicit_end.is_some() {
673 let start = explicit_start.ok_or_else(|| {
674 ToolError::invalid_input("start_line is required when end_line is supplied")
675 })?;
676 let end = explicit_end.unwrap_or(start);
677 return validate_line_range(start as usize, end as usize);
678 }
679
680 let spec = optional_str(input, "lines")?
681 .map(str::trim)
682 .filter(|s| !s.is_empty())
683 .ok_or_else(|| {
684 ToolError::invalid_input(
685 "mode=lines requires `lines` (for example \"10-40\") or start_line/end_line",
686 )
687 })?;
688
689 if let Some((start, end)) = spec.split_once('-') {
690 let start = parse_positive_line(start.trim(), "lines start")?;
691 let end = parse_positive_line(end.trim(), "lines end")?;
692 validate_line_range(start, end)
693 } else {
694 let line = parse_positive_line(spec, "lines")?;
695 validate_line_range(line, line)
696 }
697 }
698
699 fn validate_line_range(start: usize, end: usize) -> Result<(usize, usize), ToolError> {
700 if start == 0 || end == 0 {
701 return Err(ToolError::invalid_input("line numbers are 1-based"));
702 }
703 if end < start {
704 return Err(ToolError::invalid_input(
705 "end_line must be greater than or equal to start_line",
706 ));
707 }
708 Ok((start, end))
709 }
710
711 fn parse_positive_line(raw: &str, field: &str) -> Result<usize, ToolError> {
712 raw.parse::<usize>().map_err(|_| {
713 ToolError::invalid_input(format!("{field} must be a positive integer line number"))
714 })
715 }
716
717 fn collect_signal_lines(lines: &[&str], max_matches: usize) -> Vec<Value> {
718 let mut out = Vec::new();
719 for (idx, line) in lines.iter().enumerate() {
720 if !is_signal_line(line) {
721 continue;
722 }
723 out.push(json!({
724 "line": idx + 1,
725 "text": truncate_line(line.trim(), 300),
726 }));
727 if out.len() >= max_matches {
728 break;
729 }
730 }
731 out
732 }
733
734 fn is_signal_line(line: &str) -> bool {
735 let lower = line.to_lowercase();
736 [
737 "error",
738 "failed",
739 "failure",
740 "panic",
741 "warning",
742 "exception",
743 "traceback",
744 "assertion",
745 "exit code",
746 "test result",
747 "thread '",
748 ]
749 .iter()
750 .any(|needle| lower.contains(needle))
751 }
752
753 fn render_numbered_lines<'a>(
754 lines: impl IntoIterator<Item = (usize, &'a str)>,
755 max_bytes: usize,
756 ) -> String {
757 let mut rendered = String::new();
758 for (line_no, line) in lines {
759 rendered.push_str(&format!("{line_no}: {line}\n"));
760 if rendered.len() > max_bytes {
761 break;
762 }
763 }
764 truncate_text(&rendered, max_bytes)
765 }
766
767 fn truncate_text(text: &str, max_bytes: usize) -> String {
768 if text.len() <= max_bytes {
769 return text.trim_end_matches('\n').to_string();
770 }
771 let note = "\n[truncated to max_bytes]";
772 let budget = max_bytes.saturating_sub(note.len()).max(1);
773 let cut = (0..=budget)
774 .rev()
775 .find(|idx| text.is_char_boundary(*idx))
776 .unwrap_or(0);
777 format!("{}{}", text[..cut].trim_end_matches('\n'), note)
778 }
779
780 fn truncate_line(line: &str, max_chars: usize) -> String {
781 if line.chars().count() <= max_chars {
782 return line.to_string();
783 }
784 let mut out: String = line.chars().take(max_chars.saturating_sub(3)).collect();
785 out.push_str("...");
786 out
787 }
788
789 fn clamp_u64(value: u64, min: usize, max: usize) -> usize {
790 (value as usize).clamp(min, max)
791 }
792
793 #[cfg(test)]
794 mod tests {
795 use super::*;
796 use std::sync::MutexGuard;
797 use tempfile::tempdir;
798
799 struct SpilloverRootGuard {
800 prior: Option<PathBuf>,
801 }
802
803 impl Drop for SpilloverRootGuard {
804 fn drop(&mut self) {
805 crate::tools::truncate::set_test_spillover_root(self.prior.take());
806 }
807 }
808
809 fn set_spillover_root(path: PathBuf) -> SpilloverRootGuard {
810 let prior = crate::tools::truncate::set_test_spillover_root(Some(path));
811 SpilloverRootGuard { prior }
812 }
813
814 fn context() -> ToolContext {
815 let tmp = tempdir().unwrap();
816 ToolContext::new(tmp.path())
817 }
818
819 fn test_lock() -> MutexGuard<'static, ()> {
820 crate::tools::truncate::TEST_SPILLOVER_GUARD
821 .lock()
822 .unwrap_or_else(|err| err.into_inner())
823 }
824
825 fn execute_tool(input: Value) -> Result<ToolResult, ToolError> {
826 let runtime = tokio::runtime::Builder::new_current_thread()
827 .enable_all()
828 .build()
829 .unwrap();
830 runtime.block_on(RetrieveToolResultTool.execute(input, &context()))
831 }
832
833 fn execute_tool_in_session(input: Value, session_id: &str) -> Result<ToolResult, ToolError> {
834 let runtime = tokio::runtime::Builder::new_current_thread()
835 .enable_all()
836 .build()
837 .unwrap();
838 let mut context = context();
839 context.state_namespace = session_id.to_string();
840 runtime.block_on(RetrieveToolResultTool.execute(input, &context))
841 }
842
843 fn publish_test_evidence(
844 session_id: &str,
845 handle: &str,
846 bytes: &[u8],
847 expired: bool,
848 ) -> crate::tools::large_output_router::EvidenceArtifact {
849 let relative = crate::artifacts::session_artifact_relative_path(handle);
850 crate::artifacts::write_session_relative_immutable(session_id, &relative, bytes).unwrap();
851 let now = crate::tools::large_output_router::unix_millis_now();
852 let artifact = crate::tools::large_output_router::EvidenceArtifact {
853 handle: handle.to_string(),
854 digest: crate::hashing::sha256_hex(bytes),
855 size_bytes: bytes.len() as u64,
856 content_type: "application/octet-stream".to_string(),
857 tool_name: "exec_shell".to_string(),
858 call_id: handle.trim_start_matches("art_").to_string(),
859 origin_session: session_id.to_string(),
860 generation: 1,
861 redacted: false,
862 encoding: "binary".to_string(),
863 retention_state: if expired {
864 crate::tools::large_output_router::EvidenceRetentionState::Expired
865 } else {
866 crate::tools::large_output_router::EvidenceRetentionState::Live
867 },
868 created_at_unix_ms: now,
869 retain_until_unix_ms: now.saturating_add(60_000),
870 storage_path: relative,
871 };
872 crate::tools::large_output_router::publish_evidence_metadata(session_id, &artifact)
873 .unwrap();
874 artifact
875 }
876
877 fn write_owned_legacy(id: &str, content: &str, session_id: &str) -> PathBuf {
878 let path = crate::tools::truncate::write_spillover(id, content).unwrap();
879 crate::tools::truncate::publish_legacy_spillover_ownership(
880 &path,
881 session_id,
882 content.as_bytes(),
883 )
884 .unwrap();
885 path
886 }
887
888 fn write_owned_sha(content: &str, session_id: &str) -> (String, PathBuf) {
889 let sha = crate::hashing::sha256_hex(content.as_bytes());
890 let path = crate::tools::truncate::write_sha_spillover(&sha, content).unwrap();
891 crate::tools::truncate::publish_legacy_spillover_ownership(
892 &path,
893 session_id,
894 content.as_bytes(),
895 )
896 .unwrap();
897 (sha, path)
898 }
899
900 #[test]
901 fn summary_reads_spillover_by_tool_call_id() {
902 let _lock = test_lock();
903 let tmp = tempdir().unwrap();
904 let _guard = set_spillover_root(tmp.path().join("tool_outputs"));
905 let session_id = "session-legacy-summary";
906 write_owned_legacy(
907 "call-abc",
908 "checking crate\nerror[E0425]: missing value\nwarning: unused import\nfinished",
909 session_id,
910 );
911
912 let result = execute_tool_in_session(json!({"ref": "call-abc"}), session_id).unwrap();
913
914 assert!(result.success);
915 let body: Value = serde_json::from_str(&result.content).unwrap();
916 assert_eq!(body["mode"], "summary");
917 assert!(body["signal_lines"].to_string().contains("error[E0425]"));
918 assert!(body["signal_lines"].to_string().contains("warning"));
919 }
920
921 #[test]
922 fn adaptive_evidence_binary_bytes_are_exact_and_bounded() {
923 let _spill = test_lock();
924 let _artifact = crate::artifacts::TEST_ARTIFACT_SESSIONS_GUARD
925 .lock()
926 .unwrap_or_else(|err| err.into_inner());
927 let tmp = tempdir().unwrap();
928 let _root = set_spillover_root(tmp.path().join("tool_outputs"));
929 let prior =
930 crate::artifacts::set_test_artifact_sessions_root(Some(tmp.path().join("sessions")));
931 struct Restore(Option<PathBuf>);
932 impl Drop for Restore {
933 fn drop(&mut self) {
934 crate::artifacts::set_test_artifact_sessions_root(self.0.take());
935 }
936 }
937 let _restore = Restore(prior);
938 let bytes = b"\0\xffbinary\nDEEP_SENTINEL\x80tail";
939 publish_test_evidence("session-a", "art_call-binary", bytes, false);
940
941 let result = execute_tool_in_session(
942 json!({"ref": "art_call-binary", "mode": "bytes", "offset": 0, "length": 1024}),
943 "session-a",
944 )
945 .unwrap();
946 let body: Value = serde_json::from_str(&result.content).unwrap();
947 use base64::Engine as _;
948 let decoded = base64::engine::general_purpose::STANDARD
949 .decode(body["data"].as_str().unwrap())
950 .unwrap();
951 assert_eq!(decoded, bytes);
952 assert_eq!(body["total_bytes"], bytes.len());
953 }
954
955 #[test]
956 fn adaptive_evidence_retrieves_after_restart_without_memory_state() {
957 let _spill = test_lock();
958 let _artifact = crate::artifacts::TEST_ARTIFACT_SESSIONS_GUARD
959 .lock()
960 .unwrap_or_else(|err| err.into_inner());
961 let tmp = tempdir().unwrap();
962 let _root = set_spillover_root(tmp.path().join("tool_outputs"));
963 let prior =
964 crate::artifacts::set_test_artifact_sessions_root(Some(tmp.path().join("sessions")));
965 struct Restore(Option<PathBuf>);
966 impl Drop for Restore {
967 fn drop(&mut self) {
968 crate::artifacts::set_test_artifact_sessions_root(self.0.take());
969 }
970 }
971 let _restore = Restore(prior);
972
973 let bytes = b"restart-proof\nDEEP_RESTART_SENTINEL\nend";
974 publish_test_evidence("session-restart", "art_call-restart", bytes, false);
975
976 // Construct two independent contexts to model process teardown and
977 // resume. Retrieval must depend only on the sealed session artifact
978 // and metadata, never an in-memory routing table from publication.
979 let first = execute_tool_in_session(
980 json!({"ref": "art_call-restart", "mode": "metadata"}),
981 "session-restart",
982 )
983 .unwrap();
984 drop(first);
985 let resumed = execute_tool_in_session(
986 json!({"ref": "art_call-restart", "mode": "bytes", "length": 4096}),
987 "session-restart",
988 )
989 .unwrap();
990 let body: Value = serde_json::from_str(&resumed.content).unwrap();
991 use base64::Engine as _;
992 let decoded = base64::engine::general_purpose::STANDARD
993 .decode(body["data"].as_str().unwrap())
994 .unwrap();
995 assert_eq!(decoded, bytes);
996 assert_eq!(body["total_bytes"], bytes.len());
997 }
998
999 #[test]
1000 fn adaptive_evidence_distinguishes_corrupt_expired_and_generation_mismatch() {
1001 let _spill = test_lock();
1002 let _artifact = crate::artifacts::TEST_ARTIFACT_SESSIONS_GUARD
1003 .lock()
1004 .unwrap_or_else(|err| err.into_inner());
1005 let tmp = tempdir().unwrap();
1006 let _root = set_spillover_root(tmp.path().join("tool_outputs"));
1007 let prior =
1008 crate::artifacts::set_test_artifact_sessions_root(Some(tmp.path().join("sessions")));
1009 struct Restore(Option<PathBuf>);
1010 impl Drop for Restore {
1011 fn drop(&mut self) {
1012 crate::artifacts::set_test_artifact_sessions_root(self.0.take());
1013 }
1014 }
1015 let _restore = Restore(prior);
1016
1017 publish_test_evidence("session-a", "art_call-expired", b"expired", true);
1018 let expired = execute_tool_in_session(json!({"ref": "art_call-expired"}), "session-a")
1019 .unwrap_err()
1020 .to_string();
1021 assert!(expired.contains("expired"), "{expired}");
1022
1023 let artifact = publish_test_evidence("session-a", "art_call-corrupt", b"original", false);
1024 let absolute =
1025 crate::artifacts::session_artifact_absolute_path("session-a", &artifact.storage_path)
1026 .unwrap();
1027 std::fs::write(absolute, b"changed").unwrap();
1028 let corrupt = execute_tool_in_session(json!({"ref": "art_call-corrupt"}), "session-a")
1029 .unwrap_err()
1030 .to_string();
1031 assert!(corrupt.contains("corrupt"), "{corrupt}");
1032
1033 publish_test_evidence("session-a", "art_call-generation", b"stable", false);
1034 let mismatch = execute_tool_in_session(
1035 json!({"ref": "art_call-generation", "generation": 2}),
1036 "session-a",
1037 )
1038 .unwrap_err()
1039 .to_string();
1040 assert!(mismatch.contains("generation"), "{mismatch}");
1041 }
1042
1043 #[test]
1044 fn query_returns_matching_line_with_context() {
1045 let _lock = test_lock();
1046 let tmp = tempdir().unwrap();
1047 let _guard = set_spillover_root(tmp.path().join("tool_outputs"));
1048 let session_id = "session-legacy-query";
1049 write_owned_legacy(
1050 "call-query",
1051 "one\ntwo before\nneedle here\nafter\nlast",
1052 session_id,
1053 );
1054
1055 let result = execute_tool_in_session(
1056 json!({
1057 "ref": "tool_result:call-query",
1058 "mode": "query",
1059 "query": "needle",
1060 "context_lines": 1
1061 }),
1062 session_id,
1063 )
1064 .unwrap();
1065
1066 let body: Value = serde_json::from_str(&result.content).unwrap();
1067 assert_eq!(body["matched_lines"], 1);
1068 let rendered = body["results"].to_string();
1069 assert!(rendered.contains("2: two before"));
1070 assert!(rendered.contains("3: needle here"));
1071 assert!(rendered.contains("4: after"));
1072 }
1073
1074 #[test]
1075 fn lines_mode_accepts_filename_inside_spillover_root() {
1076 let _lock = test_lock();
1077 let tmp = tempdir().unwrap();
1078 let root = tmp.path().join("tool_outputs");
1079 let _guard = set_spillover_root(root.clone());
1080 let session_id = "session-legacy-lines";
1081 write_owned_legacy("call-lines", "a\nb\nc\nd", session_id);
1082
1083 let result = execute_tool_in_session(
1084 json!({
1085 "ref": "call-lines.txt",
1086 "mode": "lines",
1087 "lines": "2-3"
1088 }),
1089 session_id,
1090 )
1091 .unwrap();
1092
1093 let body: Value = serde_json::from_str(&result.content).unwrap();
1094 let excerpt = body["excerpt"].as_str().unwrap();
1095 assert!(excerpt.contains("2: b"));
1096 assert!(excerpt.contains("3: c"));
1097 assert!(!excerpt.contains("1: a"));
1098 assert!(!excerpt.contains("4: d"));
1099 }
1100
1101 #[test]
1102 fn rejects_path_outside_spillover_root() {
1103 let _lock = test_lock();
1104 let tmp = tempdir().unwrap();
1105 let root = tmp.path().join("tool_outputs");
1106 fs::create_dir_all(&root).unwrap();
1107 let outside = tmp.path().join("outside.txt");
1108 fs::write(&outside, "secret").unwrap();
1109 let _guard = set_spillover_root(root);
1110
1111 let err = execute_tool(json!({"ref": outside.display().to_string()})).unwrap_err();
1112
1113 // Unauthorized is distinct but non-leaking: no outside path detail is
1114 // echoed beyond the caller-supplied ref.
1115 let msg = err.to_string();
1116 assert!(
1117 msg.contains("authorize") && msg.contains("active session"),
1118 "expected non-leaking authorization diagnostic, got: {msg}"
1119 );
1120 }
1121
1122 #[test]
1123 fn resolves_sha_reference_from_wire_dedup() {
1124 // A SHA-keyed lookup — emulates what happens when the model
1125 // sees a `<TOOL_RESULT_REF sha="..." />` block and passes the
1126 // SHA to retrieve_tool_result.
1127 let _lock = test_lock();
1128 let tmp = tempdir().unwrap();
1129 let _guard = set_spillover_root(tmp.path().join("tool_outputs"));
1130 let body = "checking crate ... error[E0425]: cannot find value\n".repeat(80);
1131 let session_id = "session-legacy-sha";
1132 let (sha, _) = write_owned_sha(&body, session_id);
1133
1134 // Form: `sha:<hex>`
1135 let result =
1136 execute_tool_in_session(json!({"ref": format!("sha:{sha}")}), session_id).unwrap();
1137 assert!(result.success, "sha:<hex> form should resolve");
1138
1139 // Form: bare 64-hex
1140 let result = execute_tool_in_session(json!({"ref": &sha}), session_id).unwrap();
1141 assert!(result.success, "bare 64-hex form should resolve");
1142 }
1143
1144 #[test]
1145 fn resolves_art_prefix_to_legacy_spillover_id() {
1146 // The model commonly sees `id: art_call_xyz` in artifact
1147 // ref blocks. retrieve_tool_result should strip the `art_`
1148 // prefix and find the legacy `<id>.txt` file if no
1149 // session-artifact equivalent exists.
1150 let _lock = test_lock();
1151 let tmp = tempdir().unwrap();
1152 let _guard = set_spillover_root(tmp.path().join("tool_outputs"));
1153 let session_id = "session-legacy-art-prefix";
1154 write_owned_legacy("call_xyz", "line1\nline2\nline3", session_id);
1155
1156 let result = execute_tool_in_session(json!({"ref": "art_call_xyz"}), session_id).unwrap();
1157 assert!(result.success, "art_ prefix should resolve to legacy id");
1158 }
1159
1160 #[test]
1161 fn unowned_and_foreign_legacy_spillovers_fail_closed_without_leaking_content() {
1162 let _lock = test_lock();
1163 let tmp = tempdir().unwrap();
1164 let _guard = set_spillover_root(tmp.path().join("tool_outputs"));
1165 let sentinel = "SESSION_A_PRIVATE_SENTINEL";
1166
1167 crate::tools::truncate::write_spillover("call-unowned", sentinel).unwrap();
1168 let unowned =
1169 execute_tool_in_session(json!({"ref": "call-unowned", "mode": "bytes"}), "session-b")
1170 .unwrap_err()
1171 .to_string();
1172 assert!(unowned.contains("no verifiable session owner"), "{unowned}");
1173 assert!(!unowned.contains(sentinel), "{unowned}");
1174 assert!(
1175 !unowned.contains(tmp.path().to_string_lossy().as_ref()),
1176 "{unowned}"
1177 );
1178
1179 write_owned_legacy("call-foreign", sentinel, "session-a");
1180 let foreign =
1181 execute_tool_in_session(json!({"ref": "call-foreign", "mode": "bytes"}), "session-b")
1182 .unwrap_err()
1183 .to_string();
1184 assert!(foreign.contains("another session"), "{foreign}");
1185 assert!(!foreign.contains(sentinel), "{foreign}");
1186 assert!(
1187 !foreign.contains(tmp.path().to_string_lossy().as_ref()),
1188 "{foreign}"
1189 );
1190 }
1191
1192 #[test]
1193 fn owned_legacy_digest_mismatch_is_distinct_from_unauthorized() {
1194 let _lock = test_lock();
1195 let tmp = tempdir().unwrap();
1196 let _guard = set_spillover_root(tmp.path().join("tool_outputs"));
1197 let path = write_owned_legacy("call-corrupt-owned", "original", "session-a");
1198 std::fs::write(path, "changed").unwrap();
1199
1200 let error = execute_tool_in_session(json!({"ref": "call-corrupt-owned"}), "session-a")
1201 .unwrap_err()
1202 .to_string();
1203 assert!(error.contains("content is corrupt"), "{error}");
1204 assert!(!error.contains("another session"), "{error}");
1205 }
1206
1207 #[test]
1208 fn not_found_error_lists_tried_candidates_and_accepted_forms() {
1209 let _lock = test_lock();
1210 let tmp = tempdir().unwrap();
1211 let _guard = set_spillover_root(tmp.path().join("tool_outputs"));
1212 fs::create_dir_all(tmp.path().join("tool_outputs")).unwrap();
1213
1214 let err = execute_tool(json!({"ref": "definitely_missing_id"})).unwrap_err();
1215 let msg = err.to_string();
1216 assert!(msg.contains("not found"), "got: {msg}");
1217 assert!(msg.contains("active session"), "got: {msg}");
1218 assert!(msg.contains("art_<id>"), "got: {msg}");
1219 assert!(!msg.contains("tool_outputs"), "storage root leaked: {msg}");
1220 assert!(
1221 !msg.contains(tmp.path().to_string_lossy().as_ref()),
1222 "path leaked: {msg}"
1223 );
1224 }
1225
1226 #[test]
1227 fn resolves_art_prefix_via_session_artifacts() {
1228 let _lock = test_lock();
1229 let tmp = tempdir().unwrap();
1230 let _spill_guard = set_spillover_root(tmp.path().join("tool_outputs"));
1231 let _art_guard = {
1232 let prior = crate::artifacts::set_test_artifact_sessions_root(Some(
1233 tmp.path().join("sessions"),
1234 ));
1235 scopeguard_for_test(prior)
1236 };
1237 let session_id = "session-abc";
1238 let body = "this is the canonical session artifact body, not a legacy file";
1239 crate::artifacts::write_session_artifact(session_id, "art_call_real", body).unwrap();
1240
1241 let runtime = tokio::runtime::Builder::new_current_thread()
1242 .enable_all()
1243 .build()
1244 .unwrap();
1245 let workspace_tmp = tempdir().unwrap();
1246 let ctx = ToolContext::new(workspace_tmp.path()).with_state_namespace(session_id);
1247 let result = runtime
1248 .block_on(RetrieveToolResultTool.execute(json!({"ref": "art_call_real"}), &ctx))
1249 .expect("art_<id> should resolve via session artifacts");
1250 assert!(result.success);
1251 let payload: Value = serde_json::from_str(&result.content).unwrap();
1252 assert!(
1253 payload
1254 .to_string()
1255 .contains("canonical session artifact body"),
1256 "summary should pull from session artifact, got: {payload}"
1257 );
1258 }
1259
1260 #[cfg(unix)]
1261 #[test]
1262 fn rejects_symlink_inside_session_artifacts() {
1263 let _lock = test_lock();
1264 let tmp = tempdir().unwrap();
1265 let _spill_guard = set_spillover_root(tmp.path().join("tool_outputs"));
1266 let _art_guard = {
1267 let prior = crate::artifacts::set_test_artifact_sessions_root(Some(
1268 tmp.path().join("sessions"),
1269 ));
1270 scopeguard_for_test(prior)
1271 };
1272 let session_id = "session-xyz";
1273 // Plant a sensitive file outside the artifact dir.
1274 let secret = tmp.path().join("secret.txt");
1275 fs::write(&secret, "do not leak").unwrap();
1276 // Create the artifact dir, then drop a symlink inside it
1277 // pointing at the secret.
1278 let art_dir = tmp
1279 .path()
1280 .join("sessions")
1281 .join(session_id)
1282 .join("artifacts");
1283 fs::create_dir_all(&art_dir).unwrap();
1284 std::os::unix::fs::symlink(&secret, art_dir.join("art_evil.txt")).unwrap();
1285
1286 let runtime = tokio::runtime::Builder::new_current_thread()
1287 .enable_all()
1288 .build()
1289 .unwrap();
1290 let workspace_tmp = tempdir().unwrap();
1291 let ctx = ToolContext::new(workspace_tmp.path()).with_state_namespace(session_id);
1292 let result =
1293 runtime.block_on(RetrieveToolResultTool.execute(json!({"ref": "art_evil"}), &ctx));
1294 let err = result.expect_err("symlink artifact must not resolve");
1295 assert!(
1296 err.to_string().contains("not found"),
1297 "expected `not found`, got: {err}"
1298 );
1299 }
1300
1301 struct ArtifactRootGuard {
1302 prior: Option<PathBuf>,
1303 }
1304 impl Drop for ArtifactRootGuard {
1305 fn drop(&mut self) {
1306 crate::artifacts::set_test_artifact_sessions_root(self.prior.take());
1307 }
1308 }
1309 fn scopeguard_for_test(prior: Option<PathBuf>) -> ArtifactRootGuard {
1310 ArtifactRootGuard { prior }
1311 }
1312 }
1313
1313 lines RUST