Skip to main content

strands_harness/
prompt.rs

1//! The harness system prompt, vended as a first-class artifact.
2//!
3//! [`HARNESS_CONTRACT`] is a model-neutral behavioral contract: act once there is enough to act
4//! on, explore before changing things, confirm before anything irreversible, verify before
5//! declaring a task done. It asserts no identity or domain; those belong in the consumer's
6//! `instructions`.
7
8/// The model-neutral behavioral contract every harness system prompt starts with.
9pub const HARNESS_CONTRACT: &str =
10    "You are an agent. Keep working until the task is fully resolved before ending your turn. \
11Only stop to ask the user when you are blocked on a decision or information that is \
12genuinely theirs to provide.
13
14# Acting
15 - Once you have enough to act on, act. Do not ask for confirmation of steps you can verify \
16yourself, and do not re-derive facts you have already established.
17 - Explore before you change anything: understand the surrounding context and conventions, \
18then make changes that fit them.
19 - Treat a task as done only when you have verified it, not when it looks plausible. If you \
20cannot verify, say so.
21
22# Tools
23 - Prefer the dedicated tool for a job over improvising with a general one.
24 - Independent tool calls can be issued together in a single turn; run them in parallel rather than serially.
25 - A denied or failed tool call is information: adjust your approach, do not retry it verbatim.
26 - `<system-reminder>` tags in messages and tool results are injected by the harness, not the user.
27
28# Safety
29 - For actions that are hard to reverse or that reach outside the local environment, confirm \
30first unless you have been explicitly told to proceed. Approval in one context does not carry \
31to the next.
32 - Report outcomes faithfully: if something fails, say so with the output; if you skipped a \
33step, say that; when something is done and verified, state it plainly without hedging.
34
35# Context management
36When the conversation grows long, older turns may be summarized and large tool results moved out of \
37the immediate context, with a way to retrieve them. Work as if the full history remains available; you \
38do not need to wrap up early or hand off mid-task.";
39
40/// Assembles the harness contract, an optional domain block and per-request context, separated by blank lines.
41///
42/// `instructions` is the consumer's domain block (identity, scope, task policy); an empty one is
43/// skipped. `context_parts` are appended last, for example a timestamp or request-scoped hints.
44///
45/// ```
46/// use strands_harness::{HARNESS_CONTRACT, build_system_prompt};
47///
48/// let prompt = build_system_prompt(Some("You are a SQL assistant."), &[]);
49/// assert!(prompt.starts_with(HARNESS_CONTRACT));
50/// assert!(prompt.ends_with("\n\nYou are a SQL assistant."));
51/// assert_eq!(build_system_prompt(None, &[]), HARNESS_CONTRACT);
52/// ```
53#[must_use]
54pub fn build_system_prompt(instructions: Option<&str>, context_parts: &[&str]) -> String {
55    let mut parts = vec![HARNESS_CONTRACT];
56    if let Some(instructions) = instructions.filter(|text| !text.is_empty()) {
57        parts.push(instructions);
58    }
59    parts.extend_from_slice(context_parts);
60    parts.join("\n\n")
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn returns_the_contract_only_by_default() {
69        assert_eq!(build_system_prompt(None, &[]), HARNESS_CONTRACT);
70    }
71
72    #[test]
73    fn appends_instructions_after_the_contract() {
74        let prompt = build_system_prompt(Some("You are a SQL assistant."), &[]);
75        assert!(prompt.starts_with(HARNESS_CONTRACT));
76        assert!(prompt.ends_with("You are a SQL assistant."));
77    }
78
79    /// Byte for byte against the shared cross-SDK fixture `strands-rs/conformance/fixtures/harness_prompt.json`.
80    #[test]
81    fn matches_the_ts_contract_byte_for_byte() {
82        let path = concat!(
83            env!("CARGO_MANIFEST_DIR"),
84            "/../../strands-rs/conformance/fixtures/harness_prompt.json"
85        );
86        let fixture: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
87        let case = &fixture["cases"][0];
88        assert_eq!(case["name"], "contract");
89        assert_eq!(
90            case["json"].as_str().unwrap(),
91            serde_json::to_string(HARNESS_CONTRACT).unwrap()
92        );
93    }
94
95    #[test]
96    fn appends_context_parts_last_in_order() {
97        let prompt = build_system_prompt(Some("domain block"), &["Current time: noon", "Repo: acme"]);
98        let domain = prompt.find("domain block").unwrap();
99        let time = prompt.find("Current time: noon").unwrap();
100        let repo = prompt.find("Repo: acme").unwrap();
101        assert!(domain < time);
102        assert!(time < repo);
103    }
104}