diff --git a/Cargo.lock b/Cargo.lock index 7cf235ee..419220c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -357,6 +357,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b281d307588d634de920874890732659e2e7672f72b5e10e81badc1a8a83621e" dependencies = [ "aws-lc-sys", + "untrusted 0.7.1", "zeroize", ] @@ -1367,6 +1368,7 @@ name = "gateway" version = "0.3.1" dependencies = [ "anyhow", + "async-trait", "axum", "base64", "block2", @@ -1378,6 +1380,7 @@ dependencies = [ "gateway", "gateway-api-discovery", "gateway-api-types", + "gateway-chatgpt-plan", "gateway-config", "gateway-config-ui", "gateway-local", @@ -1445,6 +1448,28 @@ dependencies = [ "time", ] +[[package]] +name = "gateway-chatgpt-plan" +version = "0.3.1" +dependencies = [ + "async-trait", + "aws-lc-rs", + "axum", + "base64", + "build-ceiling", + "gateway-config", + "gateway-protocol", + "reqwest", + "serde", + "serde_json", + "sha2", + "tempfile", + "thiserror 2.0.19", + "tokio", + "tracing", + "url", +] + [[package]] name = "gateway-config" version = "0.3.1" @@ -3641,7 +3666,7 @@ dependencies = [ "cfg-if", "getrandom 0.2.17", "libc", - "untrusted", + "untrusted 0.9.0", "windows-sys 0.52.0", ] @@ -3820,7 +3845,7 @@ dependencies = [ "aws-lc-rs", "ring", "rustls-pki-types", - "untrusted", + "untrusted 0.9.0", ] [[package]] @@ -5094,6 +5119,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + [[package]] name = "untrusted" version = "0.9.0" diff --git a/Cargo.toml b/Cargo.toml index a5e64d1d..e1bd4b0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "3" -members = ["crates/*", "crates/gateway/app", "crates/gateway/config", "crates/gateway/config-ui", "crates/gateway/local", "crates/gateway/logging", "crates/gateway/progress", "crates/gateway/protocol", "crates/gateway/routing", "crates/gateway/web-search", "crates/gateway/stt/api", "crates/gateway/stt/engine", "crates/gateway/stt/backend-whisper", "crates/gateway/stt/whisper-ffi"] +members = ["crates/*", "crates/gateway/app", "crates/gateway/config", "crates/gateway/config-ui", "crates/gateway/local", "crates/gateway/logging", "crates/gateway/progress", "crates/gateway/protocol", "crates/gateway/routing", "crates/gateway/web-search", "crates/gateway/chatgpt-plan", "crates/gateway/stt/api", "crates/gateway/stt/engine", "crates/gateway/stt/backend-whisper", "crates/gateway/stt/whisper-ffi"] # crates/shared-ui is not a Rust crate: it is the TypeScript and CSS package # the config UI consumes, so the crates/* glob skips it. # crates/gateway and crates/gateway/stt are manifestless containers holding @@ -23,6 +23,7 @@ repository = "https://github.com/cppalliance/runvil-gateway" [workspace.dependencies] anyhow = "1" async-trait = "0.1" +aws-lc-rs = { version = "1.18", default-features = false, features = ["aws-lc-sys"] } axum = { version = "0.8", features = ["multipart", "ws"] } base64 = "0.22" block2 = "0.6" @@ -36,6 +37,7 @@ embed-resource = "3" flate2 = "1" futures-util = "0.3" gateway-api-discovery = { path = "crates/gateway-api-discovery" } +gateway-chatgpt-plan = { path = "crates/gateway/chatgpt-plan" } gateway-api-types = { path = "crates/gateway-api-types" } gateway-config = { path = "crates/gateway/config" } gateway-config-ui = { path = "crates/gateway/config-ui" } diff --git a/README.md b/README.md index c4ec49a4..8ba66258 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Runvil Gateway is one process that serves an OpenAI-shaped API for chat, embeddi | `POST /v1/tools/web_search` | A Gateway extension, backed by Brave Search | | `GET /v1/audio/voices` | A Gateway extension, the voice catalog | | `/v1/cache` | A Gateway extension, a local blob cache that downloads and keeps files by sha256 | -| `/config/`, `/admin/*`, `/health` | The Gateway's own settings page, admin surface, and liveness probe | +| `/config/`, `/admin/*`, `/health` | The Gateway's own settings page, admin surface, and liveness probe ([the ChatGPT plan](crates/gateway/README.md#the-chatgpt-plan) covers `/admin/chatgpt/*`, and [a config save names a revision](crates/gateway/README.md#saving-the-configuration)) | Requests are answered by a local model through a managed `llama-server` child, or forwarded to a cloud provider you configured. Chat, embeddings, and reranking work either way. Transcription runs on the Gateway's own speech pipeline, which decodes with whisper.cpp, and speech synthesis forwards to a speech endpoint you configure. diff --git a/crates/gateway-api-discovery/src/lib.rs b/crates/gateway-api-discovery/src/lib.rs index e58afa4d..ad97a67b 100644 --- a/crates/gateway-api-discovery/src/lib.rs +++ b/crates/gateway-api-discovery/src/lib.rs @@ -41,6 +41,7 @@ mod file; mod health; mod lock; mod paths; +mod private; mod shutdown; mod stale; mod sys; @@ -52,10 +53,11 @@ pub use crate::file::{GatewayDiscoveryFile, remove_if_mine}; pub use crate::health::{HealthError, ProbeError, wait_for_health, wait_for_health_cancellable}; pub use crate::lock::{GatewayInstanceLease, LaunchDecision, LaunchLock}; pub use crate::paths::{ - GATEWAY_DISCOVERY_FILE_NAME, GATEWAY_LOG_FILE_NAME, INSTANCE_LOCK_FILE_NAME, LOCK_FILE_NAME, - default_run_dir, gateway_discovery_file_path, gateway_log_path, instance_lock_file_path, - lock_file_path, run_dir, + GATEWAY_DISCOVERY_FILE_NAME, GATEWAY_LOG_FILE_NAME, GATEWAY_PRIVATE_FILE_NAME, + INSTANCE_LOCK_FILE_NAME, LOCK_FILE_NAME, default_run_dir, gateway_discovery_file_path, + gateway_log_path, gateway_private_file_path, instance_lock_file_path, lock_file_path, run_dir, }; +pub use crate::private::{GatewayPrivateFile, remove_private_if_mine}; pub use crate::shutdown::{ShutdownError, request_shutdown, request_shutdown_before}; pub use crate::stale::resolve_cancellable; #[cfg(feature = "test-fixtures")] diff --git a/crates/gateway-api-discovery/src/paths.rs b/crates/gateway-api-discovery/src/paths.rs index f1277685..dc174aee 100644 --- a/crates/gateway-api-discovery/src/paths.rs +++ b/crates/gateway-api-discovery/src/paths.rs @@ -7,6 +7,10 @@ use std::path::{Path, PathBuf}; /// The gateway discovery file's name inside the run directory. pub const GATEWAY_DISCOVERY_FILE_NAME: &str = "gateway.json"; +/// The private proofs file's name inside the run directory. It is not part +/// of the discovery contract. +pub const GATEWAY_PRIVATE_FILE_NAME: &str = "gateway-private.json"; + /// The gateway log's name inside the state directory's `logs` directory. pub const GATEWAY_LOG_FILE_NAME: &str = "gateway.log"; @@ -37,6 +41,12 @@ pub fn gateway_discovery_file_path(run_dir: &Path) -> PathBuf { run_dir.join(GATEWAY_DISCOVERY_FILE_NAME) } +/// The private proofs file inside `run_dir`. +#[must_use] +pub fn gateway_private_file_path(run_dir: &Path) -> PathBuf { + run_dir.join(GATEWAY_PRIVATE_FILE_NAME) +} + /// The launch lock inside `run_dir`. #[must_use] pub fn lock_file_path(run_dir: &Path) -> PathBuf { diff --git a/crates/gateway-api-discovery/src/private-tests.rs b/crates/gateway-api-discovery/src/private-tests.rs new file mode 100644 index 00000000..4b2902ae --- /dev/null +++ b/crates/gateway-api-discovery/src/private-tests.rs @@ -0,0 +1,120 @@ +//! Tests for the private proofs file. + +use super::*; + +fn valid() -> GatewayPrivateFile { + GatewayPrivateFile { + pid: 4242, + epoch: 1_763_000_000, + plan_proof: "plan-proof-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(), + owner_proof: "owner-proof-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".to_owned(), + } +} + +fn discovery(pid: u32, epoch: u64) -> GatewayDiscoveryFile { + GatewayDiscoveryFile { + port: 8081, + api_key: "key".to_owned(), + pid, + epoch, + version: "0.3.1".to_owned(), + started_at: "2026-11-13T12:00:00Z".to_owned(), + } +} + +#[test] +fn it_round_trips_and_is_absent_until_written() { + let dir = tempfile::TempDir::new().expect("tempdir"); + assert_eq!(GatewayPrivateFile::read(dir.path()).expect("reads"), None); + valid().write_to(dir.path()).expect("writes"); + assert_eq!( + GatewayPrivateFile::read(dir.path()).expect("reads"), + Some(valid()) + ); +} + +#[cfg(unix)] +#[test] +fn it_lands_owner_only() { + use std::os::unix::fs::PermissionsExt as _; + + let dir = tempfile::TempDir::new().expect("tempdir"); + valid().write_to(dir.path()).expect("writes"); + let mode = std::fs::metadata(gateway_private_file_path(dir.path())) + .expect("meta") + .permissions() + .mode(); + assert_eq!(mode & 0o777, 0o600); +} + +#[test] +fn proofs_pair_only_with_the_boot_that_minted_them() { + let file = valid(); + assert!(file.names_boot_of(&discovery(4242, 1_763_000_000))); + assert!( + !file.names_boot_of(&discovery(4243, 1_763_000_000)), + "another process" + ); + assert!( + !file.names_boot_of(&discovery(4242, 1_763_000_001)), + "a later boot" + ); +} + +#[test] +fn an_invalid_file_is_neither_written_nor_read() { + let dir = tempfile::TempDir::new().expect("tempdir"); + let cases = [ + GatewayPrivateFile { pid: 0, ..valid() }, + GatewayPrivateFile { + plan_proof: String::new(), + ..valid() + }, + GatewayPrivateFile { + owner_proof: "has a space".to_owned(), + ..valid() + }, + GatewayPrivateFile { + owner_proof: valid().plan_proof, + ..valid() + }, + ]; + for case in cases { + assert!(case.validation_error().is_some(), "{case:?}"); + assert!(matches!( + case.write_to(dir.path()), + Err(SidecarError::Invalid { .. }) + )); + } + assert_eq!(GatewayPrivateFile::read(dir.path()).expect("reads"), None); + + std::fs::write(gateway_private_file_path(dir.path()), b"{ not json").expect("write"); + assert!(matches!( + GatewayPrivateFile::read(dir.path()), + Err(SidecarError::Parse { .. }) + )); +} + +#[test] +fn only_the_owning_process_removes_it() { + let dir = tempfile::TempDir::new().expect("tempdir"); + remove_private_if_mine(dir.path(), 4242).expect("a missing file is fine"); + valid().write_to(dir.path()).expect("writes"); + remove_private_if_mine(dir.path(), 9999).expect("a foreign pid leaves it"); + assert!( + GatewayPrivateFile::read(dir.path()) + .expect("reads") + .is_some() + ); + remove_private_if_mine(dir.path(), 4242).expect("removes its own"); + assert_eq!(GatewayPrivateFile::read(dir.path()).expect("reads"), None); +} + +#[test] +fn debug_output_never_shows_a_proof() { + let shown = format!("{:?}", valid()); + assert!( + !shown.contains("plan-proof-a") && !shown.contains("owner-proof-b"), + "{shown}" + ); +} diff --git a/crates/gateway-api-discovery/src/private.rs b/crates/gateway-api-discovery/src/private.rs new file mode 100644 index 00000000..59b29ded --- /dev/null +++ b/crates/gateway-api-discovery/src/private.rs @@ -0,0 +1,170 @@ +//! The `gateway-private.json` file: the proofs a running gateway mints at +//! each start for Runvil's own programs. +//! +//! This file is deliberately not part of the discovery contract. +//! `gateway.json` is what any client reads to attach, and its key is the one +//! an operator hands to other tools. The proofs here are for Runvil's own +//! programs only, so they live beside it, in a file no public contract +//! names. Like the discovery file it is written atomically and owner-only, +//! and withdrawn on shutdown. +//! +//! A proof is never a credential on its own. A caller presents it in its +//! own header on top of its ordinary authentication. Both files carry the +//! same `pid` and `epoch`; a reader pairs them only when those agree, so a +//! proof from an earlier boot is never sent to a later one. + +use std::fmt; +use std::fs; +use std::io; +use std::path::Path; + +use serde::{Deserialize, Serialize}; + +use crate::atomic::write_atomic_owner_only; +use crate::error::SidecarError; +use crate::file::GatewayDiscoveryFile; +use crate::paths::gateway_private_file_path; + +/// The on-disk proofs of one gateway boot. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct GatewayPrivateFile { + /// The gateway process id, equal to the discovery file's. + pub pid: u32, + /// The gateway's boot instant in unix seconds, equal to the discovery + /// file's. + pub epoch: u64, + /// Proves the caller is Runvil's own client: required to use a model + /// served on a user's ChatGPT plan. + pub plan_proof: String, + /// Proves the caller may manage what holds the plan's credential. + pub owner_proof: String, +} + +impl fmt::Debug for GatewayPrivateFile { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter + .debug_struct("GatewayPrivateFile") + .field("pid", &self.pid) + .field("epoch", &self.epoch) + .field("plan_proof", &"[REDACTED]") + .field("owner_proof", &"[REDACTED]") + .finish() + } +} + +impl GatewayPrivateFile { + /// Whether these proofs belong to the boot `discovery` names. + #[must_use] + pub fn names_boot_of(&self, discovery: &GatewayDiscoveryFile) -> bool { + self.pid == discovery.pid && self.epoch == discovery.epoch + } + + /// The reason the file fails validation, or `None` when it is valid. + #[must_use] + pub fn validation_error(&self) -> Option<&'static str> { + if self.pid == 0 { + return Some("pid must not be 0"); + } + for proof in [&self.plan_proof, &self.owner_proof] { + if proof.is_empty() { + return Some("a proof must not be empty"); + } + if !proof.bytes().all(|byte| byte.is_ascii_graphic()) { + return Some("a proof must be printable ASCII with no spaces"); + } + } + if self.plan_proof == self.owner_proof { + return Some("the two proofs must differ"); + } + None + } + + /// Reads and validates the private file in `run_dir`, returning `None` + /// when no file exists. + /// + /// # Errors + /// Returns [`SidecarError::Read`] when the file exists but cannot be + /// read, [`SidecarError::Parse`] when it is not valid JSON, and + /// [`SidecarError::Invalid`] when it fails validation. + pub fn read(run_dir: &Path) -> Result, SidecarError> { + let path = gateway_private_file_path(run_dir); + let raw = match fs::read_to_string(&path) { + Ok(raw) => raw, + Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None), + Err(error) => { + return Err(SidecarError::Read { + path, + source: error, + }); + } + }; + let file: GatewayPrivateFile = + serde_json::from_str(&raw).map_err(|source| SidecarError::Parse { + path: path.clone(), + source: source.into(), + })?; + if let Some(reason) = file.validation_error() { + return Err(SidecarError::Invalid { + path, + reason: reason.to_owned(), + }); + } + Ok(Some(file)) + } + + /// Writes the private file into `run_dir`, creating the directory when + /// missing. The write is atomic and the file lands owner-only. + /// + /// # Errors + /// Returns [`SidecarError::Invalid`] when the file fails validation, + /// [`SidecarError::CreateDir`] when the run directory cannot be + /// created, [`SidecarError::Serialize`] when the file cannot be + /// serialized, and [`SidecarError::Write`] when the atomic write fails. + pub fn write_to(&self, run_dir: &Path) -> Result<(), SidecarError> { + let path = gateway_private_file_path(run_dir); + if let Some(reason) = self.validation_error() { + return Err(SidecarError::Invalid { + path, + reason: reason.to_owned(), + }); + } + fs::create_dir_all(run_dir).map_err(|source| SidecarError::CreateDir { + path: run_dir.to_owned(), + source, + })?; + let bytes = serde_json::to_vec_pretty(self).map_err(|source| SidecarError::Serialize { + source: source.into(), + })?; + write_atomic_owner_only(&path, &bytes) + .map_err(|source| SidecarError::Write { path, source }) + } +} + +/// Removes the private file in `run_dir` when it still belongs to process +/// `pid`, so a shutting-down gateway withdraws its proofs but never deletes +/// a replacement's. A missing, unreadable or foreign file is left alone. +/// +/// # Errors +/// Returns [`SidecarError::Remove`] when the file is this process's own but +/// cannot be removed. +pub fn remove_private_if_mine(run_dir: &Path, pid: u32) -> Result<(), SidecarError> { + let path = gateway_private_file_path(run_dir); + let owned = fs::read_to_string(&path).is_ok_and(|raw| { + serde_json::from_str::(&raw).is_ok_and(|file| file.pid == pid) + }); + if !owned { + return Ok(()); + } + match fs::remove_file(&path) { + Ok(()) => Ok(()), + Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()), + Err(error) => Err(SidecarError::Remove { + path, + source: error, + }), + } +} + +#[cfg(test)] +#[path = "private-tests.rs"] +mod tests; diff --git a/crates/gateway/README.md b/crates/gateway/README.md index 77fe33d3..da06d37e 100644 --- a/crates/gateway/README.md +++ b/crates/gateway/README.md @@ -56,6 +56,42 @@ A client pointed at a server without the extension must still transcribe, with t A feature that only works against the gateway is a bug in the design. The test is whether the client still works when the extension is absent. +## The ChatGPT plan + +The gateway can sign a person in to their ChatGPT account and serve the models their plan includes. It is an extension in the sense above, and nothing changes for a client that does not ask. An endpoint asks by setting `auth = "chatgpt-plan"`. That setting requires `protocol = "openai-responses"` and allows neither `base_url` nor `api_key`, because a plan is sent to OpenAI only and with the signed-in person's credentials. Every other endpoint keeps the default, `auth = "api-key"`. + +Two request headers carry the extension. Neither replaces the gateway's ordinary authentication, which a request passes first, and a header sent more than once counts as absent. + +- `X-Runvil-Owner-Proof` shows that the caller can read `gateway-private.json`, the private file the gateway writes for its current boot. It gates the `/admin/chatgpt` routes, which also require the caller's own address to be loopback. +- `X-Runvil-Plan-Proof` shows that the caller is Runvil's own client. It gates the plan's models. + +The sign-in exists only on a gateway that listens on loopback, meaning any address in `127.0.0.0/8` or `::1`. On any other gateway there is no sign-in, and the status route says so. The same terms ask that requests come from the user's own runtime. + +The `/admin/chatgpt` routes are the gateway's own: + +- `POST /admin/chatgpt/sign-in` starts a sign-in and returns one field, `authorize_url`, for the caller to open in a browser. The gateway listens for the browser's callback on `127.0.0.1`. A second call replaces the first. +- `GET /admin/chatgpt/status` reports where the sign-in stands, whether the plan grants use, and the plan's models. It never returns a token. A sign-in is reported as pending until its own work is finished, so a client that polls can stop at the first reply that is not pending. A warning that a token could not be revoked can still be set after a sign-out or a cancelled sign-in has returned, and a client sees it at its next read. +- `POST /admin/chatgpt/sign-out` signs out here and then revokes at OpenAI. +- `POST /admin/chatgpt/forget` signs out and drops the registration. +- `POST /admin/chatgpt/reset` deletes the saved record, readable or not. +- `POST /admin/chatgpt/models/enable` and `POST /admin/chatgpt/models/forget` add the plan's models to the configuration and remove them. + +The standard routes keep their shapes. `GET /v1/models` lists a plan model only to a caller that presents the plan proof, and only while a readable sign-in with plan permission is active. To every other caller the list omits those rows. `POST /v1/chat/completions` for a plan model keeps the standard request, stream, and error shapes. On a gateway that listens on loopback, a request that names a plan model without the proof is refused with `403` and the code `chatgpt_plan_runvil_only`. On any other gateway the refusal comes first and its code is `chatgpt_plan_loopback_only`. The gateway does not substitute another model. + +That refusal is the point of the plan proof. OpenAI's [Sign in with ChatGPT Terms](https://openai.com/policies/sign-in-with-chatgpt-terms/) restrict a plan to the application its user connected, in section 3.1: "Use the user's plan only for the application they connected. Do not provide general-purpose API access for other tools or unrelated requests." The plan proof is how this gateway keeps to that: it serves the plan to Runvil's own client and to nothing else. + +`RUNVIL_CHATGPT_AGENT_NAME` changes the name the consent screen shows and nothing else. It takes 1 to 64 characters after trimming: ASCII letters and digits, the space, and `-_.()`. Any other value is ignored, and the name is `Runvil`. + +## Saving the configuration + +A whole-document save names the revision it was read from, so a page left open cannot overwrite a newer file. `GET /admin/config` returns the revision in `ETag` and in `x-config-revision` whenever the configuration came from a file. `PUT /admin/config` takes it back as `revision` in the body, as `x-config-revision`, or as `If-Match`. + +- A save with no revision is refused with `428` and the code `config_revision_required`, and stages nothing. +- A save whose revision does not match the bytes it would replace is refused with `409` and the code `config_conflict`. +- A revision that is malformed, or two carriers that disagree, is refused with `422`. + +A save is compared with the staged draft when there is one, and with the file otherwise. So once a draft is staged, the revision from `GET /admin/config` may no longer match. `GET /admin/config-pending` returns the revision a save will be compared with, in its `revision` field and in both headers. + ## What this rules out - A parallel dialect of our own where a standard shape would do. diff --git a/crates/gateway/app/Cargo.toml b/crates/gateway/app/Cargo.toml index 8c5106dd..71a21e6d 100644 --- a/crates/gateway/app/Cargo.toml +++ b/crates/gateway/app/Cargo.toml @@ -27,6 +27,7 @@ build-ceiling.workspace = true # Optional: anyhow error payloads for the test-fixture rendezvous in # src/main.rs, enabled only by the `test-fixtures` feature. anyhow = { workspace = true, optional = true } +async-trait.workspace = true axum.workspace = true clap.workspace = true dotenvy.workspace = true @@ -50,6 +51,7 @@ rand.workspace = true # The shared loopback wall for the admin config endpoints; always on, # because those endpoints hold secrets in every build. shared-loopback.workspace = true +gateway-chatgpt-plan.workspace = true gateway-protocol.workspace = true # The provider model sheet schema (gateway-api-types) behind # `GET /admin/cloud-models` and the launch-time sheet cache @@ -162,6 +164,7 @@ tokio = { workspace = true, features = ["test-util"] } tokio-tungstenite.workspace = true gateway-routing = { workspace = true, features = ["test-helpers"] } gateway-stt = { workspace = true, features = ["test-fixtures"] } +gateway-chatgpt-plan = { workspace = true, features = ["test-fixtures"] } tempfile.workspace = true # Drives build_router in-process with forged peer addresses, so the # loopback-wall tests can present a LAN peer no real TCP connection could. diff --git a/crates/gateway/app/src/admin/walled.rs b/crates/gateway/app/src/admin/walled.rs index 4cbc205c..d81f28e7 100644 --- a/crates/gateway/app/src/admin/walled.rs +++ b/crates/gateway/app/src/admin/walled.rs @@ -15,6 +15,7 @@ #[cfg(feature = "local")] mod chat_templates; +mod chatgpt; pub(crate) mod cloud_models; mod config; mod config_apply; @@ -55,7 +56,8 @@ pub(crate) fn routes() -> Router { .merge(env_file::routes()) .merge(cloud_models::routes()) .merge(reveal::routes()) - .merge(hf::routes()); + .merge(hf::routes()) + .merge(chatgpt::routes()); // The template, orphan, and model-info routes read local-inference // facilities, so they exist only in builds with local inference. #[cfg(feature = "local")] @@ -90,6 +92,7 @@ pub(crate) fn registry() -> Vec { cloud_models::ROUTES, reveal::ROUTES, hf::ROUTES, + chatgpt::ROUTES, ] .concat(); #[cfg(feature = "local")] diff --git a/crates/gateway/app/src/admin/walled/chatgpt.rs b/crates/gateway/app/src/admin/walled/chatgpt.rs new file mode 100644 index 00000000..770068a7 --- /dev/null +++ b/crates/gateway/app/src/admin/walled/chatgpt.rs @@ -0,0 +1,251 @@ +//! The `/admin/chatgpt/*` routes: signing a user in to ChatGPT, reporting +//! where that sign-in stands, and ending it. +//! +//! These routes manage the credential that spends a user's ChatGPT plan, so +//! each takes [`OwnerCaller`]: a loopback peer, ordinary authentication, +//! and this boot's owner proof. No route returns a token. The status never +//! holds one, and the sign-in route returns only the URL the browser opens. +//! +//! Sign-in starts a one-shot listener on `127.0.0.1` for the browser's +//! callback and returns at once. The listener hands the callback to the +//! broker and closes when the attempt completes, expires, or is replaced. +//! At most one listener is open: a new sign-in closes the one before it. + +use std::sync::Arc; +use std::time::{SystemTime, UNIX_EPOCH}; + +use axum::extract::State; +use axum::http::{Method, StatusCode}; +use axum::routing::{get, post}; +use axum::{Json, Router}; +use gateway_chatgpt_plan::callback::CallbackListener; +use gateway_chatgpt_plan::plan::{Clock, PlanStatus, SignedOut}; +use gateway_chatgpt_plan::store::{Revocation, Status}; +use serde::Serialize; + +use crate::AppState; +use crate::auth::OwnerCaller; +use crate::error::GatewayError; +use crate::registry::RouteInfo; +use crate::routing::CredentialSource; + +mod models; + +const SIGN_IN: RouteInfo = RouteInfo::walled("/admin/chatgpt/sign-in", &[Method::POST]); +const STATUS: RouteInfo = RouteInfo::walled("/admin/chatgpt/status", &[Method::GET]); +const SIGN_OUT: RouteInfo = RouteInfo::walled("/admin/chatgpt/sign-out", &[Method::POST]); +const FORGET: RouteInfo = RouteInfo::walled("/admin/chatgpt/forget", &[Method::POST]); +const RESET: RouteInfo = RouteInfo::walled("/admin/chatgpt/reset", &[Method::POST]); +const MODELS_ENABLE: RouteInfo = RouteInfo::walled("/admin/chatgpt/models/enable", &[Method::POST]); +const MODELS_FORGET: RouteInfo = RouteInfo::walled("/admin/chatgpt/models/forget", &[Method::POST]); + +/// The ChatGPT sign-in routes, as the registry sees them. +pub(super) const ROUTES: &[RouteInfo] = &[ + SIGN_IN, + STATUS, + SIGN_OUT, + FORGET, + RESET, + MODELS_ENABLE, + MODELS_FORGET, +]; + +/// The ChatGPT sign-in routes. +pub(super) fn routes() -> Router { + Router::new() + .route(SIGN_IN.path, post(sign_in)) + .route(STATUS.path, get(status)) + .route(SIGN_OUT.path, post(sign_out)) + .route(FORGET.path, post(forget)) + .route(RESET.path, post(reset)) + .route(MODELS_ENABLE.path, post(models::enable_models)) + .route(MODELS_FORGET.path, post(models::forget_models)) +} + +/// The `POST /admin/chatgpt/sign-in` reply. +#[derive(Debug, Serialize)] +struct SignInReply { + /// The URL to open in the user's browser. + authorize_url: String, +} + +/// The `GET /admin/chatgpt/status` reply. It never holds a token. +#[derive(Debug, Serialize)] +#[expect( + clippy::struct_excessive_bools, + reason = "each flag is an independent fact a status page shows" +)] +struct StatusReply { + /// `active`, `signed_out`, `needs_auth`, or null when nobody has ever + /// signed in. + status: Option<&'static str>, + /// The saved record cannot be read and must be reset. + unreadable: bool, + /// The signed-in account's email. + email: Option, + /// The account granted plan usage to this app. + plan_granted: bool, + /// The last sign-out's remote outcome: `confirmed` or `unconfirmed`. + revocation: Option<&'static str>, + /// A token the gateway did not keep may still be live at the issuer. + cleanup_unconfirmed: bool, + /// A sign-in is waiting for its callback. + sign_in_pending: bool, + /// The gateway can hold a sign-in at all. + available: bool, + /// Stable reason code (`starting`, `not_loopback`, `no_profile_dir`, + /// `storage_unavailable`) when `available` is false. + unavailable_code: Option<&'static str>, + /// Why not, in one sentence, when `available` is false. + unavailable_reason: Option, + /// Caller-facing model names bound to a `chatgpt-plan` endpoint in the + /// live routing table, in catalog order. + plan_models: Vec, +} + +impl StatusReply { + fn of(plan: &PlanStatus, plan_models: Vec) -> StatusReply { + StatusReply { + status: plan.status.map(|status| match status { + Status::Active => "active", + Status::SignedOut => "signed_out", + Status::NeedsAuth => "needs_auth", + }), + unreadable: plan.unreadable, + email: plan.email.clone(), + plan_granted: plan.plan_granted, + revocation: plan.revocation.map(|revocation| match revocation { + Revocation::Confirmed => "confirmed", + Revocation::Unconfirmed => "unconfirmed", + }), + cleanup_unconfirmed: plan.cleanup_unconfirmed, + sign_in_pending: plan.sign_in_pending, + available: true, + unavailable_code: None, + unavailable_reason: None, + plan_models, + } + } + + fn unavailable(code: &'static str, reason: String) -> StatusReply { + StatusReply { + status: None, + unreadable: false, + email: None, + plan_granted: false, + revocation: None, + cleanup_unconfirmed: false, + sign_in_pending: false, + available: false, + unavailable_code: Some(code), + unavailable_reason: Some(reason), + plan_models: Vec::new(), + } + } +} + +/// The `{"revoked": bool}` reply of sign-out and forget. +#[derive(Debug, Serialize)] +struct RevokedReply { + /// The issuer confirmed the revocation. False covers a failed, timed + /// out or unneeded one: the grant may still be live at the issuer. + revoked: bool, +} + +impl From for RevokedReply { + fn from(outcome: SignedOut) -> RevokedReply { + RevokedReply { + revoked: outcome.revoked, + } + } +} + +/// `POST /admin/chatgpt/sign-in`: starts a sign-in and returns the URL to +/// open. A second call replaces the first. +async fn sign_in( + State(state): State, + _owner: OwnerCaller, +) -> Result, GatewayError> { + let plan = state.chatgpt.broker()?.clone(); + let clock: Clock = Arc::new(|| { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_or(0, |elapsed| elapsed.as_secs()) + }); + // One sign-in at a time reaches the broker and owns the listener slot. + let mut current = state.chatgpt.listener().lock().await; + let listener = CallbackListener::bind(clock) + .await + .map_err(|error| GatewayError::ChatGptUnavailable(error.to_string()))?; + let authorize_url = plan.begin_sign_in(listener.redirect_uri()).await?; + let serving = tokio::spawn(async move { + match listener.serve(&plan).await { + Ok(done) => { + tracing::info!(outcome = ?done, "the ChatGPT sign-in callback listener closed"); + } + Err(error) => { + tracing::warn!("the ChatGPT sign-in callback listener failed: {error}"); + } + } + }); + // The sign-in this one replaced can no longer complete: close its + // listener now instead of leaving its port open until it expires. The + // broker runs an exchange in its own task, so this loses no token. + if let Some(replaced) = current.replace(serving.abort_handle()) { + replaced.abort(); + } + Ok(Json(SignInReply { authorize_url })) +} + +/// `GET /admin/chatgpt/status`: where the sign-in stands. +async fn status(State(state): State, _owner: OwnerCaller) -> Json { + if let Some((code, reason)) = state.chatgpt.unavailable_info() { + return Json(StatusReply::unavailable(code, reason)); + } + let Ok(plan) = state.chatgpt.broker() else { + return Json(StatusReply::unavailable( + "storage_unavailable", + "ChatGPT sign-in is unavailable".to_owned(), + )); + }; + let plan_models = state + .live + .read() + .await + .routing + .models() + .iter() + .filter(|model| model.endpoint.source == CredentialSource::ChatgptPlan) + .map(|model| model.name.clone()) + .collect(); + Json(StatusReply::of(&plan.status(), plan_models)) +} + +/// `POST /admin/chatgpt/sign-out`: signs out here, then revokes at the +/// issuer. +async fn sign_out( + State(state): State, + _owner: OwnerCaller, +) -> Result, GatewayError> { + Ok(Json(state.chatgpt.broker()?.sign_out().await?.into())) +} + +/// `POST /admin/chatgpt/forget`: signs out and drops the registration. +async fn forget( + State(state): State, + _owner: OwnerCaller, +) -> Result, GatewayError> { + Ok(Json(state.chatgpt.broker()?.forget().await?.into())) +} + +/// `POST /admin/chatgpt/reset`: deletes the saved record, readable or not. +async fn reset( + State(state): State, + _owner: OwnerCaller, +) -> Result { + state.chatgpt.broker()?.reset()?; + Ok(StatusCode::NO_CONTENT) +} + +#[cfg(test)] +mod tests; diff --git a/crates/gateway/app/src/admin/walled/chatgpt/models-tests-guards.rs b/crates/gateway/app/src/admin/walled/chatgpt/models-tests-guards.rs new file mode 100644 index 00000000..1274b407 --- /dev/null +++ b/crates/gateway/app/src/admin/walled/chatgpt/models-tests-guards.rs @@ -0,0 +1,410 @@ +//! Refusal, draft-preservation, hand-edit preservation, generation/account +//! fence, and projection/log-canary tests for `/admin/chatgpt/models/*`. + +use std::io::Write; +use std::sync::{Arc, Mutex}; + +use gateway_chatgpt_plan::fake_issuer::Fake; +use reqwest::{Method, StatusCode}; +use serde_json::json; + +use super::{ModelRig, ModelsApi, call, error_code}; +use crate::chatgpt::ChatGpt; +use crate::test_support::{loopback_state, serve_state}; + +#[tokio::test] +async fn pending_drafts_refuse_model_writes_and_leave_files_untouched() { + let env = ModelRig::with_toml("").await; + env.sign_in().await; + + let original_toml = std::fs::read_to_string(&env.config_path).expect("read toml"); + let toml_next = env.config_path.with_file_name("gateway.toml.next"); + let staged = format!("{original_toml}\n# staged config edit\n"); + std::fs::write(&toml_next, &staged).expect("write toml.next"); + + for route in ["models/enable", "models/forget"] { + let resp = env.rig.owner(Method::POST, route).await; + assert_eq!(resp.status(), StatusCode::CONFLICT, "{route}"); + assert_eq!(error_code(resp).await, "config_has_pending_changes"); + assert_eq!( + std::fs::read_to_string(&env.config_path).expect("toml"), + original_toml + ); + assert_eq!(std::fs::read_to_string(&toml_next).expect("next"), staged); + } + + // Credential retirement (`/admin/chatgpt/forget`) stays separate so a + // dirty config draft never blocks revoking OAuth tokens. + let cred_forget = env.rig.owner(Method::POST, "forget").await; + assert_eq!(cred_forget.status(), StatusCode::OK); + env.sign_in().await; + + std::fs::remove_file(&toml_next).expect("rm toml.next"); + + // An unapplied `gateway.env.next` draft also blocks config writes. + let env_next = env.config_path.with_file_name("gateway.env.next"); + std::fs::write(&env_next, "OPENAI_KEY=staged\n").expect("write env.next"); + let resp = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(resp.status(), StatusCode::CONFLICT); + assert_eq!(error_code(resp).await, "config_has_pending_changes"); + assert_eq!( + std::fs::read_to_string(&env_next).expect("env.next"), + "OPENAI_KEY=staged\n" + ); + assert_eq!( + std::fs::read_to_string(&env.config_path).expect("toml"), + original_toml + ); +} + +#[tokio::test] +async fn enable_preserves_hand_edited_disk_config_and_uninterpolated_env_vars() { + let env = ModelRig::with_toml("").await; + env.sign_in().await; + + let env_file = env.config_path.with_extension("env"); + std::fs::write(&env_file, "HAND_API_KEY=sk-hand-secret-value\n").expect("write env"); + + let base = std::fs::read_to_string(&env.config_path).expect("read base"); + let hand_edited = format!( + "# operator note at top\n{base}\n\ + [[endpoint]]\n\ + id = \"hand-ep\"\n\ + protocol = \"openai\"\n\ + base_url = \"http://127.0.0.1:9/v1\"\n\ + api_key = \"${{HAND_API_KEY}}\"\n\n\ + [[model]]\n\ + name = \"hand-model\"\n\ + description = \"Hand-edited model\"\n\ + upstream = \"hand-model\"\n\ + endpoints = [\"hand-ep\"]\n\ + context = 64000\n" + ); + std::fs::write(&env.config_path, &hand_edited).expect("write hand-edited"); + + let resp = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(resp.status(), StatusCode::OK); + + // Original file (including `#` comment) is preserved in `.prev`. + let prev = std::fs::read_to_string(env.config_path.with_file_name("gateway.toml.prev")) + .expect("read prev"); + assert_eq!(prev, hand_edited); + + // Disk `gateway.toml` keeps `hand-ep`, `hand-model`, AND raw `${HAND_API_KEY}` + // (never expanding the secret into `gateway.toml`), plus the new plan models. + let disk = std::fs::read_to_string(&env.config_path).expect("read disk"); + assert!(disk.contains("${HAND_API_KEY}"), "{disk}"); + assert!(!disk.contains("sk-hand-secret-value"), "{disk}"); + assert!(disk.contains("name = \"hand-model\""), "{disk}"); + assert!(disk.contains("name = \"gpt-5\""), "{disk}"); + + // Live routing picked up both the hand-added model and the plan models. + let listed = env.listed_models(true).await; + assert!(listed.contains(&"hand-model".to_owned()), "{listed:?}"); + assert!(listed.contains(&"gpt-5".to_owned()), "{listed:?}"); +} + +#[tokio::test] +async fn enable_refuses_bad_sessions_missing_paths_and_bad_upstreams_and_retries_401_once() { + // 1. Unsigned -> 409 chatgpt_sign_in_required, 0 `/models` hits. + let env = ModelRig::with_toml("").await; + let resp = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(resp.status(), StatusCode::CONFLICT); + assert_eq!(error_code(resp).await, "chatgpt_sign_in_required"); + assert_eq!(env.api.hits(), 0); + + // 2. Signed out -> 409 chatgpt_sign_in_required, 0 `/models` hits. + env.sign_in().await; + assert_eq!( + env.rig.owner(Method::POST, "sign-out").await.status(), + StatusCode::OK + ); + let resp = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(resp.status(), StatusCode::CONFLICT); + assert_eq!(error_code(resp).await, "chatgpt_sign_in_required"); + assert_eq!(env.api.hits(), 0); + + // 3. Active without plan scope -> 409 chatgpt_plan_not_granted, 0 hits. + let no_grant = ModelRig::with_toml("").await; + no_grant.rig.fake.set(|b| { + b.scope = "openid profile email offline_access".to_owned(); + }); + no_grant.sign_in().await; + let resp = no_grant.rig.owner(Method::POST, "models/enable").await; + assert_eq!(resp.status(), StatusCode::CONFLICT); + assert_eq!(error_code(resp).await, "chatgpt_plan_not_granted"); + assert_eq!(no_grant.api.hits(), 0); + + // 4. Missing config_path -> 400 config_path_unavailable. + let fake = Fake::start().await; + let (_api, api_addr) = ModelsApi::start().await; + let profile = tempfile::TempDir::new().expect("tempdir"); + let mut pathless = loopback_state(Some(false)); + pathless.chatgpt = ChatGpt::with_api_base(&format!("http://{api_addr}/v1")); + pathless.chatgpt.open_on(profile.path(), fake.config()); + let owner_proof = pathless.chatgpt.owner_proof().to_owned(); + let addr = serve_state(pathless).await; + let resp = call( + addr, + Method::POST, + "models/enable", + Some("test-token"), + &[&owner_proof], + ) + .await; + assert_eq!(resp.status(), StatusCode::BAD_REQUEST); + assert_eq!(error_code(resp).await, "config_path_unavailable"); + + // 5. Redirect, 500, malformed JSON, and oversized body -> 502 Bad Gateway. + let bad = ModelRig::with_toml("").await; + bad.sign_in().await; + let (trap, trap_addr) = ModelsApi::start().await; + bad.api.set(|b| { + b.redirect_to = Some(format!("http://{trap_addr}/v1/models")); + }); + assert_eq!( + bad.rig.owner(Method::POST, "models/enable").await.status(), + StatusCode::BAD_GATEWAY + ); + assert_eq!(trap.hits(), 0, "redirects must never be followed"); + + bad.api.set(|b| { + b.redirect_to = None; + b.status = 500; + }); + assert_eq!( + bad.rig.owner(Method::POST, "models/enable").await.status(), + StatusCode::BAD_GATEWAY + ); + + bad.api.set(|b| { + b.status = 200; + b.body = "{not-json".to_owned(); + }); + assert_eq!( + bad.rig.owner(Method::POST, "models/enable").await.status(), + StatusCode::BAD_GATEWAY + ); + + bad.api.set(|b| { + b.body = " ".repeat(2 * 1024 * 1024); + }); + assert_eq!( + bad.rig.owner(Method::POST, "models/enable").await.status(), + StatusCode::BAD_GATEWAY + ); + + // 6. Cached token rejected with 401 -> refreshes once and succeeds. + let retry_env = ModelRig::with_toml("").await; + retry_env.sign_in().await; + retry_env.api.set(|b| b.fail_first_401 = true); + let ok = retry_env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(ok.status(), StatusCode::OK); + let seen_auth = retry_env.api.auth_headers(); + assert_eq!(seen_auth.len(), 2); + assert_ne!(seen_auth[0], seen_auth[1], "retry must use refreshed token"); +} + +#[tokio::test] +async fn fence_aborts_write_when_sign_in_changes_mid_fetch() { + // Case A: sign-out while `/models` is in flight. + let env = ModelRig::with_toml("").await; + env.sign_in().await; + let before = std::fs::read_to_string(&env.config_path).expect("read before"); + env.api.set(|b| b.hold = true); + + let entered = env.api.entered.notified(); + let addr = env.rig.addr; + let proof = env.rig.owner_proof.clone(); + let enable_task = tokio::spawn(async move { + call( + addr, + Method::POST, + "models/enable", + Some("test-token"), + &[&proof], + ) + .await + }); + tokio::time::timeout(std::time::Duration::from_secs(5), entered) + .await + .expect("enable reaches /models"); + + assert_eq!( + env.rig.owner(Method::POST, "sign-out").await.status(), + StatusCode::OK + ); + env.api.release.notify_waiters(); + + let resp = enable_task.await.expect("enable joined"); + assert_eq!(resp.status(), StatusCode::CONFLICT); + assert_eq!(error_code(resp).await, "chatgpt_sign_in_changed"); + assert_eq!( + std::fs::read_to_string(&env.config_path).expect("read after"), + before + ); + assert_eq!(env.rig.status().await["plan_models"], json!([])); + + // Case B: forget + sign-in as another account (`user-2`) mid-fetch. + let env2 = ModelRig::with_toml("").await; + env2.sign_in().await; + let before2 = std::fs::read_to_string(&env2.config_path).expect("read before2"); + env2.api.set(|b| b.hold = true); + + let entered2 = env2.api.entered.notified(); + let addr2 = env2.rig.addr; + let proof2 = env2.rig.owner_proof.clone(); + let enable_task2 = tokio::spawn(async move { + call( + addr2, + Method::POST, + "models/enable", + Some("test-token"), + &[&proof2], + ) + .await + }); + tokio::time::timeout(std::time::Duration::from_secs(5), entered2) + .await + .expect("enable2 reaches /models"); + + assert_eq!( + env2.rig.owner(Method::POST, "forget").await.status(), + StatusCode::OK + ); + env2.rig.fake.set(|b| b.subject = "user-2".to_owned()); + env2.sign_in().await; + env2.api.release.notify_waiters(); + + let resp2 = enable_task2.await.expect("enable2 joined"); + assert_eq!(resp2.status(), StatusCode::CONFLICT); + assert_eq!(error_code(resp2).await, "chatgpt_sign_in_changed"); + assert_eq!( + std::fs::read_to_string(&env2.config_path).expect("read after2"), + before2 + ); + assert_eq!(env2.rig.status().await["plan_models"], json!([])); +} + +#[derive(Clone, Default)] +struct BufWriter(Arc>>); + +struct GuardWriter(Arc>>); + +impl Write for GuardWriter { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.0.lock().expect("log buf").extend_from_slice(buf); + Ok(buf.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} + +impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for BufWriter { + type Writer = GuardWriter; + + fn make_writer(&'a self) -> Self::Writer { + GuardWriter(Arc::clone(&self.0)) + } +} + +#[tokio::test] +async fn nothing_beyond_the_projection_reaches_the_response_config_or_logs() { + let writer = BufWriter::default(); + let subscriber = tracing_subscriber::fmt() + .with_max_level(tracing::Level::TRACE) + .with_writer(writer.clone()) + .without_time() + .finish(); + let _guard = tracing::subscriber::set_default(subscriber); + + let env = ModelRig::with_toml("").await; + env.sign_in().await; + tokio::spawn(async { tracing::info!("canary_listener_alive") }) + .await + .expect("probe log"); + + env.api.set(|b| { + b.extra_header = Some(("x-openai-debug".to_owned(), "CANARY_HDR_99".to_owned())); + b.body = json!({ + "models": [ + { + "slug": "gpt-5", + "display_name": "GPT-5", + "visibility": "list", + "context_window": 400_000, + "base_instructions": "CANARY_PROMPT_99", + "organization": "CANARY_ORG_99" + }, + { + "slug": "CANARY_HIDDEN_SLUG", + "display_name": "CANARY_HIDDEN_NAME", + "visibility": "hide", + "context_window": 128_000 + } + ], + "account_tier": "CANARY_TIER_99" + }) + .to_string(); + }); + + let ok_resp = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(ok_resp.status(), StatusCode::OK); + let ok_text = ok_resp.text().await.expect("ok text"); + + // Also exercise upstream error and parse-failure paths with sensitive bodies. + env.api.set(|b| { + b.status = 500; + b.body = "CANARY_ERR_BODY_99".to_owned(); + }); + let err_resp = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(err_resp.status(), StatusCode::BAD_GATEWAY); + let err_text = err_resp.text().await.expect("err text"); + + env.api.set(|b| { + b.status = 200; + b.body = "{\"secret\":\"CANARY_PARSE_TOKEN_99\"".to_owned(); + }); + let bad_json = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(bad_json.status(), StatusCode::BAD_GATEWAY); + let bad_json_text = bad_json.text().await.expect("bad json text"); + + let disk = std::fs::read_to_string(&env.config_path).expect("disk"); + let logs = String::from_utf8_lossy(&writer.0.lock().expect("log buf")).into_owned(); + assert!( + logs.contains("canary_listener_alive"), + "log sink must capture spawned tasks" + ); + + let mut forbidden = vec![ + "CANARY_HDR_99".to_owned(), + "CANARY_PROMPT_99".to_owned(), + "CANARY_ORG_99".to_owned(), + "CANARY_HIDDEN_SLUG".to_owned(), + "CANARY_HIDDEN_NAME".to_owned(), + "CANARY_TIER_99".to_owned(), + "CANARY_ERR_BODY_99".to_owned(), + "CANARY_PARSE_TOKEN_99".to_owned(), + env.rig.owner_proof.clone(), + env.rig.plan_proof.clone(), + ]; + forbidden.extend(env.rig.fake.seen(|s| s.valid_refresh_tokens.clone())); + for auth in env.api.auth_headers() { + if let Some(tok) = auth.strip_prefix("Bearer ") { + forbidden.push(tok.to_owned()); + } + } + + for secret in &forbidden { + assert!(!ok_text.contains(secret), "ok response leaked {secret}"); + assert!(!err_text.contains(secret), "err response leaked {secret}"); + assert!(!bad_json_text.contains(secret), "parse err leaked {secret}"); + assert!(!disk.contains(secret), "gateway.toml leaked {secret}"); + assert!(!logs.contains(secret), "tracing logs leaked {secret}"); + } +} + +#[path = "models-tests-races.rs"] +mod races; diff --git a/crates/gateway/app/src/admin/walled/chatgpt/models-tests-races.rs b/crates/gateway/app/src/admin/walled/chatgpt/models-tests-races.rs new file mode 100644 index 00000000..9fba55ef --- /dev/null +++ b/crates/gateway/app/src/admin/walled/chatgpt/models-tests-races.rs @@ -0,0 +1,354 @@ +//! Routing-before-disk atomicity (R7) and session/setup fence concurrency +//! (R3) tests for `/admin/chatgpt/models/*`. + +use gateway_config::Config; +use reqwest::{Method, StatusCode}; +use serde_json::{Value, json}; + +use super::super::super::error_code; +use super::super::ModelRig; + +#[cfg(feature = "local")] +fn running_local(name: &str) -> crate::local::LocalRuntime { + let config = Config::from_toml_str(&format!( + "config-version = 0\n\ + [server]\nbind = \"127.0.0.1:0\"\napi_key = \"test-token\"\n\ + [[endpoint]]\nid = \"local\"\nprotocol = \"openai\"\n\ + base_url = \"http://127.0.0.1:9\"\napi_key = \"\"\n\ + [[model]]\nname = \"{name}\"\ndescription = \"running child\"\n\ + context = 4096\nupstream = \"{name}\"\nendpoints = [\"local\"]\n" + )) + .expect("local fixture config parses"); + let routing = + crate::routing::Routing::from_config(&config).expect("local fixture routing builds"); + crate::local::LocalRuntime::from_test_models(routing.models().to_vec()) +} + +#[cfg(feature = "local")] +#[tokio::test] +async fn routing_merge_failure_leaves_disk_untouched_and_retry_reconciles_live() { + let env = ModelRig::with_toml("").await; + env.sign_in().await; + let disk_before = std::fs::read_to_string(&env.config_path).expect("read gateway.toml"); + let prev_path = crate::config_shadow::rollback_path(&env.config_path); + + // A local child named `gpt-5` is still loaded in memory while its + // declaration was removed from gateway.toml on disk. + env.state.live.write().await.local = running_local("gpt-5"); + + let failed = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(failed.status(), StatusCode::BAD_REQUEST); + assert_eq!(error_code(failed).await, "switch_failed"); + + let disk_after_fail = std::fs::read_to_string(&env.config_path).expect("read gateway.toml"); + assert_eq!( + disk_after_fail, disk_before, + "routing merge failure must not commit gateway.toml before live routing builds" + ); + assert!( + !prev_path.exists(), + "failed enable must not leave a rollback file behind" + ); + + // Once the conflicting local model unloads, retrying enable succeeds and + // publishes both disk and live routing together. + env.state.live.write().await.local = crate::local::LocalRuntime::empty(); + let retried = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(retried.status(), StatusCode::OK); + let body: Value = retried.json().await.expect("json"); + assert_eq!(body["added"], json!(["gpt-5", "gpt-5-mini"])); + + let status: Value = env + .rig + .owner(Method::GET, "status") + .await + .json() + .await + .expect("status"); + assert_eq!(status["plan_models"], json!(["gpt-5", "gpt-5-mini"])); + + // And if gateway.toml on disk already contains plan rows that live routing + // had not picked up yet, an idempotent enable (`added: []`) still + // reconciles live routing from the validated config. + let baseline_config = Config::from_toml_str(&disk_before).expect("baseline config"); + let baseline_routing = + crate::routing::Routing::from_config(&baseline_config).expect("baseline routing"); + env.state.live.write().await.routing = std::sync::Arc::new(baseline_routing); + + let reconcile = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(reconcile.status(), StatusCode::OK); + let reconcile_body: Value = reconcile.json().await.expect("json"); + assert_eq!(reconcile_body["added"], json!([])); + + let status_after: Value = env + .rig + .owner(Method::GET, "status") + .await + .json() + .await + .expect("status"); + assert_eq!( + status_after["plan_models"], + json!(["gpt-5", "gpt-5-mini"]), + "idempotent enable reconciles live routing when disk already had the plan rows" + ); +} + +#[tokio::test] +async fn models_forget_supersedes_in_flight_enable_while_keeping_sign_in_active() { + let env = ModelRig::with_toml("").await; + env.sign_in().await; + + // Seed an initial enabled catalog, then hold a second `models/enable` + // while `models/forget` removes all plan models. + let seeded = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(seeded.status(), StatusCode::OK); + + env.api.set(|b| b.hold = true); + let addr = env.rig.addr; + let owner = env.rig.owner_proof.clone(); + let in_flight = tokio::spawn(async move { + super::super::super::call( + addr, + Method::POST, + "models/enable", + Some("test-token"), + &[&owner], + ) + .await + }); + env.api.entered.notified().await; + + let forgot = env.rig.owner(Method::POST, "models/forget").await; + assert_eq!(forgot.status(), StatusCode::OK); + + let mid_status: Value = env + .rig + .owner(Method::GET, "status") + .await + .json() + .await + .expect("status"); + assert_eq!( + mid_status["status"], "active", + "models/forget leaves OAuth credentials active" + ); + assert_eq!(mid_status["plan_models"], json!([])); + let disk_after_forget = + std::fs::read_to_string(&env.config_path).expect("read gateway.toml after forget"); + assert!(!disk_after_forget.contains("chatgpt-plan")); + + env.api.release.notify_one(); + let enable_resp = in_flight.await.expect("in-flight enable joins"); + assert_eq!( + enable_resp.status(), + StatusCode::CONFLICT, + "an enable started before models/forget must be superseded instead of reinstalling models" + ); + assert_eq!(error_code(enable_resp).await, "chatgpt_sign_in_changed"); + + let disk_final = std::fs::read_to_string(&env.config_path).expect("read gateway.toml"); + assert_eq!( + disk_final, disk_after_forget, + "superseded enable must not write gateway.toml" + ); + let final_status: Value = env + .rig + .owner(Method::GET, "status") + .await + .json() + .await + .expect("status"); + assert_eq!(final_status["status"], "active"); + assert_eq!(final_status["plan_models"], json!([])); +} + +#[tokio::test] +async fn sign_out_at_commit_boundary_aborts_enable_without_writing_disk_or_live() { + use std::sync::Arc; + + use gateway_chatgpt_plan::fake_issuer::Fake; + + use super::super::super::Rig; + use super::super::ModelsApi; + use crate::chatgpt::ChatGpt; + use crate::park::{Phase, PhasePark}; + use crate::test_support::{AdminPaths, app_state}; + + let fake = Fake::start().await; + let (api, api_addr) = ModelsApi::start().await; + let profile = tempfile::TempDir::new().expect("tempdir"); + let cache = profile.path().join("cache"); + std::fs::create_dir_all(cache.join("models")).expect("mkdir cache"); + let config_path = profile.path().join("gateway.toml"); + let base_toml = format!( + "config-version = 0\n\n\ + [server]\n\ + bind = \"127.0.0.1:0\"\n\ + api_key = \"test-token\"\n\n\ + [local]\n\ + cache_dir = '{}'\n", + cache.display() + ); + std::fs::write(&config_path, &base_toml).expect("write gateway.toml"); + let config = Config::from_toml_str(&base_toml).expect("valid base config"); + let mut state = app_state( + config, + Some(AdminPaths { + fixture_dir: profile.path().to_path_buf(), + active: "main".to_owned(), + config_path: config_path.clone(), + }), + ); + let park = Arc::new(PhasePark::at(Phase::ChatGptEnableCommit)); + state.park = Some(Arc::clone(&park)); + state.chatgpt = ChatGpt::with_api_base(&format!("http://{api_addr}/v1")); + state.chatgpt.open_on(profile.path(), fake.config()); + let broker = state.chatgpt.broker().expect("broker").clone(); + let rig = Rig::serve(fake, profile, state).await; + + let callback = rig.begin().await; + assert_eq!( + reqwest::get(&callback).await.expect("callback").status(), + StatusCode::OK + ); + + let addr = rig.addr; + let owner = rig.owner_proof.clone(); + let in_flight = tokio::spawn(async move { + super::super::super::call( + addr, + Method::POST, + "models/enable", + Some("test-token"), + &[&owner], + ) + .await + }); + park.entered().await; + + // Credential `forget` runs in the commit-boundary window (after pre-checks, + // TOML preparation, and routing construction, as an already-admitted + // `/admin/chatgpt/forget` handler or OAuth callback completion can). + assert!(broker.forget().await.expect("forgets").revoked); + + park.release(); + let enable_resp = in_flight.await.expect("parked enable joins"); + assert_eq!( + enable_resp.status(), + StatusCode::CONFLICT, + "session retirement at the commit boundary must abort enable before writing" + ); + assert_eq!(error_code(enable_resp).await, "chatgpt_sign_in_changed"); + + let disk_after = std::fs::read_to_string(&config_path).expect("read gateway.toml"); + assert_eq!(disk_after, base_toml, "gateway.toml must remain untouched"); + assert!( + !crate::config_shadow::rollback_path(&config_path).exists(), + "rollback file must not be created" + ); + let status: Value = rig + .owner(Method::GET, "status") + .await + .json() + .await + .expect("status"); + assert_eq!(status["plan_models"], json!([])); + assert_eq!(api.hits(), 1); +} + +#[tokio::test] +async fn same_account_sign_out_and_re_sign_in_mid_fetch_aborts_enable() { + let env = ModelRig::with_toml("").await; + env.sign_in().await; + let disk_before = std::fs::read_to_string(&env.config_path).expect("read gateway.toml"); + + env.api.set(|b| b.hold = true); + let addr = env.rig.addr; + let owner = env.rig.owner_proof.clone(); + let in_flight = tokio::spawn(async move { + super::super::super::call( + addr, + Method::POST, + "models/enable", + Some("test-token"), + &[&owner], + ) + .await + }); + env.api.entered.notified().await; + + // The same account (`user-1`, active with plan grant) signs out and signs + // back in while `/models` is in flight: only `state.generation` changed. + let signed_out = env.rig.owner(Method::POST, "sign-out").await; + assert_eq!(signed_out.status(), StatusCode::OK); + env.sign_in().await; + assert_eq!(env.rig.status().await["status"], "active"); + + env.api.release.notify_one(); + let enable_resp = in_flight.await.expect("in-flight enable joins"); + assert_eq!( + enable_resp.status(), + StatusCode::CONFLICT, + "same-account re-sign-in mid-fetch must invalidate the earlier generation's fence" + ); + assert_eq!(error_code(enable_resp).await, "chatgpt_sign_in_changed"); + + let disk_after = std::fs::read_to_string(&env.config_path).expect("read gateway.toml"); + assert_eq!(disk_after, disk_before); + assert_eq!(env.rig.status().await["plan_models"], json!([])); +} + +#[tokio::test] +async fn missing_no_redirect_models_client_fails_closed() { + use gateway_chatgpt_plan::fake_issuer::Fake; + + use super::super::super::Rig; + use super::super::ModelsApi; + use crate::chatgpt::ChatGpt; + use crate::test_support::{AdminPaths, app_state}; + + let fake = Fake::start().await; + let (api, api_addr) = ModelsApi::start().await; + let profile = tempfile::TempDir::new().expect("tempdir"); + let cache = profile.path().join("cache"); + std::fs::create_dir_all(cache.join("models")).expect("mkdir cache"); + let config_path = profile.path().join("gateway.toml"); + let base_toml = format!( + "config-version = 0\n\n\ + [server]\n\ + bind = \"127.0.0.1:0\"\n\ + api_key = \"test-token\"\n\n\ + [local]\n\ + cache_dir = '{}'\n", + cache.display() + ); + std::fs::write(&config_path, &base_toml).expect("write gateway.toml"); + let config = Config::from_toml_str(&base_toml).expect("valid base config"); + let mut state = app_state( + config, + Some(AdminPaths { + fixture_dir: profile.path().to_path_buf(), + active: "main".to_owned(), + config_path, + }), + ); + state.chatgpt = ChatGpt::without_models_client(&format!("http://{api_addr}/v1")); + state.chatgpt.open_on(profile.path(), fake.config()); + let rig = Rig::serve(fake, profile, state).await; + + let callback = rig.begin().await; + assert_eq!( + reqwest::get(&callback).await.expect("callback").status(), + StatusCode::OK + ); + + let resp = rig.owner(Method::POST, "models/enable").await; + assert_eq!(resp.status(), StatusCode::SERVICE_UNAVAILABLE); + assert_eq!(error_code(resp).await, "chatgpt_unavailable"); + assert_eq!( + api.hits(), + 0, + "without the no-redirect client, no request is ever sent" + ); +} diff --git a/crates/gateway/app/src/admin/walled/chatgpt/models-tests.rs b/crates/gateway/app/src/admin/walled/chatgpt/models-tests.rs new file mode 100644 index 00000000..4bca3746 --- /dev/null +++ b/crates/gateway/app/src/admin/walled/chatgpt/models-tests.rs @@ -0,0 +1,496 @@ +//! Tests for `POST /admin/chatgpt/models/enable` and +//! `POST /admin/chatgpt/models/forget`: happy path, idempotency, name +//! collision handling (`-chatgpt` and double-clash), and forget's +//! preservation of multi-endpoint fallback models. + +use std::net::SocketAddr; +use std::path::PathBuf; +use std::sync::{Arc, Mutex}; + +use axum::Router; +use axum::extract::State; +use axum::http::{HeaderMap, StatusCode as AxumStatus, header}; +use axum::response::{IntoResponse, Response}; +use axum::routing::get; +use gateway_chatgpt_plan::fake_issuer::Fake; +use gateway_config::Config; +use reqwest::{Method, StatusCode}; +use serde_json::{Value, json}; +use tokio::sync::Notify; + +use super::{Rig, call, error_code}; +use crate::chatgpt::{ChatGpt, PLAN_PROOF_HEADER}; +use crate::test_support::{AdminPaths, app_state}; + +#[derive(Clone, Default)] +pub(super) struct ModelsApi { + state: Arc>, + pub(super) entered: Arc, + pub(super) release: Arc, +} + +pub(super) struct ModelsBehavior { + pub(super) status: u16, + pub(super) body: String, + pub(super) redirect_to: Option, + pub(super) extra_header: Option<(String, String)>, + pub(super) fail_first_401: bool, + pub(super) hold: bool, + pub(super) hits: u32, + pub(super) auth_headers: Vec, +} + +impl Default for ModelsBehavior { + fn default() -> Self { + Self { + status: 200, + body: json!({ + "models": [ + { + "slug": "gpt-5", + "display_name": "GPT-5", + "visibility": "list", + "context_window": 400_000 + }, + { + "slug": "gpt-5-mini", + "visibility": "list", + "context_window": 200_000 + }, + { + "slug": "gpt-hidden", + "display_name": "Hidden Internal", + "visibility": "hide", + "context_window": 128_000 + }, + { + "slug": "gpt-no-ctx", + "display_name": "No Context", + "visibility": "list" + } + ] + }) + .to_string(), + redirect_to: None, + extra_header: None, + fail_first_401: false, + hold: false, + hits: 0, + auth_headers: Vec::new(), + } + } +} + +impl ModelsApi { + pub(super) async fn start() -> (Self, SocketAddr) { + let api = Self::default(); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0") + .await + .expect("models listener binds"); + let addr = listener.local_addr().expect("bound addr"); + let served = api.clone(); + tokio::spawn(async move { + let app = Router::new() + .route("/v1/models", get(handle_models)) + .with_state(served); + let _ = axum::serve(listener, app).await; + }); + (api, addr) + } + + pub(super) fn set(&self, f: impl FnOnce(&mut ModelsBehavior)) { + f(&mut self.state.lock().expect("models lock")); + } + + pub(super) fn hits(&self) -> u32 { + self.state.lock().expect("models lock").hits + } + + pub(super) fn auth_headers(&self) -> Vec { + self.state.lock().expect("models lock").auth_headers.clone() + } +} + +async fn handle_models(State(api): State, headers: HeaderMap) -> Response { + let (hold, reply) = { + let mut guard = api.state.lock().expect("models lock"); + guard.hits += 1; + if let Some(auth) = headers + .get(header::AUTHORIZATION) + .and_then(|v| v.to_str().ok()) + { + guard.auth_headers.push(auth.to_owned()); + } + if guard.fail_first_401 { + guard.fail_first_401 = false; + return (AxumStatus::UNAUTHORIZED, "expired token").into_response(); + } + let hold = guard.hold; + let response = if let Some(loc) = guard.redirect_to.clone() { + (AxumStatus::FOUND, [(header::LOCATION, loc)], String::new()).into_response() + } else { + let status = AxumStatus::from_u16(guard.status).expect("status"); + let mut resp = ( + status, + [(header::CONTENT_TYPE, "application/json")], + guard.body.clone(), + ) + .into_response(); + if let Some((k, v)) = &guard.extra_header { + resp.headers_mut().insert( + axum::http::HeaderName::from_bytes(k.as_bytes()).expect("header name"), + axum::http::HeaderValue::from_str(v).expect("header val"), + ); + } + resp + }; + (hold, response) + }; + if hold { + api.entered.notify_one(); + api.release.notified().await; + } + reply +} + +pub(super) struct ModelRig { + pub(super) rig: Rig, + pub(super) api: ModelsApi, + pub(super) config_path: PathBuf, + pub(super) state: crate::AppState, +} + +impl ModelRig { + pub(super) async fn with_toml(extra_toml: &str) -> Self { + let fake = Fake::start().await; + let (api, api_addr) = ModelsApi::start().await; + let profile = tempfile::TempDir::new().expect("tempdir"); + let cache = profile.path().join("cache"); + std::fs::create_dir_all(cache.join("models")).expect("mkdir cache"); + let config_path = profile.path().join("gateway.toml"); + let base_toml = format!( + "config-version = 0\n\n\ + [server]\n\ + bind = \"127.0.0.1:0\"\n\ + api_key = \"test-token\"\n\n\ + [local]\n\ + cache_dir = '{}'\n\n\ + {extra_toml}", + cache.display() + ); + std::fs::write(&config_path, &base_toml).expect("write gateway.toml"); + let config = Config::from_toml_str(&base_toml).expect("valid base config"); + let mut state = app_state( + config, + Some(AdminPaths { + fixture_dir: profile.path().to_path_buf(), + active: "main".to_owned(), + config_path: config_path.clone(), + }), + ); + state.chatgpt = ChatGpt::with_api_base(&format!("http://{api_addr}/v1")); + state.chatgpt.open_on(profile.path(), fake.config()); + let rig = Rig::serve(fake, profile, state.clone()).await; + Self { + rig, + api, + config_path, + state, + } + } + + pub(super) async fn sign_in(&self) { + let callback = self.rig.begin().await; + let page = reqwest::get(&callback).await.expect("callback answers"); + assert_eq!(page.status(), StatusCode::OK); + } + + pub(super) async fn listed_models(&self, with_plan_proof: bool) -> Vec { + let mut req = reqwest::Client::new() + .get(format!("http://{}/v1/models", self.rig.addr)) + .bearer_auth("test-token"); + if with_plan_proof { + req = req.header(PLAN_PROOF_HEADER, &self.rig.plan_proof); + } + let body: Value = req + .send() + .await + .expect("v1/models") + .json() + .await + .expect("json"); + body["data"] + .as_array() + .expect("data array") + .iter() + .map(|entry| entry["id"].as_str().expect("id").to_owned()) + .collect() + } +} + +#[tokio::test] +async fn enable_adds_visible_plan_models_applies_immediately_and_is_idempotent() { + let env = ModelRig::with_toml("").await; + env.sign_in().await; + + let response = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(response.status(), StatusCode::OK); + let reply: Value = response.json().await.expect("enable reply"); + assert_eq!( + reply, + json!({ + "endpoint": "chatgpt", + "added": ["gpt-5", "gpt-5-mini"], + "renamed": [], + "skipped": [{ "name": "gpt-no-ctx", "reason": "missing_context_window" }], + }) + ); + + // Both live routing and status update immediately without a restart. + assert_eq!( + env.rig.status().await["plan_models"], + json!(["gpt-5", "gpt-5-mini"]) + ); + assert_eq!(env.listed_models(true).await, ["gpt-5", "gpt-5-mini"]); + assert!(env.listed_models(false).await.is_empty()); + + // Rollback copy `.prev` was created before replacing `gateway.toml`. + assert!( + env.config_path + .with_file_name("gateway.toml.prev") + .is_file() + ); + let disk = std::fs::read_to_string(&env.config_path).expect("read config"); + assert!(disk.contains("auth = \"chatgpt-plan\""), "{disk}"); + assert!(disk.contains("description = \"GPT-5\""), "{disk}"); + assert!(!disk.contains("gpt-hidden"), "{disk}"); + + // Stamp a comment on disk, then run enable a second time: no-op writes + // nothing to disk and reports `added: []`. + let commented = format!("# keep-on-noop\n{disk}"); + std::fs::write(&env.config_path, &commented).expect("write comment"); + let second = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(second.status(), StatusCode::OK); + let second_reply: Value = second.json().await.expect("second reply"); + assert_eq!( + second_reply, + json!({ + "endpoint": "chatgpt", + "added": [], + "renamed": [], + "skipped": [{ "name": "gpt-no-ctx", "reason": "missing_context_window" }], + }) + ); + let after_second = std::fs::read_to_string(&env.config_path).expect("read after second"); + assert!( + after_second.starts_with("# keep-on-noop\n"), + "idempotent no-op should not rewrite unchanged gateway.toml" + ); +} + +#[tokio::test] +async fn enable_suffixes_clashing_names_and_skips_double_clashes() { + let extra = r#" +[[endpoint]] +id = "paid" +protocol = "openai" +base_url = "http://127.0.0.1:9/v1" +api_key = "sk-paid" + +[[model]] +name = "gpt-5" +description = "Paid GPT-5" +upstream = "gpt-5" +endpoints = ["paid"] +context = 128000 + +[[model]] +name = "gpt-4o" +description = "Paid GPT-4o" +upstream = "gpt-4o" +endpoints = ["paid"] +context = 128000 + +[[model]] +name = "gpt-4o-chatgpt" +description = "Custom suffixed GPT-4o" +upstream = "gpt-4o" +endpoints = ["paid"] +context = 128000 +"#; + let env = ModelRig::with_toml(extra).await; + env.api.set(|b| { + b.body = json!({ + "models": [ + { "slug": "gpt-5", "display_name": "GPT-5", "visibility": "list", "context_window": 400_000 }, + { "slug": "gpt-4o", "display_name": "GPT-4o", "visibility": "list", "context_window": 128_000 }, + { "slug": "o3", "display_name": "o3", "visibility": "list", "context_window": 200_000 } + ] + }) + .to_string(); + }); + env.sign_in().await; + + let resp = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(resp.status(), StatusCode::OK); + let body: Value = resp.json().await.expect("json"); + assert_eq!( + body, + json!({ + "endpoint": "chatgpt", + "added": ["gpt-5-chatgpt", "o3"], + "renamed": [{ "from": "gpt-5", "to": "gpt-5-chatgpt" }], + "skipped": [{ "name": "gpt-4o", "reason": "name_taken" }], + }) + ); + assert_eq!( + env.rig.status().await["plan_models"], + json!(["gpt-5-chatgpt", "o3"]) + ); + let disk = std::fs::read_to_string(&env.config_path).expect("read config"); + assert!(disk.contains("upstream = \"gpt-5\""), "{disk}"); + + // A repeat call recognizes the suffixed binding via upstream and adds nothing. + let again = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(again.status(), StatusCode::OK); + let again_body: Value = again.json().await.expect("json"); + assert_eq!(again_body["added"], json!([])); + assert_eq!(again_body["renamed"], json!([])); + assert_eq!( + again_body["skipped"], + json!([{ "name": "gpt-4o", "reason": "name_taken" }]) + ); +} + +#[tokio::test] +async fn forget_removes_plan_endpoint_and_plan_only_models_while_keeping_fallbacks() { + let extra = r#" +[[endpoint]] +id = "paid" +protocol = "openai" +base_url = "http://127.0.0.1:9/v1" +api_key = "sk-paid" + +[[endpoint]] +id = "chatgpt" +protocol = "openai-responses" +auth = "chatgpt-plan" + +[[model]] +name = "gpt-5" +description = "GPT-5" +upstream = "gpt-5" +endpoints = ["chatgpt"] +context = 400000 + +[[model]] +name = "hybrid" +description = "Hybrid" +upstream = "gpt-5" +endpoints = ["chatgpt", "paid"] +context = 128000 +"#; + let env = ModelRig::with_toml(extra).await; + // Works even when signed out (no active sign-in needed to clean config). + assert_eq!( + env.rig.status().await["plan_models"], + json!(["gpt-5", "hybrid"]) + ); + + let resp = env.rig.owner(Method::POST, "models/forget").await; + assert_eq!(resp.status(), StatusCode::OK); + let body: Value = resp.json().await.expect("forget reply"); + assert_eq!( + body, + json!({ + "removed_endpoints": ["chatgpt"], + "removed": ["gpt-5"], + "kept": ["hybrid"], + }) + ); + assert_eq!(env.rig.status().await["plan_models"], json!([])); + assert_eq!(env.listed_models(false).await, ["hybrid"]); + + let disk = std::fs::read_to_string(&env.config_path).expect("read config"); + assert!(!disk.contains("chatgpt-plan"), "{disk}"); + assert!(!disk.contains("name = \"gpt-5\""), "{disk}"); + assert!(disk.contains("name = \"hybrid\""), "{disk}"); + + // Second forget is a clean no-op. + let second = env.rig.owner(Method::POST, "models/forget").await; + assert_eq!(second.status(), StatusCode::OK); + assert_eq!( + second.json::().await.expect("json"), + json!({ "removed_endpoints": [], "removed": [], "kept": [] }) + ); +} + +#[tokio::test] +async fn api_first_mixed_endpoint_row_gets_a_plan_variant_instead_of_being_skipped() { + let extra = r#" +[[endpoint]] +id = "paid" +protocol = "openai" +base_url = "http://127.0.0.1:9/v1" +api_key = "sk-paid" + +[[endpoint]] +id = "chatgpt" +protocol = "openai-responses" +auth = "chatgpt-plan" + +[[model]] +name = "gpt-5" +description = "Paid primary" +upstream = "gpt-5" +endpoints = ["paid", "chatgpt"] +context = 128000 +"#; + let env = ModelRig::with_toml(extra).await; + env.sign_in().await; + assert_eq!( + env.rig.status().await["plan_models"], + json!([]), + "endpoints[0] is API-key backed, so no plan model is active yet" + ); + + env.api.set(|b| { + b.body = json!({ + "models": [{ + "slug": "gpt-5", + "display_name": "GPT-5", + "visibility": "list", + "context_window": 400_000 + }] + }) + .to_string(); + }); + + let resp = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(resp.status(), StatusCode::OK); + let body: Value = resp.json().await.expect("enable reply"); + assert_eq!( + body, + json!({ + "endpoint": "chatgpt", + "added": ["gpt-5-chatgpt"], + "renamed": [{ "from": "gpt-5", "to": "gpt-5-chatgpt" }], + "skipped": [], + }) + ); + assert_eq!( + env.rig.status().await["plan_models"], + json!(["gpt-5-chatgpt"]) + ); + + // Repeat enable is now idempotent because gpt-5-chatgpt binds endpoints[0] = "chatgpt". + let again = env.rig.owner(Method::POST, "models/enable").await; + assert_eq!(again.status(), StatusCode::OK); + assert_eq!( + again.json::().await.expect("json")["added"], + json!([]) + ); +} + +#[path = "models-tests-guards.rs"] +mod guards; diff --git a/crates/gateway/app/src/admin/walled/chatgpt/models-toml.rs b/crates/gateway/app/src/admin/walled/chatgpt/models-toml.rs new file mode 100644 index 00000000..f62f69a4 --- /dev/null +++ b/crates/gateway/app/src/admin/walled/chatgpt/models-toml.rs @@ -0,0 +1,207 @@ +//! Pure TOML table transforms for adding and removing `auth = "chatgpt-plan"` +//! endpoints and model bindings in `gateway.toml`. + +use std::collections::HashSet; + +use crate::error::GatewayError; + +const DEFAULT_PLAN_ENDPOINT_ID: &str = "chatgpt"; +const FALLBACK_PLAN_ENDPOINT_ID: &str = "chatgpt-plan"; + +pub(super) fn ensure_plan_endpoint( + root: &mut toml::map::Map, +) -> Result<(String, HashSet, bool), GatewayError> { + let mut all_ids = HashSet::new(); + let mut plan_ids = Vec::new(); + if let Some(endpoints) = root.get("endpoint").and_then(toml::Value::as_array) { + for ep in endpoints { + let Some(id) = ep.get("id").and_then(toml::Value::as_str) else { + continue; + }; + all_ids.insert(id.to_owned()); + if ep.get("auth").and_then(toml::Value::as_str) == Some("chatgpt-plan") { + plan_ids.push(id.to_owned()); + } + } + } + if let Some(first) = plan_ids.first().cloned() { + return Ok((first, plan_ids.into_iter().collect(), false)); + } + + let chosen = pick_endpoint_id(&all_ids); + let entry = root + .entry("endpoint") + .or_insert_with(|| toml::Value::Array(Vec::new())); + let toml::Value::Array(endpoints) = entry else { + return Err(GatewayError::ConfigWriteRejected( + "`endpoint` must be an array of tables".to_owned(), + )); + }; + let mut table = toml::map::Map::new(); + table.insert("id".to_owned(), toml::Value::String(chosen.clone())); + table.insert( + "protocol".to_owned(), + toml::Value::String("openai-responses".to_owned()), + ); + table.insert( + "auth".to_owned(), + toml::Value::String("chatgpt-plan".to_owned()), + ); + endpoints.push(toml::Value::Table(table)); + + let mut set = HashSet::new(); + set.insert(chosen.clone()); + Ok((chosen, set, true)) +} + +fn pick_endpoint_id(existing: &HashSet) -> String { + if !existing.contains(DEFAULT_PLAN_ENDPOINT_ID) { + return DEFAULT_PLAN_ENDPOINT_ID.to_owned(); + } + if !existing.contains(FALLBACK_PLAN_ENDPOINT_ID) { + return FALLBACK_PLAN_ENDPOINT_ID.to_owned(); + } + let mut idx = 2_u32; + loop { + let candidate = format!("{FALLBACK_PLAN_ENDPOINT_ID}-{idx}"); + if !existing.contains(&candidate) { + return candidate; + } + idx += 1; + } +} + +pub(super) fn collect_catalog_names( + root: &toml::map::Map, + plan_ids: &HashSet, +) -> (HashSet, HashSet) { + let mut names = HashSet::new(); + let mut bound_upstreams = HashSet::new(); + + for section in ["model", "local_model", "stt_model"] { + let Some(items) = root.get(section).and_then(toml::Value::as_array) else { + continue; + }; + for item in items { + if let Some(name) = item.get("name").and_then(toml::Value::as_str) { + names.insert(name.to_owned()); + } + if section == "model" { + // `Routing::from_config` binds only `endpoints[0]`, so an + // API-first mixed row (`["paid", "chatgpt"]`) is not an active + // plan binding. + let uses_plan = item + .get("endpoints") + .and_then(toml::Value::as_array) + .and_then(|eps| eps.first()) + .and_then(toml::Value::as_str) + .is_some_and(|ep| plan_ids.contains(ep)); + if uses_plan + && let Some(up) = item + .get("upstream") + .or_else(|| item.get("name")) + .and_then(toml::Value::as_str) + { + bound_upstreams.insert(up.to_owned()); + } + } + } + } + (names, bound_upstreams) +} + +pub(super) fn push_plan_model( + root: &mut toml::map::Map, + name: &str, + description: &str, + context: u32, + upstream: &str, + endpoint_id: &str, +) -> Result<(), GatewayError> { + let entry = root + .entry("model") + .or_insert_with(|| toml::Value::Array(Vec::new())); + let toml::Value::Array(models) = entry else { + return Err(GatewayError::ConfigWriteRejected( + "`model` must be an array of tables".to_owned(), + )); + }; + let mut table = toml::map::Map::new(); + table.insert("name".to_owned(), toml::Value::String(name.to_owned())); + table.insert( + "description".to_owned(), + toml::Value::String(description.to_owned()), + ); + table.insert( + "context".to_owned(), + toml::Value::Integer(i64::from(context)), + ); + table.insert( + "upstream".to_owned(), + toml::Value::String(upstream.to_owned()), + ); + table.insert( + "endpoints".to_owned(), + toml::Value::Array(vec![toml::Value::String(endpoint_id.to_owned())]), + ); + models.push(toml::Value::Table(table)); + Ok(()) +} + +pub(super) fn strip_plan_endpoints(root: &mut toml::map::Map) -> Vec { + let Some(toml::Value::Array(endpoints)) = root.get_mut("endpoint") else { + return Vec::new(); + }; + let mut removed = Vec::new(); + endpoints.retain(|ep| { + let is_plan = ep.get("auth").and_then(toml::Value::as_str) == Some("chatgpt-plan"); + if is_plan && let Some(id) = ep.get("id").and_then(toml::Value::as_str) { + removed.push(id.to_owned()); + } + !is_plan + }); + if endpoints.is_empty() { + root.remove("endpoint"); + } + removed +} + +pub(super) fn strip_plan_models( + root: &mut toml::map::Map, + removed_endpoints: &HashSet<&str>, +) -> (Vec, Vec) { + let Some(toml::Value::Array(models)) = root.get_mut("model") else { + return (Vec::new(), Vec::new()); + }; + let mut removed = Vec::new(); + let mut kept = Vec::new(); + models.retain_mut(|item| { + let Some(table) = item.as_table_mut() else { + return true; + }; + let name = table + .get("name") + .and_then(toml::Value::as_str) + .unwrap_or_default() + .to_owned(); + let Some(toml::Value::Array(eps)) = table.get_mut("endpoints") else { + return true; + }; + let before = eps.len(); + eps.retain(|ep| ep.as_str().is_none_or(|id| !removed_endpoints.contains(id))); + if eps.len() == before { + return true; + } + if eps.is_empty() { + removed.push(name); + false + } else { + kept.push(name); + true + } + }); + if models.is_empty() { + root.remove("model"); + } + (removed, kept) +} diff --git a/crates/gateway/app/src/admin/walled/chatgpt/models.rs b/crates/gateway/app/src/admin/walled/chatgpt/models.rs new file mode 100644 index 00000000..045394e0 --- /dev/null +++ b/crates/gateway/app/src/admin/walled/chatgpt/models.rs @@ -0,0 +1,351 @@ +//! `POST /admin/chatgpt/models/enable` and `POST /admin/chatgpt/models/forget`: +//! owner-gated mutations of `gateway.toml` and live routing for ChatGPT plan +//! endpoints and models. + +use std::collections::HashSet; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use axum::Json; +use axum::extract::State; +use gateway_chatgpt_plan::plan::{ChatGptPlan, SessionFence}; +use gateway_config::{Config, ProfileName, shadow_path, write_atomic}; +#[cfg(feature = "web-search")] +use gateway_web_search::WebSearchState; +use serde::Serialize; + +use crate::AppState; +use crate::auth::OwnerCaller; +use crate::chatgpt::PlanModelEntry; +use crate::config_shadow::{PendingEnv, rollback_path}; +use crate::error::{GatewayError, blocking, config_write_error}; +use crate::routing::Routing; + +#[path = "models-toml.rs"] +mod toml_ops; + +/// `POST /admin/chatgpt/models/enable` reply. +#[derive(Debug, Serialize)] +pub(super) struct EnableReply { + endpoint: String, + added: Vec, + renamed: Vec, + skipped: Vec, +} + +/// One plan model whose upstream slug collided with a non-plan model name. +#[derive(Debug, Serialize)] +pub(super) struct RenamedModel { + from: String, + to: String, +} + +/// One plan model omitted from `gateway.toml`, with a stable reason code. +#[derive(Debug, Serialize)] +pub(super) struct SkippedModel { + name: String, + reason: &'static str, +} + +/// `POST /admin/chatgpt/models/forget` reply. +#[derive(Debug, Serialize)] +pub(super) struct ForgetReply { + removed_endpoints: Vec, + removed: Vec, + kept: Vec, +} + +/// Isolated naming rule when a plan model's `slug` is already taken by a +/// non-plan model in `gateway.toml`. +fn plan_collision_name(slug: &str) -> String { + format!("{slug}-chatgpt") +} + +/// `POST /admin/chatgpt/models/enable`: fetches the account's visible plan +/// models before taking the apply lock, then commits them to `gateway.toml` +/// and swaps live routing atomically under the session fence. +pub(super) async fn enable_models( + State(state): State, + _owner: OwnerCaller, +) -> Result, GatewayError> { + let plan = state.chatgpt.broker()?.clone(); + let config_path = crate::admin::config_path(&state)?.to_path_buf(); + let _ = plan.current_fence()?; + if has_pending_drafts(&config_path) { + return Err(GatewayError::ConfigHasPendingChanges); + } + + let (fence, entries) = state.chatgpt.fetch_plan_models().await?; + + let _apply_guard = state.apply.lock().await; + if !plan.verify_fence(&fence) { + return Err(GatewayError::ChatGptSignInChanged); + } + if has_pending_drafts(&config_path) { + return Err(GatewayError::ConfigHasPendingChanges); + } + + let (reply, prepared) = { + let path = config_path.clone(); + blocking(move || prepare_enable(&path, entries)).await?? + }; + commit_prepared(&state, config_path, prepared, Some((&plan, &fence))).await?; + Ok(Json(reply)) +} + +/// `POST /admin/chatgpt/models/forget`: removes `auth = "chatgpt-plan"` +/// endpoints and plan-only models from `gateway.toml` and live routing. +pub(super) async fn forget_models( + State(state): State, + _owner: OwnerCaller, +) -> Result, GatewayError> { + let plan = state.chatgpt.broker()?.clone(); + let config_path = crate::admin::config_path(&state)?.to_path_buf(); + + let _apply_guard = state.apply.lock().await; + if has_pending_drafts(&config_path) { + return Err(GatewayError::ConfigHasPendingChanges); + } + plan.supersede_model_setup(); + + let (reply, prepared) = { + let path = config_path.clone(); + blocking(move || prepare_forget(&path)).await?? + }; + commit_prepared(&state, config_path, prepared, None).await?; + Ok(Json(reply)) +} + +fn has_pending_drafts(config_path: &Path) -> bool { + shadow_path(config_path).is_file() || shadow_path(&config_path.with_extension("env")).is_file() +} + +fn read_toml_table( + config_path: &Path, +) -> Result<(String, toml::map::Map), GatewayError> { + let raw = std::fs::read_to_string(config_path) + .map_err(|source| GatewayError::ConfigWriteIo(Box::new(source)))?; + let value: toml::Value = toml::from_str(&raw) + .map_err(|error| GatewayError::ConfigWriteRejected(error.to_string()))?; + let toml::Value::Table(table) = value else { + return Err(GatewayError::ConfigWriteRejected( + "gateway.toml root must be a TOML table".to_owned(), + )); + }; + Ok((raw, table)) +} + +struct PreparedMutation { + disk_write: Option<(String, String)>, + validated: Config, +} + +fn prepare_enable( + config_path: &Path, + entries: Vec, +) -> Result<(EnableReply, PreparedMutation), GatewayError> { + let (raw_toml, mut root) = read_toml_table(config_path)?; + let mut mutated = false; + + let (endpoint_id, plan_ids, created_endpoint) = toml_ops::ensure_plan_endpoint(&mut root)?; + if created_endpoint { + mutated = true; + } + + let (mut taken_names, mut bound_upstreams) = toml_ops::collect_catalog_names(&root, &plan_ids); + let mut added = Vec::new(); + let mut renamed = Vec::new(); + let mut skipped = Vec::new(); + + for entry in entries { + let Some(context) = entry.context_window.filter(|&ctx| ctx > 0) else { + skipped.push(SkippedModel { + name: entry.slug, + reason: "missing_context_window", + }); + continue; + }; + if bound_upstreams.contains(&entry.slug) { + continue; + } + let chosen_name = if taken_names.contains(&entry.slug) { + let candidate = plan_collision_name(&entry.slug); + if taken_names.contains(&candidate) { + skipped.push(SkippedModel { + name: entry.slug, + reason: "name_taken", + }); + continue; + } + renamed.push(RenamedModel { + from: entry.slug.clone(), + to: candidate.clone(), + }); + candidate + } else { + entry.slug.clone() + }; + + let description = entry.display_name.unwrap_or_else(|| entry.slug.clone()); + toml_ops::push_plan_model( + &mut root, + &chosen_name, + &description, + context, + &entry.slug, + &endpoint_id, + )?; + taken_names.insert(chosen_name.clone()); + bound_upstreams.insert(entry.slug); + added.push(chosen_name); + mutated = true; + } + + let reply = EnableReply { + endpoint: endpoint_id, + added, + renamed, + skipped, + }; + let prepared = prepare_toml(config_path, raw_toml, mutated.then_some(root))?; + Ok((reply, prepared)) +} + +fn prepare_forget(config_path: &Path) -> Result<(ForgetReply, PreparedMutation), GatewayError> { + let (raw_toml, mut root) = read_toml_table(config_path)?; + let removed_endpoints = toml_ops::strip_plan_endpoints(&mut root); + if removed_endpoints.is_empty() { + let prepared = prepare_toml(config_path, raw_toml, None)?; + return Ok(( + ForgetReply { + removed_endpoints, + removed: Vec::new(), + kept: Vec::new(), + }, + prepared, + )); + } + + let removed_set: HashSet<&str> = removed_endpoints.iter().map(String::as_str).collect(); + let (removed, kept) = toml_ops::strip_plan_models(&mut root, &removed_set); + let prepared = prepare_toml(config_path, raw_toml, Some(root))?; + Ok(( + ForgetReply { + removed_endpoints, + removed, + kept, + }, + prepared, + )) +} + +fn prepare_toml( + config_path: &Path, + raw_before: String, + mutated_root: Option>, +) -> Result { + let pending_env = PendingEnv::new(config_path)?; + if let Some(root) = mutated_root { + let rendered = toml::to_string_pretty(&toml::Value::Table(root)) + .map_err(|error| GatewayError::ConfigWriteRejected(error.to_string()))?; + let validated = + Config::from_toml_with_lookup(&rendered, Some(config_path), &|name: &str| { + pending_env.resolve_var(name) + }) + .map_err(config_write_error)?; + Ok(PreparedMutation { + disk_write: Some((raw_before, rendered)), + validated, + }) + } else { + let validated = + Config::from_toml_with_lookup(&raw_before, Some(config_path), &|name: &str| { + pending_env.resolve_var(name) + }) + .map_err(config_write_error)?; + Ok(PreparedMutation { + disk_write: None, + validated, + }) + } +} + +fn write_config_files( + config_path: &Path, + raw_before: &str, + rendered: &str, +) -> Result<(), GatewayError> { + let prev = rollback_path(config_path); + if !prev.exists() { + write_atomic(&prev, raw_before).map_err(config_write_error)?; + } + write_atomic(config_path, rendered).map_err(config_write_error)?; + Ok(()) +} + +async fn commit_prepared( + state: &AppState, + config_path: PathBuf, + prepared: PreparedMutation, + fence_check: Option<(&ChatGptPlan, &SessionFence)>, +) -> Result<(), GatewayError> { + let mut live = state.live.write().await; + let active_name = live + .config + .active_profile() + .and_then(|profile| ProfileName::parse(profile.name()).ok()); + let selected = match active_name.as_ref() { + Some(name) => prepared + .validated + .select_profile(Some(name)) + .or_else(|_| prepared.validated.select_profile(None)), + None => prepared.validated.select_profile(None), + } + .map_err(config_write_error)?; + + let remote = Routing::from_config(&selected) + .map_err(|error| GatewayError::switch_failed("build-routing", error))?; + #[cfg(feature = "local")] + let routing = remote + .merge(live.local.models().iter().cloned()) + .map_err(|error| GatewayError::switch_failed("merge-routing", error))?; + #[cfg(not(feature = "local"))] + let routing = remote; + #[cfg(feature = "web-search")] + let web_search = selected + .web_search_config() + .map(WebSearchState::new) + .map(Arc::new); + + #[cfg(test)] + state.park_at(crate::park::Phase::ChatGptEnableCommit).await; + match (fence_check, prepared.disk_write) { + (Some((plan, fence)), Some((raw_before, rendered))) => { + let plan = plan.clone(); + let fence = fence.clone(); + blocking(move || { + plan.commit_fenced( + &fence, + || GatewayError::ChatGptSignInChanged, + || write_config_files(&config_path, &raw_before, &rendered), + ) + }) + .await??; + } + (Some((plan, fence)), None) => { + plan.commit_fenced(fence, || GatewayError::ChatGptSignInChanged, || Ok(()))?; + } + (None, Some((raw_before, rendered))) => { + blocking(move || write_config_files(&config_path, &raw_before, &rendered)).await??; + } + (None, None) => {} + } + + live.routing = Arc::new(routing); + live.config = Arc::new(selected); + #[cfg(feature = "web-search")] + { + live.web_search = web_search; + } + Ok(()) +} diff --git a/crates/gateway/app/src/admin/walled/chatgpt/tests.rs b/crates/gateway/app/src/admin/walled/chatgpt/tests.rs new file mode 100644 index 00000000..d7822ab8 --- /dev/null +++ b/crates/gateway/app/src/admin/walled/chatgpt/tests.rs @@ -0,0 +1,405 @@ +//! The `/admin/chatgpt/*` routes over real HTTP, against the real broker +//! and the strict fake issuer. + +use std::collections::HashMap; +use std::net::SocketAddr; + +use gateway_chatgpt_plan::fake_issuer::Fake; +use reqwest::{Method, StatusCode, Url}; +use serde_json::Value; + +use crate::AppState; +use crate::chatgpt::OWNER_PROOF_HEADER; +use crate::test_support::{loopback_state, serve_state, walled_fixture}; + +const ROUTES: [(Method, &str); 7] = [ + (Method::POST, "sign-in"), + (Method::GET, "status"), + (Method::POST, "sign-out"), + (Method::POST, "forget"), + (Method::POST, "reset"), + (Method::POST, "models/enable"), + (Method::POST, "models/forget"), +]; + +/// A served gateway whose broker is open on a fake issuer. +struct Rig { + fake: Fake, + _profile: tempfile::TempDir, + addr: SocketAddr, + owner_proof: String, + plan_proof: String, +} + +impl Rig { + async fn new() -> Rig { + let fake = Fake::start().await; + let (profile, state) = walled_fixture(); + state.chatgpt.open_on(profile.path(), fake.config()); + Rig::serve(fake, profile, state).await + } + + async fn serve(fake: Fake, profile: tempfile::TempDir, state: AppState) -> Rig { + let owner_proof = state.chatgpt.owner_proof().to_owned(); + let plan_proof = state.chatgpt.plan_proof().to_owned(); + Rig { + fake, + _profile: profile, + addr: serve_state(state).await, + owner_proof, + plan_proof, + } + } + + /// Calls a route as the owner: the bearer and this boot's owner proof. + async fn owner(&self, method: Method, route: &str) -> reqwest::Response { + call( + self.addr, + method, + route, + Some("test-token"), + &[&self.owner_proof], + ) + .await + } + + async fn status(&self) -> Value { + let response = self.owner(Method::GET, "status").await; + assert_eq!(response.status(), StatusCode::OK); + response.json().await.expect("a status object") + } + + /// Starts a sign-in, teaches the fake that attempt, and returns the + /// callback URL the browser would be sent back to. + async fn begin(&self) -> String { + let response = self.owner(Method::POST, "sign-in").await; + assert_eq!(response.status(), StatusCode::OK); + let reply: Value = response.json().await.expect("a sign-in reply"); + let authorize_url = reply["authorize_url"].as_str().expect("a url"); + assert!( + authorize_url.starts_with(&self.fake.base), + "{authorize_url}" + ); + let query: HashMap = Url::parse(authorize_url) + .expect("a url") + .query_pairs() + .into_owned() + .collect(); + self.fake + .set(|behavior| behavior.nonce.clone_from(&query["nonce"])); + self.fake + .expect_exchange(&query["code_challenge"], &query["redirect_uri"]); + let mut callback = Url::parse(&query["redirect_uri"]).expect("a redirect uri"); + callback + .query_pairs_mut() + .append_pair("state", &query["state"]) + .append_pair("code", "code-1") + .append_pair("client_id", "oaiapp_1"); + callback.into() + } +} + +/// Calls `/admin/chatgpt/` with an optional bearer and one owner +/// proof header per entry of `proofs`. +async fn call( + addr: SocketAddr, + method: Method, + route: &str, + bearer: Option<&str>, + proofs: &[&str], +) -> reqwest::Response { + let mut request = + reqwest::Client::new().request(method, format!("http://{addr}/admin/chatgpt/{route}")); + if let Some(bearer) = bearer { + request = request.bearer_auth(bearer); + } + for proof in proofs { + request = request.header(OWNER_PROOF_HEADER, *proof); + } + request.send().await.expect("the gateway answers") +} + +async fn error_code(response: reqwest::Response) -> String { + let body: Value = response.json().await.expect("an error envelope"); + body["error"]["code"] + .as_str() + .unwrap_or_default() + .to_owned() +} + +#[tokio::test] +async fn every_route_refuses_a_caller_without_the_owner_proof() { + let rig = Rig::new().await; + for (method, route) in ROUTES { + let wrong = "0".repeat(64); + let attempts: [&[&str]; 4] = [ + &[], + &[&wrong], + // The plan proof is not the owner proof. + &[&rig.plan_proof], + // A repeated header counts as none, even when both are right. + &[&rig.owner_proof, &rig.owner_proof], + ]; + for proofs in attempts { + let response = call(rig.addr, method.clone(), route, Some("test-token"), proofs).await; + assert_eq!( + response.status(), + StatusCode::FORBIDDEN, + "{route} {proofs:?}" + ); + assert_eq!( + error_code(response).await, + "owner_proof_required", + "{route}" + ); + } + } + assert_eq!(rig.fake.seen(|seen| seen.discovery_hits), 0); + assert_eq!(rig.status().await["sign_in_pending"], false); +} + +#[tokio::test] +async fn the_owner_proof_does_not_stand_in_for_authentication() { + // Strict bearer auth: no keyless loopback. + let state = loopback_state(Some(false)); + let owner_proof = state.chatgpt.owner_proof().to_owned(); + let addr = serve_state(state).await; + for (method, route) in ROUTES { + let response = call(addr, method, route, None, &[&owner_proof]).await; + assert_eq!(response.status(), StatusCode::UNAUTHORIZED, "{route}"); + } +} + +#[tokio::test] +async fn status_says_why_sign_in_is_unavailable() { + let (_temp, state) = walled_fixture(); + let owner_proof = state.chatgpt.owner_proof().to_owned(); + let unopened = serve_state(state.clone()).await; + let response = call( + unopened, + Method::GET, + "status", + Some("test-token"), + &[&owner_proof], + ) + .await; + let status: Value = response.json().await.expect("a status object"); + assert_eq!(status["available"], false); + assert_eq!(status["unavailable_code"], "starting"); + assert_eq!( + status["unavailable_reason"], + "the gateway has not finished starting" + ); + assert_eq!(status["plan_models"], serde_json::json!([])); + + state.chatgpt.unavailable("a stated reason"); + let addr = serve_state(state).await; + let response = call( + addr, + Method::GET, + "status", + Some("test-token"), + &[&owner_proof], + ) + .await; + let status: Value = response.json().await.expect("a status object"); + assert_eq!(status["available"], false); + assert_eq!(status["unavailable_code"], "storage_unavailable"); + assert_eq!(status["unavailable_reason"], "a stated reason"); + assert_eq!(status["status"], Value::Null); + assert_eq!(status["plan_models"], serde_json::json!([])); + + // Non-loopback and missing-profile-dir carry distinct codes for the UI. + let (_temp_lan, lan_state) = walled_fixture(); + let lan_proof = lan_state.chatgpt.owner_proof().to_owned(); + lan_state + .chatgpt + .start("0.0.0.0:8081".parse().expect("addr"), None); + let lan_addr = serve_state(lan_state).await; + let lan_status: Value = call( + lan_addr, + Method::GET, + "status", + Some("test-token"), + &[&lan_proof], + ) + .await + .json() + .await + .expect("status"); + assert_eq!(lan_status["unavailable_code"], "not_loopback"); + + let (_temp_nop, nop_state) = walled_fixture(); + let nop_proof = nop_state.chatgpt.owner_proof().to_owned(); + nop_state + .chatgpt + .start("127.0.0.1:8081".parse().expect("addr"), None); + let nop_addr = serve_state(nop_state).await; + let nop_status: Value = call( + nop_addr, + Method::GET, + "status", + Some("test-token"), + &[&nop_proof], + ) + .await + .json() + .await + .expect("status"); + assert_eq!(nop_status["unavailable_code"], "no_profile_dir"); + + for (method, route) in ROUTES { + if route == "status" { + continue; + } + let response = call(addr, method, route, Some("test-token"), &[&owner_proof]).await; + assert_eq!( + response.status(), + StatusCode::SERVICE_UNAVAILABLE, + "{route}" + ); + assert_eq!(error_code(response).await, "chatgpt_unavailable", "{route}"); + } +} + +#[tokio::test] +async fn a_fresh_profile_reports_nobody_signed_in() { + let rig = Rig::new().await; + let status = rig.status().await; + assert_eq!( + status, + serde_json::json!({ + "status": null, + "unreadable": false, + "email": null, + "plan_granted": false, + "revocation": null, + "cleanup_unconfirmed": false, + "sign_in_pending": false, + "available": true, + "unavailable_code": null, + "unavailable_reason": null, + "plan_models": [], + }) + ); + let response = rig.owner(Method::POST, "sign-out").await; + assert_eq!(response.status(), StatusCode::OK); + let reply: Value = response.json().await.expect("a reply"); + assert_eq!(reply, serde_json::json!({ "revoked": false })); + assert!(rig.fake.grants().is_empty()); +} + +#[tokio::test] +async fn a_sign_in_and_sign_out_run_end_to_end_through_the_routes() { + let rig = Rig::new().await; + let callback = rig.begin().await; + assert_eq!(rig.status().await["sign_in_pending"], true); + + // The browser lands on the one-shot listener. + let page = reqwest::get(&callback).await.expect("the listener answers"); + assert_eq!(page.status(), StatusCode::OK); + let page = page.text().await.expect("a page"); + + let status = rig.status().await; + assert_eq!(status["status"], "active", "{status}"); + assert_eq!(status["plan_granted"], true); + assert_eq!(status["sign_in_pending"], false); + assert!(status["email"].is_string(), "{status}"); + assert_eq!(rig.fake.grants(), ["authorization_code"]); + + // Nothing the routes or the page return holds a token. + let issued = rig.fake.seen(|seen| seen.valid_refresh_tokens.clone()); + assert!(!issued.is_empty()); + for token in &issued { + assert!(!status.to_string().contains(token) && !page.contains(token)); + } + + // The listener was one-shot: it is gone. + assert!(reqwest::get(&callback).await.is_err()); + + let response = rig.owner(Method::POST, "sign-out").await; + assert_eq!(response.status(), StatusCode::OK); + let reply: Value = response.json().await.expect("a reply"); + assert_eq!(reply, serde_json::json!({ "revoked": true })); + let status = rig.status().await; + assert_eq!(status["status"], "signed_out", "{status}"); + assert_eq!(status["revocation"], "confirmed"); + assert_eq!(rig.fake.seen(|seen| seen.revoked.len()), 1); +} + +#[tokio::test] +async fn a_second_sign_in_replaces_the_first() { + let rig = Rig::new().await; + let first = rig.begin().await; + let second = rig.begin().await; + assert_ne!(first, second); + + // The first attempt's listener closed when the second replaced it: its + // port stops answering, and it exchanges nothing. + let mut closed = false; + for _ in 0..200 { + closed = reqwest::get(&first).await.is_err(); + if closed { + break; + } + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + assert!(closed, "a replaced sign-in must not keep its port"); + assert!(rig.fake.grants().is_empty()); + + let page = reqwest::get(&second).await.expect("the listener answers"); + assert_eq!(page.status(), StatusCode::OK); + assert_eq!(rig.status().await["status"], "active"); + assert_eq!(rig.fake.grants(), ["authorization_code"]); +} + +#[tokio::test] +async fn reset_clears_the_profile_and_answers_no_content() { + let rig = Rig::new().await; + let callback = rig.begin().await; + reqwest::get(&callback).await.expect("the listener answers"); + assert_eq!(rig.status().await["status"], "active"); + + let response = rig.owner(Method::POST, "reset").await; + assert_eq!(response.status(), StatusCode::NO_CONTENT); + assert_eq!(rig.status().await["status"], Value::Null); +} + +#[tokio::test] +async fn unconfirmed_revocation_remains_visible_in_status_after_new_account_activates() { + let rig = Rig::new().await; + let first_cb = rig.begin().await; + assert_eq!( + reqwest::get(&first_cb).await.expect("cb").status(), + StatusCode::OK + ); + assert_eq!(rig.status().await["status"], "active"); + + rig.fake.set(|behavior| behavior.revoke_status = 503); + let forget = rig.owner(Method::POST, "forget").await; + assert_eq!(forget.status(), StatusCode::OK); + assert_eq!( + forget.json::().await.expect("json"), + serde_json::json!({ "revoked": false }) + ); + + rig.fake.set(|behavior| { + behavior.revoke_status = 200; + behavior.subject = "user-2".to_owned(); + }); + let second_cb = rig.begin().await; + assert_eq!( + reqwest::get(&second_cb).await.expect("cb").status(), + StatusCode::OK + ); + + let status = rig.status().await; + assert_eq!(status["status"], "active", "{status}"); + assert_eq!( + status["cleanup_unconfirmed"], true, + "an unconfirmed revocation from the retired account stays reported while the new account is active: {status}" + ); +} + +#[path = "models-tests.rs"] +mod models; diff --git a/crates/gateway/app/src/admin/walled/config-tests.rs b/crates/gateway/app/src/admin/walled/config-tests.rs index fdf93ff9..85e1b7fd 100644 --- a/crates/gateway/app/src/admin/walled/config-tests.rs +++ b/crates/gateway/app/src/admin/walled/config-tests.rs @@ -1,6 +1,6 @@ -use gateway_config::{Config, ProfileSelection, profile_state_path, shadow_path}; +use gateway_config::{Config, ProfileSelection, profile_state_path, shadow_path}; -use crate::test_support::{AdminPaths, serve_with_paths}; +use crate::test_support::{AdminPaths, app_state, serve_state, serve_with_paths}; const CONFIG: &str = r#" config-version = 0 @@ -56,22 +56,25 @@ fn fixture() -> (tempfile::TempDir, Config, AdminPaths) { (temp, config, paths) } -/// The live config as a save body: `GET /admin/config` also reports the -/// running `active_profile`, which is not a configuration key and never -/// goes back in a save. +/// The live config as a save body, carrying its `x-config-revision` +/// precondition and omitting `active_profile`. async fn save_body(addr: std::net::SocketAddr) -> serde_json::Value { - let mut body: serde_json::Value = reqwest::Client::new() + let response = reqwest::Client::new() .get(format!("http://{addr}/admin/config")) .bearer_auth("test-token") .send() .await - .expect("get sends") - .json() - .await - .expect("config json"); - body.as_object_mut() - .expect("the config is an object") - .remove("active_profile"); + .expect("get sends"); + let revision = response + .headers() + .get("x-config-revision") + .and_then(|v| v.to_str().ok()) + .expect("GET /admin/config exposes x-config-revision") + .to_owned(); + let mut body: serde_json::Value = response.json().await.expect("config json"); + let map = body.as_object_mut().expect("the config is an object"); + map.remove("active_profile"); + map.insert("revision".to_owned(), serde_json::Value::String(revision)); body } @@ -204,3 +207,250 @@ async fn adding_a_cloud_provider_without_its_key_is_rejected_and_stages_nothing( assert!(!shadow_path(&config_path).exists()); } + +async fn pending_revision(addr: std::net::SocketAddr) -> String { + let body: serde_json::Value = reqwest::Client::new() + .get(format!("http://{addr}/admin/config-pending")) + .bearer_auth("test-token") + .send() + .await + .expect("pending get sends") + .json() + .await + .expect("pending json"); + body["revision"] + .as_str() + .expect("pending reply carries revision") + .to_owned() +} + +#[tokio::test] +async fn stale_revision_refuses_save_with_409_without_clobbering_newer_disk_config() { + let (_temp, config, paths) = fixture(); + let config_path = paths.config_path.clone(); + let addr = serve_with_paths(config, paths).await; + + let get_resp = reqwest::Client::new() + .get(format!("http://{addr}/admin/config")) + .bearer_auth("test-token") + .send() + .await + .expect("get config"); + let header_rev = get_resp + .headers() + .get("x-config-revision") + .and_then(|v| v.to_str().ok()) + .expect("GET /admin/config exposes x-config-revision") + .to_owned(); + let etag = get_resp + .headers() + .get(reqwest::header::ETAG) + .and_then(|v| v.to_str().ok()) + .expect("GET /admin/config exposes ETag") + .to_owned(); + assert_eq!(header_rev.len(), 64, "SHA-256 hex revision"); + assert_eq!(etag, format!("\"{header_rev}\"")); + + let loaded_rev = pending_revision(addr).await; + assert_eq!(loaded_rev, header_rev); + + let mut stale_body = save_body(addr).await; + stale_body["model"][0]["description"] = serde_json::json!("edited from stale tab"); + stale_body["revision"] = serde_json::json!(loaded_rev); + + // Out-of-band update on disk (standing in for ChatGPT models/enable). + let updated_disk = format!( + "{CONFIG}\n[[endpoint]]\nid = \"chatgpt\"\nprotocol = \"openai-responses\"\nauth = \"chatgpt-plan\"\n\n\ + [[model]]\nname = \"gpt-5\"\ndescription = \"GPT-5\"\ncontext = 400000\nupstream = \"gpt-5\"\nendpoints = [\"chatgpt\"]\n" + ); + std::fs::write(&config_path, &updated_disk).expect("write updated config"); + + let response = put_config(addr, &stale_body).await; + assert_eq!(response.status(), reqwest::StatusCode::CONFLICT); + let error: serde_json::Value = response.json().await.expect("conflict json"); + assert_eq!(error["error"]["code"], "config_conflict"); + assert!( + error["error"]["message"] + .as_str() + .is_some_and(|msg| msg.contains("reload")), + "conflict message tells caller to reload: {error}" + ); + assert!( + !shadow_path(&config_path).exists(), + "stale save stages no shadow" + ); + assert!( + std::fs::read_to_string(&config_path) + .expect("read config") + .contains("name = \"gpt-5\""), + "plan model on disk survives untouched" + ); +} + +#[tokio::test] +async fn revision_tracks_pending_drafts_apply_revert_headers_and_invalid_inputs() { + let (_temp, config, paths) = fixture(); + let config_path = paths.config_path.clone(); + let state = app_state(config, Some(paths)); + let _worker = state.commands.spawn_worker(&state).expect("worker spawns"); + let addr = serve_state(state).await; + let client = reqwest::Client::new(); + + let rev0 = pending_revision(addr).await; + let mut first = save_body(addr).await; + first["model"][0]["description"] = serde_json::json!("draft one"); + first["revision"] = serde_json::json!(rev0); + + let ok1 = put_config(addr, &first).await; + assert_eq!(ok1.status(), reqwest::StatusCode::OK); + let shadow_toml = std::fs::read_to_string(shadow_path(&config_path)).expect("read shadow"); + assert!( + !shadow_toml.contains("revision"), + "wire revision precondition never enters gateway.toml.next" + ); + + let rev1 = pending_revision(addr).await; + assert_ne!(rev1, rev0, "staging a shadow advances pending revision"); + + // Re-sending rev0 now fails 409 because the pending draft moved on. + let stale_second = put_config(addr, &first).await; + assert_eq!(stale_second.status(), reqwest::StatusCode::CONFLICT); + + // Sending rev1 via If-Match header succeeds and advances to rev2. + let mut second = save_body(addr).await; + second.as_object_mut().expect("object").remove("revision"); + second["model"][0]["description"] = serde_json::json!("draft two"); + let ok2 = client + .put(format!("http://{addr}/admin/config")) + .bearer_auth("test-token") + .header(reqwest::header::IF_MATCH, format!("\"{rev1}\"")) + .json(&second) + .send() + .await + .expect("put with If-Match"); + assert_eq!(ok2.status(), reqwest::StatusCode::OK); + let rev2 = pending_revision(addr).await; + assert_ne!(rev2, rev1); + + // Revert removes the shadow and restores pending revision back to rev0. + let revert = client + .post(format!("http://{addr}/admin/config-revert")) + .bearer_auth("test-token") + .send() + .await + .expect("revert"); + assert_eq!(revert.status(), reqwest::StatusCode::OK); + assert_eq!(pending_revision(addr).await, rev0); + + // Stage again and apply: committed GET /admin/config revision advances to match. + second["revision"] = serde_json::json!(rev0); + assert_eq!( + put_config(addr, &second).await.status(), + reqwest::StatusCode::OK + ); + let staged_rev = pending_revision(addr).await; + let apply = client + .post(format!("http://{addr}/admin/config-apply")) + .bearer_auth("test-token") + .send() + .await + .expect("apply"); + assert_eq!(apply.status(), reqwest::StatusCode::OK); + assert_eq!(pending_revision(addr).await, staged_rev); + let get_after = client + .get(format!("http://{addr}/admin/config")) + .bearer_auth("test-token") + .send() + .await + .expect("get after apply"); + assert_eq!( + get_after.headers()["x-config-revision"].to_str().unwrap(), + staged_rev + ); + + // Malformed non-string or empty revision is rejected 422 without staging. + for bad in [serde_json::json!(123), serde_json::json!("")] { + let mut invalid = save_body(addr).await; + invalid["revision"] = bad; + let resp = put_config(addr, &invalid).await; + assert_eq!(resp.status(), reqwest::StatusCode::UNPROCESSABLE_ENTITY); + assert!(!shadow_path(&config_path).exists()); + } +} + +#[tokio::test] +async fn a_save_without_a_revision_precondition_is_refused_and_stages_nothing() { + let (_temp, config, paths) = fixture(); + let config_path = paths.config_path.clone(); + let addr = serve_with_paths(config, paths).await; + + let mut absent = save_body(addr).await; + absent.as_object_mut().expect("object").remove("revision"); + let mut explicit_null = absent.clone(); + explicit_null["revision"] = serde_json::Value::Null; + + for candidate in [&absent, &explicit_null] { + let resp = put_config(addr, candidate).await; + assert_eq!(resp.status(), reqwest::StatusCode::PRECONDITION_REQUIRED); + let err: serde_json::Value = resp.json().await.expect("error json"); + assert_eq!(err["error"]["code"], "config_revision_required"); + assert!(!shadow_path(&config_path).exists()); + } +} + +#[tokio::test] +async fn running_config_cannot_be_saved_over_an_out_of_band_disk_edit() { + let (_temp, config, paths) = fixture(); + let config_path = paths.config_path.clone(); + let addr = serve_with_paths(config, paths).await; + + let disk_b = CONFIG.replace("description = \"alpha\"", "description = \"gpt-5-on-disk\""); + std::fs::write(&config_path, &disk_b).expect("write out-of-band edit"); + + let client = reqwest::Client::new(); + let get_resp = client + .get(format!("http://{addr}/admin/config")) + .bearer_auth("test-token") + .send() + .await + .expect("get running config"); + let rev_a = crate::config_shadow::bytes_revision(CONFIG.as_bytes()); + let etag = get_resp.headers()[reqwest::header::ETAG] + .to_str() + .unwrap() + .to_owned(); + let x_rev = get_resp.headers()["x-config-revision"] + .to_str() + .unwrap() + .to_owned(); + assert_eq!(etag, format!("\"{rev_a}\"")); + assert_eq!(x_rev, rev_a); + + let mut running_body: serde_json::Value = get_resp.json().await.expect("running json"); + assert_eq!(running_body["model"][0]["description"], "alpha"); + running_body + .as_object_mut() + .expect("object") + .remove("active_profile"); + + let put_if_match = client + .put(format!("http://{addr}/admin/config")) + .bearer_auth("test-token") + .header(reqwest::header::IF_MATCH, &etag) + .json(&running_body) + .send() + .await + .expect("put with If-Match from GET /admin/config"); + assert_eq!(put_if_match.status(), reqwest::StatusCode::CONFLICT); + let err: serde_json::Value = put_if_match.json().await.expect("conflict json"); + assert_eq!(err["error"]["code"], "config_conflict"); + + running_body["revision"] = serde_json::Value::String(x_rev); + assert_eq!( + put_config(addr, &running_body).await.status(), + reqwest::StatusCode::CONFLICT + ); + assert!(!shadow_path(&config_path).exists()); + let disk_after = std::fs::read_to_string(&config_path).expect("read disk"); + assert!(disk_after.contains("gpt-5-on-disk")); +} diff --git a/crates/gateway/app/src/admin/walled/config.rs b/crates/gateway/app/src/admin/walled/config.rs index bb3ffd14..e936b009 100644 --- a/crates/gateway/app/src/admin/walled/config.rs +++ b/crates/gateway/app/src/admin/walled/config.rs @@ -14,7 +14,8 @@ use std::path::Path; use axum::extract::State; -use axum::http::Method; +use axum::http::{HeaderMap, HeaderValue, Method, header}; +use axum::response::{IntoResponse, Response}; use axum::routing::get; use axum::{Json, Router}; use gateway_config::save_config_shadow; @@ -22,10 +23,12 @@ use serde::Serialize; use crate::AppState; use crate::auth::LoopbackCaller; -use crate::config_shadow::PendingEnv; +use crate::config_shadow::{PendingEnv, bytes_revision, effective_config_revision}; use crate::error::{GatewayError, WireJson, blocking, config_write_error}; use crate::registry::RouteInfo; +pub(super) const REVISION_HEADER: &str = "x-config-revision"; + /// The reply of every shadow-write route: the shadow file the save /// staged, as a display path. #[derive(Debug, Serialize)] @@ -59,9 +62,26 @@ pub(super) fn routes() -> Router { async fn admin_config( State(state): State, _caller: LoopbackCaller, -) -> Result, GatewayError> { - let config = state.config().await; - Ok(Json(config.to_json())) +) -> Result { + let _publication = state.apply.lock().await; + let has_path = crate::admin::config_path(&state).is_ok(); + let (body, revision) = { + let live = state.live.read().await; + let rev = has_path + .then(|| live.config.source_bytes().map(bytes_revision)) + .flatten(); + (live.config.to_json(), rev) + }; + let mut response = Json(body).into_response(); + if let Some(rev) = revision { + if let Ok(etag) = HeaderValue::from_str(&format!("\"{rev}\"")) { + response.headers_mut().insert(header::ETAG, etag); + } + if let Ok(value) = HeaderValue::from_str(&rev) { + response.headers_mut().insert(REVISION_HEADER, value); + } + } + Ok(response) } /// The `PUT /admin/config` route: bearer-authed, stages the global config. @@ -72,11 +92,16 @@ async fn admin_config( /// atomically. The real file stays untouched and nothing reloads. The reply /// is `{"shadow": path}`. A body containing `active_profile` is rejected /// as a config-write error: selection belongs to -/// `POST /admin/switch-profile`. +/// `POST /admin/switch-profile`. Every save requires a non-empty `revision` +/// precondition (in the JSON body or via `If-Match` / `x-config-revision`); +/// omitting it replies `428 Precondition Required` (`config_revision_required`), +/// and a mismatch against the effective on-disk config replies `409 Conflict` +/// (`config_conflict`) without staging anything. async fn admin_put_config( State(state): State, _caller: LoopbackCaller, - WireJson(body): WireJson, + headers: HeaderMap, + WireJson(mut body): WireJson, ) -> Result, GatewayError> { // Saves take the apply lock: apply promotes shadows without // re-validating, so the combination it promotes must be one the latest @@ -84,8 +109,13 @@ async fn admin_put_config( // other. let _guard = state.apply.lock().await; let config_path = crate::admin::config_path(&state)?.to_path_buf(); + let expected_revision = extract_expected_revision(&headers, &mut body)?; let document = toml_document(body)?; let shadows = blocking(move || { + let current = effective_config_revision(&config_path)?; + if expected_revision != current { + return Err(GatewayError::ConfigConflict); + } let pending_env = PendingEnv::new(&config_path)?; save_config_shadow(&config_path, document, &|name: &str| { pending_env.resolve_var(name) @@ -96,6 +126,71 @@ async fn admin_put_config( Ok(Json(ShadowReply::staged(&shadows.config))) } +fn extract_expected_revision( + headers: &HeaderMap, + body: &mut serde_json::Value, +) -> Result { + let body_rev = match body.as_object_mut().and_then(|map| map.remove("revision")) { + None | Some(serde_json::Value::Null) => None, + Some(serde_json::Value::String(text)) if !text.trim().is_empty() => Some(text), + Some(_) => { + return Err(GatewayError::ConfigWriteRejected( + "revision must be a non-empty string".to_owned(), + )); + } + }; + let header_rev = extract_header_revision(headers)?; + if let (Some(from_body), Some(from_header)) = (&body_rev, &header_rev) + && from_body != from_header + { + return Err(GatewayError::ConfigWriteRejected( + "conflicting revision values in header and body".to_owned(), + )); + } + body_rev + .or(header_rev) + .ok_or(GatewayError::ConfigRevisionRequired) +} + +fn extract_header_revision(headers: &HeaderMap) -> Result, GatewayError> { + let custom = parse_revision_header(headers.get(REVISION_HEADER), false)?; + let if_match = parse_revision_header(headers.get(header::IF_MATCH), true)?; + if let (Some(a), Some(b)) = (&custom, &if_match) + && a != b + { + return Err(GatewayError::ConfigWriteRejected( + "conflicting revision headers".to_owned(), + )); + } + Ok(custom.or(if_match)) +} + +fn parse_revision_header( + raw: Option<&HeaderValue>, + strip_quotes: bool, +) -> Result, GatewayError> { + let Some(value) = raw else { + return Ok(None); + }; + let text = value + .to_str() + .map_err(|_| GatewayError::ConfigWriteRejected("invalid revision header".to_owned()))? + .trim(); + let cleaned = if strip_quotes { + text.strip_prefix('"') + .and_then(|rest| rest.strip_suffix('"')) + .unwrap_or(text) + } else { + text + }; + if cleaned.is_empty() { + return Err(GatewayError::ConfigWriteRejected( + "revision header must not be empty".to_owned(), + )); + } + Ok(Some(cleaned.to_owned())) +} + /// Converts the request body into the TOML document a shadow save takes. fn toml_document(body: serde_json::Value) -> Result { let value = json_to_toml(body)?.ok_or_else(|| { diff --git a/crates/gateway/app/src/admin/walled/config_apply-tests-plan.rs b/crates/gateway/app/src/admin/walled/config_apply-tests-plan.rs new file mode 100644 index 00000000..ac816768 --- /dev/null +++ b/crates/gateway/app/src/admin/walled/config_apply-tests-plan.rs @@ -0,0 +1,374 @@ +//! End-to-end admin config round-trips with `auth = "chatgpt-plan"` endpoints: +//! unchanged `GET -> PUT -> POST /admin/config-apply`, unrelated edits and +//! reverts, switching an ordinary endpoint to `chatgpt-plan` and back, and +//! profile selection (`POST /admin/switch-profile` + `GET /admin/config-pending`). + +use std::net::SocketAddr; +use std::path::Path; + +use gateway_config::{Config, EndpointAuth, ProfileSelection, profile_state_path, shadow_path}; +use reqwest::StatusCode; +use serde_json::{Value, json}; + +use super::{get_json, post, save_edited, serve_fixture}; +use crate::AppState; +use crate::routing::CredentialSource; +use crate::test_support::AdminPaths; + +const PLAN_CONFIG: &str = r#" +config-version = 0 + +[server] +bind = "127.0.0.1:0" +api_key = "test-token" + +[[endpoint]] +id = "plan" +protocol = "openai-responses" +auth = "chatgpt-plan" + +[[endpoint]] +id = "own" +protocol = "openai" +base_url = "http://127.0.0.1:9" +api_key = "own-secret" + +[[model]] +name = "plan-model" +description = "plan chat" +context = 8192 +upstream = "gpt-plan" +endpoints = ["plan"] + +[[model]] +name = "own-model" +description = "own chat" +context = 8192 +upstream = "gpt-own" +endpoints = ["own"] + +[[profile]] +name = "alpha" +models = [] + +[[profile]] +name = "beta" +models = [] +"#; + +fn plan_fixture() -> (tempfile::TempDir, Config, AdminPaths) { + let temp = tempfile::TempDir::new().expect("temp dir"); + let config_path = temp.path().join("gateway.toml"); + std::fs::write(&config_path, PLAN_CONFIG).expect("write config"); + std::fs::write( + profile_state_path(&config_path), + "active_profile = \"alpha\"\n", + ) + .expect("write state"); + let config = Config::load(&config_path, &ProfileSelection::default()).expect("load config"); + let paths = AdminPaths { + fixture_dir: temp.path().to_path_buf(), + active: "alpha".to_owned(), + config_path, + }; + (temp, config, paths) +} + +fn assert_plan_json(endpoint: &Value, expected_id: &str) { + let obj = endpoint.as_object().expect("endpoint is object"); + assert_eq!(obj.get("id"), Some(&json!(expected_id))); + assert_eq!(obj.get("protocol"), Some(&json!("openai-responses"))); + assert_eq!(obj.get("auth"), Some(&json!("chatgpt-plan"))); + assert!( + !obj.contains_key("base_url"), + "plan endpoint JSON must omit base_url: {obj:?}" + ); + assert!( + !obj.contains_key("api_key"), + "plan endpoint JSON must omit api_key: {obj:?}" + ); +} + +fn assert_plan_toml_on_disk(path: &Path, expected_id: &str) { + let raw = std::fs::read_to_string(path).expect("read config file"); + let parsed: toml::Value = toml::from_str(&raw).expect("parse TOML"); + let endpoints = parsed["endpoint"].as_array().expect("endpoint array"); + let plan = endpoints + .iter() + .find(|item| item.get("id").and_then(toml::Value::as_str) == Some(expected_id)) + .expect("plan endpoint in TOML") + .as_table() + .expect("endpoint table"); + assert_eq!( + plan.get("auth").and_then(toml::Value::as_str), + Some("chatgpt-plan") + ); + assert_eq!( + plan.get("protocol").and_then(toml::Value::as_str), + Some("openai-responses") + ); + assert!( + !plan.contains_key("base_url"), + "plan endpoint TOML must never gain base_url: {plan:?}" + ); + assert!( + !plan.contains_key("api_key"), + "plan endpoint TOML must never gain api_key: {plan:?}" + ); +} + +async fn listed_models(addr: SocketAddr) -> Vec { + let body = get_json(addr, "v1/models").await; + body["data"] + .as_array() + .expect("models data array") + .iter() + .map(|entry| entry["id"].as_str().expect("model id").to_owned()) + .collect() +} + +async fn endpoint_source(state: &AppState, model_name: &str) -> CredentialSource { + state + .live + .read() + .await + .routing + .model(model_name) + .expect("model routes") + .endpoint + .source +} + +#[tokio::test] +async fn get_put_unchanged_and_apply_round_trips_a_plan_endpoint_without_url_or_key() { + let (_temp, config, paths) = plan_fixture(); + let config_path = paths.config_path.clone(); + let (addr, state) = serve_fixture(config, paths).await; + + // 1. GET /admin/config emits the plan endpoint without base_url or api_key. + let get_resp = reqwest::Client::new() + .get(format!("http://{addr}/admin/config")) + .bearer_auth("test-token") + .send() + .await + .expect("get sends"); + let etag = get_resp.headers()[reqwest::header::ETAG] + .to_str() + .expect("ETag header") + .to_owned(); + let initial: serde_json::Value = get_resp.json().await.expect("json body"); + assert_plan_json(&initial["endpoint"][0], "plan"); + assert_eq!(initial["endpoint"][1]["api_key"], "***"); + + // 2. PUT the exact JSON back unchanged with its ETag precondition. + let put = reqwest::Client::new() + .put(format!("http://{addr}/admin/config")) + .bearer_auth("test-token") + .header(reqwest::header::IF_MATCH, etag) + .json(&initial) + .send() + .await + .expect("put sends"); + assert_eq!(put.status(), StatusCode::OK); + assert_plan_toml_on_disk(&shadow_path(&config_path), "plan"); + + // 3. GET /admin/config-pending and GET /admin/config-dirty stay clean on endpoint. + let pending = get_json(addr, "admin/config-pending").await; + assert_eq!(pending["profile"]["active_profile"], "alpha"); + assert_plan_json(&pending["profile"]["endpoint"][0], "plan"); + let dirty = get_json(addr, "admin/config-dirty").await; + assert_eq!(dirty["dirty"], true); + assert_eq!(dirty["pending_files"], json!(["gateway.toml"])); + + // 4. POST /admin/config-apply promotes the shadow and reloads routing. + let apply = post(addr, "admin/config-apply").await; + assert_eq!(apply.status(), StatusCode::OK); + assert!(!shadow_path(&config_path).exists()); + assert_plan_toml_on_disk(&config_path, "plan"); + + let reloaded = Config::load(&config_path, &ProfileSelection::default()) + .expect("promoted gateway.toml loads cleanly"); + assert_eq!(reloaded.endpoints()[0].auth(), EndpointAuth::ChatgptPlan); + assert!(reloaded.endpoints()[0].api_key().is_none()); + assert_eq!( + reloaded.endpoints()[1].api_key().expect("own key").expose(), + "own-secret" + ); + + assert_eq!( + endpoint_source(&state, "plan-model").await, + CredentialSource::ChatgptPlan + ); + assert_eq!(listed_models(addr).await, vec!["own-model"]); + let after = get_json(addr, "admin/config").await; + assert_plan_json(&after["endpoint"][0], "plan"); +} + +#[tokio::test] +async fn editing_unrelated_fields_and_reverting_or_applying_never_corrupts_plan_endpoint() { + let (_temp, config, paths) = plan_fixture(); + let config_path = paths.config_path.clone(); + let (addr, _state) = serve_fixture(config, paths).await; + + let first_save = save_edited(addr, |body| { + body["model"][1]["description"] = json!("edited own chat"); + }) + .await; + assert_eq!(first_save.status(), StatusCode::OK); + assert_plan_toml_on_disk(&shadow_path(&config_path), "plan"); + + let revert = post(addr, "admin/config-revert").await; + assert_eq!(revert.status(), StatusCode::OK); + assert!(!shadow_path(&config_path).exists()); + + let second_save = save_edited(addr, |body| { + body["model"][0]["description"] = json!("edited plan chat"); + body["local"]["cache_dir"] = json!("~/.runvil-alt"); + }) + .await; + assert_eq!(second_save.status(), StatusCode::OK); + let apply = post(addr, "admin/config-apply").await; + assert_eq!(apply.status(), StatusCode::OK); + assert_plan_toml_on_disk(&config_path, "plan"); + + let after = get_json(addr, "admin/config").await; + assert_eq!(after["model"][0]["description"], "edited plan chat"); + assert_plan_json(&after["endpoint"][0], "plan"); +} + +#[tokio::test] +async fn switching_ordinary_endpoint_to_plan_and_back_updates_live_gating_and_rejects_stale_redactions() + { + let (_temp, config, paths) = plan_fixture(); + let config_path = paths.config_path.clone(); + let (addr, state) = serve_fixture(config, paths).await; + assert_eq!(listed_models(addr).await, vec!["own-model"]); + + // Switching "own" to chatgpt-plan while leaving `"api_key": "***"` is refused. + let stale_redacted = save_edited(addr, |body| { + body["endpoint"][1]["protocol"] = json!("openai-responses"); + body["endpoint"][1]["auth"] = json!("chatgpt-plan"); + body["endpoint"][1] + .as_object_mut() + .expect("endpoint object") + .remove("base_url"); + }) + .await; + assert_eq!(stale_redacted.status(), StatusCode::UNPROCESSABLE_ENTITY); + assert!(!shadow_path(&config_path).exists()); + + // Switching "own" cleanly to chatgpt-plan drops base_url and api_key and gates own-model. + let to_plan = save_edited(addr, |body| { + body["endpoint"][1] = json!({ + "id": "own", + "protocol": "openai-responses", + "auth": "chatgpt-plan", + "dominion": null, + }); + }) + .await; + assert_eq!(to_plan.status(), StatusCode::OK); + assert_eq!( + post(addr, "admin/config-apply").await.status(), + StatusCode::OK + ); + assert_plan_toml_on_disk(&config_path, "own"); + assert_eq!( + endpoint_source(&state, "own-model").await, + CredentialSource::ChatgptPlan + ); + assert!(listed_models(addr).await.is_empty()); + + let gated_chat = reqwest::Client::new() + .post(format!("http://{addr}/v1/chat/completions")) + .bearer_auth("test-token") + .json(&json!({ + "model": "own-model", + "messages": [{ "role": "user", "content": "hi" }], + })) + .send() + .await + .expect("chat sends"); + assert_eq!(gated_chat.status(), StatusCode::FORBIDDEN); + let gated_err: Value = gated_chat.json().await.expect("error json"); + assert_eq!(gated_err["error"]["code"], "chatgpt_plan_runvil_only"); + + // Switching "own" back to ordinary with `"api_key": "***"` fails closed. + let bad_back = save_edited(addr, |body| { + body["endpoint"][1] = json!({ + "id": "own", + "protocol": "openai", + "base_url": "http://127.0.0.1:9", + "api_key": "***", + "dominion": null, + }); + }) + .await; + assert_eq!(bad_back.status(), StatusCode::UNPROCESSABLE_ENTITY); + + // Switching "own" back with explicit URL and key restores Configured routing. + let valid_back = save_edited(addr, |body| { + body["endpoint"][1] = json!({ + "id": "own", + "protocol": "openai", + "base_url": "http://127.0.0.1:9", + "api_key": "fresh-secret", + "dominion": null, + }); + }) + .await; + assert_eq!(valid_back.status(), StatusCode::OK); + assert_eq!( + post(addr, "admin/config-apply").await.status(), + StatusCode::OK + ); + assert_eq!( + endpoint_source(&state, "own-model").await, + CredentialSource::Configured + ); + assert_eq!(listed_models(addr).await, vec!["own-model"]); +} + +#[tokio::test] +async fn switch_profile_and_pending_view_preserve_plan_endpoint_across_selections() { + let (_temp, config, paths) = plan_fixture(); + let config_path = paths.config_path.clone(); + let (addr, _state) = serve_fixture(config, paths).await; + + let switch = reqwest::Client::new() + .post(format!("http://{addr}/admin/switch-profile")) + .bearer_auth("test-token") + .json(&json!({ "name": "beta" })) + .send() + .await + .expect("switch-profile sends"); + assert_eq!(switch.status(), StatusCode::OK); + let switch_body: Value = switch.json().await.expect("switch json"); + assert_eq!( + switch_body, + json!({ "profile": "beta", "restart_required": true }) + ); + + let pending = get_json(addr, "admin/config-pending").await; + assert_eq!(pending["profile"]["active_profile"], "beta"); + assert_plan_json(&pending["profile"]["endpoint"][0], "plan"); + + // Saving a new profile checklist while "beta" is selected keeps the plan endpoint clean. + let save = save_edited(addr, |body| { + body["profile"] + .as_array_mut() + .expect("profiles array") + .push(json!({ "name": "gamma", "models": [] })); + }) + .await; + assert_eq!(save.status(), StatusCode::OK); + assert_eq!( + post(addr, "admin/config-apply").await.status(), + StatusCode::OK + ); + assert_plan_toml_on_disk(&config_path, "plan"); + assert_eq!( + get_json(addr, "admin/profiles").await["profiles"], + json!(["alpha", "beta", "gamma"]) + ); +} diff --git a/crates/gateway/app/src/admin/walled/config_apply-tests.rs b/crates/gateway/app/src/admin/walled/config_apply-tests.rs index 81407bfb..ba965db3 100644 --- a/crates/gateway/app/src/admin/walled/config_apply-tests.rs +++ b/crates/gateway/app/src/admin/walled/config_apply-tests.rs @@ -13,6 +13,9 @@ mod in_flight; #[path = "config_apply-tests-rollback.rs"] mod rollback; +#[path = "config_apply-tests-plan.rs"] +mod plan; + const CONFIG: &str = r#" config-version = 0 @@ -116,11 +119,17 @@ async fn save_edited( addr: SocketAddr, edit: impl FnOnce(&mut serde_json::Value), ) -> reqwest::Response { + let pending = get_json(addr, "admin/config-pending").await; + let revision = pending["revision"] + .as_str() + .expect("pending view exposes revision") + .to_owned(); let mut body = get_json(addr, "admin/config").await; body.as_object_mut() .expect("the config is an object") .remove("active_profile"); edit(&mut body); + body["revision"] = serde_json::Value::String(revision); reqwest::Client::new() .put(format!("http://{addr}/admin/config")) .bearer_auth("test-token") diff --git a/crates/gateway/app/src/admin/walled/config_pending-tests.rs b/crates/gateway/app/src/admin/walled/config_pending-tests.rs index c4cd2039..b983e731 100644 --- a/crates/gateway/app/src/admin/walled/config_pending-tests.rs +++ b/crates/gateway/app/src/admin/walled/config_pending-tests.rs @@ -1,7 +1,10 @@ -use gateway_config::{Config, ProfileSelection, profile_state_path, shadow_path, write_shadow}; +use std::sync::Arc; + +use gateway_config::{Config, ProfileSelection, profile_state_path, shadow_path, write_shadow}; use super::*; -use crate::test_support::{AdminPaths, serve_with_paths}; +use crate::park::{Phase, PhasePark}; +use crate::test_support::{AdminPaths, app_state, serve_state, serve_with_paths}; const PROFILE_CONFIG: &str = r#" config-version = 0 @@ -164,3 +167,65 @@ fn a_live_reloadable_change_needs_no_restart() { assert!(reply.dirty); assert!(!reply.restart_required); } + +#[tokio::test] +async fn pending_read_parses_and_hashes_one_captured_byte_snapshot() { + let temp = tempfile::TempDir::new().expect("temp dir"); + let config_path = temp.path().join("gateway.toml"); + std::fs::write(&config_path, PROFILE_CONFIG).expect("write initial config"); + let config = Config::load(&config_path, &ProfileSelection::default()).expect("load config"); + let mut state = app_state( + config, + Some(AdminPaths { + fixture_dir: temp.path().to_path_buf(), + active: "alpha".to_owned(), + config_path: config_path.clone(), + }), + ); + let park = Arc::new(PhasePark::at(Phase::PendingConfigAfterRead)); + state.park = Some(Arc::clone(&park)); + let addr = serve_state(state).await; + + let client = reqwest::Client::new(); + let get_task = tokio::spawn({ + let client = client.clone(); + async move { + client + .get(format!("http://{addr}/admin/config-pending")) + .bearer_auth("test-token") + .send() + .await + .expect("pending request sends") + } + }); + park.entered().await; + + let disk_b = format!("{PROFILE_CONFIG}\n[[profile]]\nname = \"gamma\"\nmodels = []\n"); + std::fs::write(&config_path, &disk_b).expect("overwrite disk mid-read"); + park.release(); + + let resp = get_task.await.expect("pending task joins"); + assert_eq!(resp.status(), reqwest::StatusCode::OK); + let rev_a = bytes_revision(PROFILE_CONFIG.as_bytes()); + assert_eq!( + resp.headers()[reqwest::header::ETAG].to_str().unwrap(), + format!("\"{rev_a}\"") + ); + assert_eq!(resp.headers()[REVISION_HEADER].to_str().unwrap(), rev_a); + let mut body: serde_json::Value = resp.json().await.expect("pending json"); + assert_eq!(body["revision"], rev_a); + assert_eq!(body["profile"]["profile"].as_array().unwrap().len(), 2); + + let mut save = body["profile"].take(); + save.as_object_mut().unwrap().remove("active_profile"); + save["revision"] = serde_json::Value::String(rev_a); + let put_resp = client + .put(format!("http://{addr}/admin/config")) + .bearer_auth("test-token") + .json(&save) + .send() + .await + .expect("put sends"); + assert_eq!(put_resp.status(), reqwest::StatusCode::CONFLICT); + assert!(!shadow_path(&config_path).exists()); +} diff --git a/crates/gateway/app/src/admin/walled/config_pending.rs b/crates/gateway/app/src/admin/walled/config_pending.rs index 78181d89..77ad2a56 100644 --- a/crates/gateway/app/src/admin/walled/config_pending.rs +++ b/crates/gateway/app/src/admin/walled/config_pending.rs @@ -1,4 +1,4 @@ -//! Pending-state read routes: `GET /admin/config-pending` and +//! Pending-state read routes: `GET /admin/config-pending` and //! `GET /admin/config-dirty`. //! //! The write route (`config.rs`) stages the global config as a @@ -14,17 +14,20 @@ use std::path::Path; use axum::extract::State; -use axum::http::Method; +use axum::http::{HeaderValue, Method, header}; +use axum::response::{IntoResponse, Response}; use axum::routing::get; use axum::{Json, Router}; use gateway_config::{ - Config, ProfileSelection, ProfileState, load_pending_config, profile_state_path, + Config, ProfileSelection, ProfileState, load_pending_config, parse_pending_config_text, + profile_state_path, read_pending_or_real_text, }; use serde::Serialize; +use super::config::REVISION_HEADER; use crate::AppState; use crate::auth::LoopbackCaller; -use crate::config_shadow::{PendingEnv, ShadowCensus, config_root, relative_name}; +use crate::config_shadow::{PendingEnv, ShadowCensus, bytes_revision, config_root, relative_name}; use crate::error::{GatewayError, blocking, pending_read_error}; use crate::registry::RouteInfo; @@ -36,6 +39,9 @@ struct PendingReply { profile: serde_json::Value, /// Always `null`: the boot side has no pending view of its own. boot: Option, + /// Lowercase SHA-256 hex digest of the effective on-disk config bytes + /// (`gateway.toml.next` when staged, else `gateway.toml`). + revision: String, } /// The `GET /admin/config-dirty` reply. @@ -79,12 +85,30 @@ pub(super) fn routes() -> Router { async fn admin_config_pending( State(state): State, _caller: LoopbackCaller, -) -> Result, GatewayError> { +) -> Result { let _publication = state.apply.lock().await; let config_path = crate::admin::config_path(&state)?.to_path_buf(); let running_profile = state.profile_name().await; + let path_for_read = config_path.clone(); + let (source_path, raw) = blocking(move || { + read_pending_or_real_text(&path_for_read).map_err(|error| pending_read_error(&error)) + }) + .await??; + #[cfg(test)] + state + .park_at(crate::park::Phase::PendingConfigAfterRead) + .await; let reply = blocking(move || { - let config = load_pending_for_running(&config_path, running_profile.as_deref())?; + let pending_env = PendingEnv::new(&config_path)?; + let config = parse_pending_config_text( + &config_path, + &source_path, + &raw, + &ProfileSelection::new(running_profile.as_deref(), None), + &|name: &str| pending_env.resolve_var(name), + ) + .map_err(|error| pending_read_error(&error))?; + let revision = bytes_revision(raw.as_bytes()); let mut profile = config.to_json(); if let Some(table) = profile.as_object_mut() { table.insert( @@ -97,10 +121,19 @@ async fn admin_config_pending( Ok::<_, GatewayError>(PendingReply { profile, boot: None, + revision, }) }) .await??; - Ok(Json(reply)) + let rev = reply.revision.clone(); + let mut response = Json(reply).into_response(); + if let Ok(etag) = HeaderValue::from_str(&format!("\"{rev}\"")) { + response.headers_mut().insert(header::ETAG, etag); + } + if let Ok(value) = HeaderValue::from_str(&rev) { + response.headers_mut().insert(REVISION_HEADER, value); + } + Ok(response) } /// Loads the shadow-preferred config under the running selection, which diff --git a/crates/gateway/app/src/auth.rs b/crates/gateway/app/src/auth.rs index f5e5c302..9d442b56 100644 --- a/crates/gateway/app/src/auth.rs +++ b/crates/gateway/app/src/auth.rs @@ -155,6 +155,9 @@ impl FromRequestParts for LoopbackCaller { } } +mod owner; +pub(crate) use owner::OwnerCaller; + #[cfg(test)] mod secret_tests; diff --git a/crates/gateway/app/src/auth/owner.rs b/crates/gateway/app/src/auth/owner.rs new file mode 100644 index 00000000..b4c470e3 --- /dev/null +++ b/crates/gateway/app/src/auth/owner.rs @@ -0,0 +1,35 @@ +//! [`OwnerCaller`]: a walled-tier caller that also presents this boot's +//! owner proof. + +use axum::extract::FromRequestParts; +use axum::http::request::Parts; +use axum::response::{IntoResponse, Response}; + +use super::LoopbackCaller; +use crate::AppState; +use crate::error::GatewayError; + +/// A [`LoopbackCaller`] that presented the owner proof: the extractor every +/// handler takes that manages what holds a ChatGPT plan's credential. +/// +/// The proof comes from the private file only Runvil's own programs read. +/// The shared key alone does not pass, so a tool that was handed that key +/// to call models cannot sign in, sign out, or read the sign-in's status. +/// The order is the wall's: a non-loopback peer is refused first, then +/// ordinary authentication runs, then the proof is checked. +pub(crate) struct OwnerCaller; + +impl FromRequestParts for OwnerCaller { + type Rejection = Response; + + async fn from_request_parts( + parts: &mut Parts, + state: &AppState, + ) -> Result { + let _caller = LoopbackCaller::from_request_parts(parts, state).await?; + if !state.chatgpt.owner_proof_presented(&parts.headers) { + return Err(GatewayError::OwnerProofRequired.into_response()); + } + Ok(OwnerCaller) + } +} diff --git a/crates/gateway/app/src/boot/boot_speech_tests-persistence.rs b/crates/gateway/app/src/boot/boot_speech_tests-persistence.rs index 578eff03..0a891b7c 100644 --- a/crates/gateway/app/src/boot/boot_speech_tests-persistence.rs +++ b/crates/gateway/app/src/boot/boot_speech_tests-persistence.rs @@ -107,9 +107,23 @@ async fn a_later_switch_persists_b_while_boot_speech_a_keeps_serving() { restarted.speech.shutdown(); } +async fn get_config_with_etag(addr: std::net::SocketAddr) -> (serde_json::Value, String) { + let resp = reqwest::Client::new() + .get(format!("http://{addr}/admin/config")) + .bearer_auth("test-token") + .send() + .await + .expect("get /admin/config sends"); + let etag = resp.headers()[reqwest::header::ETAG] + .to_str() + .expect("ETag header") + .to_owned(); + (resp.json().await.expect("config json"), etag) +} + /// The document `GET /admin/config` serves is accepted verbatim by -/// `PUT /admin/config`: the running document has no -/// `active_profile` key for the save route to refuse. +/// `PUT /admin/config` when paired with its `ETag`: the running document +/// has no `active_profile` key for the save route to refuse. #[tokio::test] async fn the_served_config_round_trips_through_the_save_route() { let backend = fake_chat_backend().await; @@ -117,7 +131,7 @@ async fn the_served_config_round_trips_through_the_save_route() { let (config, paths) = persisted_catalog(&temp, backend, "alpha"); let addr = serve_state(boot_state_with_paths(config, paths)).await; - let document = get_json(addr, "/admin/config").await; + let (document, etag) = get_config_with_etag(addr).await; assert!( document.get("active_profile").is_none(), "the running document omits the active_profile key: {document}" @@ -125,6 +139,7 @@ async fn the_served_config_round_trips_through_the_save_route() { let save = reqwest::Client::new() .put(format!("http://{addr}/admin/config")) .bearer_auth("test-token") + .header(reqwest::header::IF_MATCH, etag) .json(&document) .send() .await @@ -166,11 +181,12 @@ async fn an_apply_persisting_speech_changes_leaves_boot_speech_untouched() { assert!(outcome.is_ok(), "boot A loads: {outcome:?}"); // Stage an STT vocabulary change through the real save route. - let mut document = get_json(addr, "/admin/config").await; + let (mut document, etag) = get_config_with_etag(addr).await; document["stt"]["vocabulary"] = serde_json::json!(["beta-words"]); let save = reqwest::Client::new() .put(format!("http://{addr}/admin/config")) .bearer_auth("test-token") + .header(reqwest::header::IF_MATCH, etag) .json(&document) .send() .await diff --git a/crates/gateway/app/src/boot/provisioning_tests.rs b/crates/gateway/app/src/boot/provisioning_tests.rs index c579dd03..d792c5c3 100644 --- a/crates/gateway/app/src/boot/provisioning_tests.rs +++ b/crates/gateway/app/src/boot/provisioning_tests.rs @@ -96,12 +96,15 @@ async fn a_routing_miss_on_a_loading_model_is_model_loading_not_not_found() { .await .loading .insert("pending-model".to_owned()); - let loading = crate::relay::resolve_routed_model(&state, "pending-model").await; + let loading = + crate::relay::resolve_routed_model(&state, &axum::http::HeaderMap::new(), "pending-model") + .await; assert!( matches!(&loading, Err(crate::error::GatewayError::ModelLoading(name)) if name == "pending-model"), "a loading model resolves to ModelLoading: {loading:?}" ); - let unknown = crate::relay::resolve_routed_model(&state, "ghost").await; + let unknown = + crate::relay::resolve_routed_model(&state, &axum::http::HeaderMap::new(), "ghost").await; assert!( matches!(unknown, Err(crate::error::GatewayError::UnknownModel(_))), "a name outside loading keeps its 404: {unknown:?}" diff --git a/crates/gateway/app/src/chatgpt-models.rs b/crates/gateway/app/src/chatgpt-models.rs new file mode 100644 index 00000000..1ed787b6 --- /dev/null +++ b/crates/gateway/app/src/chatgpt-models.rs @@ -0,0 +1,184 @@ +//! Fetching and projecting the signed-in account's ChatGPT plan model catalog +//! from `{api_base}/models`. +//! +//! Only `visibility == "list"` entries are kept, projected onto the closed +//! triple `(slug, display_name, context_window)`. Redirects are never +//! followed, bodies are capped at [`MAX_MODELS_BODY`], and upstream headers, +//! extra JSON fields, hidden slugs, and error bodies never enter errors or +//! logs. + +use std::io; +use std::time::Duration; + +use gateway_chatgpt_plan::plan::{ChatGptPlan, SessionFence}; +use gateway_protocol::ProtocolError; +use gateway_protocol::http_util::read_bytes_capped; +use reqwest::StatusCode; +use serde::Deserialize; + +use crate::error::GatewayError; + +/// Maximum bytes accepted from `{api_base}/models` (1 MiB). +const MAX_MODELS_BODY: usize = 1024 * 1024; +const CONNECT_TIMEOUT: Duration = Duration::from_secs(10); +const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); + +/// Closed projection of one user-visible model from OpenAI's `/models` reply. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct PlanModelEntry { + /// Upstream model identifier (`slug`). + pub(crate) slug: String, + /// Human-readable label when supplied and non-empty. + pub(crate) display_name: Option, + /// Context window in tokens when supplied. + pub(crate) context_window: Option, +} + +#[derive(Deserialize)] +struct ModelsEnvelope { + models: Vec, +} + +#[derive(Deserialize)] +struct RawPlanModel { + slug: String, + #[serde(default)] + display_name: Option, + #[serde(default)] + visibility: Option, + #[serde(default)] + context_window: Option, +} + +/// Builds the dedicated HTTP client for `{api_base}/models`: bounded timeouts +/// and `Policy::none()` so the plan bearer never follows a redirect. Returns +/// `None` if construction fails instead of falling back to a default client +/// whose redirect policy would follow redirects. +pub(super) fn models_http_client() -> Option { + let builder = || { + reqwest::Client::builder() + .connect_timeout(CONNECT_TIMEOUT) + .timeout(REQUEST_TIMEOUT) + .redirect(reqwest::redirect::Policy::none()) + }; + builder().build().or_else(|_| builder().build()).ok() +} + +/// Snapshots a [`SessionFence`] on `plan`, fetches `{api_base}/models` with +/// the plan bearer (retrying once on `401` after evicting the cached token), +/// and returns the fence plus the projected `visibility == "list"` models. +pub(super) async fn fetch( + client: &reqwest::Client, + api_base: &str, + plan: &ChatGptPlan, +) -> Result<(SessionFence, Vec), GatewayError> { + let fence = plan.current_fence()?; + let url = format!("{}/models", api_base.trim_end_matches('/')); + + let bytes = fetch_bytes(client, &url, plan).await?; + let envelope: ModelsEnvelope = serde_json::from_slice(&bytes) + .map_err(|_| protocol_decode_error("invalid /models JSON payload"))?; + + let mut projected = Vec::new(); + for item in envelope.models { + if item.visibility.as_deref() != Some("list") { + continue; + } + let Some(slug) = clean_slug(&item.slug) else { + continue; + }; + projected.push(PlanModelEntry { + slug, + display_name: item.display_name.as_deref().and_then(clean_display_name), + context_window: item.context_window, + }); + } + Ok((fence, projected)) +} + +async fn fetch_bytes( + client: &reqwest::Client, + url: &str, + plan: &ChatGptPlan, +) -> Result, GatewayError> { + for attempt in 0..2 { + let token = plan.access_token().await?; + let response = client + .get(url) + .bearer_auth(token.expose()) + .send() + .await + .map_err(|error| { + if error.is_connect() { + GatewayError::Protocol(ProtocolError::connect(io::Error::new( + io::ErrorKind::ConnectionRefused, + "could not connect to ChatGPT models endpoint", + ))) + } else { + GatewayError::Protocol(ProtocolError::transport(io::Error::other( + "ChatGPT models request failed", + ))) + } + })?; + + let status = response.status(); + if status == StatusCode::UNAUTHORIZED && attempt == 0 { + plan.reject_access(&token); + continue; + } + if !status.is_success() { + return Err(ProtocolError::UpstreamStatus { + status: status.as_u16(), + body: String::new(), + } + .into()); + } + if response + .content_length() + .is_some_and(|announced| announced > MAX_MODELS_BODY as u64) + { + return Err(protocol_decode_error("/models body exceeds size cap")); + } + let bytes = read_bytes_capped(response, MAX_MODELS_BODY + 1) + .await + .map_err(|_| { + GatewayError::Protocol(ProtocolError::transport(io::Error::other( + "failed reading ChatGPT models response", + ))) + })?; + if bytes.len() > MAX_MODELS_BODY { + return Err(protocol_decode_error("/models body exceeds size cap")); + } + return Ok(bytes); + } + Err(ProtocolError::UpstreamStatus { + status: StatusCode::UNAUTHORIZED.as_u16(), + body: String::new(), + } + .into()) +} + +fn protocol_decode_error(reason: &'static str) -> GatewayError { + ProtocolError::UpstreamProtocol(Box::new(io::Error::new(io::ErrorKind::InvalidData, reason))) + .into() +} + +fn clean_slug(raw: &str) -> Option { + let trimmed = raw.trim(); + let ok = !trimmed.is_empty() + && trimmed.len() <= 128 + && trimmed + .chars() + .all(|c| c.is_ascii_alphanumeric() || "-_.:".contains(c)); + ok.then(|| trimmed.to_owned()) +} + +fn clean_display_name(raw: &str) -> Option { + let cleaned: String = raw + .trim() + .chars() + .filter(|c| !c.is_control()) + .take(256) + .collect(); + (!cleaned.is_empty()).then_some(cleaned) +} diff --git a/crates/gateway/app/src/chatgpt-tests-visibility.rs b/crates/gateway/app/src/chatgpt-tests-visibility.rs new file mode 100644 index 00000000..bbff4ac1 --- /dev/null +++ b/crates/gateway/app/src/chatgpt-tests-visibility.rs @@ -0,0 +1,153 @@ +//! Broker status gating for `plan_visible` and `authorize_plan`: signed-out, +//! non-granted, expired, and unreadable sessions hide plan models from +//! `GET /v1/models` and refuse inference before touching the network. + +use reqwest::StatusCode; + +use super::{OWNER_PROOF_HEADER, Rig, chat, every_route, listed, post, refusal}; + +#[tokio::test] +async fn signing_out_hides_plan_models_and_refuses_every_route_without_network() { + let rig = Rig::new().await; + let addr = rig.serve().await; + let proof = rig.plan_proof(); + + // Never signed in: every inference route refuses before kind checks or + // backend/issuer calls, and listing omits the plan model. + assert_eq!(listed(addr, &[&proof]).await, ["own-sol"]); + for (path, body) in every_route("sol") { + let outcome = refusal(post(addr, path, &body, &[&proof]).await).await; + assert_eq!( + outcome, + (StatusCode::FORBIDDEN, "chatgpt_sign_in_required".to_owned()), + "{path}" + ); + } + assert!(rig.fake.grants().is_empty()); + assert!(rig.sent().is_empty()); + + // Signed in with plan granted: listed again. + rig.sign_in(addr).await; + assert_eq!(listed(addr, &[&proof]).await, ["sol", "own-sol"]); + + // Sign out: plan model disappears without editing config, and requests + // get the sign-in-required code with zero backend traffic. + let sign_out = reqwest::Client::new() + .post(format!("http://{addr}/admin/chatgpt/sign-out")) + .bearer_auth("test-token") + .header(OWNER_PROOF_HEADER, rig.state.chatgpt.owner_proof()) + .send() + .await + .expect("the gateway answers"); + assert_eq!(sign_out.status(), StatusCode::OK); + + let grants_after_sign_out = rig.fake.grants(); + assert_eq!(listed(addr, &[&proof]).await, ["own-sol"]); + for (path, body) in every_route("sol") { + let outcome = refusal(post(addr, path, &body, &[&proof]).await).await; + assert_eq!( + outcome, + (StatusCode::FORBIDDEN, "chatgpt_sign_in_required".to_owned()), + "{path}" + ); + } + assert_eq!(rig.fake.grants(), grants_after_sign_out); + assert!(rig.sent().is_empty()); +} + +#[tokio::test] +async fn a_sign_in_without_plan_grant_neither_lists_nor_serves_plan_models() { + let rig = Rig::new().await; + rig.fake.set(|behavior| { + behavior.scope = "openid profile email offline_access resource.invoke".to_owned(); + }); + let addr = rig.serve().await; + rig.sign_in(addr).await; + let proof = rig.plan_proof(); + + let grants_before = rig.fake.grants(); + assert_eq!(listed(addr, &[&proof]).await, ["own-sol"]); + for (path, body) in every_route("sol") { + let outcome = refusal(post(addr, path, &body, &[&proof]).await).await; + assert_eq!( + outcome, + (StatusCode::FORBIDDEN, "chatgpt_plan_not_granted".to_owned()), + "{path}" + ); + } + assert_eq!(rig.fake.grants(), grants_before); + assert!(rig.sent().is_empty()); +} + +#[tokio::test] +async fn expired_and_unreadable_records_hide_plan_models_and_report_expired() { + let rig = Rig::new().await; + // Within the 120s refresh margin so the ID token verifies at sign-in while + // the next `access_token()` call must refresh immediately. + rig.fake.set(|behavior| behavior.expires_in = 60); + let addr = rig.serve().await; + rig.sign_in(addr).await; + let proof = rig.plan_proof(); + + // Force a refresh failure so the broker settles in NeedsAuth. + rig.fake + .set(|behavior| behavior.token_error = Some((400, "invalid_grant"))); + let _ = rig + .state + .chatgpt + .broker() + .expect("open") + .access_token() + .await; + + let grants_in_needs_auth = rig.fake.grants(); + assert_eq!(listed(addr, &[&proof]).await, ["own-sol"]); + let outcome = refusal(post(addr, "/v1/chat/completions", &chat("sol"), &[&proof]).await).await; + assert_eq!( + outcome, + (StatusCode::FORBIDDEN, "chatgpt_sign_in_expired".to_owned()) + ); + assert_eq!( + rig.fake.grants(), + grants_in_needs_auth, + "authorize_plan rejects from in-memory status without refreshing" + ); + assert!(rig.sent().is_empty()); + + // An unreadable store file on a fresh start behaves the same way. + let corrupt_rig = Rig::new().await; + let store_dir = corrupt_rig.profile.path().join("chatgpt-plan"); + std::fs::create_dir_all(&store_dir).expect("mkdir"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt as _; + std::fs::set_permissions(&store_dir, std::fs::Permissions::from_mode(0o700)) + .expect("chmod dir"); + } + let record_file = store_dir.join("chatgpt-plan.json"); + std::fs::write(&record_file, b"{corrupt").expect("write corrupt"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt as _; + std::fs::set_permissions(&record_file, std::fs::Permissions::from_mode(0o600)) + .expect("chmod file"); + } + let corrupt_addr = corrupt_rig.serve().await; + let corrupt_proof = corrupt_rig.plan_proof(); + assert_eq!(listed(corrupt_addr, &[&corrupt_proof]).await, ["own-sol"]); + let outcome = refusal( + post( + corrupt_addr, + "/v1/chat/completions", + &chat("sol"), + &[&corrupt_proof], + ) + .await, + ) + .await; + assert_eq!( + outcome, + (StatusCode::FORBIDDEN, "chatgpt_sign_in_expired".to_owned()) + ); + assert!(corrupt_rig.fake.grants().is_empty()); +} diff --git a/crates/gateway/app/src/chatgpt-tests.rs b/crates/gateway/app/src/chatgpt-tests.rs new file mode 100644 index 00000000..53d146c9 --- /dev/null +++ b/crates/gateway/app/src/chatgpt-tests.rs @@ -0,0 +1,477 @@ +//! The gate on plan-backed models, over real HTTP: who may use one, who may +//! see one, and what a plan request carries when it is let through. + +use std::collections::HashMap; +use std::net::SocketAddr; +use std::sync::{Arc, Mutex}; + +use axum::http::HeaderMap; +use gateway_chatgpt_plan::fake_issuer::Fake; +use gateway_config::Config; +use reqwest::{StatusCode, Url}; +use serde_json::{Value, json}; + +use super::{ChatGpt, OWNER_PROOF_HEADER, PLAN_PROOF_HEADER}; +use crate::AppState; +use crate::routing::CredentialSource; +use crate::test_support::{app_state, serve_state}; + +/// One request a fake backend received: its `Authorization` header and body. +type Received = Arc>>; + +/// A backend that records each `POST /responses` and answers 401, so a +/// test reads what was sent without imitating a model. +async fn recording_backend() -> (String, Received) { + let received: Received = Arc::default(); + let record = { + let received = Arc::clone(&received); + move |headers: HeaderMap, axum::Json(body): axum::Json| { + let authorization = headers + .get("authorization") + .and_then(|value| value.to_str().ok()) + .unwrap_or_default() + .to_owned(); + received.lock().expect("lock").push((authorization, body)); + std::future::ready(( + StatusCode::UNAUTHORIZED, + axum::Json(json!({ "error": { "message": "no", "code": "invalid_api_key" } })), + )) + } + }; + let listener = tokio::net::TcpListener::bind("127.0.0.1:0") + .await + .expect("binds"); + let addr = listener.local_addr().expect("an address"); + tokio::spawn(async move { + let router = axum::Router::new().route("/responses", axum::routing::post(record)); + let _ignored = axum::serve(listener, router).await; + }); + (format!("http://{addr}"), received) +} + +/// A gateway with two models that share one upstream name: `sol` on a +/// ChatGPT plan, `own-sol` on an ordinary API key. +struct Rig { + fake: Fake, + profile: tempfile::TempDir, + state: AppState, + received: Received, +} + +impl Rig { + async fn new() -> Rig { + let (backend, received) = recording_backend().await; + let config = Config::from_toml_str(&format!( + r#" +config-version = 0 +[server] +bind = "127.0.0.1:0" +api_key = "test-token" + +[[endpoint]] +id = "plan" +protocol = "openai-responses" +auth = "chatgpt-plan" + +[[endpoint]] +id = "own" +protocol = "openai-responses" +base_url = "{backend}" +api_key = "own-key" + +[[model]] +name = "sol" +description = "on the plan" +context = 8192 +upstream = "gpt-x" +endpoints = ["plan"] + +[[model]] +name = "own-sol" +description = "on a key" +context = 8192 +upstream = "gpt-x" +endpoints = ["own"] +"# + )) + .expect("the config parses"); + let mut state = app_state(config, None); + state.chatgpt = ChatGpt::with_api_base(&backend); + Rig { + fake: Fake::start().await, + profile: tempfile::TempDir::new().expect("tempdir"), + state, + received, + } + } + + /// Opens the broker on the fake issuer and serves the gateway. + async fn serve(&self) -> SocketAddr { + self.state + .chatgpt + .open_on(self.profile.path(), self.fake.config()); + serve_state(self.state.clone()).await + } + + fn plan_proof(&self) -> String { + self.state.chatgpt.plan_proof().to_owned() + } + + fn sent(&self) -> Vec<(String, Value)> { + self.received.lock().expect("lock").clone() + } + + /// Signs in through the admin routes and the fake issuer. + async fn sign_in(&self, addr: SocketAddr) { + let reply: Value = reqwest::Client::new() + .post(format!("http://{addr}/admin/chatgpt/sign-in")) + .bearer_auth("test-token") + .header(OWNER_PROOF_HEADER, self.state.chatgpt.owner_proof()) + .send() + .await + .expect("the gateway answers") + .json() + .await + .expect("a sign-in reply"); + let query: HashMap = + Url::parse(reply["authorize_url"].as_str().expect("a url")) + .expect("a url") + .query_pairs() + .into_owned() + .collect(); + self.fake + .set(|behavior| behavior.nonce.clone_from(&query["nonce"])); + self.fake + .expect_exchange(&query["code_challenge"], &query["redirect_uri"]); + let mut callback = Url::parse(&query["redirect_uri"]).expect("a redirect uri"); + callback + .query_pairs_mut() + .append_pair("state", &query["state"]) + .append_pair("code", "code-1") + .append_pair("client_id", "oaiapp_1"); + let page = reqwest::get(callback).await.expect("the listener answers"); + assert_eq!(page.status(), StatusCode::OK); + } +} + +/// `POST`s `body` to `path` with the bearer and one plan-proof header per +/// entry of `proofs`. +async fn post(addr: SocketAddr, path: &str, body: &Value, proofs: &[&str]) -> reqwest::Response { + let mut request = reqwest::Client::new() + .post(format!("http://{addr}{path}")) + .bearer_auth("test-token") + .json(body); + for proof in proofs { + request = request.header(PLAN_PROOF_HEADER, *proof); + } + request.send().await.expect("the gateway answers") +} + +async fn refusal(response: reqwest::Response) -> (StatusCode, String) { + let status = response.status(); + let body: Value = response.json().await.expect("an error envelope"); + ( + status, + body["error"]["code"] + .as_str() + .unwrap_or_default() + .to_owned(), + ) +} + +async fn listed(addr: SocketAddr, proofs: &[&str]) -> Vec { + let mut request = reqwest::Client::new() + .get(format!("http://{addr}/v1/models")) + .bearer_auth("test-token"); + for proof in proofs { + request = request.header(PLAN_PROOF_HEADER, *proof); + } + let body: Value = request + .send() + .await + .expect("the gateway answers") + .json() + .await + .expect("a catalog"); + body["data"] + .as_array() + .expect("a list") + .iter() + .filter_map(|model| model["id"].as_str().map(str::to_owned)) + .collect() +} + +fn chat(model: &str) -> Value { + json!({ "model": model, "messages": [{ "role": "user", "content": "hi" }] }) +} + +/// Every inference route, with a valid body naming `model`. +fn every_route(model: &str) -> Vec<(&'static str, Value)> { + let mut streamed = chat(model); + streamed["stream"] = json!(true); + vec![ + ("/v1/chat/completions", chat(model)), + ("/v1/chat/completions", streamed), + ("/v1/embeddings", json!({ "model": model, "input": "hi" })), + ( + "/v1/rerank", + json!({ "model": model, "query": "q", "documents": ["a"] }), + ), + ( + "/v1/audio/speech", + json!({ "model": model, "input": "hi", "voice": "alloy" }), + ), + ] +} + +#[tokio::test] +async fn a_plan_model_refuses_every_route_without_the_plan_proof() { + let rig = Rig::new().await; + let addr = rig.serve().await; + rig.sign_in(addr).await; + let proof = rig.plan_proof(); + let owner_proof = rig.state.chatgpt.owner_proof().to_owned(); + let wrong = "0".repeat(64); + let attempts: [&[&str]; 4] = [ + &[], + &[&wrong], + // The owner proof is not the plan proof. + &[&owner_proof], + // A repeated header counts as none, even when both are right. + &[&proof, &proof], + ]; + for (path, body) in every_route("sol") { + for proofs in attempts { + let outcome = refusal(post(addr, path, &body, proofs).await).await; + assert_eq!( + outcome, + (StatusCode::FORBIDDEN, "chatgpt_plan_runvil_only".to_owned()), + "{path} {body}" + ); + } + } + // Nothing was spent: no request left for the API, and the broker was + // asked for no token beyond the sign-in's own exchange. + assert!(rig.sent().is_empty()); + assert_eq!(rig.fake.grants(), ["authorization_code"]); +} + +#[tokio::test] +async fn the_plan_proof_alone_is_not_a_credential() { + let rig = Rig::new().await; + let addr = rig.serve().await; + let proof = rig.plan_proof(); + // A LAN-shaped caller: the proof as the bearer, and no key at all. + let response = reqwest::Client::new() + .post(format!("http://{addr}/v1/chat/completions")) + .bearer_auth(&proof) + .header(PLAN_PROOF_HEADER, &proof) + .json(&chat("sol")) + .send() + .await + .expect("the gateway answers"); + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); +} + +#[tokio::test] +async fn an_authorized_request_spends_the_plan_at_the_plan_destination() { + let rig = Rig::new().await; + let addr = rig.serve().await; + let proof = rig.plan_proof(); + + // Signed out: the gate passes, the broker has no token, nothing leaves. + let outcome = refusal(post(addr, "/v1/chat/completions", &chat("sol"), &[&proof]).await).await; + assert_eq!( + outcome, + (StatusCode::FORBIDDEN, "chatgpt_sign_in_required".to_owned()) + ); + assert!(rig.sent().is_empty()); + + rig.sign_in(addr).await; + let mut body = chat("sol"); + body["temperature"] = json!(0.2); + post(addr, "/v1/chat/completions", &body, &[&proof]).await; + + let sent = rig.sent(); + let [(authorization, request)] = sent.as_slice() else { + panic!("one request reaches the API: {sent:?}"); + }; + let token = authorization.strip_prefix("Bearer ").expect("a bearer"); + assert!(!token.is_empty()); + for not_it in ["own-key", "test-token", proof.as_str()] { + assert_ne!(token, not_it); + } + assert!( + !rig.fake + .seen(|seen| seen.valid_refresh_tokens.contains(token)), + "a refresh token is never sent to the API" + ); + // The plan's request rules, not the caller's parameters. + assert_eq!(request["model"], "gpt-x"); + assert_eq!(request["store"], false); + assert_eq!(request["stream"], true); + assert!(request.get("temperature").is_none(), "{request}"); +} + +#[tokio::test] +async fn an_ordinary_model_with_the_same_upstream_name_is_untouched() { + let rig = Rig::new().await; + let addr = rig.serve().await; + post(addr, "/v1/chat/completions", &chat("own-sol"), &[]).await; + let sent = rig.sent(); + let [(authorization, request)] = sent.as_slice() else { + panic!("one request reaches the backend: {sent:?}"); + }; + assert_eq!(authorization, "Bearer own-key"); + assert_eq!(request["model"], "gpt-x"); + assert!(rig.fake.grants().is_empty()); +} + +#[tokio::test] +async fn the_catalog_lists_a_plan_model_only_for_runvils_client() { + let rig = Rig::new().await; + let addr = rig.serve().await; + let proof = rig.plan_proof(); + // Signed out: nobody sees the plan model, even with the plan proof. + assert_eq!(listed(addr, &[&proof]).await, ["own-sol"]); + assert_eq!(rig.fake.seen(|seen| seen.discovery_hits), 0); + + rig.sign_in(addr).await; + let hits_before = rig.fake.seen(|seen| seen.discovery_hits); + assert_eq!(listed(addr, &[]).await, ["own-sol"]); + assert_eq!(listed(addr, &[&proof, &proof]).await, ["own-sol"]); + assert_eq!(listed(addr, &[&proof]).await, ["sol", "own-sol"]); + // Listing asked the issuer and the API for nothing. + assert_eq!(rig.fake.seen(|seen| seen.discovery_hits), hits_before); + assert!(rig.sent().is_empty()); +} + +#[tokio::test] +async fn a_gateway_beyond_loopback_serves_no_plan_model() { + let rig = Rig::new().await; + // What the listener actually bound decides it: a wildcard here. + let bound = "0.0.0.0:8081".parse().expect("an address"); + rig.state.chatgpt.start(bound, Some(rig.profile.path())); + let addr = serve_state(rig.state.clone()).await; + let proof = rig.plan_proof(); + for proofs in [&[][..], &[proof.as_str()][..]] { + for (path, body) in every_route("sol") { + let outcome = refusal(post(addr, path, &body, proofs).await).await; + assert_eq!( + outcome, + ( + StatusCode::FORBIDDEN, + "chatgpt_plan_loopback_only".to_owned() + ), + "{path}" + ); + } + assert_eq!(listed(addr, proofs).await, ["own-sol"]); + } + assert!(rig.sent().is_empty()); +} + +#[tokio::test] +async fn a_gateway_that_cannot_hold_a_sign_in_says_so() { + let rig = Rig::new().await; + rig.state.chatgpt.unavailable("a stated reason"); + let addr = serve_state(rig.state.clone()).await; + let proof = rig.plan_proof(); + let outcome = refusal(post(addr, "/v1/chat/completions", &chat("sol"), &[&proof]).await).await; + assert_eq!( + outcome, + ( + StatusCode::SERVICE_UNAVAILABLE, + "chatgpt_unavailable".to_owned() + ) + ); + assert!(rig.sent().is_empty()); +} + +#[tokio::test] +async fn the_catalog_entry_of_a_plan_model_spends_nothing() { + let rig = Rig::new().await; + let addr = rig.serve().await; + rig.sign_in(addr).await; + let model = rig + .state + .live + .read() + .await + .routing + .model("sol") + .expect("configured"); + assert_eq!(model.endpoint.source, CredentialSource::ChatgptPlan); + let request: crate::wire::ChatRequest = serde_json::from_value(chat("sol")).expect("a request"); + // A handler that skipped the resolver holds only this. + let sent = model.endpoint.upstream.send(request.clone(), "gpt-x").await; + assert!(sent.is_err()); + assert!( + model + .endpoint + .upstream + .stream(request, "gpt-x") + .await + .is_err() + ); + assert!(rig.sent().is_empty()); +} + +#[test] +fn only_a_loopback_listener_holds_a_sign_in() { + for (bound, holds) in [ + ("127.0.0.1:8081", true), + ("127.0.0.2:8081", true), + ("[::1]:8081", true), + ("0.0.0.0:8081", false), + ("[::]:8081", false), + ("192.168.1.20:8081", false), + ] { + let profile = tempfile::TempDir::new().expect("tempdir"); + let chatgpt = ChatGpt::new(); + chatgpt.start(bound.parse().expect("an address"), Some(profile.path())); + assert_eq!(chatgpt.broker().is_ok(), holds, "{bound}"); + // The store is created only where a sign-in can be held. + assert_eq!( + profile.path().join("chatgpt-plan").exists(), + holds, + "{bound}" + ); + } + let chatgpt = ChatGpt::new(); + chatgpt.start("127.0.0.1:8081".parse().expect("an address"), None); + assert!(chatgpt.broker().is_err()); +} + +#[test] +fn a_development_name_is_taken_only_when_it_is_plain() { + for (raw, name) in [ + ("RunvilDevAgent", Some("RunvilDevAgent")), + (" Runvil (dev) ", Some("Runvil (dev)")), + ("", None), + (" ", None), + ("Runvil".to_owned()); + match rig.plan.complete_sign_in(callback).await { + Err(PlanError::Denied(code)) => assert_eq!(code, "unrecognized_error"), + other => panic!("expected a denial, got {other:?}"), + } +} + +#[tokio::test] +async fn an_expired_attempt_is_refused_without_a_token_request() { + let rig = Rig::new().await; + let callback = rig.start(Some("oaiapp_1")).await; + rig.advance(601); + assert!(matches!( + rig.plan.complete_sign_in(callback).await, + Err(PlanError::Superseded) + )); + assert!(rig.fake.grants().is_empty()); +} + +#[tokio::test] +async fn a_first_sign_in_without_an_issued_client_is_incomplete() { + let rig = Rig::new().await; + let callback = rig.start(None).await; + assert!(matches!( + rig.plan.complete_sign_in(callback).await, + Err(PlanError::Incomplete(_)) + )); + assert!(rig.fake.grants().is_empty()); +} + +#[tokio::test] +async fn a_missing_id_token_fails_closed_and_revokes_what_was_issued() { + let rig = Rig::new().await; + let callback = rig.start(Some("oaiapp_1")).await; + rig.fake.set(|behavior| behavior.omit_id_token = true); + assert!(matches!( + rig.plan.complete_sign_in(callback).await, + Err(PlanError::Incomplete(_)) + )); + assert_eq!(rig.plan.status().status, Some(Status::SignedOut)); + assert_eq!(rig.fake.seen(|seen| seen.revoked.clone()), ["rt-1"]); + assert!(rig.record().session.refresh_token().is_none()); +} + +#[tokio::test] +async fn a_wrong_nonce_is_refused_and_the_tokens_revoked() { + let rig = Rig::new().await; + let callback = rig.start(Some("oaiapp_1")).await; + rig.fake + .set(|behavior| behavior.nonce = "another-attempts-nonce".to_owned()); + assert!(matches!( + rig.plan.complete_sign_in(callback).await, + Err(PlanError::IdToken(IdTokenError::Nonce)) + )); + assert_eq!(rig.fake.seen(|seen| seen.revoked.clone()), ["rt-1"]); + assert_eq!(rig.plan.status().status, Some(Status::SignedOut)); +} + +#[tokio::test] +async fn signing_in_again_needs_no_client_id_and_refuses_a_different_one() { + let rig = Rig::new().await; + rig.sign_in().await; + rig.plan.sign_out().await.expect("signs out"); + + // A later callback carries no client id: that is success. + let callback = rig.start(None).await; + rig.plan + .complete_sign_in(callback) + .await + .expect("signs in again"); + assert_eq!(rig.plan.status().status, Some(Status::Active)); + assert_eq!( + rig.fake + .seen(|seen| seen.token_requests[1]["client_id"].clone()), + "oaiapp_1" + ); + + // One naming another registration is refused before any exchange. + let before = rig.fake.grants().len(); + let callback = rig.start(Some("oaiapp_other")).await; + assert!(matches!( + rig.plan.complete_sign_in(callback).await, + Err(PlanError::ClientMismatch) + )); + assert_eq!(rig.fake.grants().len(), before); +} + +#[tokio::test] +async fn another_account_cannot_take_over_the_registration() { + let rig = Rig::new().await; + rig.sign_in().await; + rig.plan.sign_out().await.expect("signs out"); + + let callback = rig.start(None).await; + rig.fake + .set(|behavior| behavior.subject = "user-2".to_owned()); + assert!(matches!( + rig.plan.complete_sign_in(callback).await, + Err(PlanError::AccountMismatch) + )); + assert_eq!(rig.plan.status().status, Some(Status::SignedOut)); + assert_eq!(rig.record().subject.as_deref(), Some("user-1")); + // rt-1 by the sign-out, rt-2 because the mismatched sign-in's token was + // not kept. + assert_eq!(rig.fake.seen(|seen| seen.revoked.clone()), ["rt-1", "rt-2"]); +} + +#[tokio::test] +async fn sign_out_is_local_first_and_reports_the_remote_outcome_honestly() { + let rig = Rig::new().await; + rig.sign_in().await; + assert_eq!( + rig.plan.sign_out().await.expect("signs out"), + SignedOut { revoked: true } + ); + let record = rig.record(); + assert_eq!( + record.session, + Session::SignedOut { + revocation: Some(Revocation::Confirmed), + } + ); + assert!(!rig.plan.status().plan_granted); + assert_eq!( + record.client_id.as_deref(), + Some("oaiapp_1"), + "the registration is kept" + ); + assert!(matches!( + rig.plan.access_token().await, + Err(PlanError::NotSignedIn) + )); + + // A revoke the issuer does not confirm is reported as unconfirmed. + let callback = rig.start(None).await; + rig.plan + .complete_sign_in(callback) + .await + .expect("signs in again"); + rig.fake.set(|behavior| behavior.revoke_status = 500); + assert_eq!( + rig.plan.sign_out().await.expect("signs out"), + SignedOut { revoked: false } + ); + let record = rig.record(); + assert_eq!( + record.session, + Session::SignedOut { + revocation: Some(Revocation::Unconfirmed), + }, + "signed out here even so" + ); +} + +#[tokio::test] +async fn forget_keeps_only_the_agent_host_id_and_reset_clears_an_unreadable_record() { + let rig = Rig::new().await; + rig.sign_in().await; + let agent_host_id = rig.record().agent_host_id; + rig.plan.forget().await.expect("forgets"); + let record = rig.record(); + assert_eq!(record.agent_host_id, agent_host_id); + assert!(record.client_id.is_none() && record.subject.is_none() && record.email.is_none()); + assert_eq!( + record.session, + Session::SignedOut { + revocation: Some(Revocation::Confirmed), + } + ); + + // An unreadable record refuses everything until it is reset. + let dir = private_dir(); + Store::open(dir.path()).expect("open"); + let record_path = dir + .path() + .join(crate::store::STORE_DIR) + .join(crate::store::RECORD_FILE); + std::fs::write(&record_path, b"{ not json").expect("write"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt as _; + std::fs::set_permissions(&record_path, std::fs::Permissions::from_mode(0o600)) + .expect("chmod"); + } + let fake = Fake::start().await; + let plan = ChatGptPlan::open(dir.path(), fake.config()).expect("open"); + assert!(plan.status().unreadable); + assert!(matches!( + plan.access_token().await, + Err(PlanError::Unreadable) + )); + assert!(matches!( + plan.begin_sign_in(REDIRECT).await, + Err(PlanError::Unreadable) + )); + plan.reset().expect("resets"); + assert!(!plan.status().unreadable); + plan.begin_sign_in(REDIRECT) + .await + .expect("a fresh install can sign in"); + tokio::time::sleep(Duration::ZERO).await; +} diff --git a/crates/gateway/chatgpt-plan/src/plan.rs b/crates/gateway/chatgpt-plan/src/plan.rs new file mode 100644 index 00000000..755d43b0 --- /dev/null +++ b/crates/gateway/chatgpt-plan/src/plan.rs @@ -0,0 +1,499 @@ +//! The broker: the one owner of a ChatGPT sign-in. +//! +//! [`ChatGptPlan`] is the single place the credential changes. It serializes +//! every change behind one lock and one generation counter, and it applies +//! one rule to every token the issuer hands it: the token is either adopted +//! into the saved record or revoked. It is never just dropped. +//! +//! Three properties shape the code: +//! +//! - **The broker owns every in-flight operation.** A refresh, a code +//! exchange and a sign-out each run as a task the broker spawned, so a +//! caller that goes away cannot abandon a reply that still has to be saved +//! or revoked. +//! - **The generation check is part of the commit.** Sign-out and a new +//! sign-in bump the generation, which lives under the same lock as the +//! record. A result that arrives under an older generation is refused +//! inside the critical section that would have saved it, and revoked. +//! - **An unknown outcome fails closed.** Before a refresh is sent, the +//! record is saved as [`Session::Refreshing`]. A refresh whose reply +//! never arrives, or a restart that finds [`Session::Refreshing`], leaves +//! the stored refresh token possibly spent, so the sign-in becomes +//! [`Session::NeedsAuth`] and nothing is retried. A refresh that never +//! left (no connection) changes nothing. + +use std::fmt; +use std::path::Path; +use std::sync::{Arc, Mutex, MutexGuard}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use async_trait::async_trait; +use gateway_protocol::ProtocolError; +use gateway_protocol::upstream::BearerSource; + +use crate::id_token::{self, IdClaims, IdTokenError, Jwks}; +use crate::oauth::{OAuthClient, OAuthConfig, OAuthError, PLAN_SCOPE}; +use crate::store::{Record, Revocation, Secret, Session, Status, Store, StoreError}; + +mod refresh; +mod sign_in; +mod sign_out; + +pub use self::sign_in::Callback; +pub use self::sign_out::SignedOut; + +/// How long before expiry an access token is refreshed. +const REFRESH_MARGIN_SECS: u64 = 120; + +/// The time source, in Unix seconds. +pub type Clock = Arc u64 + Send + Sync>; + +/// Why the broker refused or failed. +#[derive(Debug, thiserror::Error)] +#[non_exhaustive] +pub enum PlanError { + /// No one is signed in. + #[error("not signed in to ChatGPT")] + NotSignedIn, + /// The sign-in can no longer be trusted; the user must sign in again. + #[error("the ChatGPT sign-in must be repeated")] + NeedsAuth, + /// The account did not grant plan usage to this app. + #[error("the ChatGPT account has not granted plan usage")] + PlanNotGranted, + /// The saved record cannot be read; it must be reset first. + #[error("the saved ChatGPT sign-in is unreadable")] + Unreadable, + /// The callback matches no pending sign-in. + #[error("no sign-in is waiting for this callback")] + UnknownAttempt, + /// The sign-in took too long, or was superseded by a sign-out or a + /// newer sign-in. + #[error("the sign-in expired or was superseded; start it again")] + Superseded, + /// The user or the issuer declined. Holds the OAuth error code. + #[error("ChatGPT declined the sign-in ({0})")] + Denied(String), + /// The callback or the token reply lacks something it must carry. + #[error("the sign-in reply is incomplete: {0}")] + Incomplete(&'static str), + /// The callback names a client other than this install's. + #[error("the sign-in reply is for another registration")] + ClientMismatch, + /// The reply is for a different account than the one registered. + #[error("signed in as a different account than this registration")] + AccountMismatch, + /// The ID token failed verification. + #[error(transparent)] + IdToken(#[from] IdTokenError), + /// An OAuth call failed. + #[error(transparent)] + OAuth(#[from] OAuthError), + /// The record could not be saved or read. + #[error(transparent)] + Store(#[from] StoreError), + /// A broker operation stopped because system randomness failed or a spawned task ended. + #[error("the ChatGPT sign-in operation could not complete ({0})")] + Interrupted(&'static str), +} + +impl PlanError { + /// The stable identifier a client sees when this error stops a request. + #[must_use] + pub fn code(&self) -> &'static str { + match self { + PlanError::NotSignedIn => "chatgpt_sign_in_required", + PlanError::NeedsAuth | PlanError::Unreadable => "chatgpt_sign_in_expired", + PlanError::PlanNotGranted => "chatgpt_plan_not_granted", + _ => "chatgpt_sign_in_failed", + } + } +} + +/// What the broker reports about itself. It never includes a token. +#[derive(Debug, Clone, PartialEq, Eq)] +#[expect( + clippy::struct_excessive_bools, + reason = "each flag is an independent fact a status page shows" +)] +pub struct PlanStatus { + /// Where the sign-in stands; `None` when no record has ever existed. + pub status: Option, + /// Whether the saved record is unreadable and must be reset. + pub unreadable: bool, + /// The signed-in account's email. + pub email: Option, + /// Whether the account granted plan usage. + pub plan_granted: bool, + /// The last sign-out's remote outcome. + pub revocation: Option, + /// Whether a refresh token the broker did not keep could not be + /// confirmed revoked, and so may still be live at the issuer. + pub cleanup_unconfirmed: bool, + /// Whether a sign-in is waiting for its callback or still completing one. + pub sign_in_pending: bool, +} + +/// Snapshot of an active, plan-granted sign-in's identity and generation. +/// +/// Captured before an out-of-lock network call and checked before committing +/// its side effects, so a sign-out, reset, or account switch in between +/// aborts the commit. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SessionFence { + generation: u64, + setup_epoch: u64, + subject: Option, +} + +struct Access { + token: Secret, + expires_at: u64, +} + +#[derive(Default)] +struct State { + record: Option, + unreadable: bool, + unconfirmed_cleanup: bool, + access: Option, + attempt: Option, + /// The serial of the claimed attempt that is still completing. + completing: Option, + /// Counts sign-ins started. An attempt that is not the latest one + /// started may not activate, even when its exchange was already sent. + attempts_started: u64, + /// Bumped by sign-out, reset and every completed sign-in. A result that + /// started under an older value is stale. It lives under the same lock + /// as the record, so the fence and the change it fences are one step. + generation: u64, + /// Tracks each token-bearing retirement across tokenless repeats. + next_retirement_id: u64, + pending_retirements: std::collections::HashSet, + signed_out_retirement: Option, + /// Bumped when plan model bindings are explicitly forgotten, superseding + /// any in-flight `models/enable` without disturbing OAuth credentials. + setup_epoch: u64, +} + +struct Inner { + store: Store, + oauth: OAuthClient, + clock: Clock, + state: Mutex, + /// Held for the whole of one refresh, so concurrent callers share it. + refresh_gate: tokio::sync::Mutex<()>, + jwks: tokio::sync::Mutex>, +} + +/// The broker. Cloning it shares the one sign-in. +#[derive(Clone)] +pub struct ChatGptPlan { + inner: Arc, +} + +impl fmt::Debug for ChatGptPlan { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("ChatGptPlan").finish_non_exhaustive() + } +} + +impl ChatGptPlan { + /// Opens the sign-in saved in the profile directory `dir`, signing in + /// with `config`. + /// + /// # Errors + /// Returns [`StoreError`] when the private store directory or its lock + /// cannot be opened. + pub fn open(dir: &Path, config: OAuthConfig) -> Result { + let clock: Clock = Arc::new(|| { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_or(0, |elapsed| elapsed.as_secs()) + }); + ChatGptPlan::open_with_clock(dir, config, clock) + } + + /// [`ChatGptPlan::open`] with an injected clock. + /// + /// # Errors + /// Returns [`StoreError`] when the private store directory or its lock + /// cannot be opened. + pub fn open_with_clock( + dir: &Path, + config: OAuthConfig, + clock: Clock, + ) -> Result { + let store = Store::open(dir)?; + let mut state = State::default(); + match store.load() { + Ok(record) => { + state.unconfirmed_cleanup = record + .as_ref() + .is_some_and(|record| record.unconfirmed_cleanup); + state.record = record; + } + Err(_) => state.unreadable = true, + } + // A refresh was in flight when the last process ended: its outcome + // is unknown, so the stored refresh token may be spent. + if let Some(record) = state.record.as_mut() + && let Session::Refreshing(creds) = &record.session + { + record.session = Session::NeedsAuth { + refresh_token: Some(creds.refresh_token.clone()), + }; + tracing::warn!("a ChatGPT token refresh was interrupted; sign in again"); + if store.save(record).is_err() { + state.unreadable = true; + } + } + Ok(ChatGptPlan { + inner: Arc::new(Inner { + store, + oauth: OAuthClient::new(config), + clock, + state: Mutex::new(state), + refresh_gate: tokio::sync::Mutex::new(()), + jwks: tokio::sync::Mutex::new(None), + }), + }) + } + + /// What the broker reports about the sign-in. Never a token. + #[must_use] + pub fn status(&self) -> PlanStatus { + let state = self.inner.lock(); + let record = state.record.as_ref(); + PlanStatus { + status: record.map(|record| record.session.status()), + unreadable: state.unreadable, + email: record.and_then(|record| record.email.clone()), + plan_granted: record.is_some_and(grants_plan), + revocation: record.and_then(|record| record.session.revocation()), + cleanup_unconfirmed: state.unconfirmed_cleanup, + sign_in_pending: self.inner.sign_in_pending(&state), + } + } + + /// The bearer for the next plan request, refreshing first when the + /// cached one is missing or about to expire. + /// + /// # Errors + /// See [`PlanError`]. Nothing is returned unless the sign-in is active + /// and plan usage was granted. + pub async fn access_token(&self) -> Result { + if let Some(token) = self.inner.cached_access()? { + return Ok(token); + } + let inner = self.inner.clone(); + tokio::spawn(async move { inner.refresh().await }) + .await + .map_err(|_| PlanError::Interrupted("token refresh task stopped"))? + } + + /// Snapshots the active, plan-granted session's generation and account + /// subject, or returns the broker refusal when no usable plan session is + /// present. + /// + /// # Errors + /// Returns [`PlanError`] when the store is unreadable, nobody is signed + /// in, re-authentication is required, or plan usage was not granted. + pub fn current_fence(&self) -> Result { + let state = self.inner.lock(); + let _ = self.inner.usable(&state)?; + let record = state.record.as_ref().ok_or(PlanError::NotSignedIn)?; + Ok(SessionFence { + generation: state.generation, + setup_epoch: state.setup_epoch, + subject: record.subject.clone(), + }) + } + + /// Whether `fence` still matches the live, plan-granted session and setup + /// epoch. + #[must_use] + pub fn verify_fence(&self, fence: &SessionFence) -> bool { + let state = self.inner.lock(); + Inner::fence_matches(&state, fence) + } + + /// Advances the model-setup epoch without signing out, so any in-flight + /// model-enable operation started earlier cannot commit stale bindings. + pub fn supersede_model_setup(&self) { + let mut state = self.inner.lock(); + state.setup_epoch += 1; + } + + /// Runs `commit` under the broker's state lock if and only if `fence` + /// still matches the live session and setup epoch, serializing the side + /// effect against sign-out, forget, reset, sign-in adoption, and + /// [`ChatGptPlan::supersede_model_setup`]. + /// + /// # Errors + /// Returns `on_stale()` without running `commit` when `fence` is stale, or + /// propagates the error returned by `commit`. + pub fn commit_fenced( + &self, + fence: &SessionFence, + on_stale: impl FnOnce() -> E, + commit: F, + ) -> Result + where + F: FnOnce() -> Result, + { + let state = self.inner.lock(); + if !Inner::fence_matches(&state, fence) { + return Err(on_stale()); + } + commit() + } + + /// Drops `rejected` from the in-memory access-token cache if it is still + /// the cached token, so the next [`ChatGptPlan::access_token`] refreshes. + pub fn reject_access(&self, rejected: &Secret) { + let mut state = self.inner.lock(); + if state + .access + .as_ref() + .is_some_and(|access| access.token.expose() == rejected.expose()) + { + state.access = None; + } + } +} + +fn grants_plan(record: &Record) -> bool { + record + .session + .scopes() + .iter() + .any(|scope| scope == PLAN_SCOPE) +} + +impl Inner { + fn lock(&self) -> MutexGuard<'_, State> { + // A poisoned lock means a panic mid-change; the state is still the + // last committed one, because every change is a whole assignment. + self.state + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + } + + fn now(&self) -> u64 { + (self.clock)() + } + + /// Revokes a refresh token the broker will not keep. When the issuer + /// does not confirm, the token is still not kept; what is kept is the + /// fact that it may be live, which the status reports. + async fn dispose(&self, client_id: &str, refresh_token: Option<&Secret>) { + let Some(token) = refresh_token else { return }; + let revoked = self.oauth.revoke(client_id, token).await; + tracing::info!(revoked, "revoked a ChatGPT refresh token that was not kept"); + if revoked { + return; + } + self.note_unconfirmed_cleanup(); + } + + pub(super) fn note_unconfirmed_cleanup(&self) { + let mut state = self.lock(); + state.unconfirmed_cleanup = true; + if state.record.is_none() + && !state.unreadable + && let Some(agent_host_id) = sign_in::new_agent_host_id() + { + let mut warning = Record::new(agent_host_id); + warning.unconfirmed_cleanup = true; + if self.store.save(&warning).is_ok() { + state.record = Some(warning); + } + return; + } + if let Some(record) = state + .record + .as_mut() + .filter(|record| !record.unconfirmed_cleanup) + { + record.unconfirmed_cleanup = true; + // Best effort: the flag is true in this process either way. + let _ = self.store.save(record); + } + } + + /// Verifies an ID token, refetching the issuer's keys once when the + /// token names a key the cache does not hold. + async fn verify( + &self, + token: &str, + client_id: &str, + nonce: Option<&str>, + access_token: &str, + ) -> Result { + let config = self.oauth.config(); + let mut cache = self.jwks.lock().await; + for refetch in [false, true] { + if refetch || cache.is_none() { + *cache = Some(self.oauth.jwks().await?); + } + let jwks = cache.clone().unwrap_or_default(); + let expected = id_token::Expected { + issuer: &config.issuer, + client_id, + nonce, + access_token, + now: self.now(), + }; + match id_token::verify(token, &jwks, &expected) { + Err(IdTokenError::UnknownKey) if !refetch => {} + outcome => return Ok(outcome?), + } + } + Err(IdTokenError::UnknownKey.into()) + } + + fn fence_matches(state: &State, fence: &SessionFence) -> bool { + !state.unreadable + && state.generation == fence.generation + && state.setup_epoch == fence.setup_epoch + && state.record.as_ref().is_some_and(|record| { + matches!(record.session, Session::Active(_) | Session::Refreshing(_)) + && grants_plan(record) + && record.subject == fence.subject + }) + } +} + +#[async_trait] +impl BearerSource for ChatGptPlan { + async fn bearer(&self) -> Result { + match self.access_token().await { + Ok(token) => Ok(gateway_config::Secret::new(token.expose().to_owned())), + // The issuer could not be reached, or is failing: that is an + // upstream fault, and the sign-in itself is still good. + Err(PlanError::OAuth(error @ OAuthError::Connect)) => { + Err(ProtocolError::connect(error)) + } + Err(PlanError::OAuth(error)) => Err(ProtocolError::transport(error)), + Err(error) => Err(ProtocolError::CredentialUnavailable { code: error.code() }), + } + } + + /// The backend refused this access token: stop handing it out, so the + /// next request refreshes. + async fn rejected(&self, rejected: &gateway_config::Secret) { + let mut state = self.inner.lock(); + if state + .access + .as_ref() + .is_some_and(|access| access.token.expose() == rejected.expose()) + { + state.access = None; + } + } +} + +#[cfg(test)] +#[path = "plan-tests.rs"] +mod tests; diff --git a/crates/gateway/chatgpt-plan/src/plan/refresh.rs b/crates/gateway/chatgpt-plan/src/plan/refresh.rs new file mode 100644 index 00000000..364a7c4d --- /dev/null +++ b/crates/gateway/chatgpt-plan/src/plan/refresh.rs @@ -0,0 +1,232 @@ +//! Refreshing the access token. +//! +//! One refresh runs at a time, and every caller that needs a token while it +//! runs shares its result. Admission is one critical section: it decides +//! whether the cached token still serves, and if not, saves the record as +//! [`Session::Refreshing`] before anything is sent. Each outcome is then +//! handled by what it proves: +//! +//! - the request never left: nothing changed at the issuer, so the session +//! returns to [`Session::Active`]; +//! - the issuer refused without ending the session: the same; +//! - the refresh token is dead, or no readable reply came back: the stored +//! token may be spent, so the session becomes [`Session::NeedsAuth`] and +//! nothing is retried; +//! - a reply arrived: it is saved only if the generation is still the one +//! the refresh started under, and revoked otherwise. +//! +//! Reference: OpenAI's "Token reference", sections "Token response and +//! refresh tokens" and "Token lifetimes", +//! . + +use std::sync::Arc; + +use super::{Access, Inner, PlanError, REFRESH_MARGIN_SECS, State, grants_plan}; +use crate::oauth::{OAuthError, TokenSet}; +use crate::store::{Record, Secret, Session}; + +impl Inner { + /// The cached access token when it is usable, `None` when a refresh is + /// needed, or the reason no token may be handed out. + pub(super) fn cached_access(&self) -> Result, PlanError> { + self.usable(&self.lock()) + } + + pub(super) fn usable(&self, state: &State) -> Result, PlanError> { + if state.unreadable { + return Err(PlanError::Unreadable); + } + let record = state.record.as_ref().ok_or(PlanError::NotSignedIn)?; + match &record.session { + Session::Active(_) | Session::Refreshing(_) => {} + Session::SignedOut { .. } => return Err(PlanError::NotSignedIn), + Session::NeedsAuth { .. } => return Err(PlanError::NeedsAuth), + } + if !grants_plan(record) { + return Err(PlanError::PlanNotGranted); + } + let now = self.now(); + Ok(state + .access + .as_ref() + .filter(|access| access.expires_at > now.saturating_add(REFRESH_MARGIN_SECS)) + .map(|access| access.token.clone())) + } + + /// One refresh, shared by every concurrent caller. + pub(super) async fn refresh(self: Arc) -> Result { + let _gate = self.refresh_gate.lock().await; + // Admission: the usability check and the marker are one critical + // section, so nothing can change the sign-in between them. + let (client_id, refresh_token, subject, generation) = { + let mut state = self.lock(); + if let Some(token) = self.usable(&state)? { + return Ok(token); + } + let generation = state.generation; + let record = state.record.as_mut().ok_or(PlanError::NotSignedIn)?; + let (Some(client_id), Some(subject)) = + (record.client_id.clone(), record.subject.clone()) + else { + return Err(PlanError::NeedsAuth); + }; + let creds = match &record.session { + Session::Active(creds) | Session::Refreshing(creds) => creds.clone(), + Session::SignedOut { .. } => return Err(PlanError::NotSignedIn), + Session::NeedsAuth { .. } => return Err(PlanError::NeedsAuth), + }; + // Saved first, published second: a failed save changes nothing. + let mut marked = record.clone(); + marked.session = Session::Refreshing(creds.clone()); + self.store.save(&marked)?; + *record = marked; + (client_id, creds.refresh_token, subject, generation) + }; + + let tokens = match self.oauth.refresh(&client_id, &refresh_token).await { + Ok(tokens) => tokens, + // The request never left: the refresh token is unspent. + Err(error @ OAuthError::Connect) => { + self.settle(generation, restore_active); + return Err(error.into()); + } + // The issuer answered and refused, without rotating. + Err(error @ OAuthError::Refused { .. }) if !error.ends_the_session() => { + self.settle(generation, restore_active); + return Err(error.into()); + } + // The refresh token is dead, or the outcome is unknown. + Err(error) => { + let dead = error.ends_the_session(); + tracing::warn!( + refused = dead, + "ChatGPT token refresh failed; sign in again" + ); + self.settle(generation, |record| { + let kept = (!dead) + .then(|| record.session.refresh_token().cloned()) + .flatten(); + record.session = Session::NeedsAuth { + refresh_token: kept, + }; + }); + return Err(PlanError::NeedsAuth); + } + }; + + // A refresh may omit the ID token. One that is present must verify + // and name the same account. + if let Some(id_token) = &tokens.id_token { + let verified = self + .verify( + id_token.expose(), + &client_id, + None, + tokens.access_token.expose(), + ) + .await; + let same_account = matches!(&verified, Ok(claims) if claims.subject == subject); + if !same_account { + tracing::warn!("a refreshed ChatGPT ID token was rejected; sign in again"); + let rotated = tokens.refresh_token.is_some(); + self.settle(generation, |record| { + let kept = (!rotated) + .then(|| record.session.refresh_token().cloned()) + .flatten(); + record.session = Session::NeedsAuth { + refresh_token: kept, + }; + }); + self.dispose(&client_id, tokens.refresh_token.as_ref()) + .await; + return Err(PlanError::NeedsAuth); + } + } + + match self.commit_refresh(generation, &tokens) { + Ok(true) => Ok(tokens.access_token), + Ok(false) => Err(PlanError::PlanNotGranted), + Err(error) => { + // Stale, or unsaved: the new token is revoked, not kept. + self.dispose(&client_id, tokens.refresh_token.as_ref()) + .await; + Err(error) + } + } + } + + /// Saves a refresh's result and caches its access token, when + /// `generation` is still current, and says whether plan usage is still + /// granted. The generation check and the save are one critical section, + /// so a sign-out cannot slip between them. + fn commit_refresh(&self, generation: u64, tokens: &TokenSet) -> Result { + let mut state = self.lock(); + if state.generation != generation { + return Err(PlanError::Superseded); + } + let expires_at = self.now().saturating_add(tokens.expires_in); + let record = state.record.as_mut().ok_or(PlanError::NotSignedIn)?; + let (Session::Refreshing(current) | Session::Active(current)) = &record.session else { + return Err(PlanError::NeedsAuth); + }; + let mut creds = current.clone(); + if let Some(rotated) = &tokens.refresh_token { + creds.refresh_token = rotated.clone(); + } + // A reply that names no scope leaves the granted scopes unchanged. + if let Some(scopes) = &tokens.scopes { + creds.scopes.clone_from(scopes); + } + let before = record.clone(); + record.session = Session::Active(creds); + if let Err(error) = self.store.save(record) { + // The disk still holds the spent token and the marker, so a + // restart will ask for a new sign-in. Do the same here. + *record = before; + record.session = Session::NeedsAuth { + refresh_token: record.session.refresh_token().cloned(), + }; + state.access = None; + return Err(error.into()); + } + let granted = grants_plan(record); + state.access = Some(Access { + token: tokens.access_token.clone(), + expires_at, + }); + // One line per refresh, so a log shows that the saved sign-in was + // used. It names no token. + tracing::info!( + expires_in_secs = tokens.expires_in, + plan_granted = granted, + "refreshed the ChatGPT access token" + ); + Ok(granted) + } + + /// Applies `change` to the record and saves it, when `generation` is + /// still current. A stale caller changes nothing. + fn settle(&self, generation: u64, change: impl FnOnce(&mut Record)) { + let mut state = self.lock(); + if state.generation != generation { + return; + } + if let Some(record) = state.record.as_mut() { + change(record); + if !matches!(record.session, Session::Active(_)) { + state.access = None; + } + if let Some(record) = state.record.as_ref() + && self.store.save(record).is_err() + { + state.unreadable = true; + } + } + } +} + +fn restore_active(record: &mut Record) { + if let Session::Refreshing(creds) = &record.session { + record.session = Session::Active(creds.clone()); + } +} diff --git a/crates/gateway/chatgpt-plan/src/plan/sign_in.rs b/crates/gateway/chatgpt-plan/src/plan/sign_in.rs new file mode 100644 index 00000000..9b6b02e9 --- /dev/null +++ b/crates/gateway/chatgpt-plan/src/plan/sign_in.rs @@ -0,0 +1,384 @@ +//! Signing in: starting an attempt and completing it from the callback. +//! +//! One attempt is pending at a time; starting another cancels the one before +//! it, even when that one's code exchange has already been sent. The callback is matched by its `state`, compared without leaking +//! where it differs, and a callback that matches nothing consumes nothing. +//! A matching callback consumes the attempt whatever it carries, so a +//! `state` is good for one use. A denial sends no token request. On a first +//! sign-in the issued client id is saved before the code is exchanged, as +//! the issuer's documentation requires. +//! +//! Reference: OpenAI's "Registration and sign-in", sections 1 to 4, +//! . + +use std::fmt::Write as _; +use std::sync::Arc; + +use sha2::{Digest as _, Sha256}; + +use super::{Access, ChatGptPlan, Inner, PlanError}; +use crate::oauth::{AuthorizeRequest, SCOPE, TokenSet, random_token}; +use crate::store::{Credentials, Record, Secret, Session}; + +/// How long a sign-in may take, from the authorize URL to its callback. +const ATTEMPT_LIFETIME_SECS: u64 = 600; + +/// One pending sign-in. +pub(super) struct Attempt { + state: String, + nonce: String, + verifier: String, + redirect_uri: String, + /// Whether this attempt registers the install (no client id yet). + registering: bool, + /// The generation the attempt started under. + generation: u64, + /// Which sign-in this is, counted from the broker's start. + serial: u64, + started_at: u64, +} + +/// The callback's query, already parsed strictly: each parameter at most +/// once. +#[derive(Debug, Clone, Default)] +pub struct Callback { + /// The `state` parameter. + pub state: String, + /// The authorization code, on success. + pub code: Option, + /// The OAuth error, on refusal. + pub error: Option, + /// The issued client id, which a first sign-in carries. + pub client_id: Option, +} + +impl ChatGptPlan { + /// Starts a sign-in whose callback will arrive at `redirect_uri`, and + /// returns the URL to open in the browser. The first sign-in of an + /// install creates its `ext_agent_host_id`. + /// + /// # Errors + /// [`PlanError::Unreadable`] when the saved record must be reset first, + /// and the store and OAuth errors of saving that id and building + /// the URL. + pub async fn begin_sign_in(&self, redirect_uri: &str) -> Result { + let inner = &self.inner; + let token = |bytes| { + random_token(bytes).ok_or(PlanError::Interrupted("system randomness unavailable")) + }; + let (state_token, nonce, verifier) = (token(32)?, token(32)?, token(48)?); + let (agent_host_id, client_id, login_hint, generation) = { + let mut state = inner.lock(); + if state.unreadable { + return Err(PlanError::Unreadable); + } + if state.record.is_none() { + let id = new_agent_host_id() + .ok_or(PlanError::Interrupted("system randomness unavailable"))?; + let mut record = Record::new(id); + record.unconfirmed_cleanup = state.unconfirmed_cleanup; + inner.store.save(&record)?; + state.record = Some(record); + } + let record = state + .record + .as_ref() + .ok_or(PlanError::Interrupted("sign-in record missing after setup"))?; + ( + record.agent_host_id.clone(), + record.client_id.clone(), + record.email.clone(), + state.generation, + ) + }; + // The URL is built before the attempt is installed, so a failure + // here leaves nothing pending. + let url = inner + .oauth + .authorize_url(&AuthorizeRequest { + client_id: client_id.as_deref(), + agent_host_id: &agent_host_id, + redirect_uri, + state: &state_token, + nonce: &nonce, + verifier: &verifier, + login_hint: login_hint.as_deref(), + }) + .await?; + let mut state = inner.lock(); + // A sign-out or a completed sign-in since the record was read makes + // what the URL was built from stale. + if state.unreadable || state.generation != generation { + return Err(PlanError::Superseded); + } + // A completion still open for an older attempt is no longer the + // sign-in the status describes. + state.completing = None; + state.attempts_started += 1; + state.attempt = Some(Attempt { + state: state_token, + nonce, + verifier, + redirect_uri: redirect_uri.to_owned(), + registering: client_id.is_none(), + generation, + serial: state.attempts_started, + started_at: inner.now(), + }); + Ok(url) + } + + /// Completes the pending sign-in from its callback: the exchange, the + /// ID-token checks, and the activation. + /// + /// # Errors + /// See [`PlanError`]. After any error the sign-in is not active, and a + /// refresh token the issuer handed over has been revoked. + pub async fn complete_sign_in(&self, callback: Callback) -> Result<(), PlanError> { + let inner = self.inner.clone(); + // The broker owns the exchange: a callback connection that closes + // early cannot abandon tokens that still need saving or revoking. + tokio::spawn(async move { inner.complete(callback).await }) + .await + .map_err(|_| PlanError::Interrupted("sign-in completion task stopped"))? + } +} + +/// Marks one claimed attempt, by its serial, as still completing. The +/// callback takes the attempt before the exchange is sent, so without this +/// mark the status would say that nothing is pending while a sign-in is +/// about to become active, and a client that polls would stop waiting. +/// +/// The mark lasts until the attempt's own work is finished, the revocation +/// of a token it will not keep included, so the status a client reads once +/// nothing is pending is final for that attempt. A sign-out, a reset, or a +/// newer attempt clears the mark at once. Dropping this clears the mark +/// only while it is still this attempt's, so an older completion that +/// finishes late cannot clear a newer one's. +pub(super) struct Completing(pub(super) Arc, pub(super) u64); + +impl Drop for Completing { + fn drop(&mut self) { + let mut state = self.0.lock(); + if state.completing == Some(self.1) { + state.completing = None; + } + } +} + +impl Inner { + async fn complete(self: Arc, callback: Callback) -> Result<(), PlanError> { + // Claim: only the matching callback consumes the attempt. + let (attempt, client_id) = { + let mut state = self.lock(); + let matches = state + .attempt + .as_ref() + .is_some_and(|attempt| same(&attempt.state, &callback.state)); + if !matches { + return Err(PlanError::UnknownAttempt); + } + let attempt = state.attempt.take().ok_or(PlanError::UnknownAttempt)?; + if self.expired(&attempt) { + return Err(PlanError::Superseded); + } + if let Some(error) = &callback.error { + return Err(PlanError::Denied(plain_code(error))); + } + let record = state.record.as_mut().ok_or(PlanError::Superseded)?; + let client_id = if attempt.registering { + let issued = callback + .client_id + .clone() + .filter(|id| !id.is_empty()) + .ok_or(PlanError::Incomplete("no client id was issued"))?; + // Saved before the exchange, and published only once saved. + let mut registered = record.clone(); + registered.client_id = Some(issued.clone()); + self.store.save(®istered)?; + *record = registered; + issued + } else { + let known = record.client_id.clone().ok_or(PlanError::Superseded)?; + if callback.client_id.as_ref().is_some_and(|id| *id != known) { + return Err(PlanError::ClientMismatch); + } + known + }; + state.completing = Some(attempt.serial); + (attempt, client_id) + }; + let _completing = Completing(Arc::clone(&self), attempt.serial); + let code = callback + .code + .as_deref() + .filter(|code| !code.is_empty()) + .ok_or(PlanError::Incomplete("no authorization code"))?; + + let tokens = self + .oauth + .exchange(&client_id, code, &attempt.verifier, &attempt.redirect_uri) + .await?; + match self.adopt(&attempt, &client_id, &tokens).await { + Ok(superseded) => { + // The refresh token this sign-in replaced is no longer + // saved, so it is revoked: a token is never just dropped. + self.dispose(&client_id, superseded.as_ref()).await; + Ok(()) + } + Err(error) => { + self.dispose(&client_id, tokens.refresh_token.as_ref()) + .await; + Err(error) + } + } + } + + /// Verifies a code exchange's tokens and makes the sign-in active, + /// returning the refresh token it replaced, if the record held one. On + /// any error nothing was activated, and the caller revokes the tokens. + async fn adopt( + &self, + attempt: &Attempt, + client_id: &str, + tokens: &TokenSet, + ) -> Result, PlanError> { + let id_token = tokens + .id_token + .as_ref() + .ok_or(PlanError::Incomplete("no ID token"))?; + let refresh_token = tokens + .refresh_token + .as_ref() + .ok_or(PlanError::Incomplete("no refresh token"))?; + let claims = self + .verify( + id_token.expose(), + client_id, + Some(&attempt.nonce), + tokens.access_token.expose(), + ) + .await?; + + // Activation: every check and the save are one critical section. + let mut state = self.lock(); + // Superseded by a sign-out or a completed sign-in, by a newer + // sign-in that has started, or by its own age. + if state.generation != attempt.generation + || state.attempts_started != attempt.serial + || self.expired(attempt) + { + return Err(PlanError::Superseded); + } + let expires_at = self.now().saturating_add(tokens.expires_in); + let revoke_settled = !state + .signed_out_retirement + .is_some_and(|id| state.pending_retirements.contains(&id)); + let record = state.record.as_mut().ok_or(PlanError::Superseded)?; + if record + .subject + .as_ref() + .is_some_and(|subject| *subject != claims.subject) + { + return Err(PlanError::AccountMismatch); + } + let before = record.clone(); + let carry_unconfirmed = before.unconfirmed_cleanup + || (revoke_settled + && matches!( + before.session, + Session::SignedOut { + revocation: Some(crate::store::Revocation::Unconfirmed), + } + )); + let superseded = record + .session + .refresh_token() + .filter(|held| *held != refresh_token) + .cloned(); + // A reply that names no scope granted what was asked for. + let scopes = tokens + .scopes + .clone() + .unwrap_or_else(|| SCOPE.split(' ').map(str::to_owned).collect()); + record.subject = Some(claims.subject); + record.email = claims.email; + record.unconfirmed_cleanup = carry_unconfirmed; + record.session = Session::Active(Credentials { + refresh_token: refresh_token.clone(), + scopes, + }); + if let Err(error) = self.store.save(record) { + *record = before; + return Err(error.into()); + } + state.signed_out_retirement = None; + state.unconfirmed_cleanup = carry_unconfirmed; + state.access = Some(Access { + token: tokens.access_token.clone(), + expires_at, + }); + // A refresh still in flight from the session before this one is + // now stale. + state.generation += 1; + Ok(superseded) + } + + fn expired(&self, attempt: &Attempt) -> bool { + self.now().saturating_sub(attempt.started_at) > ATTEMPT_LIFETIME_SECS + } + + /// Whether the status reports a sign-in as pending: an attempt that is + /// waiting for its callback and has not expired, or a claimed attempt + /// that is still completing. + pub(super) fn sign_in_pending(&self, state: &super::State) -> bool { + let awaiting = state + .attempt + .as_ref() + .is_some_and(|attempt| !self.expired(attempt)); + awaiting || state.completing.is_some() + } +} + +/// Compares two strings without revealing where they differ: both are +/// hashed, and the digests are compared in full. +fn same(left: &str, right: &str) -> bool { + let (left, right) = ( + Sha256::digest(left.as_bytes()), + Sha256::digest(right.as_bytes()), + ); + left.iter() + .zip(right.iter()) + .fold(0_u8, |diff, (a, b)| diff | (a ^ b)) + == 0 +} + +/// An OAuth error code when it is a plain lowercase word, else a fixed +/// placeholder: the callback's text is never echoed. +fn plain_code(error: &str) -> String { + let plain = (1..=40).contains(&error.len()) + && error + .bytes() + .all(|byte| byte.is_ascii_lowercase() || byte == b'_'); + if plain { + error.to_owned() + } else { + "unrecognized_error".to_owned() + } +} + +/// A fresh `urn:uuid:` version-4 `ext_agent_host_id`. +pub(super) fn new_agent_host_id() -> Option { + let mut bytes = [0_u8; 16]; + aws_lc_rs::rand::fill(&mut bytes).ok()?; + bytes[6] = (bytes[6] & 0x0f) | 0x40; + bytes[8] = (bytes[8] & 0x3f) | 0x80; + let mut id = String::from("urn:uuid:"); + for (index, byte) in bytes.iter().enumerate() { + if matches!(index, 4 | 6 | 8 | 10) { + id.push('-'); + } + let _ = write!(id, "{byte:02x}"); + } + Some(id) +} diff --git a/crates/gateway/chatgpt-plan/src/plan/sign_out.rs b/crates/gateway/chatgpt-plan/src/plan/sign_out.rs new file mode 100644 index 00000000..b8bbc19b --- /dev/null +++ b/crates/gateway/chatgpt-plan/src/plan/sign_out.rs @@ -0,0 +1,163 @@ +//! Signing out, forgetting the account, and resetting an unreadable record. +//! +//! Sign-out is local first. The generation is bumped and the record saved as +//! signed out before the network is touched, so a stalled revocation cannot +//! keep the session usable, and any refresh or sign-in still in flight has +//! its result revoked instead of saved. The remote revocation is then +//! attempted once, bounded, and reported honestly: confirmed only when the +//! issuer answered 200. + +use std::sync::Arc; + +use super::{ChatGptPlan, Inner, PlanError}; +use crate::store::{Record, Revocation, Session}; + +/// What a sign-out did. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct SignedOut { + /// Whether the issuer confirmed the revocation. `false` covers a + /// failed, timed-out or unneeded revocation: the grant may still be + /// live at the issuer, and the user can disconnect the app there. + pub revoked: bool, +} + +impl ChatGptPlan { + /// Signs out here, then revokes the refresh token at the issuer. The + /// registration and the install's `ext_agent_host_id` are kept for the + /// next sign-in. + /// + /// # Errors + /// [`PlanError::Store`] when the signed-out record could not be saved. + /// The sign-in is unusable in this process either way. + pub async fn sign_out(&self) -> Result { + let inner = self.inner.clone(); + // The broker owns the sign-out, so the revocation is attempted even + // if the caller goes away. + tokio::spawn(async move { inner.sign_out(false).await }) + .await + .map_err(|_| PlanError::Interrupted("sign-out task stopped"))? + } + + /// Signs out, then deletes the registration and the account, keeping + /// only the install's `ext_agent_host_id`. + /// + /// # Errors + /// The errors of [`ChatGptPlan::sign_out`] and of saving the record. + pub async fn forget(&self) -> Result { + let inner = self.inner.clone(); + tokio::spawn(async move { inner.sign_out(true).await }) + .await + .map_err(|_| PlanError::Interrupted("forget task stopped"))? + } + + /// Deletes the saved record, readable or not. The next sign-in starts a + /// new install, with a new `ext_agent_host_id`. This is the way out of an + /// unreadable record. + /// + /// # Errors + /// [`PlanError::Store`] when the file cannot be removed. + pub fn reset(&self) -> Result<(), PlanError> { + let mut state = self.inner.lock(); + self.inner.store.remove()?; + state.generation += 1; + state.attempts_started += 1; + state.record = None; + state.access = None; + state.attempt = None; + state.completing = None; + state.unreadable = false; + state.unconfirmed_cleanup = false; + state.signed_out_retirement = None; + Ok(()) + } +} + +impl Inner { + /// Signs out. With `forget`, the same critical section also drops the + /// registration and the account, so no sign-in can land between the two. + async fn sign_out(self: Arc, forget: bool) -> Result { + // Local first. The generation is bumped in the same critical + // section that signs out, so nothing in flight can read the new + // generation while the record still looks signed in. + let (retirement_id, held, saved) = { + let mut state = self.lock(); + state.generation += 1; + state.attempts_started += 1; + state.access = None; + state.attempt = None; + state.completing = None; + let Some(record) = state.record.as_mut() else { + return Ok(SignedOut { revoked: false }); + }; + let prior_unconfirmed = matches!( + record.session, + Session::SignedOut { + revocation: Some(Revocation::Unconfirmed), + } + ); + let held = record + .session + .refresh_token() + .cloned() + .zip(record.client_id.clone()); + if forget { + // An unconfirmed cleanup outlives the account it was for. + let unconfirmed_cleanup = record.unconfirmed_cleanup; + *record = Record::new(record.agent_host_id.clone()); + record.unconfirmed_cleanup = unconfirmed_cleanup; + } + record.session = Session::SignedOut { + revocation: (held.is_some() || prior_unconfirmed) + .then_some(Revocation::Unconfirmed), + }; + let saved = self.store.save(record); + let retirement_id = if held.is_some() { + state.next_retirement_id += 1; + let id = state.next_retirement_id; + state.pending_retirements.insert(id); + state.signed_out_retirement = Some(id); + Some(id) + } else { + if !prior_unconfirmed { + state.signed_out_retirement = None; + } + None + }; + (retirement_id, held, saved) + }; + let (Some(retirement_id), Some((refresh_token, client_id))) = (retirement_id, held) else { + saved?; + return Ok(SignedOut { revoked: false }); + }; + + let revoked = self.oauth.revoke(&client_id, &refresh_token).await; + tracing::info!(revoked, "signed out of ChatGPT"); + let superseded_unconfirmed = { + let mut state = self.lock(); + state.pending_retirements.remove(&retirement_id); + let still_owns_signed_out = state.signed_out_retirement == Some(retirement_id); + if still_owns_signed_out { + state.signed_out_retirement = None; + } + if revoked { + if still_owns_signed_out && let Some(record) = state.record.as_mut() { + record.session = Session::SignedOut { + revocation: Some(Revocation::Confirmed), + }; + let _ = self.store.save(record); + } + false + } else { + !still_owns_signed_out + } + }; + if superseded_unconfirmed { + // A newer sign-in or reset superseded the signed-out record while + // this revocation was in flight; carry the unconfirmed retirement + // into cleanup_unconfirmed so status still shows it. + self.note_unconfirmed_cleanup(); + } + saved?; + Ok(SignedOut { revoked }) + } +} diff --git a/crates/gateway/chatgpt-plan/src/store-record.rs b/crates/gateway/chatgpt-plan/src/store-record.rs new file mode 100644 index 00000000..01fb9560 --- /dev/null +++ b/crates/gateway/chatgpt-plan/src/store-record.rs @@ -0,0 +1,186 @@ +//! The persisted sign-in record and its closed session lifecycle. + +use std::fmt; + +use serde::{Deserialize, Serialize}; + +/// A secret string that never prints: `Debug` shows ``. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(transparent)] +pub struct Secret(String); + +impl Secret { + /// Wraps a token. + #[must_use] + pub fn new(value: String) -> Secret { + Secret(value) + } + + /// The token, for the one request that must carry it. + #[must_use] + pub fn expose(&self) -> &str { + &self.0 + } +} + +impl fmt::Debug for Secret { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("") + } +} + +/// Where the sign-in stands, as reported externally. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Status { + /// Signed in: a refresh token is held. + Active, + /// Signed out by the user. The registration is kept for the next sign-in. + SignedOut, + /// The credential can no longer be trusted (a refused refresh, a refresh + /// with an unknown outcome): the user must sign in again. + NeedsAuth, +} + +/// What the last remote revocation is known to have done. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Revocation { + /// OpenAI answered the revoke with HTTP 200. + Confirmed, + /// The revoke failed or timed out: the grant may still be live. + Unconfirmed, +} + +/// The credentials of a live sign-in. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Credentials { + /// The refresh token held for the next rotation. + pub refresh_token: Secret, + /// The scopes the last token response granted. + pub scopes: Vec, +} + +/// The mutually exclusive state of a ChatGPT sign-in. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "status", rename_all = "snake_case")] +pub enum Session { + /// Signed out by the user, or not yet signed in. No token is held. + SignedOut { + /// The last sign-out's remote outcome, if any. + #[serde(default, skip_serializing_if = "Option::is_none")] + revocation: Option, + }, + /// Signed in: a refresh token is held and no refresh is in flight. + Active(Credentials), + /// Saved before a refresh request is sent. Found on disk at start, its + /// outcome is unknown, so [`Session::NeedsAuth`] replaces it without + /// retrying. + Refreshing(Credentials), + /// The credential can no longer be trusted; the user must sign in again. + NeedsAuth { + /// Kept when the stored token may still be live at the issuer (an + /// unknown refresh outcome or a rejected non-rotating refresh), so + /// sign-out or a new sign-in can still attempt revocation; never used + /// to refresh. + #[serde(default, skip_serializing_if = "Option::is_none")] + refresh_token: Option, + }, +} + +impl Session { + /// The external [`Status`] this session reports. + #[must_use] + pub fn status(&self) -> Status { + match self { + Session::SignedOut { .. } => Status::SignedOut, + Session::Active(_) | Session::Refreshing(_) => Status::Active, + Session::NeedsAuth { .. } => Status::NeedsAuth, + } + } + + /// Any refresh token still held, whether usable or awaiting revocation. + #[must_use] + pub fn refresh_token(&self) -> Option<&Secret> { + match self { + Session::SignedOut { .. } => None, + Session::Active(creds) | Session::Refreshing(creds) => Some(&creds.refresh_token), + Session::NeedsAuth { refresh_token } => refresh_token.as_ref(), + } + } + + /// The granted scopes while signed in; empty otherwise. + #[must_use] + pub fn scopes(&self) -> &[String] { + match self { + Session::Active(creds) | Session::Refreshing(creds) => &creds.scopes, + Session::SignedOut { .. } | Session::NeedsAuth { .. } => &[], + } + } + + /// The last sign-out's remote outcome, while signed out. + #[must_use] + pub fn revocation(&self) -> Option { + match self { + Session::SignedOut { revocation } => *revocation, + Session::Active(_) | Session::Refreshing(_) | Session::NeedsAuth { .. } => None, + } + } +} + +/// The persisted sign-in. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Record { + /// The record's format. + pub version: u32, + /// The install's `ext_agent_host_id`, `urn:uuid:…`. Kept across + /// sign-outs. + pub agent_host_id: String, + /// The issued `oaiapp_…` client id, once registered. + pub client_id: Option, + /// The account the registration belongs to (`sub`). + pub subject: Option, + /// The account's email, for "Signed in as" and the next `login_hint`. + pub email: Option, + /// Where the sign-in stands and the credentials it holds, if any. + pub session: Session, + /// A refresh token the broker did not keep could not be confirmed + /// revoked, so it may still be live at the issuer. Only a reset clears + /// this: the remedy is to disconnect the app in ChatGPT settings. + #[serde(default)] + pub unconfirmed_cleanup: bool, +} + +impl Record { + /// The current format. + pub const VERSION: u32 = 1; + + /// A first record for a new install, with its `agent_host_id` and + /// nothing signed in. + #[must_use] + pub fn new(agent_host_id: String) -> Record { + Record { + version: Record::VERSION, + agent_host_id, + client_id: None, + subject: None, + email: None, + session: Session::SignedOut { revocation: None }, + unconfirmed_cleanup: false, + } + } + + /// Whether the registration fields agree with [`Record::session`]. + pub(super) fn is_consistent(&self) -> bool { + let has_client = self.client_id.as_deref().is_some_and(|id| !id.is_empty()); + let has_subject = self.subject.as_deref().is_some_and(|sub| !sub.is_empty()); + match &self.session { + Session::SignedOut { .. } => { + (self.subject.is_none() && self.email.is_none()) || has_client + } + Session::Active(_) | Session::Refreshing(_) | Session::NeedsAuth { .. } => { + has_client && has_subject + } + } + } +} diff --git a/crates/gateway/chatgpt-plan/src/store-tests.rs b/crates/gateway/chatgpt-plan/src/store-tests.rs new file mode 100644 index 00000000..3c05790d --- /dev/null +++ b/crates/gateway/chatgpt-plan/src/store-tests.rs @@ -0,0 +1,495 @@ +//! Tests for the owner-only sign-in record, private directory, and lock. + +use super::*; + +fn active() -> Record { + Record { + client_id: Some("oaiapp_1".to_owned()), + subject: Some("user-1".to_owned()), + email: Some("a@example.test".to_owned()), + session: Session::Active(Credentials { + refresh_token: Secret::new("rt-secret".to_owned()), + scopes: vec!["chatgpt.tokens.use.direct".to_owned()], + }), + ..Record::new("urn:uuid:00000000-0000-4000-8000-000000000000".to_owned()) + } +} + +fn private_dir() -> tempfile::TempDir { + let dir = tempfile::TempDir::new().expect("tempdir"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt as _; + fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o700)).expect("chmod"); + } + dir +} + +#[cfg(unix)] +fn write_owner_only(path: &Path, bytes: &[u8]) { + use std::os::unix::fs::{OpenOptionsExt as _, PermissionsExt as _}; + + let mut file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .mode(0o600) + .open(path) + .expect("open"); + fs::set_permissions(path, fs::Permissions::from_mode(0o600)).expect("chmod"); + file.write_all(bytes).expect("write"); +} + +#[cfg(not(unix))] +#[test] +fn unsupported_platform_refuses_to_open() { + let dir = private_dir(); + assert!(matches!( + Store::open(dir.path()), + Err(StoreError::Unsupported) + )); +} + +#[cfg(unix)] +#[test] +fn no_record_reads_as_none() { + let dir = private_dir(); + assert!( + Store::open(dir.path()) + .expect("opens") + .load() + .expect("loads") + .is_none() + ); +} + +#[cfg(unix)] +#[test] +fn a_saved_record_round_trips_owner_only_and_leaves_no_temp_file() { + use std::os::unix::fs::PermissionsExt as _; + + let dir = private_dir(); + let store = Store::open(dir.path()).expect("opens"); + store.save(&active()).expect("saves"); + assert_eq!(store.load().expect("loads"), Some(active())); + + let store_dir = dir.path().join(STORE_DIR); + let dir_mode = fs::symlink_metadata(&store_dir) + .expect("dir meta") + .permissions() + .mode(); + assert_eq!(dir_mode & 0o777, 0o700); + + for name in [RECORD_FILE, LOCK_FILE] { + let mode = fs::symlink_metadata(store_dir.join(name)) + .expect("file meta") + .permissions() + .mode(); + assert_eq!(mode & 0o777, 0o600, "{name}"); + } + + let mut names: Vec<_> = fs::read_dir(&store_dir) + .expect("list") + .map(|entry| entry.expect("entry").file_name()) + .collect(); + names.sort(); + assert_eq!( + names, + vec![ + std::ffi::OsString::from(RECORD_FILE), + std::ffi::OsString::from(LOCK_FILE), + ] + ); +} + +#[cfg(unix)] +#[test] +fn a_0775_profile_directory_works_by_creating_a_0700_store_subdirectory() { + use std::os::unix::fs::PermissionsExt as _; + + let dir = tempfile::TempDir::new().expect("tempdir"); + fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o775)).expect("chmod 0775"); + let config_file = dir.path().join("gateway.toml"); + fs::write(&config_file, b"# profile config\n").expect("write"); + + let store = Store::open(dir.path()).expect("opens under 0775 parent"); + store.save(&active()).expect("saves"); + assert_eq!(store.load().expect("loads"), Some(active())); + + let parent_mode = fs::symlink_metadata(dir.path()) + .expect("parent meta") + .permissions() + .mode(); + assert_eq!( + parent_mode & 0o777, + 0o775, + "parent permissions stay untouched" + ); + let sub_mode = fs::symlink_metadata(dir.path().join(STORE_DIR)) + .expect("sub meta") + .permissions() + .mode(); + assert_eq!(sub_mode & 0o777, 0o700); + assert!(config_file.exists()); +} + +#[cfg(unix)] +#[test] +fn a_group_writable_directory_is_refused_and_nothing_is_written() { + use std::os::unix::fs::{DirBuilderExt as _, PermissionsExt as _}; + + // Pre-existing group- or other-accessible `chatgpt-plan/` is refused on open. + for bad_mode in [0o770, 0o750, 0o710, 0o704, 0o755] { + let dir = private_dir(); + let store_dir = dir.path().join(STORE_DIR); + fs::DirBuilder::new() + .mode(0o700) + .create(&store_dir) + .expect("mkdir"); + fs::set_permissions(&store_dir, fs::Permissions::from_mode(bad_mode)).expect("chmod"); + assert!( + matches!(Store::open(dir.path()), Err(StoreError::UnsafeDirectory(_))), + "mode {bad_mode:#o}" + ); + } + + // Widening `chatgpt-plan/` after open is refused by both save and load. + let dir = private_dir(); + let store = Store::open(dir.path()).expect("opens"); + let store_dir = dir.path().join(STORE_DIR); + fs::set_permissions(&store_dir, fs::Permissions::from_mode(0o770)).expect("chmod"); + assert!(matches!( + store.save(&active()), + Err(StoreError::UnsafeDirectory(_)) + )); + assert!(!store_dir.join(RECORD_FILE).exists()); + assert!(matches!(store.load(), Err(StoreError::UnsafeDirectory(_)))); +} + +#[cfg(unix)] +#[test] +fn a_symlinked_directory_is_refused() { + use std::os::unix::fs::DirBuilderExt as _; + + // A symlinked profile directory is refused. + let dir = private_dir(); + let real = dir.path().join("real"); + fs::DirBuilder::new() + .mode(0o700) + .create(&real) + .expect("mkdir"); + let link = dir.path().join("link"); + std::os::unix::fs::symlink(&real, &link).expect("symlink"); + assert!(matches!( + Store::open(&link), + Err(StoreError::UnsafeDirectory(_)) + )); + + // A symlinked `chatgpt-plan/` subdirectory inside a real profile directory is refused. + let profile = private_dir(); + std::os::unix::fs::symlink(&real, profile.path().join(STORE_DIR)).expect("symlink subdir"); + assert!(matches!( + Store::open(profile.path()), + Err(StoreError::UnsafeDirectory(_)) + )); + + // Replacing `chatgpt-plan/` with a symlink after open is refused on load and save. + let swapped = private_dir(); + let store = Store::open(swapped.path()).expect("opens"); + let moved = swapped.path().join("moved"); + fs::rename(swapped.path().join(STORE_DIR), &moved).expect("move"); + std::os::unix::fs::symlink(&moved, swapped.path().join(STORE_DIR)).expect("swap symlink"); + assert!(matches!(store.load(), Err(StoreError::UnsafeDirectory(_)))); + assert!(matches!( + store.save(&active()), + Err(StoreError::UnsafeDirectory(_)) + )); +} + +#[cfg(unix)] +#[test] +fn a_symlinked_or_non_regular_record_is_refused_without_reading() { + let dir = private_dir(); + let store = Store::open(dir.path()).expect("opens"); + let store_dir = dir.path().join(STORE_DIR); + let record_path = store_dir.join(RECORD_FILE); + + // Even when the symlink target is a valid `0600` record owned by this user, + // `load` refuses the symlink itself. + let target = store_dir.join("elsewhere.json"); + let valid = serde_json::to_vec_pretty(&active()).expect("json"); + write_owner_only(&target, &valid); + std::os::unix::fs::symlink(&target, &record_path).expect("symlink record"); + assert!(matches!( + store.load(), + Err(StoreError::UnsafeFile("not a regular file")) + )); + fs::remove_file(&record_path).expect("unlink"); + + // A directory at the record path is refused as not a regular file. + fs::create_dir(&record_path).expect("mkdir at record path"); + assert!(matches!( + store.load(), + Err(StoreError::UnsafeFile("not a regular file")) + )); +} + +#[cfg(unix)] +#[test] +fn a_group_or_other_accessible_record_is_refused_without_reading() { + use std::os::unix::fs::PermissionsExt as _; + + let dir = private_dir(); + let store = Store::open(dir.path()).expect("opens"); + store.save(&active()).expect("saves"); + let record_path = dir.path().join(STORE_DIR).join(RECORD_FILE); + + for bad_mode in [0o640, 0o620, 0o604, 0o644] { + fs::set_permissions(&record_path, fs::Permissions::from_mode(bad_mode)).expect("chmod"); + assert!( + matches!( + store.load(), + Err(StoreError::UnsafeFile("accessible by group or others")) + ), + "mode {bad_mode:#o}" + ); + } + + fs::set_permissions(&record_path, fs::Permissions::from_mode(0o600)).expect("restore 0600"); + assert_eq!(store.load().expect("loads"), Some(active())); +} + +#[cfg(unix)] +#[test] +fn a_second_store_on_the_same_directory_is_refused_until_the_first_drops() { + use std::os::unix::fs::PermissionsExt as _; + + let dir = private_dir(); + let first = Store::open(dir.path()).expect("first opens"); + first.save(&active()).expect("saves"); + + assert!(matches!(Store::open(dir.path()), Err(StoreError::Locked))); + + drop(first); + let second = Store::open(dir.path()).expect("reopens once unlocked"); + assert_eq!(second.load().expect("loads"), Some(active())); + drop(second); + + // A widened or symlinked lock file is refused on open too. + let lock_path = dir.path().join(STORE_DIR).join(LOCK_FILE); + fs::set_permissions(&lock_path, fs::Permissions::from_mode(0o640)).expect("chmod lock"); + assert!(matches!( + Store::open(dir.path()), + Err(StoreError::UnsafeFile(_)) + )); + fs::remove_file(&lock_path).expect("remove widened lock"); + let target = dir.path().join(STORE_DIR).join("other"); + write_owner_only(&target, b""); + std::os::unix::fs::symlink(&target, &lock_path).expect("symlink lock"); + assert!(matches!( + Store::open(dir.path()), + Err(StoreError::UnsafeFile(_)) + )); +} + +#[cfg(unix)] +#[test] +fn a_corrupt_or_future_record_is_reported_never_guessed() { + let dir = private_dir(); + let store = Store::open(dir.path()).expect("opens"); + let record_path = dir.path().join(STORE_DIR).join(RECORD_FILE); + write_owner_only(&record_path, b"{ not json"); + assert!(matches!(store.load(), Err(StoreError::Corrupt))); + + let mut future = serde_json::to_value(active()).expect("value"); + future["version"] = serde_json::json!(2); + write_owner_only(&record_path, future.to_string().as_bytes()); + assert!(matches!(store.load(), Err(StoreError::Corrupt))); +} + +#[cfg(unix)] +#[test] +fn a_flat_record_is_reported_as_unreadable_never_silently_dropped() { + let dir = private_dir(); + let store = Store::open(dir.path()).expect("opens"); + let record_path = dir.path().join(STORE_DIR).join(RECORD_FILE); + + for flat in [ + serde_json::json!({ + "version": 1, + "agent_host_id": "urn:uuid:00000000-0000-4000-8000-000000000000", + "client_id": "oaiapp_1", + "subject": "user-1", + "email": "a@example.test", + "refresh_token": "rt-secret", + "scopes": ["chatgpt.tokens.use.direct"], + "status": "active", + "refresh_dispatched": false, + "revocation": null, + "unconfirmed_cleanup": false, + }), + serde_json::json!({ + "version": 1, + "agent_host_id": "urn:uuid:00000000-0000-4000-8000-000000000000", + "client_id": null, + "subject": null, + "email": null, + "refresh_token": null, + "scopes": [], + "status": "signed_out", + "refresh_dispatched": false, + "revocation": null, + "unconfirmed_cleanup": false, + }), + ] { + write_owner_only(&record_path, flat.to_string().as_bytes()); + assert!(matches!(store.load(), Err(StoreError::Corrupt)), "{flat}"); + } + drop(store); + + let plan = crate::plan::ChatGptPlan::open(dir.path(), crate::oauth::OAuthConfig::openai()) + .expect("broker opens and reports the record unreadable"); + let status = plan.status(); + assert!(status.unreadable); + assert_eq!(status.status, None); +} + +#[cfg(unix)] +#[test] +fn each_session_variant_round_trips_and_inconsistent_records_are_refused() { + let dir = private_dir(); + let store = Store::open(dir.path()).expect("opens"); + let record_path = dir.path().join(STORE_DIR).join(RECORD_FILE); + + let creds = Credentials { + refresh_token: Secret::new("rt-secret".to_owned()), + scopes: vec!["chatgpt.tokens.use.direct".to_owned()], + }; + for session in [ + Session::SignedOut { + revocation: Some(Revocation::Confirmed), + }, + Session::Active(creds.clone()), + Session::Refreshing(creds), + Session::NeedsAuth { + refresh_token: None, + }, + Session::NeedsAuth { + refresh_token: Some(Secret::new("rt-maybe-live".to_owned())), + }, + ] { + let expected = Record { + session, + ..active() + }; + store.save(&expected).expect("saves"); + assert_eq!(store.load().expect("loads"), Some(expected.clone())); + if matches!( + expected.session, + Session::SignedOut { .. } + | Session::NeedsAuth { + refresh_token: None + } + ) { + let raw = fs::read_to_string(&record_path).expect("raw"); + assert!(!raw.contains("refresh_token"), "{raw}"); + } + } + + // A non-SignedOut session without `client_id` or `subject`, or an account + // subject without `client_id`, is rejected as corrupt on load. + for mut bad in [ + Record { + client_id: None, + ..active() + }, + Record { + subject: None, + ..active() + }, + Record { + client_id: Some(String::new()), + ..active() + }, + Record { + subject: Some(String::new()), + ..active() + }, + Record { + client_id: None, + subject: Some("user-1".to_owned()), + session: Session::SignedOut { revocation: None }, + ..Record::new("urn:uuid:00000000-0000-4000-8000-000000000000".to_owned()) + }, + ] { + let bytes = serde_json::to_vec_pretty(&bad).expect("json"); + write_owner_only(&record_path, &bytes); + assert!(matches!(store.load(), Err(StoreError::Corrupt)), "{bad:?}"); + if matches!(bad.session, Session::Active(_)) { + bad.session = Session::NeedsAuth { + refresh_token: None, + }; + let bytes = serde_json::to_vec_pretty(&bad).expect("json"); + write_owner_only(&record_path, &bytes); + assert!(matches!(store.load(), Err(StoreError::Corrupt)), "{bad:?}"); + } + } +} + +#[test] +fn debug_output_never_shows_the_refresh_token() { + let shown = format!("{:?}", active()); + assert!(!shown.contains("rt-secret"), "{shown}"); + assert!(shown.contains("")); +} + +#[cfg(unix)] +#[test] +fn remove_is_idempotent() { + let dir = private_dir(); + let store = Store::open(dir.path()).expect("opens"); + store.remove().expect("nothing to remove"); + store.save(&active()).expect("saves"); + store.remove().expect("removes"); + assert!(store.load().expect("loads").is_none()); +} + +#[cfg(unix)] +#[test] +fn a_temp_file_left_by_a_crash_is_removed_by_load_and_by_save() { + use std::os::unix::fs::DirBuilderExt as _; + + let dir = private_dir(); + let store_dir = dir.path().join(STORE_DIR); + fs::DirBuilder::new() + .mode(0o700) + .create(&store_dir) + .expect("mkdir"); + let crashed_before_open = store_dir.join(".chatgpt-plan.json.before.tmp"); + write_owner_only(&crashed_before_open, b"{\"refresh_token\":\"rt-secret\"}"); + + // `Store::open` reaps crash residue once it holds the lock. + let store = Store::open(dir.path()).expect("opens"); + assert!(!crashed_before_open.exists(), "open reaps the stale temp"); + + // A contended second opener fails before `reap_stale_temps` can touch an + // active temp file belonging to the lock holder. + let in_flight = store_dir.join(".chatgpt-plan.json.inflight.tmp"); + write_owner_only(&in_flight, b"{\"refresh_token\":\"rt-live\"}"); + assert!(matches!(Store::open(dir.path()), Err(StoreError::Locked))); + assert!( + in_flight.exists(), + "a locked-out opener never reaps under the holder's feet" + ); + + let unrelated = store_dir.join("notes.txt"); + write_owner_only(&unrelated, b"x"); + assert!(store.load().expect("loads").is_none()); + assert!(!in_flight.exists(), "load reaps the stale temp"); + + let stale = store_dir.join(".chatgpt-plan.json.deadbeef.tmp"); + write_owner_only(&stale, b"{}"); + store.save(&active()).expect("saves"); + assert!(!stale.exists(), "save reaps the stale temp"); + assert!(unrelated.exists(), "other files are untouched"); +} diff --git a/crates/gateway/chatgpt-plan/src/store.rs b/crates/gateway/chatgpt-plan/src/store.rs new file mode 100644 index 00000000..02070e75 --- /dev/null +++ b/crates/gateway/chatgpt-plan/src/store.rs @@ -0,0 +1,417 @@ +//! The owner-only record of one ChatGPT sign-in, on disk. +//! +//! One file, `chatgpt-plan.json`, inside the `0700` `chatgpt-plan/` +//! subdirectory of the Gateway's profile directory, so a `0775` profile +//! directory under `umask 0002` works while keeping the record, temp files, +//! and lock private to this user. It holds `ext_agent_host_id`, the +//! registration (`client_id` and `sub`), and the closed [`Session`] lifecycle +//! ([`Session::SignedOut`], [`Session::Active`], [`Session::Refreshing`], +//! [`Session::NeedsAuth`]). The access token is never written: one refresh +//! after a restart mints a fresh one in memory. +//! +//! [`Store::open`] refuses symlinks on both the profile directory and +//! `chatgpt-plan/`, requires `chatgpt-plan/` to belong to this user with no +//! group or other access (`mode & 0o077 == 0`), holds an exclusive lock on +//! `chatgpt-plan.lock` for the life of [`Store`], and reaps crashed temp files +//! only under that lock. [`Store::load`] verifies a regular owner-only file of +//! this user before reading from the opened handle. Every write creates an +//! exclusive `0600` temp file, syncs it, renames it over the record, and syncs +//! the directory. Unverifiable or unparsable records fail closed. +//! +//! ## Windows DACL design (not yet enabled) +//! +//! [`Store::open`] returns [`StoreError::Unsupported`] on Windows until native +//! owner-only DACLs land (never `icacls`, which races creation, leaves +//! explicit ACEs for other local accounts intact, and depends on locale +//! strings): +//! 1. Cache the current user SID via `OpenProcessToken`, `GetTokenInformation` +//! (`TokenUser`), and `ConvertSidToStringSidW`. +//! 2. Open `` and `\chatgpt-plan` with `CreateFileW` +//! (`FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT`), reject +//! `FILE_ATTRIBUTE_REPARSE_POINT`, and replace the directory DACL via +//! `ConvertStringSecurityDescriptorToSecurityDescriptorW` + +//! `SetSecurityInfo` (`DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION`) +//! using `D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;)`. +//! 3. Lock `chatgpt-plan.lock` via `LockFileEx` +//! (`LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY`). +//! 4. Create temp files born owner-only via `CreateFileW` (`CREATE_NEW`, +//! `dwShareMode = 0`, `SECURITY_ATTRIBUTES` with +//! `D:P(A;;FA;;;SY)(A;;FA;;;)`), `FlushFileBuffers`, and +//! `MoveFileExW` (`MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH`). +//! 5. On load, open with `FILE_FLAG_OPEN_REPARSE_POINT`, reject reparse +//! points, and check `GetSecurityInfo` + `EqualSid` (`SE_DACL_PROTECTED` +//! and ACE SIDs restricted to `TokenUser` / `WinLocalSystemSid`, plus +//! `WinBuiltinAdministratorsSid` when elevated). +//! +//! Reference: OpenAI's "Registration and sign-in", section "5. Store +//! credentials in a local file", +//! . + +#[cfg(unix)] +use std::fs::{self, OpenOptions}; +use std::io; +#[cfg(unix)] +use std::io::{Read as _, Write as _}; +#[cfg(unix)] +use std::os::unix::fs::{DirBuilderExt as _, MetadataExt as _, OpenOptionsExt as _}; +use std::path::{Path, PathBuf}; + +#[path = "store-record.rs"] +mod record; + +pub use self::record::{Credentials, Record, Revocation, Secret, Session, Status}; + +/// The private subdirectory created inside the Gateway's profile directory. +pub const STORE_DIR: &str = "chatgpt-plan"; + +/// The record's file name inside [`STORE_DIR`]. +pub const RECORD_FILE: &str = "chatgpt-plan.json"; + +/// The exclusive lock file inside [`STORE_DIR`]. +pub const LOCK_FILE: &str = "chatgpt-plan.lock"; + +/// Owner-only mode (`rw-------`) for ancillary files in [`STORE_DIR`]. +#[cfg(unix)] +const OWNER_FILE_MODE: u32 = 0o600; + +/// What every temp file of a write starts and ends with. +#[cfg(unix)] +const TEMP_PREFIX: &str = ".chatgpt-plan.json."; +#[cfg(unix)] +const TEMP_SUFFIX: &str = ".tmp"; + +/// Why the store could not be opened, or its record read or written. +#[derive(Debug, thiserror::Error)] +#[non_exhaustive] +pub enum StoreError { + /// The file exists but does not parse, or has an unknown version. + #[error("the ChatGPT sign-in record is unreadable; sign in again after resetting it")] + Corrupt, + /// The store directory is not a private directory of this user. + #[error("the ChatGPT sign-in directory is not private to this user: {0}")] + UnsafeDirectory(&'static str), + /// A file in the store is not a regular owner-only file of this user. + #[error("the ChatGPT sign-in file is not private to this user: {0}")] + UnsafeFile(&'static str), + /// Another process already holds the store's exclusive lock. + #[error("another process is already using this ChatGPT sign-in store")] + Locked, + /// This platform cannot yet store the record owner-only. + #[error("ChatGPT sign-in is not yet supported on this platform")] + Unsupported, + /// A file-system operation failed while opening, reading, writing, + /// syncing, or removing the store. + #[error("the ChatGPT sign-in store failed while trying to {operation}")] + Io { + /// Which store phase failed. + operation: &'static str, + /// Underlying I/O error kept for internal diagnostics; not emitted in + /// the Display message. + #[source] + source: io::Error, + }, +} + +fn io_op(operation: &'static str) -> impl FnOnce(io::Error) -> StoreError { + move |source| StoreError::Io { operation, source } +} + +/// The record's private directory, exclusive lock, and read and write rules. +#[derive(Debug)] +pub struct Store { + dir: PathBuf, + uid: u32, + #[cfg(unix)] + _lock: fs::File, +} + +impl Store { + /// Opens the private `chatgpt-plan/` store under `profile_dir`, creating + /// it with mode `0700` when absent and holding its exclusive lock. + /// + /// # Errors + /// Returns [`StoreError`] when the directory or lock file is unsafe, + /// already locked, unsupported on this platform, or fails I/O. + pub fn open(profile_dir: &Path) -> Result { + open_store(profile_dir) + } + + fn path(&self) -> PathBuf { + self.dir.join(RECORD_FILE) + } + + /// Reads the record. `Ok(None)` means no sign-in has ever been stored. + /// + /// # Errors + /// Returns [`StoreError`] when the directory or record is unsafe, corrupt, + /// unsupported on this platform, or fails I/O. + pub fn load(&self) -> Result, StoreError> { + let Some(bytes) = read_private(&self.dir, &self.path(), self.uid)? else { + return Ok(None); + }; + let record: Record = serde_json::from_slice(&bytes).map_err(|_| StoreError::Corrupt)?; + if record.version != Record::VERSION || !record.is_consistent() { + return Err(StoreError::Corrupt); + } + Ok(Some(record)) + } + + /// Replaces the record, owner-only and atomically. + /// + /// # Errors + /// Returns [`StoreError`] when the directory is unsafe, unsupported on + /// this platform, or fails I/O. On error the previous record is unchanged. + pub fn save(&self, record: &Record) -> Result<(), StoreError> { + let bytes = serde_json::to_vec_pretty(record) + .map_err(|error| io_op("serialize record")(error.into()))?; + write_private(&self.dir, &self.path(), self.uid, &bytes) + } + + /// Deletes the record: the complete local teardown. A missing record is + /// not an error. + /// + /// # Errors + /// Returns [`StoreError`] on unsupported platforms or when removal fails. + pub fn remove(&self) -> Result<(), StoreError> { + remove_private(&self.dir, &self.path()) + } +} + +#[cfg(unix)] +fn open_store(profile_dir: &Path) -> Result { + if !fs::symlink_metadata(profile_dir) + .map_err(io_op("inspect profile directory"))? + .file_type() + .is_dir() + { + return Err(StoreError::UnsafeDirectory("not a plain directory")); + } + + let dir = profile_dir.join(STORE_DIR); + match fs::DirBuilder::new().mode(0o700).create(&dir) { + Ok(()) => {} + Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {} + Err(error) => return Err(io_op("create store directory")(error)), + } + + let dir_meta = fs::symlink_metadata(&dir).map_err(io_op("inspect store directory"))?; + check_private_dir(&dir_meta, None)?; + let open_meta = fs::File::open(&dir) + .and_then(|handle| handle.metadata()) + .map_err(io_op("open store directory"))?; + if !same_inode(&dir_meta, &open_meta) { + return Err(StoreError::UnsafeDirectory("not a plain directory")); + } + check_private_dir(&open_meta, None)?; + + let (lock, lock_meta) = acquire_lock(&dir.join(LOCK_FILE))?; + let uid = current_uid_in(&dir)?; + check_private_dir(&open_meta, Some(uid))?; + check_private_file(&lock_meta, Some(uid))?; + + // Reaping runs only after the exclusive lock is held, so an active temp + // write from another process can never be mistaken for crash residue. + reap_stale_temps(&dir); + Ok(Store { + dir, + uid, + _lock: lock, + }) +} + +#[cfg(unix)] +fn acquire_lock(lock_path: &Path) -> Result<(fs::File, fs::Metadata), StoreError> { + match fs::symlink_metadata(lock_path) { + Ok(meta) => check_private_file(&meta, None)?, + Err(error) if error.kind() == io::ErrorKind::NotFound => {} + Err(error) => return Err(io_op("inspect lock file")(error)), + } + + let lock = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .mode(OWNER_FILE_MODE) + .open(lock_path) + .map_err(io_op("open lock file"))?; + let fstat = lock.metadata().map_err(io_op("inspect open lock file"))?; + let lstat = fs::symlink_metadata(lock_path).map_err(io_op("inspect lock file"))?; + if !same_inode(&fstat, &lstat) { + return Err(StoreError::UnsafeFile("not a regular file")); + } + check_private_file(&fstat, None)?; + match lock.try_lock() { + Ok(()) => Ok((lock, fstat)), + Err(fs::TryLockError::WouldBlock) => Err(StoreError::Locked), + Err(fs::TryLockError::Error(error)) => Err(io_op("lock store")(error)), + } +} + +#[cfg(unix)] +fn current_uid_in(dir: &Path) -> Result { + let suffix = crate::oauth::random_token(12).ok_or_else(|| { + io_op("generate ownership probe name")(io::Error::other("no system randomness")) + })?; + let probe = dir.join(format!(".chatgpt-plan.probe.{suffix}")); + let file = OpenOptions::new() + .write(true) + .create_new(true) + .mode(OWNER_FILE_MODE) + .open(&probe) + .map_err(io_op("create ownership probe file"))?; + let uid = file + .metadata() + .map(|m| m.uid()) + .map_err(io_op("inspect ownership probe file")); + let _ = fs::remove_file(&probe); + uid +} + +#[cfg(unix)] +fn check_private_dir(meta: &fs::Metadata, uid: Option) -> Result<(), StoreError> { + if !meta.file_type().is_dir() { + return Err(StoreError::UnsafeDirectory("not a plain directory")); + } + if uid.is_some_and(|expected| meta.uid() != expected) { + return Err(StoreError::UnsafeDirectory("owned by another user")); + } + if meta.mode() & 0o077 != 0 { + return Err(StoreError::UnsafeDirectory("accessible by group or others")); + } + Ok(()) +} + +#[cfg(unix)] +fn verify_private_dir(dir: &Path, uid: u32) -> Result<(), StoreError> { + let meta = fs::symlink_metadata(dir).map_err(io_op("inspect store directory"))?; + check_private_dir(&meta, Some(uid)) +} + +#[cfg(unix)] +fn check_private_file(meta: &fs::Metadata, uid: Option) -> Result<(), StoreError> { + if !meta.file_type().is_file() { + return Err(StoreError::UnsafeFile("not a regular file")); + } + if uid.is_some_and(|expected| meta.uid() != expected) { + return Err(StoreError::UnsafeFile("owned by another user")); + } + if meta.mode() & 0o077 != 0 { + return Err(StoreError::UnsafeFile("accessible by group or others")); + } + Ok(()) +} + +#[cfg(unix)] +fn read_private(dir: &Path, target: &Path, uid: u32) -> Result>, StoreError> { + verify_private_dir(dir, uid)?; + reap_stale_temps(dir); + + let lstat = match fs::symlink_metadata(target) { + Ok(meta) => meta, + Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None), + Err(error) => return Err(io_op("inspect record file")(error)), + }; + check_private_file(&lstat, Some(uid))?; + + let mut file = fs::File::open(target).map_err(io_op("open record file"))?; + let fstat = file.metadata().map_err(io_op("inspect open record file"))?; + if !same_inode(&lstat, &fstat) { + return Err(StoreError::UnsafeFile("not a regular file")); + } + check_private_file(&fstat, Some(uid))?; + + let mut bytes = Vec::new(); + file.read_to_end(&mut bytes) + .map_err(io_op("read record file"))?; + Ok(Some(bytes)) +} + +/// Writes `bytes` to `target` in `dir` through a fresh owner-only temp file, +/// then syncs `dir` so the rename is durable. +#[cfg(unix)] +fn write_private(dir: &Path, target: &Path, uid: u32, bytes: &[u8]) -> Result<(), StoreError> { + verify_private_dir(dir, uid)?; + reap_stale_temps(dir); + let suffix = crate::oauth::random_token(12).ok_or_else(|| { + io_op("generate temp record name")(io::Error::other("no system randomness")) + })?; + let temp = dir.join(format!("{TEMP_PREFIX}{suffix}{TEMP_SUFFIX}")); + let result = (|| { + let mut file = OpenOptions::new() + .write(true) + .create_new(true) + .mode(0o600) + .open(&temp) + .map_err(io_op("create temp record file"))?; + file.write_all(bytes) + .map_err(io_op("write temp record file"))?; + file.sync_all().map_err(io_op("sync temp record file"))?; + fs::rename(&temp, target).map_err(io_op("rename record file"))?; + fs::File::open(dir) + .and_then(|dir_file| dir_file.sync_all()) + .map_err(io_op("sync store directory")) + })(); + if result.is_err() { + let _ = fs::remove_file(&temp); + } + result +} + +#[cfg(unix)] +fn remove_private(dir: &Path, target: &Path) -> Result<(), StoreError> { + reap_stale_temps(dir); + match fs::remove_file(target) { + Err(error) if error.kind() != io::ErrorKind::NotFound => { + Err(io_op("remove record file")(error)) + } + _ => Ok(()), + } +} + +#[cfg(unix)] +fn same_inode(a: &fs::Metadata, b: &fs::Metadata) -> bool { + a.dev() == b.dev() && a.ino() == b.ino() +} + +/// Removes temp files a crashed write left behind. Each may hold a refresh +/// token; callers must hold the store's exclusive lock first. +#[cfg(unix)] +fn reap_stale_temps(dir: &Path) { + let Ok(entries) = fs::read_dir(dir) else { + return; + }; + for entry in entries.flatten() { + let name = entry.file_name(); + let name = name.to_string_lossy(); + if name.starts_with(TEMP_PREFIX) && name.ends_with(TEMP_SUFFIX) { + let _ = fs::remove_file(entry.path()); + } + } +} + +/// No owner-only files yet on this platform: refuse rather than read or write +/// a token another account might access. +#[cfg(not(unix))] +fn open_store(_profile_dir: &Path) -> Result { + Err(StoreError::Unsupported) +} + +#[cfg(not(unix))] +fn read_private(_dir: &Path, _target: &Path, _uid: u32) -> Result>, StoreError> { + Err(StoreError::Unsupported) +} + +#[cfg(not(unix))] +fn write_private(_dir: &Path, _target: &Path, _uid: u32, _bytes: &[u8]) -> Result<(), StoreError> { + Err(StoreError::Unsupported) +} + +#[cfg(not(unix))] +fn remove_private(_dir: &Path, _target: &Path) -> Result<(), StoreError> { + Err(StoreError::Unsupported) +} + +#[cfg(test)] +#[path = "store-tests.rs"] +mod tests; diff --git a/crates/gateway/chatgpt-plan/src/testing.rs b/crates/gateway/chatgpt-plan/src/testing.rs new file mode 100644 index 00000000..7c82b364 --- /dev/null +++ b/crates/gateway/chatgpt-plan/src/testing.rs @@ -0,0 +1,55 @@ +//! Test support: an issuer that signs real RS256 JWTs, so verification runs +//! the same code path it runs against OpenAI. + +use std::sync::LazyLock; + +use aws_lc_rs::rand::SystemRandom; +use aws_lc_rs::rsa::{KeyPair, KeySize}; +use aws_lc_rs::signature::{KeyPair as _, RSA_PKCS1_SHA256, RsaPublicKeyComponents}; +use base64::Engine as _; +use base64::engine::general_purpose::URL_SAFE_NO_PAD; +use serde_json::{Value, json}; +use sha2::{Digest as _, Sha256}; + +/// The issuer the tests pin. +#[cfg(test)] +pub(crate) const ISSUER: &str = "https://auth.example.test"; + +/// One generated signing key, shared by every test in the process. +static KEY: LazyLock = + LazyLock::new(|| KeyPair::generate(KeySize::Rsa2048).unwrap_or_else(|_| unreachable!())); + +/// The test issuer's JWKS document, with the key under `kid`. +pub(crate) fn jwks_document(kid: &str) -> Value { + let public = RsaPublicKeyComponents::>::from(KEY.public_key()); + json!({ "keys": [ + { "kty": "EC", "kid": "ignored", "crv": "P-256", "x": "AA", "y": "AA" }, + { "kty": "RSA", "kid": kid, "alg": "RS256", "use": "sig", + "n": URL_SAFE_NO_PAD.encode(&public.n), + "e": URL_SAFE_NO_PAD.encode(&public.e) } + ]}) +} + +/// A JWT with `header` and `claims`, signed by the test key. +pub(crate) fn sign(header: &Value, claims: &Value) -> String { + let signed = format!( + "{}.{}", + URL_SAFE_NO_PAD.encode(header.to_string()), + URL_SAFE_NO_PAD.encode(claims.to_string()) + ); + let mut signature = vec![0_u8; KEY.public_modulus_len()]; + KEY.sign( + &RSA_PKCS1_SHA256, + &SystemRandom::new(), + signed.as_bytes(), + &mut signature, + ) + .unwrap_or_else(|_| unreachable!()); + format!("{signed}.{}", URL_SAFE_NO_PAD.encode(signature)) +} + +/// The `at_hash` of `access_token`. +pub(crate) fn at_hash(access_token: &str) -> String { + let digest = Sha256::digest(access_token.as_bytes()); + URL_SAFE_NO_PAD.encode(&digest[..digest.len() / 2]) +} diff --git a/crates/gateway/config-ui/ui/src/pages/settings-page.ts b/crates/gateway/config-ui/ui/src/pages/settings-page.ts index 3ba5a261..8ab227c3 100644 --- a/crates/gateway/config-ui/ui/src/pages/settings-page.ts +++ b/crates/gateway/config-ui/ui/src/pages/settings-page.ts @@ -1155,6 +1155,7 @@ export function createSettingsPage(deps: SettingsPageDeps): SettingsPage { function entryCard(options: { card: Card; title: string; + badge?: string; dependents: string[]; fields: () => HTMLElement[]; onSave: () => Promise; @@ -1173,6 +1174,12 @@ export function createSettingsPage(deps: SettingsPageDeps): SettingsPage { toggle.setAttribute("aria-expanded", String(expanded.has(card.key))); toggle.textContent = options.title; heading.append(toggle); + if (options.badge) { + const badge = document.createElement("span"); + badge.className = "pill pill-accent entry-badge"; + badge.textContent = options.badge; + heading.append(badge); + } if (options.dependents.length > 0) { const chip = document.createElement("span"); chip.className = "pill used-by-chip"; @@ -1364,53 +1371,79 @@ export function createSettingsPage(deps: SettingsPageDeps): SettingsPage { function endpointCard(card: Card, id: string): HTMLElement { const dependents = card.draft ? [] : endpointDependents(id); + const isPlan = value(card, "auth") === "chatgpt-plan"; + const dominionOptions = store + .dominions() + .filter((dominion) => dominion.kind === "remote") + .map((dominion) => dominion.id); return entryCard({ card, title: id || "(new endpoint)", + badge: isPlan ? "ChatGPT plan" : undefined, dependents, - fields: () => [ - fieldRow(card, { + fields: () => { + const idRow = fieldRow(card, { path: "id", label: "Id", help: "The handle [[model]] entries route through.", type: "input", fallback: "", - }), - fieldRow(card, { - path: "protocol", - label: "Protocol", - help: "The wire protocol this endpoint speaks.", - type: "dropdown", - options: ["openai"], - fallback: "openai", - locked: true, - }), - fieldRow(card, { - path: "base_url", - label: "Base URL", - help: "The backend's base URL (e.g. https://api.openai.com/v1).", - type: "input", - fallback: "", - }), - fieldRow(card, { - path: "api_key", - label: "API key", - help: "The credential sent to this backend.", - type: "secret", - }), - fieldRow(card, { + }); + const dominionRow = fieldRow(card, { path: "dominion", label: "Dominion", help: "Shared concurrency pool governing this endpoint.", type: "dropdown", - options: store - .dominions() - .filter((dominion) => dominion.kind === "remote") - .map((dominion) => dominion.id), + options: dominionOptions, allowNone: true, fallback: null, - }), - ], + }); + if (isPlan) { + const note = document.createElement("p"); + note.className = "field-help plan-endpoint-note"; + note.textContent = "Uses the signed-in ChatGPT plan and is sent to OpenAI only."; + return [ + idRow, + fieldRow(card, { + path: "protocol", + label: "Protocol", + help: "Plan endpoints speak the OpenAI Responses API.", + type: "dropdown", + options: ["openai-responses"], + fallback: "openai-responses", + locked: true, + }), + dominionRow, + note, + ]; + } + return [ + idRow, + fieldRow(card, { + path: "protocol", + label: "Protocol", + help: + "The wire protocol the gateway speaks to this endpoint: chat completions (openai), or the Responses API (openai-responses).", + type: "dropdown", + options: ["openai", "openai-responses"], + fallback: "openai", + }), + fieldRow(card, { + path: "base_url", + label: "Base URL", + help: "The backend's base URL (e.g. https://api.openai.com/v1).", + type: "input", + fallback: "", + }), + fieldRow(card, { + path: "api_key", + label: "API key", + help: "The credential sent to this backend.", + type: "secret", + }), + dominionRow, + ]; + }, onSave: () => saveArrayEntry("endpoint", card, id), onDelete: () => void deleteArrayEntry("endpoint", card, id, dependents), }); @@ -1443,14 +1476,20 @@ export function createSettingsPage(deps: SettingsPageDeps): SettingsPage { ): Promise { const payload = store.buildConfigPayload(); const items = Array.isArray(payload[array]) ? (payload[array] as EntryData[]) : []; + const next = effective(card); + if (array === "endpoint" && next["auth"] === "chatgpt-plan") { + next["protocol"] = "openai-responses"; + delete next["base_url"]; + delete next["api_key"]; + } if (card.draft) { - items.push(effective(card)); + items.push(next); } else { const index = items.findIndex((item) => item["id"] === id); if (index >= 0) { - items[index] = effective(card); + items[index] = next; } else { - items.push(effective(card)); + items.push(next); } } payload[array] = items; diff --git a/crates/gateway/config-ui/ui/src/pages/settings-sections.test.mjs b/crates/gateway/config-ui/ui/src/pages/settings-sections.test.mjs index ce20b6c7..a4c3c1b4 100644 --- a/crates/gateway/config-ui/ui/src/pages/settings-sections.test.mjs +++ b/crates/gateway/config-ui/ui/src/pages/settings-sections.test.mjs @@ -434,7 +434,14 @@ test("an endpoint's api_key stays *** through a save and Change reveals the inpu const protocol = card.querySelector(".field-row[data-key='protocol'] .select"); assert.equal(protocol.value, "openai"); - assert.ok(protocol.disabled, "the protocol dropdown is locked"); + assert.ok(!protocol.disabled, "the protocol dropdown offers both protocols"); + protocol.click(); + const listbox = dom.window.document.getElementById(protocol.getAttribute("aria-controls") ?? `${protocol.id}-listbox`); + assert.deepEqual( + [...listbox.querySelectorAll("[role='option']")].map((option) => option.dataset.value), + ["openai", "openai-responses"], + ); + protocol.click(); assert.equal( card.querySelector(".secret-input"), null, @@ -671,4 +678,146 @@ test("a non-numeric entry in a numeric field raises an alert under the input and assert.equal(fixed.querySelector(".field-error"), null, "a valid number clears the alert"); assert.equal(fixed.querySelector("input").getAttribute("aria-invalid"), null); assert.equal(fixed.querySelector("input").value, "9"); -}); \ No newline at end of file +}); + +test("a chatgpt-plan endpoint hides URL and API key inputs, states its OpenAI-only plan auth, and saves without base_url or api_key", async () => { + const config = modelsFixture(); + config.dominion.push({ + id: "pool-r", + kind: "remote", + max_queue: 100, + policy: "queue", + fair_scheduling: true, + }); + config.endpoint.unshift({ + id: "chatgpt", + protocol: "openai-responses", + auth: "chatgpt-plan", + dominion: null, + }); + config.model.push({ + name: "gpt-plan", + kind: "chat", + description: "Plan model", + context: 128000, + thinking: "always", + upstream: "gpt-6.1-sol", + endpoints: ["chatgpt"], + images: true, + parallel_tool_calls: true, + effort_levels: ["low", "high"], + default_effort: "high", + }); + const stub = fixtureStub({ config }); + const { dom, root } = await bootApp({ key: "k", stub }); + + navigate(dom, "#/settings/endpoints"); + await settle(); + const card = root.querySelector(".entry-card[data-entry='endpoint:chatgpt']"); + assert.ok(card, "the chatgpt-plan endpoint card renders"); + assert.equal( + card.querySelector(".entry-badge")?.textContent, + "ChatGPT plan", + "the collapsed card identifies the plan endpoint", + ); + assert.match(card.querySelector(".used-by-chip").title, /model 'gpt-plan'/); + + card.querySelector(".entry-toggle").click(); + const protocol = card.querySelector(".field-row[data-key='protocol'] .select"); + assert.equal(protocol.value, "openai-responses"); + assert.ok(protocol.disabled, "protocol is locked to openai-responses on a plan endpoint"); + assert.equal( + card.querySelector(".field-row[data-key='base_url']"), + null, + "a plan endpoint renders no base_url input", + ); + assert.equal( + card.querySelector(".field-row[data-key='api_key']"), + null, + "a plan endpoint renders no api_key input", + ); + assert.equal(card.querySelector(".secret-input"), null); + assert.equal(card.querySelector(".secret-change"), null); + assert.match( + card.querySelector(".plan-endpoint-note")?.textContent ?? "", + /signed-in ChatGPT plan.*OpenAI only/, + ); + + chooseDropdown(card, "dominion", "pool-r"); + await settle(); + root.querySelector(".entry-card[data-entry='endpoint:chatgpt'] .card-save").click(); + await settle(); + + const bodies = putBodies(stub, "/admin/config"); + assert.equal(bodies.length, 1); + const savedPlan = bodies[0].endpoint.find((entry) => entry.id === "chatgpt"); + assert.deepEqual(savedPlan, { + id: "chatgpt", + protocol: "openai-responses", + auth: "chatgpt-plan", + dominion: "pool-r", + }); + assert.equal("base_url" in savedPlan, false); + assert.equal("api_key" in savedPlan, false); + assert.equal( + stub.calls.some((call) => call.url.includes("/admin/chatgpt")), + false, + "the browser UI makes no /admin/chatgpt/* requests", + ); +}); + +test("unrelated saves across settings and sibling endpoints never add base_url or api_key to a chatgpt-plan endpoint", async () => { + const config = modelsFixture(); + config.endpoint.unshift({ + id: "chatgpt", + protocol: "openai-responses", + auth: "chatgpt-plan", + dominion: null, + }); + const stub = fixtureStub({ config }); + const { dom, root } = await bootApp({ key: "k", stub }); + + // 1. Save [server] on the Gateway panel. + navigate(dom, "#/settings/gateway"); + await settle(); + changeValue(dom, root.querySelector(".field-row[data-key='bind'] input"), "127.0.0.1:9090"); + await settle(); + root.querySelector(".card-save").click(); + await settle(); + + // 2. Edit and save the sibling API-key endpoint. + navigate(dom, "#/settings/endpoints"); + await settle(); + const sibling = root.querySelector(".entry-card[data-entry='endpoint:openai']"); + sibling.querySelector(".entry-toggle").click(); + changeValue( + dom, + sibling.querySelector(".field-row[data-key='base_url'] input"), + "https://proxy.example/v1", + ); + await settle(); + root.querySelector(".entry-card[data-entry='endpoint:openai'] .card-save").click(); + await settle(); + + // 3. Edit and save a model on the Models view. + navigate(dom, "#/local/llama-leaf"); + await settle(); + changeValue(dom, root.querySelector(".field-row[data-key='description'] textarea"), "tuned"); + await settle(); + root.querySelector(".detail-save").click(); + await settle(); + + const bodies = putBodies(stub, "/admin/config"); + assert.equal(bodies.length, 3, "all three unrelated saves staged cleanly"); + for (const body of bodies) { + const plan = body.endpoint.find((entry) => entry.id === "chatgpt"); + assert.deepEqual(plan, { + id: "chatgpt", + protocol: "openai-responses", + auth: "chatgpt-plan", + dominion: null, + }); + assert.equal("base_url" in plan, false); + assert.equal("api_key" in plan, false); + } +}); diff --git a/crates/gateway/config-ui/ui/src/services/cloud-merge.test.mjs b/crates/gateway/config-ui/ui/src/services/cloud-merge.test.mjs index 32ae91a4..f5c62ae2 100644 --- a/crates/gateway/config-ui/ui/src/services/cloud-merge.test.mjs +++ b/crates/gateway/config-ui/ui/src/services/cloud-merge.test.mjs @@ -107,3 +107,35 @@ test("a provider without an OpenAI-compatible endpoint cannot merge", () => { /OpenAI-compatible/, ); }); + +test("an existing chatgpt-plan endpoint with the provider id is preserved and skipped in favor of an API-key endpoint", () => { + const gpt = sheet.providers.openai.models[0]; + const config = { + endpoint: [ + { + id: "openai", + protocol: "openai-responses", + auth: "chatgpt-plan", + }, + ], + }; + app.mergeCloudModel(config, "openai", sheet.providers.openai, gpt, { name: "gpt-api-1" }); + app.mergeCloudModel(config, "openai", sheet.providers.openai, gpt, { name: "gpt-api-2" }); + + assert.deepEqual(config.endpoint, [ + { + id: "openai", + protocol: "openai-responses", + auth: "chatgpt-plan", + }, + { + id: "openai-api", + protocol: "openai", + base_url: "https://api.openai.com/v1", + api_key: "${OPENAI_API_KEY}", + }, + ]); + assert.deepEqual(config.model[0].endpoints, ["openai-api"]); + assert.deepEqual(config.model[1].endpoints, ["openai-api"]); +}); + diff --git a/crates/gateway/config-ui/ui/src/services/cloud-merge.ts b/crates/gateway/config-ui/ui/src/services/cloud-merge.ts index f52e1662..5681b6c2 100644 --- a/crates/gateway/config-ui/ui/src/services/cloud-merge.ts +++ b/crates/gateway/config-ui/ui/src/services/cloud-merge.ts @@ -67,10 +67,11 @@ export function mergeCloudModel( } const endpoints = arrayOf(config, "endpoint"); - if (!endpoints.some((item) => item["id"] === provider)) { + const target = resolveProviderEndpoint(endpoints, provider); + if (target.create) { const keyVar = slice.env_vars.find((variable) => variable.role === "key"); endpoints.push({ - id: provider, + id: target.id, protocol: "openai", base_url: slice.openai_base_url, ...(keyVar ? { api_key: `\${${keyVar.name}}` } : {}), @@ -84,7 +85,7 @@ export function mergeCloudModel( context, thinking: thinkingMode(entry.thinking), upstream: entry.id, - endpoints: [provider], + endpoints: [target.id], images: entry.images, adaptive_thinking: entry.thinking.adaptive, effort_levels: entry.effort_levels, @@ -99,6 +100,28 @@ export function mergeCloudModel( return config; } +/** + * Picks an API-key endpoint id for `provider`, reusing an existing ordinary + * endpoint when one matches and stepping past any `auth = "chatgpt-plan"` + * endpoint so Cloud sheet models never hijack or duplicate a plan endpoint. + */ +function resolveProviderEndpoint( + endpoints: readonly EntryData[], + provider: string, +): { id: string; create: boolean } { + for (let index = 0; ; index += 1) { + const candidate = + index === 0 ? provider : index === 1 ? `${provider}-api` : `${provider}-api-${index}`; + const existing = endpoints.find((item) => item["id"] === candidate); + if (!existing) { + return { id: candidate, create: true }; + } + if (existing["auth"] !== "chatgpt-plan") { + return { id: candidate, create: false }; + } + } +} + /** One keyed array of the config document, created when absent. */ function arrayOf(config: EntryData, key: string): EntryData[] { const value = config[key]; diff --git a/crates/gateway/config-ui/ui/src/services/config-store.test.mjs b/crates/gateway/config-ui/ui/src/services/config-store.test.mjs index 5d253351..7200d994 100644 --- a/crates/gateway/config-ui/ui/src/services/config-store.test.mjs +++ b/crates/gateway/config-ui/ui/src/services/config-store.test.mjs @@ -107,4 +107,128 @@ test("the persisted selection never appears in a config PUT", async () => { puts.every((call) => !("active_profile" in JSON.parse(call.init.body))), "the envelope's active_profile is stripped before the payload is built", ); + assert.ok( + puts.every((call) => { + const rev = JSON.parse(call.init.body).revision; + return typeof rev === "string" && rev.length === 64; + }), + "every config PUT carries the loaded 64-hex revision precondition", + ); +}); + +test("a stale Settings save after out-of-band ChatGPT model enablement is refused with 409 and tells the user to reload", async () => { + const config = modelsFixture(); + const stub = gatewayStub({ + key: "k", + config, + pending: structuredClone(config), + models: ["qwen-common"], + }); + const { dom, root } = await bootApp({ key: "k", stub }); + + navigate(dom, "#/settings/gateway"); + await settle(); + + // Out-of-band update on disk (ChatGPT sign-in + Add models) while Settings is open. + stub.state.config.endpoint.push({ + id: "chatgpt", + protocol: "openai-responses", + auth: "chatgpt-plan", + }); + stub.state.config.model.push({ + name: "gpt-5", + description: "GPT-5", + context: 400000, + upstream: "gpt-5", + endpoints: ["chatgpt"], + }); + stub.state.pending = structuredClone(stub.state.config); + + const bind = root.querySelector(".field-row[data-key='bind'] input"); + bind.value = "127.0.0.1:9090"; + bind.dispatchEvent(new dom.window.Event("change")); + await settle(); + root.querySelector(".card-save").click(); + await settle(); + + const errorToasts = [...root.querySelectorAll(".toast-error")].map((el) => el.textContent); + assert.ok( + errorToasts.some((text) => /reload/i.test(text)), + `409 refusal tells the user to reload, got: ${JSON.stringify(errorToasts)}`, + ); + assert.ok( + stub.state.pending.model.some((model) => model.name === "gpt-5"), + "out-of-band ChatGPT plan model on disk was not overwritten", + ); + assert.equal( + root.querySelector(".field-row[data-key='bind'] input").value, + "127.0.0.1:9090", + "unsaved form input stays intact after the refusal", + ); +}); + +test("consecutive saves advance the tracked revision seamlessly", async () => { + const config = modelsFixture(); + const stub = gatewayStub({ + key: "k", + config, + pending: structuredClone(config), + models: ["qwen-common"], + }); + const { dom, root } = await bootApp({ key: "k", stub }); + + navigate(dom, "#/settings/gateway"); + await settle(); + + const bind = () => root.querySelector(".field-row[data-key='bind'] input"); + bind().value = "127.0.0.1:9091"; + bind().dispatchEvent(new dom.window.Event("change")); + await settle(); + root.querySelector(".card-save").click(); + await settle(); + + bind().value = "127.0.0.1:9092"; + bind().dispatchEvent(new dom.window.Event("change")); + await settle(); + root.querySelector(".card-save").click(); + await settle(); + + const puts = stub.calls + .filter((call) => call.url.endsWith("/admin/config") && call.init.method === "PUT") + .map((call) => JSON.parse(call.init.body)); + assert.equal(puts.length, 2, "both consecutive saves succeeded"); + assert.notEqual(puts[0].revision, puts[1].revision, "second save used the updated shadow revision"); + assert.equal(stub.state.pending.server.bind, "127.0.0.1:9092"); +}); + +test("save refuses locally when the loaded snapshot lacks a revision token", async () => { + const config = modelsFixture(); + const stub = gatewayStub({ + key: "k", + config, + pending: structuredClone(config), + models: ["qwen-common"], + }); + stub.state.omitPendingRevision = true; + const { dom, root } = await bootApp({ key: "k", stub }); + + navigate(dom, "#/settings/gateway"); + await settle(); + + const bind = root.querySelector(".field-row[data-key='bind'] input"); + bind.value = "127.0.0.1:9099"; + bind.dispatchEvent(new dom.window.Event("change")); + await settle(); + root.querySelector(".card-save").click(); + await settle(); + + const puts = stub.calls.filter( + (call) => call.url.endsWith("/admin/config") && call.init.method === "PUT", + ); + assert.equal(puts.length, 0, "no unconditional PUT /admin/config is sent when revision is missing"); + const errorToasts = [...root.querySelectorAll(".toast-error")].map((el) => el.textContent); + assert.ok( + errorToasts.some((text) => /reload/i.test(text)), + `missing revision tells the user to reload, got: ${JSON.stringify(errorToasts)}`, + ); }); diff --git a/crates/gateway/config-ui/ui/src/services/config-store.ts b/crates/gateway/config-ui/ui/src/services/config-store.ts index f39e5009..dfa1016e 100644 --- a/crates/gateway/config-ui/ui/src/services/config-store.ts +++ b/crates/gateway/config-ui/ui/src/services/config-store.ts @@ -165,6 +165,8 @@ export class ConfigStore { * loaded: `undefined` until then, `null` for no profile. */ private persistedProfile: string | null | undefined = undefined; + /** The SHA-256 revision of the on-disk config `this.pending` was built from. */ + private revision: string | null = null; /** Names of models the running profile exposes (the status list). */ private runningModels: string[] = []; /** Unsaved edits: entry key -> field key -> value. */ @@ -208,6 +210,7 @@ export class ConfigStore { this.running = running; this.pending = pending.config; this.persistedProfile = pending.activeProfile; + this.revision = pending.revision; this.dirty = dirty; this.orphans = visibleOrphans(orphans); this.cache = cache; @@ -231,6 +234,7 @@ export class ConfigStore { ]); this.pending = pending.config; this.persistedProfile = pending.activeProfile; + this.revision = pending.revision; this.dirty = dirty; this.chatTemplates = chatTemplates; } @@ -462,9 +466,43 @@ export class ConfigStore { return structuredClone(this.pending); } + /** Stages the global config shadow, guarding against stale whole-doc writes. */ + private async putShadow(payload: EntryData): Promise { + if (this.revision === null || this.revision.trim() === "") { + throw new Error( + "Configuration has no revision token. Reload the page before saving.", + ); + } + try { + await this.api.putConfig(payload, this.revision); + } catch (error) { + if ( + error instanceof GatewayHttpError && + (error.code === "config_conflict" || error.status === 409) + ) { + throw new GatewayHttpError( + error.status, + "Configuration changed on disk since this page loaded. Reload the page to see the latest configuration before saving.", + error.code, + ); + } + if ( + error instanceof GatewayHttpError && + (error.code === "config_revision_required" || error.status === 428) + ) { + throw new GatewayHttpError( + error.status, + "Configuration has no revision token. Reload the page before saving.", + error.code, + ); + } + throw error; + } + } + /** Stages the global config shadow. */ async savePayload(payload: EntryData): Promise { - await this.api.putConfig(payload); + await this.putShadow(payload); await this.refreshPending(); this.notify(); } @@ -658,7 +696,7 @@ export class ConfigStore { profile["models"] = [...chosen, name]; target = selected; } - await this.api.putConfig(payload); + await this.putShadow(payload); await this.refreshPending(); this.notify(); return { name, profile: target }; @@ -721,7 +759,7 @@ export class ConfigStore { /** Saves one entry's edits (or the draft itself) to the shadow. */ async save(entry: ModelEntry): Promise { - await this.api.putConfig(this.buildSavePayload(entry)); + await this.putShadow(this.buildSavePayload(entry)); if (entry.draft) { this.discardDraft(entry); } else { @@ -737,7 +775,7 @@ export class ConfigStore { this.discardDraft(entry); return; } - await this.api.putConfig(this.buildSavePayload(entry, true)); + await this.putShadow(this.buildSavePayload(entry, true)); this.edits.delete(entryKey(entry)); await this.refreshPending(); this.notify(); diff --git a/crates/gateway/config-ui/ui/src/services/gateway-api.test.mjs b/crates/gateway/config-ui/ui/src/services/gateway-api.test.mjs index c4b113c1..7b127221 100644 --- a/crates/gateway/config-ui/ui/src/services/gateway-api.test.mjs +++ b/crates/gateway/config-ui/ui/src/services/gateway-api.test.mjs @@ -32,7 +32,7 @@ test("a putConfig refusal includes the envelope's error code", async () => { ); const api = new app.GatewayApi({ fetchFn, storage: storageShim() }); - await assert.rejects(api.putConfig({}), (error) => { + await assert.rejects(api.putConfig({}, "a".repeat(64)), (error) => { assert.ok(error instanceof app.GatewayHttpError, "the refusal throws GatewayHttpError"); assert.equal(error.status, 422); assert.equal(error.message, "unknown field `typo`", "the envelope message is kept"); @@ -40,3 +40,18 @@ test("a putConfig refusal includes the envelope's error code", async () => { return true; }); }); + +test("putConfig refuses a missing or empty revision before touching the transport", async () => { + const app = await loadApp(); + let calls = 0; + const fetchFn = async () => { + calls += 1; + return jsonResponse({ shadow: "gateway.toml.next" }, 200); + }; + const api = new app.GatewayApi({ fetchFn, storage: storageShim() }); + + for (const bad of ["", " ", null, undefined]) { + await assert.rejects(api.putConfig({}, bad), /revision token/i); + } + assert.equal(calls, 0, "no unconditional PUT /admin/config may be sent"); +}); diff --git a/crates/gateway/config-ui/ui/src/services/gateway-api.ts b/crates/gateway/config-ui/ui/src/services/gateway-api.ts index 1eb2357c..72eea1a8 100644 --- a/crates/gateway/config-ui/ui/src/services/gateway-api.ts +++ b/crates/gateway/config-ui/ui/src/services/gateway-api.ts @@ -205,6 +205,11 @@ export interface PendingPage { * defines it. */ activeProfile: string | null; + /** + * The on-disk SHA-256 revision the pending view was built from, sent back on + * `PUT /admin/config` so stale whole-document writes fail closed with 409. + */ + revision: string | null; } /** One environment variable a cloud provider reads, from the sheet. */ @@ -530,8 +535,10 @@ export class GatewayApi { */ async getConfigPending(): Promise { const data = requireRecord(await this.getJson("/admin/config-pending"), "pending config"); + const revision = + typeof data["revision"] === "string" && data["revision"] !== "" ? data["revision"] : null; if (data["profile"] === undefined) { - return { config: {}, activeProfile: null }; + return { config: {}, activeProfile: null, revision }; } const { active_profile: selection, ...config } = requireRecord( data["profile"], @@ -540,6 +547,7 @@ export class GatewayApi { return { config, activeProfile: typeof selection === "string" ? selection : null, + revision, }; } @@ -557,13 +565,22 @@ export class GatewayApi { * Stages `body` (the `GET /admin/config` JSON shape) as the config * shadow via `PUT /admin/config`. Untouched secrets stay `"***"`; the * gateway restores their real values. A body with `active_profile` - * is refused: selection belongs to {@link switchProfile}. + * is refused: selection belongs to {@link switchProfile}. Every save + * requires a non-empty `revision` precondition so stale writes fail + * closed with `409 config_conflict` instead of staging unconditionally. */ - async putConfig(body: unknown): Promise { + async putConfig(body: unknown, revision: string): Promise { + if (typeof revision !== "string" || revision.trim() === "") { + throw new Error("Configuration has no revision token. Reload the page before saving."); + } + const payload = isRecord(body) ? { ...body, revision } : body; const response = await this.send("/admin/config", { method: "PUT", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(body), + headers: { + "Content-Type": "application/json", + "x-config-revision": revision, + }, + body: JSON.stringify(payload), }); if (!response.ok) { throw await refusalError(response); diff --git a/crates/gateway/config-ui/ui/src/test-support.mjs b/crates/gateway/config-ui/ui/src/test-support.mjs index fb09fcfc..94a6b4c6 100644 --- a/crates/gateway/config-ui/ui/src/test-support.mjs +++ b/crates/gateway/config-ui/ui/src/test-support.mjs @@ -4,6 +4,7 @@ // in a fresh jsdom window and calls the exported `boot` with injected // dependencies - a stub fetch standing in for the gateway, jsdom's // window for location, sessionStorage, and hashchange. +import { createHash } from "node:crypto"; import { readFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; @@ -45,13 +46,17 @@ export async function settle(turns = 10) { } /** A JSON response the API wrapper can consume. */ -export function jsonResponse(body, status = 200) { +export function jsonResponse(body, status = 200, extraHeaders = {}) { return new Response(JSON.stringify(body), { status, - headers: { "content-type": "application/json" }, + headers: { "content-type": "application/json", ...extraHeaders }, }); } +function configRevision(doc) { + return createHash("sha256").update(JSON.stringify(doc)).digest("hex"); +} + /** An SSE response with push/end controls for staged event delivery. */ export function sseChannel() { let controller; @@ -770,10 +775,14 @@ export function gatewayStub({ return jsonResponse({ profile: name, restart_required: name !== state.active }); } if (url.endsWith("/admin/config-pending")) { - return jsonResponse({ + const envelope = { profile: { ...state.pending, active_profile: state.selected }, boot: null, - }); + }; + if (!state.omitPendingRevision) { + envelope.revision = configRevision(state.pending); + } + return jsonResponse(envelope); } if (url.endsWith("/admin/config-dirty")) { return jsonResponse(state.dirty); @@ -805,7 +814,49 @@ export function gatewayStub({ return staged; } } - const body = JSON.parse(init.body); + const rawBody = JSON.parse(init.body); + const { revision, ...body } = rawBody; + const headers = new Headers(init.headers); + const headerRevision = + headers.get("if-match")?.replace(/^"|"$/g, "") ?? headers.get("x-config-revision"); + const effectiveRevision = revision !== undefined && revision !== null ? revision : headerRevision; + if (effectiveRevision === undefined || effectiveRevision === null) { + return jsonResponse( + { + error: { + message: "config save requires a revision precondition; reload the page before saving", + type: "invalid_request_error", + code: "config_revision_required", + }, + }, + 428, + ); + } + if (typeof effectiveRevision !== "string" || effectiveRevision === "") { + return jsonResponse( + { + error: { + message: "revision must be a non-empty string", + type: "invalid_request_error", + code: "config_write_rejected", + }, + }, + 422, + ); + } + if (effectiveRevision !== configRevision(state.pending)) { + return jsonResponse( + { + error: { + message: + "configuration changed on disk since this page loaded; reload the page before saving", + type: "invalid_request_error", + code: "config_conflict", + }, + }, + 409, + ); + } if ("active_profile" in body) { return jsonResponse( { @@ -819,6 +870,36 @@ export function gatewayStub({ 422, ); } + for (const entry of Array.isArray(body.endpoint) ? body.endpoint : []) { + if (entry && typeof entry === "object" && entry.auth === "chatgpt-plan") { + if (entry.protocol !== "openai-responses") { + return jsonResponse( + { + error: { + message: `endpoint ${entry.id} has auth = "chatgpt-plan", which needs protocol = "openai-responses"`, + type: "invalid_request_error", + code: "config_write_rejected", + }, + }, + 422, + ); + } + for (const forbidden of ["base_url", "api_key"]) { + if (forbidden in entry) { + return jsonResponse( + { + error: { + message: `endpoint ${entry.id} has auth = "chatgpt-plan" and must not set ${forbidden}`, + type: "invalid_request_error", + code: "config_write_rejected", + }, + }, + 422, + ); + } + } + } + } // The boot-owned [server] section survives in the pending view when the body omits it. if (!("server" in body) && "server" in state.pending) { body.server = structuredClone(state.pending.server); @@ -832,7 +913,11 @@ export function gatewayStub({ }; return jsonResponse({ shadow: "gateway.toml.next" }); } - return jsonResponse(state.config); + const rev = configRevision(state.config); + return jsonResponse(state.config, 200, { + etag: `"${rev}"`, + "x-config-revision": rev, + }); } if (url.endsWith("/admin/orphans")) { return jsonResponse({ orphans: state.orphans }); diff --git a/crates/gateway/config/src/config.rs b/crates/gateway/config/src/config.rs index 483fac1b..793ab7eb 100644 --- a/crates/gateway/config/src/config.rs +++ b/crates/gateway/config/src/config.rs @@ -2,6 +2,7 @@ //! semantic validation. use std::net::SocketAddr; +use std::sync::Arc; use serde::{Deserialize, Serialize}; @@ -25,12 +26,15 @@ pub use gateway_api_types::{Capabilities, ModelKind, ThinkingMode}; pub use secret::Secret; use secret::de_secret; use secret::ser_redacted; +use secret::{de_opt_secret, ser_opt_redacted}; use stt::RawSttPipelineConfig; pub use stt::{ RECOMMENDED_STT_MODELS, RecommendedSttModel, SILERO_VAD_MODEL, SpeechArtifactPin, SttModelConfig, SttPipelineConfig, SttRole, WhisperBackend, }; -pub use vocab::{DominionKind, LlamaBackend, Protocol, QueuePolicy, SearchProvider, ToolDialect}; +pub use vocab::{ + DominionKind, EndpointAuth, LlamaBackend, Protocol, QueuePolicy, SearchProvider, ToolDialect, +}; use crate::error::ConfigError; @@ -108,6 +112,9 @@ pub struct Config { tools: Option, /// Optional canonical speech pipeline tuning. stt: Option, + /// Exact raw UTF-8 bytes from which this configuration was parsed, so + /// revision headers/tokens always identify the same snapshot as `to_json`. + source_bytes: Option>, } /// Private DTO for [`Config`]. Holds the raw TOML shape before validation and @@ -167,6 +174,7 @@ impl TryFrom for Config { stale_state_selection: None, tools: raw.tools, stt, + source_bytes: None, }) } } @@ -337,11 +345,22 @@ pub struct EndpointConfig { id: String, /// The wire protocol this endpoint speaks. protocol: Protocol, - /// The backend base URL (a trailing slash is trimmed). - base_url: String, - /// The credential sent to this backend. - #[serde(deserialize_with = "de_secret", serialize_with = "ser_redacted")] - api_key: Secret, + /// Where the credential comes from. Absent means `api_key` below. + #[serde(default, skip_serializing_if = "EndpointAuth::is_api_key")] + auth: EndpointAuth, + /// The backend base URL (a trailing slash is trimmed). Required unless + /// `auth = "chatgpt-plan"`, which refuses it. + #[serde(default, skip_serializing_if = "Option::is_none")] + base_url: Option, + /// The credential sent to this backend. Required unless + /// `auth = "chatgpt-plan"`, which refuses it. + #[serde( + default, + deserialize_with = "de_opt_secret", + serialize_with = "ser_opt_redacted", + skip_serializing_if = "Option::is_none" + )] + api_key: Option, /// Optional remote dominion id (`[[dominion]]`) whose shared limit and /// queue govern this endpoint. Absent means unlimited pass-through. #[serde(default)] diff --git a/crates/gateway/config/src/config/accessors.rs b/crates/gateway/config/src/config/accessors.rs index a02cdffe..2cfd758c 100644 --- a/crates/gateway/config/src/config/accessors.rs +++ b/crates/gateway/config/src/config/accessors.rs @@ -7,7 +7,7 @@ use std::net::SocketAddr; use super::{ - Config, DominionConfig, DominionKind, EndpointConfig, LlamaBackend, LocalConfig, + Config, DominionConfig, DominionKind, EndpointAuth, EndpointConfig, LlamaBackend, LocalConfig, LocalModelConfig, ModelConfig, ProfileConfig, Protocol, QueuePolicy, SearchProvider, Secret, ServerConfig, SttModelConfig, SttPipelineConfig, ToolsConfig, WebSearchConfig, }; @@ -269,16 +269,24 @@ impl EndpointConfig { self.protocol } - /// Returns the backend base URL. + /// Returns where this endpoint's credential comes from. + #[must_use] + pub fn auth(&self) -> EndpointAuth { + self.auth + } + + /// Returns the backend base URL. Empty on a ChatGPT-plan endpoint, + /// which has none to configure. #[must_use] pub fn base_url(&self) -> &str { - &self.base_url + self.base_url.as_deref().unwrap_or_default() } - /// Returns the credential sent to this backend. + /// Returns the credential sent to this backend. `None` on a + /// ChatGPT-plan endpoint, whose credential the gateway holds. #[must_use] - pub fn api_key(&self) -> &Secret { - &self.api_key + pub fn api_key(&self) -> Option<&Secret> { + self.api_key.as_ref() } /// Returns the remote dominion id (`[[dominion]]`) whose shared limit diff --git a/crates/gateway/config/src/config/imp.rs b/crates/gateway/config/src/config/imp.rs index 96c6ba36..dc77b438 100644 --- a/crates/gateway/config/src/config/imp.rs +++ b/crates/gateway/config/src/config/imp.rs @@ -117,6 +117,12 @@ impl Config { } } + /// Exact raw UTF-8 TOML bytes from which this configuration was parsed. + #[must_use] + pub fn source_bytes(&self) -> Option<&[u8]> { + self.source_bytes.as_deref() + } + /// Serializes the global configuration as a JSON object in TOML shape. /// /// The payload mirrors the operator-authored global catalog and profiles. @@ -145,6 +151,20 @@ impl Config { Self::parse_toml(raw).map_err(crate::api_error::ConfigError::from) } + /// Interpolates `${VAR}` through `lookup`, parses, and validates a TOML + /// configuration string without touching the filesystem. + /// + /// # Errors + /// Returns [`ConfigError`](crate::ConfigError) for a malformed or unresolved + /// interpolation, invalid TOML, or a failed semantic check. + pub fn from_toml_with_lookup( + raw: &str, + path: Option<&Path>, + lookup: VarLookupFn<'_>, + ) -> Result { + Self::parse_toml_with_lookup(raw, path, lookup).map_err(crate::api_error::ConfigError::from) + } + /// Returns a clone with `name` selected from the already-loaded catalog, /// or with no profile selected when `name` is `None`. /// @@ -191,7 +211,9 @@ impl Config { path: path.map(Path::to_path_buf), source: Box::new(source), })?; - Self::from_value(document, lookup) + let mut config = Self::from_value(document, lookup)?; + config.source_bytes = Some(std::sync::Arc::from(raw.as_bytes())); + Ok(config) } /// Interpolates string leaves, deserializes, and validates an already diff --git a/crates/gateway/config/src/config/secret.rs b/crates/gateway/config/src/config/secret.rs index 933d8b64..4c11822e 100644 --- a/crates/gateway/config/src/config/secret.rs +++ b/crates/gateway/config/src/config/secret.rs @@ -48,6 +48,30 @@ where Ok(Secret::new(raw)) } +/// Deserializes an optional [`Secret`] field, keeping whether it was present. +pub(super) fn de_opt_secret<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + Ok(Option::::deserialize(deserializer)?.map(Secret::new)) +} + +/// Serializes a present optional [`Secret`] as `"***"`, as +/// [`ser_redacted`] does. +#[expect( + clippy::ref_option, + reason = "serde's serialize_with passes a reference to the field" +)] +pub(super) fn ser_opt_redacted(secret: &Option, serializer: S) -> Result +where + S: serde::Serializer, +{ + match secret { + Some(_) => serializer.serialize_str("***"), + None => serializer.serialize_none(), + } +} + /// Serializes a [`Secret`] field as `"***"`: a serialized configuration never /// contains credential material, and a reader treats the marker as "keep the /// existing value" on write. diff --git a/crates/gateway/config/src/config/tests/accessors.rs b/crates/gateway/config/src/config/tests/accessors.rs index e728efb9..3f2fa73a 100644 --- a/crates/gateway/config/src/config/tests/accessors.rs +++ b/crates/gateway/config/src/config/tests/accessors.rs @@ -63,7 +63,7 @@ api_key = "backend-key" assert_eq!(endpoint.id(), "e"); assert_eq!(endpoint.protocol(), Protocol::Openai); assert_eq!(endpoint.base_url(), "http://127.0.0.1:9"); - assert_eq!(endpoint.api_key().expose(), "backend-key"); + assert_eq!(endpoint.api_key().map(Secret::expose), Some("backend-key")); } #[test] diff --git a/crates/gateway/config/src/config/tests/kinds.rs b/crates/gateway/config/src/config/tests/kinds.rs index b57b5550..896031d4 100644 --- a/crates/gateway/config/src/config/tests/kinds.rs +++ b/crates/gateway/config/src/config/tests/kinds.rs @@ -200,3 +200,52 @@ endpoints = ["e"] Err(ConfigError::Validation(_)) )); } + +/// A config with one `openai-responses` endpoint and one model on it, with +/// `model_lines` added to the model. +fn responses_config(model_lines: &str) -> String { + format!( + r#" +config-version = 0 +[server] +bind = "127.0.0.1:8081" +api_key = "t" + +[[endpoint]] +id = "r" +protocol = "openai-responses" +base_url = "https://api.openai.com/v1" +api_key = "k" + +[[model]] +name = "m" +description = "prose" +context = 8192 +upstream = "u" +endpoints = ["r"] +{model_lines} +"# + ) +} + +#[test] +fn a_chat_model_on_a_responses_endpoint_parses() { + let config = Config::from_toml_str(&responses_config("")).unwrap(); + assert_eq!(config.endpoints[0].protocol, Protocol::OpenaiResponses); +} + +#[test] +fn a_responses_endpoint_refuses_non_chat_kinds_and_emulated_dialects() { + for (lines, needle) in [ + (r#"kind = "embedding""#, "serves chat only"), + ( + r#"tool_dialect = "gemma3_tool_code""#, + "tool_dialect openai", + ), + ] { + match Config::parse_toml(&responses_config(lines)) { + Err(ConfigError::Validation(message)) => assert!(message.contains(needle), "{message}"), + other => panic!("expected a validation error for {lines}, got {other:?}"), + } + } +} diff --git a/crates/gateway/config/src/config/tests/serialize.rs b/crates/gateway/config/src/config/tests/serialize.rs index 79cdab65..66326329 100644 --- a/crates/gateway/config/src/config/tests/serialize.rs +++ b/crates/gateway/config/src/config/tests/serialize.rs @@ -228,6 +228,7 @@ fn enums_round_trip_with_their_toml_spellings() { } check(Protocol::Openai, "openai"); + check(Protocol::OpenaiResponses, "openai-responses"); check(DominionKind::Remote, "remote"); check(DominionKind::Local, "local"); check(QueuePolicy::Queue, "queue"); diff --git a/crates/gateway/config/src/config/tests/validation.rs b/crates/gateway/config/src/config/tests/validation.rs index 05700eb2..015cf6d9 100644 --- a/crates/gateway/config/src/config/tests/validation.rs +++ b/crates/gateway/config/src/config/tests/validation.rs @@ -160,5 +160,6 @@ mod dominions; mod endpoints; mod kinds; mod local_models; +mod plan_endpoints; mod profiles; mod vram; diff --git a/crates/gateway/config/src/config/tests/validation/plan_endpoints.rs b/crates/gateway/config/src/config/tests/validation/plan_endpoints.rs new file mode 100644 index 00000000..fa7f36eb --- /dev/null +++ b/crates/gateway/config/src/config/tests/validation/plan_endpoints.rs @@ -0,0 +1,117 @@ +//! Tests for `auth = "chatgpt-plan"` endpoints: what they must not set, and +//! what every other endpoint still must. + +use super::super::super::*; + +/// A config with one endpoint made of `endpoint_lines` and one chat model +/// on it. +fn config_with(endpoint_lines: &str) -> String { + format!( + r#" +config-version = 0 +[server] +bind = "127.0.0.1:8081" +api_key = "t" + +[[endpoint]] +id = "e" +{endpoint_lines} + +[[model]] +name = "m" +description = "prose" +context = 8192 +upstream = "u" +endpoints = ["e"] +"# + ) +} + +const PLAN: &str = "protocol = \"openai-responses\"\nauth = \"chatgpt-plan\""; + +fn refusal(endpoint_lines: &str) -> String { + match Config::from_toml_str(&config_with(endpoint_lines)) { + Ok(_) => panic!("must be refused: {endpoint_lines}"), + Err(error) => error.to_string(), + } +} + +#[test] +fn a_plan_endpoint_parses_with_no_destination_and_no_key() { + let config = Config::from_toml_str(&config_with(PLAN)).expect("parses"); + let endpoint = &config.endpoints()[0]; + assert_eq!(endpoint.auth(), EndpointAuth::ChatgptPlan); + assert_eq!(endpoint.base_url(), ""); + assert!(endpoint.api_key().is_none()); +} + +#[test] +fn a_plan_endpoint_refuses_any_destination_or_key() { + for (line, name) in [ + // Presence is refused, whatever the value: the right URL, an empty + // string and the redaction marker included. + (r#"base_url = "https://api.openai.com/v1""#, "base_url"), + (r#"base_url = "https://elsewhere.example/v1""#, "base_url"), + (r#"base_url = """#, "base_url"), + (r#"api_key = "k""#, "api_key"), + (r#"api_key = """#, "api_key"), + (r#"api_key = "***""#, "api_key"), + ] { + let message = refusal(&format!("{PLAN}\n{line}")); + assert!( + message.contains(&format!("must not set {name}")), + "{line}: {message}" + ); + } +} + +#[test] +fn a_plan_endpoint_needs_the_responses_protocol() { + let message = refusal("protocol = \"openai\"\nauth = \"chatgpt-plan\""); + assert!( + message.contains("needs protocol = \"openai-responses\""), + "{message}" + ); +} + +#[test] +fn an_ordinary_endpoint_still_needs_both() { + let message = refusal("protocol = \"openai\"\napi_key = \"k\""); + assert!(message.contains("needs base_url"), "{message}"); + let message = refusal("protocol = \"openai\"\nbase_url = \"https://b.example/v1\""); + assert!(message.contains("needs api_key"), "{message}"); + + // An empty key stays allowed, and the default may be written out. + for lines in [ + "protocol = \"openai\"\nbase_url = \"https://b.example/v1\"\napi_key = \"\"", + "protocol = \"openai\"\nauth = \"api-key\"\nbase_url = \"https://b.example/v1\"\napi_key = \"k\"", + ] { + let config = Config::from_toml_str(&config_with(lines)).expect("parses"); + assert_eq!(config.endpoints()[0].auth(), EndpointAuth::ApiKey); + } +} + +#[test] +fn an_unknown_auth_is_refused() { + let lines = "protocol = \"openai-responses\"\nauth = \"chatgpt\""; + assert!(Config::from_toml_str(&config_with(lines)).is_err()); +} + +#[test] +fn the_json_shape_keeps_presence() { + let plan = Config::from_toml_str(&config_with(PLAN)) + .expect("parses") + .to_json(); + let endpoint = plan["endpoint"][0].as_object().expect("an endpoint"); + assert_eq!(endpoint["auth"], "chatgpt-plan"); + assert!(!endpoint.contains_key("base_url") && !endpoint.contains_key("api_key")); + + let lines = "protocol = \"openai\"\nbase_url = \"https://b.example/v1\"\napi_key = \"k\""; + let ordinary = Config::from_toml_str(&config_with(lines)) + .expect("parses") + .to_json(); + let endpoint = ordinary["endpoint"][0].as_object().expect("an endpoint"); + assert!(!endpoint.contains_key("auth")); + assert_eq!(endpoint["base_url"], "https://b.example/v1"); + assert_eq!(endpoint["api_key"], "***"); +} diff --git a/crates/gateway/config/src/config/validate.rs b/crates/gateway/config/src/config/validate.rs index ac12d387..38e4271e 100644 --- a/crates/gateway/config/src/config/validate.rs +++ b/crates/gateway/config/src/config/validate.rs @@ -11,7 +11,10 @@ use std::collections::HashSet; use url::Url; -use super::{Capabilities, Config, DominionKind, ModelKind, ThinkingMode}; +use super::{ + Capabilities, Config, DominionKind, EndpointAuth, EndpointConfig, ModelKind, Protocol, + ThinkingMode, +}; use crate::error::ConfigError; mod models; @@ -176,13 +179,7 @@ impl Config { endpoint.id ))); } - // Parse and validate the base URL at load; the upstream adapter then - // joins the request path onto a known-good origin instead of - // concatenating an arbitrary string (CFG-003, UP-005). - validate_http_url( - &format!("endpoint {} base_url", endpoint.id), - endpoint.base_url.trim(), - )?; + validate_endpoint_credential(endpoint)?; if let Some(dominion_id) = &endpoint.dominion { let dominion = self.dominions.iter().find(|d| d.id == *dominion_id); let Some(dominion) = dominion else { @@ -371,3 +368,48 @@ fn is_iso_date(value: &str) -> bool { fn is_valid_safesearch(value: &str) -> bool { matches!(value, "" | "off" | "moderate" | "strict") } + +/// Checks an endpoint's destination and credential against its `auth`. +/// +/// A ChatGPT-plan endpoint sets neither `base_url` nor `api_key`, whatever +/// their value: the plan's credential goes to OpenAI's API and nowhere a +/// configuration can name. Every other endpoint sets both. +fn validate_endpoint_credential(endpoint: &EndpointConfig) -> Result<(), ConfigError> { + let id = &endpoint.id; + match endpoint.auth { + EndpointAuth::ChatgptPlan => { + if endpoint.protocol != Protocol::OpenaiResponses { + return Err(ConfigError::Validation(format!( + "endpoint {id} has auth = \"chatgpt-plan\", which needs protocol = \"openai-responses\"" + ))); + } + for (name, present) in [ + ("base_url", endpoint.base_url.is_some()), + ("api_key", endpoint.api_key.is_some()), + ] { + if present { + return Err(ConfigError::Validation(format!( + "endpoint {id} has auth = \"chatgpt-plan\" and must not set {name}: a ChatGPT plan is sent to OpenAI only, with the signed-in user's credential" + ))); + } + } + Ok(()) + } + EndpointAuth::ApiKey => { + let Some(base_url) = &endpoint.base_url else { + return Err(ConfigError::Validation(format!( + "endpoint {id} needs base_url" + ))); + }; + if endpoint.api_key.is_none() { + return Err(ConfigError::Validation(format!( + "endpoint {id} needs api_key" + ))); + } + // Parse and validate the base URL at load; the upstream adapter then + // joins the request path onto a known-good origin instead of + // concatenating an arbitrary string (CFG-003, UP-005). + validate_http_url(&format!("endpoint {id} base_url"), base_url.trim()) + } + } +} diff --git a/crates/gateway/config/src/config/validate/models.rs b/crates/gateway/config/src/config/validate/models.rs index 1b24fd53..5cae0f51 100644 --- a/crates/gateway/config/src/config/validate/models.rs +++ b/crates/gateway/config/src/config/validate/models.rs @@ -3,7 +3,7 @@ use std::collections::HashSet; use super::super::companion::validate_artifact_source; -use super::super::{Config, DominionKind, SttModelConfig, ToolDialect}; +use super::super::{Config, DominionKind, ModelKind, Protocol, SttModelConfig, ToolDialect}; use super::{validate_capabilities, validate_http_url, validate_kind_scope}; use crate::error::ConfigError; @@ -67,6 +67,23 @@ impl Config { ))); } } + let on_responses = model.endpoints.iter().any(|id| { + self.endpoints.iter().any(|endpoint| { + endpoint.id == *id && endpoint.protocol == Protocol::OpenaiResponses + }) + }); + if on_responses && model.kind != ModelKind::Chat { + return Err(ConfigError::Validation(format!( + "model {} is a {} model, but an openai-responses endpoint serves chat only", + model.name, model.kind + ))); + } + if on_responses && model.tool_dialect != ToolDialect::Openai { + return Err(ConfigError::Validation(format!( + "model {} must use tool_dialect openai on an openai-responses endpoint", + model.name + ))); + } validate_kind_scope( "model", &model.name, diff --git a/crates/gateway/config/src/config/vocab.rs b/crates/gateway/config/src/config/vocab.rs index 5bb8f3da..4a89c465 100644 --- a/crates/gateway/config/src/config/vocab.rs +++ b/crates/gateway/config/src/config/vocab.rs @@ -4,13 +4,47 @@ use std::fmt; use serde::{Deserialize, Serialize}; -/// The wire protocol an endpoint speaks. The OpenAI shape is the only one. +/// The wire protocol an endpoint speaks upstream. Callers always speak chat +/// completions to the gateway; this names what the gateway speaks to the +/// backend. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] #[non_exhaustive] pub enum Protocol { - /// The OpenAI `/chat/completions` shape. + /// The OpenAI `/chat/completions` shape, forwarded unchanged. Openai, + /// The OpenAI `/responses` shape. The gateway translates each chat + /// completion to a Responses request and the Responses event stream + /// back to chat completion chunks, so callers see no difference. + #[serde(rename = "openai-responses")] + OpenaiResponses, +} + +/// Where an endpoint's credential comes from. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] +#[non_exhaustive] +pub enum EndpointAuth { + /// The endpoint's own `api_key`. The default when `auth` is absent. + #[default] + #[serde(rename = "api-key")] + ApiKey, + /// A user's ChatGPT sign-in, held by the gateway. The endpoint sets + /// neither `base_url` nor `api_key`: the gateway sends the plan's + /// credential to OpenAI's API and nowhere else, and only for Runvil's + /// own client. + #[serde(rename = "chatgpt-plan")] + ChatgptPlan, +} + +impl EndpointAuth { + /// Whether this is the default, which a serialized endpoint omits. + #[expect( + clippy::trivially_copy_pass_by_ref, + reason = "serde's skip_serializing_if passes a reference" + )] + pub(crate) fn is_api_key(&self) -> bool { + *self == EndpointAuth::ApiKey + } } /// Whether a dominion pools remote providers or local GPUs managed by the diff --git a/crates/gateway/config/src/lib.rs b/crates/gateway/config/src/lib.rs index 99f686a0..6b32b396 100644 --- a/crates/gateway/config/src/lib.rs +++ b/crates/gateway/config/src/lib.rs @@ -39,17 +39,18 @@ mod shadow; pub use crate::api_error::{ConfigError, ConfigErrorKind}; pub use crate::config::{ Capabilities, Config, DominionConfig, DominionKind, DraftTokenMax, DraftTokenMaxError, - EndpointConfig, LlamaBackend, LocalConfig, LocalModelConfig, ModelConfig, ModelKind, - MultimodalProjectorConfig, ProfileConfig, Protocol, QueuePolicy, RECOMMENDED_STT_MODELS, - RecommendedSttModel, SILERO_VAD_MODEL, SearchProvider, Secret, ServerConfig, SpeculationType, - SpeculativeConfig, SpeechArtifactPin, SttModelConfig, SttPipelineConfig, SttRole, ThinkingMode, - ToolDialect, ToolsConfig, VarLookupFn, WebSearchConfig, WhisperBackend, + EndpointAuth, EndpointConfig, LlamaBackend, LocalConfig, LocalModelConfig, ModelConfig, + ModelKind, MultimodalProjectorConfig, ProfileConfig, Protocol, QueuePolicy, + RECOMMENDED_STT_MODELS, RecommendedSttModel, SILERO_VAD_MODEL, SearchProvider, Secret, + ServerConfig, SpeculationType, SpeculativeConfig, SpeechArtifactPin, SttModelConfig, + SttPipelineConfig, SttRole, ThinkingMode, ToolDialect, ToolsConfig, VarLookupFn, + WebSearchConfig, WhisperBackend, }; pub use crate::profile::{ ProfileName, ProfileNameError, ProfileSelection, ProfileState, profile_state_path, }; pub use crate::shadow::{ - PendingReport, PendingShadows, clear_profile_state, load_pending_config, pending_report, - pending_var_references, persist_profile_state, save_config_shadow, shadow_path, write_atomic, - write_shadow, + PendingReport, PendingShadows, clear_profile_state, load_pending_config, + parse_pending_config_text, pending_report, pending_var_references, persist_profile_state, + read_pending_or_real_text, save_config_shadow, shadow_path, write_atomic, write_shadow, }; diff --git a/crates/gateway/config/src/shadow-plan-tests.rs b/crates/gateway/config/src/shadow-plan-tests.rs new file mode 100644 index 00000000..cf6dab2a --- /dev/null +++ b/crates/gateway/config/src/shadow-plan-tests.rs @@ -0,0 +1,317 @@ +//! Shadow save, pending load, dirty report, and `${VAR}` reference tests for +//! `auth = "chatgpt-plan"` endpoints alongside ordinary endpoints. + +use super::*; +use crate::EndpointAuth; + +const PLAN_AND_KEY_CONFIG: &str = r#" +config-version = 0 + +[server] +bind = "127.0.0.1:8081" +api_key = "server-secret" + +[[endpoint]] +id = "plan" +protocol = "openai-responses" +auth = "chatgpt-plan" + +[[endpoint]] +id = "own" +protocol = "openai" +base_url = "https://api.example.test/v1" +api_key = "${RV_SHADOW_PLAN_TEST_KEY}" + +[[model]] +name = "plan-chat" +description = "on the plan" +context = 8192 +upstream = "gpt-plan" +endpoints = ["plan"] + +[[model]] +name = "own-chat" +description = "on an api key" +context = 8192 +upstream = "gpt-own" +endpoints = ["own"] + +[[local_model]] +name = "local" +description = "local model" +source = "/models/local.gguf" +context = 4096 + +[[profile]] +name = "work" +models = ["local"] + +[[profile]] +name = "light" +models = [] +"#; + +fn test_env_lookup(name: &str) -> Result { + if name == "RV_SHADOW_PLAN_TEST_KEY" { + return Ok("sk-from-env".to_owned()); + } + Err(std::env::VarError::NotPresent) +} + +fn write_plan_config() -> (tempfile::TempDir, PathBuf, crate::Config) { + let temp = tempfile::TempDir::new().expect("temp dir"); + let path = temp.path().join("gateway.toml"); + fs::write(&path, PLAN_AND_KEY_CONFIG).expect("write config"); + let config = load_pending_config(&path, &ProfileSelection::default(), &test_env_lookup) + .expect("initial config loads"); + (temp, path, config) +} + +/// Converts a JSON value to TOML the same way `PUT /admin/config` does: +/// `null` object members drop out and arrays/tables recurse. +fn json_to_toml(value: serde_json::Value) -> Option { + Some(match value { + serde_json::Value::Null => return None, + serde_json::Value::Bool(flag) => Value::Boolean(flag), + serde_json::Value::Number(number) => { + if let Some(integer) = number.as_i64() { + Value::Integer(integer) + } else { + Value::Float(number.as_f64().expect("finite float")) + } + } + serde_json::Value::String(text) => Value::String(text), + serde_json::Value::Array(items) => { + Value::Array(items.into_iter().filter_map(json_to_toml).collect()) + } + serde_json::Value::Object(members) => { + let mut table = toml::map::Map::new(); + for (key, member) in members { + if let Some(converted) = json_to_toml(member) { + table.insert(key, converted); + } + } + Value::Table(table) + } + }) +} + +fn assert_plan_table_has_no_url_or_key(shadow_toml: &str, id: &str) { + let parsed: Value = toml::from_str(shadow_toml).expect("shadow parses as TOML"); + let endpoints = parsed["endpoint"].as_array().expect("endpoint array"); + let plan = endpoints + .iter() + .find(|entry| entry.get("id").and_then(Value::as_str) == Some(id)) + .expect("plan endpoint exists in shadow"); + let table = plan.as_table().expect("endpoint is a table"); + assert_eq!( + table.get("auth").and_then(Value::as_str), + Some("chatgpt-plan") + ); + assert_eq!( + table.get("protocol").and_then(Value::as_str), + Some("openai-responses") + ); + assert!( + !table.contains_key("base_url"), + "plan endpoint must never gain base_url: {table:?}" + ); + assert!( + !table.contains_key("api_key"), + "plan endpoint must never gain api_key: {table:?}" + ); +} + +#[test] +fn unchanged_to_json_round_trips_through_save_config_shadow_and_load_pending() { + let (_temp, path, config) = write_plan_config(); + let wire = config.to_json(); + let candidate = json_to_toml(wire).expect("config json converts to toml"); + + let shadows = save_config_shadow(&path, candidate, &test_env_lookup) + .expect("unchanged JSON saves cleanly"); + + let shadow_raw = fs::read_to_string(&shadows.config).expect("read shadow"); + assert_plan_table_has_no_url_or_key(&shadow_raw, "plan"); + assert!( + shadow_raw.contains("${RV_SHADOW_PLAN_TEST_KEY}"), + "the sibling endpoint keeps its ${{VAR}} reference through *** restoration" + ); + + let pending = load_pending_config(&path, &ProfileSelection::default(), &test_env_lookup) + .expect("pending config loads"); + let plan = pending + .endpoints() + .iter() + .find(|endpoint| endpoint.id() == "plan") + .expect("plan endpoint loaded"); + assert_eq!(plan.auth(), EndpointAuth::ChatgptPlan); + assert!(plan.api_key().is_none()); + assert_eq!(plan.base_url(), ""); + + let pending_json = pending.to_json(); + let plan_json = pending_json["endpoint"][0] + .as_object() + .expect("plan endpoint json"); + assert_eq!( + plan_json.get("auth"), + Some(&serde_json::json!("chatgpt-plan")) + ); + assert!(!plan_json.contains_key("base_url")); + assert!(!plan_json.contains_key("api_key")); +} + +#[test] +fn editing_an_unrelated_field_preserves_plan_endpoint_and_var_references() { + let (_temp, path, config) = write_plan_config(); + let mut wire = config.to_json(); + wire["model"][0]["description"] = serde_json::json!("updated plan description"); + let candidate = json_to_toml(wire).expect("config json converts to toml"); + + let shadows = save_config_shadow(&path, candidate, &test_env_lookup).expect("save succeeds"); + let shadow_raw = fs::read_to_string(&shadows.config).expect("read shadow"); + assert_plan_table_has_no_url_or_key(&shadow_raw, "plan"); + + let references = pending_var_references(&path).expect("var references"); + assert_eq!( + references.get("RV_SHADOW_PLAN_TEST_KEY"), + Some(&vec!["endpoint own api_key".to_owned()]), + "only the ordinary endpoint references an env var" + ); + let report = pending_report(&path).expect("pending report"); + assert_eq!(report.shadowed_files, std::slice::from_ref(&path)); + assert!( + !report + .changed_sections + .iter() + .any(|section| section == "endpoint"), + "the endpoint section did not change relative to canonical TOML: {:?}", + report.changed_sections + ); +} + +#[test] +fn switching_an_ordinary_endpoint_to_plan_drops_url_and_secret_and_switching_back_requires_new_credentials() + { + let (_temp, path, config) = write_plan_config(); + let mut to_plan = config.to_json(); + to_plan["endpoint"][1] = serde_json::json!({ + "id": "own", + "protocol": "openai-responses", + "auth": "chatgpt-plan", + "dominion": null, + }); + + let shadows = save_config_shadow( + &path, + json_to_toml(to_plan).expect("converts"), + &test_env_lookup, + ) + .expect("switching ordinary endpoint to plan succeeds"); + let shadow_raw = fs::read_to_string(&shadows.config).expect("read shadow"); + assert_plan_table_has_no_url_or_key(&shadow_raw, "own"); + assert!( + !shadow_raw.contains("RV_SHADOW_PLAN_TEST_KEY") && !shadow_raw.contains("api.example.test"), + "switching to chatgpt-plan strips the previous URL and key from the shadow" + ); + + let pending_plan = load_pending_config(&path, &ProfileSelection::default(), &test_env_lookup) + .expect("pending loads"); + assert!( + pending_plan + .endpoints() + .iter() + .all(|endpoint| endpoint.auth() == EndpointAuth::ChatgptPlan) + ); + + // Switching back from chatgpt-plan to ordinary while sending `"***"` must + // fail closed because the plan shadow holds no secret to preserve. + let mut redacted_back = pending_plan.to_json(); + redacted_back["endpoint"][1] = serde_json::json!({ + "id": "own", + "protocol": "openai", + "base_url": "https://api.example.test/v1", + "api_key": "***", + "dominion": null, + }); + let redacted_err = save_config_shadow( + &path, + json_to_toml(redacted_back).expect("converts"), + &test_env_lookup, + ) + .expect_err("a plan endpoint has no secret to restore from ***"); + assert_eq!(redacted_err.kind(), crate::ConfigErrorKind::Validation); + assert!( + redacted_err.to_string().contains("endpoint own.api_key"), + "unexpected error: {redacted_err}" + ); + + // Supplying real credentials switches the endpoint back to ApiKey cleanly. + let mut valid_back = pending_plan.to_json(); + valid_back["endpoint"][1] = serde_json::json!({ + "id": "own", + "protocol": "openai", + "base_url": "https://api.example.test/v1", + "api_key": "sk-fresh", + "dominion": null, + }); + save_config_shadow( + &path, + json_to_toml(valid_back).expect("converts"), + &test_env_lookup, + ) + .expect("switching back with explicit credentials succeeds"); + let restored = load_pending_config(&path, &ProfileSelection::default(), &test_env_lookup) + .expect("restored loads"); + let own = restored + .endpoints() + .iter() + .find(|endpoint| endpoint.id() == "own") + .expect("own endpoint"); + assert_eq!(own.auth(), EndpointAuth::ApiKey); + assert_eq!(own.base_url(), "https://api.example.test/v1"); + assert_eq!(own.api_key().expect("api_key").expose(), "sk-fresh"); +} + +#[test] +fn plan_endpoint_refuses_redacted_or_empty_key_and_url_without_writing_a_shadow() { + let (_temp, path, config) = write_plan_config(); + for bad_endpoint in [ + serde_json::json!({ + "id": "plan", + "protocol": "openai-responses", + "auth": "chatgpt-plan", + "base_url": "", + }), + serde_json::json!({ + "id": "plan", + "protocol": "openai-responses", + "auth": "chatgpt-plan", + "api_key": "", + }), + serde_json::json!({ + "id": "plan", + "protocol": "openai-responses", + "auth": "chatgpt-plan", + "api_key": "***", + }), + ] { + let mut wire = config.to_json(); + wire["endpoint"][0] = bad_endpoint.clone(); + let err = save_config_shadow( + &path, + json_to_toml(wire).expect("converts"), + &test_env_lookup, + ) + .expect_err("plan endpoint with URL or key must be refused"); + assert_eq!( + err.kind(), + crate::ConfigErrorKind::Validation, + "{bad_endpoint}: {err}" + ); + assert!( + !shadow_path(&path).exists(), + "no shadow written for {bad_endpoint}" + ); + } +} diff --git a/crates/gateway/config/src/shadow-tests.rs b/crates/gateway/config/src/shadow-tests.rs index f53a3744..4382c464 100644 --- a/crates/gateway/config/src/shadow-tests.rs +++ b/crates/gateway/config/src/shadow-tests.rs @@ -2,6 +2,9 @@ use super::*; +#[path = "shadow-plan-tests.rs"] +mod plan_tests; + const CONFIG: &str = r#" config-version = 0 diff --git a/crates/gateway/config/src/shadow.rs b/crates/gateway/config/src/shadow.rs index 494c5482..bc61dc4d 100644 --- a/crates/gateway/config/src/shadow.rs +++ b/crates/gateway/config/src/shadow.rs @@ -266,22 +266,55 @@ pub fn load_pending_config( inputs: &ProfileSelection, lookup: VarLookupFn<'_>, ) -> Result { - let (source_path, raw) = read_pending_or_real(config_path) - .map_err(crate::ConfigError::from)? - .ok_or_else(|| { - crate::ConfigError::from(Repr::Read { - path: config_path.to_owned(), - source: std::io::Error::new(std::io::ErrorKind::NotFound, "config not found"), - }) - })?; - let config = Config::parse_toml_with_lookup( - &toml::to_string(&raw).map_err(|error| { - crate::ConfigError::validation(format!("pending config does not render: {error}")) - })?, - Some(&source_path), - lookup, - ) - .map_err(crate::ConfigError::from)?; + let (source_path, raw) = read_pending_or_real_text(config_path)?; + parse_pending_config_text(config_path, &source_path, &raw, inputs, lookup) +} + +/// Reads the shadow-preferred config file (`gateway.toml.next` when staged, +/// otherwise `gateway.toml`) in a single file read, returning the chosen path +/// and its exact UTF-8 text. +/// +/// # Errors +/// Returns [`ConfigError`](crate::ConfigError) when neither file exists or the +/// chosen file cannot be read as UTF-8. +pub fn read_pending_or_real_text( + config_path: &Path, +) -> Result<(PathBuf, String), crate::ConfigError> { + let shadow = shadow_path(config_path); + let chosen = if shadow.is_file() { + shadow + } else if config_path.is_file() { + config_path.to_owned() + } else { + return Err(crate::ConfigError::from(Repr::Read { + path: config_path.to_owned(), + source: std::io::Error::new(std::io::ErrorKind::NotFound, "config not found"), + })); + }; + let raw = fs::read_to_string(&chosen).map_err(|source| { + crate::ConfigError::from(Repr::Read { + path: chosen.clone(), + source, + }) + })?; + Ok((chosen, raw)) +} + +/// Parses and selects a pending configuration from already-captured UTF-8 text +/// so callers can parse and hash the exact same byte snapshot. +/// +/// # Errors +/// Returns [`ConfigError`](crate::ConfigError) under the same conditions as +/// [`load_pending_config`]. +pub fn parse_pending_config_text( + config_path: &Path, + source_path: &Path, + raw: &str, + inputs: &ProfileSelection, + lookup: VarLookupFn<'_>, +) -> Result { + let config = Config::parse_toml_with_lookup(raw, Some(source_path), lookup) + .map_err(crate::ConfigError::from)?; // The state file sits beside the real config, not the shadow. config .select_at_load(config_path, inputs) diff --git a/crates/gateway/local/src/runtime/start.rs b/crates/gateway/local/src/runtime/start.rs index 6f8b3fed..f2e05fb7 100644 --- a/crates/gateway/local/src/runtime/start.rs +++ b/crates/gateway/local/src/runtime/start.rs @@ -247,6 +247,7 @@ pub(super) fn start_impl( upstream_name, endpoint: Arc::new(Endpoint { id: endpoint_id, + source: gateway_routing::CredentialSource::Configured, upstream: Arc::new(upstream.clone()), queue: admission.queue, }), diff --git a/crates/gateway/protocol/src/error.rs b/crates/gateway/protocol/src/error.rs index 89c09778..34563094 100644 --- a/crates/gateway/protocol/src/error.rs +++ b/crates/gateway/protocol/src/error.rs @@ -39,7 +39,13 @@ pub enum ProtocolError { UpstreamProtocol(#[source] Box), /// The upstream backend returned a non-success status. - #[error("upstream returned {status}")] + /// + /// The display names the upstream's OpenAI error `code` when the body + /// carries one that is a plain identifier (for example + /// `upstream returned 400 (context_length_exceeded)`), so a client can + /// tell a context overflow or a spent quota from other refusals. The + /// upstream's message text is never shown: it may echo prompt content. + #[error("upstream returned {status}{}", upstream_code_suffix(body))] UpstreamStatus { /// The status code the backend returned. status: u16, @@ -52,6 +58,47 @@ pub enum ProtocolError { /// but the backing upstream has no implementation for it. #[error("model {0} is not available for this workload")] ModelUnavailable(String), + + /// The request is valid chat completions, but the resolved upstream's + /// protocol has no equivalent for part of it (for example a legacy + /// `function` message sent to a Responses endpoint). Nothing was sent. + #[error("this request cannot be sent to the model's endpoint: {0}")] + UnsupportedRequest(&'static str), + + /// The endpoint's credential source has no usable credential (for + /// example a ChatGPT sign-in that is signed out or needs repeating). + /// Nothing was sent. `code` is a stable identifier a client can show + /// or act on. + #[error("the model's endpoint has no usable credential ({code})")] + CredentialUnavailable { + /// Why, as a stable identifier. + code: &'static str, + }, +} + +/// ` (code)` when `body` is an OpenAI error envelope whose `error.code` is a +/// plain identifier of at most 64 characters, and nothing otherwise. +fn upstream_code_suffix(body: &str) -> String { + let code = serde_json::from_str::(body) + .ok() + .and_then(|value| { + value + .pointer("/error/code") + .and_then(serde_json::Value::as_str) + .map(str::to_owned) + }); + match code { + Some(code) + if !code.is_empty() + && code.len() <= 64 + && code + .chars() + .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.')) => + { + format!(" ({code})") + } + _ => String::new(), + } } impl ProtocolError { @@ -141,6 +188,18 @@ impl ProtocolError { "invalid_request_error", "model_unavailable", ), + ProtocolError::UnsupportedRequest(_) => ( + reqwest::StatusCode::BAD_REQUEST, + "invalid_request_error", + "unsupported_for_endpoint", + ), + // 403, not 401: a 401 would tell the caller its own gateway key + // is wrong, which it is not. + ProtocolError::CredentialUnavailable { code } => ( + reqwest::StatusCode::FORBIDDEN, + "invalid_request_error", + code, + ), } } @@ -255,6 +314,32 @@ mod tests { assert!(error.source().is_some()); } + #[test] + fn the_message_names_a_plain_upstream_code_and_nothing_else() { + let named = ProtocolError::upstream_status( + 400, + r#"{"error":{"code":"context_length_exceeded","message":"secret prompt text"}}"# + .to_owned(), + ); + assert_eq!( + named.to_string(), + "upstream returned 400 (context_length_exceeded)" + ); + assert!(!named.envelope().to_string().contains("secret prompt text")); + + for body in [ + r#"{"error":{"code":"has spaces and prose"}}"#, + r#"{"error":{"code":"line +break"}}"#, + r#"{"error":{"code":42}}"#, + r#"{"error":"just a string"}"#, + "not json", + ] { + let plain = ProtocolError::upstream_status(429, body.to_owned()); + assert_eq!(plain.to_string(), "upstream returned 429", "body: {body}"); + } + } + #[test] fn envelope_has_message_type_and_code() { let error = ProtocolError::upstream_status(503, "busy".to_owned()); diff --git a/crates/gateway/protocol/src/http_util.rs b/crates/gateway/protocol/src/http_util.rs index a6f29e39..f7c18943 100644 --- a/crates/gateway/protocol/src/http_util.rs +++ b/crates/gateway/protocol/src/http_util.rs @@ -43,6 +43,39 @@ pub fn streaming_client() -> reqwest::Client { .unwrap_or_else(|_| reqwest::Client::new()) } +/// Builds a reqwest client with [`reqwest::redirect::Policy::none`] and explicit +/// timeout bounds, retrying the identical configuration once. +/// +/// Never falls back to `reqwest::Client::default()` (which follows redirects +/// and clears timeouts) and never drops the requested timeouts on retry. +#[must_use] +pub fn hardened_client( + connect_timeout: Duration, + request_timeout: Option, +) -> Option { + let build = || { + let mut builder = reqwest::Client::builder() + .redirect(reqwest::redirect::Policy::none()) + .connect_timeout(connect_timeout); + if let Some(timeout) = request_timeout { + builder = builder.timeout(timeout); + } + builder.build() + }; + build().or_else(|_| build()).ok() +} + +/// Builds the streaming client for a backend reached with a credential that +/// must reach that backend only: like [`streaming_client`], but it never +/// follows a redirect, so neither the bearer token nor the request body can +/// be forwarded to another server. A redirect comes back to the caller as an +/// ordinary non-success status, and construction failure returns `None` so +/// callers fail closed instead of falling back to a default client. +#[must_use] +pub fn no_redirect_streaming_client() -> Option { + hardened_client(CONNECT_TIMEOUT, None) +} + /// TCP keepalive interval for audio streams, so a silently dead peer or an /// idle middlebox drop surfaces instead of hanging the stream forever. const AUDIO_TCP_KEEPALIVE: Duration = Duration::from_secs(60); diff --git a/crates/gateway/protocol/src/upstream.rs b/crates/gateway/protocol/src/upstream.rs index 9f349e51..ac889da3 100644 --- a/crates/gateway/protocol/src/upstream.rs +++ b/crates/gateway/protocol/src/upstream.rs @@ -1,7 +1,9 @@ -//! The backend-facing side: the [`Upstream`] trait and its OpenAI passthrough. +//! The backend-facing side: the [`Upstream`] trait and its HTTP backends. //! -//! This crate implements the trait once, as [`OpenAiUpstream`], which forwards -//! the OpenAI shape unchanged. +//! This crate implements the trait twice: [`OpenAiUpstream`] forwards the +//! chat completions shape unchanged, and [`ResponsesUpstream`] translates it +//! to and from the OpenAI Responses API. A [`BearerSource`] supplies the +//! Responses backend's credential for each request. use std::fmt; @@ -17,7 +19,12 @@ use crate::wire::{ RerankResponse, SpeechRequest, }; +mod bearer; mod openai; +mod responses; + +pub use self::bearer::{BearerSource, StaticBearer}; +pub use self::responses::ResponsesUpstream; /// An opened streaming chat completion: the upstream response headers worth /// forwarding to the client, plus the validated chunk stream. diff --git a/crates/gateway/protocol/src/upstream/bearer.rs b/crates/gateway/protocol/src/upstream/bearer.rs new file mode 100644 index 00000000..513d0f53 --- /dev/null +++ b/crates/gateway/protocol/src/upstream/bearer.rs @@ -0,0 +1,47 @@ +//! Where an upstream gets the credential it presents. + +use std::fmt; + +use async_trait::async_trait; +use gateway_config::Secret; + +use crate::error::ProtocolError; + +/// The bearer token an upstream presents, fetched for each request. +/// +/// A configured API key never changes, so [`StaticBearer`] hands it back as +/// is. A credential with a lifetime, such as an OAuth access token, +/// implements this to refresh before it hands one out. +#[async_trait] +pub trait BearerSource: Send + Sync + fmt::Debug { + /// The bearer for the next request. An empty secret means the request + /// carries no `Authorization` header. + /// + /// # Errors + /// Returns a [`ProtocolError`] when no usable credential exists, for + /// example a signed-out account. Nothing is sent upstream in that case. + async fn bearer(&self) -> Result; + + /// Reports that the backend answered `rejected` with a 401, so a source + /// with a lifetime can stop handing it out. The default does nothing. + async fn rejected(&self, _rejected: &Secret) {} +} + +/// A fixed credential: an endpoint's configured API key. +#[derive(Debug, Clone)] +pub struct StaticBearer(Secret); + +impl StaticBearer { + /// Wraps a configured key. An empty key sends no `Authorization` header. + #[must_use] + pub fn new(key: Secret) -> StaticBearer { + StaticBearer(key) + } +} + +#[async_trait] +impl BearerSource for StaticBearer { + async fn bearer(&self) -> Result { + Ok(self.0.clone()) + } +} diff --git a/crates/gateway/protocol/src/upstream/openai.rs b/crates/gateway/protocol/src/upstream/openai.rs index 98d104db..fc53b260 100644 --- a/crates/gateway/protocol/src/upstream/openai.rs +++ b/crates/gateway/protocol/src/upstream/openai.rs @@ -71,42 +71,7 @@ impl OpenAiUpstream { .send() .await .map_err(ProtocolError::upstream_transport)?; - - let status = response.status(); - if !status.is_success() { - let body = - crate::http_util::read_body_capped(response, crate::http_util::MAX_ERROR_BODY) - .await; - // F5: the raw body may echo prompt content or credentials, so only - // its OpenAI error envelope reaches the log, bounded and escaped. - let diagnostics = UpstreamErrorDiagnostics::from_body(&body); - let code = diagnostics.code.as_str(); - let kind = diagnostics.kind.as_str(); - let message = diagnostics.message.as_str(); - if status.is_server_error() { - tracing::warn!( - status = status.as_u16(), - code = %code, - r#type = %kind, - error.message = %message, - "upstream returned a server error" - ); - } else { - tracing::info!( - status = status.as_u16(), - code = %code, - r#type = %kind, - error.message = %message, - "upstream returned a client error" - ); - } - let body: String = body.chars().take(2000).collect(); - return Err(ProtocolError::UpstreamStatus { - status: status.as_u16(), - body, - }); - } - Ok(response) + check_status(response).await } /// POST `body` to `{base_url}/{path}` and return the success body bytes. @@ -133,6 +98,51 @@ impl OpenAiUpstream { } } +/// Passes a success response through; turns any other status into +/// [`ProtocolError::UpstreamStatus`], logging only the bounded, escaped +/// OpenAI error envelope. Shared by every HTTP upstream in this crate. +/// +/// # Errors +/// Returns [`ProtocolError::UpstreamStatus`] with a truncated body on a +/// non-success status. +pub(super) async fn check_status( + response: reqwest::Response, +) -> Result { + let status = response.status(); + if status.is_success() { + return Ok(response); + } + let body = crate::http_util::read_body_capped(response, crate::http_util::MAX_ERROR_BODY).await; + // F5: the raw body may echo prompt content or credentials, so only + // its OpenAI error envelope reaches the log, bounded and escaped. + let diagnostics = UpstreamErrorDiagnostics::from_body(&body); + let code = diagnostics.code.as_str(); + let kind = diagnostics.kind.as_str(); + let message = diagnostics.message.as_str(); + if status.is_server_error() { + tracing::warn!( + status = status.as_u16(), + code = %code, + r#type = %kind, + error.message = %message, + "upstream returned a server error" + ); + } else { + tracing::info!( + status = status.as_u16(), + code = %code, + r#type = %kind, + error.message = %message, + "upstream returned a client error" + ); + } + let body: String = body.chars().take(2000).collect(); + Err(ProtocolError::UpstreamStatus { + status: status.as_u16(), + body, + }) +} + /// Maximum characters retained from an upstream error field for diagnostics. /// Applied per field on the decoded string, before control escaping can expand /// it, so one event cannot flood the log with an oversized upstream message. @@ -184,7 +194,7 @@ fn bounded_error_field(value: Option<&serde_json::Value>) -> String { /// Escapes every control character so a crafted upstream message cannot forge /// log lines or inject terminal control sequences, keeping at most `max_chars` /// input characters. -fn escape_control(text: &str, max_chars: usize) -> String { +pub(super) fn escape_control(text: &str, max_chars: usize) -> String { let mut escaped = String::new(); for character in text.chars().take(max_chars) { match character { diff --git a/crates/gateway/protocol/src/upstream/responses.rs b/crates/gateway/protocol/src/upstream/responses.rs new file mode 100644 index 00000000..e33981f4 --- /dev/null +++ b/crates/gateway/protocol/src/upstream/responses.rs @@ -0,0 +1,146 @@ +//! The `openai-responses` endpoint protocol: chat completions from the +//! caller, OpenAI Responses to the backend. +//! +//! [`ResponsesUpstream`] rewrites each chat completion as a Responses request +//! ([`crate::wire::to_responses_request`]) and always asks the backend to +//! stream. A streaming caller gets the Responses events translated to chat +//! chunks as they arrive; a non-streaming caller gets the same chunks folded +//! into one chat completion. Either way the caller sees the chat completions +//! shape only. + +use std::sync::Arc; + +use async_trait::async_trait; + +use super::{BearerSource, StreamedChunks, Upstream}; +use crate::error::ProtocolError; +use crate::wire::{ChatRequest, ChatResponse, RequestPolicy, to_responses_request}; + +mod events; +mod sse; + +/// A backend that speaks the OpenAI Responses API, reached over HTTP. +#[derive(Debug)] +pub struct ResponsesUpstream { + base_url: String, + bearer: Arc, + policy: RequestPolicy, + /// Connect-timeout-only client that never follows a redirect: every + /// request streams, so a whole-request timeout would cut a long reply + /// off, and a followed redirect could carry the bearer or the prompt to + /// another server. `None` when hardened construction fails so calls fail + /// closed before requesting a credential or contacting an upstream. + http: Option, +} + +impl ResponsesUpstream { + /// Builds an upstream for `base_url` (a trailing slash is trimmed) that + /// presents `bearer` and sends only the fields `policy` allows. + #[must_use] + pub fn new( + base_url: &str, + bearer: Arc, + policy: RequestPolicy, + ) -> ResponsesUpstream { + ResponsesUpstream { + base_url: base_url.trim_end_matches('/').to_string(), + bearer, + policy, + http: crate::http_util::no_redirect_streaming_client(), + } + } + + /// Test-only constructor modeling a failed hardened HTTP client build. + #[cfg(test)] + #[must_use] + pub(crate) fn without_http_client( + base_url: &str, + bearer: Arc, + policy: RequestPolicy, + ) -> ResponsesUpstream { + ResponsesUpstream { + base_url: base_url.trim_end_matches('/').to_string(), + bearer, + policy, + http: None, + } + } + + /// Translates `req`, sends it to `{base_url}/responses`, and returns the + /// opened event stream. + /// + /// # Errors + /// Returns [`ProtocolError::UnsupportedRequest`] before anything is sent + /// when the request has no Responses equivalent, [`ProtocolError::UpstreamConnect`] + /// before touching the credential or network when no hardened HTTP client + /// is available, the bearer source's error when it has no credential, and + /// the transport and status errors of [`super::Upstream::stream`]. + async fn open( + &self, + req: &ChatRequest, + upstream_model: &str, + ) -> Result { + let body = to_responses_request(req, upstream_model, self.policy) + .map_err(ProtocolError::UnsupportedRequest)?; + let http = self.http.as_ref().ok_or_else(|| { + ProtocolError::connect(std::io::Error::other( + "Responses upstream requires a no-redirect HTTP client; client construction failed", + )) + })?; + let bearer = self.bearer.bearer().await?; + let mut builder = http + .post(format!("{}/responses", self.base_url)) + .json(&body); + if !bearer.is_empty() { + builder = builder.bearer_auth(bearer.expose()); + } + let response = builder + .send() + .await + .map_err(ProtocolError::upstream_transport)?; + if response.status() == reqwest::StatusCode::UNAUTHORIZED { + self.bearer.rejected(&bearer).await; + } + super::openai::check_status(response).await + } +} + +#[async_trait] +impl Upstream for ResponsesUpstream { + async fn send( + &self, + req: ChatRequest, + upstream_model: &str, + ) -> Result { + let requested = req.model.clone(); + let response = self.open(&req, upstream_model).await?; + // A non-streaming completion always reports usage. + sse::collect( + sse::chat_chunks(response, requested.clone(), true), + requested, + ) + .await + } + + async fn stream( + &self, + req: ChatRequest, + upstream_model: &str, + ) -> Result { + let requested = req.model.clone(); + let include_usage = req + .rest + .get("stream_options") + .and_then(|options| options.get("include_usage")) + .and_then(serde_json::Value::as_bool) + .unwrap_or(false); + let response = self.open(&req, upstream_model).await?; + Ok(StreamedChunks { + // The plan route sends no Content-Type; the relay's + // `text/event-stream` default is the right answer for both. + content_type: None, + cache_control: None, + chunks: sse::chat_chunks(response, requested, include_usage), + }) + } +} diff --git a/crates/gateway/protocol/src/upstream/responses/events.rs b/crates/gateway/protocol/src/upstream/responses/events.rs new file mode 100644 index 00000000..90ac02cc --- /dev/null +++ b/crates/gateway/protocol/src/upstream/responses/events.rs @@ -0,0 +1,472 @@ +//! Responses stream events to chat completion chunks. +//! +//! [`Translator`] reads one decoded Responses event at a time and says what +//! it means for the chat stream: chunks to relay, the final chunks, or a +//! failure. Text and tool-call arguments are relayed as their deltas arrive. +//! Argument deltas that arrive before their call's `output_item.added` are +//! held and sent with the call's start. Each `output_item.done` is checked +//! against what was streamed: missing argument text is sent then, so a +//! backend that skips argument deltas still delivers whole tool calls, and +//! arguments that contradict what was already sent fail the response rather +//! than relay a corrupted call. A call that ends with no arguments at all +//! gets `{}`, so its arguments always parse as JSON. +//! +//! `response.completed` and `response.incomplete` end the stream as a +//! success, the second with `finish_reason` `length` or `content_filter`. +//! Usage follows the chat contract: only when the caller asked for it +//! (`stream_options.include_usage`), as `null` on every other chunk and the +//! real figures in a separate last chunk with empty `choices`, and never +//! invented when the backend reports none. Reasoning +//! and other item kinds have no chat equivalent and are not relayed. + +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde_json::{Map, Value, json}; + +use crate::error::ProtocolError; +use crate::upstream::openai::{MAX_ERROR_MESSAGE_CHARS, escape_control}; +use crate::wire::{ChatChunk, ChatChunkChoice}; + +/// What one Responses event means for the chat stream. +#[derive(Debug)] +pub(super) enum Step { + /// Chunks to relay (possibly none); the stream continues. + Continue(Vec), + /// The last chunks; the response ended normally. + Finish(Vec), + /// The response failed; nothing more follows. + Fail(ProtocolError), +} + +/// One function call seen in the stream, in first-seen order: its position +/// is its chat `tool_calls` index. +#[derive(Debug)] +struct ToolCall { + item_id: Option, + output_index: Option, + arguments_sent: String, +} + +/// Argument text that arrived before its call's metadata. +#[derive(Debug)] +struct EarlyArguments { + item_id: Option, + output_index: Option, + text: String, +} + +/// Whether `item_id` or `output_index` names the item recorded with +/// `recorded_id` and `recorded_index`. +fn same_item( + recorded_id: Option<&str>, + recorded_index: Option, + item_id: Option<&str>, + output_index: Option, +) -> bool { + (item_id.is_some() && recorded_id == item_id) + || (output_index.is_some() && recorded_index == output_index) +} + +/// The per-response translation state. +#[derive(Debug)] +pub(super) struct Translator { + requested: String, + id: String, + created: u64, + role_sent: bool, + include_usage: bool, + calls: Vec, + early: Vec, +} + +impl Translator { + /// A translator whose chunks name `requested`, the caller's model, and + /// that adds the usage chunk only when `include_usage` is set. + pub(super) fn new(requested: String, include_usage: bool) -> Translator { + let created = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_or(0, |elapsed| elapsed.as_secs()); + Translator { + requested, + id: "chatcmpl-responses".to_owned(), + created, + role_sent: false, + include_usage, + calls: Vec::new(), + early: Vec::new(), + } + } + + /// Translates one decoded event. + pub(super) fn step(&mut self, event: &Value) -> Step { + match event.get("type").and_then(Value::as_str).unwrap_or("") { + "response.created" | "response.in_progress" => { + self.note_response(event.get("response")); + Step::Continue(Vec::new()) + } + "response.output_text.delta" => Step::Continue(self.text_delta(event, "content")), + "response.refusal.delta" => Step::Continue(self.text_delta(event, "refusal")), + "response.output_item.added" => self.item_added(event), + "response.function_call_arguments.delta" => Step::Continue(self.arguments_delta(event)), + "response.output_item.done" => self.item_done(event), + "response.completed" => Step::Finish(self.finish(event, None)), + "response.incomplete" => { + let reason = event + .pointer("/response/incomplete_details/reason") + .and_then(Value::as_str); + let finish = if reason == Some("content_filter") { + "content_filter" + } else { + "length" + }; + Step::Finish(self.finish(event, Some(finish))) + } + "response.failed" => Step::Fail(failure(event.pointer("/response/error"))), + // The documented event carries the details as its own fields; the + // ChatGPT plan route nests them under `error`. + "error" => Step::Fail(failure( + event + .get("error") + .filter(|error| error.is_object()) + .or(Some(event)), + )), + _ => Step::Continue(Vec::new()), + } + } + + /// Takes the response id and creation time when the event carries them. + fn note_response(&mut self, response: Option<&Value>) { + let Some(response) = response else { return }; + if let Some(id) = response.get("id").and_then(Value::as_str) { + id.clone_into(&mut self.id); + } + if let Some(created) = response.get("created_at").and_then(Value::as_u64) { + self.created = created; + } + } + + fn text_delta(&mut self, event: &Value, field: &str) -> Vec { + let text = event.get("delta").and_then(Value::as_str).unwrap_or(""); + if text.is_empty() { + return Vec::new(); + } + let mut delta = Map::new(); + delta.insert(field.to_owned(), Value::String(text.to_owned())); + vec![self.chunk(delta, None)] + } + + fn item_added(&mut self, event: &Value) -> Step { + let Some(item) = function_call(event) else { + return Step::Continue(Vec::new()); + }; + let output_index = event.get("output_index").and_then(Value::as_u64); + if self + .locate(item.get("id").and_then(Value::as_str), output_index) + .is_some() + { + return Step::Continue(Vec::new()); + } + match self.start_call(item, output_index, false) { + Ok(chunks) => Step::Continue(chunks), + Err(error) => Step::Fail(error), + } + } + + fn arguments_delta(&mut self, event: &Value) -> Vec { + let item_id = event.get("item_id").and_then(Value::as_str); + let output_index = event.get("output_index").and_then(Value::as_u64); + let text = event.get("delta").and_then(Value::as_str).unwrap_or(""); + if text.is_empty() { + return Vec::new(); + } + let Some(index) = self.locate(item_id, output_index) else { + // No `output_item.added` yet: hold the text for the call's start. + match self.early.iter_mut().find(|early| { + same_item( + early.item_id.as_deref(), + early.output_index, + item_id, + output_index, + ) + }) { + Some(early) => early.text.push_str(text), + None => self.early.push(EarlyArguments { + item_id: item_id.map(str::to_owned), + output_index, + text: text.to_owned(), + }), + } + return Vec::new(); + }; + self.calls[index].arguments_sent.push_str(text); + vec![self.chunk(arguments_only(index, text), None)] + } + + fn item_done(&mut self, event: &Value) -> Step { + let Some(item) = function_call(event) else { + return Step::Continue(Vec::new()); + }; + let output_index = event.get("output_index").and_then(Value::as_u64); + let Some(index) = self.locate(item.get("id").and_then(Value::as_str), output_index) else { + return match self.start_call(item, output_index, true) { + Ok(mut chunks) => { + chunks.extend(self.close_empty(self.calls.len() - 1)); + Step::Continue(chunks) + } + Err(error) => Step::Fail(error), + }; + }; + let full = item.get("arguments").and_then(Value::as_str).unwrap_or(""); + let sent = self.calls[index].arguments_sent.clone(); + if !full.starts_with(sent.as_str()) { + return Step::Fail(contradiction(index)); + } + let mut chunks = Vec::new(); + if full.len() > sent.len() { + let missing = &full[sent.len()..]; + self.calls[index].arguments_sent.push_str(missing); + chunks.push(self.chunk(arguments_only(index, missing), None)); + } + chunks.extend(self.close_empty(index)); + Step::Continue(chunks) + } + + /// Sends `{}` for a call that ended with no argument text, so its + /// arguments parse as JSON. + fn close_empty(&mut self, index: usize) -> Option { + if !self.calls[index].arguments_sent.is_empty() { + return None; + } + self.calls[index].arguments_sent.push_str("{}"); + Some(self.chunk(arguments_only(index, "{}"), None)) + } + + /// Registers a new function call and sends its id, name, and the + /// arguments known so far: the held early text reconciled with the + /// item's own. A done item (`complete`) is authoritative, so early text + /// must be its prefix. An added item holds a prefix at most, so one of + /// the two must extend the other. Anything else fails the response. + fn start_call( + &mut self, + item: &Value, + output_index: Option, + complete: bool, + ) -> Result, ProtocolError> { + let index = self.calls.len(); + let call_id = item + .get("call_id") + .or_else(|| item.get("id")) + .and_then(Value::as_str) + .map_or_else(|| format!("call_{index}"), str::to_owned); + let name = item + .get("name") + .and_then(Value::as_str) + .unwrap_or("") + .to_owned(); + let item_id = item.get("id").and_then(Value::as_str); + let early = self + .early + .iter() + .position(|early| { + same_item( + early.item_id.as_deref(), + early.output_index, + item_id, + output_index, + ) + }) + .map(|position| self.early.remove(position).text) + .unwrap_or_default(); + let held = item.get("arguments").and_then(Value::as_str).unwrap_or(""); + let arguments = if held.starts_with(early.as_str()) { + held.to_owned() + } else if !complete && early.starts_with(held) { + early + } else { + return Err(contradiction(index)); + }; + self.calls.push(ToolCall { + item_id: item_id.map(str::to_owned), + output_index, + arguments_sent: arguments.clone(), + }); + let mut delta = Map::new(); + delta.insert( + "tool_calls".to_owned(), + json!([{ + "index": index, + "id": call_id, + "type": "function", + "function": { "name": name, "arguments": arguments }, + }]), + ); + Ok(vec![self.chunk(delta, None)]) + } + + /// The position of the call with this item id or output index. + fn locate(&self, item_id: Option<&str>, output_index: Option) -> Option { + self.calls.iter().position(|call| { + same_item( + call.item_id.as_deref(), + call.output_index, + item_id, + output_index, + ) + }) + } + + /// The final chunks: the finish reason, then the usage when the caller + /// asked for it and the backend reported it. + fn finish(&mut self, event: &Value, reason: Option<&str>) -> Vec { + self.note_response(event.get("response")); + let finish = reason.unwrap_or(if self.calls.is_empty() { + "stop" + } else { + "tool_calls" + }); + let mut chunks = vec![self.chunk(Map::new(), Some(finish))]; + let usage = event + .pointer("/response/usage") + .filter(|usage| usage.is_object()); + if self.include_usage + && let Some(usage) = usage + { + let mut rest = self.envelope(); + rest.insert("usage".to_owned(), chat_usage(usage)); + chunks.push(ChatChunk { + model: self.requested.clone(), + choices: Vec::new(), + rest, + }); + } + chunks + } + + /// The `id`, `object` and `created` every chunk carries. + fn envelope(&self) -> Map { + let mut rest = Map::new(); + rest.insert("id".to_owned(), Value::String(self.id.clone())); + rest.insert( + "object".to_owned(), + Value::String("chat.completion.chunk".to_owned()), + ); + rest.insert("created".to_owned(), Value::from(self.created)); + rest + } + + fn chunk(&mut self, mut delta: Map, finish: Option<&str>) -> ChatChunk { + if !self.role_sent { + delta.insert("role".to_owned(), Value::String("assistant".to_owned())); + self.role_sent = true; + } + let mut choice_rest = Map::new(); + choice_rest.insert( + "finish_reason".to_owned(), + finish.map_or(Value::Null, |reason| Value::String(reason.to_owned())), + ); + let mut rest = self.envelope(); + if self.include_usage { + // The chat contract: every chunk but the last carries a null + // usage when the caller asked for usage. + rest.insert("usage".to_owned(), Value::Null); + } + ChatChunk { + model: self.requested.clone(), + choices: vec![ChatChunkChoice { + index: 0, + delta: Value::Object(delta), + rest: choice_rest, + }], + rest, + } + } +} + +/// The failure for arguments that contradict text already relayed or held. +fn contradiction(index: usize) -> ProtocolError { + tracing::warn!( + index, + "function call arguments contradict the streamed deltas" + ); + ProtocolError::upstream_protocol(std::io::Error::other( + "function call arguments contradict the streamed deltas", + )) +} + +/// The event's item, when it is a function call. +fn function_call(event: &Value) -> Option<&Value> { + event + .get("item") + .filter(|item| item.get("type").and_then(Value::as_str) == Some("function_call")) +} + +fn arguments_only(index: usize, arguments: &str) -> Map { + let mut delta = Map::new(); + delta.insert( + "tool_calls".to_owned(), + json!([{ "index": index, "function": { "arguments": arguments } }]), + ); + delta +} + +/// Responses usage in the chat completions shape. +fn chat_usage(usage: &Value) -> Value { + let input = usage + .get("input_tokens") + .and_then(Value::as_u64) + .unwrap_or(0); + let output = usage + .get("output_tokens") + .and_then(Value::as_u64) + .unwrap_or(0); + let total = usage + .get("total_tokens") + .and_then(Value::as_u64) + .unwrap_or(input + output); + let mut chat = json!({ + "prompt_tokens": input, + "completion_tokens": output, + "total_tokens": total, + }); + if let Some(cached) = usage.pointer("/input_tokens_details/cached_tokens") { + chat["prompt_tokens_details"] = json!({ "cached_tokens": cached }); + } + if let Some(reasoning) = usage.pointer("/output_tokens_details/reasoning_tokens") { + chat["completion_tokens_details"] = json!({ "reasoning_tokens": reasoning }); + } + chat +} + +/// A failed response as a [`ProtocolError::UpstreamStatus`]. A usage or rate +/// limit maps to 429, an invalid request to 400, anything else to 502. Only +/// the error's bounded, control-escaped `code`, `type`, and `message` are +/// logged or kept. +fn failure(error: Option<&Value>) -> ProtocolError { + let field = |name: &str| { + error + .and_then(|error| error.get(name)) + .and_then(Value::as_str) + .map_or_else(String::new, |text| { + escape_control(text, MAX_ERROR_MESSAGE_CHARS) + }) + }; + let (code, kind, message) = (field("code"), field("type"), field("message")); + tracing::info!( + code = %code, + r#type = %kind, + error.message = %message, + "upstream response failed" + ); + let status = if ["limit", "quota", "rate"] + .iter() + .any(|word| code.contains(word)) + { + 429 + } else if kind == "invalid_request_error" { + 400 + } else { + 502 + }; + let body = json!({ "error": { "code": code, "type": kind, "message": message } }); + ProtocolError::upstream_status(status, body.to_string()) +} diff --git a/crates/gateway/protocol/src/upstream/responses/sse.rs b/crates/gateway/protocol/src/upstream/responses/sse.rs new file mode 100644 index 00000000..3a18771a --- /dev/null +++ b/crates/gateway/protocol/src/upstream/responses/sse.rs @@ -0,0 +1,267 @@ +//! The Responses event stream: SSE framing, translation to chat chunks, and +//! the fold that turns those chunks into one non-streaming response. +//! +//! Framing follows the SSE parsing rules: lines end in CRLF, LF or CR; one +//! leading byte-order mark is dropped; an event's `data:` lines are joined; +//! a blank line dispatches the event; and an event still unfinished when the +//! body ends is discarded, never dispatched. The `event:` field is ignored, +//! because every event names itself in its JSON `type`. The upstream +//! `Content-Type` is not checked, because the ChatGPT plan route sends none. +//! A stream that ends before `response.completed` (or +//! `response.incomplete`) ends with an error, so a truncated reply is never +//! relayed as a complete one. + +use std::collections::VecDeque; + +use bytes::Bytes; +use futures_util::StreamExt; +use futures_util::stream::BoxStream; +use serde_json::{Map, Value, json}; + +use super::events::{Step, Translator}; +use crate::error::ProtocolError; +use crate::wire::{ChatChunk, ChatResponse}; + +/// The largest single event accepted. An `output_item.done` repeats a whole +/// item, so this is generous; a larger event ends the stream with an error +/// rather than growing the buffer without bound. +const MAX_EVENT_BYTES: usize = 16 * 1024 * 1024; + +/// The decoder's state between polls. +struct Decoder { + bytes: BoxStream<'static, reqwest::Result>, + buffer: Vec, + data: String, + translator: Translator, + ready: VecDeque>, + /// Whether the leading byte-order mark check has run. + bom_checked: bool, + ended: bool, +} + +impl Decoder { + /// Decodes the event held in `data`, queueing what it produces. + fn dispatch(&mut self) { + let data = std::mem::take(&mut self.data); + if data.is_empty() || data == "[DONE]" { + return; + } + let event = match serde_json::from_str::(&data) { + Ok(event) => event, + Err(error) => { + tracing::warn!(%error, "skipping undecodable upstream event"); + return; + } + }; + match self.translator.step(&event) { + Step::Continue(chunks) => self.ready.extend(chunks.into_iter().map(Ok)), + Step::Finish(chunks) => { + self.ready.extend(chunks.into_iter().map(Ok)); + self.ended = true; + } + Step::Fail(error) => { + self.ready.push_back(Err(error)); + self.ended = true; + } + } + } + + /// Takes one complete line from the buffer and applies it. A CR as the + /// buffer's last byte waits for the next read, which may hold its LF. + fn take_line(&mut self) -> bool { + if !self.bom_checked { + if self.buffer.len() < 3 + && !self.buffer.is_empty() + && b"\xEF\xBB\xBF".starts_with(&self.buffer) + { + return false; + } + if self.buffer.starts_with(b"\xEF\xBB\xBF") { + self.buffer.drain(..3); + } + self.bom_checked = !self.buffer.is_empty(); + } + let Some(end) = self + .buffer + .iter() + .position(|byte| matches!(byte, b'\n' | b'\r')) + else { + return false; + }; + let width = match (self.buffer[end], self.buffer.get(end + 1)) { + (b'\r', Some(b'\n')) => 2, + (b'\r', None) => return false, + _ => 1, + }; + let raw: Vec = self.buffer.drain(..end + width).collect(); + let line = String::from_utf8_lossy(&raw[..end]); + let line = line.as_ref(); + if line.is_empty() { + self.dispatch(); + } else if let Some(data) = line.strip_prefix("data:") { + if !self.data.is_empty() { + self.data.push('\n'); + } + self.data.push_str(data.strip_prefix(' ').unwrap_or(data)); + } + true + } + + /// Fails the stream: the error is the last item. + fn fail(&mut self, error: ProtocolError) { + self.ready.push_back(Err(error)); + self.ended = true; + } +} + +/// Translates a Responses SSE body into chat completion chunks naming +/// `requested`. Dropping the stream drops the upstream response, which +/// aborts the connection. +pub(super) fn chat_chunks( + response: reqwest::Response, + requested: String, + include_usage: bool, +) -> BoxStream<'static, Result> { + let decoder = Decoder { + bytes: response.bytes_stream().boxed(), + buffer: Vec::new(), + data: String::new(), + translator: Translator::new(requested, include_usage), + ready: VecDeque::new(), + bom_checked: false, + ended: false, + }; + futures_util::stream::unfold(decoder, |mut decoder| async move { + loop { + if let Some(item) = decoder.ready.pop_front() { + return Some((item, decoder)); + } + if decoder.ended { + return None; + } + if decoder.take_line() { + continue; + } + if decoder.buffer.len() + decoder.data.len() > MAX_EVENT_BYTES { + decoder.fail(ProtocolError::upstream_protocol(std::io::Error::other( + "upstream event exceeds the size limit", + ))); + continue; + } + match decoder.bytes.next().await { + Some(Ok(bytes)) => decoder.buffer.extend_from_slice(&bytes), + Some(Err(error)) => decoder.fail(ProtocolError::upstream_transport(error)), + None => { + // A lone CR at the very end still ends its line. + if decoder.buffer.last() == Some(&b'\r') { + decoder.buffer.push(b'\n'); + while !decoder.ended && decoder.take_line() {} + } + // Whatever is left is an unfinished event: discarded. + decoder.buffer.clear(); + decoder.data.clear(); + if !decoder.ended { + decoder.fail(ProtocolError::upstream_protocol(std::io::Error::other( + "upstream stream ended before the response completed", + ))); + } + } + } + } + }) + .boxed() +} + +/// Folds a translated chunk stream into one chat completion: the text +/// concatenated, tool calls assembled by index, and the last finish reason +/// and usage kept. The first error ends the fold. +pub(super) async fn collect( + mut chunks: BoxStream<'static, Result>, + requested: String, +) -> Result { + let mut content = String::new(); + let mut refusal = String::new(); + let mut calls: Vec = Vec::new(); + let mut finish = Value::Null; + let mut rest = Map::new(); + while let Some(chunk) = chunks.next().await { + let chunk = chunk?; + for key in ["id", "created", "usage"] { + if let Some(value) = chunk.rest.get(key).filter(|value| !value.is_null()) { + rest.insert(key.to_owned(), value.clone()); + } + } + for choice in &chunk.choices { + if let Some(reason) = choice.rest.get("finish_reason").filter(|v| !v.is_null()) { + finish = reason.clone(); + } + let delta = &choice.delta; + if let Some(text) = delta.get("content").and_then(Value::as_str) { + content.push_str(text); + } + if let Some(text) = delta.get("refusal").and_then(Value::as_str) { + refusal.push_str(text); + } + for call in delta + .get("tool_calls") + .and_then(Value::as_array) + .into_iter() + .flatten() + { + merge_call(&mut calls, call); + } + } + } + rest.insert( + "object".to_owned(), + Value::String("chat.completion".to_owned()), + ); + let mut message = json!({ + "role": "assistant", + "content": if content.is_empty() && !calls.is_empty() { Value::Null } else { Value::String(content) }, + }); + if !calls.is_empty() { + message["tool_calls"] = Value::Array(calls); + } + if !refusal.is_empty() { + message["refusal"] = Value::String(refusal); + } + Ok(ChatResponse { + model: requested, + choices: vec![json!({ "index": 0, "message": message, "finish_reason": finish })], + rest, + }) +} + +/// Applies one `tool_calls` delta to the assembled calls. +fn merge_call(calls: &mut Vec, delta: &Value) { + let Some(index) = delta + .get("index") + .and_then(Value::as_u64) + .and_then(|index| usize::try_from(index).ok()) + else { + return; + }; + while calls.len() <= index { + calls.push( + json!({ "id": "", "type": "function", "function": { "name": "", "arguments": "" } }), + ); + } + let call = &mut calls[index]; + if let Some(id) = delta.get("id").and_then(Value::as_str) { + call["id"] = Value::String(id.to_owned()); + } + if let Some(name) = delta.pointer("/function/name").and_then(Value::as_str) + && !name.is_empty() + { + call["function"]["name"] = Value::String(name.to_owned()); + } + if let Some(arguments) = delta.pointer("/function/arguments").and_then(Value::as_str) { + let mut joined = call["function"]["arguments"] + .as_str() + .unwrap_or("") + .to_owned(); + joined.push_str(arguments); + call["function"]["arguments"] = Value::String(joined); + } +} diff --git a/crates/gateway/protocol/src/upstream/tests.rs b/crates/gateway/protocol/src/upstream/tests.rs index c28b664b..5a7c7a1f 100644 --- a/crates/gateway/protocol/src/upstream/tests.rs +++ b/crates/gateway/protocol/src/upstream/tests.rs @@ -12,6 +12,7 @@ use super::*; mod chat; mod embedding; mod rerank; +mod responses; mod speech; mod stream; @@ -124,3 +125,48 @@ fn serve_stalled() -> (String, JoinHandle<()>) { }); (format!("http://{addr}"), handle) } + +#[tokio::test] +async fn missing_no_redirect_streaming_client_fails_closed() { + use std::sync::atomic::{AtomicUsize, Ordering}; + + #[derive(Debug)] + struct CountingBearer { + calls: AtomicUsize, + } + + #[async_trait::async_trait] + impl crate::upstream::BearerSource for CountingBearer { + async fn bearer(&self) -> Result { + self.calls.fetch_add(1, Ordering::SeqCst); + Ok(gateway_config::Secret::new("plan-secret".to_owned())) + } + } + + let (base, handle) = serve_once("200 OK", "{}"); + let bearer = std::sync::Arc::new(CountingBearer { + calls: AtomicUsize::new(0), + }); + let upstream = crate::upstream::responses::ResponsesUpstream::without_http_client( + &base, + bearer.clone(), + crate::wire::RequestPolicy::Plan, + ); + let outcome = upstream.stream(request("m"), "m").await; + assert!( + matches!( + outcome, + Err(crate::error::ProtocolError::UpstreamConnect(_)) + ), + "expected fail-closed UpstreamConnect, got {outcome:?}" + ); + assert_eq!( + bearer.calls.load(Ordering::SeqCst), + 0, + "without the no-redirect client, no bearer is even requested" + ); + // Unblock serve_once's accept without issuing any real upstream request. + let probe = base.trim_start_matches("http://"); + drop(std::net::TcpStream::connect(probe)); + assert_eq!(handle.join().expect("server"), ""); +} diff --git a/crates/gateway/protocol/src/upstream/tests/responses.rs b/crates/gateway/protocol/src/upstream/tests/responses.rs new file mode 100644 index 00000000..4991f4ac --- /dev/null +++ b/crates/gateway/protocol/src/upstream/tests/responses.rs @@ -0,0 +1,483 @@ +//! Tests for the Responses upstream: the request it sends, the event stream +//! translated to chat chunks, the non-streaming fold, and its failures. + +use std::sync::{Arc, Mutex}; + +use serde_json::{Value, json}; + +use super::*; +use crate::wire::RequestPolicy; + +mod failures; + +/// A mock Responses backend: answers one request with `status_line` and +/// `body`, written in `pieces` separate writes and with **no** +/// `Content-Type` (the ChatGPT plan route sends none). Returns the base URL +/// and the captured raw request. +fn serve_events(status_line: &str, body: &str, pieces: usize) -> (String, JoinHandle) { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind mock backend"); + let addr = listener.local_addr().expect("addr"); + let head = format!("HTTP/1.1 {status_line}\r\nConnection: close\r\n\r\n"); + let body = body.as_bytes().to_vec(); + let handle = thread::spawn(move || -> String { + let (mut stream, _) = listener.accept().expect("accept"); + let _ = stream.set_read_timeout(Some(Duration::from_millis(200))); + let mut request = Vec::new(); + let mut buf = [0_u8; 4096]; + loop { + match stream.read(&mut buf) { + Ok(0) | Err(_) => break, + Ok(n) => request.extend_from_slice(&buf[..n]), + } + } + let _ = stream.write_all(head.as_bytes()); + let size = body.len().div_ceil(pieces.max(1)).max(1); + for piece in body.chunks(size) { + let _ = stream.write_all(piece); + let _ = stream.flush(); + thread::sleep(Duration::from_millis(2)); + } + String::from_utf8_lossy(&request).into_owned() + }); + (format!("http://{addr}"), handle) +} + +fn sse(events: &[Value]) -> String { + use std::fmt::Write as _; + + let mut body = String::new(); + for event in events { + let kind = event["type"].as_str().unwrap_or("message"); + let _ = write!(body, "event: {kind}\ndata: {event}\n\n"); + } + body +} + +fn completed(usage: &Value) -> Value { + json!({ "type": "response.completed", + "response": { "id": "resp_1", "created_at": 1_763_000_000_u64, "status": "completed", + "output": [], "usage": usage } }) +} + +fn upstream(base: &str) -> ResponsesUpstream { + ResponsesUpstream::new( + base, + Arc::new(StaticBearer::new(Secret::new("sk-test".to_owned()))), + RequestPolicy::Plan, + ) +} + +async fn all_chunks(upstream: &ResponsesUpstream) -> Vec> { + all_chunks_of(upstream, request("caller-model")).await +} + +/// A streaming request that asks for usage, as the harness does. +fn usage_request() -> ChatRequest { + let mut req = request("caller-model"); + req.rest.insert( + "stream_options".to_owned(), + json!({ "include_usage": true }), + ); + req +} + +async fn all_chunks_of( + upstream: &ResponsesUpstream, + req: ChatRequest, +) -> Vec> { + let mut streamed = upstream + .stream(req, "backend-model") + .await + .expect("stream opens"); + let mut chunks = Vec::new(); + while let Some(item) = streamed.chunks.next().await { + chunks.push(item); + } + chunks +} + +#[tokio::test] +async fn text_streams_as_chat_chunks_and_unasked_usage_is_omitted() { + // The plan route's quirk: response.completed carries an empty output, so + // the text must come from the deltas. One "é" is split across writes. + let body = sse(&[ + json!({ "type": "response.created", "response": { "id": "resp_1", "created_at": 1_763_000_000_u64 } }), + json!({ "type": "response.output_text.delta", "item_id": "msg_1", "output_index": 0, "delta": "Caf" }), + json!({ "type": "response.output_text.delta", "item_id": "msg_1", "output_index": 0, "delta": "é!" }), + completed( + &json!({ "input_tokens": 10, "output_tokens": 3, "total_tokens": 13, + "input_tokens_details": { "cached_tokens": 4 }, + "output_tokens_details": { "reasoning_tokens": 1 } }), + ), + ]); + let (base, handle) = serve_events("200 OK", &body, 37); + let chunks: Vec = all_chunks(&upstream(&base)) + .await + .into_iter() + .map(|item| item.expect("chunk ok")) + .collect(); + let raw = handle.join().expect("server"); + + assert!(raw.starts_with("POST /responses "), "request line: {raw}"); + assert!( + raw.to_ascii_lowercase() + .contains("authorization: bearer sk-test") + ); + let sent: Value = + serde_json::from_str(raw.split("\r\n\r\n").nth(1).expect("body")).expect("json"); + assert_eq!(sent["model"], "backend-model"); + assert_eq!(sent["stream"], true); + assert_eq!(sent["store"], false); + + let text: String = chunks + .iter() + .filter_map(|chunk| { + chunk.choices[0] + .delta + .get("content") + .and_then(Value::as_str) + }) + .collect(); + assert_eq!(text, "Café!"); + assert!(chunks.iter().all(|chunk| chunk.model == "caller-model")); + assert_eq!(chunks[0].choices[0].delta["role"], "assistant"); + assert_eq!(chunks[0].rest["id"], "resp_1"); + // Usage was not asked for, so no usage chunk follows the finish. + let last = chunks.last().expect("a final chunk"); + assert_eq!(last.choices[0].rest["finish_reason"], "stop"); + assert!(chunks.iter().all(|chunk| !chunk.rest.contains_key("usage"))); +} + +#[tokio::test] +async fn asked_for_usage_arrives_as_a_last_chunk_with_empty_choices() { + let body = sse(&[ + json!({ "type": "response.output_text.delta", "delta": "ok" }), + completed( + &json!({ "input_tokens": 10, "output_tokens": 3, "total_tokens": 13, + "input_tokens_details": { "cached_tokens": 4 }, + "output_tokens_details": { "reasoning_tokens": 1 } }), + ), + ]); + let (base, handle) = serve_events("200 OK", &body, 1); + let chunks: Vec = all_chunks_of(&upstream(&base), usage_request()) + .await + .into_iter() + .map(|item| item.expect("chunk ok")) + .collect(); + handle.join().expect("server"); + let finish = &chunks[chunks.len() - 2]; + assert_eq!(finish.choices[0].rest["finish_reason"], "stop"); + assert_eq!( + finish.rest["usage"], + Value::Null, + "non-final chunks carry a null usage" + ); + let last = chunks.last().expect("a usage chunk"); + assert!(last.choices.is_empty()); + assert_eq!( + last.rest["usage"], + json!({ "prompt_tokens": 10, "completion_tokens": 3, "total_tokens": 13, + "prompt_tokens_details": { "cached_tokens": 4 }, + "completion_tokens_details": { "reasoning_tokens": 1 } }) + ); +} + +#[tokio::test] +async fn a_streamed_tool_call_folds_into_one_non_streaming_message() { + let body = sse(&[ + json!({ "type": "response.output_item.added", "output_index": 1, + "item": { "type": "function_call", "id": "fc_1", "call_id": "call_9", + "name": "weather", "arguments": "" } }), + json!({ "type": "response.function_call_arguments.delta", "item_id": "fc_1", + "output_index": 1, "delta": "{\"city\":" }), + json!({ "type": "response.function_call_arguments.delta", "item_id": "fc_1", + "output_index": 1, "delta": "\"Siem Reap\"}" }), + json!({ "type": "response.output_item.done", "output_index": 1, + "item": { "type": "function_call", "id": "fc_1", "call_id": "call_9", + "name": "weather", "arguments": "{\"city\":\"Siem Reap\"}" } }), + completed(&json!({ "input_tokens": 5, "output_tokens": 7 })), + ]); + let (base, handle) = serve_events("200 OK", &body, 1); + let response = upstream(&base) + .send(request("caller-model"), "backend-model") + .await + .expect("send ok"); + handle.join().expect("server"); + + assert_eq!(response.model, "caller-model"); + let choice = &response.choices[0]; + assert_eq!(choice["finish_reason"], "tool_calls"); + assert_eq!(choice["message"]["content"], Value::Null); + assert_eq!( + choice["message"]["tool_calls"], + json!([{ "id": "call_9", "type": "function", + "function": { "name": "weather", "arguments": "{\"city\":\"Siem Reap\"}" } }]) + ); + assert_eq!(response.rest["usage"]["total_tokens"], 12); + assert_eq!(response.rest["object"], "chat.completion"); +} + +#[tokio::test] +async fn a_tool_call_seen_only_at_done_still_arrives_whole_and_once() { + let item = json!({ "type": "function_call", "id": "fc_2", "call_id": "call_2", + "name": "f", "arguments": "{\"a\":1}" }); + let body = sse(&[ + json!({ "type": "response.output_item.done", "output_index": 0, "item": item }), + json!({ "type": "response.output_item.done", "output_index": 0, "item": item }), + completed(&json!({})), + ]); + let (base, handle) = serve_events("200 OK", &body, 3); + let response = upstream(&base) + .send(request("m"), "m") + .await + .expect("send ok"); + handle.join().expect("server"); + assert_eq!( + response.choices[0]["message"]["tool_calls"], + json!([{ "id": "call_2", "type": "function", "function": { "name": "f", "arguments": "{\"a\":1}" } }]) + ); +} + +#[tokio::test] +async fn a_stream_cut_before_completion_ends_with_an_error() { + let body = sse(&[json!({ "type": "response.output_text.delta", "delta": "partial" })]); + let (base, handle) = serve_events("200 OK", &body, 1); + let chunks = all_chunks(&upstream(&base)).await; + handle.join().expect("server"); + assert!(chunks[0].is_ok()); + match chunks.last() { + Some(Err(ProtocolError::UpstreamProtocol(_))) => {} + other => panic!("expected a truncation error, got {other:?}"), + } +} + +#[tokio::test] +async fn crlf_framing_and_multi_line_data_decode() { + let body = "event: response.output_text.delta\r\ndata: {\"type\":\"response.output_text.delta\",\r\ndata: \"delta\":\"ok\"}\r\n\r\n\ + data: {\"type\":\"response.completed\",\"response\":{}}\r\n\r\n"; + let (base, handle) = serve_events("200 OK", body, 5); + let chunks = all_chunks(&upstream(&base)).await; + handle.join().expect("server"); + let chunks: Vec = chunks.into_iter().map(|c| c.expect("ok")).collect(); + assert_eq!(chunks[0].choices[0].delta["content"], "ok"); + assert_eq!( + chunks.last().expect("last").choices[0].rest["finish_reason"], + "stop" + ); +} + +/// A bearer source that records what the backend rejected. +#[derive(Debug, Default)] +struct RecordingBearer(Mutex>); + +#[async_trait] +impl BearerSource for RecordingBearer { + async fn bearer(&self) -> Result { + Ok(Secret::new("expired".to_owned())) + } + + async fn rejected(&self, rejected: &Secret) { + self.0 + .lock() + .expect("lock") + .push(rejected.expose().to_owned()); + } +} + +#[tokio::test] +async fn a_401_is_reported_to_the_bearer_source() { + let (base, handle) = serve_events( + "401 Unauthorized", + "{\"error\":{\"code\":\"invalid_token\"}}", + 1, + ); + let bearer = Arc::new(RecordingBearer::default()); + let upstream = ResponsesUpstream::new(&base, bearer.clone(), RequestPolicy::Plan); + let outcome = upstream.stream(request("m"), "m").await; + handle.join().expect("server"); + assert!(matches!( + outcome, + Err(ProtocolError::UpstreamStatus { status: 401, .. }) + )); + assert_eq!(*bearer.0.lock().expect("lock"), vec!["expired".to_owned()]); +} + +#[tokio::test] +async fn an_untranslatable_request_is_refused_before_anything_is_sent() { + // Port 9 (discard) is never contacted: refusal happens first. + let upstream = upstream("http://127.0.0.1:9"); + let mut req = request("m"); + req.rest.insert("n".to_owned(), json!(3)); + match upstream.send(req, "m").await { + Err(ProtocolError::UnsupportedRequest(reason)) => assert!(reason.contains('n')), + other => panic!("expected UnsupportedRequest, got {other:?}"), + } +} + +#[tokio::test] +async fn null_usage_stays_unknown_even_when_asked_for() { + let body = sse(&[ + json!({ "type": "response.output_text.delta", "delta": "ok" }), + json!({ "type": "response.incomplete", "response": { "usage": null, + "incomplete_details": { "reason": "max_output_tokens" } } }), + ]); + let (base, handle) = serve_events("200 OK", &body, 1); + let chunks: Vec = all_chunks_of(&upstream(&base), usage_request()) + .await + .into_iter() + .map(|item| item.expect("chunk ok")) + .collect(); + handle.join().expect("server"); + let last = chunks.last().expect("a finish chunk"); + assert_eq!(last.choices[0].rest["finish_reason"], "length"); + assert!(chunks.iter().all(|chunk| chunk.rest["usage"].is_null())); + assert!( + chunks.iter().all(|chunk| !chunk.choices.is_empty()), + "no usage chunk" + ); +} + +#[tokio::test] +async fn early_text_that_the_done_arguments_contradict_fails() { + // No output_item.added: early deltas, then a done whose arguments are not + // an extension of them. The done arguments are authoritative. + let body = sse(&[ + json!({ "type": "response.function_call_arguments.delta", "item_id": "fc_1", + "output_index": 0, "delta": "{\"x\":123}" }), + json!({ "type": "response.output_item.done", "output_index": 0, + "item": { "type": "function_call", "id": "fc_1", "call_id": "c1", + "name": "f", "arguments": "{\"x\":1}" } }), + completed(&json!({})), + ]); + let (base, handle) = serve_events("200 OK", &body, 1); + let outcome = upstream(&base).send(request("m"), "m").await; + handle.join().expect("server"); + assert!( + matches!(outcome, Err(ProtocolError::UpstreamProtocol(_))), + "{outcome:?}" + ); +} + +#[tokio::test] +async fn argument_text_before_the_call_starts_is_kept() { + // A reordering backend: one delta arrives before output_item.added. + let body = sse(&[ + json!({ "type": "response.function_call_arguments.delta", "item_id": "fc_1", + "output_index": 0, "delta": "{\"x\":" }), + json!({ "type": "response.output_item.added", "output_index": 0, + "item": { "type": "function_call", "id": "fc_1", "call_id": "c1", + "name": "f", "arguments": "" } }), + json!({ "type": "response.function_call_arguments.delta", "item_id": "fc_1", + "output_index": 0, "delta": "1}" }), + json!({ "type": "response.output_item.done", "output_index": 0, + "item": { "type": "function_call", "id": "fc_1", "call_id": "c1", + "name": "f", "arguments": "{\"x\":1}" } }), + completed(&json!({})), + ]); + let (base, handle) = serve_events("200 OK", &body, 1); + let response = upstream(&base) + .send(request("m"), "m") + .await + .expect("send ok"); + handle.join().expect("server"); + assert_eq!( + response.choices[0]["message"]["tool_calls"][0]["function"]["arguments"], + "{\"x\":1}" + ); +} + +#[tokio::test] +async fn arguments_that_contradict_the_deltas_fail_the_response() { + let body = sse(&[ + json!({ "type": "response.output_item.added", "output_index": 0, + "item": { "type": "function_call", "id": "fc_1", "call_id": "c1", + "name": "f", "arguments": "" } }), + json!({ "type": "response.function_call_arguments.delta", "item_id": "fc_1", + "output_index": 0, "delta": "{\"a\"" }), + json!({ "type": "response.output_item.done", "output_index": 0, + "item": { "type": "function_call", "id": "fc_1", "call_id": "c1", + "name": "f", "arguments": "{\"b\":2}" } }), + completed(&json!({})), + ]); + let (base, handle) = serve_events("200 OK", &body, 1); + let outcome = upstream(&base).send(request("m"), "m").await; + handle.join().expect("server"); + assert!( + matches!(outcome, Err(ProtocolError::UpstreamProtocol(_))), + "{outcome:?}" + ); +} + +#[tokio::test] +async fn a_call_with_no_arguments_gets_an_empty_object() { + let item = json!({ "type": "function_call", "id": "fc_1", "call_id": "c1", + "name": "now", "arguments": "" }); + let body = sse(&[ + json!({ "type": "response.output_item.added", "output_index": 0, "item": item }), + json!({ "type": "response.output_item.done", "output_index": 0, "item": item }), + completed(&json!({})), + ]); + let (base, handle) = serve_events("200 OK", &body, 1); + let response = upstream(&base) + .send(request("m"), "m") + .await + .expect("send ok"); + handle.join().expect("server"); + assert_eq!( + response.choices[0]["message"]["tool_calls"][0]["function"]["arguments"], + "{}" + ); +} + +#[tokio::test] +async fn cr_only_lines_and_a_leading_bom_decode() { + let body = "\u{feff}data: {\"type\":\"response.output_text.delta\",\"delta\":\"hi\"}\r\r\ + data: {\"type\":\"response.completed\",\"response\":{}}\r\r"; + let (base, handle) = serve_events("200 OK", body, 7); + let chunks: Vec = all_chunks(&upstream(&base)) + .await + .into_iter() + .map(|item| item.expect("chunk ok")) + .collect(); + handle.join().expect("server"); + assert_eq!(chunks[0].choices[0].delta["content"], "hi"); + assert_eq!( + chunks.last().expect("last").choices[0].rest["finish_reason"], + "stop" + ); +} + +#[tokio::test] +async fn a_terminal_event_without_its_blank_line_is_not_a_success() { + let body = "data: {\"type\":\"response.output_text.delta\",\"delta\":\"hi\"}\n\n\ + data: {\"type\":\"response.completed\",\"response\":{}}\n"; + let (base, handle) = serve_events("200 OK", body, 1); + let chunks = all_chunks(&upstream(&base)).await; + handle.join().expect("server"); + match chunks.last() { + Some(Err(ProtocolError::UpstreamProtocol(_))) => {} + other => panic!("expected a truncation error, got {other:?}"), + } +} + +#[tokio::test] +async fn a_redirect_is_not_followed() { + // A 307 would resend the bearer and the prompt body to the new host. + let (target, target_handle) = serve_events("200 OK", "", 1); + let location = format!("307 Temporary Redirect\r\nLocation: {target}/responses"); + let (base, handle) = serve_events(&location, "", 1); + let outcome = upstream(&base).stream(request("m"), "m").await; + handle.join().expect("server"); + assert!( + matches!( + outcome, + Err(ProtocolError::UpstreamStatus { status: 307, .. }) + ), + "{outcome:?}" + ); + // Release the target's accept loop; it must have seen no request. + drop(std::net::TcpStream::connect( + target.trim_start_matches("http://"), + )); + assert_eq!(target_handle.join().expect("target"), ""); +} diff --git a/crates/gateway/protocol/src/upstream/tests/responses/failures.rs b/crates/gateway/protocol/src/upstream/tests/responses/failures.rs new file mode 100644 index 00000000..48ccbd17 --- /dev/null +++ b/crates/gateway/protocol/src/upstream/tests/responses/failures.rs @@ -0,0 +1,105 @@ +//! Tests for how a failure the upstream reports inside its event stream +//! reaches the caller. + +use serde_json::{Value, json}; + +use super::*; + +/// The last item of a stream that reports `failure` after one text delta. +async fn last_after(failure: Value) -> Option> { + let body = sse(&[ + json!({ "type": "response.output_text.delta", "delta": "Hel" }), + failure, + ]); + let (base, handle) = serve_events("200 OK", &body, 1); + let mut chunks = all_chunks(&upstream(&base)).await; + handle.join().expect("server"); + chunks.pop() +} + +/// Asserts `last` is an upstream failure that displays as `display`, which +/// names both the status and the code. +fn assert_failure(last: Option>, display: &str) { + match last { + Some(Err(error @ ProtocolError::UpstreamStatus { .. })) => { + assert_eq!(error.to_string(), display); + } + other => panic!("expected an upstream status, got {other:?}"), + } +} + +#[tokio::test] +async fn a_usage_limit_failure_mid_stream_is_a_429() { + let last = last_after(json!({ "type": "response.failed", "response": { "error": { + "code": "subscription_sharing_usage_limit_exceeded", + "message": "You have reached your plan's limit." } } })) + .await; + assert_failure( + last, + "upstream returned 429 (subscription_sharing_usage_limit_exceeded)", + ); +} + +#[tokio::test] +async fn an_error_event_with_a_nested_error_keeps_its_code() { + // The event the ChatGPT plan route sends once the plan's limit is spent. + let last = last_after(json!({ "type": "error", "sequence_number": 3, "error": { + "type": "invalid_request_error", + "code": "subscription_sharing_usage_limit_exceeded", + "message": "The ChatGPT user has reached their Subscription Sharing usage limit.", + "param": null } })) + .await; + assert_failure( + last, + "upstream returned 429 (subscription_sharing_usage_limit_exceeded)", + ); +} + +#[tokio::test] +async fn an_error_event_with_its_own_fields_keeps_its_code() { + let last = last_after(json!({ "type": "error", "sequence_number": 1, + "code": "rate_limit_exceeded", "message": "Slow down.", "param": null })) + .await; + assert_failure(last, "upstream returned 429 (rate_limit_exceeded)"); +} + +#[tokio::test] +async fn a_nested_invalid_request_that_names_no_limit_is_a_400() { + let last = last_after(json!({ "type": "error", "error": { + "type": "invalid_request_error", + "code": "unsupported_parameter", + "message": "Unsupported parameter." } })) + .await; + assert_failure(last, "upstream returned 400 (unsupported_parameter)"); +} + +#[tokio::test] +async fn a_nested_error_is_read_whole_and_the_events_own_fields_are_not_mixed_in() { + let last = last_after(json!({ "type": "error", + "code": "rate_limit_exceeded", "message": "Slow down.", + "error": { + "type": "invalid_request_error", + "code": "model_not_found", + "message": "No such model." } })) + .await; + assert_failure(last, "upstream returned 400 (model_not_found)"); +} + +#[tokio::test] +async fn an_error_field_that_is_not_an_object_leaves_the_events_own_fields() { + for not_an_object in [json!(null), json!("text"), json!(["a"])] { + let last = last_after(json!({ "type": "error", + "code": "rate_limit_exceeded", "message": "Slow down.", + "error": not_an_object })) + .await; + assert_failure(last, "upstream returned 429 (rate_limit_exceeded)"); + } +} + +#[tokio::test] +async fn an_empty_nested_error_is_a_502_with_no_code() { + let last = last_after(json!({ "type": "error", + "code": "rate_limit_exceeded", "message": "Slow down.", "error": {} })) + .await; + assert_failure(last, "upstream returned 502"); +} diff --git a/crates/gateway/protocol/src/wire.rs b/crates/gateway/protocol/src/wire.rs index 59144190..c0e4e5b3 100644 --- a/crates/gateway/protocol/src/wire.rs +++ b/crates/gateway/protocol/src/wire.rs @@ -19,12 +19,14 @@ use serde_json::{Map, Value}; mod embedding; mod rerank; +mod responses; mod speech; pub use gateway_api_types::ModelInfo; pub use self::embedding::{EmbeddingInput, EmbeddingRequest, EmbeddingResponse}; pub use self::rerank::{RerankRequest, RerankResponse}; +pub use self::responses::{RequestPolicy, to_responses_request}; pub use self::speech::{SpeechRequest, SpeechResponseFormat, SpeechStreamFormat, SpeechVoice}; /// An incoming chat completions request. diff --git a/crates/gateway/protocol/src/wire/responses.rs b/crates/gateway/protocol/src/wire/responses.rs new file mode 100644 index 00000000..2102b0e6 --- /dev/null +++ b/crates/gateway/protocol/src/wire/responses.rs @@ -0,0 +1,329 @@ +//! Chat completions to OpenAI Responses: the request half of the +//! `openai-responses` endpoint protocol. +//! +//! Callers always speak chat completions to the gateway. An endpoint whose +//! protocol is `openai-responses` receives the same request rewritten as a +//! Responses request, built here. The mapping: +//! +//! | Chat completions | Responses | +//! |---|---| +//! | leading `system` / `developer` messages | `instructions` | +//! | later `system` / `developer` messages | `developer` message items | +//! | `user` / `assistant` text | message items | +//! | assistant `tool_calls` | `function_call` items | +//! | `tool` messages | `function_call_output` items | +//! | `tools` (`function` wrapper) | flat `function` tools | +//! | `tool_choice`, `parallel_tool_calls` | the same | +//! | `reasoning_effort` | `reasoning.effort` | +//! | `response_format` | `text.format` | +//! | `temperature`, `top_p`, `max_tokens` / `max_completion_tokens` | the same, `max_output_tokens` (standard policy only) | +//! +//! The upstream request always streams and never asks the provider to +//! store the response. Fields with no Responses equivalent that only tune +//! the reply (`stream_options`, `stop`, `seed`, `chat_template_kwargs`, …) +//! are dropped; fields whose loss would change what the caller gets back +//! (`n` above 1, legacy `function` messages, non-function tools) refuse the +//! request before anything is sent. + +use serde_json::{Map, Value, json}; + +use super::ChatRequest; + +/// Which request fields an `openai-responses` endpoint accepts. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] +pub enum RequestPolicy { + /// An endpoint reached with an API key: sampling and output-limit + /// fields pass through under their Responses names. + Standard, + /// A ChatGPT plan endpoint, which refuses sampling and output-limit + /// fields (`temperature`, `top_p`, `max_output_tokens`, …), so they are + /// never sent. + Plan, +} + +/// Builds the Responses request body for `req`, addressed to +/// `upstream_model`. +/// +/// # Errors +/// Returns a static reason when `req` holds something the Responses shape +/// cannot express without changing the reply: `n` above 1, a legacy +/// `function` message or `function_call`, a tool that is not a function, a +/// content part that is neither text nor an image, a tool call or tool +/// result missing its id, or messages that are all system or developer. +pub fn to_responses_request( + req: &ChatRequest, + upstream_model: &str, + policy: RequestPolicy, +) -> Result { + if req + .rest + .get("n") + .and_then(Value::as_u64) + .is_some_and(|n| n > 1) + { + return Err("n above 1 has no Responses equivalent"); + } + let (instructions, input) = translate_messages(&req.messages)?; + if input.is_empty() { + return Err("a Responses request needs a user, assistant or tool message"); + } + + let mut body = Map::new(); + body.insert("model".into(), Value::String(upstream_model.to_owned())); + if !instructions.is_empty() { + body.insert( + "instructions".into(), + Value::String(instructions.join("\n\n")), + ); + } + body.insert("input".into(), Value::Array(input)); + body.insert("stream".into(), Value::Bool(true)); + body.insert("store".into(), Value::Bool(false)); + + if let Some(tools) = req.rest.get("tools").and_then(Value::as_array) + && !tools.is_empty() + { + let tools = tools + .iter() + .map(translate_tool) + .collect::, _>>()?; + body.insert("tools".into(), Value::Array(tools)); + } + if let Some(choice) = req.rest.get("tool_choice") { + body.insert("tool_choice".into(), translate_tool_choice(choice)?); + } + if let Some(parallel) = req.rest.get("parallel_tool_calls") { + body.insert("parallel_tool_calls".into(), parallel.clone()); + } + if let Some(effort) = req.rest.get("reasoning_effort").and_then(Value::as_str) { + body.insert("reasoning".into(), json!({ "effort": effort })); + } + if let Some(format) = req.rest.get("response_format") { + body.insert( + "text".into(), + json!({ "format": translate_response_format(format)? }), + ); + } + if policy == RequestPolicy::Standard { + for key in ["temperature", "top_p"] { + if let Some(value) = req.rest.get(key) { + body.insert(key.into(), value.clone()); + } + } + let limit = req + .rest + .get("max_completion_tokens") + .or_else(|| req.rest.get("max_tokens")); + if let Some(limit) = limit { + body.insert("max_output_tokens".into(), limit.clone()); + } + } + Ok(Value::Object(body)) +} + +/// Splits chat messages into the Responses `instructions` (the leading run +/// of system and developer messages) and the `input` items. +fn translate_messages(messages: &[Value]) -> Result<(Vec, Vec), &'static str> { + let mut instructions = Vec::new(); + let mut input = Vec::new(); + let mut leading = true; + for message in messages { + let role = message.get("role").and_then(Value::as_str).unwrap_or(""); + let content = message.get("content").unwrap_or(&Value::Null); + match role { + "system" | "developer" => { + let text = joined_text(content)?; + if leading { + instructions.push(text); + } else { + input.push(json!({ "type": "message", "role": "developer", "content": text })); + } + } + "user" => { + leading = false; + let content = user_content(content)?; + input.push(json!({ "type": "message", "role": "user", "content": content })); + } + "assistant" => { + leading = false; + if message.get("function_call").is_some() { + return Err("legacy function_call messages have no Responses equivalent"); + } + let text = joined_text(content)?; + if !text.is_empty() { + input.push(json!({ "type": "message", "role": "assistant", "content": text })); + } + if let Some(calls) = message.get("tool_calls").and_then(Value::as_array) { + for call in calls { + input.push(function_call_item(call)?); + } + } + } + "tool" => { + leading = false; + let call_id = message + .get("tool_call_id") + .and_then(Value::as_str) + .ok_or("a tool message has no tool_call_id")?; + input.push(json!({ + "type": "function_call_output", + "call_id": call_id, + "output": joined_text(content)?, + })); + } + _ => return Err("legacy function messages have no Responses equivalent"), + } + } + Ok((instructions, input)) +} + +/// The text of a message's content: a string as is, a parts array as its +/// text parts joined, null as empty. Any non-text part is refused, because +/// only user messages may carry images. +fn joined_text(content: &Value) -> Result { + match content { + Value::Null => Ok(String::new()), + Value::String(text) => Ok(text.clone()), + Value::Array(parts) => { + let mut text = String::new(); + for part in parts { + match part.get("type").and_then(Value::as_str) { + Some("text" | "refusal") => { + let piece = part + .get("text") + .or_else(|| part.get("refusal")) + .and_then(Value::as_str) + .unwrap_or(""); + text.push_str(piece); + } + _ => return Err("only user messages may hold non-text content parts"), + } + } + Ok(text) + } + _ => Err("message content must be a string, an array of parts, or null"), + } +} + +/// A user message's content: a string as is, or parts mapped to +/// `input_text` and `input_image`. +fn user_content(content: &Value) -> Result { + let Value::Array(parts) = content else { + return Ok(Value::String(joined_text(content)?)); + }; + let mut mapped = Vec::with_capacity(parts.len()); + for part in parts { + match part.get("type").and_then(Value::as_str) { + Some("text") => { + let text = part.get("text").and_then(Value::as_str).unwrap_or(""); + mapped.push(json!({ "type": "input_text", "text": text })); + } + Some("image_url") => { + let image = part.get("image_url").unwrap_or(&Value::Null); + let url = image + .get("url") + .or(Some(image)) + .and_then(Value::as_str) + .ok_or("an image_url part has no url")?; + let detail = image + .get("detail") + .and_then(Value::as_str) + .unwrap_or("auto"); + mapped.push(json!({ "type": "input_image", "image_url": url, "detail": detail })); + } + _ => return Err("only text and image_url content parts have a Responses equivalent"), + } + } + Ok(Value::Array(mapped)) +} + +/// One chat `tool_calls` entry as a Responses `function_call` item. +fn function_call_item(call: &Value) -> Result { + let call_id = call + .get("id") + .and_then(Value::as_str) + .ok_or("a tool call has no id")?; + let function = call.get("function").ok_or("a tool call has no function")?; + let name = function + .get("name") + .and_then(Value::as_str) + .ok_or("a tool call has no function name")?; + let arguments = match function.get("arguments") { + Some(Value::String(arguments)) => arguments.clone(), + None | Some(Value::Null) => "{}".to_owned(), + Some(other) => other.to_string(), + }; + Ok(json!({ "type": "function_call", "call_id": call_id, "name": name, "arguments": arguments })) +} + +/// A chat `{type: function, function: {...}}` tool as a flat Responses +/// function tool. `strict` defaults to false, because a chat client's +/// schema need not meet strict mode's rules. +fn translate_tool(tool: &Value) -> Result { + if tool.get("type").and_then(Value::as_str) != Some("function") { + return Err("only function tools have a Responses equivalent here"); + } + let function = tool + .get("function") + .ok_or("a function tool has no function")?; + let name = function + .get("name") + .and_then(Value::as_str) + .ok_or("a function tool has no name")?; + let mut flat = Map::new(); + flat.insert("type".into(), Value::String("function".into())); + flat.insert("name".into(), Value::String(name.to_owned())); + if let Some(description) = function.get("description") { + flat.insert("description".into(), description.clone()); + } + let parameters = function + .get("parameters") + .cloned() + .unwrap_or_else(|| json!({ "type": "object", "properties": {} })); + flat.insert("parameters".into(), parameters); + let strict = function + .get("strict") + .and_then(Value::as_bool) + .unwrap_or(false); + flat.insert("strict".into(), Value::Bool(strict)); + Ok(Value::Object(flat)) +} + +/// `tool_choice`: the string modes pass through; a named function loses +/// the chat `function` wrapper. +fn translate_tool_choice(choice: &Value) -> Result { + match choice { + Value::String(_) => Ok(choice.clone()), + Value::Object(_) => { + let name = choice + .get("function") + .and_then(|function| function.get("name")) + .and_then(Value::as_str) + .ok_or("tool_choice names no function")?; + Ok(json!({ "type": "function", "name": name })) + } + _ => Err("tool_choice must be a string or a named function"), + } +} + +/// `response_format` as a Responses `text.format`: `json_schema` loses its +/// wrapper object; `text` and `json_object` pass through. +fn translate_response_format(format: &Value) -> Result { + match format.get("type").and_then(Value::as_str) { + Some("text" | "json_object") => Ok(format.clone()), + Some("json_schema") => { + let schema = format + .get("json_schema") + .and_then(Value::as_object) + .ok_or("a json_schema response_format has no json_schema")?; + let mut flat = schema.clone(); + flat.insert("type".into(), Value::String("json_schema".into())); + Ok(Value::Object(flat)) + } + _ => Err("response_format must be text, json_object or json_schema"), + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/gateway/protocol/src/wire/responses/tests.rs b/crates/gateway/protocol/src/wire/responses/tests.rs new file mode 100644 index 00000000..efe6d00f --- /dev/null +++ b/crates/gateway/protocol/src/wire/responses/tests.rs @@ -0,0 +1,322 @@ +//! Tests for the chat completions to Responses request translation. + +use serde_json::{Map, Value, json}; + +use super::*; + +fn chat(body: Value) -> ChatRequest { + serde_json::from_value(body).expect("a chat request") +} + +fn translate(body: Value, policy: RequestPolicy) -> Value { + to_responses_request(&chat(body), "upstream-model", policy).expect("translates") +} + +#[test] +fn every_request_streams_unstored_to_the_upstream_model() { + let out = translate( + json!({ "model": "caller", "messages": [{ "role": "user", "content": "hi" }] }), + RequestPolicy::Standard, + ); + assert_eq!(out["model"], "upstream-model"); + assert_eq!(out["stream"], true); + assert_eq!(out["store"], false); + assert_eq!( + out["input"], + json!([{ "type": "message", "role": "user", "content": "hi" }]) + ); + assert!(out.get("instructions").is_none()); +} + +#[test] +fn leading_system_messages_become_instructions_and_later_ones_developer_items() { + let out = translate( + json!({ "model": "m", "messages": [ + { "role": "system", "content": "Be brief." }, + { "role": "developer", "content": [{ "type": "text", "text": "Cite." }] }, + { "role": "user", "content": "q" }, + { "role": "system", "content": "Now in French." }, + ]}), + RequestPolicy::Standard, + ); + assert_eq!(out["instructions"], "Be brief.\n\nCite."); + assert_eq!( + out["input"][1], + json!({ "type": "message", "role": "developer", "content": "Now in French." }) + ); +} + +#[test] +fn a_tool_round_maps_to_function_call_items() { + // The shape the harness sends after a tool round: an assistant turn with + // tool_calls and null content, then the tool's result. + let out = translate( + json!({ "model": "m", "messages": [ + { "role": "user", "content": "weather?" }, + { "role": "assistant", "content": null, "tool_calls": [ + { "id": "call_1", "type": "function", + "function": { "name": "weather", "arguments": "{\"city\":\"Siem Reap\"}" } } + ]}, + { "role": "tool", "tool_call_id": "call_1", "content": "31C" }, + ]}), + RequestPolicy::Standard, + ); + assert_eq!( + out["input"], + json!([ + { "type": "message", "role": "user", "content": "weather?" }, + { "type": "function_call", "call_id": "call_1", "name": "weather", + "arguments": "{\"city\":\"Siem Reap\"}" }, + { "type": "function_call_output", "call_id": "call_1", "output": "31C" }, + ]) + ); +} + +#[test] +fn assistant_text_with_tool_calls_keeps_both_in_order() { + let out = translate( + json!({ "model": "m", "messages": [ + { "role": "user", "content": "q" }, + { "role": "assistant", "content": "Checking.", "tool_calls": [ + { "id": "c", "type": "function", "function": { "name": "f", "arguments": {} } } + ]}, + { "role": "tool", "tool_call_id": "c", "content": [{ "type": "text", "text": "ok" }] }, + ]}), + RequestPolicy::Standard, + ); + assert_eq!( + out["input"][1], + json!({ "type": "message", "role": "assistant", "content": "Checking." }) + ); + assert_eq!(out["input"][2]["arguments"], "{}"); + assert_eq!(out["input"][3]["output"], "ok"); +} + +#[test] +fn user_parts_map_to_input_text_and_input_image() { + let out = translate( + json!({ "model": "m", "messages": [{ "role": "user", "content": [ + { "type": "text", "text": "What is this?" }, + { "type": "image_url", "image_url": { "url": "data:image/png;base64,AAAA", "detail": "low" } }, + { "type": "image_url", "image_url": { "url": "https://example.test/a.png" } }, + ]}]}), + RequestPolicy::Plan, + ); + assert_eq!( + out["input"][0]["content"], + json!([ + { "type": "input_text", "text": "What is this?" }, + { "type": "input_image", "image_url": "data:image/png;base64,AAAA", "detail": "low" }, + { "type": "input_image", "image_url": "https://example.test/a.png", "detail": "auto" }, + ]) + ); +} + +#[test] +fn harness_tools_flatten_and_default_to_non_strict() { + // The harness's request body: function tools in the chat wrapper, + // tool_choice auto, include_usage, and llama.cpp's template kwargs. + let out = translate( + json!({ + "model": "m", + "messages": [{ "role": "user", "content": "hi" }], + "stream": true, + "stream_options": { "include_usage": true }, + "tools": [{ "type": "function", "function": { + "name": "read_file", "description": "Read a file.", + "parameters": { "type": "object", "properties": { "path": { "type": "string" } } } + }}], + "tool_choice": "auto", + "chat_template_kwargs": { "enable_thinking": true }, + }), + RequestPolicy::Standard, + ); + assert_eq!( + out["tools"], + json!([{ "type": "function", "name": "read_file", "description": "Read a file.", + "parameters": { "type": "object", "properties": { "path": { "type": "string" } } }, + "strict": false }]) + ); + assert_eq!(out["tool_choice"], "auto"); + for dropped in ["stream_options", "chat_template_kwargs", "messages"] { + assert!( + out.get(dropped).is_none(), + "{dropped} must not reach the upstream" + ); + } +} + +#[test] +fn named_tool_choice_reasoning_and_json_schema_translate() { + let out = translate( + json!({ + "model": "m", + "messages": [{ "role": "user", "content": "hi" }], + "tool_choice": { "type": "function", "function": { "name": "f" } }, + "reasoning_effort": "low", + "response_format": { "type": "json_schema", "json_schema": { + "name": "answer", "strict": true, "schema": { "type": "object" } + }}, + }), + RequestPolicy::Plan, + ); + assert_eq!( + out["tool_choice"], + json!({ "type": "function", "name": "f" }) + ); + assert_eq!(out["reasoning"], json!({ "effort": "low" })); + assert_eq!( + out["text"], + json!({ "format": { "type": "json_schema", "name": "answer", "strict": true, + "schema": { "type": "object" } } }) + ); +} + +#[test] +fn standard_policy_passes_sampling_and_limits_and_plan_policy_drops_them() { + let body = json!({ + "model": "m", + "messages": [{ "role": "user", "content": "hi" }], + "temperature": 0.2, + "top_p": 0.9, + "max_tokens": 64, + }); + let standard = translate(body.clone(), RequestPolicy::Standard); + assert_eq!(standard["temperature"], 0.2); + assert_eq!(standard["top_p"], 0.9); + assert_eq!(standard["max_output_tokens"], 64); + + let plan = translate(body, RequestPolicy::Plan); + for refused in ["temperature", "top_p", "max_output_tokens", "max_tokens"] { + assert!( + plan.get(refused).is_none(), + "{refused} must not reach a plan endpoint" + ); + } +} + +#[test] +fn max_completion_tokens_wins_over_max_tokens() { + let out = translate( + json!({ "model": "m", "messages": [{ "role": "user", "content": "hi" }], + "max_tokens": 10, "max_completion_tokens": 20 }), + RequestPolicy::Standard, + ); + assert_eq!(out["max_output_tokens"], 20); +} + +#[test] +fn requests_with_no_responses_equivalent_are_refused() { + let user = json!({ "role": "user", "content": "hi" }); + let cases = [ + json!({ "model": "m", "messages": [user], "n": 2 }), + json!({ "model": "m", "messages": [{ "role": "function", "name": "f", "content": "x" }] }), + json!({ "model": "m", "messages": [user, { "role": "assistant", "content": null, + "function_call": { "name": "f", "arguments": "{}" } }] }), + json!({ "model": "m", "messages": [user], "tools": [{ "type": "code_interpreter" }] }), + json!({ "model": "m", "messages": [{ "role": "user", "content": [ + { "type": "input_audio", "input_audio": { "data": "AA", "format": "wav" } }] }] }), + json!({ "model": "m", "messages": [user, { "role": "tool", "content": "x" }] }), + json!({ "model": "m", "messages": [{ "role": "system", "content": "only rules" }] }), + json!({ "model": "m", "messages": [{ "role": "system", "content": [ + { "type": "image_url", "image_url": { "url": "x" } }] }, user] }), + ]; + for case in cases { + let request = chat(case.clone()); + assert!( + to_responses_request(&request, "u", RequestPolicy::Standard).is_err(), + "expected a refusal for {case}" + ); + } +} + +#[test] +fn n_of_one_is_accepted() { + let out = translate( + json!({ "model": "m", "messages": [{ "role": "user", "content": "hi" }], "n": 1 }), + RequestPolicy::Standard, + ); + assert!(out.get("n").is_none()); + let _: &Map = out.as_object().expect("an object"); +} + +/// A conversation as a Harness sends it: each request is the one before +/// plus new messages. The tool-call arguments are deliberately untidy. +fn growing_conversation() -> Vec { + let untidy = "{ \"b\":1,\n \"a\" : [2, 3] }"; + vec![ + json!({ "role": "system", "content": "Be brief." }), + json!({ "role": "user", "content": "Compare the two." }), + json!({ "role": "assistant", "content": "Looking both up.", "tool_calls": [ + { "id": "call_1", "type": "function", "function": { "name": "look_up", "arguments": untidy } }, + { "id": "call_2", "type": "function", "function": { "name": "look_up", "arguments": "{\"a\":4}" } }, + ]}), + json!({ "role": "tool", "tool_call_id": "call_1", "content": "one" }), + json!({ "role": "tool", "tool_call_id": "call_2", "content": "two" }), + json!({ "role": "assistant", "content": "The first is smaller." }), + json!({ "role": "system", "content": "Now in French." }), + json!({ "role": "user", "content": "And the third?" }), + json!({ "role": "assistant", "content": null, "tool_calls": [ + { "id": "call_3", "type": "function", "function": { "name": "look_up", "arguments": "{}" } }, + ]}), + json!({ "role": "tool", "tool_call_id": "call_3", "content": "three" }), + json!({ "role": "assistant", "content": "Le troisième est le plus grand." }), + ] +} + +/// A provider caches a request by its exact prefix. So a history that +/// extends another must translate to a request that extends the other's: +/// the same instructions and tools, and an `input` that only grows at the +/// end. Otherwise every round would miss the cache from the first changed +/// item on. +#[test] +fn a_longer_history_translates_to_an_extension_of_the_shorter_one() { + let conversation = growing_conversation(); + let tools = json!([{ "type": "function", "function": { + "name": "look_up", "parameters": { "type": "object", "properties": {} } + }}]); + for policy in [RequestPolicy::Standard, RequestPolicy::Plan] { + let request = |length: usize| { + translate( + json!({ "model": "m", "tools": tools, "messages": conversation[..length] }), + policy, + ) + }; + // Every history from the first user message on. + for length in 2..conversation.len() { + let (shorter, longer) = (request(length), request(length + 1)); + let (before, after) = ( + shorter["input"].as_array().expect("input"), + longer["input"].as_array().expect("input"), + ); + assert!(after.len() >= before.len(), "history of {length}"); + assert_eq!(&after[..before.len()], &before[..], "history of {length}"); + assert_eq!(shorter["instructions"], longer["instructions"]); + assert_eq!(shorter["tools"], longer["tools"]); + } + // The same request translates to the same bytes every time. + let whole = conversation.len(); + assert_eq!(request(whole).to_string(), request(whole).to_string()); + } +} + +/// Tool-call arguments go back to the provider exactly as it wrote them: +/// re-rendering the JSON would change the prefix it cached. +#[test] +fn tool_call_arguments_are_passed_through_byte_for_byte() { + let out = translate( + json!({ "model": "m", "messages": growing_conversation() }), + RequestPolicy::Standard, + ); + let arguments: Vec<&str> = out["input"] + .as_array() + .expect("input") + .iter() + .filter(|item| item["type"] == "function_call") + .filter_map(|item| item["arguments"].as_str()) + .collect(); + assert_eq!( + arguments, + ["{ \"b\":1,\n \"a\" : [2, 3] }", "{\"a\":4}", "{}"] + ); +} diff --git a/crates/gateway/routing/src/lib.rs b/crates/gateway/routing/src/lib.rs index c9660a49..dfd33e1a 100644 --- a/crates/gateway/routing/src/lib.rs +++ b/crates/gateway/routing/src/lib.rs @@ -15,5 +15,5 @@ mod model; pub mod queue; -pub use crate::model::{Endpoint, GEMMA3_TOOL_CODE, Model}; +pub use crate::model::{CredentialSource, Endpoint, GEMMA3_TOOL_CODE, Model}; pub use crate::queue::dominion_queues; diff --git a/crates/gateway/routing/src/model.rs b/crates/gateway/routing/src/model.rs index ff899daa..4716ce75 100644 --- a/crates/gateway/routing/src/model.rs +++ b/crates/gateway/routing/src/model.rs @@ -17,10 +17,24 @@ use crate::queue::DominionQueue; /// dialect emulation matches on it, so both sides name one constant. pub const GEMMA3_TOOL_CODE: &str = "gemma3_tool_code"; +/// Where an endpoint's credential comes from. A request is authorized by +/// this, never by a model's or an endpoint's name. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CredentialSource { + /// The endpoint's own configuration, or none for a local model. + Configured, + /// A user's ChatGPT sign-in, held by the gateway. The catalog entry's + /// `upstream` spends nothing; the gateway hands the one that does only + /// to a request it has authorized. + ChatgptPlan, +} + /// One backend endpoint plus the upstream that talks to it. pub struct Endpoint { /// The endpoint's configured id. pub id: String, + /// Where this endpoint's credential comes from. + pub source: CredentialSource, /// The upstream implementation forwarding to this backend. pub upstream: Arc, /// Admission control: concurrency limit plus bounded waiting queue. @@ -34,6 +48,7 @@ impl fmt::Debug for Endpoint { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Endpoint") .field("id", &self.id) + .field("source", &self.source) .field("queue", &self.queue) .finish_non_exhaustive() } @@ -62,3 +77,27 @@ pub struct Model { /// The endpoint serving this model: the first configured one. pub endpoint: Arc, } + +impl Model { + /// This model, sent through `upstream` in place of its endpoint's own. + /// Everything else, the endpoint's queue included, is unchanged. + #[must_use] + pub fn with_upstream(&self, upstream: Arc) -> Model { + Model { + name: self.name.clone(), + kind: self.kind, + description: self.description.clone(), + context: self.context, + thinking: self.thinking, + capabilities: self.capabilities.clone(), + tool_dialect: self.tool_dialect.clone(), + upstream_name: self.upstream_name.clone(), + endpoint: Arc::new(Endpoint { + id: self.endpoint.id.clone(), + source: self.endpoint.source, + upstream, + queue: self.endpoint.queue.clone(), + }), + } + } +} diff --git a/vibe/2026-11-14-1-sign-in-with-chatgpt.md b/vibe/2026-11-14-1-sign-in-with-chatgpt.md new file mode 100644 index 00000000..488e5230 --- /dev/null +++ b/vibe/2026-11-14-1-sign-in-with-chatgpt.md @@ -0,0 +1,243 @@ +--- +name: Sign in with ChatGPT +overview: The Gateway signs a person in to their ChatGPT account through OpenAI's documented flow for open-source tools, keeps the credential to itself, and serves the plan's models to Runvil's own client and to nothing else. Config saves gain a revision check, because the Gateway now edits `gateway.toml` itself. +todos: + - id: responses-upstream + content: "gateway-protocol: the Responses request and stream translation, the bearer source, and a client that follows no redirect" + status: completed + - id: private-proofs + content: "gateway-api-discovery: `gateway-private.json`, the owner proof and the plan proof of one boot" + status: completed + - id: chatgpt-plan-crate + content: "gateway-chatgpt-plan: callback, ID-token checks, OAuth calls, the broker, the owner-only record, and a fake issuer for tests" + status: completed + - id: config-revision + content: "gateway, gateway-config, config UI: a whole-document config save names the revision it was read from" + status: completed + - id: plan-routes-and-gate + content: "gateway, gateway-config, gateway-routing, config UI: `auth = \"chatgpt-plan\"`, the `/admin/chatgpt` routes, the plan gate, and model setup" + status: completed + - id: verify + content: "run every CI check on each commit" + status: completed +isProject: false +--- + +# Sign in with ChatGPT + + + +## Product Requirements + +- Problem and users: + - A person with an eligible ChatGPT account wants to use the models their plan includes from Workshop, without an API key. + - OpenAI documents a flow for open-source tools: . The person authorizes one registered tool. The tool gets an access bearer for `POST /v1/responses` and `GET /v1/models`, a refresh token for the issuer's OAuth endpoints, and an ID token that names the account. + - The Gateway already holds every other provider credential. Workshop and the Harness hold none. +- Goals: + - The whole flow stays in the Gateway process: registration, sign-in, ID-token checks, the saved refresh token, refresh, and revocation. + - An endpoint with `auth = "chatgpt-plan"` serves its models only to a caller that passes ordinary authentication and presents `X-Runvil-Plan-Proof`, on a Gateway that listens on loopback. + - The `/admin/chatgpt` routes answer only to a loopback caller that passes ordinary authentication and presents `X-Runvil-Owner-Proof`. + - A whole-document config save names the revision it was read from, so it cannot overwrite a file the Gateway or another page changed since. +- Non-goals: + - Serving a plan to any client other than Runvil's own. OpenAI's Sign in with ChatGPT Terms restrict a plan to the application its user connected, in section 3.1: "Use the user's plan only for the application they connected. Do not provide general-purpose API access for other tools or unrelated requests." The plan proof is how this Gateway keeps to that. + - A plan endpoint with its own `base_url` or `api_key`. + - A sign-in on a Gateway that listens on any address other than loopback. + - A saved credential on Windows. The store refuses to save there until an owner-only file is enforced. + - The owner proof on the admin routes that existed before this change. +- Success criteria: + - A person signs in on macOS or Linux and runs a plan model from Workshop. + - A request for a plan model without the plan proof is refused with `403 chatgpt_plan_runvil_only`. `GET /v1/models` omits plan rows for that caller and still answers `200`. Models that are not plan models are unaffected. + - A config save with no revision is refused with `428 config_revision_required`, and one with a stale revision with `409 config_conflict`. + - Every CI check passes. +- Constraints: + - The flow follows OpenAI's pages. Each module names the page and section it follows. + - Registration and sign-in: + - Token reference: + - Errors and recovery: + - Models and inference: + - The Sign in with ChatGPT Terms, , effective 4 November 2026. Section 1 requires that a saved token is local and under the user's control. Section 3.1 requires that requests come from the user's own runtime and that the plan serves only the application the user connected. + - `crates/gateway/README.md`: a standard route keeps its standard shape, and every addition is named as an extension and is opt-in. +- Open questions: None + +## Functional Specification + +- Inputs and outputs: + - `[[endpoint]]` accepts `protocol = "openai-responses"` and `auth = "chatgpt-plan"`. The second requires the first and allows neither `base_url` nor `api_key`. + - `/gateway-private.json` holds `pid`, `epoch`, `plan_proof`, and `owner_proof`. Each proof is 64 lowercase hex characters, minted at start. `pid` and `epoch` match `gateway.json`. On Unix the file has mode `0600`. + - `POST /admin/chatgpt/sign-in` starts a one-shot callback listener on `127.0.0.1` and returns `{ authorize_url }`. A second call replaces the first. + - `GET /admin/chatgpt/status` returns `status`, `unreadable`, `email`, `plan_granted`, `revocation`, `cleanup_unconfirmed`, `sign_in_pending`, `available`, `unavailable_code`, `unavailable_reason`, and `plan_models`. It never returns a token. + - `POST /admin/chatgpt/sign-out` signs out locally, then revokes at OpenAI. `POST /admin/chatgpt/forget` also drops the registration. + - `POST /admin/chatgpt/reset` deletes the saved record, readable or not, and returns `204`. + - `POST /admin/chatgpt/models/enable` and `POST /admin/chatgpt/models/forget` add the plan's endpoint and models to `gateway.toml` and to live routing, and remove them. + - `GET /admin/config` returns the revision in `ETag` and `x-config-revision` when the configuration came from a file. `GET /admin/config-pending` returns it in both headers and in a `revision` field. `PUT /admin/config` takes it as `revision` in the body, as `x-config-revision`, or as `If-Match`. + - `RUNVIL_CHATGPT_AGENT_NAME` changes the name the consent screen shows. It takes 1 to 64 characters after trimming: ASCII letters and digits, the space, and `-_.()`. Any other value is ignored and the name is `Runvil`. +- States and validation: + - A sign-in is signed out, active, or in need of a new sign-in, or its record is unreadable. + - An ID token is verified as RS256 against the issuer's JWKS, and then `iss`, `aud`, `exp` with 60 seconds of skew, `nonce`, `at_hash`, and `sub` are checked in that order. + - A plan request goes to a compiled destination, always streams, and never asks the provider to store the response. + - Whether the Gateway listens on loopback is decided once, from the bound address: `127.0.0.0/8` or `::1`. + - A Gateway removes its proofs file when it shuts down. One that shuts down to restart keeps its discovery file for its supervisor and still removes its proofs file. + - An access bearer is cached and counts as usable until 120 seconds before it expires. + - The status reports a sign-in as pending from the start of an attempt until that attempt's own work is finished: the exchange, the activation, and the revocation of a token it will not keep. A sign-out, a reset, or a newer attempt ends the pending state at once. An attempt whose callback never arrives stops being pending when it expires. A client that polls can therefore stop at the first status that is not pending. + - A warning that a token could not be revoked can be set after a sign-out or a cancelled sign-in has returned. A client sees it at its next status read. Nothing notifies it. +- Errors and recovery: + - A request that names a plan model without the plan proof: `403 chatgpt_plan_runvil_only`. On a Gateway that does not listen on loopback the refusal is `chatgpt_plan_loopback_only` and comes first. + - An `/admin/chatgpt` request without the owner proof, after it passed the loopback wall and ordinary authentication: `403 owner_proof_required`. + - A plan model with no usable sign-in: `403` with `chatgpt_sign_in_required`, `chatgpt_sign_in_expired`, or `chatgpt_plan_not_granted`, or `503 chatgpt_unavailable`. + - A config save with no revision: `428 config_revision_required`. A stale one: `409 config_conflict`. A malformed one, or two carriers that disagree: `422`. Model setup while a draft is staged: `409 config_has_pending_changes`. + - `reset` clears the current cleanup warning. A revocation that was still pending can fail later and raise the warning again. +- Security and privacy behavior: + - Neither proof replaces ordinary authentication. A proof header that is sent more than once counts as absent. + - The routing table's entry for a plan model holds an upstream that refuses. The upstream that spends the plan is handed out only by `ChatGpt::authorize_plan`, after it checks the listener mode, the exact proof, the broker, and the sign-in's state. + - The upstream adapters build each outbound request themselves, so an incoming proof header is not forwarded. + - The callback page is a fixed sentence. Nothing from the request is written to it or to a log. + - `Debug` redacts both proofs. On the private file it prints `[REDACTED]` in their place. + + + + +## Technical Design + +- Architecture: + - `gateway-protocol`: `wire/responses.rs` rewrites a chat completion as a Responses request. `upstream/responses/` translates the Responses stream back to chat chunks. `upstream/bearer.rs` is the `BearerSource` an upstream asks for its credential. `http_util` gains `hardened_client` and `no_redirect_streaming_client`, which never fall back to a client that follows redirects. + - `gateway-config`: the vocabulary gains `Protocol::OpenaiResponses` and the `chatgpt-plan` auth, and validation enforces what a plan endpoint may hold. + - `gateway-api-discovery`: `GatewayPrivateFile`, written beside `gateway.json` through the same owner-only writer. + - `gateway-chatgpt-plan`: `callback`, `id_token`, `oauth`, `plan` (the broker), `store`, and a `fake_issuer` that exists in tests and behind the `test-fixtures` feature. + - `gateway`: one `ChatGpt` handle lives across config reloads. `auth/owner.rs` is the `OwnerCaller` extractor. `admin/walled/chatgpt.rs` and `admin/walled/chatgpt/` are the routes. `chatgpt-models.rs` fetches the plan's model list. + - Config UI: a plan endpoint shows no `base_url` or `api_key`, a cloud merge skips it, and a save sends the revision. +- Data model: + - `/chatgpt-plan/chatgpt-plan.json`: a version, the agent host id, the client id, the account's subject and email, the session with its refresh token and scopes, and a flag for cleanup that was not confirmed. It is written through a fresh `0600` temp file and renamed, and its directory must be private to the user. +- Interfaces and APIs: + - Headers: `X-Runvil-Plan-Proof`, `X-Runvil-Owner-Proof`, `x-config-revision`, `ETag`, `If-Match`. + - Routes: the seven under `/admin/chatgpt/` listed above. +- Failure handling: + - A hardened client that cannot be built is kept as `None`, and each request then fails closed. + - `resolve_routed_model` applies the plan gate for chat, embeddings, rerank, and speech, before the kind check. + - Model enable captures a fence on the sign-in and commits only if the fence still holds. Sign-out and model forget supersede an enable that is in flight. A revocation that is still pending is tracked by operation id. +- Observability: + - A refresh logs one info line with no token. +- Performance and scale: + - Routing is read under the existing async lock, as before. Nothing here is lock-free. + + + + +## Testing Plan + +- Unit: + - `gateway-protocol`: the request mapping, the stream translation including tool-call arguments, and the refusal when the no-redirect client is absent. + - `gateway-chatgpt-plan`: the callback's fixed page, each ID-token rejection in order, PKCE, refresh rotation and revocation against the fake issuer, the fences under races, and the store's refusal rules. + - `gateway-api-discovery`: the private file's mode, its handling of a corrupt file, and the redaction. + - `gateway`: the gate across ordinary authentication, both proofs, and both listener modes, on chat, the model list, embeddings, rerank, and speech; the `/admin/chatgpt` routes; the enable and forget races; the `428` and `409` on saves. + - Config UI: the settings page, the cloud merge, and the config store. +- Controls: 71 hand-written mutations, each of one named rule and each killed by a named test. The rules are in the gate, the owner routes, the ID-token checks, the fences, the store, the config revision, and the Responses translation. They cover the mutations they name, not every branch. The tests of a client that cannot be built inject the kept `None`, not a failing builder. +- Exit criteria: + - `cargo fmt --all --check` + - `cargo clippy --locked --workspace --all-targets --all-features` + - `cargo nextest run --locked --workspace --all-features` + - `cargo check --locked -p gateway --no-default-features` + - `cargo doc --locked --workspace --no-deps --all-features` with `RUSTDOCFLAGS="-D warnings"` + - `cargo xtask tidy` + - `cargo deny check` + - `npm run typecheck` and `npm test` in `crates/gateway/config-ui/ui` + + + + +## Decision Record + +- Decisions: + - The credential stays in the Gateway. It already holds every provider credential, and a client that never has the token cannot leak it. + - A plan model needs a proof on top of ordinary authentication. Loopback alone would serve the plan to every local program, which the terms' section 3.1 forbids. The proof is minted per boot and never written to the public discovery file. + - Two proofs, not one. The plan proof lets a client use the models. The owner proof lets a caller manage the credential. A client that may use the models need not be able to manage the credential. + - Config saves carry a revision. Model setup makes the Gateway a second writer of `gateway.toml`, beside the settings page. The check covers writers that go through the Gateway. It is not a lock against another program that edits the file. + - The Responses protocol is its own endpoint protocol with a bearer source, not a special case inside chat completions. +- Reuse considered: + - The private file uses the discovery file's owner-only writer. The routes use the existing loopback wall and authentication. + +### Deferred and Out of Scope + +- Deferred: the owner proof on the older admin routes (`PUT /admin/config`, `/admin/env`, `/admin/config-apply`). +- Deferred: a typed permit around plan routing. Today the refusing table entry and the runtime gate enforce it. +- Deferred: a saved credential on Windows. +- Deferred: removing `gateway-private.json` at shutdown by `epoch` as well as `pid`, and a route that forces a refresh. +- Out of scope: Windows was not run at all. No route forces a token refresh, so the failure cases of refresh are tested against the fake issuer only. + + + + +## Execution Instructions + + + +### Step 1: The Responses upstream [completed] + +- Component: `responses-upstream` +- Tests: + - `cargo nextest run --locked -p gateway-protocol --all-features` + +Add the request mapping, the stream translation, `BearerSource`, and the two clients that follow no redirect. + +Commit: one commit. Subject: `Add a Responses upstream and a bearer source to gateway-protocol`. + + + + + +### Step 2: The private proofs file [completed] + +- Component: `private-proofs` +- Tests: + - `cargo nextest run --locked -p gateway-api-discovery --all-features` + +Add `GatewayPrivateFile` beside the discovery file. + +Commit: one commit. Subject: `Add the private file for a boot's owner and plan proofs`. + + + + + +### Step 3: The gateway-chatgpt-plan crate [completed] + +- Component: `chatgpt-plan-crate` +- Tests: + - `cargo nextest run --locked -p gateway-chatgpt-plan --all-features` + +Add the crate. Nothing depends on it yet. + +Commit: one commit. Subject: `Add the gateway-chatgpt-plan crate`. + + + + + +### Step 4: A revision on config saves [completed] + +- Component: `config-revision` +- Tests: + - `cargo nextest run --locked -p gateway -p gateway-config --all-features` + - `npm test` in `crates/gateway/config-ui/ui` + +`GET /admin/config` returns the revision, and `PUT /admin/config` refuses a save that names none or a stale one. The settings page sends it. This comes before the plan, because the plan's model setup is a second writer of `gateway.toml`. + +Commit: one commit. Subject: `Require a revision on whole-document config saves`. + + + + + +### Step 5: Routes, the gate, and model setup [completed] + +- Component: `plan-routes-and-gate` +- Tests: + - `cargo nextest run --locked --workspace --all-features` + - `npm test` in `crates/gateway/config-ui/ui` + +Add the config vocabulary, the routes, the gate, model setup, the settings page changes, and the README section. Model setup changes the config under the apply lock, and the revision check of step 4 stops a settings page that was open from overwriting that result with a later save. + +Commit: one commit. Subject: `Serve ChatGPT plan models to Runvil's own client`. + + + +