Skip to content
Closed
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
3 changes: 3 additions & 0 deletions src/apps/cli/src/modes/chat/session_lineage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,7 @@ mod session_lineage_tests {
user_input: "continue".to_string(),
original_user_input: None,
user_message_metadata: None,
session_workspace: None,
},
);
assert_eq!(
Expand Down Expand Up @@ -1290,6 +1291,7 @@ mod session_lineage_tests {
user_input: "continue".to_string(),
original_user_input: None,
user_message_metadata: None,
session_workspace: None,
},
AgenticEvent::TextChunk {
session_id: "first".to_string(),
Expand Down Expand Up @@ -1525,6 +1527,7 @@ mod session_lineage_tests {
user_input: "question".to_string(),
original_user_input: None,
user_message_metadata: None,
session_workspace: None,
},
false,
);
Expand Down
40 changes: 36 additions & 4 deletions src/crates/assembly/core/src/agentic/coordination/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4657,6 +4657,9 @@ Update the persona files and delete BOOTSTRAP.md as soon as bootstrap is complet
user_input: prompt.clone(),
original_user_input: (display_input != prompt).then_some(display_input),
user_message_metadata: Some(user_message_metadata.clone()),
session_workspace: self
.session_manager
.get_session_workspace_facts(&session_id),
})
.await;
self.emit_event(AgenticEvent::ModelRoundStarted {
Expand Down Expand Up @@ -6178,6 +6181,9 @@ Update the persona files and delete BOOTSTRAP.md as soon as bootstrap is complet
user_input: MANUAL_COMPACTION_COMMAND.to_string(),
original_user_input: None,
user_message_metadata: user_message_metadata.clone(),
session_workspace: self
.session_manager
.get_session_workspace_facts(&session_id),
})
.await;

Expand Down Expand Up @@ -7414,6 +7420,9 @@ Update the persona files and delete BOOTSTRAP.md as soon as bootstrap is complet
None
},
user_message_metadata: user_message_metadata.clone(),
session_workspace: self
.session_manager
.get_session_workspace_facts(&session_id),
})
.await;

Expand Down Expand Up @@ -11252,6 +11261,9 @@ Update the persona files and delete BOOTSTRAP.md as soon as bootstrap is complet
user_input: user_input_text.clone(),
original_user_input: None,
user_message_metadata: None,
session_workspace: self
.session_manager
.get_session_workspace_facts(&session_id),
})
.await;
}
Expand Down Expand Up @@ -16005,6 +16017,9 @@ impl openbitfun_runtime_ports::AgentUserShellCommandPort for ConversationCoordin
user_input: format!("!{}", request.command),
original_user_input: None,
user_message_metadata: None,
session_workspace: self
.session_manager
.get_session_workspace_facts(&request.session_id),
};

let session_manager = Arc::clone(&self.session_manager);
Expand Down Expand Up @@ -21706,10 +21721,27 @@ pub(crate) mod tests {
.expect("load turns");
let turn = turns.last().expect("shell turn");
let events = coordinator.event_queue.dequeue_batch(100).await;
assert!(events.iter().any(|envelope| matches!(
&envelope.event,
AgenticEvent::DialogTurnStarted { turn_id, .. } if turn_id == "turn-shell"
)));
let started_workspace = events
.iter()
.find_map(|envelope| match &envelope.event {
AgenticEvent::DialogTurnStarted {
turn_id,
session_workspace,
..
} if turn_id == "turn-shell" => Some(session_workspace.clone()),
_ => None,
})
.expect("shell turn started event");
let announced_path = started_workspace
.and_then(|binding| binding.workspace_path)
.expect("turn-started event carries the session workspace");
assert_eq!(
Some(announced_path),
session_manager
.get_session(&session.session_id)
.and_then(|session| session.config.workspace_path),
"turn-started event must announce the session's own workspace binding",
);
assert!(!events.iter().any(|envelope| matches!(
&envelope.event,
AgenticEvent::DialogTurnFailed { turn_id, .. } if turn_id == "turn-shell"
Expand Down
19 changes: 19 additions & 0 deletions src/crates/assembly/core/src/agentic/session/session_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2971,6 +2971,25 @@ impl SessionManager {
.map(|session| session.state.clone())
}

/// Workspace binding announced on turn events; reads only config fields.
pub fn get_session_workspace_facts(
&self,
session_id: &str,
) -> Option<openbitfun_events::SessionWorkspaceFacts> {
self.sessions.get(session_id).map(|session| {
let config = &session.config;
openbitfun_events::SessionWorkspaceFacts {
workspace_path: config.workspace_path.clone(),
project_workspace_path: config.project_workspace_path.clone(),
execution_target: config.execution_target.clone(),
workspace_id: config.workspace_id.clone(),
project_workspace_id: config.project_workspace_id.clone(),
remote_connection_id: config.remote_connection_id.clone(),
remote_ssh_host: config.remote_ssh_host.clone(),
}
})
}

pub async fn cached_system_prompt(
&self,
session_id: &str,
Expand Down
27 changes: 27 additions & 0 deletions src/crates/contracts/events/src/agentic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ pub struct DeepReviewQueueState {
pub session_concurrency_high: bool,
}

/// Workspace binding of the session a turn event belongs to.
///
/// A consumer that has not loaded the session yet (for example, a scheduled
/// job running in a workspace the UI has not opened) places the session from
/// these facts instead of guessing from its own foreground workspace.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SessionWorkspaceFacts {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub workspace_path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project_workspace_path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub execution_target: Option<SessionExecutionTarget>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub workspace_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project_workspace_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub remote_connection_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub remote_ssh_host: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum AgenticEvent {
Expand Down Expand Up @@ -182,6 +205,10 @@ pub enum AgenticEvent {
original_user_input: Option<String>,
/// Image metadata JSON for UI rendering (id, name, data_url, mime_type, image_path)
user_message_metadata: Option<serde_json::Value>,
/// Workspace the session is bound to. Absent in events written by
/// older hosts and in emitters that do not know the binding.
#[serde(default, skip_serializing_if = "Option::is_none")]
session_workspace: Option<SessionWorkspaceFacts>,
},

/// Low-frequency linking event that associates a hidden subagent session
Expand Down
115 changes: 109 additions & 6 deletions src/crates/contracts/events/src/frontend_projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,53 @@ pub fn project_agentic_frontend_event(event: AgenticEvent) -> Option<AgenticFron
user_input,
original_user_input,
user_message_metadata,
} => Some(AgenticFrontendEvent::new(
"agentic://dialog-turn-started",
json!({
session_workspace,
} => {
let mut payload = json!({
"sessionId": session_id,
"turnId": turn_id,
"turnIndex": turn_index,
"userInput": user_input,
"originalUserInput": original_user_input,
"userMessageMetadata": user_message_metadata,
}),
)),
});
if let (Some(workspace), Some(fields)) = (session_workspace, payload.as_object_mut()) {
// Same flat keys as `agentic://session-created`, so the UI can
// place a session it has not loaded with one resolver.
let entries = [
("workspacePath", workspace.workspace_path.map(Value::from)),
(
"projectWorkspacePath",
workspace.project_workspace_path.map(Value::from),
),
(
"executionTarget",
workspace
.execution_target
.and_then(|target| serde_json::to_value(target).ok()),
),
("workspaceId", workspace.workspace_id.map(Value::from)),
(
"projectWorkspaceId",
workspace.project_workspace_id.map(Value::from),
),
(
"remoteConnectionId",
workspace.remote_connection_id.map(Value::from),
),
("remoteSshHost", workspace.remote_ssh_host.map(Value::from)),
];
for (key, value) in entries {
if let Some(value) = value {
fields.insert(key.to_string(), value);
}
}
}
Some(AgenticFrontendEvent::new(
"agentic://dialog-turn-started",
payload,
))
}
AgenticEvent::SubagentSessionLinked {
session_id,
subagent_dialog_turn_id,
Expand Down Expand Up @@ -546,7 +582,7 @@ mod tests {
use super::*;
use crate::{
DeepReviewQueueReason, DeepReviewQueueState, DeepReviewQueueStatus,
ModelRoundAttemptDiagnostic,
ModelRoundAttemptDiagnostic, SessionWorkspaceFacts,
};
use openbitfun_core_types::{
ReasoningContentKind, SessionExecutionTarget, SessionExecutionTargetKind, WorktreeLifecycle,
Expand Down Expand Up @@ -607,6 +643,73 @@ mod tests {
assert_eq!(projected.payload["workspaceId"], "workspace-wt-1");
}

#[test]
fn dialog_turn_started_projects_session_workspace_binding() {
let projected = project_agentic_frontend_event(AgenticEvent::DialogTurnStarted {
session_id: "session-1".to_string(),
turn_id: "turn-1".to_string(),
turn_index: 3,
user_input: "check".to_string(),
original_user_input: None,
user_message_metadata: None,
session_workspace: Some(SessionWorkspaceFacts {
workspace_path: Some("/assistants/pdf".to_string()),
project_workspace_path: Some("/assistants/pdf".to_string()),
execution_target: Some(SessionExecutionTarget {
kind: SessionExecutionTargetKind::Local,
worktree_id: None,
root_path: "/assistants/pdf".to_string(),
base_ref: None,
base_commit: None,
branch: None,
lifecycle: None,
}),
workspace_id: Some("workspace-pdf".to_string()),
project_workspace_id: Some("workspace-pdf".to_string()),
remote_connection_id: None,
remote_ssh_host: None,
}),
})
.expect("projected");

assert_eq!(projected.event_name, "agentic://dialog-turn-started");
assert_eq!(projected.payload["workspacePath"], "/assistants/pdf");
assert_eq!(projected.payload["projectWorkspacePath"], "/assistants/pdf");
assert_eq!(projected.payload["workspaceId"], "workspace-pdf");
assert_eq!(projected.payload["projectWorkspaceId"], "workspace-pdf");
assert_eq!(
projected.payload["executionTarget"]["rootPath"],
"/assistants/pdf"
);
assert!(projected.payload.get("remoteConnectionId").is_none());
}

#[test]
fn legacy_dialog_turn_started_without_workspace_still_round_trips() {
let legacy = serde_json::json!({
"type": "DialogTurnStarted",
"session_id": "session-1",
"turn_id": "turn-1",
"turn_index": 0,
"user_input": "hello",
"original_user_input": null,
"user_message_metadata": null,
});
let event: AgenticEvent = serde_json::from_value(legacy.clone()).expect("legacy event");
let AgenticEvent::DialogTurnStarted {
session_workspace, ..
} = &event
else {
panic!("expected DialogTurnStarted");
};
assert!(session_workspace.is_none());
assert_eq!(serde_json::to_value(&event).expect("serialize"), legacy);

let projected = project_agentic_frontend_event(event).expect("projected");
assert!(projected.payload.get("workspaceId").is_none());
assert!(projected.payload.get("workspacePath").is_none());
}

#[test]
fn thinking_chunk_projects_to_text_chunk_event() {
let projected = project_agentic_frontend_event(AgenticEvent::ThinkingChunk {
Expand Down
3 changes: 2 additions & 1 deletion src/crates/contracts/events/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub mod types;
pub use agentic::{
AgenticEvent, AgenticEventEnvelope, AgenticEventPriority, DeepReviewQueueReason,
DeepReviewQueueState, DeepReviewQueueStatus, ModelRoundAttemptDiagnostic,
ModelRoundAttemptToolDiagnostic, SubagentParentInfo, ToolEventData, ToolEventIdentity,
ModelRoundAttemptToolDiagnostic, SessionWorkspaceFacts, SubagentParentInfo, ToolEventData,
ToolEventIdentity,
};
pub use backend::{
BackgroundCommandLifecycleInfo, ToolExecutionCompletedInfo, ToolExecutionErrorInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ mod tests {
user_input: "prompt".to_string(),
original_user_input: None,
user_message_metadata: None,
session_workspace: None,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6281,6 +6281,7 @@ mod tests {
user_input: "hidden".to_string(),
original_user_input: None,
user_message_metadata: None,
session_workspace: None,
});
tracker.mark_persistence_clean();
let previous_version = tracker.version();
Expand Down Expand Up @@ -6371,6 +6372,7 @@ mod tests {
user_input: "hello".to_string(),
original_user_input: None,
user_message_metadata: None,
session_workspace: None,
});
tracker.handle_agentic_event(&AgenticEvent::DialogTurnCompleted {
session_id: "session-a".to_string(),
Expand Down Expand Up @@ -6439,6 +6441,7 @@ mod tests {
user_input: "hello".to_string(),
original_user_input: None,
user_message_metadata: None,
session_workspace: None,
});
tracker.handle_agentic_event(&AgenticEvent::DialogTurnCompleted {
session_id: "session-a".to_string(),
Expand Down Expand Up @@ -6529,6 +6532,7 @@ mod tests {
user_input: "hello".to_string(),
original_user_input: None,
user_message_metadata: None,
session_workspace: None,
});
tracker.handle_agentic_event(&AgenticEvent::DialogTurnFailed {
session_id: "session-a".to_string(),
Expand Down Expand Up @@ -6558,6 +6562,7 @@ mod tests {
user_input: "queued follow-up".to_string(),
original_user_input: None,
user_message_metadata: None,
session_workspace: None,
});
tracker.handle_agentic_event(&AgenticEvent::TextChunk {
session_id: "session-a".to_string(),
Expand Down
Loading