make /compact client handle SSE so we can send keepalives

This commit is contained in:
Sayan Sisodiya
2026-02-09 22:56:01 -08:00
parent 168c359b71
commit 1bb860c88f
4 changed files with 393 additions and 19 deletions
+16 -3
View File
@@ -269,10 +269,23 @@ impl ModelClient {
};
let extra_headers = self.build_subagent_headers();
client
.compact_input(&payload, extra_headers)
// Use the stream compact endpoint if available, otherwise fallback to the unary endpoint.
match client
.compact_stream_input(&payload, extra_headers.clone())
.await
.map_err(map_api_error)
{
Ok(output) => Ok(output),
Err(ApiError::Transport(TransportError::Http { status, .. }))
if status == HttpStatusCode::NOT_FOUND
|| status == HttpStatusCode::METHOD_NOT_ALLOWED =>
{
client
.compact_input(&payload, extra_headers)
.await
.map_err(map_api_error)
}
Err(err) => Err(map_api_error(err)),
}
}
/// Builds memory summaries for each provided normalized trace.
+29
View File
@@ -756,6 +756,14 @@ fn compact_mock() -> (MockBuilder, ResponseMock) {
(mock, response_mock)
}
fn compact_stream_mock() -> (MockBuilder, ResponseMock) {
let response_mock = ResponseMock::new();
let mock = Mock::given(method("POST"))
.and(path_regex(".*/responses/compact/stream$"))
.and(response_mock.clone());
(mock, response_mock)
}
fn models_mock() -> (MockBuilder, ModelsMock) {
let models_mock = ModelsMock::new();
let mock = Mock::given(method("GET"))
@@ -820,6 +828,27 @@ pub async fn mount_compact_json_once(server: &MockServer, body: serde_json::Valu
response_mock
}
pub async fn mount_compact_stream_json_once(
server: &MockServer,
body: serde_json::Value,
) -> ResponseMock {
let (mock, response_mock) = compact_stream_mock();
let event = serde_json::json!({
"type": "compact.completed",
"response": body,
});
let response_body = format!("event: compact.completed\ndata: {event}\n\n");
mock.respond_with(
ResponseTemplate::new(200)
.insert_header("content-type", "text/event-stream")
.set_body_string(response_body),
)
.up_to_n_times(1)
.mount(server)
.await;
response_mock
}
pub async fn mount_models_once(server: &MockServer, body: ModelsResponse) -> ModelsMock {
let (mock, models_mock) = models_mock();
mock.respond_with(
@@ -156,6 +156,58 @@ async fn remote_compact_replaces_history_for_followups() -> Result<()> {
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn remote_compact_uses_stream_endpoint_when_available() -> Result<()> {
skip_if_no_network!(Ok(()));
let harness = TestCodexHarness::with_builder(
test_codex().with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing()),
)
.await?;
let codex = harness.test().codex.clone();
mount_sse_once(
harness.server(),
sse(vec![
responses::ev_assistant_message("m1", "FIRST_REMOTE_REPLY"),
responses::ev_completed("resp-1"),
]),
)
.await;
let compact_stream_mock = responses::mount_compact_stream_json_once(
harness.server(),
serde_json::json!({
"output": [
{
"type": "compaction_summary",
"encrypted_content": "ENCRYPTED_STREAM_COMPACTION_SUMMARY",
}
],
}),
)
.await;
codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "hello remote compact".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
})
.await?;
wait_for_event(&codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await;
codex.submit(Op::Compact).await?;
wait_for_event(&codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await;
let compact_request = compact_stream_mock.single_request();
assert_eq!(compact_request.path(), "/v1/responses/compact/stream");
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn remote_compact_runs_automatically() -> Result<()> {
skip_if_no_network!(Ok(()));