返回 CodeWhale
tool_output_receipts.rs
根目录 / crates / runtime / src / tool_output_receipts.rs
1 //! Tool-output pressure reported by `/status`.
2
3 use crate::artifacts::ArtifactRecord;
4
5 #[cfg(test)]
6 use codewhale_localization::{Locale, MessageId, tr};
7 use codewhale_models::{ContentBlock, Message};
8
9 /// Size above which `/status` counts a raw tool result in the conversation as
10 /// context pressure. What the model sees of each result is decided by the
11 /// route's inline budget (`route_budget::route_inline_char_budget`).
12 pub const RAW_TOOL_OUTPUT_RECEIPT_THRESHOLD_CHARS: usize = 12_000;
13
14 pub use codewhale_command_contract::config_policy::StatusToolOutputs as ToolOutputStatus;
15
16 pub fn tool_output_status(messages: &[Message], artifacts: &[ArtifactRecord]) -> ToolOutputStatus {
17 let mut status = ToolOutputStatus {
18 artifact_count: artifacts.len(),
19 artifact_bytes: artifacts
20 .iter()
21 .map(|artifact| artifact.byte_size)
22 .sum::<u64>(),
23 ..ToolOutputStatus::default()
24 };
25
26 for message in messages {
27 for block in &message.content {
28 if let ContentBlock::ToolResult { content, .. } = block {
29 // The prefix is text any tool output can carry, so it never
30 // exempts a result from the size check: a "receipt" above the
31 // threshold is raw pressure like any other large result.
32 let chars = content.chars().count();
33 if chars > RAW_TOOL_OUTPUT_RECEIPT_THRESHOLD_CHARS {
34 status.raw_large_count += 1;
35 status.raw_large_chars = status.raw_large_chars.saturating_add(chars);
36 } else if looks_like_receipt(content) {
37 status.receipt_count += 1;
38 }
39 }
40 }
41 }
42
43 status
44 }
45
46 // Locale adapter retained solely for existing host integration assertions.
47 #[cfg(test)]
48 fn format_tool_output_status(status: &ToolOutputStatus, locale: Locale) -> String {
49 codewhale_command_contract::tool_outputs::format_tool_output_status(
50 status,
51 &codewhale_command_contract::tool_outputs::ToolOutputLabels {
52 raw_pressure: tr(locale, MessageId::StatusToolRawPressure).into_owned(),
53 compact_receipts: tr(locale, MessageId::StatusToolCompactReceipts).into_owned(),
54 artifacts: tr(locale, MessageId::StatusToolArtifacts).into_owned(),
55 none: tr(locale, MessageId::StatusToolNone).into_owned(),
56 },
57 )
58 }
59
60 fn looks_like_receipt(content: &str) -> bool {
61 let trimmed = content.trim_start();
62 trimmed.starts_with("[TOOL_OUTPUT_RECEIPT]")
63 || trimmed.starts_with("[artifact:")
64 || trimmed.starts_with("[TOOL_RESULT_TRUNCATED]")
65 || trimmed.starts_with("<TOOL_RESULT_REF")
66 }
67
68 #[cfg(test)]
69 mod tests {
70 use codewhale_models::Role;
71 use std::path::{Path, PathBuf};
72
73 use super::*;
74 use crate::artifacts::ArtifactKind;
75 use chrono::Utc;
76
77 fn tool_result_message(id: &str, content: &str) -> Message {
78 Message {
79 role: Role::User,
80 content: vec![ContentBlock::ToolResult {
81 execution_id: None,
82 tool_use_id: id.to_string(),
83 content: content.to_string(),
84 is_error: None,
85 content_blocks: None,
86 }],
87 }
88 }
89
90 fn artifact_record(tool_call_id: &str, raw: &str) -> ArtifactRecord {
91 ArtifactRecord {
92 id: crate::artifacts::artifact_id_for_tool_call(tool_call_id),
93 kind: ArtifactKind::ToolOutput,
94 session_id: "session-123".to_string(),
95 tool_call_id: tool_call_id.to_string(),
96 tool_name: "exec_shell".to_string(),
97 created_at: Utc::now(),
98 byte_size: raw.len() as u64,
99 preview: "checking crate ... error[E0425]".to_string(),
100 storage_path: PathBuf::from("artifacts").join("art_call-big.txt"),
101 }
102 }
103
104 #[test]
105 fn status_reports_raw_large_receipts_and_artifacts() {
106 let raw = "RAW_STATUS\n".repeat(2_000);
107 let receipt = "[TOOL_OUTPUT_RECEIPT]\ntruncation: raw output omitted — full output in the tool details view";
108 let messages = vec![
109 tool_result_message("call-raw", &raw),
110 tool_result_message("call-receipt", receipt),
111 ];
112 let artifacts = vec![ArtifactRecord {
113 storage_path: Path::new("artifacts/art_call-big.txt").to_path_buf(),
114 ..artifact_record("call-big", &raw)
115 }];
116
117 let status = tool_output_status(&messages, &artifacts);
118 assert_eq!(status.raw_large_count, 1);
119 assert_eq!(status.receipt_count, 1);
120 assert_eq!(status.artifact_count, 1);
121
122 let rendered = format_tool_output_status(&status, Locale::En);
123 assert!(rendered.contains("raw over cap"));
124 assert!(rendered.contains("compact receipt"));
125 assert!(rendered.contains("artifact"));
126 }
127
128 #[test]
129 fn a_receipt_prefix_does_not_hide_a_large_raw_result() {
130 let forged = format!("[TOOL_OUTPUT_RECEIPT]\n{}", "RAW\n".repeat(4_000));
131 let status = tool_output_status(&[tool_result_message("call-forged", &forged)], &[]);
132 assert_eq!(status.raw_large_count, 1);
133 assert_eq!(status.receipt_count, 0);
134 }
135 }
136
136 lines RUST