Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/patched-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ jobs:
"acp::server::tests::test_prompt_stop_reason_reports_max_turn_requests" \
"context_mgmt::tests::self_compact_kind_selects_continuation_and_drops_preserved_prompt" \
"context_mgmt::tests::manual_compact_kind_selects_continuation_and_drops_preserved_prompt" \
"context_mgmt::tests::self_compact_summarizes_only_agent_visible_non_turn_context_messages" \
"agents::state_machine::ops_self_compact::tests::test_self_compact_tool_schema_and_annotations" \
"agents::state_machine::ops_self_compact::tests::test_validate_self_compact_args_valid" \
"agents::state_machine::ops_self_compact::tests::test_validate_self_compact_args_missing" \
Expand Down
76 changes: 74 additions & 2 deletions crates/goose/src/context_mgmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ pub async fn compact_messages_with_kind(
};

let messages_to_compact = messages.as_slice();
let summarizable = Conversation::new_unvalidated(
messages_to_compact
.iter()
.filter(|msg| !msg.is_turn_context())
.cloned(),
)
.agent_visible_messages();

let model = RefCompactionModel {
provider,
Expand All @@ -230,7 +237,7 @@ pub async fn compact_messages_with_kind(
&model,
Some(&GooseTokenEstimator),
&templates,
messages_to_compact,
&summarizable,
)
.await?;

Expand Down Expand Up @@ -665,6 +672,7 @@ mod tests {
struct MockProvider {
message: Message,
config: ModelConfig,
captured_systems: std::sync::Mutex<Vec<String>>,
}

impl MockProvider {
Expand All @@ -683,8 +691,13 @@ mod tests {
supports_vision: None,
request_headers: None,
},
captured_systems: std::sync::Mutex::new(Vec::new()),
}
}

fn captured_systems(&self) -> Vec<String> {
self.captured_systems.lock().unwrap().clone()
}
}

#[async_trait]
Expand All @@ -696,10 +709,14 @@ mod tests {
async fn stream(
&self,
_model_config: &ModelConfig,
_system: &str,
system: &str,
_messages: &[Message],
_tools: &[Tool],
) -> Result<MessageStream, ProviderError> {
self.captured_systems
.lock()
.unwrap()
.push(system.to_string());
let message = self.message.clone();
let usage = ProviderUsage::new("mock-model".to_string(), Usage::default());
Ok(stream_from_single_message(message, usage))
Expand Down Expand Up @@ -962,4 +979,59 @@ mod tests {
"Manual compaction must not preserve user prompt"
);
}

#[tokio::test]
async fn self_compact_summarizes_only_agent_visible_non_turn_context_messages() {
let provider = MockProvider::new(Message::assistant().with_text("new summary"), 100_000);
let archived_user = Message::user()
.with_text("ARCHIVED_HISTORY_SENTINEL")
.with_metadata(MessageMetadata::user_only());
let prior_summary = Message::user()
.with_text("PRIOR_SUMMARY_SENTINEL")
.with_metadata(MessageMetadata::agent_only().with_compaction_summary());
let turn_context = Message::user()
.with_text("TURN_CONTEXT_SENTINEL")
.with_metadata(MessageMetadata::agent_only().with_turn_context());
let active_user = Message::user().with_text("ACTIVE_USER_SENTINEL");
let active_assistant = Message::assistant().with_text("ACTIVE_ASSISTANT_SENTINEL");

let conversation = Conversation::new_unvalidated(vec![
archived_user,
prior_summary,
turn_context,
active_user,
active_assistant,
]);
let model_config = provider.config.clone();

let compaction = compact_messages_with_kind(
&provider,
&model_config,
"test-session-id",
&conversation,
CompactionKind::SelfRequested,
)
.await
.unwrap();

let systems = provider.captured_systems();
assert_eq!(systems.len(), 1);
let prompt = &systems[0];
assert!(
!prompt.contains("ARCHIVED_HISTORY_SENTINEL"),
"agent-invisible archived history must not be sent to the summarizer"
);
assert!(
!prompt.contains("TURN_CONTEXT_SENTINEL"),
"turn-context messages must not be sent to the summarizer"
);
assert!(prompt.contains("PRIOR_SUMMARY_SENTINEL"));
assert!(prompt.contains("ACTIVE_USER_SENTINEL"));
assert!(prompt.contains("ACTIVE_ASSISTANT_SENTINEL"));

// Archived history remains in the stored conversation, still agent-invisible.
assert!(compaction.conversation.messages().iter().any(|m| {
!m.is_agent_visible() && m.as_concat_text().contains("ARCHIVED_HISTORY_SENTINEL")
}));
}
}
Loading