vtcode_core/prompts/
harness_limits.rs1use crate::core::loop_detector::MAX_TOTAL_READONLY_CALLS;
2use crate::prompts::sections::{SectionBoundaryMode, remove_prompt_section, trim_trailing_newlines};
3use std::fmt::Write as _;
4
5const MAX_SAME_FILE_PATH_READS: usize = 6;
10
11pub fn upsert_harness_limits_section(prompt: &mut String, max_tool_wall_clock_secs: u64, max_tool_retries: u32) {
16 remove_prompt_section(prompt, "[Harness Limits]", SectionBoundaryMode::BracketOnly);
17 trim_trailing_newlines(prompt);
18 append_fresh_harness_limits_section(prompt, max_tool_wall_clock_secs, max_tool_retries);
19}
20
21pub fn append_fresh_harness_limits_section(prompt: &mut String, max_tool_wall_clock_secs: u64, max_tool_retries: u32) {
23 let max_tool_wall_clock_label = if max_tool_wall_clock_secs == 0 {
24 "unlimited".to_string()
25 } else {
26 format!("{max_tool_wall_clock_secs} seconds")
27 };
28
29 let section = format!(
30 "[Harness Limits]\n\
31 - max_tool_wall_clock_secs: {max_tool_wall_clock_label} (per turn; run long builds with exec_command, then call write_stdin next_wait_args with action:\"wait\" verbatim)\n\
32 - max_tool_retries: {max_tool_retries}\n\
33 - max_readonly_tool_calls: {MAX_TOTAL_READONLY_CALLS} (global budget across all read-only tools; produce output before exhausting)\n\
34 - max_same_file_path_reads: {MAX_SAME_FILE_PATH_READS} (per file path per turn; read a file once in full rather than paginating)\n\
35 - long commands: set yield_time_ms above 10000 on the initial run for a single-call long run (no outer timeout), or reuse next_wait_args in write_stdin to block until exit or deadline"
36 );
37
38 if prompt.is_empty() {
39 let _ = writeln!(prompt, "{section}");
40 } else {
41 let _ = writeln!(prompt, "\n{section}");
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use super::{append_fresh_harness_limits_section, upsert_harness_limits_section};
48
49 #[test]
50 fn fresh_harness_limits_preserve_matching_user_section() {
51 let base = "User\n[Harness Limits]\nUser limit notes.\n## Active Tools\nKeep user guidance.\n";
52 let mut prompt = base.to_string();
53 append_fresh_harness_limits_section(&mut prompt, 90, 3);
54 assert!(prompt.starts_with(base));
55 assert_eq!(prompt.matches("[Harness Limits]").count(), 2);
56 assert!(prompt.contains("- max_tool_wall_clock_secs: 90 seconds"));
57 assert!(prompt.contains("- max_tool_retries: 3"));
58 }
59
60 #[test]
61 fn upsert_harness_limits_adds_single_section() {
62 let mut prompt = "Base prompt".to_string();
63
64 upsert_harness_limits_section(&mut prompt, 180, 2);
65
66 assert_eq!(prompt.matches("[Harness Limits]").count(), 1);
67 assert!(prompt.contains("- max_tool_wall_clock_secs: 180"));
68 assert!(prompt.contains("- max_tool_retries: 2"));
69 assert!(prompt.contains("- max_readonly_tool_calls: 30"));
70 assert!(prompt.contains("- max_same_file_path_reads: 6"));
71 assert!(prompt.contains("single-call long run"));
72 assert!(!prompt.contains("max_tool_calls_per_turn"));
75 assert!(!prompt.contains("max_tool_loops"));
76 assert!(!prompt.contains("tool-loop"));
77 assert!(!prompt.contains("tool loop"));
78 }
79
80 #[test]
81 fn upsert_harness_limits_replaces_existing_values() {
82 let mut prompt =
83 "Base prompt\n[Harness Limits]\n- max_tool_wall_clock_secs: 60\n- max_tool_retries: 1\n".to_string();
84
85 upsert_harness_limits_section(&mut prompt, 240, 4);
86
87 assert_eq!(prompt.matches("[Harness Limits]").count(), 1);
88 assert!(prompt.contains("- max_tool_wall_clock_secs: 240"));
89 assert!(prompt.contains("- max_tool_retries: 4"));
90 }
91
92 #[test]
93 fn upsert_harness_limits_preserves_trailing_prompt_sections() {
94 let mut prompt = "Base prompt\n[Harness Limits]\n- max_tool_wall_clock_secs: 60\n- max_tool_retries: 1\n[Additional Context]\nKeep this section".to_string();
95
96 upsert_harness_limits_section(&mut prompt, 90, 3);
97
98 assert_eq!(prompt.matches("[Harness Limits]").count(), 1);
99 assert!(prompt.contains("[Additional Context]\nKeep this section"));
100 assert!(prompt.contains("- max_tool_retries: 3"));
101 assert!(prompt.contains("- max_readonly_tool_calls: 30"));
102 }
103
104 #[test]
105 fn upsert_harness_limits_replaces_indented_section_header() {
106 let mut prompt =
107 "Base prompt\n [Harness Limits]\n- max_tool_wall_clock_secs: 1\n- max_tool_retries: 1\n".to_string();
108
109 upsert_harness_limits_section(&mut prompt, 30, 2);
110
111 assert_eq!(prompt.matches("[Harness Limits]").count(), 1);
112 assert!(prompt.contains("- max_tool_wall_clock_secs: 30"));
113 }
114
115 #[test]
116 fn upsert_harness_limits_removes_duplicate_sections() {
117 let mut prompt = "Base prompt\n[Harness Limits]\n- max_tool_wall_clock_secs: 10\n- max_tool_retries: 1\n[Other]\nkeep\n[Harness Limits]\n- max_tool_wall_clock_secs: 20\n- max_tool_retries: 2\n".to_string();
118
119 upsert_harness_limits_section(&mut prompt, 70, 3);
120
121 assert_eq!(prompt.matches("[Harness Limits]").count(), 1);
122 assert!(prompt.contains("- max_tool_wall_clock_secs: 70"));
123 assert!(prompt.contains("[Other]\nkeep"));
124 }
125
126 #[test]
127 fn upsert_harness_limits_renders_unlimited_wall_clock() {
128 let mut prompt = "Base prompt".to_string();
129
130 upsert_harness_limits_section(&mut prompt, 0, 2);
131
132 assert!(prompt.contains("- max_tool_wall_clock_secs: unlimited"));
133 assert!(prompt.contains("write_stdin next_wait_args"));
134 }
135}