ui (subagent): Show subagent session link and make subagent sessions view-only (#12998)
This commit is contained in:
@@ -92,7 +92,7 @@ use self::message_meta::{
|
||||
use self::tool_calls::chain::{breaks_consecutive_tool_calls, ReadyToolChain, ToolChainTracker};
|
||||
use self::tool_calls::conversion::{
|
||||
build_initial_tool_call_with_message_meta, build_permission_tool_call_update,
|
||||
tool_call_update_fields_from_response, trusted_update_meta,
|
||||
tool_call_update_fields_from_response, tool_response_meta,
|
||||
};
|
||||
use self::tool_calls::enrichment::{spawn_chain_summary_enrichment, spawn_tool_title_enrichment};
|
||||
|
||||
@@ -1540,7 +1540,7 @@ impl GooseAcpAgent {
|
||||
let fields = tool_call_update_fields_from_response(tool_response, tool_request, false);
|
||||
|
||||
let update = ToolCallUpdate::new(ToolCallId::new(tool_response.id.clone()), fields)
|
||||
.meta(trusted_update_meta(tool_response));
|
||||
.meta(tool_response_meta(tool_response));
|
||||
let tool_call_notifier = ToolCallNotifier::new(cx, session_id);
|
||||
tool_call_notifier.send_update(update)?;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ use super::message_meta::{
|
||||
};
|
||||
use super::tool_calls::conversion::{
|
||||
build_initial_tool_call_with_message_meta, tool_call_update_fields_from_response,
|
||||
trusted_update_meta,
|
||||
tool_response_meta,
|
||||
};
|
||||
use super::tool_calls::enrichment::tool_chain_summary;
|
||||
use super::*;
|
||||
@@ -172,7 +172,7 @@ fn replay_conversation_to_client(
|
||||
replay_tool_requests.get(&tool_response.id),
|
||||
true,
|
||||
);
|
||||
let meta = trusted_update_meta(tool_response).unwrap_or_default();
|
||||
let meta = tool_response_meta(tool_response).unwrap_or_default();
|
||||
|
||||
let update =
|
||||
ToolCallUpdate::new(ToolCallId::new(tool_response.id.clone()), fields)
|
||||
@@ -411,6 +411,16 @@ impl GooseAcpAgent {
|
||||
self.requests_tool_call_label_enrichment(),
|
||||
replay_tail_from_meta(args.meta.as_ref()),
|
||||
)?;
|
||||
let pending_confirmations = session
|
||||
.conversation
|
||||
.as_ref()
|
||||
.map(pending_tool_confirmations)
|
||||
.unwrap_or_default();
|
||||
let awaiting_confirmation = !pending_confirmations.is_empty()
|
||||
|| session
|
||||
.conversation
|
||||
.as_ref()
|
||||
.is_some_and(has_unapplied_tool_confirmation_response);
|
||||
let (agent, extension_results) = self.prepare_acp_session_agent(cx, &session).await?;
|
||||
self.register_acp_session(session_id_str.clone(), agent.clone())
|
||||
.await;
|
||||
@@ -446,17 +456,9 @@ impl GooseAcpAgent {
|
||||
}
|
||||
response = response.meta(meta);
|
||||
|
||||
let pending_confirmations = session
|
||||
.conversation
|
||||
.as_ref()
|
||||
.map(pending_tool_confirmations)
|
||||
.unwrap_or_default();
|
||||
let should_resume_state_machine = !pending_confirmations.is_empty()
|
||||
|| session
|
||||
.conversation
|
||||
.as_ref()
|
||||
.is_some_and(has_unapplied_tool_confirmation_response);
|
||||
if should_resume_state_machine {
|
||||
let is_subagent = session.session_type == SessionType::SubAgent;
|
||||
let already_running = self.active_runs.agent_run(&session_id_str).is_some();
|
||||
if awaiting_confirmation && !is_subagent && !already_running {
|
||||
self.start_resumed_state_machine_turn(
|
||||
cx,
|
||||
&agent,
|
||||
@@ -464,14 +466,6 @@ impl GooseAcpAgent {
|
||||
&pending_confirmations,
|
||||
)
|
||||
.await?;
|
||||
} else {
|
||||
self.resend_pending_tool_permissions(
|
||||
cx,
|
||||
&agent,
|
||||
&session_id_str,
|
||||
&pending_confirmations,
|
||||
None,
|
||||
)?;
|
||||
}
|
||||
|
||||
self.closed_session_ids.lock().await.remove(&session_id_str);
|
||||
|
||||
@@ -213,16 +213,29 @@ fn extract_tool_locations_from_request(tool_request: &ToolRequest) -> Vec<ToolCa
|
||||
vec![ToolCallLocation::new(path).line(line)]
|
||||
}
|
||||
|
||||
pub(crate) fn trusted_update_meta(tool_response: &ToolResponse) -> Option<Meta> {
|
||||
pub(crate) fn tool_response_meta(tool_response: &ToolResponse) -> Option<Meta> {
|
||||
let tool_result = tool_response.tool_result.as_ref().ok()?;
|
||||
let goose_meta = tool_result
|
||||
.meta
|
||||
.as_ref()?
|
||||
let result_meta = tool_result.meta.as_ref()?;
|
||||
let mut goose_meta = result_meta
|
||||
.0
|
||||
.get(TRUSTED_TOOL_UPDATE_META_KEY)?
|
||||
.clone();
|
||||
.get(TRUSTED_TOOL_UPDATE_META_KEY)
|
||||
.and_then(serde_json::Value::as_object)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
|
||||
if let Some(subagent_id) = result_meta
|
||||
.0
|
||||
.get("subagent_session_id")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
{
|
||||
goose_meta.insert("subagentSessionId".to_string(), subagent_id.into());
|
||||
}
|
||||
|
||||
if goose_meta.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut meta_map = serde_json::Map::new();
|
||||
meta_map.insert("goose".to_string(), goose_meta);
|
||||
meta_map.insert("goose".to_string(), serde_json::Value::Object(goose_meta));
|
||||
Some(meta_map)
|
||||
}
|
||||
|
||||
@@ -720,9 +733,24 @@ mod tests {
|
||||
.map(|locs| locs.into_iter().map(|loc| (loc.path, loc.line)).collect())
|
||||
}
|
||||
|
||||
mod trusted_update_meta {
|
||||
mod tool_response_meta {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn forwards_subagent_session_id() {
|
||||
let response = response_with_meta(Some(serde_json::json!({
|
||||
"subagent_session_id": "child-session",
|
||||
"foreground_subagent": true,
|
||||
"unrelated": "not forwarded",
|
||||
})));
|
||||
|
||||
let meta = tool_response_meta(&response).expect("expected subagent metadata");
|
||||
assert_eq!(
|
||||
serde_json::Value::Object(meta),
|
||||
serde_json::json!({"goose": {"subagentSessionId": "child-session"}}),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignores_untrusted_goose_meta() {
|
||||
let response = response_with_meta(Some(serde_json::json!({
|
||||
@@ -733,12 +761,13 @@ mod tests {
|
||||
},
|
||||
})));
|
||||
|
||||
assert_eq!(trusted_update_meta(&response), None);
|
||||
assert_eq!(tool_response_meta(&response), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_trusted_meta_only() {
|
||||
fn preserves_trusted_app_meta_alongside_subagent_session() {
|
||||
let response = response_with_meta(Some(serde_json::json!({
|
||||
"subagent_session_id": "child-session",
|
||||
"goose": {
|
||||
"mcpApp": {
|
||||
"resourceUri": "ui://spoofed/app",
|
||||
@@ -753,10 +782,11 @@ mod tests {
|
||||
},
|
||||
})));
|
||||
|
||||
let extracted = trusted_update_meta(&response).expect("expected trusted meta");
|
||||
let extracted = tool_response_meta(&response).expect("expected trusted meta");
|
||||
assert_eq!(
|
||||
extracted.get("goose"),
|
||||
Some(&serde_json::json!({
|
||||
"subagentSessionId": "child-session",
|
||||
"mcpApp": {
|
||||
"resourceUri": "ui://trusted/app",
|
||||
"extensionName": "weather",
|
||||
|
||||
@@ -23,9 +23,9 @@ use tracing::warn;
|
||||
|
||||
use super::{
|
||||
get_tool_meta_value, get_tool_owner, get_tool_resource_uri, insert_trusted_tool_update_meta,
|
||||
recover_mangled_tool_name, remove_untrusted_mcp_app_meta, ActionRequiredStream, Extension,
|
||||
ExtensionLoadResult, ExtensionMutation, ExtensionSlot, GooseMcpAppToolAttachment,
|
||||
TOOL_CALL_NOTIFICATION_CHANNEL_CAPACITY,
|
||||
recover_mangled_tool_name, remove_subagent_session_meta, remove_untrusted_mcp_app_meta,
|
||||
ActionRequiredStream, Extension, ExtensionLoadResult, ExtensionMutation, ExtensionSlot,
|
||||
GooseMcpAppToolAttachment, TOOL_CALL_NOTIFICATION_CHANNEL_CAPACITY,
|
||||
};
|
||||
use crate::action_required_manager::ActionRequiredManager;
|
||||
use crate::agents::container::Container;
|
||||
@@ -712,6 +712,7 @@ impl ExtensionLease {
|
||||
remove_untrusted_mcp_app_meta(&mut result);
|
||||
if !is_platform_extension {
|
||||
ExtensionMutation::take(&mut result);
|
||||
remove_subagent_session_meta(&mut result);
|
||||
}
|
||||
if let Some(mcp_app_call) = mcp_app_call {
|
||||
mcp_app_call
|
||||
|
||||
@@ -522,6 +522,18 @@ fn remove_untrusted_mcp_app_meta(result: &mut CallToolResult) {
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_subagent_session_meta(result: &mut CallToolResult) {
|
||||
let Some(meta) = result.meta.as_mut() else {
|
||||
return;
|
||||
};
|
||||
|
||||
meta.0.remove("subagent_session_id");
|
||||
|
||||
if meta.0.is_empty() {
|
||||
result.meta = None;
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_trusted_tool_update_meta(
|
||||
result: &mut CallToolResult,
|
||||
attachment: &GooseMcpAppToolAttachment,
|
||||
@@ -1531,6 +1543,89 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
struct SubagentMetaClient;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl McpClientTrait for SubagentMetaClient {
|
||||
async fn list_tools(
|
||||
&self,
|
||||
_session_id: &str,
|
||||
_next_cursor: Option<String>,
|
||||
_cancellation_token: CancellationToken,
|
||||
) -> Result<ListToolsResult, Error> {
|
||||
Ok(ListToolsResult {
|
||||
tools: vec![Tool::new(
|
||||
"delegate".to_string(),
|
||||
"test tool".to_string(),
|
||||
Arc::new(serde_json::Map::new()),
|
||||
)],
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
async fn call_tool(
|
||||
&self,
|
||||
_ctx: &ToolCallContext,
|
||||
_name: &str,
|
||||
_arguments: Option<JsonObject>,
|
||||
_cancellation_token: CancellationToken,
|
||||
) -> Result<CallToolResult, Error> {
|
||||
let meta = serde_json::json!({
|
||||
"subagent_session_id": "other-session",
|
||||
"unrelated": "kept",
|
||||
});
|
||||
Ok(CallToolResult::success(vec![])
|
||||
.with_meta(Some(MetaObject(meta.as_object().unwrap().clone()))))
|
||||
}
|
||||
|
||||
fn get_info(&self) -> Option<&InitializeResult> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
async fn dispatch_subagent_meta_tool(extension: &str, tool: &str) -> Option<MetaObject> {
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let extension_manager = Arc::new(ExtensionManager::with_data_dir(
|
||||
temp_dir.path().to_path_buf(),
|
||||
));
|
||||
extension_manager
|
||||
.add_mock_extension(extension.to_string(), Arc::new(SubagentMetaClient))
|
||||
.await;
|
||||
|
||||
let dispatched = extension_manager
|
||||
.scope_lease("session")
|
||||
.await
|
||||
.call(
|
||||
CallToolRequestParams::new(tool.to_string()).with_arguments(object!({})),
|
||||
CallRequest::from(&ToolCallContext::new("session".to_string(), None, None)),
|
||||
CancellationToken::default(),
|
||||
)
|
||||
.await
|
||||
.expect("tool call should dispatch");
|
||||
|
||||
dispatched.result.await.unwrap().meta
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dispatch_strips_subagent_session_id_from_external_extensions() {
|
||||
let meta = dispatch_subagent_meta_tool("external", "external__delegate").await;
|
||||
|
||||
assert_eq!(
|
||||
meta.map(|meta| serde_json::Value::Object(meta.0)),
|
||||
Some(serde_json::json!({"unrelated": "kept"})),
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dispatch_keeps_subagent_session_id_from_platform_extensions() {
|
||||
let meta = dispatch_subagent_meta_tool("summon", "delegate").await;
|
||||
|
||||
assert_eq!(
|
||||
meta.and_then(|meta| meta.0.get("subagent_session_id").cloned()),
|
||||
Some(serde_json::json!("other-session")),
|
||||
);
|
||||
}
|
||||
|
||||
fn app_tool(name: &str) -> Tool {
|
||||
let mut tool = Tool::new(
|
||||
name.to_string(),
|
||||
|
||||
@@ -836,6 +836,65 @@ fn test_load_session_replays_image_attachment() {
|
||||
run_test(async { run_load_session_replays_image_attachment::<AcpServerConnection>().await });
|
||||
}
|
||||
|
||||
async fn seed_session_awaiting_approval(data_root: &Path, session_type: SessionType) -> String {
|
||||
let session_manager = SessionManager::new(data_root.to_path_buf());
|
||||
let session = session_manager
|
||||
.create_session(
|
||||
data_root.to_path_buf(),
|
||||
"Awaiting approval".to_string(),
|
||||
session_type,
|
||||
GooseMode::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
for message in [
|
||||
Message::user().with_text("run the tool"),
|
||||
Message::assistant().with_action_required(
|
||||
"approval",
|
||||
"tool".to_string(),
|
||||
Default::default(),
|
||||
None,
|
||||
),
|
||||
] {
|
||||
session_manager
|
||||
.add_message(&session.id, &message)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
session.id
|
||||
}
|
||||
|
||||
fn starts_a_run(updates: &[SessionUpdate]) -> bool {
|
||||
updates.iter().any(|update| match update {
|
||||
SessionUpdate::SessionInfoUpdate(info) => info
|
||||
.meta
|
||||
.as_ref()
|
||||
.and_then(|meta| meta.get("goose"))
|
||||
.and_then(|goose| goose.get("activeRunId"))
|
||||
.is_some_and(serde_json::Value::is_string),
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_session_resumes_pending_approval_except_for_subagents() {
|
||||
run_test(async {
|
||||
for (session_type, resumes) in [(SessionType::Acp, true), (SessionType::SubAgent, false)] {
|
||||
let data_root = tempfile::tempdir().unwrap();
|
||||
let session_id = seed_session_awaiting_approval(data_root.path(), session_type).await;
|
||||
let mut conn = new_connection(data_root.path()).await;
|
||||
|
||||
let SessionData { session, .. } = conn.load_session(&session_id, vec![]).await.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
starts_a_run(&session.session_updates()),
|
||||
resumes,
|
||||
"{session_type:?}"
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mode_set() {
|
||||
run_test(async { run_mode_set::<AcpServerConnection>().await });
|
||||
|
||||
Reference in New Issue
Block a user