Skip to main content

vtcode_core/prompts/
harness_limits.rs

1use 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
5/// Per-file-path read cap advertised in the system prompt. Must match
6/// `guards::MAX_SAME_FILE_PATH_READ_CALLS` (pinned by
7/// `read_guard.rs::advertised_per_path_read_cap_matches_enforcement`).
8/// Reading the same file more than this many times in a turn triggers recovery.
9const MAX_SAME_FILE_PATH_READS: usize = 6;
10
11/// Keep prompt guidance aligned with the selected user-visible harness limits.
12///
13/// This operation is idempotent: existing `[Harness Limits]` sections are removed before
14/// inserting the current values.
15pub fn upsert_harness_limits_section(prompt: &mut String, max_tool_wall_clock_secs: u64, max_tool_retries: u32) {
16    let max_tool_wall_clock_label = if max_tool_wall_clock_secs == 0 {
17        "unlimited".to_string()
18    } else {
19        format!("{max_tool_wall_clock_secs} seconds")
20    };
21
22    remove_prompt_section(prompt, "[Harness Limits]", SectionBoundaryMode::BracketOnly);
23    trim_trailing_newlines(prompt);
24
25    let section = format!(
26        "[Harness Limits]\n\
27         - 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\
28         - max_tool_retries: {max_tool_retries}\n\
29         - max_readonly_tool_calls: {MAX_TOTAL_READONLY_CALLS} (global budget across all read-only tools; produce output before exhausting)\n\
30         - 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\
31         - 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"
32    );
33
34    if prompt.is_empty() {
35        let _ = writeln!(prompt, "{section}");
36    } else {
37        let _ = writeln!(prompt, "\n{section}");
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::upsert_harness_limits_section;
44
45    #[test]
46    fn upsert_harness_limits_adds_single_section() {
47        let mut prompt = "Base prompt".to_string();
48
49        upsert_harness_limits_section(&mut prompt, 180, 2);
50
51        assert_eq!(prompt.matches("[Harness Limits]").count(), 1);
52        assert!(prompt.contains("- max_tool_wall_clock_secs: 180"));
53        assert!(prompt.contains("- max_tool_retries: 2"));
54        assert!(prompt.contains("- max_readonly_tool_calls: 30"));
55        assert!(prompt.contains("- max_same_file_path_reads: 6"));
56        assert!(prompt.contains("single-call long run"));
57        assert!(!prompt.contains("max_tool_calls_per_turn"));
58    }
59
60    #[test]
61    fn upsert_harness_limits_replaces_existing_values() {
62        let mut prompt =
63            "Base prompt\n[Harness Limits]\n- max_tool_wall_clock_secs: 60\n- max_tool_retries: 1\n".to_string();
64
65        upsert_harness_limits_section(&mut prompt, 240, 4);
66
67        assert_eq!(prompt.matches("[Harness Limits]").count(), 1);
68        assert!(prompt.contains("- max_tool_wall_clock_secs: 240"));
69        assert!(prompt.contains("- max_tool_retries: 4"));
70    }
71
72    #[test]
73    fn upsert_harness_limits_preserves_trailing_prompt_sections() {
74        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();
75
76        upsert_harness_limits_section(&mut prompt, 90, 3);
77
78        assert_eq!(prompt.matches("[Harness Limits]").count(), 1);
79        assert!(prompt.contains("[Additional Context]\nKeep this section"));
80        assert!(prompt.contains("- max_tool_retries: 3"));
81        assert!(prompt.contains("- max_readonly_tool_calls: 30"));
82    }
83
84    #[test]
85    fn upsert_harness_limits_replaces_indented_section_header() {
86        let mut prompt =
87            "Base prompt\n  [Harness Limits]\n- max_tool_wall_clock_secs: 1\n- max_tool_retries: 1\n".to_string();
88
89        upsert_harness_limits_section(&mut prompt, 30, 2);
90
91        assert_eq!(prompt.matches("[Harness Limits]").count(), 1);
92        assert!(prompt.contains("- max_tool_wall_clock_secs: 30"));
93    }
94
95    #[test]
96    fn upsert_harness_limits_removes_duplicate_sections() {
97        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();
98
99        upsert_harness_limits_section(&mut prompt, 70, 3);
100
101        assert_eq!(prompt.matches("[Harness Limits]").count(), 1);
102        assert!(prompt.contains("- max_tool_wall_clock_secs: 70"));
103        assert!(prompt.contains("[Other]\nkeep"));
104    }
105
106    #[test]
107    fn upsert_harness_limits_renders_unlimited_wall_clock() {
108        let mut prompt = "Base prompt".to_string();
109
110        upsert_harness_limits_section(&mut prompt, 0, 2);
111
112        assert!(prompt.contains("- max_tool_wall_clock_secs: unlimited"));
113        assert!(prompt.contains("write_stdin next_wait_args"));
114    }
115}