| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| use super::ModelOperation; | |
| use serde::{Deserialize, Serialize}; | |
| /// ChatGPT subscription facts. Account/connection identify provenance, not replay permission. | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| #[serde(deny_unknown_fields)] | |
| pub struct CodexRequestProvenance { | |
| pub connection_id: String, | |
| pub account_id: Option<String>, | |
| pub compatibility: Option<CodexCompatibility>, | |
| } | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| #[serde(deny_unknown_fields)] | |
| pub struct CodexCompatibility { | |
| pub comp_hash: String, | |
| /// Unix seconds, matching the canonical account inventory timestamp. | |
| pub fetched_at: u64, | |
| } | |
| /// Call only with ChatGPT provenance on both sides. Recorded observations do not | |
| /// expire with the catalog; a later catalog cannot certify a missing old hash. | |
| /// Native re-compaction may ask the producing model to convert its own old state. | |
| pub fn codex_replay_allowed( | |
| same_model: bool, | |
| hashes_match: Option<bool>, | |
| operation: ModelOperation, | |
| ) -> bool { | |
| (same_model && operation == ModelOperation::CodexCompaction) | |
| || hashes_match.unwrap_or(same_model) | |
| } | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| #[serde(rename_all = "snake_case")] | |
| pub enum CodexPreflightStage { | |
| Previous, | |
| Fallback, | |
| Current, | |
| } | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| #[serde(deny_unknown_fields)] | |
| pub struct CodexContinuation { | |
| pub model: String, | |
| pub model_context_window: Option<u64>, | |
| pub provenance: CodexRequestProvenance, | |
| } | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| #[serde(deny_unknown_fields)] | |
| pub struct CodexPreflight { | |
| pub stage: CodexPreflightStage, | |
| pub target: CodexContinuation, | |
| } | |
| impl CodexPreflight { | |
| pub fn retry_with_target( | |
| &self, | |
| compactor: &str, | |
| failure: &crate::model::error::ModelError, | |
| ) -> bool { | |
| use crate::model::error::{ModelError, ProviderFailureReason}; | |
| self.stage == CodexPreflightStage::Previous | |
| && compactor != self.target.model | |
| && match failure { | |
| ModelError::ContextOverflow { | |
| observed_output: false, | |
| } | |
| | ModelError::Http { .. } => true, | |
| ModelError::Provider(failure) => { | |
| failure.replay_safe() | |
| && !matches!( | |
| failure.reason(), | |
| ProviderFailureReason::QuotaExhausted | |
| | ProviderFailureReason::StreamTruncated | |
| ) | |
| } | |
| _ => false, | |
| } | |
| } | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| use crate::model::error::ModelError; | |
| #[test] | |
| fn retry_with_target_stops_on_request_encoding_rejected_but_preserves_plain_http_415() { | |
| let preflight = CodexPreflight { | |
| stage: CodexPreflightStage::Previous, | |
| target: CodexContinuation { | |
| model: "gpt-next".into(), | |
| model_context_window: None, | |
| provenance: CodexRequestProvenance { | |
| connection_id: "conn-1".into(), | |
| account_id: None, | |
| compatibility: None, | |
| }, | |
| }, | |
| }; | |
| let zstd_415 = ModelError::RequestEncodingRejected { | |
| encoding: "zstd".into(), | |
| status: 415, | |
| message: Some("disable request compression".into()), | |
| }; | |
| assert!( | |
| !preflight.retry_with_target("gpt-prev", &zstd_415), | |
| "rejected Content-Encoding must not trigger a second summary call with the target model" | |
| ); | |
| let plain_415 = ModelError::Http { | |
| status: 415, | |
| message: Some("unsupported media type".into()), | |
| }; | |
| assert!(preflight.retry_with_target("gpt-prev", &plain_415)); | |
| } | |
| } |