fix(agents): preserve session context across reply lifecycle (#12516)
Co-authored-by: Rohan Patnaik <rohan-patnaik@users.noreply.github.com>
This commit is contained in:
@@ -1773,6 +1773,24 @@ impl Agent {
|
||||
user_message: Message,
|
||||
session_config: SessionConfig,
|
||||
cancel_token: Option<CancellationToken>,
|
||||
) -> Result<BoxStream<'_, Result<AgentEvent>>> {
|
||||
let session_id = session_config.id.clone();
|
||||
let events = crate::session_context::with_session_id(
|
||||
Some(session_id.clone()),
|
||||
self.reply_with_state_machine_inner(user_message, session_config, cancel_token),
|
||||
)
|
||||
.await?;
|
||||
Ok(crate::session_context::with_session_id_stream(
|
||||
Some(session_id),
|
||||
events,
|
||||
))
|
||||
}
|
||||
|
||||
async fn reply_with_state_machine_inner(
|
||||
&self,
|
||||
user_message: Message,
|
||||
session_config: SessionConfig,
|
||||
cancel_token: Option<CancellationToken>,
|
||||
) -> Result<BoxStream<'_, Result<AgentEvent>>> {
|
||||
let session_manager = self.config.session_manager.clone();
|
||||
let session_id = session_config.id.clone();
|
||||
@@ -1836,6 +1854,21 @@ impl Agent {
|
||||
self: &Arc<Self>,
|
||||
session_config: SessionConfig,
|
||||
cancel: CancellationToken,
|
||||
) -> Result<Option<BoxStream<'static, Result<AgentEvent>>>> {
|
||||
let session_id = session_config.id.clone();
|
||||
let stream = crate::session_context::with_session_id(
|
||||
Some(session_id.clone()),
|
||||
self.resume_state_machine_turn_inner(session_config, cancel),
|
||||
)
|
||||
.await?;
|
||||
Ok(stream
|
||||
.map(|stream| crate::session_context::with_session_id_stream(Some(session_id), stream)))
|
||||
}
|
||||
|
||||
async fn resume_state_machine_turn_inner(
|
||||
self: &Arc<Self>,
|
||||
session_config: SessionConfig,
|
||||
cancel: CancellationToken,
|
||||
) -> Result<Option<BoxStream<'static, Result<AgentEvent>>>> {
|
||||
if !super::state_machine::enabled() {
|
||||
return Ok(None);
|
||||
@@ -1866,7 +1899,7 @@ impl Agent {
|
||||
}
|
||||
|
||||
let agent = Arc::clone(self);
|
||||
Ok(Some(Box::pin(async_stream::try_stream! {
|
||||
let stream = Box::pin(async_stream::try_stream! {
|
||||
let initial_stream = if resume_from_persisted_response {
|
||||
Some(
|
||||
agent
|
||||
@@ -1888,7 +1921,8 @@ impl Agent {
|
||||
while let Some(event) = stream.next().await {
|
||||
yield event?;
|
||||
}
|
||||
})))
|
||||
});
|
||||
Ok(Some(stream))
|
||||
}
|
||||
|
||||
fn tool_confirmation_request_ids(event: &AgentEvent) -> Vec<String> {
|
||||
@@ -2050,14 +2084,18 @@ impl Agent {
|
||||
cancel_token: Option<CancellationToken>,
|
||||
) -> Result<BoxStream<'_, Result<AgentEvent>>> {
|
||||
let reply_span = tracing::Span::current();
|
||||
let events = self
|
||||
.reply_impl(
|
||||
let session_id = session_config.id.clone();
|
||||
let events = crate::session_context::with_session_id(
|
||||
Some(session_id.clone()),
|
||||
self.reply_impl(
|
||||
user_message,
|
||||
session_config,
|
||||
use_state_machine,
|
||||
cancel_token,
|
||||
)
|
||||
.await?;
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
let events = crate::session_context::with_session_id_stream(Some(session_id), events);
|
||||
|
||||
// This is the single live-event identity boundary. Callers that intentionally stream
|
||||
// multiple events for one logical message must assign their shared ID before this point.
|
||||
@@ -3930,7 +3968,9 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::agents::gen_ai_telemetry::{self, test_support::SpanFieldCapture};
|
||||
use crate::plugins::discovery::{DiscoveredPlugin, PluginScope};
|
||||
use crate::providers::base::{stream_from_single_message, MessageStream, PermissionRouting};
|
||||
use crate::providers::base::{
|
||||
stream_from_single_message, MessageStream, ModelInfo, PermissionRouting,
|
||||
};
|
||||
use crate::recipe::Response;
|
||||
use crate::session::session_manager::SessionType;
|
||||
use goose_providers::conversation::token_usage::{ProviderUsage, Usage};
|
||||
@@ -4013,6 +4053,184 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct SessionContextProvider {
|
||||
calls: std::sync::Mutex<Vec<(&'static str, Option<String>)>>,
|
||||
}
|
||||
|
||||
impl SessionContextProvider {
|
||||
fn record(&self, operation: &'static str) {
|
||||
self.calls
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push((operation, crate::session_context::current_session_id()));
|
||||
}
|
||||
|
||||
fn calls(&self) -> Vec<(&'static str, Option<String>)> {
|
||||
self.calls.lock().unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl crate::providers::base::Provider for SessionContextProvider {
|
||||
fn get_name(&self) -> &str {
|
||||
"session-context"
|
||||
}
|
||||
|
||||
async fn resume(&self, _session_id: &str) -> Result<(), ProviderError> {
|
||||
self.record("resume");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn fetch_model_info(&self, model_name: &str) -> Result<ModelInfo, ProviderError> {
|
||||
self.record("fetch_model_info");
|
||||
Ok(ModelInfo::new(model_name).with_context_limit(32_000))
|
||||
}
|
||||
|
||||
async fn get_context_limit(&self, _model: &str, _override_limit: Option<usize>) -> usize {
|
||||
self.record("get_context_limit");
|
||||
32_000
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
_model_config: &goose_providers::model::ModelConfig,
|
||||
_system_prompt: &str,
|
||||
_messages: &[Message],
|
||||
_tools: &[Tool],
|
||||
) -> Result<MessageStream, ProviderError> {
|
||||
self.record("stream");
|
||||
Ok(stream_from_single_message(
|
||||
Message::assistant().with_text("done"),
|
||||
ProviderUsage::new("mock-model".to_string(), Usage::default()),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
async fn session_context_agent() -> (Agent, Arc<SessionContextProvider>, SessionConfig, TempDir)
|
||||
{
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let session_manager = Arc::new(SessionManager::new(temp_dir.path().to_path_buf()));
|
||||
let agent = Agent::with_config(AgentConfig::new(
|
||||
Arc::clone(&session_manager),
|
||||
Arc::new(PermissionManager::new(temp_dir.path().join("permissions"))),
|
||||
None,
|
||||
GooseMode::default(),
|
||||
true,
|
||||
GoosePlatform::GooseCli,
|
||||
));
|
||||
let session = session_manager
|
||||
.create_session(
|
||||
temp_dir.path().to_path_buf(),
|
||||
"session-context".to_string(),
|
||||
SessionType::Hidden,
|
||||
GooseMode::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let provider = Arc::new(SessionContextProvider::default());
|
||||
agent
|
||||
.update_provider(
|
||||
provider.clone(),
|
||||
goose_providers::model::ModelConfig::new("mock-model"),
|
||||
&session.id,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
session_manager
|
||||
.add_message(&session.id, &Message::user().with_text("initial request"))
|
||||
.await
|
||||
.unwrap();
|
||||
session_manager
|
||||
.add_message(
|
||||
&session.id,
|
||||
&Message::assistant()
|
||||
.with_text("previous response")
|
||||
.with_inference(InferenceMetadata {
|
||||
provider: provider.get_name().to_string(),
|
||||
requested_model: "mock-model".to_string(),
|
||||
resolved_model: None,
|
||||
provider_session_id: Some("saved-provider-session".to_string()),
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let session_config = SessionConfig {
|
||||
id: session.id.clone(),
|
||||
schedule_id: None,
|
||||
max_turns: Some(1),
|
||||
retry_config: None,
|
||||
};
|
||||
|
||||
(agent, provider, session_config, temp_dir)
|
||||
}
|
||||
|
||||
fn assert_session_context_calls(
|
||||
provider: &SessionContextProvider,
|
||||
session_id: &str,
|
||||
operations: &[&'static str],
|
||||
) {
|
||||
let calls = provider.calls();
|
||||
for operation in operations {
|
||||
assert!(
|
||||
calls
|
||||
.iter()
|
||||
.any(|call| call == &(*operation, Some(session_id.to_string()))),
|
||||
"{operation} was not scoped to session {session_id}: {calls:?}"
|
||||
);
|
||||
}
|
||||
assert_eq!(crate::session_context::current_session_id(), None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reply_scopes_resume_model_info_and_stream_to_session() {
|
||||
let (agent, provider, session_config, _temp_dir) = session_context_agent().await;
|
||||
let session_id = session_config.id.clone();
|
||||
let mut events = agent
|
||||
.reply(
|
||||
Message::user().with_text("continue"),
|
||||
session_config,
|
||||
false,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
while let Some(event) = events.next().await {
|
||||
event.unwrap();
|
||||
}
|
||||
|
||||
assert_session_context_calls(
|
||||
provider.as_ref(),
|
||||
&session_id,
|
||||
&["resume", "fetch_model_info", "stream"],
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn live_delegation_scopes_state_machine_setup_and_stream_to_session() {
|
||||
let (agent, provider, mut session_config, _temp_dir) = session_context_agent().await;
|
||||
session_config.max_turns = Some(2);
|
||||
let session_id = session_config.id.clone();
|
||||
let mut events = agent
|
||||
.reply_live_delegation(
|
||||
Message::user().with_text("continue"),
|
||||
session_config,
|
||||
CancellationToken::new(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
while let Some(event) = events.next().await {
|
||||
event.unwrap();
|
||||
}
|
||||
|
||||
assert_session_context_calls(
|
||||
provider.as_ref(),
|
||||
&session_id,
|
||||
&["get_context_limit", "stream"],
|
||||
);
|
||||
}
|
||||
|
||||
async fn tracing_test_agent_and_session() -> (Agent, Session, TempDir) {
|
||||
let data_dir = TempDir::new().unwrap();
|
||||
let data_path = data_dir.path().to_path_buf();
|
||||
|
||||
@@ -33,7 +33,8 @@ async fn agent_with_dummy_api() -> Result<(Agent, Arc<DummyApi>, String, tempfil
|
||||
api.uri(),
|
||||
goose_providers::api_client::AuthMethod::NoAuth,
|
||||
None,
|
||||
)?;
|
||||
)?
|
||||
.with_request_builder(crate::session_context::session_id_request_builder());
|
||||
let provider: Arc<dyn Provider> = Arc::new(
|
||||
goose_providers::openai::OpenAiProviderBuilder::new(api_client)
|
||||
.name("openai")
|
||||
@@ -229,6 +230,12 @@ async fn state_machine_confirmation_through_agent_resumes_tool_call() -> Result<
|
||||
.contains(&confirmation_id.as_str())));
|
||||
assert_eq!(calculator.total(), 1);
|
||||
assert_eq!(api.call_count(), 2);
|
||||
assert!(
|
||||
api.calls()
|
||||
.iter()
|
||||
.all(|call| call.session_id() == Some(session_config.id.as_str())),
|
||||
"initial and resumed provider requests must retain the session context"
|
||||
);
|
||||
|
||||
assert!(agent
|
||||
.submit_tool_confirmation(&session_config.id, &confirmation_id, Permission::AllowOnce)
|
||||
|
||||
@@ -135,6 +135,7 @@ impl ResponseGate {
|
||||
#[derive(Clone)]
|
||||
pub(super) struct ApiCall {
|
||||
body: Value,
|
||||
session_id: Option<String>,
|
||||
}
|
||||
|
||||
impl ApiCall {
|
||||
@@ -142,6 +143,10 @@ impl ApiCall {
|
||||
serialized_chars(&self.body)
|
||||
}
|
||||
|
||||
pub(super) fn session_id(&self) -> Option<&str> {
|
||||
self.session_id.as_deref()
|
||||
}
|
||||
|
||||
pub(super) fn input_contains(&self, needle: &str) -> bool {
|
||||
request_input(&self.body).contains(needle)
|
||||
}
|
||||
@@ -427,10 +432,15 @@ impl<'a> ConfiguredResponse<'a> {
|
||||
impl DummyApiState {
|
||||
fn respond(&self, request: &Request) -> ResponseTemplate {
|
||||
let body: Value = request.body_json().expect("OpenAI request body");
|
||||
self.calls
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push(ApiCall { body: body.clone() });
|
||||
let session_id = request
|
||||
.headers
|
||||
.get(crate::session_context::SESSION_ID_HEADER)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.map(str::to_string);
|
||||
self.calls.lock().unwrap().push(ApiCall {
|
||||
body: body.clone(),
|
||||
session_id,
|
||||
});
|
||||
|
||||
let input_tokens = serialized_chars(&body);
|
||||
let model = body["model"].as_str().expect("OpenAI request model");
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use futures::stream::BoxStream;
|
||||
use futures::StreamExt;
|
||||
use reqwest::header::{HeaderName, HeaderValue};
|
||||
|
||||
pub const SESSION_ID_HEADER: &str = "agent-session-id";
|
||||
@@ -16,6 +18,20 @@ where
|
||||
SESSION_ID.scope(session_id, f).await
|
||||
}
|
||||
|
||||
pub fn with_session_id_stream<'a, T: Send + 'a>(
|
||||
session_id: Option<String>,
|
||||
stream: BoxStream<'a, T>,
|
||||
) -> BoxStream<'a, T> {
|
||||
Box::pin(futures::stream::unfold(
|
||||
(stream, session_id),
|
||||
|(mut stream, session_id)| async move {
|
||||
with_session_id(session_id.clone(), stream.next())
|
||||
.await
|
||||
.map(|item| (item, (stream, session_id)))
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub fn current_session_id() -> Option<String> {
|
||||
SESSION_ID.try_with(|id| id.clone()).ok().flatten()
|
||||
}
|
||||
@@ -140,6 +156,24 @@ mod tests {
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_session_id_scopes_each_stream_poll() {
|
||||
let stream = futures::stream::iter([(), ()]).then(|()| async { current_session_id() });
|
||||
let mut stream =
|
||||
with_session_id_stream(Some("stream-session".to_string()), Box::pin(stream));
|
||||
|
||||
assert_eq!(
|
||||
stream.next().await,
|
||||
Some(Some("stream-session".to_string()))
|
||||
);
|
||||
assert_eq!(
|
||||
stream.next().await,
|
||||
Some(Some("stream-session".to_string()))
|
||||
);
|
||||
assert_eq!(stream.next().await, None);
|
||||
assert_eq!(current_session_id(), None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_session_id_request_builder_uses_custom_header() {
|
||||
with_session_id(Some("test-session-123".to_string()), async {
|
||||
|
||||
Reference in New Issue
Block a user