# Credential connection

1. CredentialData, stored in the pool
2. resolve_connection reads the external file or runs the helper
3. ResolvedConnection
4. Wire: model.base_url only when the credential has none

Never reduced to a bare key string. OAuth comes from `kind`, not a token prefix. One key can reach another endpoint: a Bailian key stores `https://…maas.aliyuncs.com/compatible-mode/v1`, under the same `openai-completions` protocol, with no extra model list.

## `CredentialData`

| `kind` | `auth_value` | `data` |
| --- | --- | --- |
| `api_key` | the key | usually empty |
| `oauth` | access token | `refresh_token` `expires_at_ms` `client_id` `token_endpoint` `scope` `id_token` |
| `device_code` | access token | `refresh_token` `expires_at_ms` `device_code_flow_id` |
| `cli_delegated` | empty | `store_path` `access_key_path` `refresh_key_path` `expires_key_path` |
| `credential_process` | empty | `command` `parses` `json_key_path` `cache_seconds` |
| `sso` | empty | `broker` `subject` |

Every kind also has `base_url` and `headers`, both empty by default. Email, display name and org id stay in `Credential.metadata`.

## `resolve_connection`

| Kind | Behaviour |
| --- | --- |
| `cli_delegated` | Reads the external file; that CLI stays authoritative |
| `credential_process` | Runs the helper, reuses its token for `cache_seconds`. Failure raises `AuthCredentialProcessError`, re-raised by the resolver ladder and never replaced by another layer. |
| `sso` | Raises `AuthConfigError`: reserved, no flow implements it |
| others | Returns `auth_value` |

`auth.usage.acquire_pooled` returns `(conn, profile, cred_id)`.

## Wire rules

```
api_key  = conn.auth_value if conn else opts.api_key
base_url = (conn.base_url if conn and conn.base_url else None) or model.base_url
headers  = {**(model.headers or {}), **(conn.headers if conn else {}), **(opts.headers or {})}
is_oauth = bool(conn and conn.kind in ("oauth", "device_code"))
```

Same in `openai_completions`, `openai_responses` and `anthropic`.

**Old format and carry-over**

Runtime accepts only the current structure; old JSON with `__type__` is an error. `auth/carry_over.py` runs once before the store first reads: it rewrites every payload of every old `~/.openprogram/auth/<provider>/<account>.json` into one list per provider, `auth/<provider>/credentials.json`, after copying the folder aside. No new login.

| Old `__type__` | New `kind` | `auth_value` from |
| --- | --- | --- |
| `ApiKeyPayload` | `api_key` | `api_key` |
| `OAuthPayload` | `oauth` | `access_token` |
| `DeviceCodePayload` | `device_code` | `access_token` |
| `CliDelegatedPayload` | `cli_delegated` | empty |
| `ExternalProcessPayload` | `credential_process` | empty |
| `SsoPayload` | `sso` | empty |

Other fields move to `data`.

**Tests**

- `resolve_connection`: each kind; key with and without `base_url`; live external read; helper json and text parsing, cache window, every failure raising; sso raising.
- Round trip of `CredentialData`, including `data`.
- Wire rules: credential `base_url` wins; `kind=oauth` sets `is_oauth` with no prefix.
- End to end: a Bailian-`base_url` key reaches Bailian; the official OpenAI key stays on its default.
- Carry-over: correct fields, no `__type__`, every credential arrives, a second run changes nothing.

[Auth storage](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/auth/unified-auth-storage.html)
