fix: shell

This commit is contained in:
leokun committed 2026-08-26 20:20:38 +08:00
1 parent df053c3720
commit 5450fc76e2
25 files changed
+1367 -101

No files matched your search

+93 -2
View File
@@ -9,7 +9,11 @@ use tokio_stream::StreamExt;
use tokio_util::sync::CancellationToken;
use crate::{
cursor::{connect::END_STREAM_FLAG, observability::CursorTraceRecorder, CursorSessionRegistry},
cursor::{
connect::{self, END_STREAM_FLAG},
observability::CursorTraceRecorder,
CursorSessionRegistry,
},
Result,
};
@@ -49,7 +53,7 @@ fn local_body_stream(
trace.chunk(&chunk);
if terminal {
guard.complete();
trace.finish(None);
trace.finish(end_stream_error(&chunk));
}
yield Ok::<Bytes, Infallible>(chunk);
if terminal {
@@ -67,6 +71,30 @@ fn is_end_stream_frame(frame: &Bytes) -> bool {
.is_some_and(|flags| flags & END_STREAM_FLAG != 0)
}
fn end_stream_error(frame: &Bytes) -> Option<String> {
connect::decode_frames(frame)
.ok()?
.into_iter()
.find_map(|(flags, payload)| {
if flags & END_STREAM_FLAG == 0 {
return None;
}
let value = serde_json::from_slice::<serde_json::Value>(&payload).ok()?;
let error = value.get("error")?;
let code = error.get("code").and_then(serde_json::Value::as_str);
let message = error
.get("message")
.and_then(serde_json::Value::as_str)
.filter(|message| !message.is_empty());
Some(match (code, message) {
(Some(code), Some(message)) => format!("{code}: {message}"),
(Some(code), None) => code.to_string(),
(None, Some(message)) => message.to_string(),
(None, None) => error.to_string(),
})
})
}
struct LocalRunGuard {
cancellation: CancellationToken,
completed: bool,
@@ -233,4 +261,67 @@ mod tests {
drop(stream);
assert!(!cancellation.is_cancelled());
}
#[test]
fn connect_error_end_stream_exposes_the_trace_error() {
let frame = connect::encode_error_end_stream(&connect::ConnectStreamError {
code: connect::ConnectCode::InvalidArgument,
message: "unsupported runtime action".into(),
details: Vec::new(),
})
.unwrap();
assert_eq!(
end_stream_error(&frame).as_deref(),
Some("invalid_argument: unsupported runtime action")
);
assert_eq!(end_stream_error(&connect::encode_end_stream()), None);
}
#[tokio::test]
async fn connect_error_end_stream_marks_the_local_trace_as_error() {
let store = crate::store::Store::connect("sqlite::memory:")
.await
.unwrap();
store.set_detailed_logging(true).await.unwrap();
let trace = CursorTraceRecorder::begin(
store.clone(),
"error-trace",
Some("conversation"),
"local_byok",
Some("model"),
)
.await
.unwrap();
let (sender, receiver) = mpsc::unbounded_channel();
let cancellation = CancellationToken::new();
sender
.send(
connect::encode_error_end_stream(&connect::ConnectStreamError {
code: connect::ConnectCode::InvalidArgument,
message: "unsupported runtime action".into(),
details: Vec::new(),
})
.unwrap(),
)
.unwrap();
let mut stream = Box::pin(local_body_stream(receiver, cancellation, Some(trace)));
stream.next().await.unwrap().unwrap();
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(1);
let trace = loop {
let trace = store.cursor_trace("error-trace").await.unwrap().unwrap();
if trace.status != "running" {
break trace;
}
assert!(tokio::time::Instant::now() < deadline);
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
};
assert_eq!(trace.status, "error");
assert_eq!(
trace.error_message.as_deref(),
Some("invalid_argument: unsupported runtime action")
);
}
}