Compare commits

...
Author SHA1 Message Date
RCD f0241c3e6b feat(rust): add Claude upstream auth header shaping
- Add provider-specific auth header hook to Provider trait
- Use x-api-key + anthropic-version for Claude upstream requests
- Keep bearer auth for Codex/default provider path
- Strengthen active-Claude routing test to assert Anthropic headers
- Keep Rust suite green (53/53)
2026-04-08 01:47:50 +07:00
RCD d8b1ade0ea test(rust): exercise active Claude provider path
- Add active-Claude proxy/health/runtime test
- Add Claude account/state tests for manual provider path
- Keep Rust suite green at 53/53
2026-04-08 01:46:21 +07:00
RCD 0b04915694 feat(rust): scaffold Claude provider slice
- Add claude config section and ClaudeProvider skeleton
- Register Claude in provider registry without changing default runtime provider
- Add manual Claude account CLI path and provider-generic manual account helper
- Add targeted Claude account/state tests
- Keep Rust suite green (52/52)
2026-04-08 01:41:59 +07:00
RCD 40cb8329e6 refactor(rust): route proxy through provider registry
- Make Provider trait object-safe with boxed future refresh API
- Add ProviderRegistry and active provider resolution
- Refactor proxy runtime, health, upstream routing, and refresh paths
  to use active provider instead of hardcoded Codex wiring
- Preserve Codex refresh metadata through generic refresh flow
- Keep full Rust suite green (49/49)
2026-04-08 01:37:56 +07:00
RCD d88d3a7829 feat(rust): add multi-provider config section and active provider selection
- Add ProvidersConfig with active provider field to AppConfig
- Add active_provider() and active_provider_label() config methods
- Update example TOML with providers section
- Fix all test AppConfig constructions with ..AppConfig::default()
- All 49 tests pass
2026-04-08 01:24:30 +07:00
RCD 646ceef400 feat(rust): introduce provider abstraction and refactor state to be provider-agnostic
- Add provider.rs with Provider trait, ProviderId, AuthResult, RefreshResult
- Implement CodexProvider wrapping existing Codex device/refresh flows
- Refactor state.rs: add provider-generic account methods that legacy
  codex-specific wrappers delegate to
- Simplify remove_account fallback to first remaining account
- All 49 tests pass on the new abstraction layer
2026-04-08 01:19:54 +07:00
RCD b741c439e9 fix(rust): relax expires_at assertion to work with placeholder id_token fixture 2026-04-08 01:15:55 +07:00
RCD a29917297b chore: replace test fixture tokens with placeholders for shield compliance 2026-04-08 00:49:18 +07:00
RCD d3f61fc925 chore: bump UPSTREAM_VERSION to v6.9.16 2026-04-08 00:38:44 +07:00
RCD e3302b694c Merge tag 'v6.9.16' into rust-replatform 2026-04-08 00:38:27 +07:00
Luis Pater c8b7e2b8d6 fix(executor): ensure empty stream completions use output_item.done as fallback
Fixed: #2583
2026-04-07 18:21:12 +08:00
Luis Pater cad45ffa33 Merge pull request #2578 from LemonZuo/feat_socks5h
feat: support socks5h scheme for proxy settings
2026-04-07 09:57:18 +08:00
Luis Pater 6a27bceec0 Merge pull request #2576 from zilianpn/fix/disable-cooling-auth-errors
fix(auth): honor disable-cooling and enrich no-auth errors
2026-04-07 09:56:25 +08:00
Lemon 163d68318f feat: support socks5h scheme for proxy settings 2026-04-07 07:46:11 +08:00
zilianpn 0ea768011b fix(auth): honor disable-cooling and enrich no-auth errors 2026-04-07 01:12:13 +08:00
Luis Pater 9f5bdfaa31 Merge pull request #2531 from jamestut/openai-vertex-token-usage-fix
Fix missing `response.completed.usage` for late-usage OpenAI-compatible streams
2026-04-06 09:30:49 +08:00
Luis Pater 9eabdd09db Merge pull request #2522 from aikins01/fix/strip-tool-use-signature
fix(amp): strip signature from tool_use blocks before forwarding to Claude
2026-04-06 09:30:14 +08:00
Luis Pater c3f8dc362e Merge pull request #2491 from mpfo0106/feature/claude-code-safe-alignment-sentinels
test(claude): add compatibility sentinels and centralize builtin fallback handling
2026-04-06 09:27:08 +08:00
Luis Pater b85120873b Merge pull request #2332 from RaviTharuma/fix/claude-thinking-signature
fix: preserve Claude thinking signatures in Codex translator
2026-04-06 09:25:06 +08:00
Luis Pater 6f58518c69 docs(readme): remove redundant GITSTORE_GIT_BRANCH description in README files 2026-04-06 09:23:04 +08:00
Luis Pater 000fcb15fa Merge pull request #2298 from snoyiatk/feat/add-gitstore-branch
feat(gitstore): add support for specifying git branch (via GITSTORE_G…
2026-04-06 09:21:03 +08:00
Luis Pater ea43361492 Merge pull request #2121 from destinoantagonista-wq/main
Reconcile registry model states on auth changes
2026-04-06 09:13:27 +08:00
Luis Pater c1818f197b Merge pull request #1940 from Blue-B/fix/claude-interleaved-thinking-amp-gzip-budget
fix(claude): enable interleaved-thinking beta, decode AMP error gzip, fix budget 400
2026-04-06 09:08:23 +08:00
RCD a7ea27d047 chore: record terminal operator user testing rerun 2026-04-05 20:41:25 +07:00
RCD 53be957861 fix: allow configurable codex refresh endpoint 2026-04-05 20:24:07 +07:00
RCD e6ec1fd86a chore: record terminal operator user testing 2026-04-05 20:05:54 +07:00
Aikins Laryea b0653cec7b fix(amp): strip signature from tool_use blocks before forwarding to Claude
ensureAmpSignature injects signature:"" into tool_use blocks so the
Amp TUI does not crash on P.signature.length. when Amp sends the
conversation back, Claude rejects the extra field with 400:
  tool_use.signature: Extra inputs are not permitted

strip the proxy-injected signature from tool_use blocks in
SanitizeAmpRequestBody before forwarding to the upstream API.
2026-04-05 12:26:24 +00:00
RCD 08065bd061 chore: record terminal operator scrutiny pass 2026-04-05 19:04:00 +07:00
RCD 159a3b218e fix: keep footer guidance fully visible 2026-04-05 18:55:55 +07:00
RCD 46758c9787 chore: record terminal operator scrutiny rerun 2026-04-05 18:44:03 +07:00
RCD 91fe656204 fix: count runtime-usable Codex accounts 2026-04-05 18:29:09 +07:00
RCD 67c79b9dc5 fix: keep TUI footer visible and passive polling read-only 2026-04-05 18:24:00 +07:00
RCD 53f6f99433 chore: record terminal operator scrutiny 2026-04-05 18:03:57 +07:00
RCD cea7a0f4e3 fix: make TUI runtime refresh truthful 2026-04-05 17:45:13 +07:00
RCD f4ae033c97 fix: tighten auth runtime health refresh state 2026-04-05 17:36:32 +07:00
Luis Pater 22a1a24cf5 feat(executor): add tests for preserving key order in cache control functions
Added comprehensive tests to ensure key order is maintained when modifying payloads in `normalizeCacheControlTTL` and `enforceCacheControlLimit` functions. Removed unused helper functions and refactored implementations for better readability and efficiency.
2026-04-05 17:58:13 +08:00
RCD 1c2396bb52 feat: align rust operator console state copy 2026-04-05 16:55:19 +07:00
RCD 3da810d19a feat: improve rust operator console 2026-04-05 16:39:18 +07:00
RCD 1b3542d77c feat: add runtime stats surface to proxypilot-rs 2026-04-05 16:13:41 +07:00
RCD 1c3edd9f4a feat: share auth runtime domain helpers 2026-04-05 15:56:19 +07:00
RCD c603b98c2b chore: add rust operator mission scaffolding 2026-04-05 15:42:41 +07:00
RCD fbc6545d85 feat: refresh expired codex tokens before proxying 2026-04-05 14:54:27 +07:00
RCD 20fda28fb4 feat: preserve codex plan metadata in rust state 2026-04-05 14:52:33 +07:00
RCD 469fd38420 feat: add account removal to proxypilot-rs 2026-04-05 14:51:34 +07:00
RCD 0b80a392e7 feat: surface token expiry in proxypilot-rs tui 2026-04-05 14:49:59 +07:00
RCD f9da6dcb0a feat: add account controls to proxypilot-rs tui 2026-04-05 14:47:28 +07:00
RCD 16a0799176 feat: refresh codex accounts in proxypilot-rs 2026-04-05 14:45:02 +07:00
RCD ff5fa96583 feat: add codex device login to proxypilot-rs 2026-04-05 14:39:16 +07:00
RCD 3e205f7335 feat: import codex auth files into proxypilot-rs 2026-04-05 14:32:15 +07:00
RCD 056e4d8edf feat: add codex account state to proxypilot-rs 2026-04-05 14:27:50 +07:00
RCD fcdcbfc174 feat: add explicit codex routes to proxypilot-rs 2026-04-05 14:22:48 +07:00
RCD 85687a4ab7 feat: add proxypilot-rs codex proxy shell 2026-04-05 14:19:51 +07:00
RCD 388a7a127d chore: scaffold rust replatform workspace 2026-04-05 14:19:48 +07:00
RCD e040fdb330 chore: merge upstream CLIProxyAPI v6.9.15
# Conflicts:
#	.gitignore
#	README.md
#	README_CN.md
#	README_JA.md
#	cmd/server/main.go
2026-04-05 13:55:36 +07:00
Luis Pater ada8e2905e feat(api): enhance proxy resolution for API key-based auth
Added comprehensive support for resolving proxy URLs from configuration based on API key and provider attributes. Introduced new helper functions and extended the test suite to validate fallback mechanisms and compatibility cases.
2026-04-05 01:56:34 +08:00
Luis Pater 4ba10531da feat(docs): add Poixe AI sponsorship details to README files
Added Poixe AI sponsorship information, including referral bonuses and platform capabilities, to README files in English, Japanese, and Chinese. Updated assets to include Poixe AI logo.
2026-04-05 01:20:50 +08:00
RCD 81929c06da fix(security): remove unverified management asset fallback (#12) 2026-04-04 21:25:56 +07:00
Luis Pater 3774b56e9f feat(misc): add background updater for Antigravity version caching
Introduce `StartAntigravityVersionUpdater` to periodically refresh the cached Antigravity version using a non-blocking background process. Updated main server flow to initialize the updater.
2026-04-04 22:09:11 +08:00
Luis Pater c2d4137fb9 feat(executor): enhance Qwen system message handling with strict injection and merging rules
Closes: #2537
2026-04-04 21:51:02 +08:00
Luis Pater 2ee938acaf Merge pull request #2535 from rensumo/main
feat: 动态获取 Antigravity User-Agent 版本号
2026-04-04 21:00:47 +08:00
rensumo 8d5e470e1f feat: dynamically fetch antigravity UA version from releases API
Fetch the latest version from the antigravity auto-updater releases
endpoint and cache it for 6 hours. Falls back to 1.21.9 if the API
is unreachable or returns unexpected data.
2026-04-04 14:52:59 +08:00
James 65e9e892a4 Fix missing response.completed.usage for late-usage OpenAI-compatible streams 2026-04-04 05:58:04 +00:00
Luis Pater 8430b28cfa Merge pull request #2526 from rensumo/main
feat: 升级反重力 (antigravity) UA 版本为 1.21.9
2026-04-04 11:32:16 +08:00
rensumo f3ab8f4bc5 chore: update antigravity UA version to 1.21.9 2026-04-04 07:35:08 +08:00
Luis Pater 0e4f189c2e Merge pull request #1302 from dinhkarate/feat(vertex)/add-prefix-field
Feat(vertex): add prefix field
2026-04-04 04:17:12 +08:00
Luis Pater 754b126944 fix(executor): remove commented-out code in QwenExecutor 2026-04-04 02:14:48 +08:00
Luis Pater ae37ccffbf Merge pull request #2520 from Arronlong/main
fix:qwen invalid_parameter_error
2026-04-04 02:13:09 +08:00
Luis Pater 42c062bb5b Merge pull request #2509 from adamhelfgott/fix-claude-thinking-temperature
Normalize Claude temperature when thinking is enabled
2026-04-03 23:55:50 +08:00
Luis Pater f389667ec3 Merge pull request #2513 from lonr-6/codex/fix-ws-custom-tool-repair-v2
fix: repair responses websocket custom tool call pairing
2026-04-03 23:45:38 +08:00
Arronlong 29dba0399b Comment out system message check in Qwen executor
fix qwen invalid_parameter_error
2026-04-03 23:07:33 +08:00
Luis Pater a824e7cd0b feat(models): add GPT-5.3, GPT-5.4, and GPT-5.4-mini with enhanced "thinking" levels 2026-04-03 23:05:10 +08:00
Kai Wang d1fd2c4ad4 fix: repair websocket custom tool calls 2026-04-03 17:11:44 +08:00
Kai Wang b6c6379bfa fix: repair websocket custom tool calls 2026-04-03 17:11:42 +08:00
Kai Wang 8f0e66b72e fix: repair websocket custom tool calls 2026-04-03 17:11:41 +08:00
Adam Helfgott f63cf6ff7a Normalize Claude temperature for thinking 2026-04-03 03:45:51 -04:00
mpfo0106 9b5ce8c64f Keep Claude builtin helpers aligned with the shared helper layout
The review asked for the builtin tool registry helper to live with the rest
of executor support utilities. This moves the registry code into the helps
package, exports the minimal surface executor needs, and keeps behavior tests
with the executor while leaving registry-focused checks with the helper.

Constraint: Requested layout keeps executor helper utilities centralized under internal/runtime/executor/helps
Rejected: Keep the files in executor and reply with rationale | conflicts with requested package organization
Confidence: high
Scope-risk: narrow
Reversibility: clean
Directive: Keep executor behavior tests near applyClaudeToolPrefix and keep pure registry tests in helps
Tested: go test ./internal/runtime/executor/helps ./internal/runtime/executor -run 'Claude|Builtin|Tool'; go test ./test/...; go test ./...
Not-tested: End-to-end Claude Code direct-connect/session runtime behavior
2026-04-03 00:13:02 +09:00
Duong M. CUONG 058793c73a feat(gitstore): honor configured branch and follow live remote default 2026-04-02 14:44:44 +00:00
mpfo0106 da3a498a28 Keep Claude Code compatibility work low-risk and reviewable
This change stops short of broader Claude Code runtime alignment and instead
hardens two safe edges: builtin tool prefix handling and source-informed
sentinel coverage for future drift checks.

Constraint: Must preserve existing default behavior for current users
Rejected: Implement control-plane/session alignment now | too much runtime risk for a first slice
Confidence: high
Scope-risk: narrow
Reversibility: clean
Directive: Treat the new fixtures as compatibility sentinels, not a full Claude Code schema contract
Tested: go test ./test/...; go test ./sdk/translator/...; go test ./internal/runtime/executor -run 'Claude|Builtin|Tool'; go test ./...
Not-tested: End-to-end Claude Code direct-connect/session runtime behavior
2026-04-02 20:35:39 +09:00
Ravi Tharuma 5fc2bd393e fix: retain codex thinking signature until item done 2026-03-28 14:41:25 +01:00
Ravi Tharuma 66eb12294a fix: clear stale thinking signature when no block is open 2026-03-28 14:08:31 +01:00
Ravi Tharuma 73b22ec29b fix: omit empty signature field from thinking blocks
Emit signature only when non-empty in both streaming content_block_start
and non-streaming thinking blocks. Avoids turning 'missing signature'
into 'empty/invalid signature' which Claude clients may reject.
2026-03-28 14:08:31 +01:00
Ravi Tharuma c31ae2f3b5 fix: retain previously captured thinking signature on new summary part 2026-03-28 14:08:31 +01:00
Ravi Tharuma 76b53d6b5b fix: finalize pending thinking block before next summary part 2026-03-28 14:08:31 +01:00
Ravi Tharuma a34dfed378 fix: preserve Claude thinking signatures in Codex translator 2026-03-28 14:08:31 +01:00
dinhkarateandClaude Opus 4.6 36efcc6e28 fix(vertex): include prefix in auth filename and validate at import
Address two blocking issues from PR review:
- Auth file now named vertex-{prefix}-{project}.json so importing the
  same project with different prefixes no longer overwrites credentials
- Prefix containing "/" is rejected at import time instead of being
  silently ignored at runtime
- Add prefix to in-memory metadata map for consistency

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 15:06:04 +07:00
Pham Quang Dinh a337ecf35c Merge branch 'router-for-me:main' into feat(vertex)/add-prefix-field 2026-03-17 11:48:40 +07:00
destinoantagonista-wq e08f68ed7c chore(auth): drop reconcile test file from pr 2026-03-14 14:41:26 +00:00
destinoantagonista-wq f09ed25fd3 fix(auth): tighten registry model reconciliation 2026-03-14 14:40:06 +00:00
destinoantagonista-wq e166e56249 Reconcile registry model states on auth changes
Add Manager.ReconcileRegistryModelStates to clear stale per-model runtime failures for models currently registered in the global model registry. The method finds models supported for an auth, resets non-clean ModelState entries, updates aggregated availability, persists changes, and pushes a snapshot to the scheduler. Introduce modelStateIsClean helper to determine when a model state needs resetting. Call ReconcileRegistryModelStates from Service paths that register/refresh models (applyCoreAuthAddOrUpdate and refreshModelRegistrationForAuth) to keep the scheduler and global registry aligned after model re-registration.
2026-03-13 19:41:49 +00:00
Blue-B 5f58248016 fix(claude): clamp max_tokens to model limit in normalizeClaudeBudget
When adjustedBudget < minBudget, the previous fix blindly set
max_tokens = budgetTokens+1 which could exceed MaxCompletionTokens.

Now: cap max_tokens at MaxCompletionTokens, recalculate budget, and
disable thinking entirely if constraints are unsatisfiable.

Add unit tests covering raise, clamp, disable, and no-op scenarios.
2026-03-09 22:10:30 +09:00
Blue-B 07d6689d87 fix(claude): add interleaved-thinking beta header, AMP gzip error decoding, normalizeClaudeBudget max_tokens
1. Always include interleaved-thinking-2025-05-14 beta header so that
   thinking blocks are returned correctly for all Claude models.

2. Remove status-code guard in AMP reverse proxy ModifyResponse so that
   error responses (4xx/5xx) with hidden gzip encoding are decoded
   properly — prevents garbled error messages reaching the client.

3. In normalizeClaudeBudget, when the adjusted budget falls below the
   model minimum, set max_tokens = budgetTokens+1 instead of leaving
   the request unchanged (which causes a 400 from the API).
2026-03-07 21:31:10 +09:00
dinhkarate 14cb2b95c6 feat(vertex): add --vertex-import-prefix flag for model namespacing 2026-01-29 13:32:38 +07:00
dinhkarate fdeef48498 feat(vertex): Add Prefix field to VertexCredentialStorage for per-file model namespacing 2026-01-29 13:32:38 +07:00
104 changed files with 14133 additions and 629 deletions
+8
View File
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
cargo fetch --manifest-path rust/Cargo.toml
if [ ! -f rust/proxypilot-rs.toml ]; then
cargo run --manifest-path rust/Cargo.toml -p proxypilot-rs -- init --config rust/proxypilot-rs.toml
fi
+86
View File
@@ -0,0 +1,86 @@
# Architecture
High-level system guidance for the Rust terminal operator-console slice.
**What belongs here:** module boundaries, state ownership, data flow, invariants, and where workers should add new behavior.
**What does not belong here:** low-level implementation diffs or line-by-line rewrite plans.
---
## Current Rust Entry Points
- `rust/proxypilot-rs/src/main.rs` wires CLI entrypoints.
- `cli.rs` defines command surfaces.
- `accounts.rs` handles CLI account operations against disk-backed account state.
- `config.rs` loads TOML config and resolves the state-file path.
- `state.rs` owns persisted local account state.
- `codex.rs` owns Codex device login and refresh-token exchange logic.
- `proxy.rs` owns the local HTTP proxy process and current in-memory proxy state.
- `tui.rs` owns the terminal operator console.
## State Layers
There are three distinct state layers in this mission. Workers must keep them separate and truthful:
1. **Config state**
- Loaded from TOML via `config.rs`
- Includes bind address, state-file path, upstream base URL, and fallback API key
2. **Saved local account state**
- Loaded and saved through `state.rs`
- Used by CLI account commands and by the TUI for disk-backed account visibility and local actions
3. **Live runtime state**
- Lives inside the running proxy process
- Includes the adopted active credential, request counters, refresh attempts/results, and any runtime-auth summary shown by `/v0/runtime/stats`
- Must not silently re-read disk on every stats request and pretend disk changes already altered runtime memory
## Desired Shared Domain for This Mission
This milestone should create a small shared auth/runtime domain used by both proxy and TUI-facing logic. It should own:
- RFC3339 parsing / clock helpers
- auth-health classification with the shared vocabulary:
- `valid`
- `expiring_soon`
- `expired`
- `static`
- `unknown`
- refresh outcome/status types
- runtime stats snapshot types for `/v0/runtime/stats`
The goal is to remove duplicated auth/time decision logic currently spread across `proxy.rs` and `tui.rs`.
## Runtime Stats Surface
`GET /v0/runtime/stats` is a read-only local operator endpoint.
It should expose runtime-memory state for:
- bind address
- upstream base URL
- active account name
- account count
- auth-health summary
- request counters
- last runtime refresh status
- last runtime refresh timestamp
This endpoint is the runtime source of truth for the TUI's operator-health panels.
## TUI Composition Model
The TUI should combine two inputs:
- **Disk-backed local state** for account list, selected-account details, and local account actions
- **Runtime stats HTTP data** for live auth/runtime visibility
This split is intentional. The UI must stay truthful if local saved state and running runtime state diverge.
## Mission Invariants
- The branch remains Codex-only for this mission.
- The TUI remains terminal-first and should become more operator-console-like, not more dashboard-like.
- Runtime counters are in-memory only and reset when the proxy process restarts.
- `R` targets the active account runtime action; `f` targets the selected saved account action.
- `r` reloads local/disk and runtime surfaces; `c` clears transient feedback only.
- Empty states and failures must stay readable and operator-meaningful.
+27
View File
@@ -0,0 +1,27 @@
# Environment
Environment variables, external dependencies, and setup notes.
**What belongs here:** required env vars, local setup notes, external dependency expectations, and fixture guidance.
**What does not belong here:** service start/stop commands or fixed ports outside explanatory notes.
---
## External Dependencies
- No external database, cache, or queue is required for this mission.
- No live Codex/OpenAI credentials are required for baseline automated validation.
- Use mocked local HTTP fixtures for upstream proxy and refresh-token flows unless the user explicitly expands scope to live credentials.
## Local Config Notes
- The Rust slice uses a TOML config and a separate state file.
- Run `./.factory/init.sh` to fetch Rust dependencies and create `rust/proxypilot-rs.toml` if it is missing before local smoke validation.
- Workers may generate temporary local configs for smoke validation.
- Relative state-file paths resolve relative to the chosen config file path.
## Local Runtime Notes
- The default Rust bind is `127.0.0.1:8318`.
- Keep runtime observability surfaces on the same local Rust proxy process.
- Avoid unrelated busy ports already present on the machine.
+97
View File
@@ -0,0 +1,97 @@
# User Testing
Validation guidance for the Rust terminal operator-console mission.
**What belongs here:** validation surfaces, tools, setup notes, and concurrency guidance.
**What does not belong here:** service command definitions (use `.factory/services.yaml`).
---
## Validation Surface
### HTTP runtime surface
- Endpoints:
- `GET /healthz`
- `GET /v0/runtime/stats`
- supported local Codex proxy routes touched by runtime tracking
- Primary tools:
- `curl`
- Rust mocked endpoint tests
- Validation focus:
- local health identity
- runtime stats schema and semantics
- request counters
- proactive refresh
- 401 retry refresh
- refresh failure tracking
### TUI surface
- Command:
- `cargo run -p proxypilot-rs -- tui --config <path>`
- Primary tools:
- `tuistory`
- manual local smoke when needed
- Validation focus:
- runtime/auth visibility
- truthful footer/help text
- local account visibility when runtime is down
- reload, clear feedback, selected refresh, active refresh-now
- readable empty/error states
### CLI/account surface
- Commands:
- `init`
- `account add-codex`
- `account list`
- `account activate`
- `account remove`
- `account refresh-codex`
- Primary tools:
- shell
- Validation focus:
- config creation semantics
- disk-backed account lifecycle semantics
- refreshable vs static-account behavior
## Validation Concurrency
- Automated Rust validation activities: max `2` concurrent
- rationale: machine has ample memory/CPU, but Rust tests plus local mocked listener flows should stay conservative
- Manual TUI validation: max `1` at a time
- rationale: interactive terminal verification is not realistically parallel and should not overlap with another heavy manual runtime smoke
## Setup Notes
- Baseline automated validation does not require live external credentials.
- Prefer mocked local upstream and refresh fixtures for HTTP/runtime tests.
- TUI validation requires a local config file and usually a running local Rust proxy for runtime-visible scenarios.
- Proxy-down scenarios are also required: validators should verify local disk-backed account visibility when runtime is unreachable.
## Flow Validator Guidance: shell
- Use only `.factory/validation/terminal-operator-console/user-testing/fixtures/cli-shell/` plus `/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/`.
- Use temporary config/state files inside that fixture directory; do not modify repo-root default configs.
- Stay on CLI/account flows only; do not start long-lived proxy services from this fixture.
- Do not use live device-login. Prefer `init`, `account add-codex`, `account list`, `account activate`, `account remove`, and negative-path `account refresh-codex` checks against local fixture state.
- If refresh-success validation cannot be isolated without live OpenAI OAuth, record it as blocked with concrete evidence rather than using private credentials.
## Flow Validator Guidance: curl
- Use only `.factory/validation/terminal-operator-console/user-testing/fixtures/http-runtime/` plus `/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/`.
- Own ports `18318-18339` for this validator only. Keep the proxy on `127.0.0.1:18318` unless a collision forces another port in that range.
- Mock the Codex upstream locally; never send `/v1/*` validation traffic to the real OpenAI upstream.
- Auth-refresh success paths may be blocked if the running binary cannot be pointed at a local OAuth token stub. Failure-path validation with intentionally invalid refresh tokens is allowed if it stays within the assigned fixture.
- Capture before/after `curl` output for stats-changing requests so counter deltas are explicit.
## Flow Validator Guidance: tuistory
- Use only `.factory/validation/terminal-operator-console/user-testing/fixtures/tui-console/` plus `/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/`.
- Run only one TUI session at a time.
- Own ports `18418-18439` for proxy/mock services used by this validator. Keep runtime-down scenarios fully local by stopping those fixture services instead of touching shared ports.
- Keep disk-backed state isolated to the assigned fixture config/state files so reload, activate, delete, and refresh actions are attributable to this validator alone.
- Capture snapshots before and after each key flow (`r`, `a`, `d`, `f`, `R`, `c`) and note whether feedback came from local disk state or live runtime stats.
## Discovered Validation Quirks
- CLI `account refresh-codex` and runtime-triggered refresh paths now honor `codex.refresh_token_url` when set, so validators can redirect refresh success to a local stub from the real user surface while still relying on the built-in OpenAI OAuth endpoint when the override is unset.
- `tuistory` sessions should be closed explicitly after the TUI exits with `q`; the PTY can remain allocated even after the binary terminates.
- In runtime-down TUI scenarios, selected-account refresh (`f`) still attempts the hardwired OAuth refresh exchange for refreshable saved accounts and surfaces that remote failure text. Treat that as truthful runtime-dependent behavior, not as loss of local disk state.
+16
View File
@@ -0,0 +1,16 @@
commands:
install: cargo fetch --manifest-path rust/Cargo.toml
init: ./.factory/init.sh
fmt: cargo fmt --all --manifest-path rust/Cargo.toml
test: cargo test --manifest-path rust/Cargo.toml
typecheck: cargo check --manifest-path rust/Cargo.toml
lint: cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings
tui-help: cargo run --manifest-path rust/Cargo.toml -p proxypilot-rs -- tui --help
services:
proxypilot-rs:
start: cargo run --manifest-path rust/Cargo.toml -p proxypilot-rs -- run --config rust/proxypilot-rs.toml
stop: lsof -ti :8318 | xargs -r kill
healthcheck: curl -sf http://127.0.0.1:8318/healthz
port: 8318
depends_on: []
@@ -0,0 +1,89 @@
---
name: rust-operator-worker
description: Implement Rust terminal-operator-console features for proxypilot-rs with test-first discipline and truthful runtime/TUI verification.
---
# Rust Operator Worker
NOTE: Startup and cleanup are handled by `worker-base`. This skill defines the WORK PROCEDURE.
## When to Use This Skill
Use for features in the Rust replatform slice that touch `proxypilot-rs` runtime/auth logic, local HTTP observability, CLI/account coherence, or the terminal operator console.
## Required Skills
- `tdd` — invoke before implementation to drive failing tests first for new runtime/auth/TUI helper behavior.
- `tuistory` — invoke when manually validating terminal operator-console behavior.
- `verification-before-completion` — invoke before claiming the feature is complete so evidence matches the actual commands and outputs.
## Work Procedure
1. Read `mission.md`, `validation-contract.md`, mission `AGENTS.md`, and `.factory/library/*.md` relevant to the feature before editing.
2. Confirm which validation assertions the feature fulfills and restate them in your notes.
3. Write or update failing Rust tests first (red) for the feature's intended behavior before changing implementation.
4. Implement the smallest Rust changes needed to make those tests pass while preserving the mission boundaries:
- keep the work inside the Rust replatform slice unless docs are explicitly part of the feature
- prefer shared auth/runtime helpers over duplicating logic
- keep runtime-memory state distinct from disk-backed saved state
5. Run focused checks during iteration, then the required final validators:
- `cargo fmt --all --manifest-path rust/Cargo.toml`
- `cargo test --manifest-path rust/Cargo.toml`
6. If the feature affects the TUI or runtime observability surface, perform manual verification with `tuistory` or equivalent local smoke and capture what changed on screen or via local HTTP output.
7. Review the changed files for truthfulness of operator-visible wording, especially where runtime state and disk state can diverge.
8. Produce a handoff that cites exact commands, tests added, interactive checks, and any discovered gaps.
## Example Handoff
```json
{
"salientSummary": "Added a shared auth-health/runtime model plus `/v0/runtime/stats`, then wired the TUI runtime panel to the new stats surface. Ran fmt/tests successfully and manually verified runtime counters plus active-account refresh feedback in the terminal UI.",
"whatWasImplemented": "Created a shared runtime/auth module for auth-health classification and refresh status, extended proxy state with in-memory counters and last refresh metadata, added GET /v0/runtime/stats with stable fields, and updated the TUI to render runtime stats while keeping disk-backed account controls separate.",
"whatWasLeftUndone": "",
"verification": {
"commandsRun": [
{
"command": "cargo fmt --all --manifest-path rust/Cargo.toml",
"exitCode": 0,
"observation": "Formatting completed without changes needed after final pass."
},
{
"command": "cargo test --manifest-path rust/Cargo.toml",
"exitCode": 0,
"observation": "Workspace tests passed, including new runtime stats and auth-health cases."
}
],
"interactiveChecks": [
{
"action": "Launched proxypilot-rs TUI against a local fixture config, pressed `r`, `R`, and `c`, and inspected the runtime/auth panel.",
"observed": "Runtime counters and last refresh outcome became visible, active-account refresh targeted the active account even when another row was selected, and clear-feedback only cleared the transient status line."
}
]
},
"tests": {
"added": [
{
"file": "rust/proxypilot-rs/src/proxy.rs",
"cases": [
{
"name": "runtime_stats_reset_after_restart",
"verifies": "In-memory runtime stats start empty on fresh process start and do not persist across restarts."
},
{
"name": "runtime_stats_reports_401_refresh_path_once",
"verifies": "A single client request that triggers 401 and retry increments upstream 401 and refresh counters without double-counting total proxied requests."
}
]
}
]
},
"discoveredIssues": []
}
```
## When to Return to Orchestrator
- The feature requires changing mission boundaries or touching non-Rust product surfaces outside the approved scope.
- The intended behavior cannot be implemented truthfully without clarifying runtime-vs-disk ownership semantics.
- A required local validation path cannot be exercised because config/fixtures/runtime setup is missing or broken.
- The feature reveals a larger refactor is needed than fits a single worker session.
@@ -0,0 +1,28 @@
{
"featureId": "cli-state-runtime-truthfulness-and-docs",
"reviewedAt": "2026-04-05T10:54:50.137470Z",
"commitId": "1c2396bb",
"transcriptSkeletonReviewed": true,
"diffReviewed": true,
"status": "fail",
"codeReview": {
"summary": "Reviewed feature commit 1c2396bb together with the directly relevant follow-up fixes f4ae033c and cea7a0f4. CLI wording, docs alignment, static-account refresh errors, and runtime-vs-disk refresh targeting are much clearer after those changes, but the TUI footer layout still prevents the new explanatory copy from being visible.",
"issues": [
{
"file": "rust/proxypilot-rs/src/tui.rs",
"line": 389,
"severity": "blocking",
"description": "The footer block is still allocated `Constraint::Length(3)` even though it now renders four content lines (feedback, selected-account hint, runtime status hint, and key help) inside a bordered paragraph at lines 523-534. In ratatui that height leaves room for only a single inner text row, so the newly added runtime/disk guidance and keybinding help are clipped off-screen. That means the feature does not actually make the cross-surface behavior explicit to operators, and docs/tests now describe footer copy that users cannot see."
}
]
},
"sharedStateObservations": [
{
"area": "skills",
"observation": "TUI copy changes in this mission need a visibility check, not just string-helper tests. The current worker skill and tests verified footer text helpers, but they did not verify that the fixed-height footer block can actually render the added lines.",
"evidence": "rust/proxypilot-rs/src/tui.rs:389 keeps the footer at `Constraint::Length(3)` while rust/proxypilot-rs/src/tui.rs:523-534 renders four lines; the added tests at rust/proxypilot-rs/src/tui.rs:770-842 assert string content only and do not exercise visible layout fit."
}
],
"addressesFailureFrom": null,
"summary": "Fail: the reviewed feature and its direct follow-up fixes improved CLI/runtime truthfulness, but the TUI footer's fixed height still clips the newly added selected-account/runtime hints and key help, so an operator cannot see the copy intended to explain runtime-vs-disk divergence."
}
@@ -0,0 +1,21 @@
{
"featureId": "fix-runtime-account-count-semantics",
"reviewedAt": "2026-04-05T11:36:06.651055Z",
"commitId": "91fe6562",
"transcriptSkeletonReviewed": true,
"diffReviewed": true,
"status": "pass",
"codeReview": {
"summary": "Reviewed the original runtime-stats feature (`1b3542d7`), the prior scrutiny finding, and the fix commit (`91fe6562`). The fix resolves the remaining non-blocking truthfulness issue: `/v0/runtime/stats.account_count` now counts only runtime-usable Codex accounts, mixed-provider and blank-key entries no longer inflate that value, and the TUI wording matches the runtime semantics.",
"issues": []
},
"sharedStateObservations": [
{
"area": "skills",
"observation": "The worker skipped the rust-operator-worker skill's required manual `tuistory` verification even though the feature touched TUI-visible runtime wording. For this fix the unit-level wording assertion was likely sufficient, but the skill currently leaves that lightweight label-only case undocumented.",
"evidence": "Skill procedure step 6 requires manual verification with `tuistory` when the feature affects the TUI or runtime observability surface (`/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/skills/rust-operator-worker/SKILL.md`). The handoff for session `38bb865f-5bfd-4b20-8a75-281170e69c06` records `followedProcedure: false` and says manual TUI verification was skipped in favor of unit tests because the change was only label alignment."
}
],
"addressesFailureFrom": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/scrutiny/reviews/proxy-runtime-stats-surface.json",
"summary": "Pass. The follow-up fix closes the prior `account_count` truthfulness gap and I found no new code issues in the scoped change."
}
@@ -0,0 +1,15 @@
{
"featureId": "fix-runtime-auth-health-and-date-validation",
"reviewedAt": "2026-04-05T10:53:54.938311Z",
"commitId": "f4ae033c97c0912c03c0a0eaa9b14a2aeb696f8f",
"transcriptSkeletonReviewed": true,
"diffReviewed": true,
"status": "pass",
"codeReview": {
"summary": "Reviewed commit `f4ae033c97c0912c03c0a0eaa9b14a2aeb696f8f` against the feature requirements and worker handoff. The implementation now rejects impossible RFC3339 calendar dates in `auth_runtime::parse_rfc3339_z`, classifies malformed impossible expiry metadata as `unknown`, and recomputes `/v0/runtime/stats` auth health from the live in-memory account state after proactive and 401-triggered refresh mutations. The targeted regressions in `auth_runtime.rs` and `proxy.rs` cover invalid leap-day/month-end inputs plus successful and failed refresh paths, and I found no remaining high-confidence correctness issues in scope.",
"issues": []
},
"sharedStateObservations": [],
"addressesFailureFrom": null,
"summary": "Pass. The reviewed fix closes the two reported blockers: impossible RFC3339 calendar dates no longer parse as valid expiries, and proxy runtime stats now report refreshed auth health truthfully after runtime credential mutations."
}
@@ -0,0 +1,28 @@
{
"featureId": "fix-tui-footer-and-passive-polling",
"reviewedAt": "2026-04-05T11:36:55Z",
"commitId": "67c79b9dc5755305448c33df5cb1d761f150bece",
"transcriptSkeletonReviewed": true,
"diffReviewed": true,
"status": "fail",
"codeReview": {
"summary": "Reviewed the fix commit together with both prior failed reviews (`terminal-tui-operator-console` and `cli-state-runtime-truthfulness-and-docs`). The passive-polling blocker is resolved: idle polling now stays on `/healthz` + `/v0/runtime/stats`, and the new regression test proves it no longer calls `/v1/models`. However, the footer visibility blocker is only partially fixed: the panel now fits the four nominal footer lines, but realistic long feedback still wraps and pushes the help text back out of view.",
"issues": [
{
"file": "rust/proxypilot-rs/src/tui.rs",
"line": 396,
"severity": "blocking",
"description": "The footer still has a fixed height of `Constraint::Length(6)`, which leaves only four inner text rows once the bordered `Keys` block is rendered (`tui.rs:530-543`). That is enough only when the feedback line stays on a single row. But routine operator messages in this same file are much longer\u2014for example the `runtime_refresh_target` failure text at `tui.rs:701-708` is 131 characters before account-name variation, so in the worker's own 120-column verification geometry it wraps to two rows. When that happens, one of the status/help lines is clipped again, so the feature does not fully satisfy the prior blocker of keeping all footer/help guidance visible. The added snapshot test (`tui.rs:943-975`) only exercises a short `Feedback line`, so it misses this realistic wrapped-feedback path."
}
]
},
"sharedStateObservations": [
{
"area": "skills",
"observation": "The shared Rust operator-worker guidance still does not tell workers to exercise long, wrapped operator feedback when validating fixed-height TUI regions. That omission helped this fix reintroduce a footer-visibility edge case even though the worker followed the documented procedure and added a snapshot test.",
"evidence": "`rust-operator-worker` step 6/7 asks for manual TUI verification and truthful operator-visible wording review, but it does not call out wrap/line-budget checks for fixed-height panels (`/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/skills/rust-operator-worker/SKILL.md`). In this fix, the new regression test in `rust/proxypilot-rs/src/tui.rs:943-975` only uses a one-line `Feedback line`, while the production footer can render much longer routine messages such as `runtime_refresh_target` at `tui.rs:701-708`."
}
],
"addressesFailureFrom": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/scrutiny/reviews/terminal-tui-operator-console.json; /home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/scrutiny/reviews/cli-state-runtime-truthfulness-and-docs.json",
"summary": "Fail. The fix successfully stops passive idle polling from contaminating runtime telemetry, but the footer/help blocker is not fully resolved because long, routine feedback messages can still wrap and hide guidance in the fixed-height footer."
}
@@ -0,0 +1,15 @@
{
"featureId": "fix-tui-footer-wrap-visibility",
"reviewedAt": "2026-04-05T12:02:30Z",
"commitId": "159a3b21",
"transcriptSkeletonReviewed": true,
"diffReviewed": true,
"status": "pass",
"codeReview": {
"summary": "Reviewed the prior failed fix (`fix-tui-footer-and-passive-polling`) alongside the rerun fix commit `159a3b21`, the worker handoff, and the transcript skeleton. This rerun fully addresses the remaining footer-wrap blocker: the TUI now sizes the footer from its wrapped content instead of a fixed six-row region, and the regression test was updated to use the same long runtime-active-account failure wording that previously exposed clipping.",
"issues": []
},
"sharedStateObservations": [],
"addressesFailureFrom": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/scrutiny/reviews/fix-tui-footer-and-passive-polling.json",
"summary": "Pass. The rerun replaces the fixed-height footer with content-based sizing and adds realistic long-message regression coverage, so the prior footer-wrap visibility failure is adequately resolved."
}
@@ -0,0 +1,21 @@
{
"featureId": "fix-tui-runtime-truthfulness",
"reviewedAt": "2026-04-05T10:51:50.745826Z",
"commitId": "cea7a0f4",
"transcriptSkeletonReviewed": true,
"diffReviewed": true,
"status": "pass",
"codeReview": {
"summary": "The TUI change now drives `R` from the runtime stats active account, preserves truthful runtime-unavailable and missing-runtime-active feedback, and relabels runtime-memory account counts so they are no longer presented as disk state. The scoped tests cover the new runtime-vs-disk selection logic and wording, and I found no high-confidence correctness issues in the reviewed change.",
"issues": []
},
"sharedStateObservations": [
{
"area": "skills",
"observation": "Mission worker guidance does not explain that mission artifacts may live outside the repo under `/home/fsos/.factory/missions/<id>`. The reviewed worker initially tried repo-relative mission paths and hit ENOENT before switching to the absolute mission directory.",
"evidence": "The transcript skeleton for worker session `da34164d-4575-44bf-b059-608a68652407` shows failed reads of `/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/{mission.md,validation-contract.md,AGENTS.md}` followed by successful reads from `/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/...`; `rust-operator-worker` step 1 requires reading mission artifacts but does not document this path fallback."
}
],
"addressesFailureFrom": null,
"summary": "Pass. Commit `cea7a0f4` resolves the reviewed TUI truthfulness blockers: `R` now follows runtime-active state from `/v0/runtime/stats`, runtime-unavailable cases remain explicit without hiding disk-backed state, and the runtime count label no longer claims to be disk-backed."
}
@@ -0,0 +1,33 @@
{
"featureId": "proxy-runtime-stats-surface",
"reviewedAt": "2026-04-05T10:57:26.499831Z",
"commitId": "1b3542d77c125df31d091f052ca872237c5932c6",
"transcriptSkeletonReviewed": true,
"diffReviewed": true,
"status": "pass",
"codeReview": {
"summary": "Reviewed the original runtime-stats implementation commit (`1b3542d7`) plus the directly relevant follow-up fix commit (`f4ae033c`). The feature now satisfies the core HTTP contract: `/v0/runtime/stats` is local-only, counters stay in memory and reset on restart, supported proxied requests are counted once per client request, proactive and 401-triggered refresh outcomes are surfaced, and post-refresh auth health is recomputed truthfully. I found one remaining non-blocking truthfulness gap in mixed-provider saved state.",
"issues": [
{
"file": "rust/proxypilot-rs/src/proxy.rs",
"line": 105,
"severity": "non_blocking",
"description": "`account_count` reports `accounts.accounts.len()`, which counts every saved entry loaded into memory, not just runtime-usable Codex accounts. That becomes untruthful in mixed-provider state because `AccountState` only treats `provider == \"codex\"` entries as active/refreshable (`rust/proxypilot-rs/src/state.rs:153`, `rust/proxypilot-rs/src/state.rs:170`, `rust/proxypilot-rs/src/state.rs:300`), while imported auth preserves arbitrary `provider_type` values from disk (`rust/proxypilot-rs/src/state.rs:232`). A non-Codex imported entry will inflate `/v0/runtime/stats.account_count` even though the runtime cannot actually use it."
}
]
},
"sharedStateObservations": [
{
"area": "conventions",
"observation": "This feature updated `docs/rust-replatform.md` even though the mission guidance says only docs-alignment work should touch that file. The rule exists, but the worker still crossed it in the runtime-stats feature commit.",
"evidence": "Mission AGENTS says `Docs ownership: only features explicitly scoped to docs alignment should update docs/rust-replatform.md or README.md` (`/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/AGENTS.md:59`), while commit `1b3542d7` changes `docs/rust-replatform.md` alongside `rust/proxypilot-rs/src/proxy.rs`."
},
{
"area": "services",
"observation": "Runtime-surface smoke verification depends on setup that is not represented in `.factory/services.yaml`: generating a config file and standing up a local mock upstream/refresh server. Workers had to do this ad hoc in the transcript instead of using shared service metadata.",
"evidence": "`.factory/services.yaml:8-12` only defines `proxypilot-rs` start/stop/healthcheck. The reviewed worker transcript for session `f8120536-90d3-4124-9b6f-a84412e716f4` shows manual `.factory/init.sh`, temporary config creation, and a background mock HTTP server before the `curl` smoke documented in the handoff `2026-04-05T09-14-14-581Z__proxy-runtime-stats-surface__f8120536-90d3-4124-9b6f-a84412e716f4.json`."
}
],
"addressesFailureFrom": null,
"summary": "Pass with one non-blocking issue. The runtime stats surface works as intended after the direct follow-up fix, but `account_count` still overstates runtime-usable accounts when saved state contains non-Codex imported entries."
}
@@ -0,0 +1,26 @@
{
"featureId": "shared-auth-runtime-domain",
"reviewedAt": "2026-04-05T10:52:53.771197Z",
"commitId": "1c3edd9f4a2493a2dcd27931a680c359c14c9f43",
"transcriptSkeletonReviewed": true,
"diffReviewed": true,
"status": "pass",
"codeReview": {
"summary": "The feature successfully extracted shared auth/runtime primitives into `auth_runtime.rs` and both proxy and TUI consumers were moved onto the shared RFC3339 parsing, auth-health classification, and runtime snapshot types. The only blocking correctness gap from the original commit (impossible calendar dates being accepted, plus stale runtime auth-health after refresh) was addressed by the directly relevant follow-up fix `f4ae033c`, so there are no remaining high-confidence issues in this review scope.",
"issues": []
},
"sharedStateObservations": [
{
"area": "skills",
"observation": "The worker had to deviate because the mission/bootstrap path guidance pointed at a repo-relative mission directory that did not exist; the recoverable fallback to `/home/fsos/.factory/missions/...` is not captured in the Rust worker guidance.",
"evidence": "Handoff `2026-04-05T08-56-54-173Z__shared-auth-runtime-domain__132757c8-6967-4cb2-b929-a387c3429b02.json`: lines 100-115 record `followedProcedure: false` and explicitly ask to clarify handling when bootstrap paths point to a repo-relative mission directory that does not exist."
},
{
"area": "services",
"observation": "Local smoke/runtime setup depends on generating `rust/proxypilot-rs.toml` first, but the services manifest only advertises a start command that assumes that file already exists. The canonical setup helper is discoverable only via `.factory/init.sh` rather than the shared service metadata.",
"evidence": "`.factory/services.yaml:9` starts proxypilot-rs with `--config rust/proxypilot-rs.toml`, while `.factory/init.sh:6-7` creates that file on demand before smoke validation."
}
],
"addressesFailureFrom": null,
"summary": "Reviewed the shared auth/runtime domain commit (`1c3edd9f4a2493a2dcd27931a680c359c14c9f43`), its transcript/handoff, and the directly relevant follow-up fix (`f4ae033c`). Result: pass, with no remaining actionable code defects in scope after the follow-up fix, plus two shared-state observations about mission path guidance and init/service discoverability."
}
@@ -0,0 +1,39 @@
{
"featureId": "terminal-tui-operator-console",
"reviewedAt": "2026-04-05T18:06:00Z",
"commitId": "3da810d19a174aa439f079db9b4fb9ad3a15461e",
"transcriptSkeletonReviewed": true,
"diffReviewed": true,
"status": "fail",
"codeReview": {
"summary": "Reviewed the original TUI feature commit `3da810d1` plus the directly relevant follow-up fix `cea7a0f4`. The console now shows runtime stats, handles runtime-unavailable cases more explicitly, and `R` now targets the runtime-reported active account, but two blocking truthfulness issues remain in the live console: the footer/help region is clipped by the fixed layout height, and the TUI's own background model polling mutates the runtime counters and refresh state it is supposed to observe.",
"issues": [
{
"file": "rust/proxypilot-rs/src/tui.rs",
"line": 389,
"severity": "blocking",
"description": "The footer block is still allocated `Constraint::Length(3)` while the `Keys` paragraph renders four separate content lines (`feedback`, selected-account hint, runtime hint, and key help at lines 525-532). With a bordered ratatui block that leaves only one inner text row, so the newly added help/runtime guidance is clipped off-screen. The feature therefore does not actually deliver readable, truthful footer/help text in the running console."
},
{
"file": "rust/proxypilot-rs/src/tui.rs",
"line": 113,
"severity": "blocking",
"description": "The idle poll path still calls `refresh_models()` whenever `/healthz` succeeds (`tui.rs:49-50`, `tui.rs:113-121`, `tui.rs:218-220`). That performs a real `GET /v1/models` through the local proxy, and the proxy counts every supported request and may trigger proactive refresh before forwarding (`proxy.rs:143-145`, `proxy.rs:264-267`). As a result, merely leaving the operator console open inflates `total_proxied_requests`/`successful_upstream_responses` and can change `last_refresh`, so the runtime counters and refresh outcome are not a truthful view of external runtime activity."
}
]
},
"sharedStateObservations": [
{
"area": "conventions",
"observation": "Mission guidance says only docs-scoped features should edit `docs/rust-replatform.md`, but this TUI feature commit changed that file anyway.",
"evidence": "Mission AGENTS `/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/AGENTS.md:59` says `Docs ownership: only features explicitly scoped to docs alignment should update docs/rust-replatform.md or README.md`, while commit `3da810d1` changes `docs/rust-replatform.md` alongside `rust/proxypilot-rs/src/tui.rs`."
},
{
"area": "skills",
"observation": "For TUI work, helper-string tests are not enough; shared guidance should explicitly require a layout-fit check for any new multi-line regions.",
"evidence": "`rust/proxypilot-rs/src/tui.rs:743-840` tests helper string content, but no test or skill step verifies that those lines fit inside the fixed-height footer/runtime blocks (`tui.rs:387-389`, `tui.rs:523-534`). The worker handoff for session `b1f2df79-2748-4890-a74a-2cb5e38b416c` reported footer/help validation even though the clipped layout remained."
}
],
"addressesFailureFrom": null,
"summary": "Fail. The feature and direct `cea7a0f4` follow-up improved runtime visibility and `R` targeting, but the live TUI still hides much of the footer/help copy and its own background polling contaminates the runtime counters and refresh status it displays."
}
@@ -0,0 +1,39 @@
{
"milestone": "terminal-operator-console",
"round": 3,
"status": "pass",
"validatorsRun": {
"format": {
"passed": true,
"command": "cargo fmt --all --manifest-path rust/Cargo.toml -- --check",
"exitCode": 0
},
"test": {
"passed": true,
"command": "cargo test --manifest-path rust/Cargo.toml",
"exitCode": 0
},
"typecheck": {
"passed": true,
"command": "cargo check --manifest-path rust/Cargo.toml",
"exitCode": 0
},
"lint": {
"passed": true,
"command": "cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings",
"exitCode": 0
}
},
"reviewsSummary": {
"total": 1,
"passed": 1,
"failed": 0,
"failedFeatures": []
},
"blockingIssues": [],
"nonBlockingIssues": [],
"appliedUpdates": [],
"suggestedGuidanceUpdates": [],
"rejectedObservations": [],
"previousRound": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/scrutiny/synthesis.round2.json"
}
@@ -0,0 +1,94 @@
{
"milestone": "terminal-operator-console",
"round": 1,
"status": "fail",
"validatorsRun": {
"format": {
"passed": true,
"command": "cargo fmt --all --manifest-path rust/Cargo.toml -- --check",
"exitCode": 0
},
"test": {
"passed": true,
"command": "cargo test --manifest-path rust/Cargo.toml",
"exitCode": 0
},
"typecheck": {
"passed": true,
"command": "cargo check --manifest-path rust/Cargo.toml",
"exitCode": 0
},
"lint": {
"passed": true,
"command": "cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings",
"exitCode": 0
}
},
"reviewsSummary": {
"total": 6,
"passed": 4,
"failed": 2,
"failedFeatures": [
"terminal-tui-operator-console",
"cli-state-runtime-truthfulness-and-docs"
]
},
"blockingIssues": [
{
"featureId": "terminal-tui-operator-console",
"severity": "blocking",
"description": "The TUI footer/help block still uses Constraint::Length(3) while rendering four content lines inside a bordered block, so the selected-account/runtime divergence copy and key help are clipped off-screen. This makes the console fail both the TUI readability contract and the CLI/runtime-truthfulness feature's operator-facing explanation goals."
},
{
"featureId": "terminal-tui-operator-console",
"severity": "blocking",
"description": "The TUI background polling path still calls GET /v1/models whenever /healthz succeeds, which goes through the proxy and mutates total_proxied_requests, successful_upstream_responses, and potentially last_refresh/auth state. Leaving the console open therefore contaminates the runtime stats it is supposed to observe."
}
],
"nonBlockingIssues": [
{
"featureId": "proxy-runtime-stats-surface",
"severity": "non_blocking",
"description": "runtime_stats.account_count currently reports all saved state entries rather than only runtime-usable Codex accounts, so mixed-provider saved state can overstate runtime-usable account count."
}
],
"appliedUpdates": [
{
"target": "services.yaml",
"description": "Added `init`, `typecheck`, and `lint` commands so scrutiny/user-testing workers can bootstrap the Rust slice and run the full validator suite from shared service metadata.",
"sourceFeature": "scrutiny-validator-terminal-operator-console"
},
{
"target": "library",
"description": "Documented in `.factory/library/environment.md` that `./.factory/init.sh` fetches Rust dependencies and creates `rust/proxypilot-rs.toml` when missing before smoke validation.",
"sourceFeature": "shared-auth-runtime-domain"
}
],
"suggestedGuidanceUpdates": [
{
"target": "rust-operator-worker",
"suggestion": "Clarify that mission artifacts may live under `/home/fsos/.factory/missions/<id>` even when repo-relative `.factory/missions/...` paths are absent, and instruct workers to fall back to the absolute mission directory before treating missing files as blockers.",
"evidence": "Review reports for `shared-auth-runtime-domain` and `fix-tui-runtime-truthfulness` both recorded ENOENT attempts against repo-relative mission paths before successfully reading `/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/...`.",
"isSystemic": true
},
{
"target": "rust-operator-worker",
"suggestion": "For TUI features that change visible copy or layout, require a layout-fit visibility check (render/snapshot or terminal smoke) in addition to helper-string assertions so multi-line regions are proven visible.",
"evidence": "Both `terminal-tui-operator-console` and `cli-state-runtime-truthfulness-and-docs` failed review because the footer stayed at `Constraint::Length(3)` while rendering four lines, yet the worker tests only asserted helper-string content.",
"isSystemic": true
},
{
"target": "rust-operator-worker",
"suggestion": "Reinforce the mission boundary that only docs-scoped features may edit `docs/rust-replatform.md` or `README.md`, and direct workers to defer such changes to the explicit docs-alignment feature when possible.",
"evidence": "Review reports for `proxy-runtime-stats-surface` and `terminal-tui-operator-console` both recorded edits to `docs/rust-replatform.md` despite mission AGENTS guidance reserving docs changes for docs-alignment work.",
"isSystemic": true
}
],
"rejectedObservations": [
{
"observation": "Add a dedicated mock upstream/refresh service definition to `.factory/services.yaml` for runtime smoke validation.",
"reason": "ambiguous: the reviewed mock servers were scenario-specific one-off fixtures with ad hoc ports and behavior, so a generic shared service entry would risk encoding the wrong workflow. The concrete bootstrap/config discovery gap was handled instead via additive command/library updates."
}
],
"previousRound": null
}
@@ -0,0 +1,60 @@
{
"milestone": "terminal-operator-console",
"round": 2,
"status": "fail",
"validatorsRun": {
"format": {
"passed": true,
"command": "cargo fmt --all --manifest-path rust/Cargo.toml -- --check",
"exitCode": 0
},
"test": {
"passed": true,
"command": "cargo test --manifest-path rust/Cargo.toml",
"exitCode": 0
},
"typecheck": {
"passed": true,
"command": "cargo check --manifest-path rust/Cargo.toml",
"exitCode": 0
},
"lint": {
"passed": true,
"command": "cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings",
"exitCode": 0
}
},
"reviewsSummary": {
"total": 2,
"passed": 1,
"failed": 1,
"failedFeatures": [
"fix-tui-footer-and-passive-polling"
]
},
"blockingIssues": [
{
"featureId": "fix-tui-footer-and-passive-polling",
"severity": "blocking",
"description": "The TUI footer still has a fixed six-row outer height, which leaves only four inner text rows inside the bordered keys block. Long routine feedback such as the runtime-active-account failure copy wraps at the validated terminal width, so one of the footer status/help lines becomes clipped again and the operator console still fails the footer-visibility contract."
}
],
"nonBlockingIssues": [],
"appliedUpdates": [],
"suggestedGuidanceUpdates": [
{
"target": "rust-operator-worker",
"suggestion": "When validating fixed-height TUI regions, explicitly exercise long wrapped operator feedback (not just short placeholder strings) so layout-fit checks prove realistic messages remain visible.",
"evidence": "The follow-up review for `fix-tui-footer-and-passive-polling` found the new snapshot test only covered a short `Feedback line`, while real footer messages in `rust/proxypilot-rs/src/tui.rs` are long enough to wrap and clip help text again within the fixed six-row footer.",
"isSystemic": true
},
{
"target": "rust-operator-worker",
"suggestion": "Document when label-only TUI/runtime wording changes may use lightweight non-interactive verification instead of full `tuistory` validation, so narrow observability fixes can follow a clear exception path.",
"evidence": "The pass review for `fix-runtime-account-count-semantics` noted the worker intentionally skipped manual `tuistory` verification because the change only aligned runtime/TUI labels, but the current skill does not describe that small-change exception.",
"isSystemic": false
}
],
"rejectedObservations": [],
"previousRound": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/scrutiny/synthesis.round1.json"
}
@@ -0,0 +1,19 @@
active_account = "refreshable-local"
[[accounts]]
name = "refreshable-local"
provider = "codex"
api_key = "refreshed-access-token"
refresh_token = "refreshed-refresh-token"
id_token = "TEST_ID_TOKEN_PLACEHOLDER"
email = "rerun@example.com"
account_id = "acct_refresh_rerun"
plan_type = "pro"
expires_at = "2030-01-01T00:00:00Z"
source = "import:/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/fixtures/cli-refresh-rerun/refreshable-auth.json"
[[accounts]]
name = "static-local"
provider = "codex"
api_key = "sk-static-local"
source = "manual"
@@ -0,0 +1,11 @@
# isolated CLI refresh rerun config
[server]
bind = "127.0.0.1:8318"
[state]
path = "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/fixtures/cli-refresh-rerun/proxypilot-rs.state.toml"
[codex]
upstream_base_url = "https://api.openai.com"
api_key = ""
refresh_token_url = "http://127.0.0.1:34051/oauth/token"
@@ -0,0 +1,8 @@
{
"access_token": "seed-access-token",
"refresh_token": "seed-refresh-token",
"email": "seed@example.com",
"account_id": "acct_seed",
"expired": "2020-01-01T00:00:00Z",
"type": "codex"
}
@@ -0,0 +1,13 @@
active_account = "preemptive-account"
[[accounts]]
name = "preemptive-account"
provider = "codex"
api_key = "expired-preemptive-token"
refresh_token = "preemptive-refresh-token"
id_token = "header.payload.sig"
email = "refresh@example.com"
account_id = "acct_seed"
plan_type = "seed"
expires_at = "1970-01-01T00:00:00Z"
source = "fixture"
@@ -0,0 +1,10 @@
[server]
bind = "127.0.0.1:18318"
[state]
path = "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/fixtures/http-refresh-rerun/VAL-HTTP-007.state.toml"
[codex]
upstream_base_url = "http://127.0.0.1:18319"
api_key = ""
refresh_token_url = "http://127.0.0.1:18319/oauth/token"
@@ -0,0 +1,13 @@
active_account = "retry-account"
[[accounts]]
name = "retry-account"
provider = "codex"
api_key = "stale-401-token"
refresh_token = "retry-refresh-token"
id_token = "header.payload.sig"
email = "refresh@example.com"
account_id = "acct_seed"
plan_type = "seed"
expires_at = "2035-01-01T00:00:00Z"
source = "fixture"
@@ -0,0 +1,10 @@
[server]
bind = "127.0.0.1:18318"
[state]
path = "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/fixtures/http-refresh-rerun/VAL-HTTP-008.state.toml"
[codex]
upstream_base_url = "http://127.0.0.1:18319"
api_key = ""
refresh_token_url = "http://127.0.0.1:18319/oauth/token"
@@ -0,0 +1,142 @@
import argparse
import base64
import json
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import parse_qs
def encode_claims(exp, email, account_id, plan_type):
payload = {
"email": email,
"exp": exp,
"https://api.openai.com/auth": {
"chatgpt_account_id": account_id,
"chatgpt_plan_type": plan_type,
},
}
raw = json.dumps(payload, separators=(",", ":")).encode()
return base64.urlsafe_b64encode(raw).decode().rstrip("=")
class ReusableServer(ThreadingHTTPServer):
allow_reuse_address = True
class Handler(BaseHTTPRequestHandler):
scenario = "preemptive"
log_path = None
state = {"model_calls": 0}
def log_message(self, format, *args):
return
def _write_json(self, status, payload):
body = json.dumps(payload).encode()
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
return body
def _record(self, *, response_status, response_body, request_body=""):
entry = {
"ts": time.time(),
"scenario": self.scenario,
"method": self.command,
"path": self.path,
"headers": {k: v for k, v in self.headers.items()},
"request_body": request_body,
"response_status": response_status,
"response_body": response_body,
"model_call_index": self.state["model_calls"],
}
with open(self.log_path, "a", encoding="utf-8") as fh:
fh.write(json.dumps(entry) + "\n")
def do_GET(self):
if self.path == "/__health":
body = b"ok"
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
return
if self.path == "/v1/models":
auth = self.headers.get("Authorization", "")
self.state["model_calls"] += 1
if self.scenario == "retry401" and self.state["model_calls"] == 1:
payload = {
"error": "expired token",
"received_authorization": auth,
"attempt": 1,
}
self._record(response_status=401, response_body=payload)
self._write_json(401, payload)
return
payload = {
"object": "list",
"data": [{"id": "gpt-5.2-codex", "object": "model"}],
"received_authorization": auth,
"attempt": self.state["model_calls"],
"scenario": self.scenario,
}
self._record(response_status=200, response_body=payload)
self._write_json(200, payload)
return
payload = {"error": "not found", "path": self.path}
self._record(response_status=404, response_body=payload)
self._write_json(404, payload)
def do_POST(self):
length = int(self.headers.get("Content-Length", "0"))
raw_body = self.rfile.read(length).decode()
if self.path == "/oauth/token":
form = parse_qs(raw_body)
refresh_token = form.get("refresh_token", [""])[0]
if self.scenario == "preemptive":
payload = {
"access_token": "preemptive-fresh-token",
"refresh_token": "preemptive-fresh-refresh-token",
"id_token": f"header.{encode_claims(1893456000, 'refresh@example.com', 'acct_preemptive', 'team')}.sig",
"expires_in": 3600,
"seen_refresh_token": refresh_token,
}
else:
payload = {
"access_token": "retry-fresh-token",
"refresh_token": "retry-fresh-refresh-token",
"id_token": f"header.{encode_claims(1893456000, 'refresh@example.com', 'acct_retry401', 'pro')}.sig",
"expires_in": 3600,
"seen_refresh_token": refresh_token,
}
self._record(response_status=200, response_body=payload, request_body=raw_body)
self._write_json(200, payload)
return
payload = {"error": "not found", "path": self.path}
self._record(response_status=404, response_body=payload, request_body=raw_body)
self._write_json(404, payload)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--scenario", required=True, choices=["preemptive", "retry401"])
parser.add_argument("--port", required=True, type=int)
parser.add_argument("--log", required=True)
args = parser.parse_args()
Handler.scenario = args.scenario
Handler.log_path = args.log
Handler.state = {"model_calls": 0}
server = ReusableServer(("127.0.0.1", args.port), Handler)
try:
server.serve_forever()
except KeyboardInterrupt:
pass
finally:
server.server_close()
@@ -0,0 +1,69 @@
{
"groupId": "cli-refresh-rerun",
"testedAt": "2026-04-05T13:32:26Z",
"isolation": {
"surface": "shell",
"fixtureDirectory": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/fixtures/cli-refresh-rerun",
"evidenceDirectory": "/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-refresh-rerun",
"configPath": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/fixtures/cli-refresh-rerun/proxypilot-rs.toml",
"statePath": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/fixtures/cli-refresh-rerun/proxypilot-rs.state.toml",
"refreshTokenUrl": "http://127.0.0.1:34051/oauth/token"
},
"toolsUsed": [
"shell"
],
"assertions": [
{
"id": "VAL-CLI-003",
"title": "Refresh commands handle both refreshable and static accounts clearly",
"status": "pass",
"steps": [
{
"action": "Import a refreshable saved Codex account into isolated state and add a static account",
"expected": "Fixture state contains one active refreshable account and one static account",
"observed": "account list showed `refreshable-local` active with `refresh=refreshable` and `static-local` present with `refresh=static`"
},
{
"action": "Run `account refresh-codex` with no `--name` while the refreshable account is active",
"expected": "Refresh succeeds through the configured local refresh endpoint and updates saved account metadata",
"observed": "Command printed `refreshed saved Codex account `refreshable-local` in local state`; the local stub received one POST to `/oauth/token`; state updated access token, refresh token, email, account id, plan, and expiry"
},
{
"action": "Run `account refresh-codex --name static-local`",
"expected": "Static account refresh is rejected with a clear operator-readable error and no state corruption",
"observed": "Command exited non-zero with `account `static-local` is static and has no refresh token`; follow-up state snapshot matched the post-success state byte-for-byte"
},
{
"action": "Run `account refresh-codex --name missing-local`",
"expected": "Missing account refresh is rejected with a clear operator-readable error and no state corruption",
"observed": "Command exited non-zero with `no saved Codex account named missing-local`; follow-up state snapshot remained unchanged and list output still showed the same active/static accounts"
}
],
"evidence": {
"commandOutputs": [
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-import-refreshable.txt",
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-add-static.txt",
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-list-before-refresh.txt",
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-refresh-success.txt",
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-list-after-success.txt",
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-refresh-static-fails.txt",
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-refresh-missing-fails.txt",
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-list-after-failures.txt"
],
"stateSnapshots": [
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-state-before-refresh.toml",
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-state-after-success.toml",
"terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-state-after-failures.toml"
],
"network": "terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-refresh-stub-requests.json",
"config": "terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-config.toml",
"fixtureImport": "terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-import-auth.json",
"validator": "terminal-operator-console/cli-refresh-rerun/VAL-CLI-003-refresh-related-tests.txt"
},
"issues": null
}
],
"frictions": [],
"blockers": [],
"summary": "VAL-CLI-003 passed: refresh succeeded for the active refreshable Codex account through `codex.refresh_token_url`, static and missing account refreshes failed clearly, and saved state stayed coherent after all runs."
}
@@ -0,0 +1,60 @@
{
"groupId": "cli-shell",
"surface": "shell",
"toolsUsed": [
"shell"
],
"assertions": [
{
"id": "VAL-CLI-001",
"status": "pass",
"summary": "init wrote an example config at the requested fixture path and a second run without --force refused to overwrite it with a clear error.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-001-init-success.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-001-init-refused-overwrite.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-001-config.toml"
],
"details": "Used isolated fixture config /home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/fixtures/cli-shell/20260405T121714Z/proxypilot-rs.toml. Successful init printed the written path. Re-running init returned exit code 1 with \"config already exists ... (pass --force to overwrite)\". The captured config file includes server.bind=127.0.0.1:8318, state.path=proxypilot-rs.state.toml, and codex.upstream_base_url=https://api.openai.com."
},
{
"id": "VAL-CLI-002",
"status": "pass",
"summary": "Add, list, activate, and remove commands kept saved active-account state coherent, including promotion back to the remaining account and explicit empty-state output after removing the final account.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-add-primary.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-add-backup.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-list-after-add.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-activate-backup.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-list-after-activate.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-remove-backup.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-list-after-remove-backup.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-remove-primary.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-list-after-remove-primary.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-state-after-activate-backup.toml",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-state-after-remove-backup.toml",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-002-state-after-remove-primary.toml"
],
"details": "primary became active when first added; list output marked it with `*`. After activating backup, list output switched the marker to backup and the saved state snapshot shows active_account=\"backup\". Removing backup reported \"local active account is now `primary`\" and the next state snapshot shows active_account=\"primary\". Removing primary reported \"no local active account remains\" and follow-up list output showed \"No local Rust accounts saved yet.\" with an empty accounts snapshot."
},
{
"id": "VAL-CLI-003",
"status": "blocked",
"summary": "Negative refresh paths behaved clearly for static and missing accounts, but the required refresh-success path could not be exercised within the isolated fixture because refresh-codex is hardwired to the live OpenAI OAuth refresh endpoint.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-003-state-with-static-and-refreshable.toml",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-003-list-pre-refresh.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-003-refresh-static-fails.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-003-refresh-missing-fails.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-003-list-after-failures.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-003-state-after-failures.toml",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/cli-shell/20260405T121714Z/VAL-CLI-003-refresh-success-blocked.txt"
],
"details": "Imported a refreshable local fixture account and added a static account inside the assigned fixture state. Refreshing the static account failed cleanly with \"account `static-local` is static and has no refresh token\"; refreshing a missing account failed with \"no saved Codex account named missing-local\". Follow-up list/state evidence shows saved state remained unchanged after both failures. The assertion remains blocked because the CLI refresh path uses the built-in OpenAI OAuth refresh endpoint and the supported config surface does not expose a way to redirect refresh-codex to a local mock endpoint without live credentials."
}
],
"frictions": [],
"blockers": [
"VAL-CLI-003 success-path refresh is not isolatable in this shell fixture: refresh-codex uses the built-in live OpenAI OAuth refresh endpoint, and the allowed CLI/config surface provides no local mock override. Live device-login/private credentials were explicitly disallowed, so the refresh-success portion of the assertion was recorded as blocked with evidence."
],
"overallSummary": "Validated 3 CLI shell assertions for terminal-operator-console: VAL-CLI-001 pass, VAL-CLI-002 pass, VAL-CLI-003 blocked due to non-mockable live refresh dependency. Report and evidence were written under the requested flow-report and mission evidence paths."
}
@@ -0,0 +1,84 @@
{
"groupId": "http-refresh-rerun",
"testedAt": "2026-04-05T13:34:49Z",
"isolation": {
"surface": "curl",
"fixtureDirectory": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/fixtures/http-refresh-rerun",
"evidenceDirectory": "/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-refresh-rerun",
"proxyBind": "127.0.0.1:18318",
"stubBind": "127.0.0.1:18319",
"refreshTokenUrl": "http://127.0.0.1:18319/oauth/token"
},
"toolsUsed": [
"curl"
],
"assertions": [
{
"id": "VAL-HTTP-007",
"title": "Proactive refresh is visible before forwarding expired or near-expiry requests",
"status": "pass",
"steps": [
{
"action": "Start proxypilot-rs with an expired refreshable active account plus a local OAuth/upstream stub",
"expected": "Runtime stats start with expired auth health and zero counters",
"observed": "/v0/runtime/stats reported auth_health.state=expired with total_proxied_requests=0 and auth_refresh_attempts=0"
},
{
"action": "Send GET /v1/models through the proxy",
"expected": "Proxy refreshes before forwarding and the client request succeeds with the refreshed token",
"observed": "Client received 200 JSON showing received_authorization=Bearer preemptive-fresh-token"
},
{
"action": "Inspect runtime stats and stub traffic after the request",
"expected": "Stats show one refresh attempt/success and upstream sees only refreshed-token traffic",
"observed": "Stats showed total_proxied_requests=1, successful_upstream_responses=1, auth_refresh_attempts=1, upstream_401_count=0, last_refresh.kind=success; stub log recorded POST /oauth/token before GET /v1/models with Bearer preemptive-fresh-token"
}
],
"evidence": {
"statsBefore": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-007-stats-before.json",
"requestHeaders": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-007-request.headers.txt",
"requestBody": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-007-request-body.json",
"statsAfter": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-007-stats-after.json",
"stubLog": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-007-stub-log.jsonl",
"proxyLog": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-007-proxy.log",
"stubStdout": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-007-stub.log"
},
"issues": null
},
{
"id": "VAL-HTTP-008",
"title": "Upstream 401 retry refresh is visible and counted exactly once",
"status": "pass",
"steps": [
{
"action": "Start proxypilot-rs with a refreshable active account and a local stub that returns 401 on the first /v1/models request",
"expected": "Initial stats are zeroed and the first upstream attempt uses the stale token",
"observed": "Stub log recorded the first GET /v1/models with Bearer stale-401-token and response_status=401"
},
{
"action": "Let the proxy handle the 401-triggered refresh-and-retry path for one GET /v1/models client request",
"expected": "Proxy refreshes once, retries once, and the client sees the retried success result",
"observed": "Client received 200 JSON showing received_authorization=Bearer retry-fresh-token on attempt=2"
},
{
"action": "Inspect runtime stats and stub traffic after the request",
"expected": "Stats count one proxied request, one upstream 401, one refresh success, and mock upstream shows 401 then refresh then retry",
"observed": "Stats showed total_proxied_requests=1, successful_upstream_responses=1, auth_refresh_attempts=1, upstream_401_count=1, last_refresh.kind=success; stub log showed GET 401 -> POST /oauth/token -> GET 200 with Bearer retry-fresh-token"
}
],
"evidence": {
"statsBefore": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-008-stats-before.json",
"requestHeaders": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-008-request.headers.txt",
"requestBody": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-008-request-body.json",
"statsAfter": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-008-stats-after.json",
"stubLog": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-008-stub-log.jsonl",
"proxyLog": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-008-proxy.log",
"stubStdout": "terminal-operator-console/http-refresh-rerun/VAL-HTTP-008-stub.log"
},
"issues": null
}
],
"frictions": [],
"blockers": [],
"summary": "Retested 2 assertions: VAL-HTTP-007 passed with proactive refresh before forwarding, and VAL-HTTP-008 passed with one 401-triggered refresh-and-retry path counted exactly once."
}
@@ -0,0 +1,133 @@
{
"assertions": [
{
"details": "Observed status=ok, provider=codex, listen=127.0.0.1:18318, upstream=http://127.0.0.1:18319 on the reachable fixture and the same local health response with upstream=http://127.0.0.1:18339.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-001-healthz-reachable.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-001-healthz-unreachable.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/fixture-config-fallback.toml",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/fixture-config-unreachable.toml"
],
"id": "VAL-HTTP-001",
"status": "pass",
"summary": "Health checks returned 200 with local proxy identity fields both with the mock upstream and with an unreachable local upstream port."
},
{
"details": "The stats call returned HTTP 200 with a JSON payload and the mock log remained empty (request_count=0), confirming the operator surface is read-only and local.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-002-runtime-stats.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-002-mock-log.json"
],
"id": "VAL-HTTP-002",
"status": "pass",
"summary": "The local /v0/runtime/stats surface returned 200 without auth and did not create any upstream mock traffic."
},
{
"details": "Before and after mutating the state file on disk, /v0/runtime/stats continued to report active_account_name=runtime-valid and auth_health.state=valid for the running process while exposing bind_address, upstream_base_url, account_count, auth_health, request_counters, and last_refresh.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-003-stats-before-disk-mutation.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-003-stats-after-disk-mutation.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-003-state-before.toml",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-003-state-mutated.toml"
],
"id": "VAL-HTTP-003",
"status": "pass",
"summary": "The runtime stats payload exposed the expected stable schema and stayed pinned to runtime-memory state after the disk state file was mutated externally."
},
{
"details": "Observed auth_health.state values: fallback-key -> static with source=config_fallback_key and no active_account_name; static saved account -> static with source=active_account; valid fixture -> valid; expiring fixture -> expiring_soon; expired fixture -> expired; malformed expiry fixture -> unknown with metadata_state=malformed.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-004-fallback-key.json",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-004-static-saved.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-004-valid.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-004-expiring.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-004-expired.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-004-malformed.txt"
],
"id": "VAL-HTTP-004",
"status": "pass",
"summary": "Representative fixtures produced explicit auth-health states for fallback static, saved static, valid, expiring_soon, expired, and malformed unknown cases."
},
{
"details": "Initial stats showed all request counters at 0 and last_refresh.kind=unknown with null timestamps; after two successful requests the counters rose; after restarting the proxy with the same fixture they reset to zero again.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-002-runtime-stats.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-006-stats-after-success.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-005-stats-after-restart.txt"
],
"id": "VAL-HTTP-005",
"status": "pass",
"summary": "Fresh runtime counters started at zero, last-refresh metadata was null-like before any attempt, and a restart cleared in-memory stats after traffic."
},
{
"details": "Starting from zero, one GET /v1/models and one POST /v1/chat/completions produced total_proxied_requests=2 and successful_upstream_responses=2, with two corresponding mock upstream calls.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-006-models-success.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-006-chat-success.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-006-stats-after-success.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-006-mock-log.json"
],
"id": "VAL-HTTP-006",
"status": "pass",
"summary": "Successful supported proxy requests incremented both total_proxied_requests and successful_upstream_responses."
},
{
"details": "With an expired refreshable account, the next proxied request returned 502, auth_refresh_attempts/auth_refresh_failures incremented, last_refresh.kind became failure, and the local upstream mock saw zero calls. This confirms the pre-forward refresh attempt, but successful local stubbed refresh remains blocked because the available config surface has no OAuth token endpoint override.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-007-stats-before.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-007-proactive-refresh-attempt.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-007-stats-after.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-007-mock-log.json",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/fixture-config-expired.toml"
],
"id": "VAL-HTTP-007",
"status": "blocked",
"summary": "A proactive refresh attempt was observable before forwarding, but the success path could not be validated because the runtime cannot be redirected from the user surface to a local OAuth token stub."
},
{
"details": "The local mock forced one upstream 401 and the proxy recorded upstream_401_count=1 with a single proxied request, but the refresh attempt failed with a 502 response and the mock saw no /oauth/token call. A successful refresh-and-retry path therefore remains blocked under the current user-surface configuration.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-008-009-stats-before.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-008-009-401-refresh-attempt.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-008-009-stats-after.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-008-009-mock-log.json",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/fixture-config-401-retry.toml"
],
"id": "VAL-HTTP-008",
"status": "blocked",
"summary": "The 401-triggered refresh path is visible, but the success retry case cannot be validated locally because the runtime cannot point refresh traffic at a local OAuth token stub from the user surface."
},
{
"details": "The client received HTTP 502 with a local JSON error after the mock upstream returned 401, while /v0/runtime/stats showed total_proxied_requests=1, upstream_401_count=1, auth_refresh_attempts=1, auth_refresh_failures=1, and last_refresh.kind=failure with a failure timestamp/message.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-008-009-401-refresh-attempt.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-008-009-stats-after.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-008-009-mock-log.json"
],
"id": "VAL-HTTP-009",
"status": "pass",
"summary": "A 401-triggered refresh failure remained visible to the operator surface and did not masquerade as success."
},
{
"details": "POST /v1/files returned HTTP 404 with an error naming /v1/files and the supported routes list; successful_upstream_responses and total_proxied_requests stayed unchanged across the before/after stats captures.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-010-stats-before.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-010-unsupported-route.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/http-runtime/val-http-010-stats-after.txt"
],
"id": "VAL-HTTP-010",
"status": "pass",
"summary": "Unsupported /v1/* routes returned a clear 404 JSON error and did not change success counters."
}
],
"blockers": [
"VAL-HTTP-007 and VAL-HTTP-008 remain blocked for success-path validation because proxypilot-rs exposes no user-surface config knob to redirect refresh token exchange from the hardwired OAuth endpoint to a local stub."
],
"frictions": [],
"groupId": "http-runtime",
"overallSummary": "Validated 10 HTTP runtime assertions: 8 passed and 2 were blocked (refresh-success paths VAL-HTTP-007 and VAL-HTTP-008).",
"surface": "curl",
"toolsUsed": [
"curl"
]
}
@@ -0,0 +1,188 @@
{
"groupId": "tui-console",
"surface": "tuistory",
"toolsUsed": [
"tuistory",
"shell",
"curl"
],
"assertions": [
{
"id": "VAL-TUI-001",
"status": "pass",
"summary": "The TUI opened as a readable Codex operator console with overview, runtime, runtime-stats, models, accounts, selected-account, feedback, and key-help regions visible.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-initial.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-initial.png"
],
"details": "Launching `cargo run --manifest-path rust/Cargo.toml -p proxypilot-rs -- tui --config .../config-live.toml` rendered the Codex Operator Console immediately. The initial surface showed readable runtime health, live runtime stats, a disk-backed account list, selected-account details, and footer/help text rather than a blank or cryptic screen."
},
{
"id": "VAL-TUI-002",
"status": "pass",
"summary": "The footer/help text matched the shipped key bindings, and each listed action was exercised through the TUI during validation.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-initial.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-activate-alpha.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-delete-beta.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-clear-feedback.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-trouble-after-q-snapshot.txt"
],
"details": "Visible help text listed `q`, `r`, arrows, `a`, `f`, `R`, `d`, and `c`. Those bindings were exercised across the live, runtime-down, and auth-trouble sessions: reload/poll updated state, arrows changed selection, activate/delete changed local state, selected refresh and active refresh produced targeted feedback, clear removed feedback, and `q` exited the TUI process back to the launcher shell."
},
{
"id": "VAL-TUI-003",
"status": "pass",
"summary": "With no proxy running on the configured port, the TUI explicitly showed runtime unavailability while keeping the local disk-backed account list and selected-account details visible.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-initial.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-initial.png"
],
"details": "Using the isolated runtime-down fixture on 127.0.0.1:18430 without starting a proxy produced `Health: proxy not running yet` and `Runtime unavailable` copy, while still rendering six saved accounts and full selected-account details from the local fixture state file."
},
{
"id": "VAL-TUI-004",
"status": "pass",
"summary": "Pressing `r` refreshed both disk-backed state and runtime-observed data without restarting the TUI.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-initial.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/cli-activate-beta.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-cli-activate-reload.txt"
],
"details": "After changing the saved active account externally through the CLI and generating runtime traffic through `/v1/models`, pressing `r` in the live TUI updated the disk active account from `alpha` to `beta`, loaded the model list, and refreshed runtime counters to `total=1 success=1` while still truthfully showing the runtime active account as `alpha`."
},
{
"id": "VAL-TUI-005",
"status": "pass",
"summary": "The activate action updated active markers, selected-account details, and feedback coherently.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-before-activate.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-activate-alpha.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-activate-alpha.png"
],
"details": "Before pressing `a`, the selected row was `alpha` while the disk active marker was on `beta`. After `a`, the footer reported `Activated account \\`alpha\\`.` and the active marker moved to `alpha` without corrupting selection or the selected-account panel."
},
{
"id": "VAL-TUI-006",
"status": "pass",
"summary": "The delete action removed the selected account, kept selection in bounds, and updated visible account state coherently.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-clear-feedback.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-delete-beta.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-delete-beta.png"
],
"details": "With `beta` selected, pressing `d` removed that row, reduced the saved-account count from two to one, moved selection to the remaining `alpha` row, and showed `Deleted account \\`beta\\`.` in the footer without leaving stale details on screen."
},
{
"id": "VAL-TUI-007",
"status": "pass",
"summary": "Selected-account refresh targeted the selected row and named that account clearly in feedback.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-select-beta.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-refresh-selected-beta.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-after-refresh-selected-soon.txt"
],
"details": "On the live fixture, `beta` was selected while `alpha` remained active; pressing `f` produced `Failed to refresh account \\`beta\\`: account \\`beta\\` is static and has no refresh token`. In the runtime-down fixture, selecting `soon` and pressing `f` produced feedback naming `soon`, confirming the action targets the selected row rather than the active row."
},
{
"id": "VAL-TUI-008",
"status": "pass",
"summary": "Active-account refresh-now targeted the active account even when another row was selected.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-select-beta.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-refresh-active-alpha.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-select-valid-while-active-soon.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-after-refresh-active-runtime-down.txt"
],
"details": "In the live fixture, `beta` was selected while `alpha` was the active disk/runtime account; pressing `R` produced feedback naming `alpha`, not `beta`. In the runtime-down fixture, `valid` was selected while disk active remained `soon`, and `R` reported `Disk active account remains \\`soon\\``, again proving the action targeted the active account rather than the selected row."
},
{
"id": "VAL-TUI-009",
"status": "pass",
"summary": "Clear-feedback removed transient status text without altering selection or account state.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-refresh-active-alpha.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-clear-feedback.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-after-clear-feedback.txt"
],
"details": "After visible failure feedback in both live and runtime-down sessions, pressing `c` cleared the footer message back to blank while keeping the selected row, disk active account, runtime panel, and counters unchanged."
},
{
"id": "VAL-TUI-010",
"status": "pass",
"summary": "The TUI used explicit operator-readable auth labels for valid, expiring soon, expired, static, and unknown, with distinct missing-vs-malformed expiry details.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-labels-valid.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-labels-expiring-soon.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-labels-expired.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-labels-static.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-labels-unknown-missing.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-labels-unknown-malformed.txt"
],
"details": "A dedicated auth-label fixture showed `Auth health: valid`, `expiring soon`, `expired`, `static`, and `unknown` in the selected-account panel. The `unknown-missing` account showed `Expiry: expiry metadata missing`, while `unknown-malformed` showed `Expiry: malformed expiry metadata (not-a-timestamp)`, which truthfully distinguishes the two unknown states."
},
{
"id": "VAL-TUI-011",
"status": "pass",
"summary": "When runtime stats were available, the TUI surfaced request counters and the last refresh outcome/timestamp in the runtime panel.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-initial.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-cli-activate-reload.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-trouble-after-reload.txt"
],
"details": "The idle live fixture showed zeroed counters and `Last refresh: unknown`. After proxied traffic, the live runtime panel updated to `total=1 success=1`. After a proxy-triggered refresh failure, the auth-trouble TUI showed `refresh_attempts=1 refresh_failures=1` and `Last refresh: failure for \\`refresh-fail\\` at ...`, making runtime refresh outcome visible without reading files or logs."
},
{
"id": "VAL-TUI-012",
"status": "pass",
"summary": "When runtime was down, local disk-backed actions still worked where feasible and runtime-dependent refresh paths failed with readable copy while local state stayed visible.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-after-activate-soon.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-after-refresh-selected-soon.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-after-refresh-active-runtime-down.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-after-delete-valid.txt"
],
"details": "With runtime unavailable, pressing `a` still updated the local disk active account from `valid` to `soon`; `f` failed with readable refresh-token-exchange copy naming the selected account; `R` failed with explicit `runtime stats unavailable` copy while preserving the disk active account label; and `d` still deleted a locally saved account without hiding the local account list."
},
{
"id": "VAL-CROSS-001",
"status": "pass",
"summary": "CLI active-account changes became visible in the TUI after reload while runtime state remained truthfully separate.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/cli-activate-beta.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-cli-activate-reload.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/cli-remove-alpha.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/live-after-cli-remove-alpha-reload.txt"
],
"details": "After CLI activation changed the saved active account to `beta`, TUI reload showed `Local disk active account: beta` while still showing `Runtime active account: alpha`. After CLI removal of `alpha`, reload again showed the saved state change (`beta` on disk, one saved account left) while runtime stats still truthfully reported the still-running proxy's runtime state as `alpha`."
},
{
"id": "VAL-CROSS-002",
"status": "pass",
"summary": "A proxy-triggered refresh attempt became visible across runtime surfaces without reading the state file manually.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-trouble-proxy-request.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-trouble-stats-after.json",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/auth-trouble-after-reload.txt"
],
"details": "A proxied `GET /v1/models` against the auth-trouble fixture triggered an expired-account refresh attempt and returned a local 502. `/v0/runtime/stats` then showed `auth_refresh_attempts=1`, `auth_refresh_failures=1`, and a failed `last_refresh`, and the auth-trouble TUI showed the same failed refresh outcome in its runtime panel after reload."
},
{
"id": "VAL-CROSS-003",
"status": "pass",
"summary": "When runtime was down, reload and feedback clearing preserved local state while keeping runtime failures separate from local actions.",
"evidence": [
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-after-refresh-active-runtime-down.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-after-clear-feedback.txt",
"/home/fsos/.factory/missions/7e6968f8-b3d3-4772-9518-ca3a93ea1374/evidence/terminal-operator-console/tui-console/runtime-down-clean-after-reload.txt"
],
"details": "In the runtime-down fixture, local actions changed disk state, `R` surfaced runtime unavailability separately from that local state, `c` cleared only the transient footer message, and `r` reloaded the current disk-backed account list without hiding local accounts or collapsing the separate `Runtime unavailable` messaging."
}
],
"frictions": [
"tuistory keeps the PTY session alive after `q` exits the TUI binary, so explicit `tuistory close` cleanup was still required even though the application quit successfully.",
"With runtime down, `f` still attempts the hardwired Codex OAuth refresh exchange and surfaces the resulting OAuth 401 text instead of a pure local runtime-unavailable message; this was truthful but not called out in user-testing guidance."
],
"blockers": [],
"overallSummary": "Validated 15 assigned TUI/cross-surface assertions for terminal-operator-console: 15 passed, 0 failed, 0 blocked. Evidence and the flow report were written under the requested mission and validation paths."
}
@@ -0,0 +1,20 @@
{
"milestone": "terminal-operator-console",
"round": 2,
"status": "pass",
"assertionsSummary": {
"total": 3,
"passed": 3,
"failed": 0,
"blocked": 0
},
"passedAssertions": [
"VAL-CLI-003",
"VAL-HTTP-007",
"VAL-HTTP-008"
],
"failedAssertions": [],
"blockedAssertions": [],
"appliedUpdates": [],
"previousRound": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/synthesis.round1.json"
}
@@ -0,0 +1,66 @@
{
"milestone": "terminal-operator-console",
"round": 1,
"status": "fail",
"assertionsSummary": {
"total": 28,
"passed": 25,
"failed": 0,
"blocked": 3
},
"passedAssertions": [
"VAL-CLI-001",
"VAL-CLI-002",
"VAL-HTTP-001",
"VAL-HTTP-002",
"VAL-HTTP-003",
"VAL-HTTP-004",
"VAL-HTTP-005",
"VAL-HTTP-006",
"VAL-HTTP-009",
"VAL-HTTP-010",
"VAL-TUI-001",
"VAL-TUI-002",
"VAL-TUI-003",
"VAL-TUI-004",
"VAL-TUI-005",
"VAL-TUI-006",
"VAL-TUI-007",
"VAL-TUI-008",
"VAL-TUI-009",
"VAL-TUI-010",
"VAL-TUI-011",
"VAL-TUI-012",
"VAL-CROSS-001",
"VAL-CROSS-002",
"VAL-CROSS-003"
],
"failedAssertions": [],
"blockedAssertions": [
{
"id": "VAL-CLI-003",
"blockedBy": "Negative refresh paths behaved clearly for static and missing accounts, but the required refresh-success path could not be exercised within the isolated fixture because refresh-codex is hardwired to the live OpenAI OAuth refresh endpoint."
},
{
"id": "VAL-HTTP-007",
"blockedBy": "A proactive refresh attempt was observable before forwarding, but the success path could not be validated because the runtime cannot be redirected from the user surface to a local OAuth token stub."
},
{
"id": "VAL-HTTP-008",
"blockedBy": "The 401-triggered refresh path is visible, but the success retry case cannot be validated locally because the runtime cannot point refresh traffic at a local OAuth token stub from the user surface."
}
],
"appliedUpdates": [
{
"target": "user-testing.md",
"description": "Added flow validator guidance for shell, curl, and tuistory surfaces plus isolated fixture/evidence boundaries for terminal-operator-console validation.",
"source": "setup"
},
{
"target": "user-testing.md",
"description": "Documented discovered validation quirks: hardwired OAuth refresh paths block local success-path refresh testing, tuistory PTYs need explicit close, and runtime-down selected refresh still surfaces remote OAuth failure text.",
"source": "flow-report"
}
],
"previousRound": null
}
@@ -0,0 +1,20 @@
{
"milestone": "terminal-operator-console",
"round": 2,
"status": "pass",
"assertionsSummary": {
"total": 3,
"passed": 3,
"failed": 0,
"blocked": 0
},
"passedAssertions": [
"VAL-CLI-003",
"VAL-HTTP-007",
"VAL-HTTP-008"
],
"failedAssertions": [],
"blockedAssertions": [],
"appliedUpdates": [],
"previousRound": "/home/fsos/code-lean/ai-tools/ProxyPilot/.factory/validation/terminal-operator-console/user-testing/synthesis.round1.json"
}
+8
View File
@@ -49,6 +49,7 @@ docs/*
!docs/long-context-features.md
!docs/desktop-app-build-process.md
!docs/cli.md
!docs/rust-replatform.md
AGENTS.md
CLAUDE.md
GEMINI.md
@@ -75,6 +76,7 @@ claude-progress.txt
feature_list.json
tui-snapshot*.txt
tui-ttyd.png
rust/target/
# Local settings (user-specific)
*.local.json
@@ -119,3 +121,9 @@ config-debug.yaml
test_results.txt
test_results_full.txt
tmpclaude-*
# Opencode
.beads/
.opencode/
.cli-proxy-api/
.venv/
+4
View File
@@ -19,6 +19,10 @@
Built in Go, it handles OAuth authentication, token management, and API translation automatically. One server to route them all.
</p>
<p align="center">
An experimental Rust replatform now lives on the <code>rust-replatform</code> branch with a terminal-first <code>proxypilot-rs</code> binary under <code>rust/</code>.
</p>
---
> [!TIP]
+1 -1
View File
@@ -1 +1 @@
v6.9.12
v6.9.16
+2 -1
View File
@@ -26,6 +26,7 @@ import (
"time"
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
"github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
sdkauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
"github.com/router-for-me/CLIProxyAPI/v6/sdk/proxyutil"
@@ -188,7 +189,7 @@ func fetchModels(ctx context.Context, auth *coreauth.Auth) []modelEntry {
httpReq.Close = true
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+accessToken)
httpReq.Header.Set("User-Agent", "antigravity/1.19.6 darwin/arm64")
httpReq.Header.Set("User-Agent", misc.AntigravityUserAgent())
httpClient := &http.Client{Timeout: 30 * time.Second}
if transport, _, errProxy := proxyutil.BuildHTTPTransport(auth.ProxyURL); errProxy == nil && transport != nil {
+10 -2
View File
@@ -106,6 +106,7 @@ func main() {
var switchMode string
var projectID string
var vertexImport string
var vertexImportPrefix string
var configPath string
var password string
var noIncognito bool
@@ -181,6 +182,7 @@ func main() {
flag.StringVar(&projectID, "project_id", "", "Project ID (Gemini only, not required)")
flag.StringVar(&configPath, "config", DefaultConfigPath, "Configure File Path")
flag.StringVar(&vertexImport, "vertex-import", "", "Import Vertex service account key JSON file")
flag.StringVar(&vertexImportPrefix, "vertex-import-prefix", "", "Prefix for Vertex model namespacing (use with -vertex-import)")
flag.StringVar(&password, "password", "", "")
flag.BoolVar(&launchTUI, "tui", false, "Start with terminal management UI")
flag.BoolVar(&standalone, "standalone", false, "In TUI mode, start an embedded local server")
@@ -310,6 +312,7 @@ func main() {
gitStoreRemoteURL string
gitStoreUser string
gitStorePassword string
gitStoreBranch string
gitStoreLocalPath string
gitStoreInst *store.GitTokenStore
gitStoreRoot string
@@ -379,6 +382,9 @@ func main() {
if value, ok := lookupEnv("GITSTORE_LOCAL_PATH", "gitstore_local_path"); ok {
gitStoreLocalPath = value
}
if value, ok := lookupEnv("GITSTORE_GIT_BRANCH", "gitstore_git_branch"); ok {
gitStoreBranch = value
}
if value, ok := lookupEnv("OBJECTSTORE_ENDPOINT", "objectstore_endpoint"); ok {
useObjectStore = true
objectStoreEndpoint = value
@@ -513,7 +519,7 @@ func main() {
}
gitStoreRoot = filepath.Join(gitStoreLocalPath, "gitstore")
authDir := filepath.Join(gitStoreRoot, "auths")
gitStoreInst = store.NewGitTokenStore(gitStoreRemoteURL, gitStoreUser, gitStorePassword)
gitStoreInst = store.NewGitTokenStore(gitStoreRemoteURL, gitStoreUser, gitStorePassword, gitStoreBranch)
gitStoreInst.SetBaseDir(authDir)
if errRepo := gitStoreInst.EnsureRepository(); errRepo != nil {
log.Errorf("failed to prepare git token store: %v", errRepo)
@@ -662,7 +668,7 @@ func main() {
if vertexImport != "" {
// Handle Vertex service account import
cmd.DoVertexImport(cfg, vertexImport)
cmd.DoVertexImport(cfg, vertexImport, vertexImportPrefix)
} else if showVersion {
// Version already printed at startup, just exit
return
@@ -846,6 +852,7 @@ func main() {
if standalone {
// Standalone mode: start an embedded local server and connect TUI client to it.
managementasset.StartAutoUpdater(context.Background(), configFilePath)
misc.StartAntigravityVersionUpdater(context.Background())
if !localModel {
registry.StartModelsUpdater(context.Background())
}
@@ -915,6 +922,7 @@ func main() {
}
} else {
managementasset.StartAutoUpdater(context.Background(), configFilePath)
misc.StartAntigravityVersionUpdater(context.Background())
if !localModel {
registry.StartModelsUpdater(context.Background())
}
+3
View File
@@ -87,6 +87,9 @@ max-retry-credentials: 0
# Maximum wait time in seconds for a cooled-down credential before triggering a retry.
max-retry-interval: 30
# When true, disable auth/model cooldown scheduling globally (prevents blackout windows after failure states).
disable-cooling: false
# Quota exceeded behavior
quota-exceeded:
switch-project: true # Whether to automatically switch to another project when a quota is exceeded
+54
View File
@@ -0,0 +1,54 @@
# ProxyPilot Rust Replatform
This branch carries the Rust rewrite line for ProxyPilot.
## Branch contract
- Go `main` remains the shipping line while Rust grows.
- Rust lives under [`rust/`](/home/fsos/code-lean/ai-tools/ProxyPilot/rust).
- The Rust line is terminal-first and does not treat the browser dashboard as a required early target.
- The first real milestone is a working Rust executable that can proxy a Codex-compatible request end-to-end.
## Current milestone
`proxypilot-rs` is the first Rust binary in the repo. It currently provides:
- a `run` command that starts a local proxy server
- a `tui` command that opens a minimal terminal operator view
- a TOML config model plus a separate local state file for saved accounts
- explicit Codex-facing routes for `/v1/models`, `/v1/chat/completions`, and `/v1/responses`
- a local `/v0/runtime/stats` operator surface for runtime-memory counters, auth-health, and last refresh status
- CLI account commands for adding, importing, device-login, refresh, listing, activating, and removing Codex accounts; these manage saved disk state only and do not pretend a running proxy has already adopted the change
- optional `codex.refresh_token_url` config support for redirecting Codex refresh exchanges to a local stub during validation, while preserving the live OpenAI default when unset
- a TUI operator console that shows live runtime stats, runtime-unavailable copy, local accounts, selected-account details, token state labels, terminal actions for activate/refresh/delete, separate selected-account vs active-account refresh handling, and the `r` / `c` reload-clear controls
- runtime credential resolution that prefers the active saved account over the config fallback key
- proactive Codex token refresh when the active saved account is already expired or within the refresh window, plus the existing 401-retry refresh fallback
- end-to-end tests against a mocked upstream server
## Design defaults
- Go is the behavioral reference, not a line-by-line port target.
- The Rust branch is allowed to redesign config and management surfaces.
- The product identity does not change: ProxyPilot still exists to be a local proxy for coding tools.
## Local workflow
From the repo root:
```bash
cd rust
cargo run -p proxypilot-rs -- init
cargo run -p proxypilot-rs -- account add-codex --name primary --api-key sk-...
cargo run -p proxypilot-rs -- account import-codex --file ../auths/codex-example.json --activate
cargo run -p proxypilot-rs -- account login-codex-device --activate
cargo run -p proxypilot-rs -- account refresh-codex
cargo run -p proxypilot-rs -- account remove --name old-account
cargo run -p proxypilot-rs -- run --config proxypilot-rs.toml
```
Open the TUI with:
```bash
cd rust
cargo run -p proxypilot-rs -- tui --config proxypilot-rs.toml
```
+28
View File
@@ -0,0 +1,28 @@
# Session Progress - 2026-04-05
## Summary
Merged upstream CLIProxyAPI release `v6.9.15` into ProxyPilot on a dedicated sync branch based on the latest `origin/main`.
Kept the local management-asset security fix from `origin/main`, preserved ProxyPilot branding and local/manual release workflow choices, and absorbed the upstream delta from `v6.9.13` through `v6.9.15`.
## Upstream Releases Included
- `v6.9.13` - vertex credential namespacing support, websocket custom tool-call repair, and model catalog refreshes
- `v6.9.14` - antigravity version refresh updates and additional upstream maintenance changes
- `v6.9.15` - API-key proxy resolution improvements and stricter Qwen system-message handling
## Conflict Resolution Notes
- Kept the ProxyPilot README branding and continued to omit upstream `README_CN.md` and `README_JA.md` revivals because this fork does not ship those translated files.
- Kept the existing local/manual release workflow and combined the fork's broader `.gitignore` with upstream's additional local-tool entries.
- Preserved the local `internal/managementasset/updater.go` security behavior that removes the unverified fallback download path while accepting the rest of the upstream sync.
- Merged `cmd/server/main.go` semantically so ProxyPilot keeps its wider CLI surface while also adding upstream `--vertex-import-prefix` support.
## Repo State Updates
- Updated `UPSTREAM_VERSION` to `v6.9.15`.
- Updated `features.json` upstream marker to `CLIProxyAPI v6.9.15`.
- Prepared the repo for the next clean release tag: `v0.3.0-dev-0.51`.
## Pending Verification
- `go build ./cmd/server ./cmd/proxypilot-tray ./cmd/proxypilotpack`
- `go vet ./...`
- `go test ./...`
- clean-tree packaging and release asset assembly for `v0.3.0-dev-0.51`
- `gh release list -R Finesssee/ProxyPilot --limit 5`
+1 -1
View File
@@ -1,7 +1,7 @@
{
"project": "ProxyPilot",
"version": "0.3.0",
"upstream": "CLIProxyAPI v6.9.12",
"upstream": "CLIProxyAPI v6.9.15",
"features": [
{
"id": 1,
@@ -11,6 +11,7 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
"github.com/router-for-me/CLIProxyAPI/v6/internal/runtime/geminicli"
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
"github.com/router-for-me/CLIProxyAPI/v6/sdk/proxyutil"
@@ -636,6 +637,11 @@ func (h *Handler) apiCallTransport(auth *coreauth.Auth) http.RoundTripper {
if proxyStr := strings.TrimSpace(auth.ProxyURL); proxyStr != "" {
proxyCandidates = append(proxyCandidates, proxyStr)
}
if h != nil && h.cfg != nil {
if proxyStr := strings.TrimSpace(proxyURLFromAPIKeyConfig(h.cfg, auth)); proxyStr != "" {
proxyCandidates = append(proxyCandidates, proxyStr)
}
}
}
if h != nil && h.cfg != nil {
if proxyStr := strings.TrimSpace(h.cfg.ProxyURL); proxyStr != "" {
@@ -658,6 +664,123 @@ func (h *Handler) apiCallTransport(auth *coreauth.Auth) http.RoundTripper {
return clone
}
type apiKeyConfigEntry interface {
GetAPIKey() string
GetBaseURL() string
}
func resolveAPIKeyConfig[T apiKeyConfigEntry](entries []T, auth *coreauth.Auth) *T {
if auth == nil || len(entries) == 0 {
return nil
}
attrKey, attrBase := "", ""
if auth.Attributes != nil {
attrKey = strings.TrimSpace(auth.Attributes["api_key"])
attrBase = strings.TrimSpace(auth.Attributes["base_url"])
}
for i := range entries {
entry := &entries[i]
cfgKey := strings.TrimSpace((*entry).GetAPIKey())
cfgBase := strings.TrimSpace((*entry).GetBaseURL())
if attrKey != "" && attrBase != "" {
if strings.EqualFold(cfgKey, attrKey) && strings.EqualFold(cfgBase, attrBase) {
return entry
}
continue
}
if attrKey != "" && strings.EqualFold(cfgKey, attrKey) {
if cfgBase == "" || strings.EqualFold(cfgBase, attrBase) {
return entry
}
}
if attrKey == "" && attrBase != "" && strings.EqualFold(cfgBase, attrBase) {
return entry
}
}
if attrKey != "" {
for i := range entries {
entry := &entries[i]
if strings.EqualFold(strings.TrimSpace((*entry).GetAPIKey()), attrKey) {
return entry
}
}
}
return nil
}
func proxyURLFromAPIKeyConfig(cfg *config.Config, auth *coreauth.Auth) string {
if cfg == nil || auth == nil {
return ""
}
authKind, authAccount := auth.AccountInfo()
if !strings.EqualFold(strings.TrimSpace(authKind), "api_key") {
return ""
}
attrs := auth.Attributes
compatName := ""
providerKey := ""
if len(attrs) > 0 {
compatName = strings.TrimSpace(attrs["compat_name"])
providerKey = strings.TrimSpace(attrs["provider_key"])
}
if compatName != "" || strings.EqualFold(strings.TrimSpace(auth.Provider), "openai-compatibility") {
return resolveOpenAICompatAPIKeyProxyURL(cfg, auth, strings.TrimSpace(authAccount), providerKey, compatName)
}
switch strings.ToLower(strings.TrimSpace(auth.Provider)) {
case "gemini":
if entry := resolveAPIKeyConfig(cfg.GeminiKey, auth); entry != nil {
return strings.TrimSpace(entry.ProxyURL)
}
case "claude":
if entry := resolveAPIKeyConfig(cfg.ClaudeKey, auth); entry != nil {
return strings.TrimSpace(entry.ProxyURL)
}
case "codex":
if entry := resolveAPIKeyConfig(cfg.CodexKey, auth); entry != nil {
return strings.TrimSpace(entry.ProxyURL)
}
}
return ""
}
func resolveOpenAICompatAPIKeyProxyURL(cfg *config.Config, auth *coreauth.Auth, apiKey, providerKey, compatName string) string {
if cfg == nil || auth == nil {
return ""
}
apiKey = strings.TrimSpace(apiKey)
if apiKey == "" {
return ""
}
candidates := make([]string, 0, 3)
if v := strings.TrimSpace(compatName); v != "" {
candidates = append(candidates, v)
}
if v := strings.TrimSpace(providerKey); v != "" {
candidates = append(candidates, v)
}
if v := strings.TrimSpace(auth.Provider); v != "" {
candidates = append(candidates, v)
}
for i := range cfg.OpenAICompatibility {
compat := &cfg.OpenAICompatibility[i]
for _, candidate := range candidates {
if candidate != "" && strings.EqualFold(strings.TrimSpace(candidate), compat.Name) {
for j := range compat.APIKeyEntries {
entry := &compat.APIKeyEntries[j]
if strings.EqualFold(strings.TrimSpace(entry.APIKey), apiKey) {
return strings.TrimSpace(entry.ProxyURL)
}
}
return ""
}
}
}
return ""
}
func buildProxyTransport(proxyStr string) *http.Transport {
transport, _, errBuild := proxyutil.BuildHTTPTransport(proxyStr)
if errBuild != nil {
@@ -58,6 +58,105 @@ func TestAPICallTransportInvalidAuthFallsBackToGlobalProxy(t *testing.T) {
}
}
func TestAPICallTransportAPIKeyAuthFallsBackToConfigProxyURL(t *testing.T) {
t.Parallel()
h := &Handler{
cfg: &config.Config{
SDKConfig: sdkconfig.SDKConfig{ProxyURL: "http://global-proxy.example.com:8080"},
GeminiKey: []config.GeminiKey{{
APIKey: "gemini-key",
ProxyURL: "http://gemini-proxy.example.com:8080",
}},
ClaudeKey: []config.ClaudeKey{{
APIKey: "claude-key",
ProxyURL: "http://claude-proxy.example.com:8080",
}},
CodexKey: []config.CodexKey{{
APIKey: "codex-key",
ProxyURL: "http://codex-proxy.example.com:8080",
}},
OpenAICompatibility: []config.OpenAICompatibility{{
Name: "bohe",
BaseURL: "https://bohe.example.com",
APIKeyEntries: []config.OpenAICompatibilityAPIKey{{
APIKey: "compat-key",
ProxyURL: "http://compat-proxy.example.com:8080",
}},
}},
},
}
cases := []struct {
name string
auth *coreauth.Auth
wantProxy string
}{
{
name: "gemini",
auth: &coreauth.Auth{
Provider: "gemini",
Attributes: map[string]string{"api_key": "gemini-key"},
},
wantProxy: "http://gemini-proxy.example.com:8080",
},
{
name: "claude",
auth: &coreauth.Auth{
Provider: "claude",
Attributes: map[string]string{"api_key": "claude-key"},
},
wantProxy: "http://claude-proxy.example.com:8080",
},
{
name: "codex",
auth: &coreauth.Auth{
Provider: "codex",
Attributes: map[string]string{"api_key": "codex-key"},
},
wantProxy: "http://codex-proxy.example.com:8080",
},
{
name: "openai-compatibility",
auth: &coreauth.Auth{
Provider: "bohe",
Attributes: map[string]string{
"api_key": "compat-key",
"compat_name": "bohe",
"provider_key": "bohe",
},
},
wantProxy: "http://compat-proxy.example.com:8080",
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
transport := h.apiCallTransport(tc.auth)
httpTransport, ok := transport.(*http.Transport)
if !ok {
t.Fatalf("transport type = %T, want *http.Transport", transport)
}
req, errRequest := http.NewRequest(http.MethodGet, "https://example.com", nil)
if errRequest != nil {
t.Fatalf("http.NewRequest returned error: %v", errRequest)
}
proxyURL, errProxy := httpTransport.Proxy(req)
if errProxy != nil {
t.Fatalf("httpTransport.Proxy returned error: %v", errProxy)
}
if proxyURL == nil || proxyURL.String() != tc.wantProxy {
t.Fatalf("proxy URL = %v, want %s", proxyURL, tc.wantProxy)
}
})
}
}
func TestAuthByIndexDistinguishesSharedAPIKeysAcrossProviders(t *testing.T) {
t.Parallel()
-5
View File
@@ -108,11 +108,6 @@ func createReverseProxy(upstreamURL string, secretSource SecretSource) (*httputi
// Modify incoming responses to handle gzip without Content-Encoding
// This addresses the same issue as inline handler gzip handling, but at the proxy level
proxy.ModifyResponse = func(resp *http.Response) error {
// Only process successful responses
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil
}
// Skip if already marked as gzip (Content-Encoding set)
if resp.Header.Get("Content-Encoding") != "" {
return nil
+19 -8
View File
@@ -2,6 +2,7 @@ package amp
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
@@ -290,8 +291,10 @@ func (rw *ResponseRewriter) rewriteStreamEvent(data []byte) []byte {
}
// SanitizeAmpRequestBody removes thinking blocks with empty/missing/invalid signatures
// from the messages array in a request body before forwarding to the upstream API.
// This prevents 400 errors from the API which requires valid signatures on thinking blocks.
// and strips the proxy-injected "signature" field from tool_use blocks in the messages
// array before forwarding to the upstream API.
// This prevents 400 errors from the API which requires valid signatures on thinking
// blocks and does not accept a signature field on tool_use blocks.
func SanitizeAmpRequestBody(body []byte) []byte {
messages := gjson.GetBytes(body, "messages")
if !messages.Exists() || !messages.IsArray() {
@@ -309,21 +312,30 @@ func SanitizeAmpRequestBody(body []byte) []byte {
}
var keepBlocks []interface{}
removedCount := 0
contentModified := false
for _, block := range content.Array() {
blockType := block.Get("type").String()
if blockType == "thinking" {
sig := block.Get("signature")
if !sig.Exists() || sig.Type != gjson.String || strings.TrimSpace(sig.String()) == "" {
removedCount++
contentModified = true
continue
}
}
keepBlocks = append(keepBlocks, block.Value())
// Use raw JSON to prevent float64 rounding of large integers in tool_use inputs
blockRaw := []byte(block.Raw)
if blockType == "tool_use" && block.Get("signature").Exists() {
blockRaw, _ = sjson.DeleteBytes(blockRaw, "signature")
contentModified = true
}
// sjson.SetBytes supports raw JSON strings if wrapped in gjson.Raw
keepBlocks = append(keepBlocks, json.RawMessage(blockRaw))
}
if removedCount > 0 {
if contentModified {
contentPath := fmt.Sprintf("messages.%d.content", msgIdx)
var err error
if len(keepBlocks) == 0 {
@@ -332,11 +344,10 @@ func SanitizeAmpRequestBody(body []byte) []byte {
body, err = sjson.SetBytes(body, contentPath, keepBlocks)
}
if err != nil {
log.Warnf("Amp RequestSanitizer: failed to remove thinking blocks from message %d: %v", msgIdx, err)
log.Warnf("Amp RequestSanitizer: failed to sanitize message %d: %v", msgIdx, err)
continue
}
modified = true
log.Debugf("Amp RequestSanitizer: removed %d thinking blocks with invalid signatures from message %d", removedCount, msgIdx)
}
}
@@ -145,6 +145,36 @@ func TestSanitizeAmpRequestBody_RemovesWhitespaceAndNonStringSignatures(t *testi
}
}
func TestSanitizeAmpRequestBody_StripsSignatureFromToolUseBlocks(t *testing.T) {
input := []byte(`{"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"thought","signature":"valid-sig"},{"type":"tool_use","id":"toolu_01","name":"Bash","input":{"cmd":"ls"},"signature":""}]}]}`)
result := SanitizeAmpRequestBody(input)
if contains(result, []byte(`"signature":""`)) {
t.Fatalf("expected signature to be stripped from tool_use block, got %s", string(result))
}
if !contains(result, []byte(`"valid-sig"`)) {
t.Fatalf("expected thinking signature to remain, got %s", string(result))
}
if !contains(result, []byte(`"tool_use"`)) {
t.Fatalf("expected tool_use block to remain, got %s", string(result))
}
}
func TestSanitizeAmpRequestBody_MixedInvalidThinkingAndToolUseSignature(t *testing.T) {
input := []byte(`{"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"drop-me","signature":""},{"type":"tool_use","id":"toolu_01","name":"Bash","input":{"cmd":"ls"},"signature":""}]}]}`)
result := SanitizeAmpRequestBody(input)
if contains(result, []byte("drop-me")) {
t.Fatalf("expected invalid thinking block to be removed, got %s", string(result))
}
if contains(result, []byte(`"signature"`)) {
t.Fatalf("expected signature to be stripped from tool_use block, got %s", string(result))
}
if !contains(result, []byte(`"tool_use"`)) {
t.Fatalf("expected tool_use block to remain, got %s", string(result))
}
}
func contains(data, substr []byte) bool {
for i := 0; i <= len(data)-len(substr); i++ {
if string(data[i:i+len(substr)]) == string(substr) {
@@ -26,6 +26,10 @@ type VertexCredentialStorage struct {
// Type is the provider identifier stored alongside credentials. Always "vertex".
Type string `json:"type"`
// Prefix optionally namespaces models for this credential (e.g., "teamA").
// This results in model names like "teamA/gemini-2.0-flash".
Prefix string `json:"prefix,omitempty"`
}
// SaveTokenToFile writes the credential payload to the given file path in JSON format.
+18 -2
View File
@@ -20,7 +20,7 @@ import (
// DoVertexImport imports a Google Cloud service account key JSON and persists
// it as a "vertex" provider credential. The file content is embedded in the auth
// file to allow portable deployment across stores.
func DoVertexImport(cfg *config.Config, keyPath string) {
func DoVertexImport(cfg *config.Config, keyPath string, prefix string) {
if cfg == nil {
cfg = &config.Config{}
}
@@ -62,13 +62,28 @@ func DoVertexImport(cfg *config.Config, keyPath string) {
// Default location if not provided by user. Can be edited in the saved file later.
location := "us-central1"
fileName := fmt.Sprintf("vertex-%s.json", sanitizeFilePart(projectID))
// Normalize and validate prefix: must be a single segment (no "/" allowed).
prefix = strings.TrimSpace(prefix)
prefix = strings.Trim(prefix, "/")
if prefix != "" && strings.Contains(prefix, "/") {
log.Errorf("vertex-import: prefix must be a single segment (no '/' allowed): %q", prefix)
return
}
// Include prefix in filename so importing the same project with different
// prefixes creates separate credential files instead of overwriting.
baseName := sanitizeFilePart(projectID)
if prefix != "" {
baseName = sanitizeFilePart(prefix) + "-" + baseName
}
fileName := fmt.Sprintf("vertex-%s.json", baseName)
// Build auth record
storage := &vertex.VertexCredentialStorage{
ServiceAccount: sa,
ProjectID: projectID,
Email: email,
Location: location,
Prefix: prefix,
}
metadata := map[string]any{
"service_account": sa,
@@ -76,6 +91,7 @@ func DoVertexImport(cfg *config.Config, keyPath string) {
"email": email,
"location": location,
"type": "vertex",
"prefix": prefix,
"label": labelForVertex(projectID, email),
}
record := &coreauth.Auth{
+2 -28
View File
@@ -26,7 +26,6 @@ import (
const (
defaultManagementReleaseURL = "https://api.github.com/repos/router-for-me/Cli-Proxy-API-Management-Center/releases/latest"
defaultManagementFallbackURL = "https://cpamc.router-for.me/"
managementAssetName = "management.html"
httpUserAgent = "CLIProxyAPI-management-updater"
managementSyncMinInterval = 30 * time.Second
@@ -235,10 +234,7 @@ func EnsureLatestManagementHTML(ctx context.Context, staticDir string, proxyURL
asset, remoteHash, err := fetchLatestAsset(ctx, client, releaseURL)
if err != nil {
if localFileMissing {
log.WithError(err).Warn("failed to fetch latest management release information, trying fallback page")
if ensureFallbackManagementHTML(ctx, client, localPath) {
return nil, nil
}
log.WithError(err).Warn("failed to fetch latest management release information and local management asset is missing")
return nil, nil
}
log.WithError(err).Warn("failed to fetch latest management release information")
@@ -253,10 +249,7 @@ func EnsureLatestManagementHTML(ctx context.Context, staticDir string, proxyURL
data, downloadedHash, err := downloadAsset(ctx, client, asset.BrowserDownloadURL)
if err != nil {
if localFileMissing {
log.WithError(err).Warn("failed to download management asset, trying fallback page")
if ensureFallbackManagementHTML(ctx, client, localPath) {
return nil, nil
}
log.WithError(err).Warn("failed to download management asset and local management asset is missing")
return nil, nil
}
log.WithError(err).Warn("failed to download management asset")
@@ -281,25 +274,6 @@ func EnsureLatestManagementHTML(ctx context.Context, staticDir string, proxyURL
return err == nil
}
func ensureFallbackManagementHTML(ctx context.Context, client *http.Client, localPath string) bool {
data, downloadedHash, err := downloadAsset(ctx, client, defaultManagementFallbackURL)
if err != nil {
log.WithError(err).Warn("failed to download fallback management control panel page")
return false
}
log.Warnf("management asset downloaded from fallback URL without digest verification (hash=%s) — "+
"enable verified GitHub updates by keeping disable-auto-update-panel set to false", downloadedHash)
if err = atomicWriteFile(localPath, data); err != nil {
log.WithError(err).Warn("failed to persist fallback management control panel page")
return false
}
log.Infof("management asset updated from fallback page successfully (hash=%s)", downloadedHash)
return true
}
func resolveReleaseURL(repo string) string {
repo = strings.TrimSpace(repo)
if repo == "" {
+151
View File
@@ -0,0 +1,151 @@
// Package misc provides miscellaneous utility functions for the CLI Proxy API server.
package misc
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
const (
antigravityReleasesURL = "https://antigravity-auto-updater-974169037036.us-central1.run.app/releases"
antigravityFallbackVersion = "1.21.9"
antigravityVersionCacheTTL = 6 * time.Hour
antigravityFetchTimeout = 10 * time.Second
)
type antigravityRelease struct {
Version string `json:"version"`
ExecutionID string `json:"execution_id"`
}
var (
cachedAntigravityVersion = antigravityFallbackVersion
antigravityVersionMu sync.RWMutex
antigravityVersionExpiry time.Time
antigravityUpdaterOnce sync.Once
)
// StartAntigravityVersionUpdater starts a background goroutine that periodically refreshes the cached antigravity version.
// This is intentionally decoupled from request execution to avoid blocking executors on version lookups.
func StartAntigravityVersionUpdater(ctx context.Context) {
antigravityUpdaterOnce.Do(func() {
go runAntigravityVersionUpdater(ctx)
})
}
func runAntigravityVersionUpdater(ctx context.Context) {
if ctx == nil {
ctx = context.Background()
}
ticker := time.NewTicker(antigravityVersionCacheTTL / 2)
defer ticker.Stop()
log.Infof("periodic antigravity version refresh started (interval=%s)", antigravityVersionCacheTTL/2)
refreshAntigravityVersion(ctx)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
refreshAntigravityVersion(ctx)
}
}
}
func refreshAntigravityVersion(ctx context.Context) {
version, errFetch := fetchAntigravityLatestVersion(ctx)
antigravityVersionMu.Lock()
defer antigravityVersionMu.Unlock()
now := time.Now()
if errFetch == nil {
cachedAntigravityVersion = version
antigravityVersionExpiry = now.Add(antigravityVersionCacheTTL)
log.WithField("version", version).Info("fetched latest antigravity version")
return
}
if cachedAntigravityVersion == "" || now.After(antigravityVersionExpiry) {
cachedAntigravityVersion = antigravityFallbackVersion
antigravityVersionExpiry = now.Add(antigravityVersionCacheTTL)
log.WithError(errFetch).Warn("failed to refresh antigravity version, using fallback version")
return
}
log.WithError(errFetch).Debug("failed to refresh antigravity version, keeping cached value")
}
// AntigravityLatestVersion returns the cached antigravity version refreshed by StartAntigravityVersionUpdater.
// It falls back to antigravityFallbackVersion if the cache is empty or stale.
func AntigravityLatestVersion() string {
antigravityVersionMu.RLock()
if cachedAntigravityVersion != "" && time.Now().Before(antigravityVersionExpiry) {
v := cachedAntigravityVersion
antigravityVersionMu.RUnlock()
return v
}
antigravityVersionMu.RUnlock()
return antigravityFallbackVersion
}
// AntigravityUserAgent returns the User-Agent string for antigravity requests
// using the latest version fetched from the releases API.
func AntigravityUserAgent() string {
return fmt.Sprintf("antigravity/%s darwin/arm64", AntigravityLatestVersion())
}
func fetchAntigravityLatestVersion(ctx context.Context) (string, error) {
if ctx == nil {
ctx = context.Background()
}
client := &http.Client{Timeout: antigravityFetchTimeout}
httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodGet, antigravityReleasesURL, nil)
if errReq != nil {
return "", fmt.Errorf("build antigravity releases request: %w", errReq)
}
resp, errDo := client.Do(httpReq)
if errDo != nil {
return "", fmt.Errorf("fetch antigravity releases: %w", errDo)
}
defer func() {
if errClose := resp.Body.Close(); errClose != nil {
log.WithError(errClose).Warn("antigravity releases response body close error")
}
}()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("antigravity releases API returned status %d", resp.StatusCode)
}
var releases []antigravityRelease
if errDecode := json.NewDecoder(resp.Body).Decode(&releases); errDecode != nil {
return "", fmt.Errorf("decode antigravity releases response: %w", errDecode)
}
if len(releases) == 0 {
return "", errors.New("antigravity releases API returned empty list")
}
version := releases[0].Version
if version == "" {
return "", errors.New("antigravity releases API returned empty version")
}
return version, nil
}
+155 -63
View File
@@ -280,6 +280,7 @@
"dynamic_allowed": true,
"levels": [
"low",
"medium",
"high"
]
}
@@ -554,6 +555,7 @@
"dynamic_allowed": true,
"levels": [
"low",
"medium",
"high"
]
}
@@ -610,6 +612,8 @@
"dynamic_allowed": true,
"levels": [
"minimal",
"low",
"medium",
"high"
]
}
@@ -838,6 +842,7 @@
"dynamic_allowed": true,
"levels": [
"low",
"medium",
"high"
]
}
@@ -896,6 +901,8 @@
"dynamic_allowed": true,
"levels": [
"minimal",
"low",
"medium",
"high"
]
}
@@ -1070,6 +1077,8 @@
"dynamic_allowed": true,
"levels": [
"minimal",
"low",
"medium",
"high"
]
}
@@ -1371,6 +1380,75 @@
"xhigh"
]
}
},
{
"id": "gpt-5.3-codex",
"object": "model",
"created": 1770307200,
"owned_by": "openai",
"type": "openai",
"display_name": "GPT 5.3 Codex",
"version": "gpt-5.3",
"description": "Stable version of GPT 5.3 Codex, The best model for coding and agentic tasks across domains.",
"context_length": 400000,
"max_completion_tokens": 128000,
"supported_parameters": [
"tools"
],
"thinking": {
"levels": [
"low",
"medium",
"high",
"xhigh"
]
}
},
{
"id": "gpt-5.4",
"object": "model",
"created": 1772668800,
"owned_by": "openai",
"type": "openai",
"display_name": "GPT 5.4",
"version": "gpt-5.4",
"description": "Stable version of GPT 5.4",
"context_length": 1050000,
"max_completion_tokens": 128000,
"supported_parameters": [
"tools"
],
"thinking": {
"levels": [
"low",
"medium",
"high",
"xhigh"
]
}
},
{
"id": "gpt-5.4-mini",
"object": "model",
"created": 1773705600,
"owned_by": "openai",
"type": "openai",
"display_name": "GPT 5.4 Mini",
"version": "gpt-5.4-mini",
"description": "GPT-5.4 mini brings the strengths of GPT-5.4 to a faster, more efficient model designed for high-volume workloads.",
"context_length": 400000,
"max_completion_tokens": 128000,
"supported_parameters": [
"tools"
],
"thinking": {
"levels": [
"low",
"medium",
"high",
"xhigh"
]
}
}
],
"codex-team": [
@@ -1623,6 +1701,29 @@
"xhigh"
]
}
},
{
"id": "gpt-5.4-mini",
"object": "model",
"created": 1773705600,
"owned_by": "openai",
"type": "openai",
"display_name": "GPT 5.4 Mini",
"version": "gpt-5.4-mini",
"description": "GPT-5.4 mini brings the strengths of GPT-5.4 to a faster, more efficient model designed for high-volume workloads.",
"context_length": 400000,
"max_completion_tokens": 128000,
"supported_parameters": [
"tools"
],
"thinking": {
"levels": [
"low",
"medium",
"high",
"xhigh"
]
}
}
],
"codex-plus": [
@@ -1898,6 +1999,29 @@
"xhigh"
]
}
},
{
"id": "gpt-5.4-mini",
"object": "model",
"created": 1773705600,
"owned_by": "openai",
"type": "openai",
"display_name": "GPT 5.4 Mini",
"version": "gpt-5.4-mini",
"description": "GPT-5.4 mini brings the strengths of GPT-5.4 to a faster, more efficient model designed for high-volume workloads.",
"context_length": 400000,
"max_completion_tokens": 128000,
"supported_parameters": [
"tools"
],
"thinking": {
"levels": [
"low",
"medium",
"high",
"xhigh"
]
}
}
],
"codex-pro": [
@@ -2173,55 +2297,40 @@
"xhigh"
]
}
},
{
"id": "gpt-5.4-mini",
"object": "model",
"created": 1773705600,
"owned_by": "openai",
"type": "openai",
"display_name": "GPT 5.4 Mini",
"version": "gpt-5.4-mini",
"description": "GPT-5.4 mini brings the strengths of GPT-5.4 to a faster, more efficient model designed for high-volume workloads.",
"context_length": 400000,
"max_completion_tokens": 128000,
"supported_parameters": [
"tools"
],
"thinking": {
"levels": [
"low",
"medium",
"high",
"xhigh"
]
}
}
],
"qwen": [
{
"id": "qwen3-coder-plus",
"object": "model",
"created": 1753228800,
"owned_by": "qwen",
"type": "qwen",
"display_name": "Qwen3 Coder Plus",
"version": "3.0",
"description": "Advanced code generation and understanding model",
"context_length": 32768,
"max_completion_tokens": 8192,
"supported_parameters": [
"temperature",
"top_p",
"max_tokens",
"stream",
"stop"
]
},
{
"id": "qwen3-coder-flash",
"object": "model",
"created": 1753228800,
"owned_by": "qwen",
"type": "qwen",
"display_name": "Qwen3 Coder Flash",
"version": "3.0",
"description": "Fast code generation model",
"context_length": 8192,
"max_completion_tokens": 2048,
"supported_parameters": [
"temperature",
"top_p",
"max_tokens",
"stream",
"stop"
]
},
{
"id": "coder-model",
"object": "model",
"created": 1771171200,
"owned_by": "qwen",
"type": "qwen",
"display_name": "Qwen 3.5 Plus",
"version": "3.5",
"display_name": "Qwen 3.6 Plus",
"version": "3.6",
"description": "efficient hybrid model with leading coding performance",
"context_length": 1048576,
"max_completion_tokens": 65536,
@@ -2232,25 +2341,6 @@
"stream",
"stop"
]
},
{
"id": "vision-model",
"object": "model",
"created": 1758672000,
"owned_by": "qwen",
"type": "qwen",
"display_name": "Qwen3 Vision Model",
"version": "3.0",
"description": "Vision model model",
"context_length": 32768,
"max_completion_tokens": 2048,
"supported_parameters": [
"temperature",
"top_p",
"max_tokens",
"stream",
"stop"
]
}
],
"iflow": [
@@ -2639,11 +2729,12 @@
"context_length": 1048576,
"max_completion_tokens": 65535,
"thinking": {
"min": 128,
"max": 32768,
"min": 1,
"max": 65535,
"dynamic_allowed": true,
"levels": [
"low",
"medium",
"high"
]
}
@@ -2659,11 +2750,12 @@
"context_length": 1048576,
"max_completion_tokens": 65535,
"thinking": {
"min": 128,
"max": 32768,
"min": 1,
"max": 65535,
"dynamic_allowed": true,
"levels": [
"low",
"medium",
"high"
]
}
@@ -24,6 +24,7 @@ import (
"github.com/google/uuid"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
"github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
"github.com/router-for-me/CLIProxyAPI/v6/internal/runtime/executor/helps"
"github.com/router-for-me/CLIProxyAPI/v6/internal/thinking"
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
@@ -45,7 +46,7 @@ const (
antigravityGeneratePath = "/v1internal:generateContent"
antigravityClientID = "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com"
antigravityClientSecret = "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"
defaultAntigravityAgent = "antigravity/1.19.6 darwin/arm64"
defaultAntigravityAgent = "antigravity/1.21.9 darwin/arm64" // fallback only; overridden at runtime by misc.AntigravityUserAgent()
antigravityAuthType = "antigravity"
refreshSkew = 3000 * time.Second
antigravityCreditsRetryTTL = 5 * time.Hour
@@ -1739,7 +1740,7 @@ func resolveUserAgent(auth *cliproxyauth.Auth) string {
}
}
}
return defaultAntigravityAgent
return misc.AntigravityUserAgent()
}
func antigravityRetryAttempts(auth *cliproxyauth.Auth, cfg *config.Config) int {
+213 -250
View File
@@ -8,7 +8,6 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
@@ -137,6 +136,7 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
// Disable thinking if tool_choice forces tool use (Anthropic API constraint)
body = disableThinkingIfToolChoiceForced(body)
body = normalizeClaudeTemperatureForThinking(body)
// Auto-inject cache_control if missing (optimization for ClawdBot/clients without caching support)
if countCacheControls(body) == 0 {
@@ -307,6 +307,7 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
// Disable thinking if tool_choice forces tool use (Anthropic API constraint)
body = disableThinkingIfToolChoiceForced(body)
body = normalizeClaudeTemperatureForThinking(body)
// Auto-inject cache_control if missing (optimization for ClawdBot/clients without caching support)
if countCacheControls(body) == 0 {
@@ -651,6 +652,25 @@ func disableThinkingIfToolChoiceForced(body []byte) []byte {
return body
}
// normalizeClaudeTemperatureForThinking keeps Anthropic message requests valid when
// thinking is enabled. Anthropic rejects temperatures other than 1 when
// thinking.type is enabled/adaptive/auto.
func normalizeClaudeTemperatureForThinking(body []byte) []byte {
if !gjson.GetBytes(body, "temperature").Exists() {
return body
}
thinkingType := strings.ToLower(strings.TrimSpace(gjson.GetBytes(body, "thinking.type").String()))
switch thinkingType {
case "enabled", "adaptive", "auto":
if temp := gjson.GetBytes(body, "temperature"); temp.Exists() && temp.Type == gjson.Number && temp.Float() == 1 {
return body
}
body, _ = sjson.SetBytes(body, "temperature", 1)
}
return body
}
type compositeReadCloser struct {
io.Reader
closers []func() error
@@ -820,6 +840,9 @@ func applyClaudeHeaders(r *http.Request, auth *cliproxyauth.Auth, apiKey string,
baseBetas += ",oauth-2025-04-20"
}
}
if !strings.Contains(baseBetas, "interleaved-thinking") {
baseBetas += ",interleaved-thinking-2025-05-14"
}
hasClaude1MHeader := false
if ginHeaders != nil {
@@ -928,12 +951,9 @@ func applyClaudeToolPrefix(body []byte, prefix string) []byte {
return body
}
// Collect built-in tool names (those with a non-empty "type" field) so we can
// skip them consistently in both tools and message history.
builtinTools := map[string]bool{}
for _, name := range []string{"web_search", "code_execution", "text_editor", "computer"} {
builtinTools[name] = true
}
// Collect built-in tool names from the authoritative fallback seed list and
// augment it with any typed built-ins present in the current request body.
builtinTools := helps.AugmentClaudeBuiltinToolRegistry(body, nil)
if tools := gjson.GetBytes(body, "tools"); tools.Exists() && tools.IsArray() {
tools.ForEach(func(index, tool gjson.Result) bool {
@@ -1442,182 +1462,6 @@ func countCacheControls(payload []byte) int {
return count
}
func parsePayloadObject(payload []byte) (map[string]any, bool) {
if len(payload) == 0 {
return nil, false
}
var root map[string]any
if err := json.Unmarshal(payload, &root); err != nil {
return nil, false
}
return root, true
}
func marshalPayloadObject(original []byte, root map[string]any) []byte {
if root == nil {
return original
}
out, err := json.Marshal(root)
if err != nil {
return original
}
return out
}
func asObject(v any) (map[string]any, bool) {
obj, ok := v.(map[string]any)
return obj, ok
}
func asArray(v any) ([]any, bool) {
arr, ok := v.([]any)
return arr, ok
}
func countCacheControlsMap(root map[string]any) int {
count := 0
if system, ok := asArray(root["system"]); ok {
for _, item := range system {
if obj, ok := asObject(item); ok {
if _, exists := obj["cache_control"]; exists {
count++
}
}
}
}
if tools, ok := asArray(root["tools"]); ok {
for _, item := range tools {
if obj, ok := asObject(item); ok {
if _, exists := obj["cache_control"]; exists {
count++
}
}
}
}
if messages, ok := asArray(root["messages"]); ok {
for _, msg := range messages {
msgObj, ok := asObject(msg)
if !ok {
continue
}
content, ok := asArray(msgObj["content"])
if !ok {
continue
}
for _, item := range content {
if obj, ok := asObject(item); ok {
if _, exists := obj["cache_control"]; exists {
count++
}
}
}
}
}
return count
}
func normalizeTTLForBlock(obj map[string]any, seen5m *bool) bool {
ccRaw, exists := obj["cache_control"]
if !exists {
return false
}
cc, ok := asObject(ccRaw)
if !ok {
*seen5m = true
return false
}
ttlRaw, ttlExists := cc["ttl"]
ttl, ttlIsString := ttlRaw.(string)
if !ttlExists || !ttlIsString || ttl != "1h" {
*seen5m = true
return false
}
if *seen5m {
delete(cc, "ttl")
return true
}
return false
}
func findLastCacheControlIndex(arr []any) int {
last := -1
for idx, item := range arr {
obj, ok := asObject(item)
if !ok {
continue
}
if _, exists := obj["cache_control"]; exists {
last = idx
}
}
return last
}
func stripCacheControlExceptIndex(arr []any, preserveIdx int, excess *int) {
for idx, item := range arr {
if *excess <= 0 {
return
}
obj, ok := asObject(item)
if !ok {
continue
}
if _, exists := obj["cache_control"]; exists && idx != preserveIdx {
delete(obj, "cache_control")
*excess--
}
}
}
func stripAllCacheControl(arr []any, excess *int) {
for _, item := range arr {
if *excess <= 0 {
return
}
obj, ok := asObject(item)
if !ok {
continue
}
if _, exists := obj["cache_control"]; exists {
delete(obj, "cache_control")
*excess--
}
}
}
func stripMessageCacheControl(messages []any, excess *int) {
for _, msg := range messages {
if *excess <= 0 {
return
}
msgObj, ok := asObject(msg)
if !ok {
continue
}
content, ok := asArray(msgObj["content"])
if !ok {
continue
}
for _, item := range content {
if *excess <= 0 {
return
}
obj, ok := asObject(item)
if !ok {
continue
}
if _, exists := obj["cache_control"]; exists {
delete(obj, "cache_control")
*excess--
}
}
}
}
// normalizeCacheControlTTL ensures cache_control TTL values don't violate the
// prompt-caching-scope-2026-01-05 ordering constraint: a 1h-TTL block must not
// appear after a 5m-TTL block anywhere in the evaluation order.
@@ -1630,58 +1474,75 @@ func stripMessageCacheControl(messages []any, excess *int) {
// Strategy: walk all cache_control blocks in evaluation order. Once a 5m block
// is seen, strip ttl from ALL subsequent 1h blocks (downgrading them to 5m).
func normalizeCacheControlTTL(payload []byte) []byte {
root, ok := parsePayloadObject(payload)
if !ok {
if len(payload) == 0 || !gjson.ValidBytes(payload) {
return payload
}
original := payload
seen5m := false
modified := false
if tools, ok := asArray(root["tools"]); ok {
for _, tool := range tools {
if obj, ok := asObject(tool); ok {
if normalizeTTLForBlock(obj, &seen5m) {
modified = true
}
}
processBlock := func(path string, obj gjson.Result) {
cc := obj.Get("cache_control")
if !cc.Exists() {
return
}
if !cc.IsObject() {
seen5m = true
return
}
ttl := cc.Get("ttl")
if ttl.Type != gjson.String || ttl.String() != "1h" {
seen5m = true
return
}
if !seen5m {
return
}
ttlPath := path + ".cache_control.ttl"
updated, errDel := sjson.DeleteBytes(payload, ttlPath)
if errDel != nil {
return
}
payload = updated
modified = true
}
if system, ok := asArray(root["system"]); ok {
for _, item := range system {
if obj, ok := asObject(item); ok {
if normalizeTTLForBlock(obj, &seen5m) {
modified = true
}
}
}
tools := gjson.GetBytes(payload, "tools")
if tools.IsArray() {
tools.ForEach(func(idx, item gjson.Result) bool {
processBlock(fmt.Sprintf("tools.%d", int(idx.Int())), item)
return true
})
}
if messages, ok := asArray(root["messages"]); ok {
for _, msg := range messages {
msgObj, ok := asObject(msg)
if !ok {
continue
system := gjson.GetBytes(payload, "system")
if system.IsArray() {
system.ForEach(func(idx, item gjson.Result) bool {
processBlock(fmt.Sprintf("system.%d", int(idx.Int())), item)
return true
})
}
messages := gjson.GetBytes(payload, "messages")
if messages.IsArray() {
messages.ForEach(func(msgIdx, msg gjson.Result) bool {
content := msg.Get("content")
if !content.IsArray() {
return true
}
content, ok := asArray(msgObj["content"])
if !ok {
continue
}
for _, item := range content {
if obj, ok := asObject(item); ok {
if normalizeTTLForBlock(obj, &seen5m) {
modified = true
}
}
}
}
content.ForEach(func(itemIdx, item gjson.Result) bool {
processBlock(fmt.Sprintf("messages.%d.content.%d", int(msgIdx.Int()), int(itemIdx.Int())), item)
return true
})
return true
})
}
if !modified {
return payload
return original
}
return marshalPayloadObject(payload, root)
return payload
}
// enforceCacheControlLimit removes excess cache_control blocks from a payload
@@ -1701,64 +1562,166 @@ func normalizeCacheControlTTL(payload []byte) []byte {
// Phase 4: remaining system blocks (last system).
// Phase 5: remaining tool blocks (last tool).
func enforceCacheControlLimit(payload []byte, maxBlocks int) []byte {
root, ok := parsePayloadObject(payload)
if !ok {
if len(payload) == 0 || !gjson.ValidBytes(payload) {
return payload
}
total := countCacheControlsMap(root)
total := countCacheControls(payload)
if total <= maxBlocks {
return payload
}
excess := total - maxBlocks
var system []any
if arr, ok := asArray(root["system"]); ok {
system = arr
}
var tools []any
if arr, ok := asArray(root["tools"]); ok {
tools = arr
}
var messages []any
if arr, ok := asArray(root["messages"]); ok {
messages = arr
}
if len(system) > 0 {
stripCacheControlExceptIndex(system, findLastCacheControlIndex(system), &excess)
system := gjson.GetBytes(payload, "system")
if system.IsArray() {
lastIdx := -1
system.ForEach(func(idx, item gjson.Result) bool {
if item.Get("cache_control").Exists() {
lastIdx = int(idx.Int())
}
return true
})
if lastIdx >= 0 {
system.ForEach(func(idx, item gjson.Result) bool {
if excess <= 0 {
return false
}
i := int(idx.Int())
if i == lastIdx {
return true
}
if !item.Get("cache_control").Exists() {
return true
}
path := fmt.Sprintf("system.%d.cache_control", i)
updated, errDel := sjson.DeleteBytes(payload, path)
if errDel != nil {
return true
}
payload = updated
excess--
return true
})
}
}
if excess <= 0 {
return marshalPayloadObject(payload, root)
return payload
}
if len(tools) > 0 {
stripCacheControlExceptIndex(tools, findLastCacheControlIndex(tools), &excess)
tools := gjson.GetBytes(payload, "tools")
if tools.IsArray() {
lastIdx := -1
tools.ForEach(func(idx, item gjson.Result) bool {
if item.Get("cache_control").Exists() {
lastIdx = int(idx.Int())
}
return true
})
if lastIdx >= 0 {
tools.ForEach(func(idx, item gjson.Result) bool {
if excess <= 0 {
return false
}
i := int(idx.Int())
if i == lastIdx {
return true
}
if !item.Get("cache_control").Exists() {
return true
}
path := fmt.Sprintf("tools.%d.cache_control", i)
updated, errDel := sjson.DeleteBytes(payload, path)
if errDel != nil {
return true
}
payload = updated
excess--
return true
})
}
}
if excess <= 0 {
return marshalPayloadObject(payload, root)
return payload
}
if len(messages) > 0 {
stripMessageCacheControl(messages, &excess)
messages := gjson.GetBytes(payload, "messages")
if messages.IsArray() {
messages.ForEach(func(msgIdx, msg gjson.Result) bool {
if excess <= 0 {
return false
}
content := msg.Get("content")
if !content.IsArray() {
return true
}
content.ForEach(func(itemIdx, item gjson.Result) bool {
if excess <= 0 {
return false
}
if !item.Get("cache_control").Exists() {
return true
}
path := fmt.Sprintf("messages.%d.content.%d.cache_control", int(msgIdx.Int()), int(itemIdx.Int()))
updated, errDel := sjson.DeleteBytes(payload, path)
if errDel != nil {
return true
}
payload = updated
excess--
return true
})
return true
})
}
if excess <= 0 {
return marshalPayloadObject(payload, root)
return payload
}
if len(system) > 0 {
stripAllCacheControl(system, &excess)
system = gjson.GetBytes(payload, "system")
if system.IsArray() {
system.ForEach(func(idx, item gjson.Result) bool {
if excess <= 0 {
return false
}
if !item.Get("cache_control").Exists() {
return true
}
path := fmt.Sprintf("system.%d.cache_control", int(idx.Int()))
updated, errDel := sjson.DeleteBytes(payload, path)
if errDel != nil {
return true
}
payload = updated
excess--
return true
})
}
if excess <= 0 {
return marshalPayloadObject(payload, root)
return payload
}
if len(tools) > 0 {
stripAllCacheControl(tools, &excess)
tools = gjson.GetBytes(payload, "tools")
if tools.IsArray() {
tools.ForEach(func(idx, item gjson.Result) bool {
if excess <= 0 {
return false
}
if !item.Get("cache_control").Exists() {
return true
}
path := fmt.Sprintf("tools.%d.cache_control", int(idx.Int()))
updated, errDel := sjson.DeleteBytes(payload, path)
if errDel != nil {
return true
}
payload = updated
excess--
return true
})
}
return marshalPayloadObject(payload, root)
return payload
}
// injectMessagesCacheControl adds cache_control to the second-to-last user turn for multi-turn caching.
@@ -739,6 +739,35 @@ func TestApplyClaudeToolPrefix_ToolChoiceBuiltin(t *testing.T) {
}
}
func TestApplyClaudeToolPrefix_KnownFallbackBuiltinsRemainUnprefixed(t *testing.T) {
for _, builtin := range []string{"web_search", "code_execution", "text_editor", "computer"} {
t.Run(builtin, func(t *testing.T) {
input := []byte(fmt.Sprintf(`{
"tools":[{"name":"Read"}],
"tool_choice":{"type":"tool","name":%q},
"messages":[{"role":"assistant","content":[{"type":"tool_use","name":%q,"id":"toolu_1","input":{}},{"type":"tool_reference","tool_name":%q},{"type":"tool_result","tool_use_id":"toolu_1","content":[{"type":"tool_reference","tool_name":%q}]}]}]
}`, builtin, builtin, builtin, builtin))
out := applyClaudeToolPrefix(input, "proxy_")
if got := gjson.GetBytes(out, "tool_choice.name").String(); got != builtin {
t.Fatalf("tool_choice.name = %q, want %q", got, builtin)
}
if got := gjson.GetBytes(out, "messages.0.content.0.name").String(); got != builtin {
t.Fatalf("messages.0.content.0.name = %q, want %q", got, builtin)
}
if got := gjson.GetBytes(out, "messages.0.content.1.tool_name").String(); got != builtin {
t.Fatalf("messages.0.content.1.tool_name = %q, want %q", got, builtin)
}
if got := gjson.GetBytes(out, "messages.0.content.2.content.0.tool_name").String(); got != builtin {
t.Fatalf("messages.0.content.2.content.0.tool_name = %q, want %q", got, builtin)
}
if got := gjson.GetBytes(out, "tools.0.name").String(); got != "proxy_Read" {
t.Fatalf("tools.0.name = %q, want %q", got, "proxy_Read")
}
})
}
}
func TestStripClaudeToolPrefixFromResponse(t *testing.T) {
input := []byte(`{"content":[{"type":"tool_use","name":"proxy_alpha","id":"t1","input":{}},{"type":"tool_use","name":"bravo","id":"t2","input":{}}]}`)
out := stripClaudeToolPrefixFromResponse(input, "proxy_")
@@ -965,6 +994,28 @@ func TestNormalizeCacheControlTTL_PreservesOriginalBytesWhenNoChange(t *testing.
}
}
func TestNormalizeCacheControlTTL_PreservesKeyOrderWhenModified(t *testing.T) {
payload := []byte(`{"model":"m","messages":[{"role":"user","content":[{"type":"text","text":"u1","cache_control":{"type":"ephemeral","ttl":"1h"}}]}],"tools":[{"name":"t1","cache_control":{"type":"ephemeral"}}],"system":[{"type":"text","text":"s1","cache_control":{"type":"ephemeral"}}]}`)
out := normalizeCacheControlTTL(payload)
if gjson.GetBytes(out, "messages.0.content.0.cache_control.ttl").Exists() {
t.Fatalf("messages.0.content.0.cache_control.ttl should be removed after a default-5m block")
}
outStr := string(out)
idxModel := strings.Index(outStr, `"model"`)
idxMessages := strings.Index(outStr, `"messages"`)
idxTools := strings.Index(outStr, `"tools"`)
idxSystem := strings.Index(outStr, `"system"`)
if idxModel == -1 || idxMessages == -1 || idxTools == -1 || idxSystem == -1 {
t.Fatalf("failed to locate top-level keys in output: %s", outStr)
}
if !(idxModel < idxMessages && idxMessages < idxTools && idxTools < idxSystem) {
t.Fatalf("top-level key order changed:\noriginal: %s\ngot: %s", payload, out)
}
}
func TestEnforceCacheControlLimit_StripsNonLastToolBeforeMessages(t *testing.T) {
payload := []byte(`{
"tools": [
@@ -994,6 +1045,31 @@ func TestEnforceCacheControlLimit_StripsNonLastToolBeforeMessages(t *testing.T)
}
}
func TestEnforceCacheControlLimit_PreservesKeyOrderWhenModified(t *testing.T) {
payload := []byte(`{"model":"m","messages":[{"role":"user","content":[{"type":"text","text":"u1","cache_control":{"type":"ephemeral"}},{"type":"text","text":"u2","cache_control":{"type":"ephemeral"}}]}],"tools":[{"name":"t1","cache_control":{"type":"ephemeral"}},{"name":"t2","cache_control":{"type":"ephemeral"}}],"system":[{"type":"text","text":"s1","cache_control":{"type":"ephemeral"}}]}`)
out := enforceCacheControlLimit(payload, 4)
if got := countCacheControls(out); got != 4 {
t.Fatalf("cache_control count = %d, want 4", got)
}
if gjson.GetBytes(out, "tools.0.cache_control").Exists() {
t.Fatalf("tools.0.cache_control should be removed first (non-last tool)")
}
outStr := string(out)
idxModel := strings.Index(outStr, `"model"`)
idxMessages := strings.Index(outStr, `"messages"`)
idxTools := strings.Index(outStr, `"tools"`)
idxSystem := strings.Index(outStr, `"system"`)
if idxModel == -1 || idxMessages == -1 || idxTools == -1 || idxSystem == -1 {
t.Fatalf("failed to locate top-level keys in output: %s", outStr)
}
if !(idxModel < idxMessages && idxMessages < idxTools && idxTools < idxSystem) {
t.Fatalf("top-level key order changed:\noriginal: %s\ngot: %s", payload, out)
}
}
func TestEnforceCacheControlLimit_ToolOnlyPayloadStillRespectsLimit(t *testing.T) {
payload := []byte(`{
"tools": [
@@ -1833,3 +1909,43 @@ func TestApplyCloaking_PreservesConfiguredStrictModeAndSensitiveWordsWhenModeOmi
t.Fatalf("expected configured sensitive word obfuscation to apply, got %q", got)
}
}
func TestNormalizeClaudeTemperatureForThinking_AdaptiveCoercesToOne(t *testing.T) {
payload := []byte(`{"temperature":0,"thinking":{"type":"adaptive"},"output_config":{"effort":"max"}}`)
out := normalizeClaudeTemperatureForThinking(payload)
if got := gjson.GetBytes(out, "temperature").Float(); got != 1 {
t.Fatalf("temperature = %v, want 1", got)
}
}
func TestNormalizeClaudeTemperatureForThinking_EnabledCoercesToOne(t *testing.T) {
payload := []byte(`{"temperature":0.2,"thinking":{"type":"enabled","budget_tokens":2048}}`)
out := normalizeClaudeTemperatureForThinking(payload)
if got := gjson.GetBytes(out, "temperature").Float(); got != 1 {
t.Fatalf("temperature = %v, want 1", got)
}
}
func TestNormalizeClaudeTemperatureForThinking_NoThinkingLeavesTemperatureAlone(t *testing.T) {
payload := []byte(`{"temperature":0,"messages":[{"role":"user","content":"hi"}]}`)
out := normalizeClaudeTemperatureForThinking(payload)
if got := gjson.GetBytes(out, "temperature").Float(); got != 0 {
t.Fatalf("temperature = %v, want 0", got)
}
}
func TestNormalizeClaudeTemperatureForThinking_AfterForcedToolChoiceKeepsOriginalTemperature(t *testing.T) {
payload := []byte(`{"temperature":0,"thinking":{"type":"adaptive"},"output_config":{"effort":"max"},"tool_choice":{"type":"any"}}`)
out := disableThinkingIfToolChoiceForced(payload)
out = normalizeClaudeTemperatureForThinking(out)
if gjson.GetBytes(out, "thinking").Exists() {
t.Fatalf("thinking should be removed when tool_choice forces tool use")
}
if got := gjson.GetBytes(out, "temperature").Float(); got != 0 {
t.Fatalf("temperature = %v, want 0", got)
}
}
+46 -4
View File
@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"sort"
"strings"
"time"
@@ -167,22 +168,63 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
helps.AppendAPIResponseChunk(ctx, e.cfg, data)
lines := bytes.Split(data, []byte("\n"))
outputItemsByIndex := make(map[int64][]byte)
var outputItemsFallback [][]byte
for _, line := range lines {
if !bytes.HasPrefix(line, dataTag) {
continue
}
line = bytes.TrimSpace(line[5:])
if gjson.GetBytes(line, "type").String() != "response.completed" {
eventData := bytes.TrimSpace(line[5:])
eventType := gjson.GetBytes(eventData, "type").String()
if eventType == "response.output_item.done" {
itemResult := gjson.GetBytes(eventData, "item")
if !itemResult.Exists() || itemResult.Type != gjson.JSON {
continue
}
outputIndexResult := gjson.GetBytes(eventData, "output_index")
if outputIndexResult.Exists() {
outputItemsByIndex[outputIndexResult.Int()] = []byte(itemResult.Raw)
} else {
outputItemsFallback = append(outputItemsFallback, []byte(itemResult.Raw))
}
continue
}
if detail, ok := helps.ParseCodexUsage(line); ok {
if eventType != "response.completed" {
continue
}
if detail, ok := helps.ParseCodexUsage(eventData); ok {
reporter.Publish(ctx, detail)
}
completedData := eventData
outputResult := gjson.GetBytes(completedData, "response.output")
shouldPatchOutput := (!outputResult.Exists() || !outputResult.IsArray() || len(outputResult.Array()) == 0) && (len(outputItemsByIndex) > 0 || len(outputItemsFallback) > 0)
if shouldPatchOutput {
completedDataPatched := completedData
completedDataPatched, _ = sjson.SetRawBytes(completedDataPatched, "response.output", []byte(`[]`))
indexes := make([]int64, 0, len(outputItemsByIndex))
for idx := range outputItemsByIndex {
indexes = append(indexes, idx)
}
sort.Slice(indexes, func(i, j int) bool {
return indexes[i] < indexes[j]
})
for _, idx := range indexes {
completedDataPatched, _ = sjson.SetRawBytes(completedDataPatched, "response.output.-1", outputItemsByIndex[idx])
}
for _, item := range outputItemsFallback {
completedDataPatched, _ = sjson.SetRawBytes(completedDataPatched, "response.output.-1", item)
}
completedData = completedDataPatched
}
var param any
out := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, originalPayload, body, line, &param)
out := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, originalPayload, body, completedData, &param)
resp = cliproxyexecutor.Response{Payload: out, Headers: httpResp.Header.Clone()}
return resp, nil
}
@@ -0,0 +1,46 @@
package executor
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
"github.com/tidwall/gjson"
)
func TestCodexExecutorExecute_EmptyStreamCompletionOutputUsesOutputItemDone(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
_, _ = w.Write([]byte("data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"ok\"}]},\"output_index\":0}\n"))
_, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"created_at\":1775555723,\"status\":\"completed\",\"model\":\"gpt-5.4-mini-2026-03-17\",\"output\":[],\"usage\":{\"input_tokens\":8,\"output_tokens\":28,\"total_tokens\":36}}}\n\n"))
}))
defer server.Close()
executor := NewCodexExecutor(&config.Config{})
auth := &cliproxyauth.Auth{Attributes: map[string]string{
"base_url": server.URL,
"api_key": "test",
}}
resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{
Model: "gpt-5.4-mini",
Payload: []byte(`{"model":"gpt-5.4-mini","messages":[{"role":"user","content":"Say ok"}]}`),
}, cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString("openai"),
Stream: false,
})
if err != nil {
t.Fatalf("Execute error: %v", err)
}
gotContent := gjson.GetBytes(resp.Payload, "choices.0.message.content").String()
if gotContent != "ok" {
t.Fatalf("choices.0.message.content = %q, want %q; payload=%s", gotContent, "ok", string(resp.Payload))
}
}
@@ -734,7 +734,7 @@ func newProxyAwareWebsocketDialer(cfg *config.Config, auth *cliproxyauth.Auth) *
}
switch setting.URL.Scheme {
case "socks5":
case "socks5", "socks5h":
var proxyAuth *proxy.Auth
if setting.URL.User != nil {
username := setting.URL.User.Username()
@@ -0,0 +1,38 @@
package helps
import "github.com/tidwall/gjson"
var defaultClaudeBuiltinToolNames = []string{
"web_search",
"code_execution",
"text_editor",
"computer",
}
func newClaudeBuiltinToolRegistry() map[string]bool {
registry := make(map[string]bool, len(defaultClaudeBuiltinToolNames))
for _, name := range defaultClaudeBuiltinToolNames {
registry[name] = true
}
return registry
}
func AugmentClaudeBuiltinToolRegistry(body []byte, registry map[string]bool) map[string]bool {
if registry == nil {
registry = newClaudeBuiltinToolRegistry()
}
tools := gjson.GetBytes(body, "tools")
if !tools.Exists() || !tools.IsArray() {
return registry
}
tools.ForEach(func(_, tool gjson.Result) bool {
if tool.Get("type").String() == "" {
return true
}
if name := tool.Get("name").String(); name != "" {
registry[name] = true
}
return true
})
return registry
}
@@ -0,0 +1,32 @@
package helps
import "testing"
func TestClaudeBuiltinToolRegistry_DefaultSeedFallback(t *testing.T) {
registry := AugmentClaudeBuiltinToolRegistry(nil, nil)
for _, name := range defaultClaudeBuiltinToolNames {
if !registry[name] {
t.Fatalf("default builtin %q missing from fallback registry", name)
}
}
}
func TestClaudeBuiltinToolRegistry_AugmentsTypedBuiltinsFromBody(t *testing.T) {
registry := AugmentClaudeBuiltinToolRegistry([]byte(`{
"tools": [
{"type": "web_search_20250305", "name": "web_search"},
{"type": "custom_builtin_20250401", "name": "special_builtin"},
{"name": "Read"}
]
}`), nil)
if !registry["web_search"] {
t.Fatal("expected default typed builtin web_search in registry")
}
if !registry["special_builtin"] {
t.Fatal("expected typed builtin from body to be added to registry")
}
if registry["Read"] {
t.Fatal("expected untyped custom tool to stay out of builtin registry")
}
}
@@ -298,6 +298,14 @@ func (e *OpenAICompatExecutor) ExecuteStream(ctx context.Context, auth *cliproxy
helps.RecordAPIResponseError(ctx, e.cfg, errScan)
reporter.PublishFailure(ctx)
out <- cliproxyexecutor.StreamChunk{Err: errScan}
} else {
// In case the upstream close the stream without a terminal [DONE] marker.
// Feed a synthetic done marker through the translator so pending
// response.completed events are still emitted exactly once.
chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, opts.OriginalRequest, translated, []byte("data: [DONE]"), &param)
for i := range chunks {
out <- cliproxyexecutor.StreamChunk{Payload: chunks[i]}
}
}
// Ensure we record the request if no usage chunk was ever seen
reporter.EnsurePublished(ctx)
+87 -24
View File
@@ -30,7 +30,7 @@ const (
qwenRateLimitWindow = time.Minute // sliding window duration
)
var qwenDefaultSystemMessage = []byte(`{"role":"system","content":[{"type":"text","text":"","cache_control":{"type":"ephemeral"}}]}`)
var qwenDefaultSystemMessage = []byte(`{"role":"system","content":[{"type":"text","text":"You are Qwen Code.","cache_control":{"type":"ephemeral"}}]}`)
// qwenBeijingLoc caches the Beijing timezone to avoid repeated LoadLocation syscalls.
var qwenBeijingLoc = func() *time.Location {
@@ -172,38 +172,101 @@ func timeUntilNextDay() time.Duration {
return tomorrow.Sub(now)
}
// ensureQwenSystemMessage prepends a default system message if none exists in "messages".
// ensureQwenSystemMessage ensures the request has a single system message at the beginning.
// It always injects the default system prompt and merges any user-provided system messages
// into the injected system message content to satisfy Qwen's strict message ordering rules.
func ensureQwenSystemMessage(payload []byte) ([]byte, error) {
messages := gjson.GetBytes(payload, "messages")
if messages.Exists() && messages.IsArray() {
for _, msg := range messages.Array() {
if strings.EqualFold(msg.Get("role").String(), "system") {
return payload, nil
isInjectedSystemPart := func(part gjson.Result) bool {
if !part.Exists() || !part.IsObject() {
return false
}
if !strings.EqualFold(part.Get("type").String(), "text") {
return false
}
if !strings.EqualFold(part.Get("cache_control.type").String(), "ephemeral") {
return false
}
text := part.Get("text").String()
return text == "" || text == "You are Qwen Code."
}
defaultParts := gjson.ParseBytes(qwenDefaultSystemMessage).Get("content")
var systemParts []any
if defaultParts.Exists() && defaultParts.IsArray() {
for _, part := range defaultParts.Array() {
systemParts = append(systemParts, part.Value())
}
}
if len(systemParts) == 0 {
systemParts = append(systemParts, map[string]any{
"type": "text",
"text": "You are Qwen Code.",
"cache_control": map[string]any{
"type": "ephemeral",
},
})
}
appendSystemContent := func(content gjson.Result) {
makeTextPart := func(text string) map[string]any {
return map[string]any{
"type": "text",
"text": text,
}
}
var buf bytes.Buffer
buf.WriteByte('[')
buf.Write(qwenDefaultSystemMessage)
for _, msg := range messages.Array() {
buf.WriteByte(',')
buf.WriteString(msg.Raw)
if !content.Exists() || content.Type == gjson.Null {
return
}
buf.WriteByte(']')
updated, errSet := sjson.SetRawBytes(payload, "messages", buf.Bytes())
if errSet != nil {
return nil, fmt.Errorf("qwen executor: set default system message failed: %w", errSet)
if content.IsArray() {
for _, part := range content.Array() {
if part.Type == gjson.String {
systemParts = append(systemParts, makeTextPart(part.String()))
continue
}
if isInjectedSystemPart(part) {
continue
}
systemParts = append(systemParts, part.Value())
}
return
}
return updated, nil
if content.Type == gjson.String {
systemParts = append(systemParts, makeTextPart(content.String()))
return
}
if content.IsObject() {
if isInjectedSystemPart(content) {
return
}
systemParts = append(systemParts, content.Value())
return
}
systemParts = append(systemParts, makeTextPart(content.String()))
}
var buf bytes.Buffer
buf.WriteByte('[')
buf.Write(qwenDefaultSystemMessage)
buf.WriteByte(']')
updated, errSet := sjson.SetRawBytes(payload, "messages", buf.Bytes())
messages := gjson.GetBytes(payload, "messages")
var nonSystemMessages []any
if messages.Exists() && messages.IsArray() {
for _, msg := range messages.Array() {
if strings.EqualFold(msg.Get("role").String(), "system") {
appendSystemContent(msg.Get("content"))
continue
}
nonSystemMessages = append(nonSystemMessages, msg.Value())
}
}
newMessages := make([]any, 0, 1+len(nonSystemMessages))
newMessages = append(newMessages, map[string]any{
"role": "system",
"content": systemParts,
})
newMessages = append(newMessages, nonSystemMessages...)
updated, errSet := sjson.SetBytes(payload, "messages", newMessages)
if errSet != nil {
return nil, fmt.Errorf("qwen executor: set default system message failed: %w", errSet)
return nil, fmt.Errorf("qwen executor: set system message failed: %w", errSet)
}
return updated, nil
}
@@ -4,6 +4,7 @@ import (
"testing"
"github.com/router-for-me/CLIProxyAPI/v6/internal/thinking"
"github.com/tidwall/gjson"
)
func TestQwenExecutorParseSuffix(t *testing.T) {
@@ -28,3 +29,123 @@ func TestQwenExecutorParseSuffix(t *testing.T) {
})
}
}
func TestEnsureQwenSystemMessage_MergeStringSystem(t *testing.T) {
payload := []byte(`{
"model": "qwen3.6-plus",
"stream": true,
"messages": [
{ "role": "system", "content": "ABCDEFG" },
{ "role": "user", "content": [ { "type": "text", "text": "你好" } ] }
]
}`)
out, err := ensureQwenSystemMessage(payload)
if err != nil {
t.Fatalf("ensureQwenSystemMessage() error = %v", err)
}
msgs := gjson.GetBytes(out, "messages").Array()
if len(msgs) != 2 {
t.Fatalf("messages length = %d, want 2", len(msgs))
}
if msgs[0].Get("role").String() != "system" {
t.Fatalf("messages[0].role = %q, want %q", msgs[0].Get("role").String(), "system")
}
parts := msgs[0].Get("content").Array()
if len(parts) != 2 {
t.Fatalf("messages[0].content length = %d, want 2", len(parts))
}
if parts[0].Get("text").String() != "You are Qwen Code." || parts[0].Get("cache_control.type").String() != "ephemeral" {
t.Fatalf("messages[0].content[0] = %s, want injected system part", parts[0].Raw)
}
if parts[1].Get("type").String() != "text" || parts[1].Get("text").String() != "ABCDEFG" {
t.Fatalf("messages[0].content[1] = %s, want text part with ABCDEFG", parts[1].Raw)
}
if msgs[1].Get("role").String() != "user" {
t.Fatalf("messages[1].role = %q, want %q", msgs[1].Get("role").String(), "user")
}
}
func TestEnsureQwenSystemMessage_MergeObjectSystem(t *testing.T) {
payload := []byte(`{
"messages": [
{ "role": "system", "content": { "type": "text", "text": "ABCDEFG" } },
{ "role": "user", "content": [ { "type": "text", "text": "你好" } ] }
]
}`)
out, err := ensureQwenSystemMessage(payload)
if err != nil {
t.Fatalf("ensureQwenSystemMessage() error = %v", err)
}
msgs := gjson.GetBytes(out, "messages").Array()
if len(msgs) != 2 {
t.Fatalf("messages length = %d, want 2", len(msgs))
}
parts := msgs[0].Get("content").Array()
if len(parts) != 2 {
t.Fatalf("messages[0].content length = %d, want 2", len(parts))
}
if parts[1].Get("text").String() != "ABCDEFG" {
t.Fatalf("messages[0].content[1].text = %q, want %q", parts[1].Get("text").String(), "ABCDEFG")
}
}
func TestEnsureQwenSystemMessage_PrependsWhenMissing(t *testing.T) {
payload := []byte(`{
"messages": [
{ "role": "user", "content": [ { "type": "text", "text": "你好" } ] }
]
}`)
out, err := ensureQwenSystemMessage(payload)
if err != nil {
t.Fatalf("ensureQwenSystemMessage() error = %v", err)
}
msgs := gjson.GetBytes(out, "messages").Array()
if len(msgs) != 2 {
t.Fatalf("messages length = %d, want 2", len(msgs))
}
if msgs[0].Get("role").String() != "system" {
t.Fatalf("messages[0].role = %q, want %q", msgs[0].Get("role").String(), "system")
}
if !msgs[0].Get("content").IsArray() || len(msgs[0].Get("content").Array()) == 0 {
t.Fatalf("messages[0].content = %s, want non-empty array", msgs[0].Get("content").Raw)
}
if msgs[1].Get("role").String() != "user" {
t.Fatalf("messages[1].role = %q, want %q", msgs[1].Get("role").String(), "user")
}
}
func TestEnsureQwenSystemMessage_MergesMultipleSystemMessages(t *testing.T) {
payload := []byte(`{
"messages": [
{ "role": "system", "content": "A" },
{ "role": "user", "content": [ { "type": "text", "text": "hi" } ] },
{ "role": "system", "content": "B" }
]
}`)
out, err := ensureQwenSystemMessage(payload)
if err != nil {
t.Fatalf("ensureQwenSystemMessage() error = %v", err)
}
msgs := gjson.GetBytes(out, "messages").Array()
if len(msgs) != 2 {
t.Fatalf("messages length = %d, want 2", len(msgs))
}
parts := msgs[0].Get("content").Array()
if len(parts) != 3 {
t.Fatalf("messages[0].content length = %d, want 3", len(parts))
}
if parts[1].Get("text").String() != "A" {
t.Fatalf("messages[0].content[1].text = %q, want %q", parts[1].Get("text").String(), "A")
}
if parts[2].Get("text").String() != "B" {
t.Fatalf("messages[0].content[2].text = %q, want %q", parts[2].Get("text").String(), "B")
}
}
+242 -5
View File
@@ -32,16 +32,24 @@ type GitTokenStore struct {
repoDir string
configDir string
remote string
branch string
username string
password string
lastGC time.Time
}
type resolvedRemoteBranch struct {
name plumbing.ReferenceName
hash plumbing.Hash
}
// NewGitTokenStore creates a token store that saves credentials to disk through the
// TokenStorage implementation embedded in the token record.
func NewGitTokenStore(remote, username, password string) *GitTokenStore {
// When branch is non-empty, clone/pull/push operations target that branch instead of the remote default.
func NewGitTokenStore(remote, username, password, branch string) *GitTokenStore {
return &GitTokenStore{
remote: remote,
branch: strings.TrimSpace(branch),
username: username,
password: password,
}
@@ -120,7 +128,11 @@ func (s *GitTokenStore) EnsureRepository() error {
s.dirLock.Unlock()
return fmt.Errorf("git token store: create repo dir: %w", errMk)
}
if _, errClone := git.PlainClone(repoDir, &git.CloneOptions{Auth: authMethod, URL: s.remote}); errClone != nil {
cloneOpts := &git.CloneOptions{Auth: authMethod, URL: s.remote}
if s.branch != "" {
cloneOpts.ReferenceName = plumbing.NewBranchReferenceName(s.branch)
}
if _, errClone := git.PlainClone(repoDir, cloneOpts); errClone != nil {
if errors.Is(errClone, transport.ErrEmptyRemoteRepository) {
_ = os.RemoveAll(gitDir)
repo, errInit := git.PlainInit(repoDir, false)
@@ -128,6 +140,13 @@ func (s *GitTokenStore) EnsureRepository() error {
s.dirLock.Unlock()
return fmt.Errorf("git token store: init empty repo: %w", errInit)
}
if s.branch != "" {
headRef := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName(s.branch))
if errHead := repo.Storer.SetReference(headRef); errHead != nil {
s.dirLock.Unlock()
return fmt.Errorf("git token store: set head to branch %s: %w", s.branch, errHead)
}
}
if _, errRemote := repo.Remote("origin"); errRemote != nil {
if _, errCreate := repo.CreateRemote(&config.RemoteConfig{
Name: "origin",
@@ -176,16 +195,39 @@ func (s *GitTokenStore) EnsureRepository() error {
s.dirLock.Unlock()
return fmt.Errorf("git token store: worktree: %w", errWorktree)
}
if errPull := worktree.Pull(&git.PullOptions{Auth: authMethod, RemoteName: "origin"}); errPull != nil {
if s.branch != "" {
if errCheckout := s.checkoutConfiguredBranch(repo, worktree, authMethod); errCheckout != nil {
s.dirLock.Unlock()
return errCheckout
}
} else {
// When branch is unset, ensure the working tree follows the remote default branch
if err := checkoutRemoteDefaultBranch(repo, worktree, authMethod); err != nil {
if !shouldFallbackToCurrentBranch(repo, err) {
s.dirLock.Unlock()
return fmt.Errorf("git token store: checkout remote default: %w", err)
}
}
}
pullOpts := &git.PullOptions{Auth: authMethod, RemoteName: "origin"}
if s.branch != "" {
pullOpts.ReferenceName = plumbing.NewBranchReferenceName(s.branch)
}
if errPull := worktree.Pull(pullOpts); errPull != nil {
switch {
case errors.Is(errPull, git.NoErrAlreadyUpToDate),
errors.Is(errPull, git.ErrUnstagedChanges),
errors.Is(errPull, git.ErrNonFastForwardUpdate):
// Ignore clean syncs, local edits, and remote divergence—local changes win.
case errors.Is(errPull, transport.ErrAuthenticationRequired),
errors.Is(errPull, plumbing.ErrReferenceNotFound),
errors.Is(errPull, transport.ErrEmptyRemoteRepository):
// Ignore authentication prompts and empty remote references on initial sync.
case errors.Is(errPull, plumbing.ErrReferenceNotFound):
if s.branch != "" {
s.dirLock.Unlock()
return fmt.Errorf("git token store: pull: %w", errPull)
}
// Ignore missing references only when following the remote default branch.
default:
s.dirLock.Unlock()
return fmt.Errorf("git token store: pull: %w", errPull)
@@ -554,6 +596,192 @@ func (s *GitTokenStore) relativeToRepo(path string) (string, error) {
return rel, nil
}
func (s *GitTokenStore) checkoutConfiguredBranch(repo *git.Repository, worktree *git.Worktree, authMethod transport.AuthMethod) error {
branchRefName := plumbing.NewBranchReferenceName(s.branch)
headRef, errHead := repo.Head()
switch {
case errHead == nil && headRef.Name() == branchRefName:
return nil
case errHead != nil && !errors.Is(errHead, plumbing.ErrReferenceNotFound):
return fmt.Errorf("git token store: get head: %w", errHead)
}
if err := worktree.Checkout(&git.CheckoutOptions{Branch: branchRefName}); err == nil {
return nil
} else if _, errRef := repo.Reference(branchRefName, true); errRef == nil {
return fmt.Errorf("git token store: checkout branch %s: %w", s.branch, err)
} else if !errors.Is(errRef, plumbing.ErrReferenceNotFound) {
return fmt.Errorf("git token store: inspect branch %s: %w", s.branch, errRef)
} else if err := s.checkoutConfiguredRemoteTrackingBranch(repo, worktree, branchRefName, authMethod); err != nil {
return fmt.Errorf("git token store: checkout branch %s: %w", s.branch, err)
}
return nil
}
func (s *GitTokenStore) checkoutConfiguredRemoteTrackingBranch(repo *git.Repository, worktree *git.Worktree, branchRefName plumbing.ReferenceName, authMethod transport.AuthMethod) error {
remoteRefName := plumbing.ReferenceName("refs/remotes/origin/" + s.branch)
remoteRef, err := repo.Reference(remoteRefName, true)
if errors.Is(err, plumbing.ErrReferenceNotFound) {
if errSync := syncRemoteReferences(repo, authMethod); errSync != nil {
return fmt.Errorf("sync remote refs: %w", errSync)
}
remoteRef, err = repo.Reference(remoteRefName, true)
}
if err != nil {
return err
}
if err := worktree.Checkout(&git.CheckoutOptions{Branch: branchRefName, Create: true, Hash: remoteRef.Hash()}); err != nil {
return err
}
cfg, err := repo.Config()
if err != nil {
return fmt.Errorf("git token store: repo config: %w", err)
}
if _, ok := cfg.Branches[s.branch]; !ok {
cfg.Branches[s.branch] = &config.Branch{Name: s.branch}
}
cfg.Branches[s.branch].Remote = "origin"
cfg.Branches[s.branch].Merge = branchRefName
if err := repo.SetConfig(cfg); err != nil {
return fmt.Errorf("git token store: set branch config: %w", err)
}
return nil
}
func syncRemoteReferences(repo *git.Repository, authMethod transport.AuthMethod) error {
if err := repo.Fetch(&git.FetchOptions{Auth: authMethod, RemoteName: "origin"}); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return err
}
return nil
}
// resolveRemoteDefaultBranch queries the origin remote to determine the remote's default branch
// (the target of HEAD) and returns the corresponding local branch reference name (e.g. refs/heads/master).
func resolveRemoteDefaultBranch(repo *git.Repository, authMethod transport.AuthMethod) (resolvedRemoteBranch, error) {
if err := syncRemoteReferences(repo, authMethod); err != nil {
return resolvedRemoteBranch{}, fmt.Errorf("resolve remote default: sync remote refs: %w", err)
}
remote, err := repo.Remote("origin")
if err != nil {
return resolvedRemoteBranch{}, fmt.Errorf("resolve remote default: get remote: %w", err)
}
refs, err := remote.List(&git.ListOptions{Auth: authMethod})
if err != nil {
if resolved, ok := resolveRemoteDefaultBranchFromLocal(repo); ok {
return resolved, nil
}
return resolvedRemoteBranch{}, fmt.Errorf("resolve remote default: list remote refs: %w", err)
}
for _, r := range refs {
if r.Name() == plumbing.HEAD {
if r.Type() == plumbing.SymbolicReference {
if target, ok := normalizeRemoteBranchReference(r.Target()); ok {
return resolvedRemoteBranch{name: target}, nil
}
}
s := r.String()
if idx := strings.Index(s, "->"); idx != -1 {
if target, ok := normalizeRemoteBranchReference(plumbing.ReferenceName(strings.TrimSpace(s[idx+2:]))); ok {
return resolvedRemoteBranch{name: target}, nil
}
}
}
}
if resolved, ok := resolveRemoteDefaultBranchFromLocal(repo); ok {
return resolved, nil
}
for _, r := range refs {
if normalized, ok := normalizeRemoteBranchReference(r.Name()); ok {
return resolvedRemoteBranch{name: normalized, hash: r.Hash()}, nil
}
}
return resolvedRemoteBranch{}, fmt.Errorf("resolve remote default: remote default branch not found")
}
func resolveRemoteDefaultBranchFromLocal(repo *git.Repository) (resolvedRemoteBranch, bool) {
ref, err := repo.Reference(plumbing.ReferenceName("refs/remotes/origin/HEAD"), true)
if err != nil || ref.Type() != plumbing.SymbolicReference {
return resolvedRemoteBranch{}, false
}
target, ok := normalizeRemoteBranchReference(ref.Target())
if !ok {
return resolvedRemoteBranch{}, false
}
return resolvedRemoteBranch{name: target}, true
}
func normalizeRemoteBranchReference(name plumbing.ReferenceName) (plumbing.ReferenceName, bool) {
switch {
case strings.HasPrefix(name.String(), "refs/heads/"):
return name, true
case strings.HasPrefix(name.String(), "refs/remotes/origin/"):
return plumbing.NewBranchReferenceName(strings.TrimPrefix(name.String(), "refs/remotes/origin/")), true
default:
return "", false
}
}
func shouldFallbackToCurrentBranch(repo *git.Repository, err error) bool {
if !errors.Is(err, transport.ErrAuthenticationRequired) && !errors.Is(err, transport.ErrEmptyRemoteRepository) {
return false
}
_, headErr := repo.Head()
return headErr == nil
}
// checkoutRemoteDefaultBranch ensures the working tree is checked out to the remote's default branch
// (the branch target of origin/HEAD). If the local branch does not exist it will be created to track
// the remote branch.
func checkoutRemoteDefaultBranch(repo *git.Repository, worktree *git.Worktree, authMethod transport.AuthMethod) error {
resolved, err := resolveRemoteDefaultBranch(repo, authMethod)
if err != nil {
return err
}
branchRefName := resolved.name
// If HEAD already points to the desired branch, nothing to do.
headRef, errHead := repo.Head()
if errHead == nil && headRef.Name() == branchRefName {
return nil
}
// If local branch exists, attempt a checkout
if _, err := repo.Reference(branchRefName, true); err == nil {
if err := worktree.Checkout(&git.CheckoutOptions{Branch: branchRefName}); err != nil {
return fmt.Errorf("checkout branch %s: %w", branchRefName.String(), err)
}
return nil
}
// Try to find the corresponding remote tracking ref (refs/remotes/origin/<name>)
branchShort := strings.TrimPrefix(branchRefName.String(), "refs/heads/")
remoteRefName := plumbing.ReferenceName("refs/remotes/origin/" + branchShort)
hash := resolved.hash
if remoteRef, err := repo.Reference(remoteRefName, true); err == nil {
hash = remoteRef.Hash()
} else if err != nil && !errors.Is(err, plumbing.ErrReferenceNotFound) {
return fmt.Errorf("checkout remote default: remote ref %s: %w", remoteRefName.String(), err)
}
if hash == plumbing.ZeroHash {
return fmt.Errorf("checkout remote default: remote ref %s not found", remoteRefName.String())
}
if err := worktree.Checkout(&git.CheckoutOptions{Branch: branchRefName, Create: true, Hash: hash}); err != nil {
return fmt.Errorf("checkout create branch %s: %w", branchRefName.String(), err)
}
cfg, err := repo.Config()
if err != nil {
return fmt.Errorf("git token store: repo config: %w", err)
}
if _, ok := cfg.Branches[branchShort]; !ok {
cfg.Branches[branchShort] = &config.Branch{Name: branchShort}
}
cfg.Branches[branchShort].Remote = "origin"
cfg.Branches[branchShort].Merge = branchRefName
if err := repo.SetConfig(cfg); err != nil {
return fmt.Errorf("git token store: set branch config: %w", err)
}
return nil
}
func (s *GitTokenStore) commitAndPushLocked(message string, relPaths ...string) error {
repoDir := s.repoDirSnapshot()
if repoDir == "" {
@@ -619,7 +847,16 @@ func (s *GitTokenStore) commitAndPushLocked(message string, relPaths ...string)
return errRewrite
}
s.maybeRunGC(repo)
if err = repo.Push(&git.PushOptions{Auth: s.gitAuth(), Force: true}); err != nil {
pushOpts := &git.PushOptions{Auth: s.gitAuth(), Force: true}
if s.branch != "" {
pushOpts.RefSpecs = []config.RefSpec{config.RefSpec("refs/heads/" + s.branch + ":refs/heads/" + s.branch)}
} else {
// When branch is unset, pin push to the currently checked-out branch.
if headRef, err := repo.Head(); err == nil {
pushOpts.RefSpecs = []config.RefSpec{config.RefSpec(headRef.Name().String() + ":" + headRef.Name().String())}
}
}
if err = repo.Push(pushOpts); err != nil {
if errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil
}
+585
View File
@@ -0,0 +1,585 @@
package store
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/go-git/go-git/v6"
gitconfig "github.com/go-git/go-git/v6/config"
"github.com/go-git/go-git/v6/plumbing"
"github.com/go-git/go-git/v6/plumbing/object"
)
type testBranchSpec struct {
name string
contents string
}
func TestEnsureRepositoryUsesRemoteDefaultBranchWhenBranchNotConfigured(t *testing.T) {
root := t.TempDir()
remoteDir := setupGitRemoteRepository(t, root, "trunk",
testBranchSpec{name: "trunk", contents: "remote default branch\n"},
testBranchSpec{name: "release/2026", contents: "release branch\n"},
)
store := NewGitTokenStore(remoteDir, "", "", "")
store.SetBaseDir(filepath.Join(root, "workspace", "auths"))
if err := store.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "trunk", "remote default branch\n")
advanceRemoteBranch(t, filepath.Join(root, "seed"), remoteDir, "trunk", "remote default branch updated\n", "advance trunk")
advanceRemoteBranch(t, filepath.Join(root, "seed"), remoteDir, "release/2026", "release branch updated\n", "advance release")
if err := store.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository second call: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "trunk", "remote default branch updated\n")
assertRemoteHeadBranch(t, remoteDir, "trunk")
}
func TestEnsureRepositoryUsesConfiguredBranchWhenExplicitlySet(t *testing.T) {
root := t.TempDir()
remoteDir := setupGitRemoteRepository(t, root, "trunk",
testBranchSpec{name: "trunk", contents: "remote default branch\n"},
testBranchSpec{name: "release/2026", contents: "release branch\n"},
)
store := NewGitTokenStore(remoteDir, "", "", "release/2026")
store.SetBaseDir(filepath.Join(root, "workspace", "auths"))
if err := store.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "release/2026", "release branch\n")
advanceRemoteBranch(t, filepath.Join(root, "seed"), remoteDir, "trunk", "remote default branch updated\n", "advance trunk")
advanceRemoteBranch(t, filepath.Join(root, "seed"), remoteDir, "release/2026", "release branch updated\n", "advance release")
if err := store.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository second call: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "release/2026", "release branch updated\n")
assertRemoteHeadBranch(t, remoteDir, "trunk")
}
func TestEnsureRepositoryReturnsErrorForMissingConfiguredBranch(t *testing.T) {
root := t.TempDir()
remoteDir := setupGitRemoteRepository(t, root, "trunk",
testBranchSpec{name: "trunk", contents: "remote default branch\n"},
)
store := NewGitTokenStore(remoteDir, "", "", "missing-branch")
store.SetBaseDir(filepath.Join(root, "workspace", "auths"))
err := store.EnsureRepository()
if err == nil {
t.Fatal("EnsureRepository succeeded, want error for nonexistent configured branch")
}
assertRemoteHeadBranch(t, remoteDir, "trunk")
}
func TestEnsureRepositoryReturnsErrorForMissingConfiguredBranchOnExistingRepositoryPull(t *testing.T) {
root := t.TempDir()
remoteDir := setupGitRemoteRepository(t, root, "trunk",
testBranchSpec{name: "trunk", contents: "remote default branch\n"},
)
baseDir := filepath.Join(root, "workspace", "auths")
store := NewGitTokenStore(remoteDir, "", "", "")
store.SetBaseDir(baseDir)
if err := store.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository initial clone: %v", err)
}
reopened := NewGitTokenStore(remoteDir, "", "", "missing-branch")
reopened.SetBaseDir(baseDir)
err := reopened.EnsureRepository()
if err == nil {
t.Fatal("EnsureRepository succeeded on reopen, want error for nonexistent configured branch")
}
assertRepositoryHeadBranch(t, filepath.Join(root, "workspace"), "trunk")
assertRemoteHeadBranch(t, remoteDir, "trunk")
}
func TestEnsureRepositoryInitializesEmptyRemoteUsingConfiguredBranch(t *testing.T) {
root := t.TempDir()
remoteDir := filepath.Join(root, "remote.git")
if _, err := git.PlainInit(remoteDir, true); err != nil {
t.Fatalf("init bare remote: %v", err)
}
branch := "feature/gemini-fix"
store := NewGitTokenStore(remoteDir, "", "", branch)
store.SetBaseDir(filepath.Join(root, "workspace", "auths"))
if err := store.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository: %v", err)
}
assertRepositoryHeadBranch(t, filepath.Join(root, "workspace"), branch)
assertRemoteBranchExistsWithCommit(t, remoteDir, branch)
assertRemoteBranchDoesNotExist(t, remoteDir, "master")
}
func TestEnsureRepositoryExistingRepoSwitchesToConfiguredBranch(t *testing.T) {
root := t.TempDir()
remoteDir := setupGitRemoteRepository(t, root, "master",
testBranchSpec{name: "master", contents: "remote master branch\n"},
testBranchSpec{name: "develop", contents: "remote develop branch\n"},
)
baseDir := filepath.Join(root, "workspace", "auths")
store := NewGitTokenStore(remoteDir, "", "", "")
store.SetBaseDir(baseDir)
if err := store.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository initial clone: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "master", "remote master branch\n")
reopened := NewGitTokenStore(remoteDir, "", "", "develop")
reopened.SetBaseDir(baseDir)
if err := reopened.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository reopen: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "develop", "remote develop branch\n")
workspaceDir := filepath.Join(root, "workspace")
if err := os.WriteFile(filepath.Join(workspaceDir, "branch.txt"), []byte("local develop update\n"), 0o600); err != nil {
t.Fatalf("write local branch marker: %v", err)
}
reopened.mu.Lock()
err := reopened.commitAndPushLocked("Update develop branch marker", "branch.txt")
reopened.mu.Unlock()
if err != nil {
t.Fatalf("commitAndPushLocked: %v", err)
}
assertRepositoryHeadBranch(t, workspaceDir, "develop")
assertRemoteBranchContents(t, remoteDir, "develop", "local develop update\n")
assertRemoteBranchContents(t, remoteDir, "master", "remote master branch\n")
}
func TestEnsureRepositoryExistingRepoSwitchesToConfiguredBranchCreatedAfterClone(t *testing.T) {
root := t.TempDir()
remoteDir := setupGitRemoteRepository(t, root, "master",
testBranchSpec{name: "master", contents: "remote master branch\n"},
)
baseDir := filepath.Join(root, "workspace", "auths")
store := NewGitTokenStore(remoteDir, "", "", "")
store.SetBaseDir(baseDir)
if err := store.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository initial clone: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "master", "remote master branch\n")
advanceRemoteBranchFromNewBranch(t, filepath.Join(root, "seed"), remoteDir, "release/2026", "release branch\n", "create release")
reopened := NewGitTokenStore(remoteDir, "", "", "release/2026")
reopened.SetBaseDir(baseDir)
if err := reopened.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository reopen: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "release/2026", "release branch\n")
}
func TestEnsureRepositoryResetsToRemoteDefaultWhenBranchUnset(t *testing.T) {
root := t.TempDir()
remoteDir := setupGitRemoteRepository(t, root, "master",
testBranchSpec{name: "master", contents: "remote master branch\n"},
testBranchSpec{name: "develop", contents: "remote develop branch\n"},
)
baseDir := filepath.Join(root, "workspace", "auths")
// First store pins to develop and prepares local workspace
storePinned := NewGitTokenStore(remoteDir, "", "", "develop")
storePinned.SetBaseDir(baseDir)
if err := storePinned.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository pinned: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "develop", "remote develop branch\n")
// Second store has branch unset and should reset local workspace to remote default (master)
storeDefault := NewGitTokenStore(remoteDir, "", "", "")
storeDefault.SetBaseDir(baseDir)
if err := storeDefault.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository default: %v", err)
}
// Local HEAD should now follow remote default (master)
assertRepositoryHeadBranch(t, filepath.Join(root, "workspace"), "master")
// Make a local change and push using the store with branch unset; push should update remote master
workspaceDir := filepath.Join(root, "workspace")
if err := os.WriteFile(filepath.Join(workspaceDir, "branch.txt"), []byte("local master update\n"), 0o600); err != nil {
t.Fatalf("write local master marker: %v", err)
}
storeDefault.mu.Lock()
if err := storeDefault.commitAndPushLocked("Update master marker", "branch.txt"); err != nil {
storeDefault.mu.Unlock()
t.Fatalf("commitAndPushLocked: %v", err)
}
storeDefault.mu.Unlock()
assertRemoteBranchContents(t, remoteDir, "master", "local master update\n")
}
func TestEnsureRepositoryFollowsRenamedRemoteDefaultBranchWhenAvailable(t *testing.T) {
root := t.TempDir()
remoteDir := setupGitRemoteRepository(t, root, "master",
testBranchSpec{name: "master", contents: "remote master branch\n"},
testBranchSpec{name: "main", contents: "remote main branch\n"},
)
baseDir := filepath.Join(root, "workspace", "auths")
store := NewGitTokenStore(remoteDir, "", "", "")
store.SetBaseDir(baseDir)
if err := store.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository initial clone: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "master", "remote master branch\n")
setRemoteHeadBranch(t, remoteDir, "main")
advanceRemoteBranch(t, filepath.Join(root, "seed"), remoteDir, "main", "remote main branch updated\n", "advance main")
reopened := NewGitTokenStore(remoteDir, "", "", "")
reopened.SetBaseDir(baseDir)
if err := reopened.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository after remote default rename: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "main", "remote main branch updated\n")
assertRemoteHeadBranch(t, remoteDir, "main")
}
func TestEnsureRepositoryKeepsCurrentBranchWhenRemoteDefaultCannotBeResolved(t *testing.T) {
root := t.TempDir()
remoteDir := setupGitRemoteRepository(t, root, "master",
testBranchSpec{name: "master", contents: "remote master branch\n"},
testBranchSpec{name: "develop", contents: "remote develop branch\n"},
)
baseDir := filepath.Join(root, "workspace", "auths")
pinned := NewGitTokenStore(remoteDir, "", "", "develop")
pinned.SetBaseDir(baseDir)
if err := pinned.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository pinned: %v", err)
}
assertRepositoryBranchAndContents(t, filepath.Join(root, "workspace"), "develop", "remote develop branch\n")
authServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="git"`)
http.Error(w, "auth required", http.StatusUnauthorized)
}))
defer authServer.Close()
repo, err := git.PlainOpen(filepath.Join(root, "workspace"))
if err != nil {
t.Fatalf("open workspace repo: %v", err)
}
cfg, err := repo.Config()
if err != nil {
t.Fatalf("read repo config: %v", err)
}
cfg.Remotes["origin"].URLs = []string{authServer.URL}
if err := repo.SetConfig(cfg); err != nil {
t.Fatalf("set repo config: %v", err)
}
reopened := NewGitTokenStore(remoteDir, "", "", "")
reopened.SetBaseDir(baseDir)
if err := reopened.EnsureRepository(); err != nil {
t.Fatalf("EnsureRepository default branch fallback: %v", err)
}
assertRepositoryHeadBranch(t, filepath.Join(root, "workspace"), "develop")
}
func setupGitRemoteRepository(t *testing.T, root, defaultBranch string, branches ...testBranchSpec) string {
t.Helper()
remoteDir := filepath.Join(root, "remote.git")
if _, err := git.PlainInit(remoteDir, true); err != nil {
t.Fatalf("init bare remote: %v", err)
}
seedDir := filepath.Join(root, "seed")
seedRepo, err := git.PlainInit(seedDir, false)
if err != nil {
t.Fatalf("init seed repo: %v", err)
}
if err := seedRepo.Storer.SetReference(plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName(defaultBranch))); err != nil {
t.Fatalf("set seed HEAD: %v", err)
}
worktree, err := seedRepo.Worktree()
if err != nil {
t.Fatalf("open seed worktree: %v", err)
}
defaultSpec, ok := findBranchSpec(branches, defaultBranch)
if !ok {
t.Fatalf("missing default branch spec for %q", defaultBranch)
}
commitBranchMarker(t, seedDir, worktree, defaultSpec, "seed default branch")
for _, branch := range branches {
if branch.name == defaultBranch {
continue
}
if err := worktree.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName(defaultBranch)}); err != nil {
t.Fatalf("checkout default branch %s: %v", defaultBranch, err)
}
if err := worktree.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName(branch.name), Create: true}); err != nil {
t.Fatalf("create branch %s: %v", branch.name, err)
}
commitBranchMarker(t, seedDir, worktree, branch, "seed branch "+branch.name)
}
if _, err := seedRepo.CreateRemote(&gitconfig.RemoteConfig{Name: "origin", URLs: []string{remoteDir}}); err != nil {
t.Fatalf("create origin remote: %v", err)
}
if err := seedRepo.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []gitconfig.RefSpec{gitconfig.RefSpec("refs/heads/*:refs/heads/*")},
}); err != nil {
t.Fatalf("push seed branches: %v", err)
}
remoteRepo, err := git.PlainOpen(remoteDir)
if err != nil {
t.Fatalf("open remote repo: %v", err)
}
if err := remoteRepo.Storer.SetReference(plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName(defaultBranch))); err != nil {
t.Fatalf("set remote HEAD: %v", err)
}
return remoteDir
}
func commitBranchMarker(t *testing.T, seedDir string, worktree *git.Worktree, branch testBranchSpec, message string) {
t.Helper()
if err := os.WriteFile(filepath.Join(seedDir, "branch.txt"), []byte(branch.contents), 0o600); err != nil {
t.Fatalf("write branch marker for %s: %v", branch.name, err)
}
if _, err := worktree.Add("branch.txt"); err != nil {
t.Fatalf("add branch marker for %s: %v", branch.name, err)
}
if _, err := worktree.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: "CLIProxyAPI",
Email: "cliproxy@local",
When: time.Unix(1711929600, 0),
},
}); err != nil {
t.Fatalf("commit branch marker for %s: %v", branch.name, err)
}
}
func advanceRemoteBranch(t *testing.T, seedDir, remoteDir, branch, contents, message string) {
t.Helper()
seedRepo, err := git.PlainOpen(seedDir)
if err != nil {
t.Fatalf("open seed repo: %v", err)
}
worktree, err := seedRepo.Worktree()
if err != nil {
t.Fatalf("open seed worktree: %v", err)
}
if err := worktree.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName(branch)}); err != nil {
t.Fatalf("checkout branch %s: %v", branch, err)
}
commitBranchMarker(t, seedDir, worktree, testBranchSpec{name: branch, contents: contents}, message)
if err := seedRepo.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []gitconfig.RefSpec{
gitconfig.RefSpec(plumbing.NewBranchReferenceName(branch).String() + ":" + plumbing.NewBranchReferenceName(branch).String()),
},
}); err != nil {
t.Fatalf("push branch %s update to %s: %v", branch, remoteDir, err)
}
}
func advanceRemoteBranchFromNewBranch(t *testing.T, seedDir, remoteDir, branch, contents, message string) {
t.Helper()
seedRepo, err := git.PlainOpen(seedDir)
if err != nil {
t.Fatalf("open seed repo: %v", err)
}
worktree, err := seedRepo.Worktree()
if err != nil {
t.Fatalf("open seed worktree: %v", err)
}
if err := worktree.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName("master")}); err != nil {
t.Fatalf("checkout master before creating %s: %v", branch, err)
}
if err := worktree.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName(branch), Create: true}); err != nil {
t.Fatalf("create branch %s: %v", branch, err)
}
commitBranchMarker(t, seedDir, worktree, testBranchSpec{name: branch, contents: contents}, message)
if err := seedRepo.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []gitconfig.RefSpec{
gitconfig.RefSpec(plumbing.NewBranchReferenceName(branch).String() + ":" + plumbing.NewBranchReferenceName(branch).String()),
},
}); err != nil {
t.Fatalf("push new branch %s update to %s: %v", branch, remoteDir, err)
}
}
func findBranchSpec(branches []testBranchSpec, name string) (testBranchSpec, bool) {
for _, branch := range branches {
if branch.name == name {
return branch, true
}
}
return testBranchSpec{}, false
}
func assertRepositoryBranchAndContents(t *testing.T, repoDir, branch, wantContents string) {
t.Helper()
repo, err := git.PlainOpen(repoDir)
if err != nil {
t.Fatalf("open local repo: %v", err)
}
head, err := repo.Head()
if err != nil {
t.Fatalf("local repo head: %v", err)
}
if got, want := head.Name(), plumbing.NewBranchReferenceName(branch); got != want {
t.Fatalf("local head branch = %s, want %s", got, want)
}
contents, err := os.ReadFile(filepath.Join(repoDir, "branch.txt"))
if err != nil {
t.Fatalf("read branch marker: %v", err)
}
if got := string(contents); got != wantContents {
t.Fatalf("branch marker contents = %q, want %q", got, wantContents)
}
}
func assertRepositoryHeadBranch(t *testing.T, repoDir, branch string) {
t.Helper()
repo, err := git.PlainOpen(repoDir)
if err != nil {
t.Fatalf("open local repo: %v", err)
}
head, err := repo.Head()
if err != nil {
t.Fatalf("local repo head: %v", err)
}
if got, want := head.Name(), plumbing.NewBranchReferenceName(branch); got != want {
t.Fatalf("local head branch = %s, want %s", got, want)
}
}
func assertRemoteHeadBranch(t *testing.T, remoteDir, branch string) {
t.Helper()
remoteRepo, err := git.PlainOpen(remoteDir)
if err != nil {
t.Fatalf("open remote repo: %v", err)
}
head, err := remoteRepo.Reference(plumbing.HEAD, false)
if err != nil {
t.Fatalf("read remote HEAD: %v", err)
}
if got, want := head.Target(), plumbing.NewBranchReferenceName(branch); got != want {
t.Fatalf("remote HEAD target = %s, want %s", got, want)
}
}
func setRemoteHeadBranch(t *testing.T, remoteDir, branch string) {
t.Helper()
remoteRepo, err := git.PlainOpen(remoteDir)
if err != nil {
t.Fatalf("open remote repo: %v", err)
}
if err := remoteRepo.Storer.SetReference(plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName(branch))); err != nil {
t.Fatalf("set remote HEAD to %s: %v", branch, err)
}
}
func assertRemoteBranchExistsWithCommit(t *testing.T, remoteDir, branch string) {
t.Helper()
remoteRepo, err := git.PlainOpen(remoteDir)
if err != nil {
t.Fatalf("open remote repo: %v", err)
}
ref, err := remoteRepo.Reference(plumbing.NewBranchReferenceName(branch), false)
if err != nil {
t.Fatalf("read remote branch %s: %v", branch, err)
}
if got := ref.Hash(); got == plumbing.ZeroHash {
t.Fatalf("remote branch %s hash = %s, want non-zero hash", branch, got)
}
}
func assertRemoteBranchDoesNotExist(t *testing.T, remoteDir, branch string) {
t.Helper()
remoteRepo, err := git.PlainOpen(remoteDir)
if err != nil {
t.Fatalf("open remote repo: %v", err)
}
if _, err := remoteRepo.Reference(plumbing.NewBranchReferenceName(branch), false); err == nil {
t.Fatalf("remote branch %s exists, want missing", branch)
} else if err != plumbing.ErrReferenceNotFound {
t.Fatalf("read remote branch %s: %v", branch, err)
}
}
func assertRemoteBranchContents(t *testing.T, remoteDir, branch, wantContents string) {
t.Helper()
remoteRepo, err := git.PlainOpen(remoteDir)
if err != nil {
t.Fatalf("open remote repo: %v", err)
}
ref, err := remoteRepo.Reference(plumbing.NewBranchReferenceName(branch), false)
if err != nil {
t.Fatalf("read remote branch %s: %v", branch, err)
}
commit, err := remoteRepo.CommitObject(ref.Hash())
if err != nil {
t.Fatalf("read remote branch %s commit: %v", branch, err)
}
tree, err := commit.Tree()
if err != nil {
t.Fatalf("read remote branch %s tree: %v", branch, err)
}
file, err := tree.File("branch.txt")
if err != nil {
t.Fatalf("read remote branch %s file: %v", branch, err)
}
contents, err := file.Contents()
if err != nil {
t.Fatalf("read remote branch %s contents: %v", branch, err)
}
if contents != wantContents {
t.Fatalf("remote branch %s contents = %q, want %q", branch, contents, wantContents)
}
}
+24 -3
View File
@@ -174,7 +174,8 @@ func (a *Applier) normalizeClaudeBudget(body []byte, budgetTokens int, modelInfo
// Ensure the request satisfies Claude constraints:
// 1) Determine effective max_tokens (request overrides model default)
// 2) If budget_tokens >= max_tokens, reduce budget_tokens to max_tokens-1
// 3) If the adjusted budget falls below the model minimum, leave the request unchanged
// 3) If the adjusted budget falls below the model minimum, try raising max_tokens
// (clamped to MaxCompletionTokens); disable thinking if constraints are unsatisfiable
// 4) If max_tokens came from model default, write it back into the request
effectiveMax, setDefaultMax := a.effectiveMaxTokens(body, modelInfo)
@@ -193,8 +194,28 @@ func (a *Applier) normalizeClaudeBudget(body []byte, budgetTokens int, modelInfo
minBudget = modelInfo.Thinking.Min
}
if minBudget > 0 && adjustedBudget > 0 && adjustedBudget < minBudget {
// If enforcing the max_tokens constraint would push the budget below the model minimum,
// leave the request unchanged.
// Enforcing budget_tokens < max_tokens pushed the budget below the model minimum.
// Try raising max_tokens to fit the original budget.
needed := budgetTokens + 1
maxAllowed := 0
if modelInfo != nil {
maxAllowed = modelInfo.MaxCompletionTokens
}
if maxAllowed > 0 && needed > maxAllowed {
// Cannot use original budget; cap max_tokens at model limit.
needed = maxAllowed
}
cappedBudget := needed - 1
if cappedBudget < minBudget {
// Impossible to satisfy both budget >= minBudget and budget < max_tokens
// within the model's completion limit. Disable thinking entirely.
body, _ = sjson.DeleteBytes(body, "thinking")
return body
}
body, _ = sjson.SetBytes(body, "max_tokens", needed)
if cappedBudget != budgetTokens {
body, _ = sjson.SetBytes(body, "thinking.budget_tokens", cappedBudget)
}
return body
}
@@ -0,0 +1,99 @@
package claude
import (
"testing"
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
"github.com/tidwall/gjson"
)
func TestNormalizeClaudeBudget_RaisesMaxTokens(t *testing.T) {
a := &Applier{}
modelInfo := &registry.ModelInfo{
MaxCompletionTokens: 64000,
Thinking: &registry.ThinkingSupport{Min: 1024, Max: 128000},
}
body := []byte(`{"max_tokens":1000,"thinking":{"type":"enabled","budget_tokens":5000}}`)
out := a.normalizeClaudeBudget(body, 5000, modelInfo)
maxTok := gjson.GetBytes(out, "max_tokens").Int()
if maxTok != 5001 {
t.Fatalf("max_tokens = %d, want 5001, body=%s", maxTok, string(out))
}
}
func TestNormalizeClaudeBudget_ClampsToModelMax(t *testing.T) {
a := &Applier{}
modelInfo := &registry.ModelInfo{
MaxCompletionTokens: 64000,
Thinking: &registry.ThinkingSupport{Min: 1024, Max: 128000},
}
body := []byte(`{"max_tokens":500,"thinking":{"type":"enabled","budget_tokens":200000}}`)
out := a.normalizeClaudeBudget(body, 200000, modelInfo)
maxTok := gjson.GetBytes(out, "max_tokens").Int()
if maxTok != 64000 {
t.Fatalf("max_tokens = %d, want 64000 (capped to model limit), body=%s", maxTok, string(out))
}
budget := gjson.GetBytes(out, "thinking.budget_tokens").Int()
if budget != 63999 {
t.Fatalf("budget_tokens = %d, want 63999 (max_tokens-1), body=%s", budget, string(out))
}
}
func TestNormalizeClaudeBudget_DisablesThinkingWhenUnsatisfiable(t *testing.T) {
a := &Applier{}
modelInfo := &registry.ModelInfo{
MaxCompletionTokens: 1000,
Thinking: &registry.ThinkingSupport{Min: 1024, Max: 128000},
}
body := []byte(`{"max_tokens":500,"thinking":{"type":"enabled","budget_tokens":2000}}`)
out := a.normalizeClaudeBudget(body, 2000, modelInfo)
if gjson.GetBytes(out, "thinking").Exists() {
t.Fatalf("thinking should be removed when constraints are unsatisfiable, body=%s", string(out))
}
}
func TestNormalizeClaudeBudget_NoClamping(t *testing.T) {
a := &Applier{}
modelInfo := &registry.ModelInfo{
MaxCompletionTokens: 64000,
Thinking: &registry.ThinkingSupport{Min: 1024, Max: 128000},
}
body := []byte(`{"max_tokens":32000,"thinking":{"type":"enabled","budget_tokens":16000}}`)
out := a.normalizeClaudeBudget(body, 16000, modelInfo)
maxTok := gjson.GetBytes(out, "max_tokens").Int()
if maxTok != 32000 {
t.Fatalf("max_tokens should remain 32000, got %d, body=%s", maxTok, string(out))
}
budget := gjson.GetBytes(out, "thinking.budget_tokens").Int()
if budget != 16000 {
t.Fatalf("budget_tokens should remain 16000, got %d, body=%s", budget, string(out))
}
}
func TestNormalizeClaudeBudget_AdjustsBudgetToMaxMinus1(t *testing.T) {
a := &Applier{}
modelInfo := &registry.ModelInfo{
MaxCompletionTokens: 8192,
Thinking: &registry.ThinkingSupport{Min: 1024, Max: 128000},
}
body := []byte(`{"max_tokens":8192,"thinking":{"type":"enabled","budget_tokens":10000}}`)
out := a.normalizeClaudeBudget(body, 10000, modelInfo)
maxTok := gjson.GetBytes(out, "max_tokens").Int()
if maxTok != 8192 {
t.Fatalf("max_tokens = %d, want 8192 (unchanged), body=%s", maxTok, string(out))
}
budget := gjson.GetBytes(out, "thinking.budget_tokens").Int()
if budget != 8191 {
t.Fatalf("budget_tokens = %d, want 8191 (max_tokens-1), body=%s", budget, string(out))
}
}
@@ -26,6 +26,9 @@ type ConvertCodexResponseToClaudeParams struct {
HasToolCall bool
BlockIndex int
HasReceivedArgumentsDelta bool
ThinkingBlockOpen bool
ThinkingStopPending bool
ThinkingSignature string
}
// ConvertCodexResponseToClaude performs sophisticated streaming response format conversion.
@@ -44,7 +47,7 @@ type ConvertCodexResponseToClaudeParams struct {
//
// Returns:
// - [][]byte: A slice of Claude Code-compatible JSON responses
func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) [][]byte {
func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRawJSON, _ []byte, rawJSON []byte, param *any) [][]byte {
if *param == nil {
*param = &ConvertCodexResponseToClaudeParams{
HasToolCall: false,
@@ -52,7 +55,6 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
}
}
// log.Debugf("rawJSON: %s", string(rawJSON))
if !bytes.HasPrefix(rawJSON, dataTag) {
return [][]byte{}
}
@@ -60,9 +62,18 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
output := make([]byte, 0, 512)
rootResult := gjson.ParseBytes(rawJSON)
params := (*param).(*ConvertCodexResponseToClaudeParams)
if params.ThinkingBlockOpen && params.ThinkingStopPending {
switch rootResult.Get("type").String() {
case "response.content_part.added", "response.completed":
output = append(output, finalizeCodexThinkingBlock(params)...)
}
}
typeResult := rootResult.Get("type")
typeStr := typeResult.String()
var template []byte
if typeStr == "response.created" {
template = []byte(`{"type":"message_start","message":{"id":"","type":"message","role":"assistant","model":"claude-opus-4-1-20250805","stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0},"content":[],"stop_reason":null}}`)
template, _ = sjson.SetBytes(template, "message.model", rootResult.Get("response.model").String())
@@ -70,43 +81,46 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
output = translatorcommon.AppendSSEEventBytes(output, "message_start", template, 2)
} else if typeStr == "response.reasoning_summary_part.added" {
if params.ThinkingBlockOpen && params.ThinkingStopPending {
output = append(output, finalizeCodexThinkingBlock(params)...)
}
template = []byte(`{"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
template, _ = sjson.SetBytes(template, "index", params.BlockIndex)
params.ThinkingBlockOpen = true
params.ThinkingStopPending = false
output = translatorcommon.AppendSSEEventBytes(output, "content_block_start", template, 2)
} else if typeStr == "response.reasoning_summary_text.delta" {
template = []byte(`{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":""}}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
template, _ = sjson.SetBytes(template, "index", params.BlockIndex)
template, _ = sjson.SetBytes(template, "delta.thinking", rootResult.Get("delta").String())
output = translatorcommon.AppendSSEEventBytes(output, "content_block_delta", template, 2)
} else if typeStr == "response.reasoning_summary_part.done" {
template = []byte(`{"type":"content_block_stop","index":0}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
(*param).(*ConvertCodexResponseToClaudeParams).BlockIndex++
output = translatorcommon.AppendSSEEventBytes(output, "content_block_stop", template, 2)
params.ThinkingStopPending = true
if params.ThinkingSignature != "" {
output = append(output, finalizeCodexThinkingBlock(params)...)
}
} else if typeStr == "response.content_part.added" {
template = []byte(`{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
template, _ = sjson.SetBytes(template, "index", params.BlockIndex)
output = translatorcommon.AppendSSEEventBytes(output, "content_block_start", template, 2)
} else if typeStr == "response.output_text.delta" {
template = []byte(`{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":""}}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
template, _ = sjson.SetBytes(template, "index", params.BlockIndex)
template, _ = sjson.SetBytes(template, "delta.text", rootResult.Get("delta").String())
output = translatorcommon.AppendSSEEventBytes(output, "content_block_delta", template, 2)
} else if typeStr == "response.content_part.done" {
template = []byte(`{"type":"content_block_stop","index":0}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
(*param).(*ConvertCodexResponseToClaudeParams).BlockIndex++
template, _ = sjson.SetBytes(template, "index", params.BlockIndex)
params.BlockIndex++
output = translatorcommon.AppendSSEEventBytes(output, "content_block_stop", template, 2)
} else if typeStr == "response.completed" {
template = []byte(`{"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"input_tokens":0,"output_tokens":0}}`)
p := (*param).(*ConvertCodexResponseToClaudeParams).HasToolCall
p := params.HasToolCall
stopReason := rootResult.Get("response.stop_reason").String()
if p {
template, _ = sjson.SetBytes(template, "delta.stop_reason", "tool_use")
@@ -128,13 +142,13 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
itemResult := rootResult.Get("item")
itemType := itemResult.Get("type").String()
if itemType == "function_call" {
(*param).(*ConvertCodexResponseToClaudeParams).HasToolCall = true
(*param).(*ConvertCodexResponseToClaudeParams).HasReceivedArgumentsDelta = false
output = append(output, finalizeCodexThinkingBlock(params)...)
params.HasToolCall = true
params.HasReceivedArgumentsDelta = false
template = []byte(`{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
template, _ = sjson.SetBytes(template, "index", params.BlockIndex)
template, _ = sjson.SetBytes(template, "content_block.id", util.SanitizeClaudeToolID(itemResult.Get("call_id").String()))
{
// Restore original tool name if shortened
name := itemResult.Get("name").String()
rev := buildReverseMapFromClaudeOriginalShortToOriginal(originalRequestRawJSON)
if orig, ok := rev[name]; ok {
@@ -146,37 +160,43 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
output = translatorcommon.AppendSSEEventBytes(output, "content_block_start", template, 2)
template = []byte(`{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
template, _ = sjson.SetBytes(template, "index", params.BlockIndex)
output = translatorcommon.AppendSSEEventBytes(output, "content_block_delta", template, 2)
} else if itemType == "reasoning" {
params.ThinkingSignature = itemResult.Get("encrypted_content").String()
if params.ThinkingStopPending {
output = append(output, finalizeCodexThinkingBlock(params)...)
}
}
} else if typeStr == "response.output_item.done" {
itemResult := rootResult.Get("item")
itemType := itemResult.Get("type").String()
if itemType == "function_call" {
template = []byte(`{"type":"content_block_stop","index":0}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
(*param).(*ConvertCodexResponseToClaudeParams).BlockIndex++
template, _ = sjson.SetBytes(template, "index", params.BlockIndex)
params.BlockIndex++
output = translatorcommon.AppendSSEEventBytes(output, "content_block_stop", template, 2)
} else if itemType == "reasoning" {
if signature := itemResult.Get("encrypted_content").String(); signature != "" {
params.ThinkingSignature = signature
}
output = append(output, finalizeCodexThinkingBlock(params)...)
params.ThinkingSignature = ""
}
} else if typeStr == "response.function_call_arguments.delta" {
(*param).(*ConvertCodexResponseToClaudeParams).HasReceivedArgumentsDelta = true
params.HasReceivedArgumentsDelta = true
template = []byte(`{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
template, _ = sjson.SetBytes(template, "index", params.BlockIndex)
template, _ = sjson.SetBytes(template, "delta.partial_json", rootResult.Get("delta").String())
output = translatorcommon.AppendSSEEventBytes(output, "content_block_delta", template, 2)
} else if typeStr == "response.function_call_arguments.done" {
// Some models (e.g. gpt-5.3-codex-spark) send function call arguments
// in a single "done" event without preceding "delta" events.
// Emit the full arguments as a single input_json_delta so the
// downstream Claude client receives the complete tool input.
// When delta events were already received, skip to avoid duplicating arguments.
if !(*param).(*ConvertCodexResponseToClaudeParams).HasReceivedArgumentsDelta {
if !params.HasReceivedArgumentsDelta {
if args := rootResult.Get("arguments").String(); args != "" {
template = []byte(`{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}`)
template, _ = sjson.SetBytes(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex)
template, _ = sjson.SetBytes(template, "index", params.BlockIndex)
template, _ = sjson.SetBytes(template, "delta.partial_json", args)
output = translatorcommon.AppendSSEEventBytes(output, "content_block_delta", template, 2)
@@ -191,15 +211,6 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
// This function processes the complete Codex response and transforms it into a single Claude Code-compatible
// JSON response. It handles message content, tool calls, reasoning content, and usage metadata, combining all
// the information into a single response that matches the Claude Code API format.
//
// Parameters:
// - ctx: The context for the request, used for cancellation and timeout handling
// - modelName: The name of the model being used for the response (unused in current implementation)
// - rawJSON: The raw JSON response from the Codex API
// - param: A pointer to a parameter object for the conversion (unused in current implementation)
//
// Returns:
// - []byte: A Claude Code-compatible JSON response containing all message content and metadata
func ConvertCodexResponseToClaudeNonStream(_ context.Context, _ string, originalRequestRawJSON, _ []byte, rawJSON []byte, _ *any) []byte {
revNames := buildReverseMapFromClaudeOriginalShortToOriginal(originalRequestRawJSON)
@@ -230,6 +241,7 @@ func ConvertCodexResponseToClaudeNonStream(_ context.Context, _ string, original
switch item.Get("type").String() {
case "reasoning":
thinkingBuilder := strings.Builder{}
signature := item.Get("encrypted_content").String()
if summary := item.Get("summary"); summary.Exists() {
if summary.IsArray() {
summary.ForEach(func(_, part gjson.Result) bool {
@@ -260,9 +272,12 @@ func ConvertCodexResponseToClaudeNonStream(_ context.Context, _ string, original
}
}
}
if thinkingBuilder.Len() > 0 {
if thinkingBuilder.Len() > 0 || signature != "" {
block := []byte(`{"type":"thinking","thinking":""}`)
block, _ = sjson.SetBytes(block, "thinking", thinkingBuilder.String())
if signature != "" {
block, _ = sjson.SetBytes(block, "signature", signature)
}
out, _ = sjson.SetRawBytes(out, "content.-1", block)
}
case "message":
@@ -371,6 +386,30 @@ func buildReverseMapFromClaudeOriginalShortToOriginal(original []byte) map[strin
return rev
}
func ClaudeTokenCount(ctx context.Context, count int64) []byte {
func ClaudeTokenCount(_ context.Context, count int64) []byte {
return translatorcommon.ClaudeInputTokensJSON(count)
}
func finalizeCodexThinkingBlock(params *ConvertCodexResponseToClaudeParams) []byte {
if !params.ThinkingBlockOpen {
return nil
}
output := make([]byte, 0, 256)
if params.ThinkingSignature != "" {
signatureDelta := []byte(`{"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":""}}`)
signatureDelta, _ = sjson.SetBytes(signatureDelta, "index", params.BlockIndex)
signatureDelta, _ = sjson.SetBytes(signatureDelta, "delta.signature", params.ThinkingSignature)
output = translatorcommon.AppendSSEEventBytes(output, "content_block_delta", signatureDelta, 2)
}
contentBlockStop := []byte(`{"type":"content_block_stop","index":0}`)
contentBlockStop, _ = sjson.SetBytes(contentBlockStop, "index", params.BlockIndex)
output = translatorcommon.AppendSSEEventBytes(output, "content_block_stop", contentBlockStop, 2)
params.BlockIndex++
params.ThinkingBlockOpen = false
params.ThinkingStopPending = false
return output
}
@@ -0,0 +1,282 @@
package claude
import (
"context"
"strings"
"testing"
"github.com/tidwall/gjson"
)
func TestConvertCodexResponseToClaude_StreamThinkingIncludesSignature(t *testing.T) {
ctx := context.Background()
originalRequest := []byte(`{"messages":[]}`)
var param any
chunks := [][]byte{
[]byte("data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_123\",\"model\":\"gpt-5\"}}"),
[]byte("data: {\"type\":\"response.reasoning_summary_part.added\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"Let me think\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_part.done\"}"),
[]byte("data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"reasoning\",\"encrypted_content\":\"enc_sig_123\"}}"),
}
var outputs [][]byte
for _, chunk := range chunks {
outputs = append(outputs, ConvertCodexResponseToClaude(ctx, "", originalRequest, nil, chunk, &param)...)
}
startFound := false
signatureDeltaFound := false
stopFound := false
for _, out := range outputs {
for _, line := range strings.Split(string(out), "\n") {
if !strings.HasPrefix(line, "data: ") {
continue
}
data := gjson.Parse(strings.TrimPrefix(line, "data: "))
switch data.Get("type").String() {
case "content_block_start":
if data.Get("content_block.type").String() == "thinking" {
startFound = true
if data.Get("content_block.signature").Exists() {
t.Fatalf("thinking start block should NOT have signature field when signature is unknown: %s", line)
}
}
case "content_block_delta":
if data.Get("delta.type").String() == "signature_delta" {
signatureDeltaFound = true
if got := data.Get("delta.signature").String(); got != "enc_sig_123" {
t.Fatalf("unexpected signature delta: %q", got)
}
}
case "content_block_stop":
stopFound = true
}
}
}
if !startFound {
t.Fatal("expected thinking content_block_start event")
}
if !signatureDeltaFound {
t.Fatal("expected signature_delta event for thinking block")
}
if !stopFound {
t.Fatal("expected content_block_stop event for thinking block")
}
}
func TestConvertCodexResponseToClaude_StreamThinkingWithoutReasoningItemStillIncludesSignatureField(t *testing.T) {
ctx := context.Background()
originalRequest := []byte(`{"messages":[]}`)
var param any
chunks := [][]byte{
[]byte("data: {\"type\":\"response.reasoning_summary_part.added\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"Let me think\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_part.done\"}"),
[]byte("data: {\"type\":\"response.completed\",\"response\":{\"usage\":{\"input_tokens\":1,\"output_tokens\":1}}}"),
}
var outputs [][]byte
for _, chunk := range chunks {
outputs = append(outputs, ConvertCodexResponseToClaude(ctx, "", originalRequest, nil, chunk, &param)...)
}
thinkingStartFound := false
thinkingStopFound := false
signatureDeltaFound := false
for _, out := range outputs {
for _, line := range strings.Split(string(out), "\n") {
if !strings.HasPrefix(line, "data: ") {
continue
}
data := gjson.Parse(strings.TrimPrefix(line, "data: "))
if data.Get("type").String() == "content_block_start" && data.Get("content_block.type").String() == "thinking" {
thinkingStartFound = true
if data.Get("content_block.signature").Exists() {
t.Fatalf("thinking start block should NOT have signature field without encrypted_content: %s", line)
}
}
if data.Get("type").String() == "content_block_stop" && data.Get("index").Int() == 0 {
thinkingStopFound = true
}
if data.Get("type").String() == "content_block_delta" && data.Get("delta.type").String() == "signature_delta" {
signatureDeltaFound = true
}
}
}
if !thinkingStartFound {
t.Fatal("expected thinking content_block_start event")
}
if !thinkingStopFound {
t.Fatal("expected thinking content_block_stop event")
}
if signatureDeltaFound {
t.Fatal("did not expect signature_delta without encrypted_content")
}
}
func TestConvertCodexResponseToClaude_StreamThinkingFinalizesPendingBlockBeforeNextSummaryPart(t *testing.T) {
ctx := context.Background()
originalRequest := []byte(`{"messages":[]}`)
var param any
chunks := [][]byte{
[]byte("data: {\"type\":\"response.reasoning_summary_part.added\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"First part\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_part.done\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_part.added\"}"),
}
var outputs [][]byte
for _, chunk := range chunks {
outputs = append(outputs, ConvertCodexResponseToClaude(ctx, "", originalRequest, nil, chunk, &param)...)
}
startCount := 0
stopCount := 0
for _, out := range outputs {
for _, line := range strings.Split(string(out), "\n") {
if !strings.HasPrefix(line, "data: ") {
continue
}
data := gjson.Parse(strings.TrimPrefix(line, "data: "))
if data.Get("type").String() == "content_block_start" && data.Get("content_block.type").String() == "thinking" {
startCount++
}
if data.Get("type").String() == "content_block_stop" {
stopCount++
}
}
}
if startCount != 2 {
t.Fatalf("expected 2 thinking block starts, got %d", startCount)
}
if stopCount != 1 {
t.Fatalf("expected pending thinking block to be finalized before second start, got %d stops", stopCount)
}
}
func TestConvertCodexResponseToClaude_StreamThinkingRetainsSignatureAcrossMultipartReasoning(t *testing.T) {
ctx := context.Background()
originalRequest := []byte(`{"messages":[]}`)
var param any
chunks := [][]byte{
[]byte("data: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"reasoning\",\"encrypted_content\":\"enc_sig_multipart\"}}"),
[]byte("data: {\"type\":\"response.reasoning_summary_part.added\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"First part\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_part.done\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_part.added\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"Second part\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_part.done\"}"),
[]byte("data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"reasoning\"}}"),
}
var outputs [][]byte
for _, chunk := range chunks {
outputs = append(outputs, ConvertCodexResponseToClaude(ctx, "", originalRequest, nil, chunk, &param)...)
}
signatureDeltaCount := 0
for _, out := range outputs {
for _, line := range strings.Split(string(out), "\n") {
if !strings.HasPrefix(line, "data: ") {
continue
}
data := gjson.Parse(strings.TrimPrefix(line, "data: "))
if data.Get("type").String() == "content_block_delta" && data.Get("delta.type").String() == "signature_delta" {
signatureDeltaCount++
if got := data.Get("delta.signature").String(); got != "enc_sig_multipart" {
t.Fatalf("unexpected signature delta: %q", got)
}
}
}
}
if signatureDeltaCount != 2 {
t.Fatalf("expected signature_delta for both multipart thinking blocks, got %d", signatureDeltaCount)
}
}
func TestConvertCodexResponseToClaude_StreamThinkingUsesEarlyCapturedSignatureWhenDoneOmitsIt(t *testing.T) {
ctx := context.Background()
originalRequest := []byte(`{"messages":[]}`)
var param any
chunks := [][]byte{
[]byte("data: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"reasoning\",\"encrypted_content\":\"enc_sig_early\"}}"),
[]byte("data: {\"type\":\"response.reasoning_summary_part.added\"}"),
[]byte("data: {\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"Let me think\"}"),
[]byte("data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"reasoning\"}}"),
}
var outputs [][]byte
for _, chunk := range chunks {
outputs = append(outputs, ConvertCodexResponseToClaude(ctx, "", originalRequest, nil, chunk, &param)...)
}
signatureDeltaCount := 0
for _, out := range outputs {
for _, line := range strings.Split(string(out), "\n") {
if !strings.HasPrefix(line, "data: ") {
continue
}
data := gjson.Parse(strings.TrimPrefix(line, "data: "))
if data.Get("type").String() == "content_block_delta" && data.Get("delta.type").String() == "signature_delta" {
signatureDeltaCount++
if got := data.Get("delta.signature").String(); got != "enc_sig_early" {
t.Fatalf("unexpected signature delta: %q", got)
}
}
}
}
if signatureDeltaCount != 1 {
t.Fatalf("expected signature_delta from early-captured signature, got %d", signatureDeltaCount)
}
}
func TestConvertCodexResponseToClaudeNonStream_ThinkingIncludesSignature(t *testing.T) {
ctx := context.Background()
originalRequest := []byte(`{"messages":[]}`)
response := []byte(`{
"type":"response.completed",
"response":{
"id":"resp_123",
"model":"gpt-5",
"usage":{"input_tokens":10,"output_tokens":20},
"output":[
{
"type":"reasoning",
"encrypted_content":"enc_sig_nonstream",
"summary":[{"type":"summary_text","text":"internal reasoning"}]
},
{
"type":"message",
"content":[{"type":"output_text","text":"final answer"}]
}
]
}
}`)
out := ConvertCodexResponseToClaudeNonStream(ctx, "", originalRequest, nil, response, nil)
parsed := gjson.ParseBytes(out)
thinking := parsed.Get("content.0")
if thinking.Get("type").String() != "thinking" {
t.Fatalf("expected first content block to be thinking, got %s", thinking.Raw)
}
if got := thinking.Get("signature").String(); got != "enc_sig_nonstream" {
t.Fatalf("expected signature to be preserved, got %q", got)
}
if got := thinking.Get("thinking").String(); got != "internal reasoning" {
t.Fatalf("unexpected thinking text: %q", got)
}
}
@@ -20,12 +20,14 @@ type oaiToResponsesStateReasoning struct {
OutputIndex int
}
type oaiToResponsesState struct {
Seq int
ResponseID string
Created int64
Started bool
ReasoningID string
ReasoningIndex int
Seq int
ResponseID string
Created int64
Started bool
CompletionPending bool
CompletedEmitted bool
ReasoningID string
ReasoningIndex int
// aggregation buffers for response.output
// Per-output message text buffers by index
MsgTextBuf map[int]*strings.Builder
@@ -60,6 +62,141 @@ func emitRespEvent(event string, payload []byte) []byte {
return translatorcommon.SSEEventData(event, payload)
}
func buildResponsesCompletedEvent(st *oaiToResponsesState, requestRawJSON []byte, nextSeq func() int) []byte {
completed := []byte(`{"type":"response.completed","sequence_number":0,"response":{"id":"","object":"response","created_at":0,"status":"completed","background":false,"error":null}}`)
completed, _ = sjson.SetBytes(completed, "sequence_number", nextSeq())
completed, _ = sjson.SetBytes(completed, "response.id", st.ResponseID)
completed, _ = sjson.SetBytes(completed, "response.created_at", st.Created)
// Inject original request fields into response as per docs/response.completed.json
if requestRawJSON != nil {
req := gjson.ParseBytes(requestRawJSON)
if v := req.Get("instructions"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.instructions", v.String())
}
if v := req.Get("max_output_tokens"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.max_output_tokens", v.Int())
}
if v := req.Get("max_tool_calls"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.max_tool_calls", v.Int())
}
if v := req.Get("model"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.model", v.String())
}
if v := req.Get("parallel_tool_calls"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.parallel_tool_calls", v.Bool())
}
if v := req.Get("previous_response_id"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.previous_response_id", v.String())
}
if v := req.Get("prompt_cache_key"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.prompt_cache_key", v.String())
}
if v := req.Get("reasoning"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.reasoning", v.Value())
}
if v := req.Get("safety_identifier"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.safety_identifier", v.String())
}
if v := req.Get("service_tier"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.service_tier", v.String())
}
if v := req.Get("store"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.store", v.Bool())
}
if v := req.Get("temperature"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.temperature", v.Float())
}
if v := req.Get("text"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.text", v.Value())
}
if v := req.Get("tool_choice"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.tool_choice", v.Value())
}
if v := req.Get("tools"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.tools", v.Value())
}
if v := req.Get("top_logprobs"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.top_logprobs", v.Int())
}
if v := req.Get("top_p"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.top_p", v.Float())
}
if v := req.Get("truncation"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.truncation", v.String())
}
if v := req.Get("user"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.user", v.Value())
}
if v := req.Get("metadata"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.metadata", v.Value())
}
}
outputsWrapper := []byte(`{"arr":[]}`)
type completedOutputItem struct {
index int
raw []byte
}
outputItems := make([]completedOutputItem, 0, len(st.Reasonings)+len(st.MsgItemAdded)+len(st.FuncArgsBuf))
if len(st.Reasonings) > 0 {
for _, r := range st.Reasonings {
item := []byte(`{"id":"","type":"reasoning","summary":[{"type":"summary_text","text":""}]}`)
item, _ = sjson.SetBytes(item, "id", r.ReasoningID)
item, _ = sjson.SetBytes(item, "summary.0.text", r.ReasoningData)
outputItems = append(outputItems, completedOutputItem{index: r.OutputIndex, raw: item})
}
}
if len(st.MsgItemAdded) > 0 {
for i := range st.MsgItemAdded {
txt := ""
if b := st.MsgTextBuf[i]; b != nil {
txt = b.String()
}
item := []byte(`{"id":"","type":"message","status":"completed","content":[{"type":"output_text","annotations":[],"logprobs":[],"text":""}],"role":"assistant"}`)
item, _ = sjson.SetBytes(item, "id", fmt.Sprintf("msg_%s_%d", st.ResponseID, i))
item, _ = sjson.SetBytes(item, "content.0.text", txt)
outputItems = append(outputItems, completedOutputItem{index: st.MsgOutputIx[i], raw: item})
}
}
if len(st.FuncArgsBuf) > 0 {
for key := range st.FuncArgsBuf {
args := ""
if b := st.FuncArgsBuf[key]; b != nil {
args = b.String()
}
callID := st.FuncCallIDs[key]
name := st.FuncNames[key]
item := []byte(`{"id":"","type":"function_call","status":"completed","arguments":"","call_id":"","name":""}`)
item, _ = sjson.SetBytes(item, "id", fmt.Sprintf("fc_%s", callID))
item, _ = sjson.SetBytes(item, "arguments", args)
item, _ = sjson.SetBytes(item, "call_id", callID)
item, _ = sjson.SetBytes(item, "name", name)
outputItems = append(outputItems, completedOutputItem{index: st.FuncOutputIx[key], raw: item})
}
}
sort.Slice(outputItems, func(i, j int) bool { return outputItems[i].index < outputItems[j].index })
for _, item := range outputItems {
outputsWrapper, _ = sjson.SetRawBytes(outputsWrapper, "arr.-1", item.raw)
}
if gjson.GetBytes(outputsWrapper, "arr.#").Int() > 0 {
completed, _ = sjson.SetRawBytes(completed, "response.output", []byte(gjson.GetBytes(outputsWrapper, "arr").Raw))
}
if st.UsageSeen {
completed, _ = sjson.SetBytes(completed, "response.usage.input_tokens", st.PromptTokens)
completed, _ = sjson.SetBytes(completed, "response.usage.input_tokens_details.cached_tokens", st.CachedTokens)
completed, _ = sjson.SetBytes(completed, "response.usage.output_tokens", st.CompletionTokens)
if st.ReasoningTokens > 0 {
completed, _ = sjson.SetBytes(completed, "response.usage.output_tokens_details.reasoning_tokens", st.ReasoningTokens)
}
total := st.TotalTokens
if total == 0 {
total = st.PromptTokens + st.CompletionTokens
}
completed, _ = sjson.SetBytes(completed, "response.usage.total_tokens", total)
}
return emitRespEvent("response.completed", completed)
}
// ConvertOpenAIChatCompletionsResponseToOpenAIResponses converts OpenAI Chat Completions streaming chunks
// to OpenAI Responses SSE events (response.*).
func ConvertOpenAIChatCompletionsResponseToOpenAIResponses(ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) [][]byte {
@@ -90,6 +227,10 @@ func ConvertOpenAIChatCompletionsResponseToOpenAIResponses(ctx context.Context,
return [][]byte{}
}
if bytes.Equal(rawJSON, []byte("[DONE]")) {
if st.CompletionPending && !st.CompletedEmitted {
st.CompletedEmitted = true
return [][]byte{buildResponsesCompletedEvent(st, requestRawJSON, func() int { st.Seq++; return st.Seq })}
}
return [][]byte{}
}
@@ -165,6 +306,8 @@ func ConvertOpenAIChatCompletionsResponseToOpenAIResponses(ctx context.Context,
st.TotalTokens = 0
st.ReasoningTokens = 0
st.UsageSeen = false
st.CompletionPending = false
st.CompletedEmitted = false
// response.created
created := []byte(`{"type":"response.created","sequence_number":0,"response":{"id":"","object":"response","created_at":0,"status":"in_progress","background":false,"error":null,"output":[]}}`)
created, _ = sjson.SetBytes(created, "sequence_number", nextSeq())
@@ -374,8 +517,9 @@ func ConvertOpenAIChatCompletionsResponseToOpenAIResponses(ctx context.Context,
}
}
// finish_reason triggers finalization, including text done/content done/item done,
// reasoning done/part.done, function args done/item done, and completed
// finish_reason triggers item-level finalization. response.completed is
// deferred until the terminal [DONE] marker so late usage-only chunks can
// still populate response.usage.
if fr := choice.Get("finish_reason"); fr.Exists() && fr.String() != "" {
// Emit message done events for all indices that started a message
if len(st.MsgItemAdded) > 0 {
@@ -464,138 +608,7 @@ func ConvertOpenAIChatCompletionsResponseToOpenAIResponses(ctx context.Context,
st.FuncArgsDone[key] = true
}
}
completed := []byte(`{"type":"response.completed","sequence_number":0,"response":{"id":"","object":"response","created_at":0,"status":"completed","background":false,"error":null}}`)
completed, _ = sjson.SetBytes(completed, "sequence_number", nextSeq())
completed, _ = sjson.SetBytes(completed, "response.id", st.ResponseID)
completed, _ = sjson.SetBytes(completed, "response.created_at", st.Created)
// Inject original request fields into response as per docs/response.completed.json
if requestRawJSON != nil {
req := gjson.ParseBytes(requestRawJSON)
if v := req.Get("instructions"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.instructions", v.String())
}
if v := req.Get("max_output_tokens"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.max_output_tokens", v.Int())
}
if v := req.Get("max_tool_calls"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.max_tool_calls", v.Int())
}
if v := req.Get("model"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.model", v.String())
}
if v := req.Get("parallel_tool_calls"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.parallel_tool_calls", v.Bool())
}
if v := req.Get("previous_response_id"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.previous_response_id", v.String())
}
if v := req.Get("prompt_cache_key"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.prompt_cache_key", v.String())
}
if v := req.Get("reasoning"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.reasoning", v.Value())
}
if v := req.Get("safety_identifier"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.safety_identifier", v.String())
}
if v := req.Get("service_tier"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.service_tier", v.String())
}
if v := req.Get("store"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.store", v.Bool())
}
if v := req.Get("temperature"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.temperature", v.Float())
}
if v := req.Get("text"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.text", v.Value())
}
if v := req.Get("tool_choice"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.tool_choice", v.Value())
}
if v := req.Get("tools"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.tools", v.Value())
}
if v := req.Get("top_logprobs"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.top_logprobs", v.Int())
}
if v := req.Get("top_p"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.top_p", v.Float())
}
if v := req.Get("truncation"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.truncation", v.String())
}
if v := req.Get("user"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.user", v.Value())
}
if v := req.Get("metadata"); v.Exists() {
completed, _ = sjson.SetBytes(completed, "response.metadata", v.Value())
}
}
// Build response.output using aggregated buffers
outputsWrapper := []byte(`{"arr":[]}`)
type completedOutputItem struct {
index int
raw []byte
}
outputItems := make([]completedOutputItem, 0, len(st.Reasonings)+len(st.MsgItemAdded)+len(st.FuncArgsBuf))
if len(st.Reasonings) > 0 {
for _, r := range st.Reasonings {
item := []byte(`{"id":"","type":"reasoning","summary":[{"type":"summary_text","text":""}]}`)
item, _ = sjson.SetBytes(item, "id", r.ReasoningID)
item, _ = sjson.SetBytes(item, "summary.0.text", r.ReasoningData)
outputItems = append(outputItems, completedOutputItem{index: r.OutputIndex, raw: item})
}
}
if len(st.MsgItemAdded) > 0 {
for i := range st.MsgItemAdded {
txt := ""
if b := st.MsgTextBuf[i]; b != nil {
txt = b.String()
}
item := []byte(`{"id":"","type":"message","status":"completed","content":[{"type":"output_text","annotations":[],"logprobs":[],"text":""}],"role":"assistant"}`)
item, _ = sjson.SetBytes(item, "id", fmt.Sprintf("msg_%s_%d", st.ResponseID, i))
item, _ = sjson.SetBytes(item, "content.0.text", txt)
outputItems = append(outputItems, completedOutputItem{index: st.MsgOutputIx[i], raw: item})
}
}
if len(st.FuncArgsBuf) > 0 {
for key := range st.FuncArgsBuf {
args := ""
if b := st.FuncArgsBuf[key]; b != nil {
args = b.String()
}
callID := st.FuncCallIDs[key]
name := st.FuncNames[key]
item := []byte(`{"id":"","type":"function_call","status":"completed","arguments":"","call_id":"","name":""}`)
item, _ = sjson.SetBytes(item, "id", fmt.Sprintf("fc_%s", callID))
item, _ = sjson.SetBytes(item, "arguments", args)
item, _ = sjson.SetBytes(item, "call_id", callID)
item, _ = sjson.SetBytes(item, "name", name)
outputItems = append(outputItems, completedOutputItem{index: st.FuncOutputIx[key], raw: item})
}
}
sort.Slice(outputItems, func(i, j int) bool { return outputItems[i].index < outputItems[j].index })
for _, item := range outputItems {
outputsWrapper, _ = sjson.SetRawBytes(outputsWrapper, "arr.-1", item.raw)
}
if gjson.GetBytes(outputsWrapper, "arr.#").Int() > 0 {
completed, _ = sjson.SetRawBytes(completed, "response.output", []byte(gjson.GetBytes(outputsWrapper, "arr").Raw))
}
if st.UsageSeen {
completed, _ = sjson.SetBytes(completed, "response.usage.input_tokens", st.PromptTokens)
completed, _ = sjson.SetBytes(completed, "response.usage.input_tokens_details.cached_tokens", st.CachedTokens)
completed, _ = sjson.SetBytes(completed, "response.usage.output_tokens", st.CompletionTokens)
if st.ReasoningTokens > 0 {
completed, _ = sjson.SetBytes(completed, "response.usage.output_tokens_details.reasoning_tokens", st.ReasoningTokens)
}
total := st.TotalTokens
if total == 0 {
total = st.PromptTokens + st.CompletionTokens
}
completed, _ = sjson.SetBytes(completed, "response.usage.total_tokens", total)
}
out = append(out, emitRespEvent("response.completed", completed))
st.CompletionPending = true
}
return true
@@ -24,6 +24,120 @@ func parseOpenAIResponsesSSEEvent(t *testing.T, chunk []byte) (string, gjson.Res
return event, gjson.Parse(dataLine)
}
func TestConvertOpenAIChatCompletionsResponseToOpenAIResponses_ResponseCompletedWaitsForDone(t *testing.T) {
t.Parallel()
request := []byte(`{"model":"gpt-5.4","tool_choice":"auto","parallel_tool_calls":true}`)
tests := []struct {
name string
in []string
doneInputIndex int // Index in tt.in where the terminal [DONE] chunk arrives and response.completed must be emitted.
hasUsage bool
inputTokens int64
outputTokens int64
totalTokens int64
}{
{
// A provider may send finish_reason first and only attach usage in a later chunk (e.g. Vertex AI),
// so response.completed must wait for [DONE] to include that usage.
name: "late usage after finish reason",
in: []string{
`data: {"id":"resp_late_usage","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":"assistant","content":null,"reasoning_content":null,"tool_calls":[{"index":0,"id":"call_late_usage","type":"function","function":{"name":"read","arguments":""}}]},"finish_reason":null}]}`,
`data: {"id":"resp_late_usage","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":[{"index":0,"function":{"arguments":"{\"filePath\":\"C:\\\\repo\\\\README.md\"}"}}]},"finish_reason":"tool_calls"}]}`,
`data: {"id":"resp_late_usage","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[],"usage":{"prompt_tokens":11,"completion_tokens":7,"total_tokens":18}}`,
`data: [DONE]`,
},
doneInputIndex: 3,
hasUsage: true,
inputTokens: 11,
outputTokens: 7,
totalTokens: 18,
},
{
// When usage arrives on the same chunk as finish_reason, we still expect a
// single response.completed event and it should remain deferred until [DONE].
name: "usage on finish reason chunk",
in: []string{
`data: {"id":"resp_usage_same_chunk","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":"assistant","content":null,"reasoning_content":null,"tool_calls":[{"index":0,"id":"call_usage_same_chunk","type":"function","function":{"name":"read","arguments":""}}]},"finish_reason":null}]}`,
`data: {"id":"resp_usage_same_chunk","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":[{"index":0,"function":{"arguments":"{\"filePath\":\"C:\\\\repo\\\\README.md\"}"}}]},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":13,"completion_tokens":5,"total_tokens":18}}`,
`data: [DONE]`,
},
doneInputIndex: 2,
hasUsage: true,
inputTokens: 13,
outputTokens: 5,
totalTokens: 18,
},
{
// An OpenAI-compatible streams from a buggy server might never send usage, so response.completed should
// still wait for [DONE] but omit the usage object entirely.
name: "no usage chunk",
in: []string{
`data: {"id":"resp_no_usage","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":"assistant","content":null,"reasoning_content":null,"tool_calls":[{"index":0,"id":"call_no_usage","type":"function","function":{"name":"read","arguments":""}}]},"finish_reason":null}]}`,
`data: {"id":"resp_no_usage","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":[{"index":0,"function":{"arguments":"{\"filePath\":\"C:\\\\repo\\\\README.md\"}"}}]},"finish_reason":"tool_calls"}]}`,
`data: [DONE]`,
},
doneInputIndex: 2,
hasUsage: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
completedCount := 0
completedInputIndex := -1
var completedData gjson.Result
// Reuse converter state across input lines to simulate one streaming response.
var param any
for i, line := range tt.in {
// One upstream chunk can emit multiple downstream SSE events.
for _, chunk := range ConvertOpenAIChatCompletionsResponseToOpenAIResponses(context.Background(), "model", request, request, []byte(line), &param) {
event, data := parseOpenAIResponsesSSEEvent(t, chunk)
if event != "response.completed" {
continue
}
completedCount++
completedInputIndex = i
completedData = data
if i < tt.doneInputIndex {
t.Fatalf("unexpected early response.completed on input index %d", i)
}
}
}
if completedCount != 1 {
t.Fatalf("expected exactly 1 response.completed event, got %d", completedCount)
}
if completedInputIndex != tt.doneInputIndex {
t.Fatalf("expected response.completed on terminal [DONE] chunk at input index %d, got %d", tt.doneInputIndex, completedInputIndex)
}
// Missing upstream usage should stay omitted in the final completed event.
if !tt.hasUsage {
if completedData.Get("response.usage").Exists() {
t.Fatalf("expected response.completed to omit usage when none was provided, got %s", completedData.Get("response.usage").Raw)
}
return
}
// When usage is present, the final response.completed event must preserve the usage values.
if got := completedData.Get("response.usage.input_tokens").Int(); got != tt.inputTokens {
t.Fatalf("unexpected response.usage.input_tokens: got %d want %d", got, tt.inputTokens)
}
if got := completedData.Get("response.usage.output_tokens").Int(); got != tt.outputTokens {
t.Fatalf("unexpected response.usage.output_tokens: got %d want %d", got, tt.outputTokens)
}
if got := completedData.Get("response.usage.total_tokens").Int(); got != tt.totalTokens {
t.Fatalf("unexpected response.usage.total_tokens: got %d want %d", got, tt.totalTokens)
}
})
}
}
func TestConvertOpenAIChatCompletionsResponseToOpenAIResponses_MultipleToolCallsRemainSeparate(t *testing.T) {
in := []string{
`data: {"id":"resp_test","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":"assistant","content":null,"reasoning_content":null,"tool_calls":[{"index":0,"id":"call_read","type":"function","function":{"name":"read","arguments":""}}]},"finish_reason":null}]}`,
@@ -31,6 +145,7 @@ func TestConvertOpenAIChatCompletionsResponseToOpenAIResponses_MultipleToolCalls
`data: {"id":"resp_test","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":"assistant","content":null,"reasoning_content":null,"tool_calls":[{"index":1,"id":"call_glob","type":"function","function":{"name":"glob","arguments":""}}]},"finish_reason":null}]}`,
`data: {"id":"resp_test","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":[{"index":1,"function":{"arguments":"{\"path\":\"C:\\\\repo\",\"pattern\":\"*.{yml,yaml}\"}"}}]},"finish_reason":null}]}`,
`data: {"id":"resp_test","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":null},"finish_reason":"tool_calls"}],"usage":{"completion_tokens":10,"total_tokens":20,"prompt_tokens":10}}`,
`data: [DONE]`,
}
request := []byte(`{"model":"gpt-5.4","tool_choice":"auto","parallel_tool_calls":true}`)
@@ -131,6 +246,7 @@ func TestConvertOpenAIChatCompletionsResponseToOpenAIResponses_MultiChoiceToolCa
`data: {"id":"resp_multi_choice","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":"assistant","content":null,"reasoning_content":null,"tool_calls":[{"index":0,"id":"call_choice0","type":"function","function":{"name":"glob","arguments":""}}]},"finish_reason":null},{"index":1,"delta":{"role":"assistant","content":null,"reasoning_content":null,"tool_calls":[{"index":0,"id":"call_choice1","type":"function","function":{"name":"read","arguments":""}}]},"finish_reason":null}]}`,
`data: {"id":"resp_multi_choice","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":[{"index":0,"function":{"arguments":"{\"path\":\"C:\\\\repo\",\"pattern\":\"*.go\"}"}}]},"finish_reason":null},{"index":1,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":[{"index":0,"function":{"arguments":"{\"filePath\":\"C:\\\\repo\\\\README.md\",\"limit\":20,\"offset\":1}"}}]},"finish_reason":null}]}`,
`data: {"id":"resp_multi_choice","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":null},"finish_reason":"tool_calls"},{"index":1,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":null},"finish_reason":"tool_calls"}],"usage":{"completion_tokens":10,"total_tokens":20,"prompt_tokens":10}}`,
`data: [DONE]`,
}
request := []byte(`{"model":"gpt-5.4","tool_choice":"auto","parallel_tool_calls":true}`)
@@ -213,6 +329,7 @@ func TestConvertOpenAIChatCompletionsResponseToOpenAIResponses_MixedMessageAndTo
in := []string{
`data: {"id":"resp_mixed","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":"assistant","content":"hello","reasoning_content":null,"tool_calls":null},"finish_reason":null},{"index":1,"delta":{"role":"assistant","content":null,"reasoning_content":null,"tool_calls":[{"index":0,"id":"call_choice1","type":"function","function":{"name":"read","arguments":""}}]},"finish_reason":null}]}`,
`data: {"id":"resp_mixed","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":null},"finish_reason":"stop"},{"index":1,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":[{"index":0,"function":{"arguments":"{\"filePath\":\"C:\\\\repo\\\\README.md\",\"limit\":20,\"offset\":1}"}}]},"finish_reason":"tool_calls"}],"usage":{"completion_tokens":10,"total_tokens":20,"prompt_tokens":10}}`,
`data: [DONE]`,
}
request := []byte(`{"model":"gpt-5.4","tool_choice":"auto","parallel_tool_calls":true}`)
@@ -261,6 +378,7 @@ func TestConvertOpenAIChatCompletionsResponseToOpenAIResponses_FunctionCallDoneA
`data: {"id":"resp_order","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":"assistant","content":null,"reasoning_content":null,"tool_calls":[{"index":1,"id":"call_read","type":"function","function":{"name":"read","arguments":""}}]},"finish_reason":null}]}`,
`data: {"id":"resp_order","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":[{"index":1,"function":{"arguments":"{\"filePath\":\"C:\\\\repo\\\\README.md\",\"limit\":20,\"offset\":1}"}}]},"finish_reason":null}]}`,
`data: {"id":"resp_order","object":"chat.completion.chunk","created":1773896263,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":null},"finish_reason":"tool_calls"}],"usage":{"completion_tokens":10,"total_tokens":20,"prompt_tokens":10}}`,
`data: [DONE]`,
}
request := []byte(`{"model":"gpt-5.4","tool_choice":"auto","parallel_tool_calls":true}`)
+2346
View File
File diff suppressed because it is too large Load Diff
+27
View File
@@ -0,0 +1,27 @@
[workspace]
members = ["proxypilot-rs"]
resolver = "2"
[workspace.package]
edition = "2024"
license = "MIT"
version = "0.1.0"
[workspace.dependencies]
anyhow = "1.0.100"
axum = { version = "0.8.8", features = ["macros", "json"] }
base64 = "0.22.1"
clap = { version = "4.6.0", features = ["derive"] }
crossterm = "0.29.0"
http = "1.3.1"
ratatui = "0.29.0"
reqwest = { version = "0.12.25", default-features = false, features = ["http2", "json", "rustls-tls"] }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.145"
tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread", "signal", "time", "sync", "net"] }
toml = "0.9.8"
tower = "0.5.2"
tower-http = { version = "0.6.6", features = ["trace"] }
tracing = "0.1.41"
tracing-subscriber = { version = "0.3.20", features = ["env-filter"] }
url = "2.5.7"
+26
View File
@@ -0,0 +1,26 @@
[package]
name = "proxypilot-rs"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
anyhow.workspace = true
axum.workspace = true
base64.workspace = true
clap.workspace = true
crossterm.workspace = true
http.workspace = true
ratatui.workspace = true
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
tokio.workspace = true
toml.workspace = true
tower-http.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
url.workspace = true
[dev-dependencies]
tower = { workspace = true, features = ["util"] }
+537
View File
@@ -0,0 +1,537 @@
use std::fs;
use std::path::Path;
use anyhow::{Context, Result};
use crate::codex;
use crate::config::AppConfig;
use crate::state::{AccountState, ImportedCodexAuth};
fn saved_account_message(provider_label: &str, name: &str, active: bool) -> String {
if active {
format!(
"saved {} account `{}` in local state and marked it active",
provider_label, name
)
} else {
format!("saved {} account `{}` in local state", provider_label, name)
}
}
fn imported_account_message(name: &str, active: bool) -> String {
if active {
format!(
"imported Codex auth `{}` into local state and marked it active",
name
)
} else {
format!("imported Codex auth `{}` into local state", name)
}
}
fn device_login_message(name: &str, active: bool) -> String {
if active {
format!(
"saved Codex device login `{}` in local state and marked it active",
name
)
} else {
format!("saved Codex device login `{}` in local state", name)
}
}
fn refresh_message(name: &str) -> String {
format!("refreshed saved Codex account `{}` in local state", name)
}
fn refresh_mode_label(refresh_token: Option<&str>) -> &'static str {
if refresh_token
.map(|value| !value.trim().is_empty())
.unwrap_or(false)
{
"refreshable"
} else {
"static"
}
}
fn account_list_row(account: &crate::state::AccountEntry, active_name: Option<&str>) -> String {
let marker = if active_name == Some(account.name.as_str()) {
"*"
} else {
"-"
};
let email = account.email.as_deref().unwrap_or("-");
let source = account.source.as_deref().unwrap_or("-");
let plan = account.plan_type.as_deref().unwrap_or("-");
let refresh = refresh_mode_label(account.refresh_token.as_deref());
format!(
"{marker} {:<16} provider={} email={} plan={} refresh={} source={}",
account.name, account.provider, email, plan, refresh, source
)
}
pub fn add_codex_account(
config: &AppConfig,
config_path: &Path,
name: String,
api_key: String,
activate: bool,
) -> Result<()> {
let state_path = config.resolve_state_path(config_path);
let mut state = AccountState::load_or_default(&state_path)?;
state.add_or_replace_codex_account(name.clone(), api_key, activate)?;
state.save(&state_path)?;
println!(
"{}",
saved_account_message(
"Codex",
&name,
state.active_account.as_deref() == Some(name.as_str())
)
);
println!("state file: {}", state_path.display());
Ok(())
}
pub fn add_claude_account(
config: &AppConfig,
config_path: &Path,
name: String,
api_key: String,
activate: bool,
) -> Result<()> {
let state_path = config.resolve_state_path(config_path);
let mut state = AccountState::load_or_default(&state_path)?;
state.add_or_replace_manual_account(crate::provider::CLAUDE_PROVIDER, name.clone(), api_key, activate)?;
state.save(&state_path)?;
println!(
"{}",
saved_account_message(
"Claude",
&name,
state.active_account.as_deref() == Some(name.as_str())
)
);
println!("state file: {}", state_path.display());
Ok(())
}
pub async fn refresh_claude_account(
config: &AppConfig,
config_path: &Path,
name: Option<String>,
) -> Result<()> {
let state_path = config.resolve_state_path(config_path);
let state = AccountState::load_or_default(&state_path)?;
let target = if let Some(name) = name.filter(|value| !value.trim().is_empty()) {
state
.account_by_name_and_provider(&name, crate::provider::CLAUDE_PROVIDER)
.ok_or_else(|| anyhow::anyhow!("no saved Claude account named {}", name))?
} else {
state
.active_account_for_provider(crate::provider::CLAUDE_PROVIDER)
.ok_or_else(|| anyhow::anyhow!("no active Claude account to refresh"))?
};
let _ = target;
anyhow::bail!("Claude refresh is not implemented yet")
}
pub fn list_accounts(config: &AppConfig, config_path: &Path) -> Result<()> {
let state_path = config.resolve_state_path(config_path);
let state = AccountState::load_or_default(&state_path)?;
if state.accounts.is_empty() {
println!("No local Rust accounts saved yet.");
println!("state file: {}", state_path.display());
return Ok(());
}
println!("ProxyPilot Rust local account state");
println!("state file: {}", state_path.display());
println!();
for account in &state.accounts {
println!(
"{}",
account_list_row(account, state.active_account.as_deref())
);
}
Ok(())
}
pub fn activate_account(config: &AppConfig, config_path: &Path, name: String) -> Result<()> {
let state_path = config.resolve_state_path(config_path);
let mut state = AccountState::load_or_default(&state_path)?;
state.activate(&name)?;
state.save(&state_path)?;
println!("local active account set to `{}`", name);
println!("state file: {}", state_path.display());
Ok(())
}
pub fn remove_account(config: &AppConfig, config_path: &Path, name: String) -> Result<()> {
let state_path = config.resolve_state_path(config_path);
let mut state = AccountState::load_or_default(&state_path)?;
state.remove_account(&name)?;
state.save(&state_path)?;
println!("removed account `{}` from local state", name);
match state.active_account.as_deref() {
Some(active) => println!("local active account is now `{}`", active),
None => println!("no local active account remains"),
}
println!("state file: {}", state_path.display());
Ok(())
}
pub fn import_codex_account(
config: &AppConfig,
config_path: &Path,
auth_file: &Path,
name: Option<String>,
activate: bool,
) -> Result<()> {
let raw = fs::read_to_string(auth_file)
.with_context(|| format!("failed to read Codex auth file {}", auth_file.display()))?;
let imported: ImportedCodexAuth = serde_json::from_str(&raw)
.with_context(|| format!("failed to parse Codex auth file {}", auth_file.display()))?;
let resolved_name = name
.filter(|value| !value.trim().is_empty())
.or_else(|| {
if imported.email.trim().is_empty() {
None
} else {
Some(imported.email.trim().to_string())
}
})
.or_else(|| {
auth_file
.file_stem()
.map(|value| value.to_string_lossy().to_string())
})
.unwrap_or_else(|| "codex-import".to_string());
let state_path = config.resolve_state_path(config_path);
let mut state = AccountState::load_or_default(&state_path)?;
state.add_imported_codex_account(
resolved_name.clone(),
imported,
format!("import:{}", auth_file.display()),
activate,
)?;
state.save(&state_path)?;
println!(
"{}",
imported_account_message(
&resolved_name,
state.active_account.as_deref() == Some(resolved_name.as_str())
)
);
println!("source file: {}", auth_file.display());
println!("state file: {}", state_path.display());
Ok(())
}
pub async fn login_codex_device(
config: &AppConfig,
config_path: &Path,
name: Option<String>,
activate: bool,
) -> Result<()> {
let result = codex::login_with_device_flow().await?;
let resolved_name = name
.filter(|value| !value.trim().is_empty())
.or_else(|| result.email.clone())
.or_else(|| result.account_id.clone())
.unwrap_or_else(|| "codex-device".to_string());
let state_path = config.resolve_state_path(config_path);
let mut state = AccountState::load_or_default(&state_path)?;
state.add_device_codex_account(resolved_name.clone(), result, activate)?;
state.save(&state_path)?;
println!(
"{}",
device_login_message(
&resolved_name,
state.active_account.as_deref() == Some(resolved_name.as_str())
)
);
println!("state file: {}", state_path.display());
Ok(())
}
pub async fn refresh_codex_account(
config: &AppConfig,
config_path: &Path,
name: Option<String>,
) -> Result<()> {
let state_path = config.resolve_state_path(config_path);
let mut state = AccountState::load_or_default(&state_path)?;
let target = if let Some(name) = name.filter(|value| !value.trim().is_empty()) {
state
.codex_account_by_name(&name)
.ok_or_else(|| anyhow::anyhow!("no saved Codex account named {}", name))?
} else {
state
.active_codex_account()
.ok_or_else(|| anyhow::anyhow!("no active Codex account to refresh"))?
};
let refresh_token = target.refresh_token.as_deref().ok_or_else(|| {
anyhow::anyhow!(
"account `{}` is static and has no refresh token",
target.name
)
})?;
let result = codex::refresh_with_refresh_token_from_config(config, refresh_token).await?;
let refreshed_name = target.name.clone();
state.update_codex_account_tokens(&refreshed_name, result)?;
state.save(&state_path)?;
println!("{}", refresh_message(&refreshed_name));
println!("state file: {}", state_path.display());
Ok(())
}
#[cfg(test)]
mod tests {
use std::fs;
use std::time::{SystemTime, UNIX_EPOCH};
use super::*;
fn temp_paths(label: &str) -> (std::path::PathBuf, std::path::PathBuf) {
let stamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let base = std::env::temp_dir().join(format!("proxypilot-rs-{label}-{stamp}"));
let config_path = base.with_extension("toml");
let state_path = base.with_extension("state.toml");
(config_path, state_path)
}
#[test]
fn list_accounts_marks_refreshability_in_operator_copy() {
assert_eq!(refresh_mode_label(Some("token")), "refreshable");
assert_eq!(refresh_mode_label(None), "static");
assert_eq!(refresh_mode_label(Some(" ")), "static");
}
#[test]
fn account_list_row_marks_active_and_static_accounts_readably() {
let account = crate::state::AccountEntry {
name: "primary".to_string(),
provider: "codex".to_string(),
api_key: "key".to_string(),
refresh_token: None,
id_token: None,
email: Some("dev@example.com".to_string()),
account_id: None,
plan_type: Some("pro".to_string()),
expires_at: None,
source: Some("manual".to_string()),
};
let row = account_list_row(&account, Some("primary"));
assert!(row.starts_with("* primary"));
assert!(row.contains("refresh=static"));
assert!(row.contains("source=manual"));
}
#[test]
fn saved_account_messages_stay_explicit_about_local_state() {
assert_eq!(
saved_account_message("Codex", "primary", true),
"saved Codex account `primary` in local state and marked it active"
);
assert_eq!(
saved_account_message("Codex", "backup", false),
"saved Codex account `backup` in local state"
);
assert_eq!(
imported_account_message("imported", true),
"imported Codex auth `imported` into local state and marked it active"
);
assert_eq!(
device_login_message("device", false),
"saved Codex device login `device` in local state"
);
assert_eq!(
refresh_message("primary"),
"refreshed saved Codex account `primary` in local state"
);
}
#[test]
fn saved_account_messages_can_describe_claude_accounts() {
assert_eq!(
saved_account_message("Claude", "claude-main", true),
"saved Claude account `claude-main` in local state and marked it active"
);
}
#[test]
fn account_list_row_includes_provider_name_for_claude() {
let account = crate::state::AccountEntry {
name: "claude-main".to_string(),
provider: crate::provider::CLAUDE_PROVIDER.to_string(),
api_key: "key".to_string(),
refresh_token: None,
id_token: None,
email: Some("dev@example.com".to_string()),
account_id: None,
plan_type: Some("max".to_string()),
expires_at: None,
source: Some("manual".to_string()),
};
let row = account_list_row(&account, Some("claude-main"));
assert!(row.contains("provider=claude"));
assert!(row.contains("plan=max"));
}
#[tokio::test]
async fn refresh_codex_account_reports_static_accounts_clearly() {
let (config_path, state_path) = temp_paths("static-refresh");
let config = AppConfig {
state: crate::config::StateConfig {
path: state_path.display().to_string(),
},
..AppConfig::default()
};
fs::write(&config_path, AppConfig::example_toml()).unwrap();
let mut state = AccountState::default();
state
.add_or_replace_codex_account("primary".to_string(), "sk-test".to_string(), true)
.unwrap();
state.save(&state_path).unwrap();
let error = refresh_codex_account(&config, &config_path, Some("primary".to_string()))
.await
.unwrap_err()
.to_string();
assert!(error.contains("static"));
assert!(error.contains("no refresh token"));
let _ = fs::remove_file(config_path);
let _ = fs::remove_file(state_path);
}
#[tokio::test]
async fn refresh_codex_account_reports_missing_targets_clearly() {
let (config_path, state_path) = temp_paths("missing-refresh");
let config = AppConfig {
state: crate::config::StateConfig {
path: state_path.display().to_string(),
},
..AppConfig::default()
};
fs::write(&config_path, AppConfig::example_toml()).unwrap();
AccountState::default().save(&state_path).unwrap();
let error = refresh_codex_account(&config, &config_path, Some("missing".to_string()))
.await
.unwrap_err()
.to_string();
assert!(error.contains("no saved Codex account named missing"));
let _ = fs::remove_file(config_path);
let _ = fs::remove_file(state_path);
}
#[tokio::test]
async fn refresh_codex_account_uses_configured_refresh_endpoint() {
use axum::{Json, Router, routing::post};
use serde_json::json;
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
use tokio::net::TcpListener;
async fn refresh_handler() -> impl axum::response::IntoResponse {
Json(json!({
"access_token": "refreshed-access",
"refresh_token": "refreshed-refresh",
"id_token": "TEST_ID_TOKEN_PLACEHOLDER",
"expires_in": 3600
}))
}
let refresh_hits = Arc::new(AtomicUsize::new(0));
let refresh_hits_for_handler = refresh_hits.clone();
let refresh_app = Router::new().route(
"/oauth/token",
post(move || {
let refresh_hits_for_handler = refresh_hits_for_handler.clone();
async move {
refresh_hits_for_handler.fetch_add(1, Ordering::SeqCst);
refresh_handler().await
}
}),
);
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let refresh_addr = listener.local_addr().unwrap();
let server = tokio::spawn(async move {
axum::serve(listener, refresh_app).await.unwrap();
});
let (config_path, state_path) = temp_paths("refresh-endpoint");
let mut config = AppConfig::default();
config.state.path = state_path.display().to_string();
config.codex.refresh_token_url = format!("http://{refresh_addr}/oauth/token");
fs::write(&config_path, AppConfig::example_toml()).unwrap();
let mut state = AccountState::default();
state
.add_device_codex_account(
"primary".to_string(),
crate::codex::DeviceAuthResult {
access_token: "old-access".to_string(),
refresh_token: "old-refresh".to_string(),
id_token: "old-id".to_string(),
email: Some("refresh@example.com".to_string()),
account_id: Some("acct".to_string()),
plan_type: Some("pro".to_string()),
expires_at: Some("1970-01-01T00:00:00Z".to_string()),
},
true,
)
.unwrap();
state.save(&state_path).unwrap();
refresh_codex_account(&config, &config_path, Some("primary".to_string()))
.await
.unwrap();
assert_eq!(refresh_hits.load(Ordering::SeqCst), 1);
let updated = AccountState::load_or_default(&state_path).unwrap();
let refreshed = updated.codex_account_by_name("primary").unwrap();
assert_eq!(refreshed.api_key, "refreshed-access");
assert_eq!(
refreshed.refresh_token.as_deref(),
Some("refreshed-refresh")
);
// The placeholder id_token does not encode an exp claim, so expires_at
// is derived from the current time + expires_in rather than a fixed value.
assert!(refreshed.expires_at.is_some(), "expires_at should be set after refresh");
server.abort();
let _ = fs::remove_file(config_path);
let _ = fs::remove_file(state_path);
}
}
+510
View File
@@ -0,0 +1,510 @@
use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
pub const EXPIRING_SOON_WINDOW_SECS: i64 = 300;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuthHealthState {
Valid,
ExpiringSoon,
Expired,
Static,
Unknown,
}
impl AuthHealthState {
pub fn label(self) -> &'static str {
match self {
Self::Valid => "valid",
Self::ExpiringSoon => "expiring soon",
Self::Expired => "expired",
Self::Static => "static",
Self::Unknown => "unknown",
}
}
pub fn is_refresh_worthy(self) -> bool {
matches!(self, Self::ExpiringSoon | Self::Expired)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuthCredentialSource {
ActiveAccount,
ConfigFallbackKey,
NoCredential,
}
impl AuthCredentialSource {
pub fn label(self) -> &'static str {
match self {
Self::ActiveAccount => "saved account",
Self::ConfigFallbackKey => "config fallback key",
Self::NoCredential => "no credential",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuthMetadataState {
Present,
Missing,
Malformed,
}
impl AuthMetadataState {
pub fn label(self) -> &'static str {
match self {
Self::Present => "present",
Self::Missing => "missing",
Self::Malformed => "malformed",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthHealthSnapshot {
pub state: AuthHealthState,
pub source: AuthCredentialSource,
pub metadata_state: AuthMetadataState,
pub expires_at: Option<String>,
pub expires_at_unix_secs: Option<i64>,
pub expires_in_secs: Option<i64>,
}
impl AuthHealthSnapshot {
pub fn summary_label(&self) -> &'static str {
self.state.label()
}
pub fn source_label(&self) -> &'static str {
self.source.label()
}
pub fn expiry_detail(&self) -> String {
match self.state {
AuthHealthState::Valid => self
.expires_at
.as_deref()
.map(|value| format!("valid until {}", value))
.unwrap_or_else(|| "valid".to_string()),
AuthHealthState::ExpiringSoon => self
.expires_at
.as_deref()
.map(|value| format!("expiring soon at {}", value))
.unwrap_or_else(|| "expiring soon".to_string()),
AuthHealthState::Expired => self
.expires_at
.as_deref()
.map(|value| format!("expired at {}", value))
.unwrap_or_else(|| "expired".to_string()),
AuthHealthState::Static => match self.source {
AuthCredentialSource::ConfigFallbackKey => "config fallback key".to_string(),
_ => "static access token".to_string(),
},
AuthHealthState::Unknown => match self.metadata_state {
AuthMetadataState::Malformed => self
.expires_at
.as_deref()
.map(|value| format!("malformed expiry metadata ({value})"))
.unwrap_or_else(|| "malformed expiry metadata".to_string()),
AuthMetadataState::Missing => "expiry metadata missing".to_string(),
AuthMetadataState::Present => "auth state unknown".to_string(),
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum RefreshStatusKind {
Success,
Failure,
Skipped,
#[default]
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct RefreshStatusSnapshot {
pub kind: RefreshStatusKind,
pub account_name: Option<String>,
pub occurred_at_unix_secs: Option<i64>,
pub message: Option<String>,
}
impl RefreshStatusSnapshot {
pub fn success(account_name: impl Into<String>, occurred_at_unix_secs: i64) -> Self {
Self {
kind: RefreshStatusKind::Success,
account_name: Some(account_name.into()),
occurred_at_unix_secs: Some(occurred_at_unix_secs),
message: None,
}
}
pub fn failure(
account_name: Option<impl Into<String>>,
occurred_at_unix_secs: i64,
message: impl Into<String>,
) -> Self {
Self {
kind: RefreshStatusKind::Failure,
account_name: account_name.map(Into::into),
occurred_at_unix_secs: Some(occurred_at_unix_secs),
message: Some(message.into()),
}
}
pub fn skipped(message: impl Into<String>) -> Self {
Self {
kind: RefreshStatusKind::Skipped,
account_name: None,
occurred_at_unix_secs: None,
message: Some(message.into()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct RuntimeRequestCounters {
pub total_proxied_requests: u64,
pub successful_upstream_responses: u64,
pub auth_refresh_attempts: u64,
pub auth_refresh_failures: u64,
pub upstream_401_count: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeStatsSnapshot {
pub bind_address: String,
pub upstream_base_url: String,
pub active_account_name: Option<String>,
pub account_count: usize,
pub auth_health: AuthHealthSnapshot,
pub request_counters: RuntimeRequestCounters,
pub last_refresh: RefreshStatusSnapshot,
}
impl RuntimeStatsSnapshot {
pub fn new(
bind_address: impl Into<String>,
upstream_base_url: impl Into<String>,
active_account_name: Option<String>,
account_count: usize,
auth_health: AuthHealthSnapshot,
) -> Self {
Self {
bind_address: bind_address.into(),
upstream_base_url: upstream_base_url.into(),
active_account_name,
account_count,
auth_health,
request_counters: RuntimeRequestCounters::default(),
last_refresh: RefreshStatusSnapshot::default(),
}
}
}
pub fn now_unix_secs() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| duration.as_secs() as i64)
.unwrap_or_default()
}
pub fn parse_rfc3339_z(value: &str) -> Option<i64> {
if value.len() != 20 || !value.ends_with('Z') {
return None;
}
if value.as_bytes().get(4) != Some(&b'-')
|| value.as_bytes().get(7) != Some(&b'-')
|| value.as_bytes().get(10) != Some(&b'T')
|| value.as_bytes().get(13) != Some(&b':')
|| value.as_bytes().get(16) != Some(&b':')
{
return None;
}
let year = value.get(0..4)?.parse::<i64>().ok()?;
let month = value.get(5..7)?.parse::<i64>().ok()?;
let day = value.get(8..10)?.parse::<i64>().ok()?;
let hour = value.get(11..13)?.parse::<i64>().ok()?;
let minute = value.get(14..16)?.parse::<i64>().ok()?;
let second = value.get(17..19)?.parse::<i64>().ok()?;
if !(1..=12).contains(&month)
|| !(1..=31).contains(&day)
|| !(0..=23).contains(&hour)
|| !(0..=59).contains(&minute)
|| !(0..=59).contains(&second)
{
return None;
}
if day > i64::from(days_in_month(year, month as u32)?) {
return None;
}
let adjusted_year = year - if month <= 2 { 1 } else { 0 };
let era = if adjusted_year >= 0 {
adjusted_year
} else {
adjusted_year - 399
} / 400;
let yoe = adjusted_year - era * 400;
let month_prime = month + if month > 2 { -3 } else { 9 };
let doy = (153 * month_prime + 2) / 5 + day - 1;
if !(0..=365).contains(&doy) {
return None;
}
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
let days = era * 146_097 + doe - 719_468;
Some(days * 86_400 + hour * 3_600 + minute * 60 + second)
}
fn days_in_month(year: i64, month: u32) -> Option<u8> {
Some(match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 if is_leap_year(year) => 29,
2 => 28,
_ => return None,
})
}
fn is_leap_year(year: i64) -> bool {
let year = year.rem_euclid(400);
year % 4 == 0 && (year % 100 != 0 || year == 0)
}
pub fn evaluate_auth_health(
source: AuthCredentialSource,
refresh_token: Option<&str>,
expires_at: Option<&str>,
now_unix_secs: i64,
) -> AuthHealthSnapshot {
let expires_at = normalize_optional(expires_at);
let refresh_token = normalize_optional(refresh_token);
match source {
AuthCredentialSource::ConfigFallbackKey => AuthHealthSnapshot {
state: AuthHealthState::Static,
source,
metadata_state: if expires_at.is_some() {
AuthMetadataState::Present
} else {
AuthMetadataState::Missing
},
expires_at,
expires_at_unix_secs: None,
expires_in_secs: None,
},
AuthCredentialSource::NoCredential => AuthHealthSnapshot {
state: AuthHealthState::Unknown,
source,
metadata_state: if expires_at.is_some() {
AuthMetadataState::Present
} else {
AuthMetadataState::Missing
},
expires_at: expires_at.clone(),
expires_at_unix_secs: expires_at.as_deref().and_then(parse_rfc3339_z),
expires_in_secs: None,
},
AuthCredentialSource::ActiveAccount => {
if refresh_token.is_none() {
return AuthHealthSnapshot {
state: AuthHealthState::Static,
source,
metadata_state: if expires_at.is_some() {
AuthMetadataState::Present
} else {
AuthMetadataState::Missing
},
expires_at: expires_at.clone(),
expires_at_unix_secs: expires_at.as_deref().and_then(parse_rfc3339_z),
expires_in_secs: None,
};
}
let Some(expires_at_text) = expires_at.clone() else {
return AuthHealthSnapshot {
state: AuthHealthState::Unknown,
source,
metadata_state: AuthMetadataState::Missing,
expires_at: None,
expires_at_unix_secs: None,
expires_in_secs: None,
};
};
let Some(expires_at_unix_secs) = parse_rfc3339_z(&expires_at_text) else {
return AuthHealthSnapshot {
state: AuthHealthState::Unknown,
source,
metadata_state: AuthMetadataState::Malformed,
expires_at: Some(expires_at_text),
expires_at_unix_secs: None,
expires_in_secs: None,
};
};
let expires_in_secs = expires_at_unix_secs - now_unix_secs;
let state = if expires_in_secs <= 0 {
AuthHealthState::Expired
} else if expires_in_secs <= EXPIRING_SOON_WINDOW_SECS {
AuthHealthState::ExpiringSoon
} else {
AuthHealthState::Valid
};
AuthHealthSnapshot {
state,
source,
metadata_state: AuthMetadataState::Present,
expires_at: Some(expires_at_text),
expires_at_unix_secs: Some(expires_at_unix_secs),
expires_in_secs: Some(expires_in_secs),
}
}
}
}
fn normalize_optional(value: Option<&str>) -> Option<String> {
value
.map(str::trim)
.filter(|value| !value.is_empty())
.map(ToOwned::to_owned)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_rfc3339_z_timestamps() {
assert_eq!(parse_rfc3339_z("1970-01-01T00:00:00Z"), Some(0));
assert_eq!(parse_rfc3339_z("1970-01-02T00:00:00Z"), Some(86_400));
}
#[test]
fn rejects_non_rfc3339_z_timestamps() {
assert_eq!(parse_rfc3339_z("2026-04-05 12:00:00"), None);
assert_eq!(parse_rfc3339_z("2026-13-05T12:00:00Z"), None);
assert_eq!(parse_rfc3339_z("2026-04-31T12:00:00Z"), None);
assert_eq!(parse_rfc3339_z("2023-02-29T12:00:00Z"), None);
}
#[test]
fn accepts_valid_leap_day_timestamps() {
assert_eq!(parse_rfc3339_z("2024-02-29T12:00:00Z"), Some(1_709_208_000));
assert_eq!(parse_rfc3339_z("2000-02-29T23:59:59Z"), Some(951_868_799));
}
#[test]
fn classifies_active_account_auth_health_states() {
let now = 1_700_000_000;
let valid = evaluate_auth_health(
AuthCredentialSource::ActiveAccount,
Some("refresh"),
Some("2023-11-15T12:00:30Z"),
now,
);
assert_eq!(valid.state, AuthHealthState::Valid);
assert_eq!(valid.summary_label(), "valid");
let expiring_soon = evaluate_auth_health(
AuthCredentialSource::ActiveAccount,
Some("refresh"),
Some("2023-11-14T22:14:05Z"),
now,
);
assert_eq!(expiring_soon.state, AuthHealthState::ExpiringSoon);
assert!(expiring_soon.state.is_refresh_worthy());
let expired = evaluate_auth_health(
AuthCredentialSource::ActiveAccount,
Some("refresh"),
Some("2023-11-14T22:07:00Z"),
now,
);
assert_eq!(expired.state, AuthHealthState::Expired);
assert!(expired.state.is_refresh_worthy());
}
#[test]
fn classifies_static_fallback_and_malformed_metadata_explicitly() {
let static_fallback = evaluate_auth_health(
AuthCredentialSource::ConfigFallbackKey,
None,
None,
1_700_000_000,
);
assert_eq!(static_fallback.state, AuthHealthState::Static);
assert_eq!(static_fallback.source_label(), "config fallback key");
let static_saved_token = evaluate_auth_health(
AuthCredentialSource::ActiveAccount,
None,
Some("2026-04-06T00:00:00Z"),
1_700_000_000,
);
assert_eq!(static_saved_token.state, AuthHealthState::Static);
let malformed = evaluate_auth_health(
AuthCredentialSource::ActiveAccount,
Some("refresh"),
Some("not-a-timestamp"),
1_700_000_000,
);
assert_eq!(malformed.state, AuthHealthState::Unknown);
assert_eq!(malformed.metadata_state, AuthMetadataState::Malformed);
assert!(
malformed
.expiry_detail()
.contains("malformed expiry metadata")
);
let impossible_calendar_date = evaluate_auth_health(
AuthCredentialSource::ActiveAccount,
Some("refresh"),
Some("2023-02-29T12:00:00Z"),
1_700_000_000,
);
assert_eq!(impossible_calendar_date.state, AuthHealthState::Unknown);
assert_eq!(
impossible_calendar_date.metadata_state,
AuthMetadataState::Malformed
);
}
#[test]
fn runtime_stats_snapshot_serializes_with_stable_fields() {
let snapshot = RuntimeStatsSnapshot::new(
"127.0.0.1:8318",
"https://upstream.example/v1",
Some("primary".to_string()),
2,
AuthHealthSnapshot {
state: AuthHealthState::Valid,
source: AuthCredentialSource::ActiveAccount,
metadata_state: AuthMetadataState::Present,
expires_at: Some("2026-04-06T00:00:00Z".to_string()),
expires_at_unix_secs: Some(1_765_440_000),
expires_in_secs: Some(86_400),
},
);
let payload = serde_json::to_value(snapshot).unwrap();
assert_eq!(payload["bind_address"], "127.0.0.1:8318");
assert_eq!(payload["auth_health"]["state"], "valid");
assert_eq!(payload["request_counters"]["total_proxied_requests"], 0);
assert_eq!(payload["last_refresh"]["kind"], "unknown");
}
}
+57
View File
@@ -0,0 +1,57 @@
use anyhow::{Result, bail};
use crate::provider::{Provider, ProviderId, RefreshResult};
pub struct ClaudeProvider {
upstream_base_url: String,
}
impl ClaudeProvider {
pub fn new(upstream_base_url: String) -> Self {
Self { upstream_base_url }
}
pub fn from_config(config: &crate::config::AppConfig) -> Self {
Self::new(config.claude.upstream_base_url.clone())
}
}
impl Provider for ClaudeProvider {
fn id(&self) -> ProviderId {
ProviderId::CLAUDE
}
fn display_name(&self) -> &'static str {
"Claude"
}
fn upstream_base_url(&self) -> &str {
&self.upstream_base_url
}
fn default_upstream_base_url() -> &'static str
where
Self: Sized,
{
"https://api.anthropic.com"
}
fn apply_auth_headers(
&self,
request: reqwest::RequestBuilder,
api_key: &str,
) -> reqwest::RequestBuilder {
request
.header("x-api-key", api_key)
.header("anthropic-version", "2023-06-01")
}
fn refresh_token<'a>(
&'a self,
_refresh_token: &'a str,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<RefreshResult>> + Send + 'a>> {
Box::pin(async move {
bail!("Claude refresh is not implemented yet")
})
}
}
+125
View File
@@ -0,0 +1,125 @@
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(name = "proxypilot-rs")]
#[command(about = "Experimental terminal-first Rust replatform for ProxyPilot")]
#[command(version)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
}
#[derive(Debug, Subcommand)]
pub enum Command {
/// Write an example config to disk.
Init {
#[arg(long, default_value = "proxypilot-rs.toml")]
config: PathBuf,
#[arg(long, default_value_t = false)]
force: bool,
},
/// Run the local proxy server.
Run {
#[arg(long, default_value = "proxypilot-rs.toml")]
config: PathBuf,
},
/// Open a terminal operator view for the Rust rewrite.
Tui {
#[arg(long, default_value = "proxypilot-rs.toml")]
config: PathBuf,
},
/// Manage local account state for the Rust rewrite.
Account {
#[command(flatten)]
shared: SharedConfig,
#[command(subcommand)]
command: AccountCommand,
},
}
#[derive(Debug, Clone, Args)]
pub struct SharedConfig {
#[arg(long, default_value = "proxypilot-rs.toml")]
pub config: PathBuf,
}
#[derive(Debug, Subcommand)]
pub enum AccountCommand {
/// Add or replace a Codex account in the local state file.
AddCodex {
#[command(flatten)]
shared: SharedConfig,
#[arg(long)]
name: String,
#[arg(long)]
api_key: String,
#[arg(long, default_value_t = false)]
activate: bool,
},
/// Add or replace a Claude account in the local state file.
AddClaude {
#[command(flatten)]
shared: SharedConfig,
#[arg(long)]
name: String,
#[arg(long)]
api_key: String,
#[arg(long, default_value_t = false)]
activate: bool,
},
/// Import a Codex auth JSON file from the Go/ProxyPilot world.
ImportCodex {
#[command(flatten)]
shared: SharedConfig,
#[arg(long)]
file: PathBuf,
#[arg(long)]
name: Option<String>,
#[arg(long, default_value_t = false)]
activate: bool,
},
/// Start the Codex device flow and save the resulting account locally.
LoginCodexDevice {
#[command(flatten)]
shared: SharedConfig,
#[arg(long)]
name: Option<String>,
#[arg(long, default_value_t = false)]
activate: bool,
},
/// Refresh a saved Codex account using its refresh token.
RefreshCodex {
#[command(flatten)]
shared: SharedConfig,
#[arg(long)]
name: Option<String>,
},
/// Refresh a saved Claude account.
RefreshClaude {
#[command(flatten)]
shared: SharedConfig,
#[arg(long)]
name: Option<String>,
},
/// List saved accounts and show which one is active.
List {
#[command(flatten)]
shared: SharedConfig,
},
/// Select the active account used by the proxy.
Activate {
#[command(flatten)]
shared: SharedConfig,
#[arg(long)]
name: String,
},
/// Remove a saved account from the local state file.
Remove {
#[command(flatten)]
shared: SharedConfig,
#[arg(long)]
name: String,
},
}
+755
View File
@@ -0,0 +1,755 @@
use std::time::Duration;
use anyhow::{Context, Result, bail};
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::time::sleep;
use crate::provider::{Provider, ProviderId, RefreshResult};
pub const CLIENT_ID: &str = "app_EMoamEEZ73f0CkXaXp7hrann";
pub const DEVICE_USER_CODE_URL: &str = "https://auth.openai.com/api/accounts/deviceauth/usercode";
pub const DEVICE_TOKEN_URL: &str = "https://auth.openai.com/api/accounts/deviceauth/token";
pub const DEVICE_VERIFICATION_URL: &str = "https://auth.openai.com/codex/device";
pub const DEVICE_REDIRECT_URI: &str = "https://auth.openai.com/deviceauth/callback";
pub const OAUTH_TOKEN_URL: &str = "https://auth.openai.com/oauth/token";
const DEFAULT_POLL_INTERVAL_SECS: u64 = 5;
const DEVICE_TIMEOUT_SECS: u64 = 15 * 60;
#[derive(Debug, Clone)]
pub struct DeviceAuthResult {
pub access_token: String,
pub refresh_token: String,
pub id_token: String,
pub email: Option<String>,
pub account_id: Option<String>,
pub plan_type: Option<String>,
pub expires_at: Option<String>,
}
pub struct CodexProvider {
upstream_base_url: String,
refresh_token_url: String,
client: Client,
}
impl CodexProvider {
pub fn new(upstream_base_url: String, refresh_token_url: String) -> Self {
Self {
upstream_base_url,
refresh_token_url,
client: Client::new(),
}
}
pub fn from_config(config: &crate::config::AppConfig) -> Self {
Self::new(
config.codex.upstream_base_url.clone(),
config.codex.refresh_token_url.clone(),
)
}
}
impl Provider for CodexProvider {
fn id(&self) -> ProviderId {
ProviderId::CODEX
}
fn display_name(&self) -> &'static str {
"Codex (OpenAI)"
}
fn upstream_base_url(&self) -> &str {
&self.upstream_base_url
}
fn default_upstream_base_url() -> &'static str {
"https://api.openai.com"
}
fn refresh_token<'a>(
&'a self,
refresh_token: &'a str,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<RefreshResult>> + Send + 'a>> {
Box::pin(async move {
let endpoints = if self.refresh_token_url.trim().is_empty() {
DeviceEndpoints::default()
} else {
DeviceEndpoints {
oauth_token_url: self.refresh_token_url.clone(),
..DeviceEndpoints::default()
}
};
let result = refresh_with_refresh_token_for_test(
self.client.clone(),
endpoints,
refresh_token,
)
.await?;
Ok(RefreshResult {
access_token: result.access_token,
refresh_token: Some(result.refresh_token),
id_token: Some(result.id_token),
email: result.email,
account_id: result.account_id,
plan_type: result.plan_type,
expires_at: result.expires_at,
})
})
}
}
#[derive(Debug, Clone)]
pub struct DeviceEndpoints {
pub user_code_url: String,
pub token_poll_url: String,
pub verification_url: String,
pub oauth_token_url: String,
pub redirect_uri: String,
}
impl Default for DeviceEndpoints {
fn default() -> Self {
Self {
user_code_url: DEVICE_USER_CODE_URL.to_string(),
token_poll_url: DEVICE_TOKEN_URL.to_string(),
verification_url: DEVICE_VERIFICATION_URL.to_string(),
oauth_token_url: OAUTH_TOKEN_URL.to_string(),
redirect_uri: DEVICE_REDIRECT_URI.to_string(),
}
}
}
pub fn device_endpoints_from_config(config: &crate::config::AppConfig) -> DeviceEndpoints {
let refresh_token_url = config.codex.refresh_token_url.trim();
if refresh_token_url.is_empty() {
DeviceEndpoints::default()
} else {
DeviceEndpoints {
oauth_token_url: refresh_token_url.to_string(),
..DeviceEndpoints::default()
}
}
}
#[derive(Debug, Serialize)]
struct DeviceCodeRequest<'a> {
client_id: &'a str,
}
#[derive(Debug, Deserialize)]
struct DeviceCodeResponse {
device_auth_id: String,
#[serde(default)]
user_code: String,
#[serde(default)]
usercode: String,
#[serde(default)]
interval: Option<Value>,
}
#[derive(Debug, Serialize)]
struct DeviceTokenPollRequest<'a> {
device_auth_id: &'a str,
user_code: &'a str,
}
#[derive(Debug, Deserialize)]
struct DeviceTokenPollResponse {
authorization_code: String,
code_verifier: String,
}
#[derive(Debug, Serialize)]
struct TokenExchangeRequest<'a> {
grant_type: &'a str,
client_id: &'a str,
code: &'a str,
redirect_uri: &'a str,
code_verifier: &'a str,
}
#[derive(Debug, Deserialize)]
struct TokenExchangeResponse {
access_token: String,
refresh_token: String,
id_token: String,
#[serde(default)]
expires_in: i64,
}
#[derive(Debug, Deserialize)]
struct JwtClaims {
#[serde(default)]
email: String,
#[serde(default)]
exp: i64,
#[serde(default, rename = "https://api.openai.com/auth")]
codex_auth: JwtCodexAuth,
}
#[derive(Debug, Default, Deserialize)]
struct JwtCodexAuth {
#[serde(default)]
chatgpt_account_id: String,
#[serde(default)]
chatgpt_plan_type: String,
}
pub async fn login_with_device_flow() -> Result<DeviceAuthResult> {
login_with_device_flow_for_test(Client::new(), DeviceEndpoints::default()).await
}
pub async fn refresh_with_refresh_token(refresh_token: &str) -> Result<DeviceAuthResult> {
refresh_with_refresh_token_for_test(Client::new(), DeviceEndpoints::default(), refresh_token)
.await
}
pub async fn refresh_with_refresh_token_from_config(
config: &crate::config::AppConfig,
refresh_token: &str,
) -> Result<DeviceAuthResult> {
refresh_with_refresh_token_for_test(
Client::new(),
device_endpoints_from_config(config),
refresh_token,
)
.await
}
pub async fn login_with_device_flow_for_test(
client: Client,
endpoints: DeviceEndpoints,
) -> Result<DeviceAuthResult> {
let code_response = request_device_code(&client, &endpoints).await?;
let user_code = if code_response.user_code.trim().is_empty() {
code_response.usercode.trim().to_string()
} else {
code_response.user_code.trim().to_string()
};
let device_auth_id = code_response.device_auth_id.trim().to_string();
if user_code.is_empty() || device_auth_id.is_empty() {
bail!("codex device flow did not return required fields");
}
let poll_interval = parse_poll_interval(code_response.interval.as_ref());
println!("Starting Codex device authentication...");
println!("Codex device URL: {}", endpoints.verification_url);
println!("Codex device code: {}", user_code);
println!("Approve the login in your browser, then come back here.");
let device_tokens = poll_for_authorization(
&client,
&endpoints,
&device_auth_id,
&user_code,
poll_interval,
)
.await?;
if device_tokens.authorization_code.trim().is_empty()
|| device_tokens.code_verifier.trim().is_empty()
{
bail!("codex device flow token response missing required fields");
}
exchange_authorization_code(&client, &endpoints, &device_tokens).await
}
pub async fn refresh_with_refresh_token_for_test(
client: Client,
endpoints: DeviceEndpoints,
refresh_token: &str,
) -> Result<DeviceAuthResult> {
let trimmed = refresh_token.trim();
if trimmed.is_empty() {
bail!("refresh token is required");
}
let response = client
.post(&endpoints.oauth_token_url)
.header("Accept", "application/json")
.form(&[
("client_id", CLIENT_ID),
("grant_type", "refresh_token"),
("refresh_token", trimmed),
("scope", "openid profile email"),
])
.send()
.await
.context("failed to refresh Codex tokens")?;
let status = response.status();
let body = response
.text()
.await
.context("failed to read Codex refresh response")?;
if !status.is_success() {
bail!(
"codex token refresh failed with status {}: {}",
status,
body
);
}
let payload: TokenExchangeResponse =
serde_json::from_str(&body).context("failed to decode Codex refresh response")?;
build_auth_result(payload)
}
async fn request_device_code(
client: &Client,
endpoints: &DeviceEndpoints,
) -> Result<DeviceCodeResponse> {
let response = client
.post(&endpoints.user_code_url)
.header("Accept", "application/json")
.json(&DeviceCodeRequest {
client_id: CLIENT_ID,
})
.send()
.await
.context("failed to request Codex device code")?;
let status = response.status();
let body = response
.text()
.await
.context("failed to read device code response")?;
if !status.is_success() {
bail!(
"codex device code request failed with status {}: {}",
status,
body
);
}
serde_json::from_str(&body).context("failed to decode Codex device code response")
}
async fn poll_for_authorization(
client: &Client,
endpoints: &DeviceEndpoints,
device_auth_id: &str,
user_code: &str,
poll_interval: Duration,
) -> Result<DeviceTokenPollResponse> {
let deadline = tokio::time::Instant::now() + Duration::from_secs(DEVICE_TIMEOUT_SECS);
loop {
if tokio::time::Instant::now() >= deadline {
bail!("codex device authentication timed out after 15 minutes");
}
let response = client
.post(&endpoints.token_poll_url)
.header("Accept", "application/json")
.json(&DeviceTokenPollRequest {
device_auth_id,
user_code,
})
.send()
.await
.context("failed to poll Codex device token")?;
let status = response.status();
let body = response
.text()
.await
.context("failed to read Codex device poll response")?;
if status.is_success() {
return serde_json::from_str(&body)
.context("failed to decode Codex device token response");
}
if status.as_u16() == 403 || status.as_u16() == 404 {
sleep(poll_interval).await;
continue;
}
let trimmed = body.trim();
bail!(
"codex device token polling failed with status {}: {}",
status,
if trimmed.is_empty() {
"empty response body"
} else {
trimmed
}
);
}
}
async fn exchange_authorization_code(
client: &Client,
endpoints: &DeviceEndpoints,
device_tokens: &DeviceTokenPollResponse,
) -> Result<DeviceAuthResult> {
let response = client
.post(&endpoints.oauth_token_url)
.header("Accept", "application/json")
.form(&TokenExchangeRequest {
grant_type: "authorization_code",
client_id: CLIENT_ID,
code: device_tokens.authorization_code.trim(),
redirect_uri: &endpoints.redirect_uri,
code_verifier: device_tokens.code_verifier.trim(),
})
.send()
.await
.context("failed to exchange device authorization code for tokens")?;
let status = response.status();
let body = response
.text()
.await
.context("failed to read OAuth token response")?;
if !status.is_success() {
bail!(
"codex token exchange failed with status {}: {}",
status,
body
);
}
let payload: TokenExchangeResponse =
serde_json::from_str(&body).context("failed to decode OAuth token response")?;
build_auth_result(payload)
}
fn build_auth_result(payload: TokenExchangeResponse) -> Result<DeviceAuthResult> {
let claims = parse_jwt_claims(&payload.id_token).ok();
let expires_at = claims
.as_ref()
.and_then(|claims| {
if claims.exp > 0 {
Some(claims.exp)
} else {
None
}
})
.map(format_unix_timestamp)
.or_else(|| {
if payload.expires_in > 0 {
Some(format_unix_timestamp(
chrono_like_now_secs() + payload.expires_in,
))
} else {
None
}
});
Ok(DeviceAuthResult {
access_token: payload.access_token,
refresh_token: payload.refresh_token,
id_token: payload.id_token,
email: claims
.as_ref()
.and_then(|claims| trimmed_option(&claims.email)),
account_id: claims
.as_ref()
.and_then(|claims| trimmed_option(&claims.codex_auth.chatgpt_account_id)),
plan_type: claims
.as_ref()
.and_then(|claims| trimmed_option(&claims.codex_auth.chatgpt_plan_type)),
expires_at,
})
}
fn parse_poll_interval(value: Option<&Value>) -> Duration {
let default = Duration::from_secs(DEFAULT_POLL_INTERVAL_SECS);
let Some(value) = value else {
return default;
};
match value {
Value::Number(number) => number.as_u64().map(Duration::from_secs).unwrap_or(default),
Value::String(text) => text
.trim()
.parse::<u64>()
.map(Duration::from_secs)
.unwrap_or(default),
_ => default,
}
}
fn parse_jwt_claims(token: &str) -> Result<JwtClaims> {
let payload = token
.split('.')
.nth(1)
.context("invalid JWT token format")?;
let bytes = URL_SAFE_NO_PAD
.decode(payload)
.context("failed to decode JWT payload")?;
serde_json::from_slice(&bytes).context("failed to parse JWT claims")
}
fn trimmed_option(value: &str) -> Option<String> {
let trimmed = value.trim();
if trimmed.is_empty() {
None
} else {
Some(trimmed.to_string())
}
}
fn chrono_like_now_secs() -> i64 {
use std::time::{SystemTime, UNIX_EPOCH};
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| duration.as_secs() as i64)
.unwrap_or_default()
}
fn format_unix_timestamp(timestamp: i64) -> String {
use std::time::{Duration as StdDuration, UNIX_EPOCH};
let when = UNIX_EPOCH + StdDuration::from_secs(timestamp.max(0) as u64);
let datetime: chrono_like::DateTime = when.into();
datetime.to_rfc3339()
}
mod chrono_like {
use std::time::SystemTime;
pub struct DateTime(SystemTime);
impl From<SystemTime> for DateTime {
fn from(value: SystemTime) -> Self {
Self(value)
}
}
impl DateTime {
pub fn to_rfc3339(&self) -> String {
use std::time::UNIX_EPOCH;
let duration = self
.0
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64;
let secs = duration;
let tm = time_parts(secs);
format!(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second
)
}
}
struct Parts {
year: i64,
month: i64,
day: i64,
hour: i64,
minute: i64,
second: i64,
}
fn time_parts(mut secs: i64) -> Parts {
let second = secs.rem_euclid(60);
secs = (secs - second) / 60;
let minute = secs.rem_euclid(60);
secs = (secs - minute) / 60;
let hour = secs.rem_euclid(24);
let days = (secs - hour) / 24;
civil_from_days(days).with_time(hour, minute, second)
}
struct Civil {
year: i64,
month: i64,
day: i64,
}
impl Civil {
fn with_time(self, hour: i64, minute: i64, second: i64) -> Parts {
Parts {
year: self.year,
month: self.month,
day: self.day,
hour,
minute,
second,
}
}
}
fn civil_from_days(days: i64) -> Civil {
let z = days + 719468;
let era = if z >= 0 { z } else { z - 146096 } / 146097;
let doe = z - era * 146097;
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
let y = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = doy - (153 * mp + 2) / 5 + 1;
let m = mp + if mp < 10 { 3 } else { -9 };
let year = y + if m <= 2 { 1 } else { 0 };
Civil {
year,
month: m,
day: d,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use axum::extract::State;
use axum::response::IntoResponse;
use axum::routing::post;
use axum::{Json, Router};
use serde_json::json;
use tokio::sync::oneshot;
async fn start_listener(app: Router) -> (String, oneshot::Sender<()>) {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = oneshot::channel::<()>();
tokio::spawn(async move {
let server = axum::serve(listener, app).with_graceful_shutdown(async {
let _ = rx.await;
});
let _ = server.await;
});
(format!("http://{}", addr), tx)
}
#[tokio::test]
async fn device_flow_returns_tokens_and_claims() {
async fn user_code() -> impl axum::response::IntoResponse {
Json(json!({
"device_auth_id": "device-auth-id",
"user_code": "ABCD-EFGH",
"interval": 0
}))
}
async fn token_poll(
State(done): State<std::sync::Arc<std::sync::atomic::AtomicBool>>,
) -> impl axum::response::IntoResponse {
if done.swap(true, std::sync::atomic::Ordering::SeqCst) {
(
axum::http::StatusCode::FORBIDDEN,
Json(json!({"pending": true})),
)
.into_response()
} else {
Json(json!({
"authorization_code": "auth-code",
"code_verifier": "verifier",
"code_challenge": "challenge"
}))
.into_response()
}
}
async fn exchange() -> impl axum::response::IntoResponse {
let payload = json!({
"email": "device@example.com",
"exp": 1767225600_i64,
"https://api.openai.com/auth": {
"chatgpt_account_id": "acct_device",
"chatgpt_plan_type": "plus"
}
});
let claims = URL_SAFE_NO_PAD.encode(serde_json::to_vec(&payload).unwrap());
Json(json!({
"access_token": "device-access-token",
"refresh_token": "device-refresh-token",
"id_token": format!("header.{}.sig", claims),
"expires_in": 3600
}))
}
let done = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let user_code_app = Router::new().route("/usercode", post(user_code));
let token_app = Router::new()
.route("/token", post(token_poll))
.with_state(done);
let exchange_app = Router::new().route("/oauth/token", post(exchange));
let (user_code_url, user_shutdown) = start_listener(user_code_app).await;
let (token_url, token_shutdown) = start_listener(token_app).await;
let (oauth_url, oauth_shutdown) = start_listener(exchange_app).await;
let result = login_with_device_flow_for_test(
Client::new(),
DeviceEndpoints {
user_code_url: format!("{user_code_url}/usercode"),
token_poll_url: format!("{token_url}/token"),
verification_url: "https://example.test/device".to_string(),
oauth_token_url: format!("{oauth_url}/oauth/token"),
redirect_uri: "https://example.test/callback".to_string(),
},
)
.await
.unwrap();
assert_eq!(result.access_token, "device-access-token");
assert_eq!(result.refresh_token, "device-refresh-token");
assert_eq!(result.email.as_deref(), Some("device@example.com"));
assert_eq!(result.account_id.as_deref(), Some("acct_device"));
assert_eq!(result.plan_type.as_deref(), Some("plus"));
let _ = user_shutdown.send(());
let _ = token_shutdown.send(());
let _ = oauth_shutdown.send(());
}
#[tokio::test]
async fn refresh_flow_returns_updated_tokens_and_claims() {
async fn refresh_exchange() -> impl axum::response::IntoResponse {
let payload = json!({
"email": "refresh@example.com",
"exp": 1767225600_i64,
"https://api.openai.com/auth": {
"chatgpt_account_id": "acct_refresh",
"chatgpt_plan_type": "pro"
}
});
let claims = URL_SAFE_NO_PAD.encode(serde_json::to_vec(&payload).unwrap());
Json(json!({
"access_token": "refreshed-access-token",
"refresh_token": "refreshed-refresh-token",
"id_token": format!("header.{}.sig", claims),
"expires_in": 3600
}))
}
let refresh_app = Router::new().route("/oauth/token", post(refresh_exchange));
let (refresh_url, refresh_shutdown) = start_listener(refresh_app).await;
let result = refresh_with_refresh_token_for_test(
Client::new(),
DeviceEndpoints {
oauth_token_url: format!("{refresh_url}/oauth/token"),
..DeviceEndpoints::default()
},
"old-refresh-token",
)
.await
.unwrap();
assert_eq!(result.access_token, "refreshed-access-token");
assert_eq!(result.refresh_token, "refreshed-refresh-token");
assert_eq!(result.email.as_deref(), Some("refresh@example.com"));
assert_eq!(result.account_id.as_deref(), Some("acct_refresh"));
assert_eq!(result.plan_type.as_deref(), Some("pro"));
let _ = refresh_shutdown.send(());
}
}
+265
View File
@@ -0,0 +1,265 @@
use std::fs;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AppConfig {
#[serde(default)]
pub server: ServerConfig,
#[serde(default)]
pub state: StateConfig,
#[serde(default)]
pub codex: CodexConfig,
#[serde(default)]
pub claude: ClaudeConfig,
#[serde(default)]
pub providers: ProvidersConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ProvidersConfig {
#[serde(default)]
pub active: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
#[serde(default = "default_bind")]
pub bind: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateConfig {
#[serde(default = "default_state_path")]
pub path: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodexConfig {
#[serde(default = "default_codex_base_url")]
pub upstream_base_url: String,
#[serde(default)]
pub api_key: String,
#[serde(default)]
pub refresh_token_url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeConfig {
#[serde(default = "default_claude_base_url")]
pub upstream_base_url: String,
#[serde(default)]
pub api_key: String,
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
bind: default_bind(),
}
}
}
impl Default for StateConfig {
fn default() -> Self {
Self {
path: default_state_path(),
}
}
}
impl Default for CodexConfig {
fn default() -> Self {
Self {
upstream_base_url: default_codex_base_url(),
api_key: "set-me".to_string(),
refresh_token_url: String::new(),
}
}
}
impl Default for ClaudeConfig {
fn default() -> Self {
Self {
upstream_base_url: default_claude_base_url(),
api_key: String::new(),
}
}
}
impl AppConfig {
pub fn load(path: &Path) -> Result<Self> {
let raw = fs::read_to_string(path)
.with_context(|| format!("failed to read config at {}", path.display()))?;
let config: Self = toml::from_str(&raw)
.with_context(|| format!("failed to parse TOML config at {}", path.display()))?;
config.validate()?;
Ok(config)
}
pub fn write_example(path: &Path, force: bool) -> Result<()> {
if path.exists() && !force {
bail!(
"config already exists at {} (pass --force to overwrite)",
path.display()
);
}
if let Some(parent) = path.parent()
&& !parent.as_os_str().is_empty()
{
fs::create_dir_all(parent).with_context(|| {
format!("failed to create config directory {}", parent.display())
})?;
}
fs::write(path, Self::example_toml())
.with_context(|| format!("failed to write example config to {}", path.display()))?;
Ok(())
}
pub fn validate(&self) -> Result<()> {
if self.server.bind.trim().is_empty() {
bail!("server.bind cannot be empty");
}
if self.codex.upstream_base_url.trim().is_empty() {
bail!("codex.upstream_base_url cannot be empty");
}
if self.state.path.trim().is_empty() {
bail!("state.path cannot be empty");
}
Ok(())
}
pub fn health_url(&self) -> String {
format!("http://{}/healthz", self.server.bind)
}
pub fn config_summary(&self, path: &Path) -> Vec<String> {
let lines = vec![
format!("config: {}", path.display()),
format!("listen: {}", self.server.bind),
format!("state path: {}", self.resolve_state_path(path).display()),
format!(
"active provider: {}",
self.active_provider_label()
),
format!("codex upstream: {}", self.codex.upstream_base_url),
format!(
"codex refresh token endpoint: {}",
if self.codex.refresh_token_url.trim().is_empty() {
"live default".to_string()
} else {
self.codex.refresh_token_url.clone()
}
),
format!(
"codex fallback api key: {}",
if self.codex.api_key.trim().is_empty() {
"missing"
} else {
"configured"
}
),
format!("claude upstream: {}", self.claude.upstream_base_url),
format!(
"claude fallback api key: {}",
if self.claude.api_key.trim().is_empty() {
"missing"
} else {
"configured"
}
),
];
lines
}
pub fn active_provider(&self) -> &str {
self.providers
.active
.as_deref()
.unwrap_or(crate::provider::CODEX_PROVIDER)
}
pub fn active_provider_label(&self) -> String {
let tag = self.active_provider();
match tag {
crate::provider::CODEX_PROVIDER => "codex (default)".to_string(),
crate::provider::CLAUDE_PROVIDER => "claude".to_string(),
other => other.to_string(),
}
}
pub fn example_toml() -> &'static str {
r#"# ProxyPilot Rust replatform config
#
# The rewrite keeps long-lived account state in a separate local file.
[server]
bind = "127.0.0.1:8318"
[state]
path = "proxypilot-rs.state.toml"
[providers]
# active = "codex" # default; change to "claude" or "gemini" when available
[codex]
upstream_base_url = "https://api.openai.com"
api_key = ""
# refresh_token_url = "http://127.0.0.1:18319/oauth/token"
[claude]
upstream_base_url = "https://api.anthropic.com"
api_key = ""
"#
}
pub fn resolve_state_path(&self, config_path: &Path) -> PathBuf {
let raw = PathBuf::from(self.state.path.trim());
if raw.is_absolute() {
raw
} else if let Some(parent) = config_path.parent() {
parent.join(raw)
} else {
raw
}
}
}
pub fn default_config_path() -> PathBuf {
PathBuf::from("proxypilot-rs.toml")
}
fn default_bind() -> String {
"127.0.0.1:8318".to_string()
}
fn default_codex_base_url() -> String {
"https://api.openai.com".to_string()
}
fn default_claude_base_url() -> String {
"https://api.anthropic.com".to_string()
}
fn default_state_path() -> String {
"proxypilot-rs.state.toml".to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_example_config() {
let config: AppConfig = toml::from_str(AppConfig::example_toml()).unwrap();
assert_eq!(config.server.bind, "127.0.0.1:8318");
assert_eq!(config.state.path, "proxypilot-rs.state.toml");
assert_eq!(config.codex.upstream_base_url, "https://api.openai.com");
assert!(config.codex.refresh_token_url.is_empty());
assert_eq!(config.claude.upstream_base_url, "https://api.anthropic.com");
}
}
+11
View File
@@ -0,0 +1,11 @@
pub mod accounts;
pub mod auth_runtime;
pub mod claude;
pub mod cli;
pub mod codex;
pub mod config;
pub mod proxy;
pub mod provider;
pub mod provider_registry;
pub mod state;
pub mod tui;
+121
View File
@@ -0,0 +1,121 @@
use std::path::PathBuf;
use anyhow::Result;
use clap::Parser;
use proxypilot_rs::cli::{AccountCommand, Cli, Command};
use proxypilot_rs::config::{AppConfig, default_config_path};
use tracing_subscriber::EnvFilter;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("proxypilot_rs=info,tower_http=info")),
)
.with_target(false)
.compact()
.init();
let cli = Cli::parse();
match cli.command {
Command::Init { config, force } => {
let path = resolve_config_path(config);
AppConfig::write_example(&path, force)?;
println!("wrote {}", path.display());
}
Command::Run { config } => {
let path = resolve_config_path(config);
let config = AppConfig::load(&path)?;
proxypilot_rs::proxy::run(config, &path).await?;
}
Command::Tui { config } => {
let path = resolve_config_path(config);
let config = AppConfig::load(&path)?;
proxypilot_rs::tui::run(config, &path).await?;
}
Command::Account { command, .. } => match command {
AccountCommand::AddCodex {
shared,
name,
api_key,
activate,
} => {
let path = resolve_config_path(shared.config);
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::add_codex_account(
&config, &path, name, api_key, activate,
)?;
}
AccountCommand::AddClaude {
shared,
name,
api_key,
activate,
} => {
let path = resolve_config_path(shared.config);
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::add_claude_account(
&config, &path, name, api_key, activate,
)?;
}
AccountCommand::List { shared } => {
let path = resolve_config_path(shared.config);
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::list_accounts(&config, &path)?;
}
AccountCommand::ImportCodex {
shared,
file,
name,
activate,
} => {
let path = resolve_config_path(shared.config);
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::import_codex_account(
&config, &path, &file, name, activate,
)?;
}
AccountCommand::LoginCodexDevice {
shared,
name,
activate,
} => {
let path = resolve_config_path(shared.config);
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::login_codex_device(&config, &path, name, activate).await?;
}
AccountCommand::RefreshCodex { shared, name } => {
let path = resolve_config_path(shared.config);
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::refresh_codex_account(&config, &path, name).await?;
}
AccountCommand::RefreshClaude { shared, name } => {
let path = resolve_config_path(shared.config);
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::refresh_claude_account(&config, &path, name).await?;
}
AccountCommand::Activate { shared, name } => {
let path = resolve_config_path(shared.config);
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::activate_account(&config, &path, name)?;
}
AccountCommand::Remove { shared, name } => {
let path = resolve_config_path(shared.config);
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::remove_account(&config, &path, name)?;
}
},
}
Ok(())
}
fn resolve_config_path(path: PathBuf) -> PathBuf {
if path.as_os_str().is_empty() {
default_config_path()
} else {
path
}
}
+139
View File
@@ -0,0 +1,139 @@
use std::future::Future;
use std::pin::Pin;
use anyhow::Result;
use reqwest;
use serde::{Deserialize, Serialize};
use crate::state::AccountEntry;
pub const CODEX_PROVIDER: &str = "codex";
pub const CLAUDE_PROVIDER: &str = "claude";
pub const GEMINI_PROVIDER: &str = "gemini";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct ProviderId(pub &'static str);
impl ProviderId {
pub const CODEX: ProviderId = ProviderId(CODEX_PROVIDER);
pub const CLAUDE: ProviderId = ProviderId(CLAUDE_PROVIDER);
pub const GEMINI: ProviderId = ProviderId(GEMINI_PROVIDER);
pub fn as_str(&self) -> &'static str {
self.0
}
}
impl std::fmt::Display for ProviderId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.0)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthResult {
pub access_token: String,
pub refresh_token: Option<String>,
pub id_token: Option<String>,
pub email: Option<String>,
pub account_id: Option<String>,
pub plan_type: Option<String>,
pub expires_at: Option<String>,
}
impl AuthResult {
pub fn into_account_entry(
self,
name: String,
source: String,
_activate: bool,
) -> AccountEntry {
AccountEntry {
name,
provider: self.access_token_provider_name().to_string(),
api_key: self.access_token,
refresh_token: self.refresh_token,
id_token: self.id_token,
email: self.email,
account_id: self.account_id,
plan_type: self.plan_type,
expires_at: self.expires_at,
source: Some(source),
}
}
fn access_token_provider_name(&self) -> &str {
CODEX_PROVIDER
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefreshResult {
pub access_token: String,
pub refresh_token: Option<String>,
pub id_token: Option<String>,
pub email: Option<String>,
pub account_id: Option<String>,
pub plan_type: Option<String>,
pub expires_at: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelInfo {
pub id: String,
pub owned_by: Option<String>,
}
pub trait Provider: Send + Sync + 'static {
fn id(&self) -> ProviderId;
fn display_name(&self) -> &'static str;
fn provider_tag(&self) -> &'static str {
self.id().as_str()
}
fn upstream_base_url(&self) -> &str;
fn default_upstream_base_url() -> &'static str
where
Self: Sized;
fn refresh_token<'a>(
&'a self,
refresh_token: &'a str,
) -> Pin<Box<dyn Future<Output = Result<RefreshResult>> + Send + 'a>>;
fn models_path(&self) -> &str {
"/v1/models"
}
fn chat_completions_path(&self) -> &str {
"/v1/chat/completions"
}
fn responses_path(&self) -> &str {
"/v1/responses"
}
fn apply_auth_headers(
&self,
request: reqwest::RequestBuilder,
api_key: &str,
) -> reqwest::RequestBuilder {
request.bearer_auth(api_key)
}
}
pub fn provider_from_tag(tag: &str) -> Option<ProviderId> {
match tag {
CODEX_PROVIDER => Some(ProviderId::CODEX),
CLAUDE_PROVIDER => Some(ProviderId::CLAUDE),
GEMINI_PROVIDER => Some(ProviderId::GEMINI),
_ => None,
}
}
pub fn is_known_provider(tag: &str) -> bool {
provider_from_tag(tag).is_some()
}
@@ -0,0 +1,90 @@
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::claude::ClaudeProvider;
use crate::codex::CodexProvider;
use crate::config::AppConfig;
use crate::provider::{Provider, ProviderId};
use crate::state::AccountState;
pub struct ProviderRegistry {
providers: Vec<Arc<dyn Provider>>,
}
impl ProviderRegistry {
pub fn new(config: &AppConfig) -> Self {
let providers: Vec<Arc<dyn Provider>> = vec![
Arc::new(CodexProvider::from_config(config)),
Arc::new(ClaudeProvider::from_config(config)),
];
Self { providers }
}
pub fn active_provider(&self, config: &AppConfig) -> Arc<dyn Provider> {
let tag = config.active_provider();
self.providers
.iter()
.find(|p| p.provider_tag() == tag)
.cloned()
.unwrap_or_else(|| {
self.providers
.first()
.cloned()
.expect("at least one provider must be registered")
})
}
pub fn get(&self, id: ProviderId) -> Option<Arc<dyn Provider>> {
self.providers
.iter()
.find(|p| p.id() == id)
.cloned()
}
}
#[derive(Clone)]
pub struct ResolvedProvider {
pub provider: Arc<dyn Provider>,
pub api_key: Option<String>,
pub refresh_token: Option<String>,
pub account_name: Option<String>,
}
pub async fn resolve_active_provider(
config: &AppConfig,
registry: &ProviderRegistry,
accounts: &RwLock<AccountState>,
) -> ResolvedProvider {
let provider = registry.active_provider(config);
let tag = provider.provider_tag();
let (api_key, refresh_token, account_name) = {
let state = accounts.read().await;
let active = state.active_account_for_provider(tag);
let key = active
.as_ref()
.map(|a| a.api_key.clone())
.or_else(|| match tag {
crate::provider::CODEX_PROVIDER => {
let fallback = config.codex.api_key.trim();
if fallback.is_empty() { None } else { Some(fallback.to_string()) }
}
crate::provider::CLAUDE_PROVIDER => {
let fallback = config.claude.api_key.trim();
if fallback.is_empty() { None } else { Some(fallback.to_string()) }
}
_ => None,
});
let refresh = active.as_ref().and_then(|a| a.refresh_token.clone());
let name = active.as_ref().map(|a| a.name.clone());
(key, refresh, name)
};
ResolvedProvider {
provider,
api_key,
refresh_token,
account_name,
}
}
File diff suppressed because it is too large Load Diff
+543
View File
@@ -0,0 +1,543 @@
use std::fs;
use std::path::Path;
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use crate::config::AppConfig;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AccountState {
#[serde(default)]
pub active_account: Option<String>,
#[serde(default)]
pub accounts: Vec<AccountEntry>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountEntry {
pub name: String,
pub provider: String,
pub api_key: String,
#[serde(default)]
pub refresh_token: Option<String>,
#[serde(default)]
pub id_token: Option<String>,
#[serde(default)]
pub email: Option<String>,
#[serde(default)]
pub account_id: Option<String>,
#[serde(default)]
pub plan_type: Option<String>,
#[serde(default)]
pub expires_at: Option<String>,
#[serde(default)]
pub source: Option<String>,
}
#[derive(Debug, Clone)]
pub struct ActiveCodexAccount {
pub name: String,
pub api_key: String,
pub refresh_token: Option<String>,
pub id_token: Option<String>,
pub email: Option<String>,
pub account_id: Option<String>,
pub plan_type: Option<String>,
pub expires_at: Option<String>,
}
impl AccountState {
pub fn load_or_default(path: &Path) -> Result<Self> {
if !path.exists() {
return Ok(Self::default());
}
let raw = fs::read_to_string(path)
.with_context(|| format!("failed to read state file {}", path.display()))?;
let state: Self = toml::from_str(&raw)
.with_context(|| format!("failed to parse state file {}", path.display()))?;
Ok(state)
}
pub fn save(&self, path: &Path) -> Result<()> {
if let Some(parent) = path.parent()
&& !parent.as_os_str().is_empty()
{
fs::create_dir_all(parent).with_context(|| {
format!("failed to create state directory {}", parent.display())
})?;
}
let raw = toml::to_string_pretty(self).context("failed to serialize account state")?;
fs::write(path, raw)
.with_context(|| format!("failed to write state file {}", path.display()))?;
Ok(())
}
pub fn add_or_replace_manual_account(
&mut self,
provider: &str,
name: String,
api_key: String,
activate: bool,
) -> Result<()> {
let trimmed_name = name.trim();
let trimmed_key = api_key.trim();
if trimmed_name.is_empty() {
bail!("account name cannot be empty");
}
if trimmed_key.is_empty() {
bail!("api key cannot be empty");
}
self.accounts.retain(|account| account.name != trimmed_name);
self.accounts.push(AccountEntry {
name: trimmed_name.to_string(),
provider: provider.to_string(),
api_key: trimmed_key.to_string(),
refresh_token: None,
id_token: None,
email: None,
account_id: None,
plan_type: None,
expires_at: None,
source: Some("manual".to_string()),
});
if activate || self.active_account.is_none() {
self.active_account = Some(trimmed_name.to_string());
}
self.accounts.sort_by(|a, b| a.name.cmp(&b.name));
Ok(())
}
pub fn add_or_replace_codex_account(
&mut self,
name: String,
api_key: String,
activate: bool,
) -> Result<()> {
self.add_or_replace_manual_account(crate::provider::CODEX_PROVIDER, name, api_key, activate)
}
pub fn activate(&mut self, name: &str) -> Result<()> {
let trimmed = name.trim();
if trimmed.is_empty() {
bail!("account name cannot be empty");
}
if self.accounts.iter().any(|account| account.name == trimmed) {
self.active_account = Some(trimmed.to_string());
Ok(())
} else {
bail!("no saved account named {}", trimmed)
}
}
pub fn remove_account(&mut self, name: &str) -> Result<()> {
let trimmed = name.trim();
if trimmed.is_empty() {
bail!("account name cannot be empty");
}
let original_len = self.accounts.len();
self.accounts.retain(|account| account.name != trimmed);
if self.accounts.len() == original_len {
bail!("no saved account named {}", trimmed);
}
if self.active_account.as_deref() == Some(trimmed) {
self.active_account = self
.accounts
.first()
.map(|account| account.name.clone());
}
Ok(())
}
pub fn active_account_for_provider(&self, provider: &str) -> Option<ActiveCodexAccount> {
let active_name = self.active_account.as_deref()?;
self.accounts
.iter()
.find(|account| account.provider == provider && account.name == active_name)
.map(|account| ActiveCodexAccount {
name: account.name.clone(),
api_key: account.api_key.clone(),
refresh_token: account.refresh_token.clone(),
id_token: account.id_token.clone(),
email: account.email.clone(),
account_id: account.account_id.clone(),
plan_type: account.plan_type.clone(),
expires_at: account.expires_at.clone(),
})
}
pub fn account_by_name_and_provider(&self, name: &str, provider: &str) -> Option<ActiveCodexAccount> {
let trimmed = name.trim();
self.accounts
.iter()
.find(|account| account.provider == provider && account.name == trimmed)
.map(|account| ActiveCodexAccount {
name: account.name.clone(),
api_key: account.api_key.clone(),
refresh_token: account.refresh_token.clone(),
id_token: account.id_token.clone(),
email: account.email.clone(),
account_id: account.account_id.clone(),
plan_type: account.plan_type.clone(),
expires_at: account.expires_at.clone(),
})
}
pub fn runtime_usable_account_count_for_provider(&self, provider: &str) -> usize {
self.accounts
.iter()
.filter(|account| account.provider == provider && !account.api_key.trim().is_empty())
.count()
}
pub fn update_account_tokens_for_provider(
&mut self,
account_name: &str,
provider: &str,
access_token: String,
refresh_token: Option<String>,
id_token: Option<String>,
email: Option<String>,
account_id: Option<String>,
plan_type: Option<String>,
expires_at: Option<String>,
) -> Result<()> {
let trimmed = account_name.trim();
let entry = self
.accounts
.iter_mut()
.find(|account| account.provider == provider && account.name == trimmed)
.ok_or_else(|| anyhow::anyhow!("no saved {} account named {}", provider, trimmed))?;
entry.api_key = access_token;
if refresh_token.is_some() { entry.refresh_token = refresh_token; }
if id_token.is_some() { entry.id_token = id_token; }
if email.is_some() { entry.email = email; }
if account_id.is_some() { entry.account_id = account_id; }
if plan_type.is_some() { entry.plan_type = plan_type; }
if expires_at.is_some() { entry.expires_at = expires_at; }
Ok(())
}
// Legacy codex-specific wrappers (delegate to provider-agnostic methods)
pub fn active_codex_account(&self) -> Option<ActiveCodexAccount> {
self.active_account_for_provider(crate::provider::CODEX_PROVIDER)
}
pub fn runtime_usable_codex_account_count(&self) -> usize {
self.runtime_usable_account_count_for_provider(crate::provider::CODEX_PROVIDER)
}
pub fn codex_account_by_name(&self, name: &str) -> Option<ActiveCodexAccount> {
self.account_by_name_and_provider(name, crate::provider::CODEX_PROVIDER)
}
pub fn effective_codex_api_key(&self, config: &AppConfig) -> Option<String> {
self.active_codex_account()
.map(|account| account.api_key)
.or_else(|| {
let fallback = config.codex.api_key.trim();
if fallback.is_empty() {
None
} else {
Some(fallback.to_string())
}
})
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct ImportedCodexAuth {
#[serde(default)]
pub access_token: String,
#[serde(default)]
pub refresh_token: String,
#[serde(default)]
pub email: String,
#[serde(default)]
pub account_id: String,
#[serde(default, alias = "expired")]
pub expires_at: String,
#[serde(default, rename = "type")]
pub provider_type: String,
}
impl AccountState {
pub fn add_imported_codex_account(
&mut self,
name: String,
imported: ImportedCodexAuth,
source: String,
activate: bool,
) -> Result<()> {
let trimmed_name = name.trim();
if trimmed_name.is_empty() {
bail!("account name cannot be empty");
}
if imported.access_token.trim().is_empty() {
bail!("imported Codex auth file is missing access_token");
}
self.accounts.retain(|account| account.name != trimmed_name);
self.accounts.push(AccountEntry {
name: trimmed_name.to_string(),
provider: if imported.provider_type.trim().is_empty() {
"codex".to_string()
} else {
imported.provider_type.trim().to_string()
},
api_key: imported.access_token.trim().to_string(),
refresh_token: optional_trimmed(imported.refresh_token),
id_token: None,
email: optional_trimmed(imported.email),
account_id: optional_trimmed(imported.account_id),
plan_type: None,
expires_at: optional_trimmed(imported.expires_at),
source: Some(source),
});
if activate || self.active_account.is_none() {
self.active_account = Some(trimmed_name.to_string());
}
self.accounts.sort_by(|a, b| a.name.cmp(&b.name));
Ok(())
}
}
impl AccountState {
pub fn add_device_codex_account(
&mut self,
name: String,
result: crate::codex::DeviceAuthResult,
activate: bool,
) -> Result<()> {
let trimmed_name = name.trim();
if trimmed_name.is_empty() {
bail!("account name cannot be empty");
}
if result.access_token.trim().is_empty() {
bail!("device auth result missing access token");
}
self.accounts.retain(|account| account.name != trimmed_name);
self.accounts.push(AccountEntry {
name: trimmed_name.to_string(),
provider: "codex".to_string(),
api_key: result.access_token,
refresh_token: optional_trimmed(result.refresh_token),
id_token: optional_trimmed(result.id_token),
email: result.email,
account_id: result.account_id,
plan_type: result.plan_type,
expires_at: result.expires_at,
source: Some("device-login".to_string()),
});
if activate || self.active_account.is_none() {
self.active_account = Some(trimmed_name.to_string());
}
self.accounts.sort_by(|a, b| a.name.cmp(&b.name));
Ok(())
}
pub fn update_codex_account_tokens(
&mut self,
account_name: &str,
result: crate::codex::DeviceAuthResult,
) -> Result<()> {
self.update_account_tokens_for_provider(
account_name,
crate::provider::CODEX_PROVIDER,
result.access_token,
optional_trimmed(result.refresh_token),
optional_trimmed(result.id_token),
result.email,
result.account_id,
result.plan_type,
result.expires_at,
)
}
}
fn optional_trimmed(value: String) -> Option<String> {
let trimmed = value.trim();
if trimmed.is_empty() {
None
} else {
Some(trimmed.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn active_account_overrides_config_fallback() {
let mut state = AccountState::default();
state
.add_or_replace_codex_account("primary".to_string(), "state-key".to_string(), true)
.unwrap();
let config = AppConfig::default();
assert_eq!(
state.effective_codex_api_key(&config).as_deref(),
Some("state-key")
);
}
#[test]
fn config_fallback_is_used_when_no_account_exists() {
let config = AppConfig {
codex: crate::config::CodexConfig {
upstream_base_url: "https://api.openai.com".to_string(),
api_key: "fallback-key".to_string(),
refresh_token_url: String::new(),
},
..AppConfig::default()
};
let state = AccountState::default();
assert_eq!(
state.effective_codex_api_key(&config).as_deref(),
Some("fallback-key")
);
}
#[test]
fn imported_codex_account_carries_metadata() {
let mut state = AccountState::default();
state
.add_imported_codex_account(
"imported".to_string(),
ImportedCodexAuth {
access_token: "imported-token".to_string(),
refresh_token: "refresh".to_string(),
email: "dev@example.com".to_string(),
account_id: "acct_123".to_string(),
expires_at: "2026-04-06T00:00:00Z".to_string(),
provider_type: "codex".to_string(),
},
"file".to_string(),
true,
)
.unwrap();
let account = state
.accounts
.iter()
.find(|entry| entry.name == "imported")
.unwrap();
assert_eq!(account.email.as_deref(), Some("dev@example.com"));
assert_eq!(account.account_id.as_deref(), Some("acct_123"));
assert_eq!(account.plan_type.as_deref(), None);
assert_eq!(account.expires_at.as_deref(), Some("2026-04-06T00:00:00Z"));
assert_eq!(account.source.as_deref(), Some("file"));
}
#[test]
fn removing_active_account_promotes_next_saved_account() {
let mut state = AccountState::default();
state
.add_or_replace_codex_account("primary".to_string(), "key-1".to_string(), true)
.unwrap();
state
.add_or_replace_codex_account("backup".to_string(), "key-2".to_string(), false)
.unwrap();
state.remove_account("primary").unwrap();
assert_eq!(state.active_account.as_deref(), Some("backup"));
assert_eq!(state.accounts.len(), 1);
assert_eq!(state.accounts[0].name, "backup");
}
#[test]
fn device_account_carries_plan_type() {
let mut state = AccountState::default();
state
.add_device_codex_account(
"device".to_string(),
crate::codex::DeviceAuthResult {
access_token: "access".to_string(),
refresh_token: "refresh".to_string(),
id_token: "id".to_string(),
email: Some("dev@example.com".to_string()),
account_id: Some("acct_123".to_string()),
plan_type: Some("pro".to_string()),
expires_at: Some("2026-04-06T00:00:00Z".to_string()),
},
true,
)
.unwrap();
let account = state.active_codex_account().unwrap();
assert_eq!(account.plan_type.as_deref(), Some("pro"));
}
#[test]
#[test]
fn manual_claude_account_uses_provider_generic_path() {
let mut state = AccountState::default();
state
.add_or_replace_manual_account(crate::provider::CLAUDE_PROVIDER, "claude-main".to_string(), "claude-key".to_string(), true)
.unwrap();
let account = state
.active_account_for_provider(crate::provider::CLAUDE_PROVIDER)
.unwrap();
assert_eq!(account.name, "claude-main");
assert_eq!(account.api_key, "claude-key");
assert_eq!(state.runtime_usable_account_count_for_provider(crate::provider::CLAUDE_PROVIDER), 1);
}
fn runtime_usable_codex_account_count_ignores_mixed_provider_and_empty_key_entries() {
let mut state = AccountState::default();
state.accounts.push(AccountEntry {
name: "primary".to_string(),
provider: "codex".to_string(),
api_key: "state-key".to_string(),
refresh_token: None,
id_token: None,
email: None,
account_id: None,
plan_type: None,
expires_at: None,
source: Some("manual".to_string()),
});
state.accounts.push(AccountEntry {
name: "mixed".to_string(),
provider: "anthropic".to_string(),
api_key: "other-key".to_string(),
refresh_token: None,
id_token: None,
email: None,
account_id: None,
plan_type: None,
expires_at: None,
source: Some("manual".to_string()),
});
state.accounts.push(AccountEntry {
name: "broken".to_string(),
provider: "codex".to_string(),
api_key: " ".to_string(),
refresh_token: None,
id_token: None,
email: None,
account_id: None,
plan_type: None,
expires_at: None,
source: Some("manual".to_string()),
});
assert_eq!(state.runtime_usable_codex_account_count(), 1);
}
}
File diff suppressed because it is too large Load Diff
+53 -1
View File
@@ -6,6 +6,7 @@ package handlers
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
@@ -492,6 +493,7 @@ func (h *BaseAPIHandler) ExecuteWithAuthManager(ctx context.Context, handlerType
opts.Metadata = reqMeta
resp, err := h.AuthManager.Execute(ctx, providers, req, opts)
if err != nil {
err = enrichAuthSelectionError(err, providers, normalizedModel)
status := http.StatusInternalServerError
if se, ok := err.(interface{ StatusCode() int }); ok && se != nil {
if code := se.StatusCode(); code > 0 {
@@ -538,6 +540,7 @@ func (h *BaseAPIHandler) ExecuteCountWithAuthManager(ctx context.Context, handle
opts.Metadata = reqMeta
resp, err := h.AuthManager.ExecuteCount(ctx, providers, req, opts)
if err != nil {
err = enrichAuthSelectionError(err, providers, normalizedModel)
status := http.StatusInternalServerError
if se, ok := err.(interface{ StatusCode() int }); ok && se != nil {
if code := se.StatusCode(); code > 0 {
@@ -588,6 +591,7 @@ func (h *BaseAPIHandler) ExecuteStreamWithAuthManager(ctx context.Context, handl
opts.Metadata = reqMeta
streamResult, err := h.AuthManager.ExecuteStream(ctx, providers, req, opts)
if err != nil {
err = enrichAuthSelectionError(err, providers, normalizedModel)
errChan := make(chan *interfaces.ErrorMessage, 1)
status := http.StatusInternalServerError
if se, ok := err.(interface{ StatusCode() int }); ok && se != nil {
@@ -697,7 +701,7 @@ func (h *BaseAPIHandler) ExecuteStreamWithAuthManager(ctx context.Context, handl
chunks = retryResult.Chunks
continue outer
}
streamErr = retryErr
streamErr = enrichAuthSelectionError(retryErr, providers, normalizedModel)
}
}
@@ -840,6 +844,54 @@ func replaceHeader(dst http.Header, src http.Header) {
}
}
func enrichAuthSelectionError(err error, providers []string, model string) error {
if err == nil {
return nil
}
var authErr *coreauth.Error
if !errors.As(err, &authErr) || authErr == nil {
return err
}
code := strings.TrimSpace(authErr.Code)
if code != "auth_not_found" && code != "auth_unavailable" {
return err
}
providerText := strings.Join(providers, ",")
if providerText == "" {
providerText = "unknown"
}
modelText := strings.TrimSpace(model)
if modelText == "" {
modelText = "unknown"
}
baseMessage := strings.TrimSpace(authErr.Message)
if baseMessage == "" {
baseMessage = "no auth available"
}
detail := fmt.Sprintf("%s (providers=%s, model=%s)", baseMessage, providerText, modelText)
// Clarify the most common alias confusion between Anthropic route names and internal provider keys.
if strings.Contains(","+providerText+",", ",claude,") {
detail += "; check Claude auth/key session and cooldown state via /v0/management/auth-files"
}
status := authErr.HTTPStatus
if status <= 0 {
status = http.StatusServiceUnavailable
}
return &coreauth.Error{
Code: authErr.Code,
Message: detail,
Retryable: authErr.Retryable,
HTTPStatus: status,
}
}
// WriteErrorResponse writes an error message to the response writer using the HTTP status embedded in the message.
func (h *BaseAPIHandler) WriteErrorResponse(c *gin.Context, msg *interfaces.ErrorMessage) {
status := http.StatusInternalServerError
@@ -5,10 +5,12 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
)
@@ -66,3 +68,46 @@ func TestWriteErrorResponse_AddonHeadersEnabled(t *testing.T) {
t.Fatalf("X-Request-Id = %#v, want %#v", got, []string{"new-1", "new-2"})
}
}
func TestEnrichAuthSelectionError_DefaultsTo503WithContext(t *testing.T) {
in := &coreauth.Error{Code: "auth_not_found", Message: "no auth available"}
out := enrichAuthSelectionError(in, []string{"claude"}, "claude-sonnet-4-6")
var got *coreauth.Error
if !errors.As(out, &got) || got == nil {
t.Fatalf("expected coreauth.Error, got %T", out)
}
if got.StatusCode() != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want %d", got.StatusCode(), http.StatusServiceUnavailable)
}
if !strings.Contains(got.Message, "providers=claude") {
t.Fatalf("message missing provider context: %q", got.Message)
}
if !strings.Contains(got.Message, "model=claude-sonnet-4-6") {
t.Fatalf("message missing model context: %q", got.Message)
}
if !strings.Contains(got.Message, "/v0/management/auth-files") {
t.Fatalf("message missing management hint: %q", got.Message)
}
}
func TestEnrichAuthSelectionError_PreservesExplicitStatus(t *testing.T) {
in := &coreauth.Error{Code: "auth_unavailable", Message: "no auth available", HTTPStatus: http.StatusTooManyRequests}
out := enrichAuthSelectionError(in, []string{"gemini"}, "gemini-2.5-pro")
var got *coreauth.Error
if !errors.As(out, &got) || got == nil {
t.Fatalf("expected coreauth.Error, got %T", out)
}
if got.StatusCode() != http.StatusTooManyRequests {
t.Fatalf("status = %d, want %d", got.StatusCode(), http.StatusTooManyRequests)
}
}
func TestEnrichAuthSelectionError_IgnoresOtherErrors(t *testing.T) {
in := errors.New("boom")
out := enrichAuthSelectionError(in, []string{"claude"}, "claude-sonnet-4-6")
if out != in {
t.Fatalf("expected original error to be returned unchanged")
}
}
@@ -2,10 +2,13 @@ package handlers
import (
"context"
"errors"
"net/http"
"strings"
"sync"
"testing"
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
coreexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
@@ -463,6 +466,76 @@ func TestExecuteStreamWithAuthManager_DoesNotRetryAfterFirstByte(t *testing.T) {
}
}
func TestExecuteStreamWithAuthManager_EnrichesBootstrapRetryAuthUnavailableError(t *testing.T) {
executor := &failOnceStreamExecutor{}
manager := coreauth.NewManager(nil, nil, nil)
manager.RegisterExecutor(executor)
auth1 := &coreauth.Auth{
ID: "auth1",
Provider: "codex",
Status: coreauth.StatusActive,
Metadata: map[string]any{"email": "test1@example.com"},
}
if _, err := manager.Register(context.Background(), auth1); err != nil {
t.Fatalf("manager.Register(auth1): %v", err)
}
registry.GetGlobalRegistry().RegisterClient(auth1.ID, auth1.Provider, []*registry.ModelInfo{{ID: "test-model"}})
t.Cleanup(func() {
registry.GetGlobalRegistry().UnregisterClient(auth1.ID)
})
handler := NewBaseAPIHandlers(&sdkconfig.SDKConfig{
Streaming: sdkconfig.StreamingConfig{
BootstrapRetries: 1,
},
}, manager)
dataChan, _, errChan := handler.ExecuteStreamWithAuthManager(context.Background(), "openai", "test-model", []byte(`{"model":"test-model"}`), "")
if dataChan == nil || errChan == nil {
t.Fatalf("expected non-nil channels")
}
var got []byte
for chunk := range dataChan {
got = append(got, chunk...)
}
if len(got) != 0 {
t.Fatalf("expected empty payload, got %q", string(got))
}
var gotErr *interfaces.ErrorMessage
for msg := range errChan {
if msg != nil {
gotErr = msg
}
}
if gotErr == nil {
t.Fatalf("expected terminal error")
}
if gotErr.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want %d", gotErr.StatusCode, http.StatusServiceUnavailable)
}
var authErr *coreauth.Error
if !errors.As(gotErr.Error, &authErr) || authErr == nil {
t.Fatalf("expected coreauth.Error, got %T", gotErr.Error)
}
if authErr.Code != "auth_unavailable" {
t.Fatalf("code = %q, want %q", authErr.Code, "auth_unavailable")
}
if !strings.Contains(authErr.Message, "providers=codex") {
t.Fatalf("message missing provider context: %q", authErr.Message)
}
if !strings.Contains(authErr.Message, "model=test-model") {
t.Fatalf("message missing model context: %q", authErr.Message)
}
if executor.Calls() != 1 {
t.Fatalf("expected exactly one upstream call before retry path selection failure, got %d", executor.Calls())
}
}
func TestExecuteStreamWithAuthManager_PinnedAuthKeepsSameUpstream(t *testing.T) {
executor := &authAwareStreamExecutor{}
manager := coreauth.NewManager(nil, nil, nil)
@@ -379,7 +379,7 @@ func shouldReplaceWebsocketTranscript(rawJSON []byte, nextInput gjson.Result) bo
for _, item := range nextInput.Array() {
switch strings.TrimSpace(item.Get("type").String()) {
case "function_call":
case "function_call", "custom_tool_call":
return true
case "message":
role := strings.TrimSpace(item.Get("role").String())
@@ -431,7 +431,7 @@ func dedupeFunctionCallsByCallID(rawArray string) (string, error) {
continue
}
itemType := strings.TrimSpace(gjson.GetBytes(item, "type").String())
if itemType == "function_call" {
if isResponsesToolCallType(itemType) {
callID := strings.TrimSpace(gjson.GetBytes(item, "call_id").String())
if callID != "" {
if _, ok := seenCallIDs[callID]; ok {
@@ -520,6 +520,92 @@ func TestRepairResponsesWebsocketToolCallsDropsOrphanOutputWhenCallMissing(t *te
}
}
func TestRepairResponsesWebsocketToolCallsInsertsCachedCustomToolOutput(t *testing.T) {
cache := newWebsocketToolOutputCache(time.Minute, 10)
sessionKey := "session-1"
cacheWarm := []byte(`{"previous_response_id":"resp-1","input":[{"type":"custom_tool_call_output","call_id":"call-1","output":"ok"}]}`)
warmed := repairResponsesWebsocketToolCallsWithCache(cache, sessionKey, cacheWarm)
if gjson.GetBytes(warmed, "input.0.call_id").String() != "call-1" {
t.Fatalf("expected warmup output to remain")
}
raw := []byte(`{"input":[{"type":"custom_tool_call","call_id":"call-1","name":"apply_patch"},{"type":"message","id":"msg-1"}]}`)
repaired := repairResponsesWebsocketToolCallsWithCache(cache, sessionKey, raw)
input := gjson.GetBytes(repaired, "input").Array()
if len(input) != 3 {
t.Fatalf("repaired input len = %d, want 3", len(input))
}
if input[0].Get("type").String() != "custom_tool_call" || input[0].Get("call_id").String() != "call-1" {
t.Fatalf("unexpected first item: %s", input[0].Raw)
}
if input[1].Get("type").String() != "custom_tool_call_output" || input[1].Get("call_id").String() != "call-1" {
t.Fatalf("missing inserted output: %s", input[1].Raw)
}
if input[2].Get("type").String() != "message" || input[2].Get("id").String() != "msg-1" {
t.Fatalf("unexpected trailing item: %s", input[2].Raw)
}
}
func TestRepairResponsesWebsocketToolCallsDropsOrphanCustomToolCall(t *testing.T) {
cache := newWebsocketToolOutputCache(time.Minute, 10)
sessionKey := "session-1"
raw := []byte(`{"input":[{"type":"custom_tool_call","call_id":"call-1","name":"apply_patch"},{"type":"message","id":"msg-1"}]}`)
repaired := repairResponsesWebsocketToolCallsWithCache(cache, sessionKey, raw)
input := gjson.GetBytes(repaired, "input").Array()
if len(input) != 1 {
t.Fatalf("repaired input len = %d, want 1", len(input))
}
if input[0].Get("type").String() != "message" || input[0].Get("id").String() != "msg-1" {
t.Fatalf("unexpected remaining item: %s", input[0].Raw)
}
}
func TestRepairResponsesWebsocketToolCallsInsertsCachedCustomToolCallForOrphanOutput(t *testing.T) {
outputCache := newWebsocketToolOutputCache(time.Minute, 10)
callCache := newWebsocketToolOutputCache(time.Minute, 10)
sessionKey := "session-1"
callCache.record(sessionKey, "call-1", []byte(`{"type":"custom_tool_call","call_id":"call-1","name":"apply_patch"}`))
raw := []byte(`{"input":[{"type":"custom_tool_call_output","call_id":"call-1","output":"ok"},{"type":"message","id":"msg-1"}]}`)
repaired := repairResponsesWebsocketToolCallsWithCaches(outputCache, callCache, sessionKey, raw)
input := gjson.GetBytes(repaired, "input").Array()
if len(input) != 3 {
t.Fatalf("repaired input len = %d, want 3", len(input))
}
if input[0].Get("type").String() != "custom_tool_call" || input[0].Get("call_id").String() != "call-1" {
t.Fatalf("missing inserted call: %s", input[0].Raw)
}
if input[1].Get("type").String() != "custom_tool_call_output" || input[1].Get("call_id").String() != "call-1" {
t.Fatalf("unexpected output item: %s", input[1].Raw)
}
if input[2].Get("type").String() != "message" || input[2].Get("id").String() != "msg-1" {
t.Fatalf("unexpected trailing item: %s", input[2].Raw)
}
}
func TestRepairResponsesWebsocketToolCallsDropsOrphanCustomToolOutputWhenCallMissing(t *testing.T) {
outputCache := newWebsocketToolOutputCache(time.Minute, 10)
callCache := newWebsocketToolOutputCache(time.Minute, 10)
sessionKey := "session-1"
raw := []byte(`{"input":[{"type":"custom_tool_call_output","call_id":"call-1","output":"ok"},{"type":"message","id":"msg-1"}]}`)
repaired := repairResponsesWebsocketToolCallsWithCaches(outputCache, callCache, sessionKey, raw)
input := gjson.GetBytes(repaired, "input").Array()
if len(input) != 1 {
t.Fatalf("repaired input len = %d, want 1", len(input))
}
if input[0].Get("type").String() != "message" || input[0].Get("id").String() != "msg-1" {
t.Fatalf("unexpected remaining item: %s", input[0].Raw)
}
}
func TestRecordResponsesWebsocketToolCallsFromPayloadWithCache(t *testing.T) {
cache := newWebsocketToolOutputCache(time.Minute, 10)
sessionKey := "session-1"
@@ -536,6 +622,38 @@ func TestRecordResponsesWebsocketToolCallsFromPayloadWithCache(t *testing.T) {
}
}
func TestRecordResponsesWebsocketCustomToolCallsFromCompletedPayloadWithCache(t *testing.T) {
cache := newWebsocketToolOutputCache(time.Minute, 10)
sessionKey := "session-1"
payload := []byte(`{"type":"response.completed","response":{"id":"resp-1","output":[{"type":"custom_tool_call","id":"ctc-1","call_id":"call-1","name":"apply_patch","input":"*** Begin Patch"}]}}`)
recordResponsesWebsocketToolCallsFromPayloadWithCache(cache, sessionKey, payload)
cached, ok := cache.get(sessionKey, "call-1")
if !ok {
t.Fatalf("expected cached custom tool call")
}
if gjson.GetBytes(cached, "type").String() != "custom_tool_call" || gjson.GetBytes(cached, "call_id").String() != "call-1" {
t.Fatalf("unexpected cached custom tool call: %s", cached)
}
}
func TestRecordResponsesWebsocketCustomToolCallsFromOutputItemDoneWithCache(t *testing.T) {
cache := newWebsocketToolOutputCache(time.Minute, 10)
sessionKey := "session-1"
payload := []byte(`{"type":"response.output_item.done","item":{"type":"custom_tool_call","id":"ctc-1","call_id":"call-1","name":"apply_patch","input":"*** Begin Patch"}}`)
recordResponsesWebsocketToolCallsFromPayloadWithCache(cache, sessionKey, payload)
cached, ok := cache.get(sessionKey, "call-1")
if !ok {
t.Fatalf("expected cached custom tool call")
}
if gjson.GetBytes(cached, "type").String() != "custom_tool_call" || gjson.GetBytes(cached, "call_id").String() != "call-1" {
t.Fatalf("unexpected cached custom tool call: %s", cached)
}
}
func TestForwardResponsesWebsocketPreservesCompletedEvent(t *testing.T) {
gin.SetMode(gin.TestMode)
@@ -1023,6 +1141,161 @@ func TestNormalizeResponsesWebsocketRequestDropsDuplicateFunctionCallsByCallID(t
}
}
func TestNormalizeResponsesWebsocketRequestTreatsCustomToolTranscriptReplacementAsReset(t *testing.T) {
lastRequest := []byte(`{"model":"test-model","stream":true,"input":[{"type":"message","id":"msg-1"},{"type":"custom_tool_call","id":"ctc-1","call_id":"call-1","name":"apply_patch"},{"type":"custom_tool_call_output","id":"tool-out-1","call_id":"call-1"},{"type":"message","id":"assistant-1","role":"assistant"}]}`)
lastResponseOutput := []byte(`[
{"type":"message","id":"assistant-1","role":"assistant"}
]`)
raw := []byte(`{"type":"response.create","input":[{"type":"custom_tool_call","id":"ctc-compact","call_id":"call-1","name":"apply_patch"},{"type":"custom_tool_call_output","id":"tool-out-compact","call_id":"call-1"},{"type":"message","id":"msg-2"}]}`)
normalized, next, errMsg := normalizeResponsesWebsocketRequest(raw, lastRequest, lastResponseOutput)
if errMsg != nil {
t.Fatalf("unexpected error: %v", errMsg.Error)
}
if gjson.GetBytes(normalized, "previous_response_id").Exists() {
t.Fatalf("previous_response_id must not exist in transcript replacement mode")
}
items := gjson.GetBytes(normalized, "input").Array()
if len(items) != 3 {
t.Fatalf("replacement input len = %d, want 3: %s", len(items), normalized)
}
if items[0].Get("id").String() != "ctc-compact" ||
items[1].Get("id").String() != "tool-out-compact" ||
items[2].Get("id").String() != "msg-2" {
t.Fatalf("replacement transcript was not preserved as-is: %s", normalized)
}
if !bytes.Equal(next, normalized) {
t.Fatalf("next request snapshot should match replacement request")
}
}
func TestNormalizeResponsesWebsocketRequestDropsDuplicateCustomToolCallsByCallID(t *testing.T) {
lastRequest := []byte(`{"model":"test-model","stream":true,"input":[{"type":"custom_tool_call","id":"ctc-1","call_id":"call-1","name":"apply_patch"},{"type":"custom_tool_call_output","id":"tool-out-1","call_id":"call-1"}]}`)
lastResponseOutput := []byte(`[
{"type":"custom_tool_call","id":"ctc-1","call_id":"call-1","name":"apply_patch"}
]`)
raw := []byte(`{"type":"response.create","input":[{"type":"message","id":"msg-2"}]}`)
normalized, _, errMsg := normalizeResponsesWebsocketRequest(raw, lastRequest, lastResponseOutput)
if errMsg != nil {
t.Fatalf("unexpected error: %v", errMsg.Error)
}
items := gjson.GetBytes(normalized, "input").Array()
if len(items) != 3 {
t.Fatalf("merged input len = %d, want 3: %s", len(items), normalized)
}
if items[0].Get("id").String() != "ctc-1" ||
items[1].Get("id").String() != "tool-out-1" ||
items[2].Get("id").String() != "msg-2" {
t.Fatalf("unexpected merged input order: %s", normalized)
}
}
func TestResponsesWebsocketCompactionResetsTurnStateOnCustomToolTranscriptReplacement(t *testing.T) {
gin.SetMode(gin.TestMode)
executor := &websocketCompactionCaptureExecutor{}
manager := coreauth.NewManager(nil, nil, nil)
manager.RegisterExecutor(executor)
auth := &coreauth.Auth{ID: "auth-sse", Provider: executor.Identifier(), Status: coreauth.StatusActive}
if _, err := manager.Register(context.Background(), auth); err != nil {
t.Fatalf("Register auth: %v", err)
}
registry.GetGlobalRegistry().RegisterClient(auth.ID, auth.Provider, []*registry.ModelInfo{{ID: "test-model"}})
t.Cleanup(func() {
registry.GetGlobalRegistry().UnregisterClient(auth.ID)
})
base := handlers.NewBaseAPIHandlers(&sdkconfig.SDKConfig{}, manager)
h := NewOpenAIResponsesAPIHandler(base)
router := gin.New()
router.GET("/v1/responses/ws", h.ResponsesWebsocket)
router.POST("/v1/responses/compact", h.Compact)
server := httptest.NewServer(router)
defer server.Close()
wsURL := "ws" + strings.TrimPrefix(server.URL, "http") + "/v1/responses/ws"
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
if err != nil {
t.Fatalf("dial websocket: %v", err)
}
defer func() {
if errClose := conn.Close(); errClose != nil {
t.Fatalf("close websocket: %v", errClose)
}
}()
requests := []string{
`{"type":"response.create","model":"test-model","input":[{"type":"message","id":"msg-1"}]}`,
`{"type":"response.create","input":[{"type":"custom_tool_call_output","call_id":"call-1","id":"tool-out-1"}]}`,
}
for i := range requests {
if errWrite := conn.WriteMessage(websocket.TextMessage, []byte(requests[i])); errWrite != nil {
t.Fatalf("write websocket message %d: %v", i+1, errWrite)
}
_, payload, errReadMessage := conn.ReadMessage()
if errReadMessage != nil {
t.Fatalf("read websocket message %d: %v", i+1, errReadMessage)
}
if got := gjson.GetBytes(payload, "type").String(); got != wsEventTypeCompleted {
t.Fatalf("message %d payload type = %s, want %s", i+1, got, wsEventTypeCompleted)
}
}
compactResp, errPost := server.Client().Post(
server.URL+"/v1/responses/compact",
"application/json",
strings.NewReader(`{"model":"test-model","input":[{"type":"message","id":"summary-1"}]}`),
)
if errPost != nil {
t.Fatalf("compact request failed: %v", errPost)
}
if errClose := compactResp.Body.Close(); errClose != nil {
t.Fatalf("close compact response body: %v", errClose)
}
if compactResp.StatusCode != http.StatusOK {
t.Fatalf("compact status = %d, want %d", compactResp.StatusCode, http.StatusOK)
}
postCompact := `{"type":"response.create","input":[{"type":"custom_tool_call","id":"ctc-compact","call_id":"call-1","name":"apply_patch"},{"type":"custom_tool_call_output","id":"tool-out-compact","call_id":"call-1"},{"type":"message","id":"msg-2"}]}`
if errWrite := conn.WriteMessage(websocket.TextMessage, []byte(postCompact)); errWrite != nil {
t.Fatalf("write post-compact websocket message: %v", errWrite)
}
_, payload, errReadMessage := conn.ReadMessage()
if errReadMessage != nil {
t.Fatalf("read post-compact websocket message: %v", errReadMessage)
}
if got := gjson.GetBytes(payload, "type").String(); got != wsEventTypeCompleted {
t.Fatalf("post-compact payload type = %s, want %s", got, wsEventTypeCompleted)
}
executor.mu.Lock()
defer executor.mu.Unlock()
if executor.compactPayload == nil {
t.Fatalf("compact payload was not captured")
}
if len(executor.streamPayloads) != 3 {
t.Fatalf("stream payload count = %d, want 3", len(executor.streamPayloads))
}
merged := executor.streamPayloads[2]
items := gjson.GetBytes(merged, "input").Array()
if len(items) != 3 {
t.Fatalf("merged input len = %d, want 3: %s", len(items), merged)
}
if items[0].Get("id").String() != "ctc-compact" ||
items[1].Get("id").String() != "tool-out-compact" ||
items[2].Get("id").String() != "msg-2" {
t.Fatalf("unexpected post-compact input order: %s", merged)
}
if items[0].Get("call_id").String() != "call-1" {
t.Fatalf("post-compact custom tool call id = %s, want call-1", items[0].Get("call_id").String())
}
}
func TestResponsesWebsocketCompactionResetsTurnStateOnTranscriptReplacement(t *testing.T) {
gin.SetMode(gin.TestMode)
@@ -266,15 +266,15 @@ func repairResponsesToolCallsArray(outputCache, callCache *websocketToolOutputCa
continue
}
itemType := strings.TrimSpace(gjson.GetBytes(item, "type").String())
switch itemType {
case "function_call_output":
switch {
case isResponsesToolCallOutputType(itemType):
callID := strings.TrimSpace(gjson.GetBytes(item, "call_id").String())
if callID == "" {
continue
}
outputPresent[callID] = struct{}{}
outputCache.record(sessionKey, callID, item)
case "function_call":
case isResponsesToolCallType(itemType):
callID := strings.TrimSpace(gjson.GetBytes(item, "call_id").String())
if callID == "" {
continue
@@ -293,7 +293,7 @@ func repairResponsesToolCallsArray(outputCache, callCache *websocketToolOutputCa
continue
}
itemType := strings.TrimSpace(gjson.GetBytes(item, "type").String())
if itemType == "function_call_output" {
if isResponsesToolCallOutputType(itemType) {
callID := strings.TrimSpace(gjson.GetBytes(item, "call_id").String())
if callID == "" {
// Upstream rejects tool outputs without a call_id; drop it.
@@ -325,7 +325,7 @@ func repairResponsesToolCallsArray(outputCache, callCache *websocketToolOutputCa
// Drop orphaned function_call_output items; upstream rejects transcripts with missing calls.
continue
}
if itemType != "function_call" {
if !isResponsesToolCallType(itemType) {
filtered = append(filtered, item)
continue
}
@@ -376,7 +376,7 @@ func recordResponsesWebsocketToolCallsFromPayloadWithCache(cache *websocketToolO
return
}
for _, item := range output.Array() {
if strings.TrimSpace(item.Get("type").String()) != "function_call" {
if !isResponsesToolCallType(item.Get("type").String()) {
continue
}
callID := strings.TrimSpace(item.Get("call_id").String())
@@ -390,7 +390,7 @@ func recordResponsesWebsocketToolCallsFromPayloadWithCache(cache *websocketToolO
if !item.Exists() || !item.IsObject() {
return
}
if strings.TrimSpace(item.Get("type").String()) != "function_call" {
if !isResponsesToolCallType(item.Get("type").String()) {
return
}
callID := strings.TrimSpace(item.Get("call_id").String())
@@ -400,3 +400,21 @@ func recordResponsesWebsocketToolCallsFromPayloadWithCache(cache *websocketToolO
cache.record(sessionKey, callID, json.RawMessage(item.Raw))
}
}
func isResponsesToolCallType(itemType string) bool {
switch strings.TrimSpace(itemType) {
case "function_call", "custom_tool_call":
return true
default:
return false
}
}
func isResponsesToolCallOutputType(itemType string) bool {
switch strings.TrimSpace(itemType) {
case "function_call_output", "custom_tool_call_output":
return true
default:
return false
}
}
+180 -35
View File
@@ -234,6 +234,84 @@ func (m *Manager) RefreshSchedulerEntry(authID string) {
m.scheduler.upsertAuth(snapshot)
}
// ReconcileRegistryModelStates aligns per-model runtime state with the current
// registry snapshot for one auth.
//
// Supported models are reset to a clean state because re-registration already
// cleared the registry-side cooldown/suspension snapshot. ModelStates for
// models that are no longer present in the registry are pruned entirely so
// renamed/removed models cannot keep auth-level status stale.
func (m *Manager) ReconcileRegistryModelStates(ctx context.Context, authID string) {
if m == nil || authID == "" {
return
}
supportedModels := registry.GetGlobalRegistry().GetModelsForClient(authID)
supported := make(map[string]struct{}, len(supportedModels))
for _, model := range supportedModels {
if model == nil {
continue
}
modelKey := canonicalModelKey(model.ID)
if modelKey == "" {
continue
}
supported[modelKey] = struct{}{}
}
var snapshot *Auth
now := time.Now()
m.mu.Lock()
auth, ok := m.auths[authID]
if ok && auth != nil && len(auth.ModelStates) > 0 {
changed := false
for modelKey, state := range auth.ModelStates {
baseModel := canonicalModelKey(modelKey)
if baseModel == "" {
baseModel = strings.TrimSpace(modelKey)
}
if _, supportedModel := supported[baseModel]; !supportedModel {
// Drop state for models that disappeared from the current registry
// snapshot. Keeping them around leaks stale errors into auth-level
// status, management output, and websocket fallback checks.
delete(auth.ModelStates, modelKey)
changed = true
continue
}
if state == nil {
continue
}
if modelStateIsClean(state) {
continue
}
resetModelState(state, now)
changed = true
}
if len(auth.ModelStates) == 0 {
auth.ModelStates = nil
}
if changed {
updateAggregatedAvailability(auth, now)
if !hasModelError(auth, now) {
auth.LastError = nil
auth.StatusMessage = ""
auth.Status = StatusActive
}
auth.UpdatedAt = now
if errPersist := m.persist(ctx, auth); errPersist != nil {
logEntryWithRequestID(ctx).WithField("auth_id", auth.ID).Warnf("failed to persist auth changes during model state reconciliation: %v", errPersist)
}
snapshot = auth.Clone()
}
}
m.mu.Unlock()
if m.scheduler != nil && snapshot != nil {
m.scheduler.upsertAuth(snapshot)
}
}
func (m *Manager) SetSelector(selector Selector) {
if m == nil {
return
@@ -1838,6 +1916,7 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) {
} else {
if result.Model != "" {
if !isRequestScopedNotFoundResultError(result.Error) {
disableCooling := quotaCooldownDisabledForAuth(auth)
state := ensureModelState(auth, result.Model)
state.Unavailable = true
state.Status = StatusError
@@ -1858,31 +1937,45 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) {
} else {
switch statusCode {
case 401:
next := now.Add(30 * time.Minute)
state.NextRetryAfter = next
suspendReason = "unauthorized"
shouldSuspendModel = true
if disableCooling {
state.NextRetryAfter = time.Time{}
} else {
next := now.Add(30 * time.Minute)
state.NextRetryAfter = next
suspendReason = "unauthorized"
shouldSuspendModel = true
}
case 402, 403:
next := now.Add(30 * time.Minute)
state.NextRetryAfter = next
suspendReason = "payment_required"
shouldSuspendModel = true
if disableCooling {
state.NextRetryAfter = time.Time{}
} else {
next := now.Add(30 * time.Minute)
state.NextRetryAfter = next
suspendReason = "payment_required"
shouldSuspendModel = true
}
case 404:
next := now.Add(12 * time.Hour)
state.NextRetryAfter = next
suspendReason = "not_found"
shouldSuspendModel = true
if disableCooling {
state.NextRetryAfter = time.Time{}
} else {
next := now.Add(12 * time.Hour)
state.NextRetryAfter = next
suspendReason = "not_found"
shouldSuspendModel = true
}
case 429:
var next time.Time
backoffLevel := state.Quota.BackoffLevel
if result.RetryAfter != nil {
next = now.Add(*result.RetryAfter)
} else {
cooldown, nextLevel := nextQuotaCooldown(backoffLevel, quotaCooldownDisabledForAuth(auth))
if cooldown > 0 {
next = now.Add(cooldown)
if !disableCooling {
if result.RetryAfter != nil {
next = now.Add(*result.RetryAfter)
} else {
cooldown, nextLevel := nextQuotaCooldown(backoffLevel, disableCooling)
if cooldown > 0 {
next = now.Add(cooldown)
}
backoffLevel = nextLevel
}
backoffLevel = nextLevel
}
state.NextRetryAfter = next
state.Quota = QuotaState{
@@ -1891,11 +1984,13 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) {
NextRecoverAt: next,
BackoffLevel: backoffLevel,
}
suspendReason = "quota"
shouldSuspendModel = true
setModelQuota = true
if !disableCooling {
suspendReason = "quota"
shouldSuspendModel = true
setModelQuota = true
}
case 408, 500, 502, 503, 504:
if quotaCooldownDisabledForAuth(auth) {
if disableCooling {
state.NextRetryAfter = time.Time{}
} else {
next := now.Add(1 * time.Minute)
@@ -1966,8 +2061,28 @@ func resetModelState(state *ModelState, now time.Time) {
state.UpdatedAt = now
}
func modelStateIsClean(state *ModelState) bool {
if state == nil {
return true
}
if state.Status != StatusActive {
return false
}
if state.Unavailable || state.StatusMessage != "" || !state.NextRetryAfter.IsZero() || state.LastError != nil {
return false
}
if state.Quota.Exceeded || state.Quota.Reason != "" || !state.Quota.NextRecoverAt.IsZero() || state.Quota.BackoffLevel != 0 {
return false
}
return true
}
func updateAggregatedAvailability(auth *Auth, now time.Time) {
if auth == nil || len(auth.ModelStates) == 0 {
if auth == nil {
return
}
if len(auth.ModelStates) == 0 {
clearAggregatedAvailability(auth)
return
}
allUnavailable := true
@@ -1975,10 +2090,12 @@ func updateAggregatedAvailability(auth *Auth, now time.Time) {
quotaExceeded := false
quotaRecover := time.Time{}
maxBackoffLevel := 0
hasState := false
for _, state := range auth.ModelStates {
if state == nil {
continue
}
hasState = true
stateUnavailable := false
if state.Status == StatusDisabled {
stateUnavailable = true
@@ -2008,6 +2125,10 @@ func updateAggregatedAvailability(auth *Auth, now time.Time) {
}
}
}
if !hasState {
clearAggregatedAvailability(auth)
return
}
auth.Unavailable = allUnavailable
if allUnavailable {
auth.NextRetryAfter = earliestRetry
@@ -2027,6 +2148,15 @@ func updateAggregatedAvailability(auth *Auth, now time.Time) {
}
}
func clearAggregatedAvailability(auth *Auth) {
if auth == nil {
return
}
auth.Unavailable = false
auth.NextRetryAfter = time.Time{}
auth.Quota = QuotaState{}
}
func hasModelError(auth *Auth, now time.Time) bool {
if auth == nil || len(auth.ModelStates) == 0 {
return false
@@ -2211,6 +2341,7 @@ func applyAuthFailureState(auth *Auth, resultErr *Error, retryAfter *time.Durati
if isRequestScopedNotFoundResultError(resultErr) {
return
}
disableCooling := quotaCooldownDisabledForAuth(auth)
auth.Unavailable = true
auth.Status = StatusError
auth.UpdatedAt = now
@@ -2224,32 +2355,46 @@ func applyAuthFailureState(auth *Auth, resultErr *Error, retryAfter *time.Durati
switch statusCode {
case 401:
auth.StatusMessage = "unauthorized"
auth.NextRetryAfter = now.Add(30 * time.Minute)
if disableCooling {
auth.NextRetryAfter = time.Time{}
} else {
auth.NextRetryAfter = now.Add(30 * time.Minute)
}
case 402, 403:
auth.StatusMessage = "payment_required"
auth.NextRetryAfter = now.Add(30 * time.Minute)
if disableCooling {
auth.NextRetryAfter = time.Time{}
} else {
auth.NextRetryAfter = now.Add(30 * time.Minute)
}
case 404:
auth.StatusMessage = "not_found"
auth.NextRetryAfter = now.Add(12 * time.Hour)
if disableCooling {
auth.NextRetryAfter = time.Time{}
} else {
auth.NextRetryAfter = now.Add(12 * time.Hour)
}
case 429:
auth.StatusMessage = "quota exhausted"
auth.Quota.Exceeded = true
auth.Quota.Reason = "quota"
var next time.Time
if retryAfter != nil {
next = now.Add(*retryAfter)
} else {
cooldown, nextLevel := nextQuotaCooldown(auth.Quota.BackoffLevel, quotaCooldownDisabledForAuth(auth))
if cooldown > 0 {
next = now.Add(cooldown)
if !disableCooling {
if retryAfter != nil {
next = now.Add(*retryAfter)
} else {
cooldown, nextLevel := nextQuotaCooldown(auth.Quota.BackoffLevel, disableCooling)
if cooldown > 0 {
next = now.Add(cooldown)
}
auth.Quota.BackoffLevel = nextLevel
}
auth.Quota.BackoffLevel = nextLevel
}
auth.Quota.NextRecoverAt = next
auth.NextRetryAfter = next
case 408, 500, 502, 503, 504:
auth.StatusMessage = "transient upstream error"
if quotaCooldownDisabledForAuth(auth) {
if disableCooling {
auth.NextRetryAfter = time.Time{}
} else {
auth.NextRetryAfter = now.Add(1 * time.Minute)
@@ -180,6 +180,34 @@ func (e *authFallbackExecutor) StreamCalls() []string {
return out
}
type retryAfterStatusError struct {
status int
message string
retryAfter time.Duration
}
func (e *retryAfterStatusError) Error() string {
if e == nil {
return ""
}
return e.message
}
func (e *retryAfterStatusError) StatusCode() int {
if e == nil {
return 0
}
return e.status
}
func (e *retryAfterStatusError) RetryAfter() *time.Duration {
if e == nil {
return nil
}
d := e.retryAfter
return &d
}
func newCredentialRetryLimitTestManager(t *testing.T, maxRetryCredentials int) (*Manager, *credentialRetryLimitExecutor) {
t.Helper()
@@ -450,6 +478,174 @@ func TestManager_MarkResult_RespectsAuthDisableCoolingOverride(t *testing.T) {
}
}
func TestManager_MarkResult_RespectsAuthDisableCoolingOverride_On403(t *testing.T) {
prev := quotaCooldownDisabled.Load()
quotaCooldownDisabled.Store(false)
t.Cleanup(func() { quotaCooldownDisabled.Store(prev) })
m := NewManager(nil, nil, nil)
auth := &Auth{
ID: "auth-403",
Provider: "claude",
Metadata: map[string]any{
"disable_cooling": true,
},
}
if _, errRegister := m.Register(context.Background(), auth); errRegister != nil {
t.Fatalf("register auth: %v", errRegister)
}
model := "test-model-403"
reg := registry.GetGlobalRegistry()
reg.RegisterClient(auth.ID, "claude", []*registry.ModelInfo{{ID: model}})
t.Cleanup(func() { reg.UnregisterClient(auth.ID) })
m.MarkResult(context.Background(), Result{
AuthID: auth.ID,
Provider: "claude",
Model: model,
Success: false,
Error: &Error{HTTPStatus: http.StatusForbidden, Message: "forbidden"},
})
updated, ok := m.GetByID(auth.ID)
if !ok || updated == nil {
t.Fatalf("expected auth to be present")
}
state := updated.ModelStates[model]
if state == nil {
t.Fatalf("expected model state to be present")
}
if !state.NextRetryAfter.IsZero() {
t.Fatalf("expected NextRetryAfter to be zero when disable_cooling=true, got %v", state.NextRetryAfter)
}
if count := reg.GetModelCount(model); count <= 0 {
t.Fatalf("expected model count > 0 when disable_cooling=true, got %d", count)
}
}
func TestManager_Execute_DisableCooling_DoesNotBlackoutAfter403(t *testing.T) {
prev := quotaCooldownDisabled.Load()
quotaCooldownDisabled.Store(false)
t.Cleanup(func() { quotaCooldownDisabled.Store(prev) })
m := NewManager(nil, nil, nil)
executor := &authFallbackExecutor{
id: "claude",
executeErrors: map[string]error{
"auth-403-exec": &Error{
HTTPStatus: http.StatusForbidden,
Message: "forbidden",
},
},
}
m.RegisterExecutor(executor)
auth := &Auth{
ID: "auth-403-exec",
Provider: "claude",
Metadata: map[string]any{
"disable_cooling": true,
},
}
if _, errRegister := m.Register(context.Background(), auth); errRegister != nil {
t.Fatalf("register auth: %v", errRegister)
}
model := "test-model-403-exec"
reg := registry.GetGlobalRegistry()
reg.RegisterClient(auth.ID, "claude", []*registry.ModelInfo{{ID: model}})
t.Cleanup(func() { reg.UnregisterClient(auth.ID) })
req := cliproxyexecutor.Request{Model: model}
_, errExecute1 := m.Execute(context.Background(), []string{"claude"}, req, cliproxyexecutor.Options{})
if errExecute1 == nil {
t.Fatal("expected first execute error")
}
if statusCodeFromError(errExecute1) != http.StatusForbidden {
t.Fatalf("first execute status = %d, want %d", statusCodeFromError(errExecute1), http.StatusForbidden)
}
_, errExecute2 := m.Execute(context.Background(), []string{"claude"}, req, cliproxyexecutor.Options{})
if errExecute2 == nil {
t.Fatal("expected second execute error")
}
if statusCodeFromError(errExecute2) != http.StatusForbidden {
t.Fatalf("second execute status = %d, want %d", statusCodeFromError(errExecute2), http.StatusForbidden)
}
}
func TestManager_Execute_DisableCooling_DoesNotBlackoutAfter429RetryAfter(t *testing.T) {
prev := quotaCooldownDisabled.Load()
quotaCooldownDisabled.Store(false)
t.Cleanup(func() { quotaCooldownDisabled.Store(prev) })
m := NewManager(nil, nil, nil)
executor := &authFallbackExecutor{
id: "claude",
executeErrors: map[string]error{
"auth-429-exec": &retryAfterStatusError{
status: http.StatusTooManyRequests,
message: "quota exhausted",
retryAfter: 2 * time.Minute,
},
},
}
m.RegisterExecutor(executor)
auth := &Auth{
ID: "auth-429-exec",
Provider: "claude",
Metadata: map[string]any{
"disable_cooling": true,
},
}
if _, errRegister := m.Register(context.Background(), auth); errRegister != nil {
t.Fatalf("register auth: %v", errRegister)
}
model := "test-model-429-exec"
reg := registry.GetGlobalRegistry()
reg.RegisterClient(auth.ID, "claude", []*registry.ModelInfo{{ID: model}})
t.Cleanup(func() { reg.UnregisterClient(auth.ID) })
req := cliproxyexecutor.Request{Model: model}
_, errExecute1 := m.Execute(context.Background(), []string{"claude"}, req, cliproxyexecutor.Options{})
if errExecute1 == nil {
t.Fatal("expected first execute error")
}
if statusCodeFromError(errExecute1) != http.StatusTooManyRequests {
t.Fatalf("first execute status = %d, want %d", statusCodeFromError(errExecute1), http.StatusTooManyRequests)
}
_, errExecute2 := m.Execute(context.Background(), []string{"claude"}, req, cliproxyexecutor.Options{})
if errExecute2 == nil {
t.Fatal("expected second execute error")
}
if statusCodeFromError(errExecute2) != http.StatusTooManyRequests {
t.Fatalf("second execute status = %d, want %d", statusCodeFromError(errExecute2), http.StatusTooManyRequests)
}
calls := executor.ExecuteCalls()
if len(calls) != 2 {
t.Fatalf("execute calls = %d, want 2", len(calls))
}
updated, ok := m.GetByID(auth.ID)
if !ok || updated == nil {
t.Fatalf("expected auth to be present")
}
state := updated.ModelStates[model]
if state == nil {
t.Fatalf("expected model state to be present")
}
if !state.NextRetryAfter.IsZero() {
t.Fatalf("expected NextRetryAfter to be zero when disable_cooling=true, got %v", state.NextRetryAfter)
}
}
func TestManager_MarkResult_RequestScopedNotFoundDoesNotCooldownAuth(t *testing.T) {
m := NewManager(nil, nil, nil)
+3
View File
@@ -312,6 +312,7 @@ func (s *Service) applyCoreAuthAddOrUpdate(ctx context.Context, auth *coreauth.A
// This operation may block on network calls, but the auth configuration
// is already effective at this point.
s.registerModelsForAuth(auth)
s.coreManager.ReconcileRegistryModelStates(ctx, auth.ID)
// Refresh the scheduler entry so that the auth's supportedModelSet is rebuilt
// from the now-populated global model registry. Without this, newly added auths
@@ -1027,6 +1028,7 @@ func (s *Service) refreshModelRegistrationForAuth(current *coreauth.Auth) bool {
s.ensureExecutorsForAuth(current)
}
s.registerModelsForAuth(current)
s.coreManager.ReconcileRegistryModelStates(context.Background(), current.ID)
latest, ok := s.latestAuthForModelRegistration(current.ID)
if !ok || latest.Disabled {
@@ -1040,6 +1042,7 @@ func (s *Service) refreshModelRegistrationForAuth(current *coreauth.Auth) bool {
// no auth fields changed, but keeps the refresh path simple and correct.
s.ensureExecutorsForAuth(latest)
s.registerModelsForAuth(latest)
s.coreManager.ReconcileRegistryModelStates(context.Background(), latest.ID)
s.coreManager.RefreshSchedulerEntry(current.ID)
return true
}
+2 -2
View File
@@ -58,7 +58,7 @@ func Parse(raw string) (Setting, error) {
}
switch parsedURL.Scheme {
case "socks5", "http", "https":
case "socks5", "socks5h", "http", "https":
setting.Mode = ModeProxy
setting.URL = parsedURL
return setting, nil
@@ -95,7 +95,7 @@ func BuildHTTPTransport(raw string) (*http.Transport, Mode, error) {
case ModeDirect:
return NewDirectTransport(), setting.Mode, nil
case ModeProxy:
if setting.URL.Scheme == "socks5" {
if setting.URL.Scheme == "socks5" || setting.URL.Scheme == "socks5h" {
var proxyAuth *proxy.Auth
if setting.URL.User != nil {
username := setting.URL.User.Username()
+22
View File
@@ -30,6 +30,7 @@ func TestParse(t *testing.T) {
{name: "http", input: "http://proxy.example.com:8080", want: ModeProxy},
{name: "https", input: "https://proxy.example.com:8443", want: ModeProxy},
{name: "socks5", input: "socks5://proxy.example.com:1080", want: ModeProxy},
{name: "socks5h", input: "socks5h://proxy.example.com:1080", want: ModeProxy},
{name: "invalid", input: "bad-value", want: ModeInvalid, wantErr: true},
}
@@ -137,3 +138,24 @@ func TestBuildHTTPTransportSOCKS5ProxyInheritsDefaultTransportSettings(t *testin
t.Fatalf("TLSHandshakeTimeout = %v, want %v", transport.TLSHandshakeTimeout, defaultTransport.TLSHandshakeTimeout)
}
}
func TestBuildHTTPTransportSOCKS5HProxy(t *testing.T) {
t.Parallel()
transport, mode, errBuild := BuildHTTPTransport("socks5h://proxy.example.com:1080")
if errBuild != nil {
t.Fatalf("BuildHTTPTransport returned error: %v", errBuild)
}
if mode != ModeProxy {
t.Fatalf("mode = %d, want %d", mode, ModeProxy)
}
if transport == nil {
t.Fatal("expected transport, got nil")
}
if transport.Proxy != nil {
t.Fatal("expected SOCKS5H transport to bypass http proxy function")
}
if transport.DialContext == nil {
t.Fatal("expected SOCKS5H transport to have custom DialContext")
}
}
@@ -0,0 +1,106 @@
package test
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
type jsonObject = map[string]any
func loadClaudeCodeSentinelFixture(t *testing.T, name string) jsonObject {
t.Helper()
path := filepath.Join("testdata", "claude_code_sentinels", name)
data := mustReadFile(t, path)
var payload jsonObject
if err := json.Unmarshal(data, &payload); err != nil {
t.Fatalf("unmarshal %s: %v", name, err)
}
return payload
}
func mustReadFile(t *testing.T, path string) []byte {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
return data
}
func requireStringField(t *testing.T, obj jsonObject, key string) string {
t.Helper()
value, ok := obj[key].(string)
if !ok || value == "" {
t.Fatalf("field %q missing or empty: %#v", key, obj[key])
}
return value
}
func TestClaudeCodeSentinel_ToolProgressShape(t *testing.T) {
payload := loadClaudeCodeSentinelFixture(t, "tool_progress.json")
if got := requireStringField(t, payload, "type"); got != "tool_progress" {
t.Fatalf("type = %q, want tool_progress", got)
}
requireStringField(t, payload, "tool_use_id")
requireStringField(t, payload, "tool_name")
requireStringField(t, payload, "session_id")
if _, ok := payload["elapsed_time_seconds"].(float64); !ok {
t.Fatalf("elapsed_time_seconds missing or non-number: %#v", payload["elapsed_time_seconds"])
}
}
func TestClaudeCodeSentinel_SessionStateShape(t *testing.T) {
payload := loadClaudeCodeSentinelFixture(t, "session_state_changed.json")
if got := requireStringField(t, payload, "type"); got != "system" {
t.Fatalf("type = %q, want system", got)
}
if got := requireStringField(t, payload, "subtype"); got != "session_state_changed" {
t.Fatalf("subtype = %q, want session_state_changed", got)
}
state := requireStringField(t, payload, "state")
switch state {
case "idle", "running", "requires_action":
default:
t.Fatalf("unexpected session state %q", state)
}
requireStringField(t, payload, "session_id")
}
func TestClaudeCodeSentinel_ToolUseSummaryShape(t *testing.T) {
payload := loadClaudeCodeSentinelFixture(t, "tool_use_summary.json")
if got := requireStringField(t, payload, "type"); got != "tool_use_summary" {
t.Fatalf("type = %q, want tool_use_summary", got)
}
requireStringField(t, payload, "summary")
rawIDs, ok := payload["preceding_tool_use_ids"].([]any)
if !ok || len(rawIDs) == 0 {
t.Fatalf("preceding_tool_use_ids missing or empty: %#v", payload["preceding_tool_use_ids"])
}
for i, raw := range rawIDs {
if id, ok := raw.(string); !ok || id == "" {
t.Fatalf("preceding_tool_use_ids[%d] invalid: %#v", i, raw)
}
}
}
func TestClaudeCodeSentinel_ControlRequestCanUseToolShape(t *testing.T) {
payload := loadClaudeCodeSentinelFixture(t, "control_request_can_use_tool.json")
if got := requireStringField(t, payload, "type"); got != "control_request" {
t.Fatalf("type = %q, want control_request", got)
}
requireStringField(t, payload, "request_id")
request, ok := payload["request"].(map[string]any)
if !ok {
t.Fatalf("request missing or invalid: %#v", payload["request"])
}
if got := requireStringField(t, request, "subtype"); got != "can_use_tool" {
t.Fatalf("request.subtype = %q, want can_use_tool", got)
}
requireStringField(t, request, "tool_name")
requireStringField(t, request, "tool_use_id")
if input, ok := request["input"].(map[string]any); !ok || len(input) == 0 {
t.Fatalf("request.input missing or empty: %#v", request["input"])
}
}

Some files were not shown because too many files have changed in this diff Show More