mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-17 00:06:34 +00:00
fix(dispatch): isolate cross-account replay state
Track connection transcripts by the actual response account and sanitize typed Responses output state before replaying across accounts. Rebuild target auth, Cookie, installation metadata, and turn state boundaries, fail closed on malformed turn metadata, and persist only target-account state after replay. Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -131,8 +131,10 @@ struct ResponsesWebSocketSession {
|
||||
connection_id: String,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ConnectionTranscriptCollector {
|
||||
account_id: String,
|
||||
request_input: Vec<Value>,
|
||||
continued_from_previous_response: bool,
|
||||
completed_items: Vec<Value>,
|
||||
completed: Option<ConnectionTranscriptFacts>,
|
||||
}
|
||||
@@ -143,6 +145,20 @@ enum ForwardDispatchOutcome {
|
||||
}
|
||||
|
||||
impl ConnectionTranscriptCollector {
|
||||
fn new(
|
||||
account_id: String,
|
||||
request_input: Vec<Value>,
|
||||
continued_from_previous_response: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
account_id,
|
||||
request_input,
|
||||
continued_from_previous_response,
|
||||
completed_items: Vec::new(),
|
||||
completed: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn observe(&mut self, event: &CanonicalResponseEvent) {
|
||||
match event.event_type() {
|
||||
Some("response.output_item.done") => {
|
||||
@@ -166,7 +182,13 @@ impl ConnectionTranscriptCollector {
|
||||
.filter(|output| !output.is_empty())
|
||||
.cloned()
|
||||
.unwrap_or_else(|| self.completed_items.clone());
|
||||
self.completed = Some(ConnectionTranscriptFacts::new(response_id, output));
|
||||
self.completed = Some(ConnectionTranscriptFacts::new(
|
||||
self.account_id.clone(),
|
||||
self.request_input.clone(),
|
||||
self.continued_from_previous_response,
|
||||
response_id,
|
||||
output,
|
||||
));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -346,6 +368,9 @@ async fn forward_dispatch_stream(
|
||||
request_id: &str,
|
||||
) -> ForwardDispatchOutcome {
|
||||
let ResponseDispatchStream {
|
||||
account_id,
|
||||
request_input,
|
||||
continued_from_previous_response,
|
||||
body,
|
||||
mut canonical_events,
|
||||
response_headers,
|
||||
@@ -361,7 +386,11 @@ async fn forward_dispatch_stream(
|
||||
|
||||
let mut body = body;
|
||||
let mut terminal_sent = false;
|
||||
let mut completed = ConnectionTranscriptCollector::default();
|
||||
let mut completed = ConnectionTranscriptCollector::new(
|
||||
account_id,
|
||||
request_input,
|
||||
continued_from_previous_response,
|
||||
);
|
||||
loop {
|
||||
let next = if terminal_sent {
|
||||
ActiveResponseInput::Upstream(body.next().await)
|
||||
|
||||
@@ -3,12 +3,56 @@
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
use crate::{
|
||||
dispatch::controllers::history::{CrossAccountReplay, sanitize_cross_account_input},
|
||||
infra::identity::AccountPseudonymizer,
|
||||
upstream::openai::protocol::responses::CodexResponsesRequest,
|
||||
};
|
||||
|
||||
const TURN_STATE_METADATA_KEY: &str = "x-codex-turn-state";
|
||||
const TURN_METADATA_KEY: &str = "x-codex-turn-metadata";
|
||||
const INSTALLATION_IDENTITY_KEYS: [&str; 3] = [
|
||||
"installation_id",
|
||||
"installationId",
|
||||
"x-codex-installation-id",
|
||||
];
|
||||
const CROSS_ACCOUNT_IDENTITY_KEYS: [&str; 26] = [
|
||||
"authorization",
|
||||
"Authorization",
|
||||
"cookie",
|
||||
"Cookie",
|
||||
"chatgpt-account-id",
|
||||
"chatgpt_account_id",
|
||||
"chatgptAccountId",
|
||||
"account_id",
|
||||
"accountId",
|
||||
"user_id",
|
||||
"userId",
|
||||
"chatgpt_user_id",
|
||||
"chatgptUserId",
|
||||
"access_token",
|
||||
"accessToken",
|
||||
"session_token",
|
||||
"sessionToken",
|
||||
"refresh_token",
|
||||
"refreshToken",
|
||||
"id_token",
|
||||
"idToken",
|
||||
"token",
|
||||
"cookies",
|
||||
"cookie_header",
|
||||
"cookieHeader",
|
||||
"cf_clearance",
|
||||
];
|
||||
const CROSS_ACCOUNT_OPAQUE_STATE_KEYS: [&str; 8] = [
|
||||
"turnState",
|
||||
"turn_state",
|
||||
"x-codex-turn-state",
|
||||
"previous_response_id",
|
||||
"previousResponseId",
|
||||
"response_id",
|
||||
"responseId",
|
||||
"conversation",
|
||||
];
|
||||
|
||||
/// 持久化密钥驱动的账号身份隔离服务。
|
||||
#[derive(Clone)]
|
||||
@@ -32,24 +76,21 @@ pub(in crate::dispatch) struct AccountScopedIdentity {
|
||||
|
||||
/// 单个请求内账号绑定 opaque 状态的 owner。
|
||||
pub(in crate::dispatch) struct AccountIdentityScope {
|
||||
turn_state_account_id: Option<String>,
|
||||
account_bound_state_owner_id: Option<String>,
|
||||
}
|
||||
|
||||
impl AccountIdentityScope {
|
||||
pub(in crate::dispatch) fn new(preferred_account_id: Option<String>) -> Self {
|
||||
Self {
|
||||
turn_state_account_id: preferred_account_id,
|
||||
account_bound_state_owner_id: preferred_account_id,
|
||||
}
|
||||
}
|
||||
|
||||
fn preserves_turn_state(&mut self, account_id: &str, has_turn_state: bool) -> bool {
|
||||
if !has_turn_state {
|
||||
return false;
|
||||
}
|
||||
fn bind_and_is_cross_account(&mut self, account_id: &str) -> bool {
|
||||
let owner = self
|
||||
.turn_state_account_id
|
||||
.account_bound_state_owner_id
|
||||
.get_or_insert_with(|| account_id.to_string());
|
||||
owner == account_id
|
||||
owner != account_id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,15 +141,19 @@ impl AccountIdentityService {
|
||||
scope: &mut AccountIdentityScope,
|
||||
mut request: CodexResponsesRequest,
|
||||
account_id: &str,
|
||||
cross_account_replay: Option<&CrossAccountReplay>,
|
||||
) -> AccountScopedRequest {
|
||||
if let Some(replay) = cross_account_replay {
|
||||
replay.validate_target(account_id);
|
||||
}
|
||||
let cross_account =
|
||||
cross_account_replay.is_some() || scope.bind_and_is_cross_account(account_id);
|
||||
let identity = AccountScopedIdentity {
|
||||
installation_id: self.pseudonymizer.installation_id(account_id),
|
||||
};
|
||||
let client_metadata_turn_state = client_metadata_string(&request, TURN_STATE_METADATA_KEY);
|
||||
let preserve_turn_state = scope.preserves_turn_state(
|
||||
account_id,
|
||||
request.turn_state.is_some() || client_metadata_turn_state.is_some(),
|
||||
);
|
||||
let preserve_turn_state = !cross_account
|
||||
&& (request.turn_state.is_some() || client_metadata_turn_state.is_some());
|
||||
let turn_state = if preserve_turn_state {
|
||||
request.turn_state.clone()
|
||||
} else {
|
||||
@@ -122,9 +167,14 @@ impl AccountIdentityService {
|
||||
let turn_metadata = request
|
||||
.turn_metadata
|
||||
.as_deref()
|
||||
.map(|raw| Self::scope_turn_metadata(raw, &identity));
|
||||
.and_then(|raw| Self::scope_turn_metadata(raw, &identity, cross_account));
|
||||
let client_metadata_turn_metadata = client_metadata_string(&request, TURN_METADATA_KEY)
|
||||
.map(|raw| Self::scope_turn_metadata(&raw, &identity));
|
||||
.and_then(|raw| Self::scope_turn_metadata(&raw, &identity, cross_account));
|
||||
|
||||
if cross_account {
|
||||
sanitize_cross_account_input(&mut request);
|
||||
remove_cross_account_request_state(&mut request);
|
||||
}
|
||||
|
||||
apply_identity_to_request(
|
||||
&mut request,
|
||||
@@ -133,6 +183,7 @@ impl AccountIdentityService {
|
||||
turn_metadata.as_deref(),
|
||||
client_metadata_turn_state.as_deref(),
|
||||
client_metadata_turn_metadata.as_deref(),
|
||||
cross_account,
|
||||
);
|
||||
request.turn_state = turn_state;
|
||||
request.turn_metadata = turn_metadata;
|
||||
@@ -146,31 +197,33 @@ impl AccountIdentityService {
|
||||
}
|
||||
}
|
||||
|
||||
fn scope_turn_metadata(raw: &str, identity: &AccountScopedIdentity) -> String {
|
||||
fn scope_turn_metadata(
|
||||
raw: &str,
|
||||
identity: &AccountScopedIdentity,
|
||||
cross_account: bool,
|
||||
) -> Option<String> {
|
||||
let Ok(Value::Object(mut metadata)) = serde_json::from_str::<Value>(raw) else {
|
||||
return raw.to_string();
|
||||
return (!cross_account).then(|| raw.to_string());
|
||||
};
|
||||
|
||||
let contains_installation_identity = [
|
||||
"installation_id",
|
||||
"installationId",
|
||||
"x-codex-installation-id",
|
||||
]
|
||||
.into_iter()
|
||||
.any(|key| metadata.contains_key(key));
|
||||
if !contains_installation_identity {
|
||||
return raw.to_string();
|
||||
if cross_account {
|
||||
remove_cross_account_metadata_state(&mut metadata);
|
||||
} else if !INSTALLATION_IDENTITY_KEYS
|
||||
.into_iter()
|
||||
.any(|key| metadata.contains_key(key))
|
||||
{
|
||||
return Some(raw.to_string());
|
||||
}
|
||||
|
||||
for key in [
|
||||
"installation_id",
|
||||
"installationId",
|
||||
"x-codex-installation-id",
|
||||
] {
|
||||
for key in INSTALLATION_IDENTITY_KEYS {
|
||||
replace_existing_metadata_field(&mut metadata, key, Some(&identity.installation_id));
|
||||
}
|
||||
|
||||
serde_json::to_string(&metadata).unwrap_or_else(|_| raw.to_string())
|
||||
match serde_json::to_string(&metadata) {
|
||||
Ok(metadata) => Some(metadata),
|
||||
Err(_) if cross_account => None,
|
||||
Err(_) => Some(raw.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn pseudonym(&self, domain: &str, account_id: Option<&str>, value: &str) -> String {
|
||||
@@ -194,6 +247,7 @@ fn apply_identity_to_request(
|
||||
turn_metadata: Option<&str>,
|
||||
client_metadata_turn_state: Option<&str>,
|
||||
client_metadata_turn_metadata: Option<&str>,
|
||||
cross_account: bool,
|
||||
) {
|
||||
for (key, value) in [
|
||||
(
|
||||
@@ -215,6 +269,7 @@ fn apply_identity_to_request(
|
||||
identity,
|
||||
client_metadata_turn_state,
|
||||
client_metadata_turn_metadata,
|
||||
cross_account,
|
||||
);
|
||||
request.set_client_metadata(client_metadata);
|
||||
}
|
||||
@@ -224,12 +279,17 @@ fn scoped_client_metadata(
|
||||
identity: &AccountScopedIdentity,
|
||||
turn_state: Option<&str>,
|
||||
turn_metadata: Option<&str>,
|
||||
cross_account: bool,
|
||||
) -> Option<Value> {
|
||||
let client_metadata = client_metadata?;
|
||||
let Value::Object(mut metadata) = client_metadata.clone() else {
|
||||
return Some(client_metadata.clone());
|
||||
return (!cross_account).then(|| client_metadata.clone());
|
||||
};
|
||||
|
||||
if cross_account {
|
||||
remove_cross_account_metadata_state(&mut metadata);
|
||||
}
|
||||
|
||||
for (key, value) in [
|
||||
(
|
||||
"x-codex-installation-id",
|
||||
@@ -254,6 +314,28 @@ fn scoped_client_metadata(
|
||||
(!metadata.is_empty()).then_some(Value::Object(metadata))
|
||||
}
|
||||
|
||||
fn remove_cross_account_request_state(request: &mut CodexResponsesRequest) {
|
||||
for key in CROSS_ACCOUNT_IDENTITY_KEYS
|
||||
.into_iter()
|
||||
.chain(CROSS_ACCOUNT_OPAQUE_STATE_KEYS)
|
||||
{
|
||||
request.replace_existing_identity_field(key, None);
|
||||
}
|
||||
for key in ["turn_metadata", TURN_METADATA_KEY] {
|
||||
request.replace_existing_identity_field(key, None);
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_cross_account_metadata_state(metadata: &mut Map<String, Value>) {
|
||||
for key in CROSS_ACCOUNT_IDENTITY_KEYS
|
||||
.into_iter()
|
||||
.chain(CROSS_ACCOUNT_OPAQUE_STATE_KEYS)
|
||||
.chain(["turnMetadata", "turn_metadata", TURN_METADATA_KEY])
|
||||
{
|
||||
metadata.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_existing_metadata_field(
|
||||
metadata: &mut Map<String, Value>,
|
||||
key: &str,
|
||||
|
||||
@@ -16,7 +16,10 @@ use crate::{
|
||||
},
|
||||
upstream::openai::{
|
||||
failure::{UpstreamFailureFacts, UpstreamFailureKind},
|
||||
protocol::responses::{CodexResponsesRequest, PreviousResponseScope, StreamCommitPolicy},
|
||||
protocol::responses::{
|
||||
CodexResponsesRequest, LocalReplayItem, LocalReplayTranscript, PreviousResponseScope,
|
||||
StreamCommitPolicy,
|
||||
},
|
||||
transport::CodexBackendTransport,
|
||||
transport::websocket::PreviousResponseUnavailableReason,
|
||||
},
|
||||
@@ -38,7 +41,8 @@ enum HistorySource {
|
||||
pub(super) struct HistoryState {
|
||||
source: HistorySource,
|
||||
managed_entry: Option<SessionAffinityEntry>,
|
||||
local_replay_input: Option<Arc<Vec<Value>>>,
|
||||
local_replay_transcript: Option<Arc<LocalReplayTranscript>>,
|
||||
local_replay_account_id: Option<String>,
|
||||
external_attempted_account_id: Option<String>,
|
||||
replay_owner_account: bool,
|
||||
}
|
||||
@@ -46,7 +50,8 @@ pub(super) struct HistoryState {
|
||||
#[derive(Default)]
|
||||
pub(in crate::dispatch) struct ConnectionReplaySnapshot {
|
||||
last_response_id: Option<String>,
|
||||
input: Option<Arc<Vec<Value>>>,
|
||||
account_id: Option<String>,
|
||||
transcript: Option<Arc<LocalReplayTranscript>>,
|
||||
}
|
||||
|
||||
pub(in crate::dispatch) struct ConnectionReplayPlan {
|
||||
@@ -54,16 +59,57 @@ pub(in crate::dispatch) struct ConnectionReplayPlan {
|
||||
}
|
||||
|
||||
enum ConnectionReplayUpdate {
|
||||
Replace(Vec<Value>),
|
||||
Append {
|
||||
history: Arc<Vec<Value>>,
|
||||
input: Vec<Value>,
|
||||
},
|
||||
Replace,
|
||||
Append { history: Arc<LocalReplayTranscript> },
|
||||
Unavailable,
|
||||
}
|
||||
|
||||
/// 已确认来源和目标账号不同的完整 transcript 重放边界。
|
||||
pub(in crate::dispatch) struct CrossAccountReplay {
|
||||
source_account_id: String,
|
||||
target_account_id: String,
|
||||
}
|
||||
|
||||
impl CrossAccountReplay {
|
||||
fn between(source_account_id: &str, target_account_id: &str) -> Option<Self> {
|
||||
(source_account_id != target_account_id).then(|| Self {
|
||||
source_account_id: source_account_id.to_string(),
|
||||
target_account_id: target_account_id.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(in crate::dispatch) fn validate_target(&self, account_id: &str) {
|
||||
debug_assert_ne!(self.source_account_id, self.target_account_id);
|
||||
debug_assert_eq!(self.target_account_id, account_id);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct PreparedHistoryRequest {
|
||||
pub(super) request: CodexResponsesRequest,
|
||||
pub(super) cross_account_replay: Option<CrossAccountReplay>,
|
||||
}
|
||||
|
||||
pub(in crate::dispatch) struct HistoryController;
|
||||
|
||||
pub(in crate::dispatch) fn sanitize_cross_account_input(request: &mut CodexResponsesRequest) {
|
||||
if !request.input().iter().any(is_known_response_output_item) {
|
||||
return;
|
||||
}
|
||||
let input = request
|
||||
.input()
|
||||
.iter()
|
||||
.cloned()
|
||||
.filter_map(|item| {
|
||||
if is_known_response_output_item(&item) {
|
||||
sanitize_cross_account_output(item)
|
||||
} else {
|
||||
Some(item)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
request.set_input(input);
|
||||
}
|
||||
|
||||
impl HistoryController {
|
||||
pub(in crate::dispatch) fn new_connection_replay_snapshot() -> ConnectionReplaySnapshot {
|
||||
ConnectionReplaySnapshot::default()
|
||||
@@ -73,28 +119,29 @@ impl HistoryController {
|
||||
snapshot: &ConnectionReplaySnapshot,
|
||||
request: &mut CodexResponsesRequest,
|
||||
) -> ConnectionReplayPlan {
|
||||
let turn_input = request.input().to_vec();
|
||||
let update = match request.previous_response_id() {
|
||||
None => {
|
||||
request.local_replay_input = None;
|
||||
ConnectionReplayUpdate::Replace(turn_input)
|
||||
clear_local_replay(request);
|
||||
ConnectionReplayUpdate::Replace
|
||||
}
|
||||
Some(previous_response_id)
|
||||
if snapshot.last_response_id.as_deref() == Some(previous_response_id) =>
|
||||
{
|
||||
if let Some(history) = snapshot.input.as_ref() {
|
||||
request.local_replay_input = Some(Arc::clone(history));
|
||||
if let (Some(account_id), Some(history)) =
|
||||
(snapshot.account_id.as_ref(), snapshot.transcript.as_ref())
|
||||
{
|
||||
request.local_replay_transcript = Some(Arc::clone(history));
|
||||
request.local_replay_account_id = Some(account_id.clone());
|
||||
ConnectionReplayUpdate::Append {
|
||||
history: Arc::clone(history),
|
||||
input: turn_input,
|
||||
}
|
||||
} else {
|
||||
request.local_replay_input = None;
|
||||
clear_local_replay(request);
|
||||
ConnectionReplayUpdate::Unavailable
|
||||
}
|
||||
}
|
||||
Some(_) => {
|
||||
request.local_replay_input = None;
|
||||
clear_local_replay(request);
|
||||
ConnectionReplayUpdate::Unavailable
|
||||
}
|
||||
};
|
||||
@@ -107,18 +154,38 @@ impl HistoryController {
|
||||
plan: ConnectionReplayPlan,
|
||||
response_id: String,
|
||||
output: Vec<Value>,
|
||||
account_id: String,
|
||||
request_input: Vec<Value>,
|
||||
continued_from_previous_response: bool,
|
||||
) {
|
||||
snapshot.last_response_id = Some(response_id);
|
||||
snapshot.account_id = Some(account_id.clone());
|
||||
match plan.update {
|
||||
ConnectionReplayUpdate::Replace(input) => {
|
||||
snapshot.input = Some(Arc::new(sanitized_transcript(input, output)));
|
||||
ConnectionReplayUpdate::Replace => {
|
||||
snapshot.transcript = Some(Arc::new(transcript_for_turn(
|
||||
request_input,
|
||||
output,
|
||||
&account_id,
|
||||
)));
|
||||
}
|
||||
ConnectionReplayUpdate::Append { mut history, input } => {
|
||||
let mut turn = sanitized_transcript(input, output);
|
||||
Arc::make_mut(&mut history).append(&mut turn);
|
||||
snapshot.input = Some(history);
|
||||
ConnectionReplayUpdate::Append { mut history } => {
|
||||
if continued_from_previous_response {
|
||||
append_turn(
|
||||
Arc::make_mut(&mut history),
|
||||
request_input,
|
||||
output,
|
||||
&account_id,
|
||||
);
|
||||
snapshot.transcript = Some(history);
|
||||
} else {
|
||||
snapshot.transcript = Some(Arc::new(transcript_for_turn(
|
||||
request_input,
|
||||
output,
|
||||
&account_id,
|
||||
)));
|
||||
}
|
||||
}
|
||||
ConnectionReplayUpdate::Unavailable => snapshot.input = None,
|
||||
ConnectionReplayUpdate::Unavailable => snapshot.transcript = None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,51 +212,67 @@ impl HistoryController {
|
||||
None
|
||||
}
|
||||
};
|
||||
let local_replay_input = request.local_replay_input.clone();
|
||||
let source = match (&managed_entry, &local_replay_input) {
|
||||
let (local_replay_transcript, local_replay_account_id) = match (
|
||||
request.local_replay_transcript.clone(),
|
||||
request.local_replay_account_id.clone(),
|
||||
) {
|
||||
(Some(transcript), Some(account_id)) => (Some(transcript), Some(account_id)),
|
||||
(None, None) => (None, None),
|
||||
_ => {
|
||||
tracing::warn!(
|
||||
previous_response_id,
|
||||
"Ignoring incomplete connection-local replay provenance"
|
||||
);
|
||||
(None, None)
|
||||
}
|
||||
};
|
||||
let has_complete_local_replay = local_replay_transcript.is_some();
|
||||
let source = match (&managed_entry, has_complete_local_replay) {
|
||||
(Some(_), _) => HistorySource::Managed,
|
||||
(None, Some(_)) => HistorySource::ConnectionLocal,
|
||||
(None, None) => HistorySource::ExternalUnknown,
|
||||
(None, true) => HistorySource::ConnectionLocal,
|
||||
(None, false) => HistorySource::ExternalUnknown,
|
||||
};
|
||||
HistoryState {
|
||||
source,
|
||||
managed_entry,
|
||||
local_replay_input,
|
||||
local_replay_transcript,
|
||||
local_replay_account_id,
|
||||
external_attempted_account_id: None,
|
||||
replay_owner_account: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn preferred_account_id(state: &HistoryState) -> Option<&str> {
|
||||
state
|
||||
.managed_entry
|
||||
.as_ref()
|
||||
.map(|entry| entry.account_id.as_str())
|
||||
state.local_replay_account_id.as_deref().or_else(|| {
|
||||
state
|
||||
.managed_entry
|
||||
.as_ref()
|
||||
.map(|entry| entry.account_id.as_str())
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn prepare_attempt(
|
||||
state: &mut HistoryState,
|
||||
request: &CodexResponsesRequest,
|
||||
account_id: &str,
|
||||
) -> Result<CodexResponsesRequest, String> {
|
||||
) -> Result<PreparedHistoryRequest, String> {
|
||||
let prepared = match state.source {
|
||||
HistorySource::None => Some(request.clone()),
|
||||
HistorySource::None => Some(PreparedHistoryRequest::plain(request.clone())),
|
||||
HistorySource::ExternalUnknown => {
|
||||
match state.external_attempted_account_id.as_deref() {
|
||||
None => {
|
||||
state.external_attempted_account_id = Some(account_id.to_string());
|
||||
Some(external_history_request(request))
|
||||
}
|
||||
Some(first_account_id) if first_account_id == account_id => {
|
||||
Some(external_history_request(request))
|
||||
Some(PreparedHistoryRequest::plain(external_history_request(
|
||||
request,
|
||||
)))
|
||||
}
|
||||
Some(first_account_id) if first_account_id == account_id => Some(
|
||||
PreparedHistoryRequest::plain(external_history_request(request)),
|
||||
),
|
||||
Some(_) => None,
|
||||
}
|
||||
}
|
||||
HistorySource::ConnectionLocal => state
|
||||
.local_replay_input
|
||||
.as_deref()
|
||||
.map(|replay_input| full_replay_request(request, replay_input)),
|
||||
HistorySource::ConnectionLocal => full_replay_from_state(state, request, account_id),
|
||||
HistorySource::Managed => prepare_managed_attempt(state, request, account_id),
|
||||
};
|
||||
prepared.ok_or_else(|| CROSS_ACCOUNT_HISTORY_UNAVAILABLE_MESSAGE.to_string())
|
||||
@@ -213,7 +296,7 @@ impl HistoryController {
|
||||
.is_some_and(|account_id| Self::can_retry_same_account(state, account_id)),
|
||||
can_retry_next_candidate: match state.source {
|
||||
HistorySource::None | HistorySource::ConnectionLocal => true,
|
||||
HistorySource::Managed => state.local_replay_input.is_some(),
|
||||
HistorySource::Managed => state.local_replay_transcript.is_some(),
|
||||
HistorySource::ExternalUnknown => false,
|
||||
},
|
||||
}
|
||||
@@ -294,7 +377,7 @@ impl HistoryController {
|
||||
return false;
|
||||
};
|
||||
entry.account_id == account_id
|
||||
&& state.local_replay_input.is_some()
|
||||
&& state.local_replay_transcript.is_some()
|
||||
&& !state.replay_owner_account
|
||||
}
|
||||
}
|
||||
@@ -303,10 +386,10 @@ fn prepare_managed_attempt(
|
||||
state: &HistoryState,
|
||||
request: &CodexResponsesRequest,
|
||||
account_id: &str,
|
||||
) -> Option<CodexResponsesRequest> {
|
||||
) -> Option<PreparedHistoryRequest> {
|
||||
let entry = state.managed_entry.as_ref()?;
|
||||
if entry.account_id != account_id || state.replay_owner_account {
|
||||
return replay_for_conversation(state, request, &entry.conversation_id);
|
||||
return replay_for_conversation(state, request, account_id, &entry.conversation_id);
|
||||
}
|
||||
|
||||
match entry.continuation_scope {
|
||||
@@ -320,10 +403,10 @@ fn prepare_managed_attempt(
|
||||
if prepared.turn_state.as_deref().is_none_or(str::is_empty) {
|
||||
prepared.turn_state.clone_from(&entry.turn_state);
|
||||
}
|
||||
Some(prepared)
|
||||
Some(PreparedHistoryRequest::plain(prepared))
|
||||
}
|
||||
PreviousResponseScope::ReplayRequired => {
|
||||
replay_for_conversation(state, request, &entry.conversation_id)
|
||||
replay_for_conversation(state, request, account_id, &entry.conversation_id)
|
||||
}
|
||||
PreviousResponseScope::Unavailable => None,
|
||||
}
|
||||
@@ -332,21 +415,37 @@ fn prepare_managed_attempt(
|
||||
fn replay_for_conversation(
|
||||
state: &HistoryState,
|
||||
request: &CodexResponsesRequest,
|
||||
account_id: &str,
|
||||
conversation_id: &str,
|
||||
) -> Option<CodexResponsesRequest> {
|
||||
state.local_replay_input.as_deref().map(|replay_input| {
|
||||
let mut prepared = full_replay_request(request, replay_input);
|
||||
prepared.local_conversation_id = Some(conversation_id.to_string());
|
||||
) -> Option<PreparedHistoryRequest> {
|
||||
full_replay_from_state(state, request, account_id).map(|mut prepared| {
|
||||
prepared.request.local_conversation_id = Some(conversation_id.to_string());
|
||||
prepared
|
||||
})
|
||||
}
|
||||
|
||||
fn full_replay_from_state(
|
||||
state: &HistoryState,
|
||||
request: &CodexResponsesRequest,
|
||||
account_id: &str,
|
||||
) -> Option<PreparedHistoryRequest> {
|
||||
let transcript = state.local_replay_transcript.as_deref()?;
|
||||
let source_account_id = state.local_replay_account_id.as_deref()?;
|
||||
Some(full_replay_request(
|
||||
request,
|
||||
transcript,
|
||||
source_account_id,
|
||||
account_id,
|
||||
))
|
||||
}
|
||||
|
||||
impl HistoryState {
|
||||
fn empty() -> Self {
|
||||
Self {
|
||||
source: HistorySource::None,
|
||||
managed_entry: None,
|
||||
local_replay_input: None,
|
||||
local_replay_transcript: None,
|
||||
local_replay_account_id: None,
|
||||
external_attempted_account_id: None,
|
||||
replay_owner_account: false,
|
||||
}
|
||||
@@ -363,43 +462,186 @@ fn external_history_request(original: &CodexResponsesRequest) -> CodexResponsesR
|
||||
|
||||
fn full_replay_request(
|
||||
original: &CodexResponsesRequest,
|
||||
replay_input: &[Value],
|
||||
) -> CodexResponsesRequest {
|
||||
transcript: &LocalReplayTranscript,
|
||||
source_account_id: &str,
|
||||
target_account_id: &str,
|
||||
) -> PreparedHistoryRequest {
|
||||
let cross_account_replay = CrossAccountReplay::between(source_account_id, target_account_id);
|
||||
let mut request = original.clone();
|
||||
let mut input = Vec::with_capacity(replay_input.len() + original.input().len());
|
||||
input.extend_from_slice(replay_input);
|
||||
let mut input = replay_input_for_account(transcript, target_account_id);
|
||||
input.reserve(original.input().len());
|
||||
input.extend_from_slice(original.input());
|
||||
sanitize_replay_items(&mut input);
|
||||
request.set_input(input);
|
||||
request.set_previous_response_id(None);
|
||||
request.turn_state = None;
|
||||
request.stream_commit_policy = StreamCommitPolicy::UntilOutputOrTerminal;
|
||||
request
|
||||
PreparedHistoryRequest {
|
||||
request,
|
||||
cross_account_replay,
|
||||
}
|
||||
}
|
||||
|
||||
fn sanitized_transcript(mut input: Vec<Value>, mut output: Vec<Value>) -> Vec<Value> {
|
||||
sanitize_replay_items(&mut input);
|
||||
sanitize_replay_items(&mut output);
|
||||
input.append(&mut output);
|
||||
input
|
||||
}
|
||||
|
||||
fn sanitize_replay_items(values: &mut [Value]) {
|
||||
for value in values {
|
||||
match value {
|
||||
Value::Array(values) => sanitize_replay_items(values),
|
||||
Value::Object(object) => {
|
||||
object.remove("id");
|
||||
object.remove("encrypted_content");
|
||||
for value in object.values_mut() {
|
||||
sanitize_replay_items(std::slice::from_mut(value));
|
||||
}
|
||||
}
|
||||
Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {}
|
||||
impl PreparedHistoryRequest {
|
||||
fn plain(request: CodexResponsesRequest) -> Self {
|
||||
Self {
|
||||
request,
|
||||
cross_account_replay: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_local_replay(request: &mut CodexResponsesRequest) {
|
||||
request.local_replay_transcript = None;
|
||||
request.local_replay_account_id = None;
|
||||
}
|
||||
|
||||
fn transcript_for_turn(
|
||||
input: Vec<Value>,
|
||||
output: Vec<Value>,
|
||||
account_id: &str,
|
||||
) -> LocalReplayTranscript {
|
||||
let mut transcript = LocalReplayTranscript::default();
|
||||
append_client_input(&mut transcript, input, account_id);
|
||||
append_account_output(&mut transcript, output, account_id);
|
||||
transcript
|
||||
}
|
||||
|
||||
fn append_turn(
|
||||
transcript: &mut LocalReplayTranscript,
|
||||
input: Vec<Value>,
|
||||
output: Vec<Value>,
|
||||
account_id: &str,
|
||||
) {
|
||||
project_transcript_to_account(transcript, account_id);
|
||||
append_client_input(transcript, input, account_id);
|
||||
append_account_output(transcript, output, account_id);
|
||||
}
|
||||
|
||||
fn append_client_input(
|
||||
transcript: &mut LocalReplayTranscript,
|
||||
input: Vec<Value>,
|
||||
account_id: &str,
|
||||
) {
|
||||
transcript.items.extend(input.into_iter().map(|item| {
|
||||
if is_known_response_output_item(&item) {
|
||||
if has_account_bound_output_state(&item) {
|
||||
LocalReplayItem::AccountOutput {
|
||||
account_id: account_id.to_string(),
|
||||
item,
|
||||
}
|
||||
} else {
|
||||
LocalReplayItem::SanitizedOutput(item)
|
||||
}
|
||||
} else {
|
||||
LocalReplayItem::ClientInput(item)
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
fn has_account_bound_output_state(item: &Value) -> bool {
|
||||
item.get("id").is_some() || item.get("encrypted_content").is_some()
|
||||
}
|
||||
|
||||
fn append_account_output(
|
||||
transcript: &mut LocalReplayTranscript,
|
||||
output: Vec<Value>,
|
||||
account_id: &str,
|
||||
) {
|
||||
transcript.items.extend(
|
||||
output
|
||||
.into_iter()
|
||||
.map(|item| LocalReplayItem::AccountOutput {
|
||||
account_id: account_id.to_string(),
|
||||
item,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
fn project_transcript_to_account(transcript: &mut LocalReplayTranscript, account_id: &str) {
|
||||
transcript.items = transcript
|
||||
.items
|
||||
.drain(..)
|
||||
.filter_map(|item| match item {
|
||||
LocalReplayItem::AccountOutput {
|
||||
account_id: owner,
|
||||
item,
|
||||
} if owner != account_id => {
|
||||
sanitize_cross_account_output(item).map(LocalReplayItem::SanitizedOutput)
|
||||
}
|
||||
item => Some(item),
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
||||
fn replay_input_for_account(transcript: &LocalReplayTranscript, account_id: &str) -> Vec<Value> {
|
||||
transcript
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
LocalReplayItem::ClientInput(value) | LocalReplayItem::SanitizedOutput(value) => {
|
||||
Some(value.clone())
|
||||
}
|
||||
LocalReplayItem::AccountOutput {
|
||||
account_id: owner,
|
||||
item,
|
||||
} if owner == account_id => Some(without_output_id(item.clone())),
|
||||
LocalReplayItem::AccountOutput { item, .. } => {
|
||||
sanitize_cross_account_output(item.clone())
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn without_output_id(mut item: Value) -> Value {
|
||||
if let Value::Object(object) = &mut item {
|
||||
object.remove("id");
|
||||
}
|
||||
item
|
||||
}
|
||||
|
||||
fn sanitize_cross_account_output(mut item: Value) -> Option<Value> {
|
||||
if let Value::Object(object) = &mut item {
|
||||
if matches!(
|
||||
object.get("type").and_then(Value::as_str),
|
||||
Some("compaction" | "compaction_summary" | "context_compaction")
|
||||
) {
|
||||
return None;
|
||||
}
|
||||
object.remove("id");
|
||||
object.remove("encrypted_content");
|
||||
}
|
||||
Some(item)
|
||||
}
|
||||
|
||||
fn is_known_response_output_item(item: &Value) -> bool {
|
||||
let item_type = item.get("type").and_then(Value::as_str);
|
||||
if item_type == Some("message") {
|
||||
return item.get("role").and_then(Value::as_str) == Some("assistant");
|
||||
}
|
||||
matches!(
|
||||
item_type,
|
||||
Some(
|
||||
"agent_message"
|
||||
| "reasoning"
|
||||
| "local_shell_call"
|
||||
| "function_call"
|
||||
| "tool_search_call"
|
||||
| "custom_tool_call"
|
||||
| "computer_call"
|
||||
| "web_search_call"
|
||||
| "file_search_call"
|
||||
| "code_interpreter_call"
|
||||
| "image_generation_call"
|
||||
| "mcp_call"
|
||||
| "mcp_list_tools"
|
||||
| "mcp_approval_request"
|
||||
| "compaction"
|
||||
| "compaction_summary"
|
||||
| "context_compaction"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fn is_continuation_busy(facts: &UpstreamFailureFacts) -> bool {
|
||||
matches!(
|
||||
facts.kind,
|
||||
|
||||
@@ -340,18 +340,19 @@ impl ControllerSet {
|
||||
request: &CodexResponsesRequest,
|
||||
context: AttemptRoutePreparationContext<'_>,
|
||||
) -> AttemptRoutePreparation {
|
||||
let request = match HistoryController::prepare_attempt(
|
||||
let prepared = match HistoryController::prepare_attempt(
|
||||
&mut scope.history,
|
||||
request,
|
||||
context.account_id,
|
||||
) {
|
||||
Ok(request) => request,
|
||||
Ok(prepared) => prepared,
|
||||
Err(message) => return AttemptRoutePreparation::Unavailable { message },
|
||||
};
|
||||
AttemptRoutePreparation::Ready(Box::new(context.account_identity.scope_request(
|
||||
&mut scope.identity,
|
||||
request,
|
||||
prepared.request,
|
||||
context.account_id,
|
||||
prepared.cross_account_replay.as_ref(),
|
||||
)))
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,21 @@ impl StreamFinalizer {
|
||||
self.controller_context.started_at
|
||||
}
|
||||
|
||||
pub(in crate::dispatch) fn account_id(&self) -> &str {
|
||||
&self.controller_context.account_id
|
||||
}
|
||||
|
||||
pub(in crate::dispatch) fn request_input(&self) -> &[serde_json::Value] {
|
||||
self.controller_context.request.input()
|
||||
}
|
||||
|
||||
pub(in crate::dispatch) fn continued_from_previous_response(&self) -> bool {
|
||||
self.controller_context
|
||||
.request
|
||||
.previous_response_id()
|
||||
.is_some()
|
||||
}
|
||||
|
||||
pub(in crate::dispatch) async fn finalize(
|
||||
self,
|
||||
sender: &mpsc::Sender<Result<Bytes, ResponseDispatchStreamError>>,
|
||||
|
||||
@@ -79,6 +79,9 @@ pub type ResponseBodyStream =
|
||||
|
||||
/// Responses live SSE 调度结果。
|
||||
pub struct ResponseDispatchStream {
|
||||
pub(crate) account_id: String,
|
||||
pub(crate) request_input: Vec<Value>,
|
||||
pub(crate) continued_from_previous_response: bool,
|
||||
pub body: ResponseBodyStream,
|
||||
pub(crate) canonical_events: tokio::sync::mpsc::UnboundedReceiver<Vec<CanonicalResponseEvent>>,
|
||||
pub response_headers: Vec<(String, String)>,
|
||||
@@ -102,13 +105,25 @@ pub(crate) struct ConnectionReplayPlan {
|
||||
|
||||
/// API 从 canonical 响应事件采集的原始 transcript 事实。
|
||||
pub(crate) struct ConnectionTranscriptFacts {
|
||||
account_id: String,
|
||||
request_input: Vec<Value>,
|
||||
continued_from_previous_response: bool,
|
||||
response_id: String,
|
||||
output: Vec<Value>,
|
||||
}
|
||||
|
||||
impl ConnectionTranscriptFacts {
|
||||
pub(crate) fn new(response_id: String, output: Vec<Value>) -> Self {
|
||||
pub(crate) fn new(
|
||||
account_id: String,
|
||||
request_input: Vec<Value>,
|
||||
continued_from_previous_response: bool,
|
||||
response_id: String,
|
||||
output: Vec<Value>,
|
||||
) -> Self {
|
||||
Self {
|
||||
account_id,
|
||||
request_input,
|
||||
continued_from_previous_response,
|
||||
response_id,
|
||||
output,
|
||||
}
|
||||
@@ -168,6 +183,9 @@ impl ResponseDispatchService {
|
||||
plan.inner,
|
||||
transcript.response_id,
|
||||
transcript.output,
|
||||
transcript.account_id,
|
||||
transcript.request_input,
|
||||
transcript.continued_from_previous_response,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,9 @@ pub(in crate::dispatch) fn spawn_live_response_stream(
|
||||
body: CodexBackendSseStream,
|
||||
shutdown: CancellationToken,
|
||||
) -> ResponseDispatchStream {
|
||||
let account_id = finalizer.account_id().to_string();
|
||||
let request_input = finalizer.request_input().to_vec();
|
||||
let continued_from_previous_response = finalizer.continued_from_previous_response();
|
||||
let response_headers = finalizer.response_headers().to_vec();
|
||||
let started_at = finalizer.started_at();
|
||||
let (sender, receiver) = mpsc::channel(8);
|
||||
@@ -81,6 +84,9 @@ pub(in crate::dispatch) fn spawn_live_response_stream(
|
||||
});
|
||||
|
||||
ResponseDispatchStream {
|
||||
account_id,
|
||||
request_input,
|
||||
continued_from_previous_response,
|
||||
body: Box::pin(MpscResponseBodyStream {
|
||||
receiver,
|
||||
cancel: Some(cancel_sender),
|
||||
|
||||
@@ -70,8 +70,12 @@ pub struct CodexResponsesRequest {
|
||||
pub parent_thread_id: Option<String>,
|
||||
/// 已知 previous response 的持久化范围,仅用于本地 transport 校验。
|
||||
pub previous_response_scope: Option<PreviousResponseScope>,
|
||||
/// 当前下游 WebSocket 持有的完整历史前缀,仅用于连接内换号恢复。
|
||||
pub local_replay_input: Option<Arc<Vec<Value>>>,
|
||||
/// 当前下游 WebSocket 持有的带来源类型历史,仅用于连接内换号恢复。
|
||||
#[doc(hidden)]
|
||||
pub local_replay_transcript: Option<Arc<LocalReplayTranscript>>,
|
||||
/// `local_replay_transcript` 最后一次成功响应的账号。
|
||||
#[doc(hidden)]
|
||||
pub local_replay_account_id: Option<String>,
|
||||
/// 当前下游连接能否在本轮完成后形成完整 canonical replay snapshot。
|
||||
pub local_replay_available: bool,
|
||||
/// 流式响应在交给下游前的本地提交策略。
|
||||
@@ -96,6 +100,41 @@ pub enum StreamCommitPolicy {
|
||||
UntilOutputOrTerminal,
|
||||
}
|
||||
|
||||
/// 连接内重放 transcript;输入与上游输出在类型层面保持分离。
|
||||
#[doc(hidden)]
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct LocalReplayTranscript {
|
||||
pub(crate) items: Vec<LocalReplayItem>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) enum LocalReplayItem {
|
||||
ClientInput(Value),
|
||||
SanitizedOutput(Value),
|
||||
AccountOutput { account_id: String, item: Value },
|
||||
}
|
||||
|
||||
impl LocalReplayTranscript {
|
||||
/// 构造仅含客户端输入的 transcript。
|
||||
#[doc(hidden)]
|
||||
pub fn from_client_input(input: Vec<Value>) -> Self {
|
||||
Self {
|
||||
items: input
|
||||
.into_iter()
|
||||
.map(LocalReplayItem::ClientInput)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn values(&self) -> impl DoubleEndedIterator<Item = &Value> {
|
||||
self.items.iter().map(|item| match item {
|
||||
LocalReplayItem::ClientInput(value)
|
||||
| LocalReplayItem::SanitizedOutput(value)
|
||||
| LocalReplayItem::AccountOutput { item: value, .. } => value,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Responses 请求携带的 Codex 运行语义。
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct CodexRequestSemantics {
|
||||
@@ -633,7 +672,8 @@ impl CodexResponsesRequest {
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
previous_response_scope: None,
|
||||
local_replay_input: None,
|
||||
local_replay_transcript: None,
|
||||
local_replay_account_id: None,
|
||||
local_replay_available: false,
|
||||
stream_commit_policy: StreamCommitPolicy::FirstForwardableEvent,
|
||||
}
|
||||
@@ -845,10 +885,10 @@ impl CodexResponsesRequest {
|
||||
}
|
||||
|
||||
fn latest_multi_agent_mode(&self) -> Option<&str> {
|
||||
latest_multi_agent_mode(self.input()).or_else(|| {
|
||||
self.local_replay_input
|
||||
latest_multi_agent_mode(self.input().iter()).or_else(|| {
|
||||
self.local_replay_transcript
|
||||
.as_deref()
|
||||
.and_then(|input| latest_multi_agent_mode(input.as_slice()))
|
||||
.and_then(|transcript| latest_multi_agent_mode(transcript.values()))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -895,8 +935,10 @@ fn non_empty_owned_string(value: &str) -> Option<String> {
|
||||
(!value.is_empty()).then(|| value.to_string())
|
||||
}
|
||||
|
||||
fn latest_multi_agent_mode(input: &[Value]) -> Option<&str> {
|
||||
input.iter().rev().find_map(|item| {
|
||||
fn latest_multi_agent_mode<'a>(
|
||||
input: impl DoubleEndedIterator<Item = &'a Value>,
|
||||
) -> Option<&'a str> {
|
||||
input.rev().find_map(|item| {
|
||||
if item.get("role").and_then(Value::as_str) != Some("developer") {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use std::{fs, net::SocketAddr, path::Path, time::Duration};
|
||||
|
||||
use axum::Router;
|
||||
use codex_proxy_rs::fleet::account::AccountStatus;
|
||||
use chrono::Utc;
|
||||
use codex_proxy_rs::fleet::{account::AccountStatus, cookies::PgCookieStore};
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use serde_json::{Value, json};
|
||||
use tokio::{net::TcpListener, task::JoinHandle, time::timeout};
|
||||
@@ -16,6 +17,7 @@ use tokio_tungstenite::{
|
||||
|
||||
use crate::dispatch::service::{
|
||||
accept_async as accept_upstream_websocket, accept_websocket_with_authorization,
|
||||
accept_websocket_with_request_headers,
|
||||
};
|
||||
|
||||
struct TestServer {
|
||||
@@ -43,7 +45,7 @@ fn responses_websocket_replay_policy_should_be_owned_by_history_controller() {
|
||||
for forbidden in [
|
||||
"ConnectionReplayState",
|
||||
"PendingReplayUpdate",
|
||||
"sanitize_replay_items",
|
||||
"sanitize_cross_account_output",
|
||||
".previous_response_id()",
|
||||
"dispatch::controllers",
|
||||
] {
|
||||
@@ -57,7 +59,10 @@ fn responses_websocket_replay_policy_should_be_owned_by_history_controller() {
|
||||
"ConnectionReplayUpdate::Replace",
|
||||
"ConnectionReplayUpdate::Append",
|
||||
"ConnectionReplayUpdate::Unavailable",
|
||||
"sanitize_replay_items",
|
||||
"struct CrossAccountReplay",
|
||||
"LocalReplayItem::ClientInput",
|
||||
"sanitize_cross_account_output",
|
||||
"project_transcript_to_account",
|
||||
"snapshot.last_response_id.as_deref() == Some(previous_response_id)",
|
||||
] {
|
||||
assert!(
|
||||
@@ -77,7 +82,8 @@ fn responses_websocket_replay_policy_should_be_owned_by_history_controller() {
|
||||
"ResponseDispatchService must expose narrow replay boundary {boundary}"
|
||||
);
|
||||
}
|
||||
assert!(!protocol.contains("sanitize_replay_items"));
|
||||
assert!(protocol.contains("enum LocalReplayItem"));
|
||||
assert!(!protocol.contains("sanitize_cross_account_output"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -355,6 +361,10 @@ async fn responses_websocket_should_replay_connection_history_after_previous_res
|
||||
let mut first_payload: Value = serde_json::from_str(&response_create_payload("first")).unwrap();
|
||||
first_payload["input"][0]["id"] = json!("client_input_id");
|
||||
first_payload["input"][0]["encrypted_content"] = json!("client_secret");
|
||||
first_payload["input"][0]["tool_arguments"] = json!({
|
||||
"id": "user_tool_argument_id",
|
||||
"encrypted_content": "user_tool_argument_secret"
|
||||
});
|
||||
websocket
|
||||
.send(Message::Text(first_payload.to_string().into()))
|
||||
.await
|
||||
@@ -380,10 +390,427 @@ async fn responses_websocket_should_replay_connection_history_after_previous_res
|
||||
assert_eq!(replayed["input"][0]["content"], "first");
|
||||
assert_eq!(replayed["input"][1]["role"], "assistant");
|
||||
assert_eq!(replayed["input"][2]["content"], "second");
|
||||
let replayed_json = replayed.to_string();
|
||||
assert!(!replayed_json.contains("client_input_id"));
|
||||
assert!(!replayed_json.contains("client_secret"));
|
||||
assert!(!replayed_json.contains("assistant_output_resp_first"));
|
||||
assert_eq!(replayed["input"][0]["id"], "client_input_id");
|
||||
assert_eq!(replayed["input"][0]["encrypted_content"], "client_secret");
|
||||
assert_eq!(
|
||||
replayed["input"][0]["tool_arguments"]["id"],
|
||||
"user_tool_argument_id"
|
||||
);
|
||||
assert_eq!(
|
||||
replayed["input"][0]["tool_arguments"]["encrypted_content"],
|
||||
"user_tool_argument_secret"
|
||||
);
|
||||
assert!(replayed["input"][1].get("id").is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn responses_websocket_cross_account_replay_should_isolate_account_bound_state() {
|
||||
let upstream_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let upstream_base_url = format!("http://{}", upstream_listener.local_addr().unwrap());
|
||||
let upstream = tokio::spawn(async move {
|
||||
let (primary_stream, _) = upstream_listener.accept().await.unwrap();
|
||||
let (mut primary, primary_headers) =
|
||||
accept_websocket_with_request_headers(primary_stream, "Bearer access-primary").await;
|
||||
let initial_primary = receive_upstream_request(&mut primary).await;
|
||||
send_upstream_response_with_output(
|
||||
&mut primary,
|
||||
"resp_account_a",
|
||||
"answer from A",
|
||||
vec![
|
||||
json!({
|
||||
"type": "message",
|
||||
"id": "msg_account_a",
|
||||
"role": "assistant",
|
||||
"encrypted_content": "encrypted_message_a",
|
||||
"content": [{
|
||||
"type": "output_text",
|
||||
"id": "nested_content_id_a",
|
||||
"text": "answer from A"
|
||||
}],
|
||||
}),
|
||||
json!({
|
||||
"type": "reasoning",
|
||||
"id": "reasoning_account_a",
|
||||
"encrypted_content": "encrypted_reasoning_a",
|
||||
"summary": [{
|
||||
"type": "summary_text",
|
||||
"id": "nested_summary_id_a",
|
||||
"text": "reasoning summary A"
|
||||
}],
|
||||
}),
|
||||
json!({
|
||||
"type": "function_call",
|
||||
"id": "function_account_a",
|
||||
"call_id": "call_account_a",
|
||||
"caller": "client",
|
||||
"name": "lookup",
|
||||
"arguments": "{\"id\":\"tool_argument_id_a\"}",
|
||||
}),
|
||||
json!({
|
||||
"type": "compaction",
|
||||
"id": "compaction_account_a",
|
||||
"encrypted_content": "encrypted_compaction_a",
|
||||
}),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
|
||||
let (secondary_stream, _) = upstream_listener.accept().await.unwrap();
|
||||
let (mut secondary, secondary_headers) =
|
||||
accept_websocket_with_request_headers(secondary_stream, "Bearer access-secondary")
|
||||
.await;
|
||||
let cross_account_replay = receive_upstream_request(&mut secondary).await;
|
||||
send_upstream_response_with_output(
|
||||
&mut secondary,
|
||||
"resp_account_b",
|
||||
"answer from B",
|
||||
vec![
|
||||
json!({
|
||||
"type": "message",
|
||||
"id": "msg_account_b",
|
||||
"role": "assistant",
|
||||
"encrypted_content": "encrypted_message_b",
|
||||
"content": [{"type": "output_text", "text": "answer from B"}],
|
||||
}),
|
||||
json!({
|
||||
"type": "reasoning",
|
||||
"id": "reasoning_account_b",
|
||||
"encrypted_content": "encrypted_reasoning_b",
|
||||
"summary": [{"type": "summary_text", "text": "reasoning summary B"}],
|
||||
}),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
|
||||
let continued_secondary = receive_upstream_request(&mut secondary).await;
|
||||
secondary
|
||||
.send(Message::Text(
|
||||
json!({
|
||||
"type": "response.failed",
|
||||
"response": {
|
||||
"id": "resp_account_b_missing",
|
||||
"status": "failed",
|
||||
"error": {
|
||||
"code": "previous_response_not_found",
|
||||
"message": "Previous response with id resp_account_b was not found",
|
||||
},
|
||||
},
|
||||
})
|
||||
.to_string()
|
||||
.into(),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
secondary.close(None).await.unwrap();
|
||||
|
||||
let (retry_stream, _) = upstream_listener.accept().await.unwrap();
|
||||
let (mut retry, retry_headers) =
|
||||
accept_websocket_with_request_headers(retry_stream, "Bearer access-secondary").await;
|
||||
let same_account_replay = receive_upstream_request(&mut retry).await;
|
||||
send_upstream_response(&mut retry, "resp_account_b_recovered", "recovered on B").await;
|
||||
|
||||
(
|
||||
primary_headers,
|
||||
secondary_headers,
|
||||
retry_headers,
|
||||
initial_primary,
|
||||
cross_account_replay,
|
||||
continued_secondary,
|
||||
same_account_replay,
|
||||
)
|
||||
});
|
||||
let (app, state, api_key, pool, _dir) =
|
||||
crate::dispatch::service::test_app_with_two_accounts_and_state(upstream_base_url).await;
|
||||
let cookie_store = PgCookieStore::new(pool);
|
||||
cookie_store
|
||||
.capture_set_cookie(
|
||||
"acct_primary",
|
||||
"cf_clearance=primary; Domain=.chatgpt.com; Path=/codex",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
cookie_store
|
||||
.capture_set_cookie(
|
||||
"acct_secondary",
|
||||
"cf_clearance=secondary; Domain=.chatgpt.com; Path=/codex",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let server = spawn_app(app).await;
|
||||
let mut websocket = connect_responses_websocket(server.address, &api_key).await;
|
||||
|
||||
let mut first_payload: Value = serde_json::from_str(&response_create_payload("first")).unwrap();
|
||||
first_payload["input"][0]["id"] = json!("client_input_id");
|
||||
first_payload["input"][0]["encrypted_content"] = json!("client_semantic_secret");
|
||||
first_payload["input"][0]["tool_arguments"] = json!({"id": "client_tool_argument_id"});
|
||||
first_payload["input"].as_array_mut().unwrap().push(json!({
|
||||
"type": "reasoning",
|
||||
"id": "client_replayed_reasoning_id_a",
|
||||
"encrypted_content": "client_replayed_reasoning_encrypted_a",
|
||||
"summary": [{
|
||||
"type": "summary_text",
|
||||
"id": "client_replayed_summary_id_a",
|
||||
"text": "client replayed summary A"
|
||||
}]
|
||||
}));
|
||||
first_payload["turnState"] = json!("turn_state_account_a");
|
||||
first_payload["x-codex-installation-id"] = json!("client_installation_a");
|
||||
first_payload["client_metadata"] = json!({
|
||||
"x-codex-installation-id": "client_installation_a",
|
||||
"x-codex-turn-state": "turn_state_account_a"
|
||||
});
|
||||
websocket
|
||||
.send(Message::Text(first_payload.to_string().into()))
|
||||
.await
|
||||
.unwrap();
|
||||
let first = receive_response(&mut websocket).await;
|
||||
assert_eq!(first.last().unwrap()["response"]["id"], "resp_account_a");
|
||||
|
||||
timeout(Duration::from_secs(2), async {
|
||||
loop {
|
||||
if state
|
||||
.services
|
||||
.session_affinity
|
||||
.lookup("resp_account_a", Utc::now())
|
||||
.await
|
||||
.is_some()
|
||||
{
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("first response affinity should be recorded before removal");
|
||||
assert!(
|
||||
state
|
||||
.services
|
||||
.session_affinity
|
||||
.forget("resp_account_a")
|
||||
.await
|
||||
);
|
||||
|
||||
assert!(
|
||||
state
|
||||
.services
|
||||
.account_pool
|
||||
.set_status("acct_primary", AccountStatus::Disabled)
|
||||
.await
|
||||
);
|
||||
let mut second_payload: Value =
|
||||
serde_json::from_str(&response_create_payload("second")).unwrap();
|
||||
second_payload["previous_response_id"] = json!("resp_account_a");
|
||||
second_payload["turnState"] = json!("turn_state_account_a");
|
||||
second_payload["turn_state"] = json!("turn_state_account_a_projection");
|
||||
second_payload["x-codex-turn-state"] = json!("turn_state_account_a_projection");
|
||||
second_payload["turnMetadata"] = json!("{malformed-account-a-metadata");
|
||||
second_payload["authorization"] = json!("Bearer leaked-account-a");
|
||||
second_payload["cookie"] = json!("session=leaked-account-a");
|
||||
second_payload["chatgpt-account-id"] = json!("chatgpt-primary");
|
||||
second_payload["account_id"] = json!("acct_primary");
|
||||
second_payload["conversation"] = json!("conversation_account_a");
|
||||
second_payload["x-codex-installation-id"] = json!("client_installation_a");
|
||||
second_payload["installation_id"] = json!("client_installation_a");
|
||||
second_payload["installationId"] = json!("client_installation_a");
|
||||
second_payload["client_metadata"] = json!({
|
||||
"safe": "preserved",
|
||||
"account_id": "acct_primary",
|
||||
"cookie": "session=leaked-account-a",
|
||||
"x-codex-installation-id": "client_installation_a",
|
||||
"installation_id": "client_installation_a",
|
||||
"installationId": "client_installation_a",
|
||||
"x-codex-turn-state": "turn_state_account_a",
|
||||
"x-codex-turn-metadata": json!({
|
||||
"installation_id": "client_installation_a",
|
||||
"account_id": "acct_primary",
|
||||
"authorization": "Bearer leaked-account-a",
|
||||
"turn_state": "turn_state_account_a",
|
||||
"conversation": "conversation_account_a",
|
||||
"session_id": "session_semantic"
|
||||
}).to_string()
|
||||
});
|
||||
websocket
|
||||
.send(Message::Text(second_payload.to_string().into()))
|
||||
.await
|
||||
.unwrap();
|
||||
let second = receive_response(&mut websocket).await;
|
||||
assert_eq!(second.last().unwrap()["response"]["id"], "resp_account_b");
|
||||
|
||||
let mut third_payload: Value = serde_json::from_str(&response_create_payload("third")).unwrap();
|
||||
third_payload["previous_response_id"] = json!("resp_account_b");
|
||||
websocket
|
||||
.send(Message::Text(third_payload.to_string().into()))
|
||||
.await
|
||||
.unwrap();
|
||||
let third = receive_response(&mut websocket).await;
|
||||
assert_eq!(
|
||||
third.last().unwrap()["response"]["id"],
|
||||
"resp_account_b_recovered"
|
||||
);
|
||||
|
||||
let (
|
||||
primary_headers,
|
||||
secondary_headers,
|
||||
retry_headers,
|
||||
initial_primary,
|
||||
cross_account_replay,
|
||||
continued_secondary,
|
||||
same_account_replay,
|
||||
) = upstream.await.unwrap();
|
||||
assert_eq!(
|
||||
request_header(&primary_headers, "authorization"),
|
||||
Some("Bearer access-primary")
|
||||
);
|
||||
assert_eq!(
|
||||
request_header(&secondary_headers, "authorization"),
|
||||
Some("Bearer access-secondary")
|
||||
);
|
||||
assert_eq!(
|
||||
request_header(&secondary_headers, "chatgpt-account-id"),
|
||||
Some("chatgpt-secondary")
|
||||
);
|
||||
assert_eq!(
|
||||
request_header(&secondary_headers, "cookie"),
|
||||
Some("cf_clearance=secondary")
|
||||
);
|
||||
assert_eq!(
|
||||
request_header(&retry_headers, "authorization"),
|
||||
Some("Bearer access-secondary")
|
||||
);
|
||||
let primary_installation = request_header(&primary_headers, "x-codex-installation-id").unwrap();
|
||||
let secondary_installation =
|
||||
request_header(&secondary_headers, "x-codex-installation-id").unwrap();
|
||||
assert_ne!(primary_installation, secondary_installation);
|
||||
assert_eq!(
|
||||
request_header(&retry_headers, "x-codex-installation-id"),
|
||||
Some(secondary_installation)
|
||||
);
|
||||
assert!(request_header(&secondary_headers, "x-codex-turn-state").is_none());
|
||||
assert!(request_header(&secondary_headers, "x-codex-turn-metadata").is_none());
|
||||
|
||||
assert_eq!(initial_primary["turnState"], "turn_state_account_a");
|
||||
assert!(cross_account_replay.get("previous_response_id").is_none());
|
||||
for key in [
|
||||
"turnState",
|
||||
"turn_state",
|
||||
"x-codex-turn-state",
|
||||
"turnMetadata",
|
||||
"authorization",
|
||||
"cookie",
|
||||
"chatgpt-account-id",
|
||||
"account_id",
|
||||
"conversation",
|
||||
] {
|
||||
assert!(
|
||||
cross_account_replay.get(key).is_none(),
|
||||
"cross-account replay leaked top-level field {key}"
|
||||
);
|
||||
}
|
||||
let metadata = &cross_account_replay["client_metadata"];
|
||||
assert_eq!(metadata["safe"], "preserved");
|
||||
for key in ["account_id", "cookie", "x-codex-turn-state"] {
|
||||
assert!(
|
||||
metadata.get(key).is_none(),
|
||||
"cross-account replay leaked metadata field {key}"
|
||||
);
|
||||
}
|
||||
let scoped_turn_metadata: Value = serde_json::from_str(
|
||||
metadata["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.expect("valid client metadata turnMetadata should be retained"),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
scoped_turn_metadata["installation_id"].as_str(),
|
||||
Some(secondary_installation)
|
||||
);
|
||||
assert_eq!(scoped_turn_metadata["session_id"], "session_semantic");
|
||||
for key in ["account_id", "authorization", "turn_state", "conversation"] {
|
||||
assert!(
|
||||
scoped_turn_metadata.get(key).is_none(),
|
||||
"cross-account replay leaked turnMetadata field {key}"
|
||||
);
|
||||
}
|
||||
for key in [
|
||||
"x-codex-installation-id",
|
||||
"installation_id",
|
||||
"installationId",
|
||||
] {
|
||||
assert_eq!(
|
||||
cross_account_replay[key].as_str(),
|
||||
Some(secondary_installation),
|
||||
"top-level installation projection {key}"
|
||||
);
|
||||
assert_eq!(
|
||||
metadata[key].as_str(),
|
||||
Some(secondary_installation),
|
||||
"metadata installation projection {key}"
|
||||
);
|
||||
}
|
||||
|
||||
let replay_input = cross_account_replay["input"].as_array().unwrap();
|
||||
assert_eq!(replay_input.len(), 6);
|
||||
assert_eq!(replay_input[0]["id"], "client_input_id");
|
||||
assert_eq!(
|
||||
replay_input[0]["encrypted_content"],
|
||||
"client_semantic_secret"
|
||||
);
|
||||
assert_eq!(
|
||||
replay_input[0]["tool_arguments"]["id"],
|
||||
"client_tool_argument_id"
|
||||
);
|
||||
assert!(replay_input[1].get("id").is_none());
|
||||
assert!(replay_input[1].get("encrypted_content").is_none());
|
||||
assert_eq!(
|
||||
replay_input[1]["summary"][0]["id"],
|
||||
"client_replayed_summary_id_a"
|
||||
);
|
||||
assert!(replay_input[2].get("id").is_none());
|
||||
assert!(replay_input[2].get("encrypted_content").is_none());
|
||||
assert_eq!(replay_input[2]["content"][0]["id"], "nested_content_id_a");
|
||||
assert!(replay_input[3].get("id").is_none());
|
||||
assert!(replay_input[3].get("encrypted_content").is_none());
|
||||
assert_eq!(replay_input[3]["summary"][0]["id"], "nested_summary_id_a");
|
||||
assert!(replay_input[4].get("id").is_none());
|
||||
assert_eq!(replay_input[4]["call_id"], "call_account_a");
|
||||
assert_eq!(replay_input[4]["caller"], "client");
|
||||
assert_eq!(
|
||||
replay_input[4]["arguments"],
|
||||
"{\"id\":\"tool_argument_id_a\"}"
|
||||
);
|
||||
assert_eq!(replay_input[5]["content"], "second");
|
||||
let cross_account_json = cross_account_replay.to_string();
|
||||
for forbidden in [
|
||||
"msg_account_a",
|
||||
"reasoning_account_a",
|
||||
"function_account_a",
|
||||
"compaction_account_a",
|
||||
"encrypted_message_a",
|
||||
"encrypted_reasoning_a",
|
||||
"encrypted_compaction_a",
|
||||
"client_replayed_reasoning_id_a",
|
||||
"client_replayed_reasoning_encrypted_a",
|
||||
] {
|
||||
assert!(
|
||||
!cross_account_json.contains(forbidden),
|
||||
"cross-account replay leaked {forbidden}"
|
||||
);
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
continued_secondary["previous_response_id"],
|
||||
"resp_account_b"
|
||||
);
|
||||
assert_eq!(continued_secondary["input"][0]["content"], "third");
|
||||
assert!(same_account_replay.get("previous_response_id").is_none());
|
||||
let same_account_json = same_account_replay.to_string();
|
||||
assert!(!same_account_json.contains("encrypted_message_a"));
|
||||
assert!(!same_account_json.contains("encrypted_reasoning_a"));
|
||||
assert!(!same_account_json.contains("client_replayed_reasoning_encrypted_a"));
|
||||
assert!(same_account_json.contains("encrypted_message_b"));
|
||||
assert!(same_account_json.contains("encrypted_reasoning_b"));
|
||||
assert!(!same_account_json.contains("msg_account_b"));
|
||||
assert!(!same_account_json.contains("reasoning_account_b"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -577,6 +1004,26 @@ async fn send_upstream_response(
|
||||
websocket: &mut WebSocketStream<tokio::net::TcpStream>,
|
||||
response_id: &str,
|
||||
text: &str,
|
||||
) {
|
||||
send_upstream_response_with_output(
|
||||
websocket,
|
||||
response_id,
|
||||
text,
|
||||
vec![json!({
|
||||
"type": "message",
|
||||
"id": format!("assistant_output_{response_id}"),
|
||||
"role": "assistant",
|
||||
"content": [{"type": "output_text", "text": text}],
|
||||
})],
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
async fn send_upstream_response_with_output(
|
||||
websocket: &mut WebSocketStream<tokio::net::TcpStream>,
|
||||
response_id: &str,
|
||||
text: &str,
|
||||
output: Vec<Value>,
|
||||
) {
|
||||
for event in [
|
||||
json!({
|
||||
@@ -593,12 +1040,7 @@ async fn send_upstream_response(
|
||||
"id": response_id,
|
||||
"object": "response",
|
||||
"status": "completed",
|
||||
"output": [{
|
||||
"type": "message",
|
||||
"id": format!("assistant_output_{response_id}"),
|
||||
"role": "assistant",
|
||||
"content": [{"type": "output_text", "text": text}],
|
||||
}],
|
||||
"output": output,
|
||||
"usage": {
|
||||
"input_tokens": 3,
|
||||
"output_tokens": 1,
|
||||
@@ -619,6 +1061,13 @@ async fn receive_upstream_request(websocket: &mut WebSocketStream<tokio::net::Tc
|
||||
serde_json::from_str(&request.into_text().unwrap()).unwrap()
|
||||
}
|
||||
|
||||
fn request_header<'a>(
|
||||
headers: &'a tokio_tungstenite::tungstenite::http::HeaderMap,
|
||||
name: &str,
|
||||
) -> Option<&'a str> {
|
||||
headers.get(name).and_then(|value| value.to_str().ok())
|
||||
}
|
||||
|
||||
async fn receive_response<S>(websocket: &mut WebSocketStream<S>) -> Vec<Value>
|
||||
where
|
||||
S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin,
|
||||
|
||||
@@ -1098,6 +1098,35 @@ pub(crate) async fn accept_websocket_with_authorization(
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub(crate) async fn accept_websocket_with_request_headers(
|
||||
stream: TcpStream,
|
||||
expected_authorization: &'static str,
|
||||
) -> (
|
||||
tokio_tungstenite::WebSocketStream<TcpStream>,
|
||||
tokio_tungstenite::tungstenite::http::HeaderMap,
|
||||
) {
|
||||
let captured_headers = Arc::new(Mutex::new(None));
|
||||
let captured_headers_for_callback = Arc::clone(&captured_headers);
|
||||
let websocket = accept_hdr_async(stream, move |request, _response| {
|
||||
assert_eq!(
|
||||
request
|
||||
.headers()
|
||||
.get("authorization")
|
||||
.and_then(|value| value.to_str().ok()),
|
||||
Some(expected_authorization)
|
||||
);
|
||||
*captured_headers_for_callback.lock().unwrap() = Some(request.headers().clone());
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let headers = captured_headers
|
||||
.lock()
|
||||
.unwrap()
|
||||
.take()
|
||||
.expect("websocket request headers should be captured");
|
||||
(websocket, headers)
|
||||
}
|
||||
|
||||
async fn reject_next_websocket_upgrade(
|
||||
listener: &TcpListener,
|
||||
expected_authorization: &str,
|
||||
|
||||
@@ -2506,7 +2506,24 @@ async fn responses_failover_should_replace_account_identity_and_preserve_session
|
||||
.body(Body::from(
|
||||
json!({
|
||||
"model": "gpt-5.5",
|
||||
"input": [{"role": "user", "content": "Replay without identity pollution"}],
|
||||
"input": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Replay without identity pollution",
|
||||
"id": "client_user_message_id",
|
||||
"tool_arguments": {"id": "client_tool_argument_id"}
|
||||
},
|
||||
{
|
||||
"type": "reasoning",
|
||||
"id": "reasoning_account_primary",
|
||||
"encrypted_content": "encrypted_reasoning_account_primary",
|
||||
"summary": [{
|
||||
"type": "summary_text",
|
||||
"id": "nested_reasoning_summary_id",
|
||||
"text": "semantic summary"
|
||||
}]
|
||||
}
|
||||
],
|
||||
"stream": false,
|
||||
"use_websocket": false,
|
||||
"prompt_cache_key": "cache-client",
|
||||
@@ -2704,7 +2721,29 @@ async fn responses_failover_should_replace_account_identity_and_preserve_session
|
||||
assert!(secondary_metadata.get("x-codex-turn-state").is_none());
|
||||
assert!(secondary.headers.get("x-codex-turn-state").is_none());
|
||||
assert_eq!(secondary_metadata["safe"], "preserved");
|
||||
assert_eq!(secondary_body["input"], primary_body["input"]);
|
||||
assert_eq!(
|
||||
secondary_body["input"][0], primary_body["input"][0],
|
||||
"ordinary client input must remain verbatim"
|
||||
);
|
||||
assert_eq!(
|
||||
secondary_body["input"][0]["tool_arguments"]["id"],
|
||||
"client_tool_argument_id"
|
||||
);
|
||||
assert_eq!(primary_body["input"][1]["id"], "reasoning_account_primary");
|
||||
assert_eq!(
|
||||
primary_body["input"][1]["encrypted_content"],
|
||||
"encrypted_reasoning_account_primary"
|
||||
);
|
||||
assert!(secondary_body["input"][1].get("id").is_none());
|
||||
assert!(
|
||||
secondary_body["input"][1]
|
||||
.get("encrypted_content")
|
||||
.is_none()
|
||||
);
|
||||
assert_eq!(
|
||||
secondary_body["input"][1]["summary"][0]["id"],
|
||||
"nested_reasoning_summary_id"
|
||||
);
|
||||
assert!(secondary_body.get("previous_response_id").is_none());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use codex_proxy_rs::upstream::openai::protocol::responses::LocalReplayTranscript;
|
||||
use serde_json::Value;
|
||||
|
||||
use super::*;
|
||||
@@ -536,9 +537,10 @@ fn responses_request_semantics_should_read_mode_from_local_replay_history() {
|
||||
});
|
||||
let mut request =
|
||||
build_codex_request(body.as_object().unwrap().clone(), &HeaderMap::new(), None);
|
||||
request.local_replay_input = Some(Arc::new(vec![developer_multi_agent_mode(
|
||||
"Proactive multi-agent delegation is active.",
|
||||
)]));
|
||||
request.local_replay_transcript =
|
||||
Some(Arc::new(LocalReplayTranscript::from_client_input(vec![
|
||||
developer_multi_agent_mode("Proactive multi-agent delegation is active."),
|
||||
])));
|
||||
|
||||
assert_eq!(request.semantics().reasoning_preset, Some("ultra"));
|
||||
}
|
||||
|
||||
+12
-5
@@ -370,8 +370,10 @@ Continuation scope:
|
||||
|
||||
完整 transcript 只存在于当前入站 Responses WebSocket:
|
||||
|
||||
- 每次 completed 后追加本轮 input 与 output。
|
||||
- full replay 前递归删除 item `id` 和 `encrypted_content`,保留工具配对所需的 `call_id`。
|
||||
- 每次 completed 后记录实际响应账号,并以 `ClientInput`、`SanitizedOutput`、`AccountOutput(account_id)` 三种来源类型追加本轮 input 与 output。
|
||||
- 普通客户端 input 按值保留;input 中显式携带的已知 Responses output item 按当前来源账号标记。清洗只作用于这些 item 顶层,不递归删除用户消息、工具输出或工具参数中的同名字段。
|
||||
- 同账号 full replay 删除 upstream output 顶层 `id`,保留该账号返回的 `encrypted_content`;跨账号 full replay 同时删除顶层 `id` 和 `encrypted_content`,并丢弃纯 opaque 的 compaction item。
|
||||
- B 成功后,快照会永久清洗旧账号的 `AccountOutput`,随后只把 B 新返回的 output 作为账号绑定状态保存。
|
||||
- transcript 不写 PostgreSQL/Redis,不跨下游连接共享。
|
||||
- 有完整 transcript 时,HistoryController 可以删除 previous ID 并在同账号或后续候选全量重放。
|
||||
- 只有归属而没有 transcript 时,只允许 owner 账号续接。
|
||||
@@ -386,13 +388,18 @@ Continuation scope:
|
||||
| access token、`chatgpt-account-id`、Cookie | 上游账号认证 | 每次从当前账号重建 |
|
||||
| `x-codex-installation-id` 及其 metadata 投影 | 安装/设备指纹 | 由实例 HMAC secret 按账号稳定派生 |
|
||||
| `x-codex-turn-state` | 上游 opaque sticky state | 同账号可复用,换号必须删除 |
|
||||
| `turnMetadata` | 客户端语义与 installation 投影 | 换号时类型化清除账号字段并改写 installation;解析失败直接删除 |
|
||||
| `previous_response_id` | 上游历史句柄 | 只在 owner 账号/socket 使用;full replay 时删除 |
|
||||
| session/thread/conversation/turn/window/parent ID | 客户端会话拓扑 | 原样透传,不按账号改写 |
|
||||
| `conversation` | Responses 服务端会话句柄 | 换号时删除 |
|
||||
| session/thread/`conversation_id`/turn/window/parent ID | 客户端会话拓扑 | 原样透传,不按账号改写 |
|
||||
| `prompt_cache_key`、`x-client-request-id` | 会话缓存与请求关联 | 客户端提供时原样透传 |
|
||||
| output item `id`、`encrypted_content` | 上游输出引用 | full replay 时删除 |
|
||||
| output item 顶层 `id` | 上游输出引用 | full replay 时删除,嵌套同名字段不动 |
|
||||
| output item 顶层 `encrypted_content` | 上游加密状态 | 同账号保留,换号删除 |
|
||||
| tool `call_id` | 工具输入输出配对 | full replay 时保留 |
|
||||
|
||||
`AccountScopedIdentity` 只包含 installation ID;`AccountScopedRequest` 只有在当前账号认证、Cookie、installation ID 和 turn-state 边界处理完成后才能进入 upstream transport。
|
||||
跨账号重放的不变量是:目标账号收到完整的客户端会话语义,但请求中不存在任何由来源账号认证、签发、加密、路由或缓存的账号绑定状态。
|
||||
|
||||
`CrossAccountReplay(source, target)` 是 HistoryController 产生的显式边界;同一请求内发生普通账号 failover 时也执行相同的 Responses input 类型化清洗。`AccountScopedIdentity` 只包含 installation ID。`AccountScopedRequest` 只有在当前账号认证、Cookie、installation ID、turn-state 和 metadata 边界处理完成后才能进入 upstream transport。
|
||||
|
||||
## 数据存储
|
||||
|
||||
|
||||
Reference in New Issue
Block a user