Restoring the primary after a fallback rebuilt credential ownership from
live config.yaml, not from the config the session resolved on. Two edits
made mid-session moved it onto another account:
- alpha and beta share one URL and the session runs on custom:beta. Once
beta's live endpoint moves, the named lookup misses, load_runtime_pool
falls back to URL-only matching, and the session resumes on alpha's
`hermes auth add` key.
- the session runs on custom:deepseek with a declared key_env. Once the
declaration is removed from live config, the entry looks credential-less,
so the built-in's env row is no longer foreign and restore swaps to
DEEPSEEK_API_KEY and api.deepseek.com.
Both `_primary_runtime` builders (agent construction and a model switch)
now record the owning entry's provenance (`custom_entry`), matched the way
resolution matched it, in the frozen scope when one is active. The record
holds the entry's name, provider_key, base_url, key_env names and whether
it declares a credential; it never holds a secret. Restore takes the pool
key and the ownership filter from that record, so live edits can't
reassign the endpoint or drop the declaration. A named identity whose
snapshot recorded no owner, including a snapshot from an older builder,
keeps its snapshot credential instead of borrowing by URL. A genuinely bare
`custom` snapshot without a recorded owner still matches by URL, as
before.
The two builders move unchanged into agent/primary_runtime_snapshot.py, so
agent_init.py and agent_runtime_helpers.py, both over the file-size
target, shrink instead of growing. test_primary_runtime_restore's
named-pool test now builds its agent under the config it restores with,
as a real session does; built without that config, its snapshot
correctly records no owner.
Refs #140297
An auxiliary task pinned to `custom:deepseek` still sent the built-in's key
to the entry's endpoint. `_normalize_aux_provider("custom:deepseek")` is
`deepseek`, so `_get_cached_client` peeked the built-in's whole pool, and
its env-seeded row reached resolve_provider_client as the explicit key,
ahead of the entry's own key_env. Pool recovery after an auth/quota error
loaded the same unscoped slug: `_recoverable_pool_provider` dropped the
`custom:` identity, so the entry's 401 rotated or benched the built-in's
row.
Both reads now go through `pool_for_aux_provider`: a pinned
`custom:<name>` gets its entry's scoped view (none owned means no peek and
no rotation), and `_recoverable_pool_provider` keeps the pinned identity.
A bare built-in name still reads its whole pool.
Refs #140297
load_runtime_pool keyed the restored primary's pool on
resolve_runtime_pool_key("custom", url), the first entry configured on that
URL, while only the ownership filter used the snapshot's `custom:<name>`.
With two entries on one URL, restoring beta after a fallback resumed on
alpha's `hermes auth add` row (manual rows pass beta's filter, and a
`custom:` pool skips it entirely). A named identity now takes both the pool
key and the ownership rules from the same named match. A genuinely bare
`custom` snapshot still takes the first entry on its URL, as before.
Refs #140297
A `providers:` entry keyed like a built-in (`providers.deepseek` with
`key_env: DEEPSEEK_ENTRY_KEY`) has the durable pool slug `deepseek`, which
is also the built-in's pool id. `load_pool("deepseek")` seeds that pool
from the built-in's own env vars (`env:DEEPSEEK_API_KEY`), and the
named-custom rung reads the pool before the entry's key_env. Every turn
to the entry's endpoint therefore carried the built-in provider's key,
sending a credential to a host it was never configured for. Seen live on
v0.22.1 and main. The same slug pool was also loaded unfiltered by a
bare `provider: custom` route whose URL matches the entry, by restoring
the primary after a cross-provider fallback (which then swapped in the
built-in row's key and base_url), and by a delegated child's pool. With
the declared variable unset, the host-derived `DEEPSEEK_API_KEY` stood in
for it on DeepSeek's own host.
Credential ownership now holds on every pool acquisition for the entry's
runtime. agent/custom_entry_pool.py defines it by provenance: for an
entry that declares a credential (api_key, key_env/api_key_env or
key_cmd), a slug pool contributes only its manual (`hermes auth add`)
rows and rows seeded from the entry's own key_env; `custom:<name>` pools
stay whole. The entry-scoped view is applied on:
- main, frozen and auxiliary resolution, and URL-matched bare custom
routes (the owning entry comes from the frozen config when present);
- primary restore, keyed on the snapshot's runtime identity (`custom` /
`custom:<name>`), never on the shared slug: no owned rows keeps the
entry's fixed credential;
- a delegated child's pool.
A bare custom route to an entry with a declared credential now gets that
credential, and a declared credential that resolves to nothing gets no
implicit host-gated env key (the key_env warning says so truthfully).
Unchanged: explicit built-in picks still use their env keys; the
built-in loads and persists its whole pool (persisting a view leaves the
omitted rows on disk); entries that declare no credential resolve as
before; manual rows keep their precedence on the main path, and aux
resolution stays configured-key-first.
Refs #140297