mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
f00f93d8c0
## Why Client-created response items enter history without IDs, so their identity is lost across rollout persistence and resume. IDs should be assigned once at the history-recording boundary, while IDs returned by the server must remain unchanged. The Responses API validates item IDs using type-specific prefixes. Locally generated IDs therefore use the matching prefix plus a hyphenated UUIDv7, keeping them valid while distinguishable from server-generated IDs. Because this changes persisted history and provider request shapes, the behavior is opt-in behind the under-development `item_ids` feature. Compaction triggers remain request controls whose API shape does not accept an ID. ## What changed - Register the disabled-by-default `item_ids` feature and expose it in `config.schema.json`. - Make supported optional `ResponseItem` IDs serializable and expose them in the generated app-server schemas. - When `item_ids` is enabled, assign an ID during conversation-history preparation if an item has no ID. - Generate type-prefixed, hyphenated UUIDv7 IDs using the Responses API item conventions. - Preserve existing server IDs without rewriting them. - Persist assigned IDs in rollouts and include them in subsequent Responses requests. - Remove the unsupported ID field from `CompactionTrigger` and document why it has no ID. - Add integration coverage for enabled ID persistence, preservation of server IDs, and omission of generated IDs while the feature is disabled. `prepare_conversation_items_for_history` is the single response-item ID allocation boundary. ## Test plan - `just test -p codex-features` - `just test -p codex-core response_item_ids_persist_across_resume_and_preserve_server_ids` - `just test -p codex-core non_openai_responses_requests_omit_item_turn_metadata` - `just test -p codex-core resize_all_images_prepares_failures_before_history_insertion` - `just test -p codex-protocol` - `just test -p codex-app-server-protocol` - `just test -p codex-api azure_default_store_attaches_ids_and_headers`
116 lines
3.4 KiB
Rust
116 lines
3.4 KiB
Rust
use crate::auth::SharedAuthProvider;
|
|
use crate::common::CompactionInput;
|
|
use crate::endpoint::session::EndpointSession;
|
|
use crate::error::ApiError;
|
|
use crate::provider::Provider;
|
|
use codex_client::HttpTransport;
|
|
use codex_client::RequestTelemetry;
|
|
use codex_protocol::models::ResponseItem;
|
|
use http::HeaderMap;
|
|
use http::Method;
|
|
use serde::Deserialize;
|
|
use std::sync::Arc;
|
|
use std::sync::OnceLock;
|
|
use std::time::Duration;
|
|
|
|
const X_CODEX_TURN_STATE_HEADER: &str = "x-codex-turn-state";
|
|
|
|
pub struct CompactClient<T: HttpTransport> {
|
|
session: EndpointSession<T>,
|
|
}
|
|
|
|
impl<T: HttpTransport> CompactClient<T> {
|
|
pub fn new(transport: T, provider: Provider, auth: SharedAuthProvider) -> Self {
|
|
Self {
|
|
session: EndpointSession::new(transport, provider, auth),
|
|
}
|
|
}
|
|
|
|
pub fn with_telemetry(self, request: Option<Arc<dyn RequestTelemetry>>) -> Self {
|
|
Self {
|
|
session: self.session.with_request_telemetry(request),
|
|
}
|
|
}
|
|
|
|
fn path() -> &'static str {
|
|
"responses/compact"
|
|
}
|
|
|
|
pub async fn compact(
|
|
&self,
|
|
body: serde_json::Value,
|
|
extra_headers: HeaderMap,
|
|
request_timeout: Duration,
|
|
turn_state: Option<&OnceLock<String>>,
|
|
) -> Result<Vec<ResponseItem>, ApiError> {
|
|
let resp = self
|
|
.session
|
|
.execute_with(
|
|
Method::POST,
|
|
Self::path(),
|
|
extra_headers,
|
|
Some(body),
|
|
|req| {
|
|
req.timeout = Some(request_timeout);
|
|
},
|
|
)
|
|
.await?;
|
|
if let Some(turn_state) = turn_state
|
|
&& let Some(header_value) = resp
|
|
.headers
|
|
.get(X_CODEX_TURN_STATE_HEADER)
|
|
.and_then(|value| value.to_str().ok())
|
|
{
|
|
let _ = turn_state.set(header_value.to_string());
|
|
}
|
|
let parsed: CompactHistoryResponse =
|
|
serde_json::from_slice(&resp.body).map_err(|e| ApiError::Stream(e.to_string()))?;
|
|
Ok(parsed.output)
|
|
}
|
|
|
|
pub async fn compact_input(
|
|
&self,
|
|
input: &CompactionInput<'_>,
|
|
extra_headers: HeaderMap,
|
|
request_timeout: Duration,
|
|
turn_state: Option<&OnceLock<String>>,
|
|
) -> Result<Vec<ResponseItem>, ApiError> {
|
|
let body = serde_json::to_value(input)
|
|
.map_err(|e| ApiError::Stream(format!("failed to encode compaction input: {e}")))?;
|
|
self.compact(body, extra_headers, request_timeout, turn_state)
|
|
.await
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct CompactHistoryResponse {
|
|
output: Vec<ResponseItem>,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use codex_client::Request;
|
|
use codex_client::Response;
|
|
use codex_client::StreamResponse;
|
|
use codex_client::TransportError;
|
|
|
|
#[derive(Clone, Default)]
|
|
struct DummyTransport;
|
|
|
|
impl HttpTransport for DummyTransport {
|
|
async fn execute(&self, _req: Request) -> Result<Response, TransportError> {
|
|
Err(TransportError::Build("execute should not run".to_string()))
|
|
}
|
|
|
|
async fn stream(&self, _req: Request) -> Result<StreamResponse, TransportError> {
|
|
Err(TransportError::Build("stream should not run".to_string()))
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn path_is_responses_compact() {
|
|
assert_eq!(CompactClient::<DummyTransport>::path(), "responses/compact");
|
|
}
|
|
}
|