Add turn-aware response item injection (#38047)

## What changed

- Add `inject_response_items_for_turn` for callers that inject Responses API
  items immediately before admitting user input. It defers the rollout flush so
  the injected items and user input are persisted together by turn admission.
- Keep `inject_response_items` behavior unchanged by flushing after it delegates
  to the new helper.
- Update the turn-start persistence test to exercise the turn-aware path.

GitOrigin-RevId: 8349b65f937af2b2a95d36b3fd73d862d38e5fd9
This commit is contained in:
Bryan Ashley
2026-08-11 18:14:09 +00:00
committed by copyberry
parent f2a6f2585c
commit d6ca19d99b
2 changed files with 15 additions and 2 deletions
+14 -1
View File
@@ -593,6 +593,20 @@ impl CodexThread {
/// Record raw Responses API items without starting a new turn.
pub async fn inject_response_items(&self, items: Vec<ResponseItem>) -> CodexResult<()> {
self.inject_response_items_for_turn(items).await?;
self.session.flush_rollout().await?;
Ok(())
}
/// Record raw Responses API items immediately before admitting a user turn.
///
/// The caller must submit the associated user input while retaining its
/// thread-operation lock. The subsequent turn persistence includes both
/// these items and the user input, without an independent rollout flush.
pub async fn inject_response_items_for_turn(
&self,
items: Vec<ResponseItem>,
) -> CodexResult<()> {
if items.is_empty() {
return Err(CodexErr::InvalidRequest(
"items must not be empty".to_string(),
@@ -613,7 +627,6 @@ impl CodexThread {
self.session
.inject_no_new_turn(items, Some(turn_context.as_ref()))
.await;
self.session.flush_rollout().await?;
Ok(())
}
@@ -54,7 +54,7 @@ async fn default_turn_start_persists_developer_and_user_input_before_model_reque
.await
.expect("build default thread-store session");
test.codex
.inject_response_items(vec![ResponseItem::Message {
.inject_response_items_for_turn(vec![ResponseItem::Message {
id: None,
role: "developer".to_string(),
content: vec![ContentItem::InputText {