use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::driver::{ModelRoundTrace, TurnResult, TurnTrace};
use crate::interaction::TurnInteraction;
use causa_kernel::{
BlockContent, ConversationId, RoundId, StreamDelta, ToolCallPayload, TurnContext, TurnId,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextEvent {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub conversation_id: Option<ConversationId>,
pub turn_id: TurnId,
pub kind: ContextEventKind,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContextEventKind {
TurnStarted,
ToolBatchDispatched {
round_id: RoundId,
calls: Vec<ToolCallPayload>,
},
TurnOutcome {
result: TurnResult,
trace: TurnTrace,
},
TextDelta {
round_id: RoundId,
delta: String,
},
ReasoningDelta {
round_id: RoundId,
delta: String,
},
ToolCallDelta {
round_id: RoundId,
call_index: usize,
name_delta: Option<String>,
arguments_delta: Option<String>,
},
}
pub fn project_turn(
context: &TurnContext,
result: &TurnResult,
trace: &TurnTrace,
conversation_id: Option<ConversationId>,
) -> Vec<ContextEvent> {
let turn_id = context.turn_id();
let mut events = Vec::with_capacity(2 + trace.rounds.len());
events.push(ContextEvent {
conversation_id: conversation_id.clone(),
turn_id: turn_id.clone(),
kind: ContextEventKind::TurnStarted,
});
let calls_by_block = call_index(context);
for round in &trace.rounds {
if round.tool_batch.is_none() {
continue;
}
let calls = committed_calls(&calls_by_block, round);
events.push(ContextEvent {
conversation_id: conversation_id.clone(),
turn_id: turn_id.clone(),
kind: ContextEventKind::ToolBatchDispatched {
round_id: round.round_id,
calls,
},
});
}
events.push(ContextEvent {
conversation_id,
turn_id,
kind: ContextEventKind::TurnOutcome {
result: result.clone(),
trace: trace.clone(),
},
});
events
}
fn committed_calls(
call_index: &HashMap<causa_kernel::BlockId, ToolCallPayload>,
round: &ModelRoundTrace,
) -> Vec<ToolCallPayload> {
round
.applied_block_ids
.iter()
.filter_map(|id| call_index.get(id).cloned())
.collect()
}
pub struct StreamEventCollector {
turn_id: TurnId,
conversation_id: Option<ConversationId>,
events: std::sync::Mutex<Vec<ContextEvent>>,
}
impl StreamEventCollector {
pub fn new(turn_id: TurnId, conversation_id: Option<ConversationId>) -> Self {
Self {
turn_id,
conversation_id,
events: std::sync::Mutex::new(Vec::new()),
}
}
pub fn events(&self) -> Vec<ContextEvent> {
self.events.lock().unwrap().clone()
}
pub fn into_events(self) -> Vec<ContextEvent> {
self.events.into_inner().unwrap()
}
}
#[async_trait::async_trait]
impl TurnInteraction for StreamEventCollector {
async fn on_delta(&self, round_id: RoundId, delta: &StreamDelta) {
let kind = match delta {
StreamDelta::TextDelta { delta } => ContextEventKind::TextDelta {
round_id,
delta: delta.clone(),
},
StreamDelta::ReasoningDelta { delta } => ContextEventKind::ReasoningDelta {
round_id,
delta: delta.clone(),
},
StreamDelta::ToolCallDelta {
call_index,
name_delta,
arguments_delta,
..
} => ContextEventKind::ToolCallDelta {
round_id,
call_index: *call_index,
name_delta: name_delta.clone(),
arguments_delta: arguments_delta.clone(),
},
StreamDelta::Usage(_) | StreamDelta::Done { .. } | StreamDelta::Error { .. } => {
return;
}
};
self.events.lock().unwrap().push(ContextEvent {
conversation_id: self.conversation_id.clone(),
turn_id: self.turn_id.clone(),
kind,
});
}
}
pub fn project_streaming_turn(
context: &TurnContext,
result: &TurnResult,
trace: &TurnTrace,
conversation_id: Option<ConversationId>,
deltas: Vec<ContextEvent>,
) -> Vec<ContextEvent> {
let turn_id = context.turn_id();
let envelope = |kind: ContextEventKind| ContextEvent {
conversation_id: conversation_id.clone(),
turn_id: turn_id.clone(),
kind,
};
let mut delta_by_round: HashMap<u32, Vec<ContextEvent>> = HashMap::new();
for event in deltas {
let round = match &event.kind {
ContextEventKind::TextDelta { round_id, .. }
| ContextEventKind::ReasoningDelta { round_id, .. }
| ContextEventKind::ToolCallDelta { round_id, .. } => round_id.0,
_ => continue,
};
delta_by_round.entry(round).or_default().push(event);
}
let mut rounds: Vec<u32> = trace
.rounds
.iter()
.map(|r| r.round_id.0)
.chain(delta_by_round.keys().copied())
.collect();
rounds.sort_unstable();
rounds.dedup();
let mut events = Vec::with_capacity(2 + rounds.len());
events.push(envelope(ContextEventKind::TurnStarted));
for round in rounds {
if let Some(round_deltas) = delta_by_round.remove(&round) {
events.extend(round_deltas);
}
if let Some(trace_round) = trace
.rounds
.iter()
.find(|r| r.round_id.0 == round)
.filter(|r| r.tool_batch.is_some())
{
let calls = committed_calls(&call_index(context), trace_round);
events.push(envelope(ContextEventKind::ToolBatchDispatched {
round_id: RoundId(round),
calls,
}));
}
}
events.push(envelope(ContextEventKind::TurnOutcome {
result: result.clone(),
trace: trace.clone(),
}));
events
}
fn call_index(context: &TurnContext) -> HashMap<causa_kernel::BlockId, ToolCallPayload> {
context
.blocks()
.iter()
.filter_map(|b| match &b.content {
BlockContent::ToolCall(call) => Some((b.id.clone(), call.clone())),
_ => None,
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::driver::{OutputSummary, ToolBatchTrace, TurnInterruption};
use causa_kernel::{
BlockId, BlockSequence, ContextVersion, InvocationId, ModelResponse, ModelStopReason,
TextPayload, ToolCallId, TurnContext,
};
use serde_json::json;
fn turn_id(byte: u8) -> TurnId {
TurnId::new(format!("turn-{byte}"))
}
fn empty_trace() -> TurnTrace {
TurnTrace {
rounds: Vec::new(),
tool_calls_total: 0,
total_duration_ms: 0,
}
}
fn turn_with_tool_call(tool: &str) -> TurnContext {
let mut ctx = TurnContext::new(turn_id(1));
ctx.append_input(TextPayload::new("hi"), "user")
.expect("append_input");
let response = ModelResponse {
text: TextPayload::new(String::new()),
tool_calls: vec![causa_kernel::ToolCallDraft {
tool_name: tool.to_string(),
arguments: json!({"k": "v"}),
provider_call_id: None,
}],
};
ctx.append_model_output(
InvocationId {
turn_id: turn_id(1),
round_id: RoundId(0),
},
&response,
ModelStopReason::ToolUse,
)
.expect("append_model_output");
ctx
}
fn round_with_tool_batch(round_id: RoundId, call_block_seq: u64) -> ModelRoundTrace {
ModelRoundTrace {
round_id,
invocation_id: InvocationId {
turn_id: turn_id(1),
round_id,
},
frame_version: ContextVersion(0),
attempts: Vec::new(),
output_summary: Some(OutputSummary {
stop_reason: ModelStopReason::ToolUse,
usage: None,
tool_call_count: 1,
response_text_bytes: 0,
}),
applied_block_ids: vec![BlockId {
turn_id: turn_id(1),
sequence: BlockSequence(call_block_seq),
}],
tool_batch: Some(ToolBatchTrace {
calls: vec![],
completion_order: Vec::new(),
}),
}
}
fn round_endturn(round_id: RoundId) -> ModelRoundTrace {
ModelRoundTrace {
round_id,
invocation_id: InvocationId {
turn_id: turn_id(2),
round_id,
},
frame_version: ContextVersion(0),
attempts: Vec::new(),
output_summary: Some(OutputSummary {
stop_reason: ModelStopReason::EndTurn,
usage: None,
tool_call_count: 0,
response_text_bytes: 0,
}),
applied_block_ids: vec![],
tool_batch: None,
}
}
fn completed() -> TurnResult {
TurnResult::Completed {
final_output: causa_kernel::ModelOutput {
stop_reason: ModelStopReason::EndTurn,
usage: None,
reasoning: None,
response: ModelResponse {
text: TextPayload::new(String::new()),
tool_calls: Vec::new(),
},
},
}
}
#[test]
fn empty_trace_emits_only_started_and_outcome() {
let ctx = TurnContext::new(turn_id(1));
let events = project_turn(&ctx, &completed(), &empty_trace(), None);
assert_eq!(events.len(), 2);
assert!(matches!(events[0].kind, ContextEventKind::TurnStarted));
assert!(matches!(
events[1].kind,
ContextEventKind::TurnOutcome { .. }
));
}
#[test]
fn dispatched_calls_derive_from_committed_tool_call_blocks() {
let ctx = turn_with_tool_call("echo");
let mut trace = empty_trace();
trace.rounds.push(round_with_tool_batch(RoundId(0), 1));
let events = project_turn(&ctx, &completed(), &trace, None);
assert_eq!(events.len(), 3);
match &events[1].kind {
ContextEventKind::ToolBatchDispatched { round_id, calls } => {
assert_eq!(*round_id, RoundId(0));
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].tool_name, "echo");
assert_eq!(calls[0].arguments, json!({"k": "v"}));
}
other => panic!("expected ToolBatchDispatched, got {other:?}"),
}
}
#[test]
fn endturn_round_produces_no_dispatch_event() {
let ctx = TurnContext::new(turn_id(1));
let mut trace = empty_trace();
trace.rounds.push(round_endturn(RoundId(7)));
let events = project_turn(&ctx, &completed(), &trace, None);
assert_eq!(
events.len(),
2,
"EndTurn must not produce ToolBatchDispatched"
);
assert!(matches!(events[0].kind, ContextEventKind::TurnStarted));
assert!(matches!(
events[1].kind,
ContextEventKind::TurnOutcome { .. }
));
}
#[test]
fn multiple_rounds_emit_dispatches_in_round_id_order() {
let ctx = turn_with_tool_call("echo");
let mut trace = empty_trace();
trace.rounds.push(round_with_tool_batch(RoundId(0), 1));
trace.rounds.push(round_endturn(RoundId(1)));
trace.rounds.push(round_with_tool_batch(RoundId(2), 1));
let events = project_turn(&ctx, &completed(), &trace, None);
assert_eq!(events.len(), 4);
let dispatched_round_ids: Vec<RoundId> = events
.iter()
.filter_map(|e| match e.kind {
ContextEventKind::ToolBatchDispatched { round_id, .. } => Some(round_id),
_ => None,
})
.collect();
assert_eq!(dispatched_round_ids, vec![RoundId(0), RoundId(2)]);
}
#[test]
fn envelope_routes_conversation_and_turn_ids() {
let conv = ConversationId("conv-42".to_string());
let ctx = turn_with_tool_call("echo");
let mut trace = empty_trace();
trace.rounds.push(round_with_tool_batch(RoundId(0), 1));
let events = project_turn(&ctx, &completed(), &trace, Some(conv.clone()));
for e in &events {
assert_eq!(e.conversation_id, Some(conv.clone()));
assert_eq!(e.turn_id, turn_id(1));
}
let bare = project_turn(&ctx, &completed(), &trace, None);
let json = serde_json::to_string(&bare[0]).expect("serialize");
assert!(!json.contains("conversation_id"));
}
#[test]
fn interrupted_turn_carries_cause_in_outcome() {
let ctx = TurnContext::new(turn_id(1));
let mut trace = empty_trace();
trace.rounds.push(round_endturn(RoundId(0)));
let cause = TurnInterruption::MaxModelRounds { limit: 8 };
let events = project_turn(
&ctx,
&TurnResult::Interrupted {
cause: cause.clone(),
},
&trace,
None,
);
assert_eq!(events.len(), 2);
match &events[1].kind {
ContextEventKind::TurnOutcome { result, .. } => match result {
TurnResult::Interrupted { cause: c } => assert_eq!(*c, cause),
other => panic!("expected Interrupted, got {other:?}"),
},
other => panic!("expected TurnOutcome, got {other:?}"),
}
}
#[test]
fn round_trip_turn_started_no_conversation() {
let original = ContextEvent {
conversation_id: None,
turn_id: turn_id(7),
kind: ContextEventKind::TurnStarted,
};
let json = serde_json::to_string(&original).expect("serialize");
assert!(!json.contains("conversation_id"));
assert!(json.contains("\"type\":\"turn_started\""));
let restored: ContextEvent = serde_json::from_str(&json).expect("deserialize");
assert_eq!(restored.turn_id, turn_id(7));
assert_eq!(restored.conversation_id, None);
}
#[test]
fn round_trip_tool_batch_dispatched() {
let original = ContextEvent {
conversation_id: Some(ConversationId("conv-batch".into())),
turn_id: turn_id(11),
kind: ContextEventKind::ToolBatchDispatched {
round_id: RoundId(3),
calls: vec![ToolCallPayload {
call_id: ToolCallId("call-a".into()),
tool_name: "echo".into(),
arguments: json!({"k": "v"}),
}],
},
};
let json = serde_json::to_string(&original).expect("serialize");
let restored: ContextEvent = serde_json::from_str(&json).expect("deserialize");
match restored.kind {
ContextEventKind::ToolBatchDispatched { round_id, calls } => {
assert_eq!(round_id, RoundId(3));
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].call_id.0, "call-a");
}
other => panic!("expected ToolBatchDispatched, got {other:?}"),
}
assert_eq!(
restored.conversation_id,
Some(ConversationId("conv-batch".into()))
);
assert_eq!(restored.turn_id, turn_id(11));
}
#[test]
fn round_trip_turn_outcome_completed_and_interrupted() {
let completed_event = ContextEvent {
conversation_id: Some(ConversationId("conv-out".into())),
turn_id: turn_id(99),
kind: ContextEventKind::TurnOutcome {
result: completed(),
trace: empty_trace(),
},
};
let json = serde_json::to_string(&completed_event).expect("serialize");
let restored: ContextEvent = serde_json::from_str(&json).expect("deserialize");
assert!(matches!(
restored.kind,
ContextEventKind::TurnOutcome {
result: TurnResult::Completed { .. },
..
}
));
let interrupted_event = ContextEvent {
conversation_id: None,
turn_id: turn_id(13),
kind: ContextEventKind::TurnOutcome {
result: TurnResult::Interrupted {
cause: TurnInterruption::CompactionFailed {
reason: "budget exceeded".into(),
},
},
trace: empty_trace(),
},
};
let json = serde_json::to_string(&interrupted_event).expect("serialize");
let restored: ContextEvent = serde_json::from_str(&json).expect("deserialize");
match restored.kind {
ContextEventKind::TurnOutcome {
result: TurnResult::Interrupted { cause },
..
} => assert_eq!(
cause,
TurnInterruption::CompactionFailed {
reason: "budget exceeded".into()
}
),
other => panic!("expected TurnOutcome Interrupted, got {other:?}"),
}
}
#[test]
fn project_turn_output_survives_serialization_round_trip() {
let ctx = turn_with_tool_call("echo");
let mut trace = empty_trace();
trace.rounds.push(round_with_tool_batch(RoundId(0), 1));
let events = project_turn(
&ctx,
&completed(),
&trace,
Some(ConversationId("conv-e2e".into())),
);
let json = serde_json::to_string(&events).expect("serialize");
let restored: Vec<ContextEvent> = serde_json::from_str(&json).expect("deserialize");
assert_eq!(restored.len(), events.len());
for e in &restored {
assert_eq!(e.conversation_id, Some(ConversationId("conv-e2e".into())));
assert_eq!(e.turn_id, turn_id(1));
}
match &restored[1].kind {
ContextEventKind::ToolBatchDispatched { calls, .. } => {
assert_eq!(calls.len(), 1);
}
other => panic!("expected ToolBatchDispatched, got {other:?}"),
}
}
#[test]
fn round_trip_streaming_delta_variants() {
let variants = vec![
ContextEvent {
conversation_id: Some(ConversationId("conv-s".into())),
turn_id: turn_id(21),
kind: ContextEventKind::TextDelta {
round_id: RoundId(1),
delta: "he".into(),
},
},
ContextEvent {
conversation_id: None,
turn_id: turn_id(22),
kind: ContextEventKind::ReasoningDelta {
round_id: RoundId(0),
delta: "pondering".into(),
},
},
ContextEvent {
conversation_id: None,
turn_id: turn_id(23),
kind: ContextEventKind::ToolCallDelta {
round_id: RoundId(2),
call_index: 1,
name_delta: Some("echo".into()),
arguments_delta: Some("{\"a\":1}".into()),
},
},
];
for original in variants {
let json = serde_json::to_string(&original).expect("serialize");
let restored: ContextEvent = serde_json::from_str(&json).expect("deserialize");
assert_eq!(
serde_json::to_string(&restored).expect("re-serialize"),
json,
"round-trip must be stable"
);
}
}
#[test]
fn streaming_delta_wire_discriminators_are_pinned() {
let event = ContextEvent {
conversation_id: None,
turn_id: turn_id(1),
kind: ContextEventKind::TextDelta {
round_id: RoundId(0),
delta: "x".into(),
},
};
let json = serde_json::to_string(&event).expect("serialize");
assert!(json.contains("\"type\":\"text_delta\""), "{json}");
assert!(json.contains("\"round_id\":0"), "{json}");
let call = ContextEvent {
conversation_id: None,
turn_id: turn_id(1),
kind: ContextEventKind::ToolCallDelta {
round_id: RoundId(0),
call_index: 0,
name_delta: Some("echo".into()),
arguments_delta: None,
},
};
let json = serde_json::to_string(&call).expect("serialize");
assert!(json.contains("\"type\":\"tool_call_delta\""), "{json}");
let reasoning = ContextEvent {
conversation_id: None,
turn_id: turn_id(1),
kind: ContextEventKind::ReasoningDelta {
round_id: RoundId(0),
delta: "r".into(),
},
};
let json = serde_json::to_string(&reasoning).expect("serialize");
assert!(json.contains("\"type\":\"reasoning_delta\""), "{json}");
}
#[test]
fn collector_maps_deltas_and_skips_terminal_ones() {
let collector = StreamEventCollector::new(turn_id(9), None);
let deltas = vec![
StreamDelta::TextDelta { delta: "he".into() },
StreamDelta::ReasoningDelta { delta: "th".into() },
StreamDelta::ToolCallDelta {
call_index: 0,
provider_call_id: Some("p1".into()),
name_delta: Some("echo".into()),
arguments_delta: None,
},
StreamDelta::Usage(causa_kernel::ModelUsage {
input_tokens: 1,
output_tokens: 2,
cache_read_tokens: None,
cache_write_tokens: None,
reasoning_tokens: None,
}),
StreamDelta::Done {
stop_reason: ModelStopReason::EndTurn,
final_output: causa_kernel::ModelOutput {
response: ModelResponse {
text: TextPayload::new("he"),
tool_calls: vec![],
},
usage: None,
stop_reason: ModelStopReason::EndTurn,
reasoning: None,
},
},
StreamDelta::Error {
kind: causa_kernel::ModelInvokeErrorKind::Transient,
message: "x".into(),
},
];
let rt = tokio::runtime::Runtime::new().unwrap();
for d in &deltas {
rt.block_on(collector.on_delta(RoundId(4), d));
}
let events = collector.into_events();
assert_eq!(events.len(), 3);
assert!(matches!(
events[0].kind,
ContextEventKind::TextDelta {
round_id: RoundId(4),
..
}
));
assert!(matches!(
events[1].kind,
ContextEventKind::ReasoningDelta { .. }
));
match &events[2].kind {
ContextEventKind::ToolCallDelta {
call_index,
name_delta,
..
} => {
assert_eq!(*call_index, 0);
assert_eq!(name_delta.as_deref(), Some("echo"));
}
other => panic!("expected ToolCallDelta, got {other:?}"),
}
}
}