use causa_kernel::{
AttemptControl, AttemptNumber, CancellationToken, ContextError, ContextVersion,
GenerationOptions, InvocationId, ModelGateway, ModelInvokeError, ModelOutput, ModelRef,
ModelRequest, ModelResponse, ModelStopReason, ModelUsage, ReasoningPayload, RoundId,
TextPayload, ToolSurface, TurnContext, TurnId, TurnSnapshot,
};
struct OneShotGateway {
output: ModelOutput,
}
#[async_trait::async_trait]
impl ModelGateway for OneShotGateway {
async fn invoke(
&self,
request: &ModelRequest,
_control: &AttemptControl,
) -> Result<ModelOutput, ModelInvokeError> {
assert_eq!(request.invocation_id.round_id, RoundId(0));
assert_eq!(request.attempt, AttemptNumber(1));
assert_eq!(request.frame.model_context.blocks.len(), 1);
Ok(self.output.clone())
}
}
#[tokio::test]
async fn external_single_shot_driver_assembles_from_root_facade() {
let mut context = TurnContext::new(TurnId::new("ext-1"));
context
.append_input(TextPayload::new("hi"), "user")
.unwrap();
let gateway = OneShotGateway {
output: ModelOutput {
response: ModelResponse {
text: TextPayload::new("hello"),
tool_calls: vec![],
},
usage: Some(ModelUsage {
input_tokens: 10,
output_tokens: 2,
cache_read_tokens: Some(4),
cache_write_tokens: None,
reasoning_tokens: Some(1),
}),
stop_reason: ModelStopReason::EndTurn,
reasoning: Some(ReasoningPayload {
text: "thinking".into(),
signature: Some("sig".into()),
}),
},
};
let frame = context.frame(RoundId(0));
let invocation = InvocationId {
turn_id: context.turn_id(),
round_id: RoundId(0),
};
let request = ModelRequest {
invocation_id: invocation.clone(),
attempt: AttemptNumber(1),
frame,
model: ModelRef::new("external-model"),
tool_surface: ToolSurface::empty(),
generation: GenerationOptions::default(),
cache: causa_kernel::CacheDirective::None,
};
let ctrl = AttemptControl::new(CancellationToken::new(), None);
let output = gateway.invoke(&request, &ctrl).await.unwrap();
let applied = context
.append_model_output(invocation, &output.response, output.stop_reason)
.unwrap();
context.seal();
assert!(context.is_sealed());
assert_eq!(applied.block_ids.len(), 1); assert_eq!(context.version(), ContextVersion(2));
let json = serde_json::to_string(&context.snapshot()).unwrap();
let snapshot: TurnSnapshot = serde_json::from_str(&json).unwrap();
assert_eq!(snapshot.blocks.as_slice().len(), 2);
assert!(matches!(
context.append_input(TextPayload::new("more"), "user"),
Err(ContextError::SealedTurn)
));
}