Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(claude-agent-sdk): send every system message, not just the first

sanil-231d3ad20a53d1
diff --git a/src/openhuman/inference/provider/claude_agent_sdk/subprocess.rs b/src/openhuman/inference/provider/claude_agent_sdk/subprocess.rs
index 9a1d69fcd2..d32e62255b 100644
--- a/src/openhuman/inference/provider/claude_agent_sdk/subprocess.rs
+++ b/src/openhuman/inference/provider/claude_agent_sdk/subprocess.rs
@@ -261,10 +261,7 @@ impl ChatModel<()> for ClaudeAgentSdkProvider {
) -> tinyinference::Result<ModelResponse> {
let messages = coalesce_prompt_tool_results(&request.messages);
let messages = with_prompt_tool_instructions(&messages, &request.tools);
- let system = messages.iter().find_map(|message| match message {
- Message::System(_) => Some(message.text()),
- _ => None,
- });
+ let system = coalesce_system_prompt(&messages);
let last_user = messages
.iter()
.rev()
@@ -291,6 +288,24 @@ impl ChatModel<()> for ClaudeAgentSdkProvider {
}
}
+/// Join every system message into the one system prompt the CLI accepts.
+///
+/// The CLI takes a single `--system-prompt`, and this used to `find_map` the
+/// first system message and drop the rest (codex on #6068). What gets dropped is
+/// not incidental: the artifact contents list and the turn-cap wrap-up are both
+/// appended as system messages *precisely because* a system message is the one
+/// thing compression and the trim will not remove. Taking only the first put
+/// them back to being removable — on this provider alone, and silently.
+fn coalesce_system_prompt(messages: &[Message]) -> Option<String> {
+ let parts: Vec<String> = messages
+ .iter()
+ .filter(|message| matches!(message, Message::System(_)))
+ .map(|message| message.text())
+ .filter(|text| !text.trim().is_empty())
+ .collect();
+ (!parts.is_empty()).then(|| parts.join("\n\n"))
+}
+
#[cfg(test)]
#[path = "subprocess_tests.rs"]
mod tests;
diff --git a/src/openhuman/inference/provider/claude_agent_sdk/subprocess_tests.rs b/src/openhuman/inference/provider/claude_agent_sdk/subprocess_tests.rs
index 87ee1b134c..ec920180aa 100644
--- a/src/openhuman/inference/provider/claude_agent_sdk/subprocess_tests.rs
+++ b/src/openhuman/inference/provider/claude_agent_sdk/subprocess_tests.rs
@@ -212,3 +212,40 @@ printf '%s\n' '{"type":"result","result":"Calling.<tool_call>{\"name\":\"lookup\
Some("claude-agent-sdk")
);
}
+
+/// The CLI takes one `--system-prompt`, so every system message has to be
+/// folded into it. Taking only the first dropped the artifact contents list and
+/// the turn-cap wrap-up, which are appended as system messages exactly so the
+/// context ladder cannot remove them (codex on #6068).
+#[test]
+fn every_system_message_reaches_the_cli_prompt() {
+ let messages = vec![
+ Message::system("you are a helpful agent"),
+ Message::user("fetch the issues"),
+ Message::system("## Stored results from this turn\n\n- `file_read` → `a.json`"),
+ ];
+
+ let system = coalesce_system_prompt(&messages).expect("a system prompt");
+
+ assert!(
+ system.contains("you are a helpful agent"),
+ "the persona must survive: {system}"
+ );
+ assert!(
+ system.contains("Stored results from this turn"),
+ "a later system message must not be dropped: {system}"
+ );
+ assert!(
+ system.find("you are a helpful agent") < system.find("Stored results"),
+ "order must be preserved so the persona still leads: {system}"
+ );
+}
+
+#[test]
+fn a_conversation_with_no_system_message_yields_none() {
+ let messages = vec![Message::user("hello")];
+ assert!(coalesce_system_prompt(&messages).is_none());
+ // Blank system messages are not a prompt either.
+ let blank = vec![Message::system(" ")];
+ assert!(coalesce_system_prompt(&blank).is_none());
+}