Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(model-hub): migrate persisted native authentication without runtime overrides (#2070)
Call White2b2c4df05a95
diff --git a/config/atomic_io.py b/config/atomic_io.pyindex df8df18e4..d2b769412 100644--- a/config/atomic_io.py+++ b/config/atomic_io.py@@ -1,4 +1,4 @@-"""One owner for replacing an agent-owned state document atomically.+"""One owner for publishing a complete local state document atomically.Nineteen modules used to hand-roll "write a temp file, then ``os.replace`` itover the destination", and no two of them hand-rolled it quite the same way.@@ -19,14 +19,17 @@ formats stay where they are (``indent=2`` for human-read state, compact formachine-read projections, ``sort_keys`` for diffable manifests): those are realper-file decisions, unlike the durability and permission gaps above.-**Scope.** This owns replacing one *whole* state document that Avibe itself-owns. It is deliberately not the owner of every rename in the repository, and+**Scope.** This owns the mechanics of replacing one *whole* state document.+It is deliberately not the owner of every rename in the repository, andseveral callers stay outside it on purpose: a compare-and-swap that re-readsbefore replacing (``config/v2_config``), an editor writing *user* files whoseexisting mode and mtime must survive (``core/file_browser_service``), adatabase swap that must move sidecars with the file (``storage/backups``), andanything reserving a temp *name* without writing content (``vibe/screenshot``).Reach for this when the write is "these bytes, entirely, or nothing".+Native migration's user-file transaction delegates publication here with an+explicit captured mode and a final consent check, retaining ownership of its+compare-before-write, strict directory durability and recovery policy.The guarantees, for every caller:@@ -37,20 +40,17 @@ The guarantees, for every caller:each other's fragments.* A failed write never leaves a temp file behind, and never truncates thedestination — the old contents survive until the rename succeeds.-* The replacement is owner-private (0600) from the moment it exists, because- ``mkstemp`` creates it that way and the rename preserves it. There is no- window in which a file holding agent credentials is world-readable.--Every path written through here is agent-owned or user-owned local state, so-0600 is the right mode for all of them and this module does not take a-parameter for it. A caller that genuinely needs a looser mode should say so-explicitly rather than inherit one by accident.+* By default the replacement is owner-private (0600) from creation through+ publication. A user-file editor may explicitly supply its captured mode;+ that mode is applied only to the unpublished inode, before its fsync.+ No caller widens a published path through this primitive."""from __future__ import annotationsimport osimport tempfile+from collections.abc import Callablefrom pathlib import Path__all__ = ["write_atomic"]@@ -74,7 +74,14 @@ def _fsync_directory(path: Path) -> None:os.close(descriptor)-def write_atomic(path: Path, data: str | bytes, *, follow_symlinks: bool = False) -> None:+def write_atomic(+ path: Path,+ data: str | bytes,+ *,+ follow_symlinks: bool = False,+ mode: int = 0o600,+ before_replace: Callable[[], None] | None = None,+) -> None:"""Replace *path* with *data* atomically, creating parent directories.``follow_symlinks`` writes *through* a symlink to its real target instead of@@ -84,15 +91,24 @@ def write_atomic(path: Path, data: str | bytes, *, follow_symlinks: bool = Falsesetup. Off by default, because for state this process owns, resolving asymlink an attacker planted is the wrong answer.- Raises ``OSError`` if the write fails; the destination is left untouched.+ ``mode`` is an explicit publication mode, not a request to inherit the+ destination's current permissions. Agent-owned state keeps the 0600 default.+ ``before_replace`` may reject stale caller-owned consent after the temporary+ bytes and permissions are flushed, immediately before publication. This is+ a comparison hook, not a cross-process atomic compare-and-swap guarantee.++ Failures before publication leave the destination untouched by this writer+ and remove the unpublished temporary file. Callback exceptions propagate."""+ if isinstance(mode, bool) or not isinstance(mode, int) or not 0 <= mode <= 0o777:+ raise ValueError("invalid publication mode")target = Path(os.path.realpath(path)) if follow_symlinks else Path(path)target.parent.mkdir(parents=True, exist_ok=True)payload = data.encode("utf-8") if isinstance(data, str) else data- # mkstemp creates the file 0600 without relying on Unix-only APIs, and the- # rename carries that mode onto the destination.+ # mkstemp starts owner-private. Its creation mode is still filtered by+ # umask, so set the exact publication mode on the unpublished inode.descriptor, temporary_name = tempfile.mkstemp(prefix=f".{target.name}.",suffix=".tmp",@@ -104,7 +120,13 @@ def write_atomic(path: Path, data: str | bytes, *, follow_symlinks: bool = Falsedescriptor = -1handle.write(payload)handle.flush()+ if hasattr(os, "fchmod"):+ os.fchmod(handle.fileno(), mode)+ else: # pragma: no cover - platform permission fallback+ os.chmod(temporary_name, mode)os.fsync(handle.fileno())+ if before_replace is not None:+ before_replace()os.replace(temporary_name, target)unpublished = None_fsync_directory(target.parent)diff --git a/core/handlers/model_hub/adapter.py b/core/handlers/model_hub/adapter.pyindex 311ab5b74..22792ca75 100644--- a/core/handlers/model_hub/adapter.py+++ b/core/handlers/model_hub/adapter.py@@ -450,6 +450,7 @@ class EngineAdapter(Protocol):secret: str,base_url: str | None,*,+ auth_scheme: str | None = None,on_reserved: Callable[[str], None] | None = None,) -> str:"""Store an API-key secret in the ENGINE-OWNED credential store and@@ -461,7 +462,8 @@ class EngineAdapter(Protocol):reservation and before any secret bytes are written. A callback failureforbids the write. Hub OAuth credentials never pass through here —they are created engine-side by the OAuth flow and surfaced via- ``OAuthFlowState.credential_ref``."""+ ``OAuthFlowState.credential_ref``. ``auth_scheme`` optionally preserves+ an explicit static transport; omission retains legacy semantics."""...async def provision_oauth_credential(@@ -511,10 +513,21 @@ class EngineAdapter(Protocol):protocol: str,secret: str,base_url: str | None,+ *,+ auth_scheme: str | None = None,) -> bool:"""Compare transient native material inside custody; return no secrets."""...+ async def credential_auth_scheme(self, credential_ref: str) -> str | None:+ """Resolve immutable static transport without requiring the old secret.++ A safe missing/content-corrupt private document may recover from its+ ref. Readable wrong-kind/unknown/conflicting metadata, unsafe paths and+ I/O errors reject. The replacement still needs normal key validation.+ """+ ...+async def retarget_api_key_credential(self,credential_ref: str,@@ -539,7 +552,17 @@ class EngineAdapter(Protocol):...async def revoke_credential(self, credential_ref: str) -> None:- """Release the stored credential (source deletion / key replacement)."""+ """Release a stored credential, preserving OAuth auth-file ordering."""+ ...++ async def revoke_api_key_credential(self, credential_ref: str) -> None:+ """Retire an unbound ref under durable API-key-only caller intent.++ Remove only the exact safe private ref namespace, even if the old+ document is missing/content-corrupt. Reject readable identity conflicts+ and unsafe paths. Never delete watched OAuth auth files or call OAuth+ management APIs. Return only after durable cleanup; failure is retryable.+ """...async def provision_transient_credential(@@ -548,6 +571,7 @@ class EngineAdapter(Protocol):secret: str,base_url: str | None,*,+ auth_scheme: str | None = None,on_reserved: Callable[[str], None] | None = None,) -> str:"""Provision an unbound credential for an unsaved observation.diff --git a/core/handlers/model_hub/migration.py b/core/handlers/model_hub/migration.pyindex 71ecfe14a..53124b933 100644--- a/core/handlers/model_hub/migration.py+++ b/core/handlers/model_hub/migration.py@@ -6,7 +6,6 @@ import asyncioimport base64import hashlibimport json-import osimport refrom dataclasses import dataclass, field, replacefrom datetime import datetime, timezone@@ -32,17 +31,20 @@ from core.handlers.model_hub.reasoning_tiers import resolve_reasoning_tiersfrom core.handlers.model_hub.migration_files import (claude_settings_paths,codex_config_paths,+ env_reference,+ env_references,opencode_auth_path,opencode_config_paths,plan_native_cleanup,+ planned_native_references,read_native_config,- read_native_toml,)from core.handlers.model_hub.migration_journal import (NativeFileEdit,NativeTakeoverJournal,TakeoverStateError,)+from core.handlers.model_hub.migration_persisted import PersistedInventoryfrom vibe.backend_model_catalog import (backend_model_entries,bundled_catalog_reasoning_efforts_by_model,@@ -56,6 +58,10 @@ from vibe.native_oauth_store import (apply_keychain_edit,read_native_oauth,)+from vibe.model_hub_runtime.api_key_vendors import (+ validate_api_key_auth_scheme,+ validate_migration_api_key_transport,+)from vibe.opencode_config import (get_opencode_custom_provider_adapter,)@@ -96,6 +102,10 @@ class MigrationCredentialsInvalidError(RuntimeError):"""Custody transferred, but one or more grants require Hub reauthentication."""+class MigrationReauthorizationRequiredError(RuntimeError):+ """A changed native snapshot cannot prove a new post-takeover OAuth grant."""++class MigrationHost(Protocol):store: Anyadapter: Any@@ -152,6 +162,7 @@ class MigrationHost(Protocol):self,payload: dict[str, object],*, on_reserved: Callable[[str], None] | None = None,+ auth_scheme: str | None = None,) -> SourceObservation: ...async def _require_proven_observation(@@ -220,6 +231,14 @@ class NativeMigrationItem:# A metadata-only OS-store row represents one selected credential# container. Its API-key/OAuth components are resolved after consent.native_store_placeholder: bool = False+ source_paths: tuple[str, ...] = ()+ required_backends: tuple[str, ...] = ()+ shell_variables: tuple[str, ...] = field(default=(), repr=False)+ shell_auth_variables: tuple[str, ...] = field(default=(), repr=False)+ shell_values: tuple[tuple[str, str], ...] = field(default=(), repr=False)+ file_snapshots: tuple[NativeFileEdit, ...] = field(default=(), repr=False)+ auth_scheme: str | None = field(default=None, repr=False)+ receipt_identity: str | None = field(default=None, repr=False)def to_payload(self) -> dict[str, object]:# Presentation metadata is additive: `vendor` and `display_name` let a@@ -238,6 +257,8 @@ class NativeMigrationItem:"vendor": self.vendor,"display_name": self.display_name,"masked_credential": self.masked_credential,+ "source_paths": list(self.source_paths),+ "required_backends": list(self.required_backends),}@@ -428,6 +449,7 @@ def _native_store_items(*,mask_credential: Callable[[str], str],state: Mapping[str, object] | None = None,+ source_paths: tuple[str, ...] = (),) -> list[NativeMigrationItem]:if snapshot is None:return []@@ -449,13 +471,17 @@ def _native_store_items(items: list[NativeMigrationItem] = []if has_oauth or placeholder or blocked:action: MigrationAction = "keep_native" if blocked else "import"+ note = _NATIVE_SUPPLY_NOTE+ if blocked and payload.get("store") == "file":+ reason = "unreadable" if payload.get("status") == "permission_needed" else "config"+ note = f"settings.models.migration.blocked.{reason}"_, source_id = _ids(backend, "oauth_native", "native-store", action)account = _safe_account_label(material.get("email")) if material else Noneitems.append(NativeMigrationItem(id=primary_id, source_id=source_id, backend=backend,kind="oauth_native", masked_detail=account or "",proposed_action=action, selected=not blocked,- notes_key=_NATIVE_SUPPLY_NOTE, vendor=vendor,+ notes_key=note, vendor=vendor,protocol=cast(Any, oauth_protocol),display_name="Claude" if backend == "claude" else "ChatGPT",account_label=account, oauth_material=material,@@ -463,6 +489,7 @@ def _native_store_items(native_store_edit=snapshot.keychain_edit,native_store_revision=snapshot.revision,native_store_placeholder=placeholder,+ source_paths=source_paths,))if secret:base_url = _oauth_text(state, "base_url")@@ -480,6 +507,7 @@ def _native_store_items(native_provider_id=_oauth_text(state, "active_provider_id"),native_store_edit=snapshot.keychain_edit,native_store_revision=snapshot.revision,+ source_paths=source_paths,))if backend == "codex":routing_revision = _stable_suffix(@@ -491,7 +519,12 @@ def _native_store_items(return items-def _blocked_item(backend: str, identity: str, reason: str = "config") -> NativeMigrationItem:+def _blocked_item(+ backend: str, identity: str, reason: str = "config", *,+ source_paths: tuple[str, ...] = (),+ shell_variables: tuple[str, ...] = (),+ shell_auth_variables: tuple[str, ...] = (),+) -> NativeMigrationItem:item_id, source_id = _ids(backend, "api_key", identity, "reauth")return NativeMigrationItem(id=item_id, source_id=source_id, backend=cast(Any, backend),@@ -500,28 +533,40 @@ def _blocked_item(backend: str, identity: str, reason: str = "config") -> Nativevendor={"claude": "anthropic", "codex": "openai", "opencode": "opencode"}[backend],protocol="anthropic" if backend == "claude" else "openai_responses",display_name={"claude": "Claude Code", "codex": "Codex", "opencode": "OpenCode"}[backend],+ source_paths=source_paths, shell_variables=shell_variables,+ shell_auth_variables=shell_auth_variables,)+def _native_source_paths(persisted: PersistedInventory, backend: str) -> tuple[str, ...]:+ paths = persisted.credential_paths[backend]+ return tuple(dict.fromkeys([+ *(str(guard.path) for guard in persisted.guards if guard.path in paths and guard.before is not None),+ *(str(path) for path in paths if path in persisted.problems),+ ]))++def _claude_items(*,home: Optional[Path],mask_credential: Callable[[str], str],allow_secret: bool = False,project_roots: tuple[Path, ...] = (),+ persisted: PersistedInventory,) -> list[NativeMigrationItem]:items: list[NativeMigrationItem] = []for path in claude_settings_paths(home, project_roots):- try:- config = read_native_config(path)- except (TakeoverStateError, OSError):- items.append(_blocked_item("claude", str(path)))+ if path in persisted.problems:+ items.append(_blocked_item(+ "claude", str(path), persisted.problems[path], source_paths=(str(path),),+ ))continue+ config = persisted.documents[path]if config is None:continueenv = config.get("env", {})if not isinstance(env, dict):- items.append(_blocked_item("claude", str(path)))+ items.append(_blocked_item("claude", str(path), source_paths=(str(path),)))continuebase_url = _oauth_text(env, "ANTHROPIC_BASE_URL")api_key = _oauth_text(env, "ANTHROPIC_API_KEY")@@ -537,13 +582,42 @@ def _claude_items(notes_key=_CUSTOM_ENDPOINT_NOTE if base_url else None,vendor="anthropic", protocol="anthropic", display_name="Anthropic",base_url=base_url, secret=api_key, masked_credential=detail,+ source_paths=(str(path),),+ ))+ if config.get("apiKeyHelper"):+ items.append(_blocked_item(+ "claude", f"{path}:helper", "helper", source_paths=(str(path),),+ ))+ token = _oauth_text(env, "ANTHROPIC_AUTH_TOKEN")+ if token:+ try:+ scheme = validate_api_key_auth_scheme("anthropic", "anthropic", base_url, token, "bearer")+ except ValueError:+ items.append(_blocked_item(+ "claude", f"{path}:ANTHROPIC_AUTH_TOKEN", "token", source_paths=(str(path),),+ ))+ else:+ item_id, source_id = _ids(+ "claude", "api_key", f"{path}:ANTHROPIC_AUTH_TOKEN", "import",+ _stable_suffix(token, base_url or "", scheme or ""),+ )+ masked = mask_credential(token)+ items.append(NativeMigrationItem(+ id=item_id, source_id=source_id, backend="claude", kind="api_key",+ masked_detail=masked, proposed_action="import", selected=True,+ notes_key=_CUSTOM_ENDPOINT_NOTE, vendor="anthropic", protocol="anthropic",+ display_name="Anthropic", base_url=base_url, secret=token,+ masked_credential=masked, source_paths=(str(path),), auth_scheme=scheme,+ ))+ if env.get("CLAUDE_CODE_OAUTH_TOKEN"):+ items.append(_blocked_item(+ "claude", f"{path}:CLAUDE_CODE_OAUTH_TOKEN", "token", source_paths=(str(path),),))- if config.get("apiKeyHelper") or env.get("ANTHROPIC_AUTH_TOKEN") or env.get("CLAUDE_CODE_OAUTH_TOKEN"):- items.append(_blocked_item("claude", f"{path}:helper-or-token", "credential"))items.extend(_native_store_items("claude", read_native_oauth("claude", home=home, allow_secret=allow_secret),mask_credential=mask_credential,+ source_paths=_native_source_paths(persisted, "claude"),))return items@@ -554,22 +628,25 @@ def _codex_items(mask_credential: Callable[[str], str],allow_secret: bool = False,project_roots: tuple[Path, ...] = (),+ persisted: PersistedInventory,) -> list[NativeMigrationItem]:items = _native_store_items("codex", read_native_oauth("codex", home=home, allow_secret=allow_secret),mask_credential=mask_credential, state=read_codex_auth_state(home),+ source_paths=_native_source_paths(persisted, "codex"),)for path in codex_config_paths(home, project_roots):- try:- config = read_native_toml(path)- except (TakeoverStateError, OSError):- items.append(_blocked_item("codex", str(path)))+ if path in persisted.problems:+ items.append(_blocked_item(+ "codex", str(path), persisted.problems[path], source_paths=(str(path),),+ ))continue+ config = persisted.documents[path]if config is None:continueproviders = config.get("model_providers", {})if not isinstance(providers, dict):- items.append(_blocked_item("codex", str(path)))+ items.append(_blocked_item("codex", str(path), source_paths=(str(path),)))continuefor provider_id, provider in providers.items():if not isinstance(provider, dict):@@ -578,15 +655,41 @@ def _codex_items(env_key = _oauth_text(provider, "env_key")headers = provider.get("http_headers", {})env_headers = provider.get("env_http_headers", {})- if (- (env_key and os.environ.get(env_key))- or env_headers- or (headers and (key or config.get("model_provider") == provider_id))- ):+ names = tuple(dict.fromkeys([+ *([env_key] if env_key else []),+ *((value for value in env_headers.values() if isinstance(value, str))+ if isinstance(env_headers, dict) else ()),+ ]))+ paths = tuple(dict.fromkeys([+ str(path), *(source for name in names for source in persisted.resolve(name).paths),+ ]))+ if env_headers or headers:# Header templates and shell-owned keys cannot be silently# reinterpreted as an ordinary Authorization bearer grant.- items.append(_blocked_item("codex", f"{path}:{provider_id}", "environment"))+ items.append(_blocked_item(+ "codex", f"{path}:{provider_id}", "headers",+ source_paths=paths, shell_variables=names, shell_auth_variables=names,+ ))continue+ shell_values: tuple[tuple[str, str], ...] = ()+ if env_key:+ resolved = persisted.resolve(env_key)+ saved_key = resolved.value.strip() if resolved.value is not None else None+ if resolved.reason or not saved_key:+ items.append(_blocked_item(+ "codex", f"{path}:{provider_id}", resolved.reason or "reference",+ source_paths=paths, shell_variables=names, shell_auth_variables=names,+ ))+ continue+ if key and key != saved_key:+ # Two credential mechanisms must not silently discard one.+ items.append(_blocked_item(+ "codex", f"{path}:{provider_id}", "credential",+ source_paths=paths, shell_variables=names, shell_auth_variables=names,+ ))+ continue+ key = saved_key+ shell_values = ((env_key, cast(str, resolved.value)),)if not key:continuebase_url = _oauth_text(provider, "base_url")@@ -603,26 +706,14 @@ def _codex_items(vendor="openai", protocol=protocol, display_name="OpenAI",secret=key, base_url=base_url, masked_credential=masked,native_provider_id=provider_id,+ source_paths=paths, shell_variables=names, shell_values=shell_values,+ shell_auth_variables=names,))return items-def _load_opencode_provider_catalog(home: Optional[Path]) -> dict[str, dict[str, Any]]:- if home is not None:- path = home / ".cache" / "opencode" / "models.json"- else:- cache_home = os.environ.get("XDG_CACHE_HOME")- path = (- Path(cache_home).expanduser() / "opencode" / "models.json"- if cache_home- else Path.home() / ".cache" / "opencode" / "models.json"- )- try:- payload = json.loads(path.read_text(encoding="utf-8"))- except (OSError, UnicodeError, json.JSONDecodeError):- return {}- if not isinstance(payload, dict):- return {}+def _load_opencode_provider_catalog(persisted: PersistedInventory) -> dict[str, dict[str, Any]]:+ payload = persisted.documents.get(persisted.catalog_path) or {}return {provider_id.strip().lower(): providerfor provider_id, provider in payload.items()@@ -695,43 +786,63 @@ def _opencode_items(home: Optional[Path],mask_credential: Callable[[str], str],project_roots: tuple[Path, ...] = (),+ persisted: PersistedInventory,) -> list[NativeMigrationItem]:items: list[NativeMigrationItem] = []try:auth_entries = read_native_config(opencode_auth_path(home)) or {}except (TakeoverStateError, OSError):- return [_blocked_item("opencode", "auth-file")]- provider_catalog = _load_opencode_provider_catalog(home)+ return [_blocked_item(+ "opencode", "auth-file", "unreadable",+ source_paths=(str(opencode_auth_path(home)),),+ )]+ provider_catalog = _load_opencode_provider_catalog(persisted)seen_providers: set[str] = set()for path in opencode_config_paths(home, project_roots):- try:- config = read_native_config(path, jsonc=True)- except (TakeoverStateError, OSError):- items.append(_blocked_item("opencode", str(path)))+ if path in persisted.problems:+ items.append(_blocked_item(+ "opencode", str(path), persisted.problems[path], source_paths=(str(path),),+ ))continue+ config = persisted.documents[path]if config is None:continueprovider_configs = config.get("provider", {})if not isinstance(provider_configs, dict):- items.append(_blocked_item("opencode", str(path)))+ items.append(_blocked_item("opencode", str(path), source_paths=(str(path),)))continueseen_providers.update(provider_configs)relevant_auth = {key: value for key, value in auth_entries.items() if key in provider_configs}items.extend(_opencode_candidates(provider_configs, relevant_auth, provider_catalog, str(path), mask_credential,+ persisted=persisted, auth_path=opencode_auth_path(home),))unseen_auth = {key: value for key, value in auth_entries.items() if key not in seen_providers}- items.extend(_opencode_candidates({}, unseen_auth, provider_catalog, "auth-file", mask_credential))+ items.extend(_opencode_candidates(+ {}, unseen_auth, provider_catalog, str(opencode_auth_path(home)), mask_credential,+ persisted=persisted, auth_path=opencode_auth_path(home),+ ))return itemsdef _opencode_candidates(provider_configs: dict, auth_entries: dict, provider_catalog: dict,locator: str, mask_credential: Callable[[str], str],+ *, persisted: PersistedInventory, auth_path: Path,) -> list[NativeMigrationItem]:provider_ids = set(provider_configs) | set(auth_entries)items: list[NativeMigrationItem] = []for provider_id in sorted(provider_ids):+ paths = (locator,)+ names: tuple[str, ...] = ()+ auth_names: tuple[str, ...] = ()++ def blocked(identity: str, reason: str = "config") -> NativeMigrationItem:+ return _blocked_item(+ "opencode", identity, reason, source_paths=paths, shell_variables=names,+ shell_auth_variables=auth_names,+ )+if (not isinstance(provider_id, str) or not provider_idor len(provider_id) > 64@@ -739,57 +850,87 @@ def _opencode_candidates(or not provider_id[0].isalnum()or contains_credential_material(provider_id)):- items.append(_blocked_item("opencode", f"{locator}:invalid-provider"))+ items.append(blocked(f"{locator}:invalid-provider"))continueprovider_config = provider_configs.get(provider_id, {})if not isinstance(provider_config, dict):- items.append(_blocked_item("opencode", f"{locator}:{provider_id}"))+ items.append(blocked(f"{locator}:{provider_id}"))continueoptions = provider_config.get("options")if not isinstance(options, dict):options = {}key_setting = options.get("apiKey")- if isinstance(key_setting, str) and (- "{file:" in key_setting- or (- "{env:" in key_setting- and (- re.fullmatch(r"\{env:[^{}]+\}", key_setting) is None- or os.environ.get(key_setting[5:-1])- )- )- ):- items.append(_blocked_item("opencode", f"{locator}:{provider_id}:key-reference", "environment"))+ names = env_references(options)+ auth_names = tuple(dict.fromkeys([+ *env_references(key_setting), *env_references(options.get("headers")),+ ]))+ paths = tuple(dict.fromkeys([+ locator, *(source for name in names for source in persisted.resolve(name).paths),+ *([str(auth_path)] if provider_id in auth_entries else []),+ ]))+ if options.get("headers"):+ items.append(blocked(f"{locator}:{provider_id}:headers", "headers"))continue- config_key = _opencode_plaintext_key(options.get("apiKey"))+ shell_values: list[tuple[str, str]] = []+ config_key = _opencode_plaintext_key(key_setting)auth_entry = auth_entries.get(provider_id, {})if not isinstance(auth_entry, dict):- items.append(_blocked_item("opencode", f"{locator}:{provider_id}"))+ items.append(blocked(f"{locator}:{provider_id}"))continueauth_key = (_opencode_plaintext_key(auth_entry.get("key"))if auth_entry.get("type") == "api"else None)+ key_name = env_reference(key_setting)+ if key_name:+ resolved = persisted.resolve(key_name)+ config_key = (resolved.value.strip() or None) if resolved.value is not None else None+ if resolved.reason or (not config_key and not auth_key):+ items.append(blocked(+ f"{locator}:{provider_id}:key-reference", resolved.reason or "reference",+ ))+ continue+ if config_key:+ shell_values.append((key_name, cast(str, resolved.value)))+ elif isinstance(key_setting, str) and ("{env:" in key_setting or "{file:" in key_setting):+ items.append(blocked(f"{locator}:{provider_id}:key-reference", "reference"))+ continuesecret = config_key or auth_keyif secret is None:if options.get("apiKey") or auth_entry:- items.append(_blocked_item("opencode", f"{locator}:{provider_id}", "credential"))+ items.append(blocked(f"{locator}:{provider_id}", "credential"))continueraw_base_url = options.get("baseURL")base_url = raw_base_url.strip() if isinstance(raw_base_url, str) and raw_base_url.strip() else None+ base_name = env_reference(raw_base_url)+ if base_name:+ resolved = persisted.resolve(base_name)+ if resolved.reason or not resolved.value:+ items.append(blocked(+ f"{locator}:{provider_id}:base-reference", resolved.reason or "reference",+ ))+ continue+ base_url = resolved.value+ shell_values.append((base_name, base_url))+ elif base_url and ("{env:" in base_url or "{file:" in base_url):+ items.append(blocked(f"{locator}:{provider_id}:base-reference", "reference"))+ continuecatalog_provider = provider_catalog.get(provider_id, {})if base_url is None:catalog_api = catalog_provider.get("api")if isinstance(catalog_api, str) and catalog_api.strip():base_url = catalog_api.strip()+ paths = tuple(dict.fromkeys([*paths, str(persisted.catalog_path)]))protocol = _opencode_protocol(provider_id, provider_config, catalog_provider)+ if protocol != _opencode_protocol(provider_id, provider_config, {}):+ paths = tuple(dict.fromkeys([*paths, str(persisted.catalog_path)]))if (protocol is None or provider_id in _OPENCODE_UNSUPPORTED_NATIVE_IDSor (base_url is None and provider_id not in {"anthropic", "openai"})or (auth_entry and auth_entry.get("type") != "api")):- items.append(_blocked_item("opencode", f"{locator}:{provider_id}", "credential"))+ items.append(blocked(f"{locator}:{provider_id}", "credential"))continuemanual_models = _opencode_manual_models(provider_config)action: MigrationAction = "import"@@ -824,6 +965,8 @@ def _opencode_candidates(secret=secret,manual_models=manual_models,masked_credential=masked_secret,+ source_paths=paths, shell_variables=names, shell_values=tuple(shell_values),+ shell_auth_variables=auth_names,))if auth_key and auth_key != secret:@@ -840,16 +983,201 @@ def _opencode_candidates(return items+def _shell_items(+ persisted: PersistedInventory, mask_credential: Callable[[str], str],+ native_items: list[NativeMigrationItem],+) -> list[NativeMigrationItem]:+ """Import literal credentials only with a single, persisted target."""+ items: list[NativeMigrationItem] = []+ specs = (+ ("claude", "ANTHROPIC_API_KEY", "ANTHROPIC_BASE_URL", "anthropic", "anthropic"),+ ("claude", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL", "anthropic", "anthropic"),+ ("claude", "CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_BASE_URL", "anthropic", "anthropic"),+ ("codex", "OPENAI_API_KEY", "OPENAI_BASE_URL", "openai", "openai_responses"),+ ("codex", "CODEX_API_KEY", "OPENAI_BASE_URL", "openai", "openai_responses"),+ ("opencode", "OPENROUTER_API_KEY", None, "openrouter", "openai_chat"),+ )+ for backend, name, base_name, vendor, protocol in specs:+ if any(item.backend == backend and name in item.shell_auth_variables for item in native_items):+ # A native provider reference owns the target and transport.+ continue+ value = persisted.resolve(name)+ # Use the same canonical API-key bytes as proof, reuse and custody.+ # The original assignment remains separate for exact native cleanup.+ secret = value.value.strip() if value.value is not None else None+ if not value.paths or (not secret and not value.reason):+ continue+ base = persisted.resolve(base_name) if base_name else None+ names = (name, *((base_name,) if base and base.paths else ()))+ paths = tuple(dict.fromkeys([*value.paths, *(base.paths if base else ())]))+ reason = value.reason or (base.reason if base else None)+ base_url = base.value if base else None+ if base_url and any(+ profile.values.get(name) and profile.values.get(base_name) != base_url+ for profile in persisted.profiles+ ):+ # A login shell and an interactive shell need not read the same+ # profiles. A key cannot inherit a guessed target from another one.+ reason = reason or "ambiguous_shell"+ scheme = None+ if name == "CLAUDE_CODE_OAUTH_TOKEN":+ reason = reason or "token"+ elif name == "ANTHROPIC_AUTH_TOKEN" and secret and not reason:+ try:+ scheme = validate_api_key_auth_scheme(+ vendor, protocol, base_url, secret, "bearer",+ )+ except ValueError:+ reason = "token"+ if reason or not secret:+ items.append(_blocked_item(+ backend, f"shell:{name}", reason or "reference",+ source_paths=paths, shell_variables=names, shell_auth_variables=(name,),+ ))+ continue+ if vendor == "openrouter":+ base_url = "https://openrouter.ai/api/v1"+ kind = "opencode_provider" if backend == "opencode" else "api_key"+ item_id, source_id = _ids(+ backend, kind, f"shell:{name}", "import",+ _stable_suffix(secret, base_url or "", scheme or ""),+ )+ masked = mask_credential(secret)+ items.append(NativeMigrationItem(+ id=item_id, source_id=source_id, backend=cast(Any, backend),+ kind=cast(Any, kind), masked_detail=masked, proposed_action="import",+ selected=True, notes_key=_CUSTOM_ENDPOINT_NOTE if base_url else None,+ vendor=vendor, protocol=cast(Any, protocol), display_name=vendor,+ base_url=base_url, secret=secret, masked_credential=masked,+ source_paths=paths, shell_variables=names,+ shell_auth_variables=(name,),+ shell_values=(+ (name, cast(str, value.value)),+ *(((base_name, base_url),) if base and base.value else ()),+ ),+ auth_scheme=scheme,+ ))+ return items+++def _surface_surviving_auth_references(+ items: list[NativeMigrationItem], *, home: Path | None,+ project_roots: tuple[Path, ...],+) -> list[NativeMigrationItem]:+ """Preview the same native after images used by the cleanup transaction."""+ selected = [item for item in items if item.proposed_action == "import"]+ auth_names = {+ name for item in selected for name, _ in item.shell_values+ if name in item.shell_auth_variables+ }+ if not auth_names:+ return items+ try:+ edits = {+ edit.path: edit for edit in plan_native_cleanup(+ selected, home=home, project_roots=project_roots, _include_shell=False,+ )+ }+ references = planned_native_references(edits, home=home, project_roots=project_roots)+ except (OSError, TakeoverStateError):+ # Store permissions and unsupported credentials have their own rows.+ # A preview cannot replace the consent-time native-store resolution.+ return items+ return [+ replace(+ item, proposed_action="reauth", selected=False,+ notes_key="settings.models.migration.blocked.reference",+ source_paths=tuple(dict.fromkeys([+ *item.source_paths,+ *(path for name in item.shell_auth_variables if name in references+ for path in references[name]),+ ])),+ ) if (+ item.proposed_action == "import"+ and set(item.shell_auth_variables) & auth_names & references.keys()+ ) else item+ for item in items+ ]+++def _bind_persisted_inventory(+ items: list[NativeMigrationItem], persisted: PersistedInventory,+) -> list[NativeMigrationItem]:+ """Consent includes all consumers of any shell assignment being removed."""+ dependencies: dict[str, set[tuple[str, str]]] = {}+ for item in items:+ dependencies.setdefault(item.backend, set()).update(+ (path, name) for name in item.shell_variables+ for path in persisted.resolve(name).paths+ )+ for backend in dependencies:+ # Unsupported providers still consume their saved references. A+ # selectable peer cannot erase that source by omitting a blocked row.+ dependencies[backend].update(+ (path, name) for name in persisted.references[backend]+ for path in persisted.resolve(name).paths+ )+ linked = {backend: {backend} for backend in dependencies}+ for backend, values in dependencies.items():+ for other, other_values in dependencies.items():+ if values & other_values:+ linked[backend].add(other)+ for _ in linked:+ for backend in linked:+ linked[backend].update(+ member for other in tuple(linked[backend]) for member in linked[other]+ )+ config_paths = set().union(*persisted.backend_paths.values())+ unknown_consumers = [+ row for row in items+ if row.notes_key in {+ "settings.models.migration.blocked.config",+ "settings.models.migration.blocked.unreadable",+ }+ and any(Path(path) in config_paths for path in row.source_paths)+ ]+ result = []+ for item in items:+ closure = tuple(sorted(linked[item.backend]))+ shared = any(dependencies[backend] for backend in closure)+ if shared and unknown_consumers and item.proposed_action == "import":+ item = replace(+ item, proposed_action="reauth", selected=False,+ notes_key=unknown_consumers[0].notes_key,+ source_paths=tuple(dict.fromkeys([+ *item.source_paths,+ *(path for row in unknown_consumers for path in row.source_paths),+ ])),+ )+ guards = persisted.snapshots(item.backend, shared=shared)+ revision = persisted.revision(guards)+ dependency_revision = _stable_suffix(+ *closure,+ *(f"{backend}\0{path}\0{name}" for backend in closure+ for path, name in sorted(dependencies[backend])),+ )+ result.append(replace(+ item,+ id=f"mig_{_stable_suffix(item.id, revision, dependency_revision, item.auth_scheme or '')}",+ required_backends=closure if len(closure) > 1 else (),+ file_snapshots=guards,+ ))+ # A read concurrent with discovery must never bind a different before image.+ for guard in dict.fromkeys(guard for item in result for guard in item.file_snapshots):+ guard.check()+ return result++def _deduplicate_opencode_items(items: list[NativeMigrationItem],validate_base_url: Callable[[object], Optional[str]] | None,) -> list[NativeMigrationItem]:"""One logical credential/target, with consent covering every native copy."""- groups: dict[tuple[str, str, str | None, str], list[NativeMigrationItem]] = {}+ groups: dict[tuple[str, str, str | None, str, str | None], list[NativeMigrationItem]] = {}for item in items:if item.kind == "opencode_provider" and item.proposed_action == "import" and item.secret:target = validate_base_url(item.base_url) if validate_base_url else item.base_url- groups.setdefault((item.vendor, item.protocol, target, item.secret), []).append(item)+ groups.setdefault((item.vendor, item.protocol, target, item.secret, item.auth_scheme), []).append(item)replacements: dict[str, NativeMigrationItem] = {}duplicates: set[str] = set()for group in groups.values():@@ -867,11 +1195,35 @@ def _deduplicate_opencode_items(replacements[first.id] = replace(first, id=f"mig_{revision}", source_id=f"src_{revision}",manual_models=tuple(models.values()),+ source_paths=tuple(dict.fromkeys(path for item in group for path in item.source_paths)),+ shell_variables=tuple(dict.fromkeys(name for item in group for name in item.shell_variables)),+ shell_auth_variables=tuple(dict.fromkeys(+ name for item in group for name in item.shell_auth_variables+ )),+ shell_values=tuple(dict.fromkeys(pair for item in group for pair in item.shell_values)),)duplicates.update(item.id for item in group[1:])return [replacements.get(item.id, item) for item in items if item.id not in duplicates]+def _require_native_api_key_transport(+ item: NativeMigrationItem, *, observed_protocol: str | None = None,+) -> None:+ if item.kind == "oauth_native":+ return+ protocol = observed_protocol if observed_protocol is not None else item.protocol+ try:+ validate_migration_api_key_transport(+ item.vendor, protocol, item.base_url, item.secret, item.auth_scheme,+ )+ if (protocol == "anthropic") != (item.protocol == "anthropic"):+ # None is legacy protocol-auth, not permission to replace a native+ # x-api-key with Bearer (or vice versa) after observation.+ raise ValueError+ except ValueError:+ raise MigrationConflictError from None++def scan_native_configs(config: ModelHubConfig,*,@@ -885,32 +1237,27 @@ def scan_native_configs() -> list[NativeMigrationItem]:"""Read native stores without modifying or deleting any path."""+ persisted = PersistedInventory.read(home, project_roots)items = [*_claude_items(home=home,mask_credential=mask_credential,allow_secret="claude" in secret_backends,project_roots=project_roots,+ persisted=persisted,),*_codex_items(home=home, mask_credential=mask_credential,allow_secret="codex" in secret_backends,project_roots=project_roots,+ persisted=persisted,+ ),+ *_opencode_items(+ home=home, mask_credential=mask_credential, project_roots=project_roots,+ persisted=persisted,),- *_opencode_items(home=home, mask_credential=mask_credential, project_roots=project_roots),]- if home is None:- # The process cannot remove a key from the parent shell or its startup- # files. Do not claim complete native takeover while that precedence- # layer remains active, and never execute a helper to obtain its key.- inherited = {- "claude": ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL", "CLAUDE_CODE_OAUTH_TOKEN"),- "codex": ("OPENAI_API_KEY", "OPENAI_BASE_URL", "CODEX_API_KEY"),- "opencode": ("OPENCODE_CONFIG_CONTENT", "OPENAI_API_KEY", "ANTHROPIC_API_KEY", "OPENROUTER_API_KEY"),- }- for backend, names in inherited.items():- if any(os.environ.get(name) for name in names):- items.append(_blocked_item(backend, "inherited-environment", "environment"))+ items.extend(_shell_items(persisted, mask_credential, items))for backend, auth in (legacy_auth or {}).items():if backend not in {"claude", "codex"}:continue@@ -948,6 +1295,18 @@ def scan_native_configs(continuevalid_items.append(item)items = valid_items+ admitted: list[NativeMigrationItem] = []+ for item in items:+ if item.proposed_action == "import":+ try:+ _require_native_api_key_transport(item)+ except MigrationConflictError:+ item = replace(+ item, proposed_action="reauth", selected=False,+ notes_key="settings.models.migration.blocked.transport",+ )+ admitted.append(item)+ items = admitteditems = _deduplicate_opencode_items(items, validate_base_url)existing_native_sources = {source.vendor: source@@ -956,6 +1315,11 @@ def scan_native_configs(}candidates: list[NativeMigrationItem] = []for item in items:+ # Keep the credential/target identity independent of both consent's+ # full file snapshots and an existing Source's custody placement.+ # Completed receipts can then recognize the exact old grant without+ # accepting old consent after a file changed.+ item = replace(item, receipt_identity=item.id)if (item.native_store_placeholderand (clean_native_stores or {}).get(item.backend) == item.native_store_revision@@ -973,7 +1337,10 @@ def scan_native_configs())continuecandidates.append(item)- return candidates+ candidates = _surface_surviving_auth_references(+ candidates, home=home, project_roots=project_roots,+ )+ return _bind_persisted_inventory(candidates, persisted)def _validated_source(@@ -1073,9 +1440,13 @@ async def _prepare_takeover(consented: list[NativeMigrationItem] | None = None,project_roots: tuple[Path, ...] = (),retained_source_ids: tuple[str, ...] | None = None,+ retained_item_ids: frozenset[str] = frozenset(),clean_native_stores: Mapping[str, str] | None = None,) -> dict[str, Any]:"""Stage all grants and durable before/after images, still native-owned."""+ for item in selected:+ # Recheck before cleanup planning, reuse, proof or credential custody.+ _require_native_api_key_transport(item)clean_native_stores = dict(clean_native_stores or {})cleanup_items = [*selected,@@ -1093,8 +1464,11 @@ async def _prepare_takeover(# A completed receipt can authorize cleanup of the exact old material# reintroduced by an external writer. Its current Hub refs are already# authoritative; never provision that old OAuth snapshot again.- for item in selected if retained_source_ids is None else ():+ for item in selected:+ if item.id in retained_item_ids:+ continueprotocol = item.protocol+ auth_options = {"auth_scheme": item.auth_scheme} if item.auth_scheme is not None else {}observation: SourceObservation | None = Noneexisting = next((source for source in updated.sources if source.id == item.source_id), None)if item.kind != "oauth_native":@@ -1112,15 +1486,17 @@ async def _prepare_takeover(and await host._engine_call(host.adapter.matches_api_key_credential(candidate.credential_ref, item.vendor, protocol,item.secret, validate_base_url(item.base_url),+ **auth_options,))):# Identity reuse is not current authentication proof.# Observe the exact existing target/ref without changing# its inventory, state, or user-owned routes.- await host._require_proven_observation(+ reuse_observation = await host._require_proven_observation(candidate.vendor, candidate.base_url,candidate.credential_ref, (candidate.protocol,),)+ _require_native_api_key_transport(item, observed_protocol=reuse_observation.protocol)_ensure_takeover_placement(updated, candidate, item.backend)source_ids.append(candidate.id)break@@ -1129,8 +1505,9 @@ async def _prepare_takeover("vendor": item.vendor,"base_url": validate_base_url(item.base_url),"key": item.secret,- }, on_reserved=lambda ref: host.revocations.add("observation", ref))+ }, on_reserved=lambda ref: host.revocations.add("observation", ref), **auth_options)protocol = cast(Any, observation.protocol)+ _require_native_api_key_transport(item, observed_protocol=protocol)if observation is None:continueif existing is not None and not (@@ -1162,7 +1539,7 @@ async def _prepare_takeover(else:credential_ref = await host._engine_call(host.adapter.provision_credential(item.vendor, protocol, item.secret, validate_base_url(item.base_url),- on_reserved=reserved,+ on_reserved=reserved, **auth_options,))if not provisioned or provisioned[-1]["credential_ref"] != credential_ref:raise MigrationConflictError@@ -1219,6 +1596,7 @@ async def _prepare_takeover(record = {"version": 1, "phase": "prepared","items": [item.to_payload() for item in (consented or selected)],+ "inventory_ids": [item.receipt_identity or item.id for item in (consented or selected)],"backends": backends, "source_ids": source_ids,"credentials": provisioned,"previous": previous.to_payload(), "updated": updated.to_payload(),@@ -1264,7 +1642,11 @@ async def _revert_takeover(host: MigrationHost, record: dict[str, Any]) -> None:host._save_config(previous)host._engine_synced = Falsefor raw in reversed(record["files"]):- NativeFileEdit.from_payload(raw).apply(reverse=True)+ edit = NativeFileEdit.from_payload(raw)+ # A compare-only guard never owned any bytes. An external edit must+ # stop forward publication, but cannot prevent rollback of our writes.+ if edit.before != edit.after:+ edit.apply(reverse=True)for edit in reversed(record.get("keychain", [])):await asyncio.to_thread(apply_keychain_edit, edit, reverse=True)for credential in reversed(record["credentials"]):@@ -1356,7 +1738,8 @@ async def _resume_takeover(# so a newly observed login can be presented for fresh consent.if (record["source_ids"] or record["credentials"]- or record["files"] or record.get("keychain")+ or any(raw["before"] != raw["after"] for raw in record["files"])+ or record.get("keychain")or record["native_before"] != record["native_after"]):record["phase"] = "exposed"@@ -1412,6 +1795,10 @@ async def _resume_takeover(record.setdefault("validated_source_ids", []).append(credential["source_id"])host.migration_journal.save(record)await _verify_clean_native_stores(host, record)+ # Runtime start is another await boundary. A new saved login or+ # consumer introduced there must not disappear behind a receipt.+ for raw in record["files"]:+ NativeFileEdit.from_payload(raw).check(applied=True)if invalid_source_ids:terminal_config = host._clone_config(updated)for source in terminal_config.sources:@@ -1507,10 +1894,30 @@ async def apply_native_migration(if len(selected) != len(item_ids) or any(item.proposed_action != "import" for item in selected):raise MigrationConflictErrorbackends = tuple(sorted({item.backend for item in selected}))+ if any(set(item.required_backends) - set(backends) for item in selected):+ raise MigrationConflictError# A CLI takeover cannot leave an unselected credential maintaining its# original authentication. Selection is therefore grouped by backend.if any(item.backend in backends and item.id not in item_ids for item in available):raise MigrationConflictError+ retained_inventory_ids = (+ set(record.get("inventory_ids", [item["id"] for item in record["items"]]))+ & {item.receipt_identity or item.id for item in selected}+ if record is not None and record["phase"] == "complete" else set()+ )+ previous_oauth_backends = (+ host.migration_journal.oauth_custody_backends(record)+ if record is not None and record["phase"] == "complete" else frozenset()+ )+ if retained_inventory_ids:+ # Fresh file-bound consent may describe the same old grant that+ # an external writer restored after cleanup. Keep the current Hub+ # credential rather than provisioning/refreshing that old grant.+ # Before file-bound IDs existed, receipt item IDs already were+ # these opaque inventory IDs; preserve that exact legacy proof.+ # Consent can add or omit other backends. Grant ownership is not+ # conditional on the entire selection being repeated.+ completed_record = recordasync with host.migration_guard(backends) as verify_idle:async with host._mutation_lock:if completed_record is not None:@@ -1529,6 +1936,7 @@ async def apply_native_migration()consented = selectedselected = []+ retained_item_ids: set[str] = set()clean_native_stores: dict[str, str] = {}for original in consented:resolved = [@@ -1575,7 +1983,19 @@ async def apply_native_migration(for item in resolved):raise MigrationConflictError+ if (+ original.backend in previous_oauth_backends+ and (original.receipt_identity or original.id) not in retained_inventory_ids+ and any(item.kind == "oauth_native" for item in resolved)+ ):+ # Snapshot/revision changes cannot distinguish new+ # authorization from an old refresh token copied into+ # an edited container. Only Hub reauthentication can+ # establish a fresh grant; never reprovision by guess.+ raise MigrationReauthorizationRequiredErrorselected.extend(item for item in resolved if item not in selected)+ if (original.receipt_identity or original.id) in retained_inventory_ids:+ retained_item_ids.update(item.id for item in resolved)if any(item.backend in backends and item not in selected for item in rescanned):raise MigrationConflictErrorrecord = await _prepare_takeover(@@ -1587,6 +2007,7 @@ async def apply_native_migration(tuple(completed_record["source_ids"])if completed_record is not None else None),+ retained_item_ids=frozenset(retained_item_ids),clean_native_stores=clean_native_stores,)return await _resume_takeover(host, record, verify_idle)diff --git a/core/handlers/model_hub/migration_files.py b/core/handlers/model_hub/migration_files.pyindex 5b70bc713..33a975a4b 100644--- a/core/handlers/model_hub/migration_files.py+++ b/core/handlers/model_hub/migration_files.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport jsonimport os+import refrom pathlib import Pathfrom typing import TYPE_CHECKING@@ -31,6 +32,52 @@ from .migration_journal import NativeFileEdit, TakeoverStateError, _read_regularif TYPE_CHECKING:from .migration import NativeMigrationItem++class NativeReferenceError(TakeoverStateError):+ """A planned native configuration still consumes selected authentication."""++ def __init__(self, references: dict[str, tuple[str, ...]]) -> None:+ super().__init__("native configuration still references selected authentication")+ self.references = references+++def env_reference(value: object) -> str | None:+ if not isinstance(value, str):+ return None+ match = re.fullmatch(r"\{env:([A-Za-z_][A-Za-z_0-9]*)\}", value)+ return match[1] if match else None+++def env_references(value: object) -> tuple[str, ...]:+ """Inventory references even in unsupported header templates."""+ if isinstance(value, str):+ return tuple(re.findall(r"\{env:([A-Za-z_][A-Za-z_0-9]*)\}", value))+ if isinstance(value, dict):+ return tuple(dict.fromkeys(name for child in value.values() for name in env_references(child)))+ if isinstance(value, list):+ return tuple(dict.fromkeys(name for child in value for name in env_references(child)))+ return ()+++def native_config_references(backend: str, payload: dict) -> frozenset[str]:+ """The native interpreters' references, independent of credential rows."""+ if backend == "opencode":+ return frozenset(env_references(payload))+ names: set[str] = set()+ if backend == "codex":+ providers = payload.get("model_providers", {})+ for provider in providers.values() if isinstance(providers, dict) else ():+ if not isinstance(provider, dict):+ continue+ name = provider.get("env_key")+ if isinstance(name, str) and name:+ names.add(name)+ headers = provider.get("env_http_headers", {})+ if isinstance(headers, dict):+ names.update(value for value in headers.values() if isinstance(value, str))+ return frozenset(names)++def _object(content: bytes, *, jsonc: bool = False) -> dict:try:result = parse_jsonc_object(content.decode()) if jsonc else json.loads(content)@@ -82,6 +129,14 @@ def opencode_auth_path(home: Path | None) -> Path:return get_opencode_auth_path(home)+def opencode_catalog_path(home: Path | None) -> Path:+ if home is not None:+ return home / ".cache/opencode/models.json"+ cache_home = os.environ.get("XDG_CACHE_HOME")+ root = Path(cache_home).expanduser() if cache_home else Path.home() / ".cache"+ return root / "opencode/models.json"++def codex_config_paths(home: Path | None, projects: tuple[Path, ...]) -> tuple[Path, ...]:config_path, _ = get_codex_config_paths(home)return tuple(dict.fromkeys([config_path.absolute(), *(root / ".codex/config.toml" for root in projects)]))@@ -102,11 +157,46 @@ def read_native_config(path: Path, *, jsonc: bool = False) -> dict | None:return None if content is None else _object(content, jsonc=jsonc)+def planned_native_references(+ edits: dict[Path, NativeFileEdit], *, home: Path | None,+ project_roots: tuple[Path, ...] = (),+) -> dict[str, tuple[str, ...]]:+ """Read the exact after images, including layers with no migration row.++ Unselected layers become no-op guards so a new consumer introduced during+ proof cannot invalidate the decision to remove a shell assignment.+ """+ references: dict[str, list[str]] = {}+ for backend, paths in (+ ("claude", claude_settings_paths(home, project_roots)),+ ("codex", codex_config_paths(home, project_roots)),+ ("opencode", opencode_config_paths(home, project_roots)),+ ):+ for path in paths:+ if path not in edits:+ before = _read_regular(path)+ edits[path] = NativeFileEdit(path, before, before)+ content = edits[path].after+ if content is None:+ continue+ try:+ payload = (+ tomllib.loads(content.decode()) if backend == "codex"+ else _object(content, jsonc=backend == "opencode")+ )+ except (ValueError, UnicodeError):+ raise TakeoverStateError("native configuration cannot be parsed") from None+ for name in native_config_references(backend, payload):+ references.setdefault(name, []).append(str(path))+ return {name: tuple(paths) for name, paths in references.items()}++def plan_native_cleanup(items: list[NativeMigrationItem],*,home: Path | None,project_roots: tuple[Path, ...] = (),+ _include_shell: bool = True,) -> list[NativeFileEdit]:"""One before/after image per path, even when providers share a file.@@ -115,6 +205,15 @@ def plan_native_cleanup(document after a parse failure."""edits: dict[Path, NativeFileEdit] = {}+ selected_backends = {item.backend for item in items}+ if any(set(item.required_backends) - selected_backends for item in items):+ raise TakeoverStateError("shared native credentials require joint consent")+ for item in items:+ for guard in item.file_snapshots:+ if guard.path in edits and edits[guard.path] != guard:+ raise TakeoverStateError("conflicting native credential snapshots")+ guard.check()+ edits[guard.path] = guard# Native-store resolution owns credential paths (including isolated secure# roots), while this planner owns one final file image for the whole batch.for item in items:@@ -129,8 +228,10 @@ def plan_native_cleanup(before["raw"].encode() if before["exists"] else None,after["raw"].encode() if after["exists"] else None,)- if path in edits and edits[path] != edit:- raise TakeoverStateError("conflicting native credential snapshots")+ if path in edits:+ previous = edits[path]+ if previous.before != edit.before or (previous.after != previous.before and previous != edit):+ raise TakeoverStateError("conflicting native credential snapshots")edits[path] = editdef edit_json(path: Path, transform, *, jsonc: bool = False, guard_unchanged: bool = False) -> None:@@ -157,6 +258,13 @@ def plan_native_cleanup(# any credential. It cannot authorize cleanup of another native store.backends = {item.backend for item in items if not item.native_store_placeholder}selected_secrets = {item.secret for item in items if item.secret}++ def selected_api_key(value: object) -> bool:+ # Producers normalize static keys for proof/custody. Exact raw bytes+ # remain in the checked snapshots and journal before-images; this+ # comparison does not replace their consent or concurrency checks.+ return isinstance(value, str) and value.strip() in selected_secrets+if "claude" in backends:def clear_settings(payload: dict) -> None:env = payload.get("env")@@ -164,18 +272,25 @@ def plan_native_cleanup(raise TakeoverStateError("native configuration cannot be parsed")if isinstance(env, dict):for key in ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL", "CLAUDE_CODE_OAUTH_TOKEN"):- if key != "ANTHROPIC_BASE_URL" and env.get(key) and env[key] not in selected_secrets:- raise TakeoverStateError("another native credential requires migration")+ value = env.get(key)+ if key != "ANTHROPIC_BASE_URL" and value:+ selected = (+ selected_api_key(value)+ if key in {"ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN"}+ else isinstance(value, str) and value in selected_secrets+ )+ if not selected:+ raise TakeoverStateError("another native credential requires migration")env.pop(key, None)if payload.get("apiKeyHelper"):raise TakeoverStateError("native credential helper requires configuration")payload.pop("apiKeyHelper", None)for path in claude_settings_paths(home, project_roots):- edit_json(path, clear_settings)+ edit_json(path, clear_settings, guard_unchanged=True)backup_path = get_claude_oauth_settings_backup_path(home).absolute()backup_edit = edits.get(backup_path)- if backup_edit and backup_edit.after is not None:+ if backup_edit and backup_edit.before != backup_edit.after and backup_edit.after is not None:backup = _object(backup_edit.after)if set(backup) <= {"version", "env"} and not backup.get("env"):edits[backup_path] = NativeFileEdit(backup_path, backup_edit.before, None)@@ -184,7 +299,7 @@ def plan_native_cleanup(config_path, auth_path = get_codex_config_paths(home)def clear_auth(payload: dict) -> None:- if payload.get("OPENAI_API_KEY") and payload["OPENAI_API_KEY"] not in selected_secrets:+ if payload.get("OPENAI_API_KEY") and not selected_api_key(payload["OPENAI_API_KEY"]):raise TakeoverStateError("another native credential requires migration")if payload.get("tokens") and not any(item.backend == "codex" and item.kind == "oauth_native"@@ -195,9 +310,12 @@ def plan_native_cleanup(for key in ("OPENAI_API_KEY", "tokens", "auth_mode", "last_refresh"):payload.pop(key, None)- edit_json(auth_path, clear_auth)+ edit_json(auth_path, clear_auth, guard_unchanged=True)for config_path in codex_config_paths(home, project_roots):content = _read_regular(config_path)+ if config_path in edits and content != edits[config_path].before:+ raise TakeoverStateError("native configuration changed")+ edits.setdefault(config_path, NativeFileEdit(config_path, content, content))if content is None:continuetry:@@ -213,7 +331,7 @@ def plan_native_cleanup(provider = providers.get(provider_id)if not isinstance(provider, dict):continue- if provider.get("experimental_bearer_token") and provider["experimental_bearer_token"] not in selected_secrets:+ if provider.get("experimental_bearer_token") and not selected_api_key(provider["experimental_bearer_token"]):raise TakeoverStateError("another native credential requires migration")# Retain user labels, capabilities, and timeout preferences.for key in ("base_url", "env_key", "experimental_bearer_token", "requires_openai_auth"):@@ -243,6 +361,7 @@ def plan_native_cleanup(if "opencode" in backends:vendors = {item.vendor for item in items if item.backend == "opencode"}+ shell_values = dict(pair for item in items for pair in item.shell_values)def clear_providers(payload: dict) -> None:providers = payload.get("provider")@@ -257,15 +376,18 @@ def plan_native_cleanup(options = provider.get("options")if isinstance(options, dict):value = options.get("apiKey")- if value and value not in selected_secrets:- # An unset environment placeholder supplied no native- # credential; an auth.json fallback was the imported key.- unresolved = (+ if value and not selected_api_key(value):+ # The inventory bound the saved assignment, or proved+ # its absence before selecting the auth.json fallback.+ reference = (isinstance(value, str)and value.startswith("{env:") and value.endswith("}")- and not os.environ.get(value[5:-1])+ and (+ not shell_values.get(value[5:-1])+ or shell_values[value[5:-1]].strip() in selected_secrets+ ))- if not unresolved:+ if not reference:raise TakeoverStateError("another native credential requires migration")options.pop("apiKey", None)options.pop("baseURL", None)@@ -280,4 +402,36 @@ def plan_native_cleanup(payload.pop(vendor, None)edit_json(opencode_auth_path(home), clear_provider_auth, guard_unchanged=True)+ if backends and _include_shell:+ from .migration_shell import cleanup_shell_profile, read_shell_profiles++ selected_values: dict[str, str] = {}+ for item in items:+ for name, value in item.shell_values:+ if name in selected_values and selected_values[name] != value:+ raise TakeoverStateError("conflicting native credential snapshots")+ selected_values[name] = value+ if selected_values:+ references = planned_native_references(edits, home=home, project_roots=project_roots)+ auth_names = {name for item in items for name in item.shell_auth_variables}+ live_auth = {+ name: references[name]+ for name in selected_values.keys() & auth_names & references.keys()+ }+ if live_auth:+ raise NativeReferenceError(live_auth)+ # A Base URL may still serve a provider that had no credential to+ # import. Retain its assignment, even in a selected backend.+ selected_values = {+ name: value for name, value in selected_values.items()+ if name not in references+ }+ for profile in read_shell_profiles(home, frozenset(selected_values)):+ previous = edits.get(profile.path)+ if previous and (previous.before != profile.before or previous.mode != profile.mode):+ raise TakeoverStateError("native configuration changed")+ # Keep guards even when this profile contributes no selected line.+ edits[profile.path] = cleanup_shell_profile(profile, selected_values)+ for edit in edits.values():+ edit.check()return list(edits.values())diff --git a/core/handlers/model_hub/migration_journal.py b/core/handlers/model_hub/migration_journal.pyindex 0ccdd2d72..ff41efb0d 100644--- a/core/handlers/model_hub/migration_journal.py+++ b/core/handlers/model_hub/migration_journal.py@@ -54,12 +54,13 @@ class NativeFileEdit:before: bytes | Noneafter: bytes | Nonemode: int = 0o600+ before_mode: int | None = None@classmethoddef plan(cls, path: Path, after: bytes | None) -> NativeFileEdit:before = _read_regular(path)mode = stat.S_IMODE(path.stat().st_mode) if before is not None else 0o600- return cls(path.absolute(), before, after, mode)+ return cls(path.absolute(), before, after, mode, mode if before is not None and before != after else None)def to_payload(self) -> dict[str, Any]:return {@@ -67,14 +68,20 @@ class NativeFileEdit:"before": _encoded(self.before),"after": _encoded(self.after),"mode": self.mode,+ **({"before_mode": self.before_mode} if self.before_mode is not None else {}),}@classmethoddef from_payload(cls, payload: object) -> NativeFileEdit:- if not isinstance(payload, dict) or set(payload) != {"path", "before", "after", "mode"}:+ required = {"path", "before", "after", "mode"}+ if (+ not isinstance(payload, dict) or not required <= set(payload)+ or set(payload) - required - {"before_mode"}+ ):raise TakeoverStateError("invalid takeover snapshot")path = payload["path"]mode = payload["mode"]+ before_mode = payload.get("before_mode")if (not isinstance(path, str)or not Path(path).is_absolute()@@ -82,23 +89,38 @@ class NativeFileEdit:or isinstance(mode, bool)or mode < 0or mode > 0o777+ or (+ before_mode is not None and (+ not isinstance(before_mode, int) or isinstance(before_mode, bool)+ or not 0 <= before_mode <= 0o777+ )+ )):raise TakeoverStateError("invalid takeover snapshot")- return cls(Path(path), _decoded(payload["before"]), _decoded(payload["after"]), mode)+ return cls(Path(path), _decoded(payload["before"]), _decoded(payload["after"]), mode, before_mode)def check(self, *, applied: bool = False) -> None:expected = self.after if applied else self.before- if _read_regular(self.path) != expected:- raise TakeoverStateError("native configuration changed")+ mode = (self.mode if applied else self.before_mode) if self.before != self.after else None+ self._verify(expected, mode)def apply(self, *, reverse: bool = False) -> None:"""Compare before writing; replay accepts only either recorded state."""source, target = (self.after, self.before) if reverse else (self.before, self.after)+ target_mode = self.before_mode if reverse and self.before_mode is not None else self.modeactual = _read_regular(self.path)if actual == target:+ if self.before != self.after:+ if target is not None:+ # Bytes and mode were published together. Replay may complete+ # durability, but never chmod someone else's replacement.+ self._verify(target, self.before_mode if reverse else self.mode, sync=True)+ _fsync_directory(self.path.parent)returnif actual != source:raise TakeoverStateError("native configuration changed")+ source_mode = self.mode if reverse else self.before_mode+ self._verify(source, source_mode)if target is None:self.path.unlink()# The absence is part of the ownership decision, not best effort.@@ -106,12 +128,41 @@ class NativeFileEdit:raise TakeoverStateError("native cleanup did not persist")_fsync_directory(self.path.parent)return- self.path.parent.mkdir(parents=True, exist_ok=True)- # Authentication-bearing files must never become more permissive.- write_atomic(self.path, target)+ # Keep one publication owner. The caller owns source consent and+ # strict durability; the primitive publishes captured mode WITH bytes.+ write_atomic(+ self.path, target, mode=target_mode,+ before_replace=lambda: self._verify(source, source_mode),+ )_fsync_directory(self.path.parent)- if _read_regular(self.path) != target:- raise TakeoverStateError("native configuration did not persist")+ self._verify(target, target_mode)++ def _verify(self, expected: bytes | None, mode: int | None, *, sync: bool = False) -> None:+ if expected is None:+ if _read_regular(self.path) is not None:+ raise TakeoverStateError("native configuration changed")+ return+ descriptor = os.open(+ self.path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0),+ )+ try:+ current = os.fstat(descriptor)+ if not stat.S_ISREG(current.st_mode):+ raise TakeoverStateError("native configuration is not a regular file")+ with os.fdopen(descriptor, "rb", closefd=False) as handle:+ content = handle.read()+ current = os.fstat(descriptor)+ entry = self.path.lstat()+ if (+ content != expected+ or (current.st_dev, current.st_ino) != (entry.st_dev, entry.st_ino)+ or (mode is not None and stat.S_IMODE(current.st_mode) != mode)+ ):+ raise TakeoverStateError("native configuration changed")+ if sync:+ os.fsync(descriptor)+ finally:+ os.close(descriptor)def _fsync_directory(path: Path) -> None:@@ -127,9 +178,40 @@ def _fsync_directory(path: Path) -> None:class NativeTakeoverJournal:"""One controller-owned transaction, plus the last idempotency receipt."""+ _OAUTH_BACKENDS = frozenset({"claude", "codex"})+def __init__(self, path: Path):self.path = path+ @classmethod+ def oauth_custody_backends(cls, record: dict[str, Any] | None) -> frozenset[str]:+ """Require proof or reauthorization without retaining older grants."""+ if record is None:+ return frozenset()+ if "oauth_custody_backends" in record:+ marker = record["oauth_custody_backends"]+ if (+ not isinstance(marker, list)+ or any(not isinstance(value, str) or value not in cls._OAUTH_BACKENDS for value in marker)+ or len(set(marker)) != len(marker)+ ):+ raise TakeoverStateError("invalid takeover custody evidence")+ backends = set(marker)+ elif record["phase"] == "complete":+ # An old last receipt cannot exclude overwritten batches, even+ # when its own batch was empty. Absence is not explicit [].+ backends = set(cls._OAUTH_BACKENDS)+ else:+ backends = set()+ if record["source_ids"]:+ # A mixed opaque container/Source bundle cannot prove which+ # container was empty. Keep the existing conservative policy.+ backends.update(+ item["backend"] for item in record["items"]+ if item.get("kind") == "oauth_native" and item["backend"] in cls._OAUTH_BACKENDS+ )+ return frozenset(backends)+def _prepare(self) -> None:self.path.parent.mkdir(parents=True, mode=0o700, exist_ok=True)mode = self.path.parent.lstat().st_mode@@ -166,6 +248,14 @@ class NativeTakeoverJournal:or item.get("backend") not in payload["backends"]for item in payload["items"])+ or (+ "inventory_ids" in payload+ and (+ not isinstance(payload["inventory_ids"], list)+ or len(payload["inventory_ids"]) != len(payload["items"])+ or any(not isinstance(value, str) or not value for value in payload["inventory_ids"])+ )+ )or not isinstance(payload.get("clean_native_stores", {}), dict)or any(backend not in {"claude", "codex"}@@ -259,9 +349,12 @@ class NativeTakeoverJournal:raise TakeoverStateError("invalid takeover terminal configuration")elif payload.get("outcome", "success") not in {"success", "needs_auth", "reauth_requested"}:raise TakeoverStateError("invalid takeover receipt")+ self.oauth_custody_backends(payload)return payloaddef save(self, payload: dict[str, Any]) -> None:+ if "oauth_custody_backends" in payload:+ self.oauth_custody_backends(payload)self._prepare()if self.path.exists():self.load() # Refuse unsafe/corrupt state, never silently overwrite.@@ -280,9 +373,11 @@ class NativeTakeoverJournal:def complete(self, record: dict[str, Any]) -> None:receipt = NativeTakeoverJournal(self.path.with_name("last-completed.json"))+ previous = receipt.load()+ custody_backends = self.oauth_custody_backends(previous) | self.oauth_custody_backends(record)clean_stores = {backend: revision- for backend, revision in (receipt.load() or {}).get("clean_native_stores", {}).items()+ for backend, revision in (previous or {}).get("clean_native_stores", {}).items()if backend not in record["backends"]}clean_stores.update(record.get("clean_native_stores", {}))@@ -290,6 +385,7 @@ class NativeTakeoverJournal:"version": 1,"phase": "complete","items": record["items"],+ **({"inventory_ids": record["inventory_ids"]} if "inventory_ids" in record else {}),"backends": record["backends"],"source_ids": record["source_ids"],"outcome": (@@ -303,6 +399,7 @@ class NativeTakeoverJournal:if source["id"] in record["source_ids"]},"clean_native_stores": clean_stores,+ "oauth_custody_backends": sorted(custody_backends),})self.forget()diff --git a/core/handlers/model_hub/migration_persisted.py b/core/handlers/model_hub/migration_persisted.pynew file mode 100644index 000000000..d94790a06--- /dev/null+++ b/core/handlers/model_hub/migration_persisted.py@@ -0,0 +1,166 @@+"""One persisted inventory for credential references and consent snapshots."""++from __future__ import annotations++import hashlib+from dataclasses import dataclass, field+from pathlib import Path++from vibe.native_oauth_store import native_credentials_paths++from .migration_files import (+ _object,+ claude_settings_paths,+ codex_config_paths,+ native_config_references,+ opencode_auth_path,+ opencode_catalog_path,+ opencode_config_paths,+ tomllib,+)+from .migration_journal import NativeFileEdit, TakeoverStateError, _read_regular+from .migration_shell import ShellProfile, read_shell_profiles++BUILTIN_NAMES = frozenset({+ "ANTHROPIC_API_KEY", "ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN",+ "CLAUDE_CODE_OAUTH_TOKEN", "OPENAI_API_KEY", "CODEX_API_KEY",+ "OPENAI_BASE_URL", "OPENROUTER_API_KEY",+})+++@dataclass(frozen=True, repr=False)+class PersistedValue:+ value: str | None = None+ paths: tuple[str, ...] = ()+ reason: str | None = None+++@dataclass(repr=False)+class PersistedInventory:+ profiles: tuple[ShellProfile, ...]+ guards: tuple[NativeFileEdit, ...]+ documents: dict[Path, dict | None]+ problems: dict[Path, str]+ backend_paths: dict[str, tuple[Path, ...]]+ credential_paths: dict[str, tuple[Path, ...]]+ references: dict[str, frozenset[str]]+ catalog_path: Path+ _values: dict[str, PersistedValue] = field(default_factory=dict)++ @classmethod+ def read(cls, home: Path | None, projects: tuple[Path, ...]) -> PersistedInventory:+ backend_paths = {+ "claude": claude_settings_paths(home, projects),+ "codex": codex_config_paths(home, projects),+ "opencode": opencode_config_paths(home, projects),+ }+ documents: dict[Path, dict | None] = {}+ problems: dict[Path, str] = {}+ guards: dict[Path, NativeFileEdit] = {}+ references: dict[str, set[str]] = {backend: set() for backend in backend_paths}+ for backend, paths in backend_paths.items():+ for path in paths:+ try:+ before = _read_regular(path)+ guards[path] = NativeFileEdit(path, before, before)+ except (OSError, TakeoverStateError):+ problems[path] = "unreadable"+ continue+ try:+ payload = (+ None if before is None else+ tomllib.loads(before.decode()) if backend == "codex" else+ _object(before, jsonc=backend == "opencode")+ )+ documents[path] = payload+ except (ValueError, UnicodeError, TakeoverStateError):+ problems[path] = "config"+ continue+ if not payload:+ continue+ references[backend].update(native_config_references(backend, payload))+ # Native stores still own their read/permission policy. These snapshots+ # only bind file changes; no OS credential API is invoked here.+ credential_paths = {+ "claude": tuple(path.absolute() for path in native_credentials_paths("claude", home)),+ "codex": tuple(path.absolute() for path in native_credentials_paths("codex", home)),+ "opencode": (opencode_auth_path(home).absolute(),),+ }+ for path in dict.fromkeys(path for paths in credential_paths.values() for path in paths):+ path = path.absolute()+ try:+ before = _read_regular(path)+ guards[path] = NativeFileEdit(path, before, before)+ except (OSError, TakeoverStateError):+ problems[path] = "unreadable"+ catalog_path = opencode_catalog_path(home).absolute()+ try:+ before = _read_regular(catalog_path)+ guards[catalog_path] = NativeFileEdit(catalog_path, before, before)+ documents[catalog_path] = _object(before) if before else {}+ except (OSError, TakeoverStateError):+ # The optional catalog is not an authentication store. Its+ # existing loader degrades to no catalog when it is unreadable.+ documents[catalog_path] = {}+ names = BUILTIN_NAMES | frozenset().union(*references.values())+ return cls(+ read_shell_profiles(home, names), tuple(guards.values()), documents,+ problems, backend_paths, credential_paths,+ {backend: frozenset(names) for backend, names in references.items()},+ catalog_path,+ )++ def resolve(self, name: str) -> PersistedValue:+ if name in self._values:+ return self._values[name]+ paths: list[str] = []+ values: set[str] = set()+ reasons: list[str] = []+ for profile in self.profiles:+ relevant = [issue for issue in profile.issues if name in issue.names or not issue.names]+ if relevant or any(assignment.name == name for assignment in profile.assignments):+ paths.append(str(profile.path))+ reasons.extend(issue.reason for issue in relevant)+ value = profile.values.get(name)+ if value is not None:+ values.add(value)+ reason = next(+ (reason for reason in ("unreadable", "dynamic_shell", "ambiguous_shell") if reason in reasons),+ "ambiguous_shell" if len(values) > 1 else None,+ )+ result = PersistedValue(+ next(iter(values)) if len(values) == 1 and reason is None else None,+ tuple(dict.fromkeys(paths)), reason,+ )+ self._values[name] = result+ return result++ def snapshots(self, backend: str, *, shared: bool = False) -> tuple[NativeFileEdit, ...]:+ paths = {*self.backend_paths[backend], *self.credential_paths[backend]}+ if backend == "opencode":+ paths.add(self.catalog_path)+ if shared:+ # Only native configuration can introduce another shell consumer.+ # Sharing guards is not consent to journal other backends' grants.+ paths.update(path for group in self.backend_paths.values() for path in group)+ guards = tuple(+ guard for guard in self.guards+ if guard.path in paths+ )+ return (+ *guards,+ *(NativeFileEdit(profile.path, profile.before, profile.before, profile.mode) for profile in self.profiles+ if not any(issue.reason == "unreadable" for issue in profile.issues)),+ )++ @staticmethod+ def revision(guards: tuple[NativeFileEdit, ...]) -> str:+ digest = hashlib.sha256()+ for guard in guards:+ digest.update(str(guard.path).encode())+ digest.update(f"\0mode:{guard.mode:o}\0".encode())+ digest.update(+ b"\0absent\0" if guard.before is None+ else b"\0present\0" + hashlib.sha256(guard.before).digest()+ )+ return digest.hexdigest()diff --git a/core/handlers/model_hub/migration_shell.py b/core/handlers/model_hub/migration_shell.pynew file mode 100644index 000000000..185bf8a68--- /dev/null+++ b/core/handlers/model_hub/migration_shell.py@@ -0,0 +1,1399 @@+"""Inventory literal shell assignments without interpreting a startup script.++Only complete, top-level assignment lines are removable. This is an inventory+of stored values, not a model of shell execution or effective precedence.+Includes and ordinary commands are not followed. The small lexer exists to+keep quoted text, compound commands and here-documents out of the literal path.+"""++from __future__ import annotations++import re+from collections.abc import Callable, Iterator, Mapping+from dataclasses import dataclass, replace+from pathlib import Path+from typing import TYPE_CHECKING++if TYPE_CHECKING:+ from .migration_journal import NativeFileEdit+++SHELL_PROFILE_NAMES = (+ ".profile", ".bash_profile", ".bash_login", ".bashrc",+ ".zshenv", ".zprofile", ".zshrc", ".zlogin",+)++_NAME = r"[a-zA-Z_][a-zA-Z_0-9]*"+_ASSIGNMENT = re.compile(rf"^({_NAME})=")+_WRITER = re.compile(rf"^({_NAME})(?:\[[^]]*\])?\+?=")+_OPERATORS = ("<<<", "<<-", "&&", "||", ";;", ";&", "<<", ">>", "<&", ">&", "<>", ">|", "|&", "((", "))")+_SEPARATORS = {";", ";;", ";&", "&", "&&", "||", "|", "|&", "(", ")", "((", "))"}+_DECLARATIONS = {"export", "readonly", "declare", "typeset", "local"}+_ZSH_DECLARATIONS = {+ # Flags, optional numeric options, implicit numeric attribute. The aliases+ # share bin_typeset, but not its entire option alphabet.+ "typeset": ("AHTUafghklmrtuxz", "EFLRZip", ""),+ "declare": ("AHTUafghklmrtuxz", "EFLRZip", ""),+ "local": ("AHTUahlrtux", "EFLRZip", ""),+ "export": ("HTUafhlrtu", "EFLRZip", ""),+ "readonly": ("AHTUafghlptux", "EFLRZi", ""),+ "integer": ("Hghlrtux", "LRZip", "i"),+ "float": ("Hghlrtux", "EFLRZp", "E"),+}+_COMMAND_PREFIXES = {"if", "then", "elif", "else", "while", "until", "do", "!", "time", "always"}+_ZSH_PROFILES = frozenset({".zshenv", ".zprofile", ".zshrc", ".zlogin"})+_DYNAMIC_STARTS = "$`~{*?["+# Src/options.c zshletters, plus bin_set's own -s sorting option. -A/-o+# consume values; -b/-c are not ordinary set options.+_ZSH_OPTION_FLAGS = "0123456789BCDEFGHIJKLMNOPQRSTUVWXYZadefghiklmnprstuvwxy"+++@dataclass(frozen=True, repr=False)+class ShellAssignment:+ name: str+ value: str+ start: int+ end: int+++@dataclass(frozen=True, repr=False)+class ShellIssue:+ names: tuple[str, ...]+ reason: str+ line: int | None+++@dataclass(frozen=True, repr=False)+class ShellProfile:+ path: Path+ before: bytes | None+ assignments: tuple[ShellAssignment, ...]+ issues: tuple[ShellIssue, ...]+ mode: int = 0o600++ @property+ def values(self) -> dict[str, str]:+ invalid = {name for issue in self.issues for name in issue.names}+ values: dict[str, str] = {}+ for assignment in self.assignments:+ if assignment.name in values and values[assignment.name] != assignment.value:+ invalid.add(assignment.name)+ values[assignment.name] = assignment.value+ return {name: value for name, value in values.items() if name not in invalid}+++@dataclass(frozen=True, repr=False)+class _Token:+ raw: str+ value: str+ literal: bool = True+ operator: bool = False+ expansions: tuple[tuple[str, str], ...] = ()+ fd_name: str | None = None++ def is_code(self, word: str) -> bool:+ return self.raw == word+++@dataclass(frozen=True, repr=False)+class _Statement:+ tokens: tuple[_Token, ...]+ start: int+ end: int+ line: int+ data: bool = False+++def _quoted_end(text: str, start: int, quote: str) -> int:+ index = start + 1+ while index < len(text):+ if text[index] == quote:+ return index + 1+ if text[index] == "\\" and quote != "'":+ index += 1+ index += 1+ return len(text)+++def _group_end(text: str, start: int, opening: str, closing: str) -> int:+ """Bound an expansion, never evaluate its contents."""+ depth = 1+ index = start + 1+ while index < len(text):+ char = text[index]+ if char in "'\"`":+ index = _quoted_end(text, index, char)+ continue+ if char == "\\":+ index += 2+ continue+ if char == opening:+ depth += 1+ elif char == closing:+ depth -= 1+ if not depth:+ return index + 1+ index += 1+ return len(text)+++def _word(text: str, start: int) -> tuple[_Token, int]:+ value: list[str] = []+ expansions: list[tuple[str, str]] = []+ literal = True+ quote: str | None = None+ index = start+ while index < len(text):+ char = text[index]+ if quote is None and (char in " \t\n;&|()<>" or text.startswith("\r\n", index)):+ break+ if char == quote:+ quote = None+ index += 1+ continue+ if quote == "'":+ value.append(char)+ index += 1+ continue+ if char in "'\"" and quote is None:+ quote = char+ index += 1+ continue+ if char == "\\":+ if index + 1 == len(text):+ literal = False+ index += 1+ continue+ following = text[index + 1]+ if text.startswith("\\\r\n", index):+ index += 3+ continue+ if quote == '"' and following not in '$`"\\\n':+ value.append("\\")+ index += 1+ continue+ if following != "\n":+ value.append(following)+ index += 2+ continue+ if char == "$" and index + 1 < len(text) and text[index + 1] in "({":+ opening = text[index + 1]+ closing = ")" if opening == "(" else "}"+ end = _group_end(text, index + 1, opening, closing)+ closed = text[end - 1:end] == closing+ body = text[index + 2:end - 1 if closed else end]+ kind = "command" if opening == "(" else "parameter"+ if opening == "(" and body.startswith("("):+ kind = "arithmetic"+ expansions.append((kind, body))+ value.append(text[index:end])+ literal = False+ index = end+ continue+ if char == "`":+ end = _quoted_end(text, index, "`")+ expansions.append(("command", text[index + 1:end - 1]))+ value.append(text[index:end])+ literal = False+ index = end+ continue+ if char == "$" or (quote is None and char in "~{}*?[]"):+ literal = False+ value.append(char)+ index += 1+ return _Token(+ text[start:index], "".join(value), literal and quote is None,+ expansions=tuple(expansions),+ ), index+++def _statements(text: str, *, heredocs: bool = True) -> Iterator[_Statement]:+ """Tokenize logical lines, isolating here-document bodies as data."""+ index = 0+ start = 0+ line = 1+ tokens: list[_Token] = []+ word_end = -1+ while index < len(text):+ char = text[index]+ if char in " \t" or text.startswith("\r\n", index):+ index += 1+ continue+ if char == "#":+ end = text.find("\n", index)+ index = len(text) if end < 0 else end+ continue+ if char == "\n":+ index += 1+ yield _Statement(tuple(tokens), start, index, line)+ line += text.count("\n", start, index)+ start = index+ documents = [+ (token.value, tokens[pos - 1].raw == "<<-")+ for pos, token in enumerate(tokens)+ if pos and tokens[pos - 1].raw in {"<<", "<<-"} and not token.operator+ ] if heredocs else []+ tokens = []+ for delimiter, strip_tabs in documents:+ body_start, body_line = index, line+ while index < len(text):+ end = text.find("\n", index)+ end = len(text) if end < 0 else end + 1+ candidate = text[index:end].removesuffix("\n").removesuffix("\r")+ if (candidate.lstrip("\t") if strip_tabs else candidate) == delimiter:+ break+ index = end+ body = text[body_start:index]+ for statement in _statements(body, heredocs=False):+ yield _Statement(+ statement.tokens, body_start + statement.start,+ body_start + statement.end, body_line + statement.line - 1, True,+ )+ line += body.count("\n")+ if index < len(text):+ line += text.count("\n", index, end)+ index = end+ start = index+ continue+ if char in ";&|()<>":+ fd_name = None+ if char in "<>" and tokens and word_end == index:+ # Logical unquoted adjacency may cross escaped newlines.+ # Quotes/other escapes remain in raw and cannot pass this+ # syntax check; original source offsets are never rewritten.+ logical = tokens[-1].raw.replace("\\\r\n", "").replace("\\\n", "")+ match = re.fullmatch(rf"\{{({_NAME})\}}", logical)+ if match:+ fd_name = match[1]+ tokens.pop()+ if (+ char in "<>" and tokens and tokens[-1].raw.isascii()+ and tokens[-1].raw.isdigit() and text[index - 1:index].isdigit()+ ):+ # An adjacent IO_NUMBER is part of the redirection, not a+ # command word. Quoted numbers and whitespace do not qualify.+ tokens.pop()+ operator = next((op for op in _OPERATORS if text.startswith(op, index)), char)+ tokens.append(_Token(operator, operator, operator=True, fd_name=fd_name))+ index += len(operator)+ continue+ token, index = _word(text, index)+ tokens.append(token)+ word_end = index+ if start < len(text):+ yield _Statement(tuple(tokens), start, len(text), line)+++def _segments(tokens: tuple[_Token, ...]) -> list[list[_Token]]:+ segments: list[list[_Token]] = []+ current: list[_Token] = []+ for token in tokens:+ if token.operator and token.raw in _SEPARATORS:+ if current and current[0].is_code("coproc") and token.raw in {"(", "(("}:+ current.append(token) # Retain the named compound introducer.+ if current:+ segments.append(current)+ current = []+ segments.append([token])+ elif not current and token.raw in {"{", "}"} | _COMMAND_PREFIXES:+ segments.append([token])+ elif current and current[0].raw == "function" and token.raw == "{":+ segments.extend([current, [token]])+ current = []+ else:+ current.append(token)+ if current:+ segments.append(current)+ return segments+++def _arithmetic_writers(text: str, names: frozenset[str]) -> set[str]:+ # Arithmetic is only inspected inside a known arithmetic/evaluation context.+ return {+ name for name in names+ if re.search(+ rf"(?<![\w])(?:\+\+|--)\s*{re.escape(name)}\b"+ rf"|\b{re.escape(name)}\s*(?:\[[^]]*\]\s*)?(?:\+\+|--|(?:[+*/%&|^-]|<<|>>)?=(?!=))",+ text,+ )+ }+++class _WrittenCode:+ """One finite, deduplicated owner for written code, expansion text and argv.++ A child is a written operand/subexpression, never expansion output, an+ included file or a resolved function. No recursion/analysis cutoff turns+ recognized code into safe data. Role and local dialect are part of identity.+ """++ def __init__(self, names: frozenset[str]):+ self.names = names+ self.found: set[str] = set()+ self.pending: list[tuple[str, str | tuple[_Token, ...], str]] = []+ self.seen: set[tuple[str, str | tuple[_Token, ...], str]] = set()++ def add(self, role: str, body: str | tuple[_Token, ...], dialect: str) -> None:+ key = (role, body, dialect)+ if body and key not in self.seen:+ self.seen.add(key)+ self.pending.append(key)++ def embedded(self, token: _Token, dialect: str) -> None:+ for kind, body in token.expansions:+ if kind == "parameter":+ match = re.match(rf"^({_NAME}):?=", body)+ if match:+ self.found.update({match[1]} & self.names)+ self.add("expansion", body, dialect)+ elif kind == "arithmetic":+ self.found.update(_arithmetic_writers(body, self.names))+ else:+ self.add("source", body, dialect)++ def run(self) -> set[str]:+ while self.pending and not self.found >= self.names:+ self.visit(*self.pending.pop())+ return self.found++ def visit(self, role: str, body: str | tuple[_Token, ...], dialect: str) -> None:+ if role == "argv":+ _argv_writers(list(body), self, dialect)+ return+ if role == "expansion":+ # Completion word lists expand words, not shell statements.+ # Source comments/heredocs must not discard written expansions;+ # the shared word lexer still protects quotes and escaped dollars.+ index = 0+ while index < len(body):+ if body[index] in " \t\r\n;&|()<>":+ index += 1+ continue+ token, index = _word(body, index)+ self.embedded(token, dialect)+ return+ for statement in _statements(body):+ for segment in _segments(statement.tokens):+ _command_writers(segment, self, dialect)+ if any(token.raw == "((" for token in statement.tokens):+ self.found.update(_arithmetic_writers(body[statement.start:statement.end], self.names))+++@dataclass(frozen=True, repr=False)+class _UnknownOptions:+ options: dict[str, _Token | None]+ remaining: list[_Token]+++def _options(+ arguments: list[_Token],+ flags: str = "",+ required: str = "",+ *,+ numeric: str = "",+ plus: bool = False,+ long: Mapping[str, tuple[str, str]] | None = None,+ stop_after: str = "",+ stop_unless: str = "",+ validate: Callable[[str, _Token], bool] | None = None,+) -> tuple[dict[str, _Token | None], list[_Token]] | _UnknownOptions | None:+ """Decode argument roles, not values; the last occurrence of an option wins.++ A required short argument may be attached, separate, or end a bundle.+ Zsh's optional numeric arguments consume only a digit-leading word/suffix.+ Long optional arguments are attached-only. Stop at the first operand/--.+ Invalid literal syntax cannot invoke the builtin; a dynamic word in option+ position instead leaves its role unknown. Required option arguments and+ words after an operand/-- never pass through that uncertainty boundary.+ """+ options: dict[str, _Token | None] = {}+ index = 0+ while index < len(arguments):+ token = arguments[index]+ argument = token.value+ if not token.literal and (not argument or argument[0] in _DYNAMIC_STARTS):+ return _UnknownOptions(options, arguments[index:])+ if argument == "--":+ return options, arguments[index + 1:]+ if plus and argument == "+":+ options["+"] = None+ return options, arguments[index + 1:]+ if len(argument) < 2 or argument[0] not in ("-+" if plus else "-"):+ return options, arguments[index:]+ if argument.startswith("--"):+ name, separator, value = argument[2:].partition("=")+ spec = (long or {}).get(name)+ if spec is None:+ return None if token.literal else _UnknownOptions(options, arguments[index:])+ flag, arity = spec+ operand = None+ if separator:+ if arity == "none":+ return None+ operand = _Token(value, value, token.literal)+ elif arity == "required":+ index += 1+ if index >= len(arguments):+ return None+ operand = arguments[index]+ if operand is not None and validate is not None and not validate(flag, operand):+ return None+ options[flag] = operand+ index += 1+ if flag in stop_after and not options.keys() & set(stop_unless):+ return options, arguments[index:]+ continue+ position = 1+ while position < len(argument):+ flag = argument[position]+ position += 1+ operand = None+ if flag in required or flag in numeric:+ suffix = argument[position:]+ if suffix and (flag in required or suffix[0].isdigit()):+ operand = _Token(suffix, suffix, token.literal)+ position = len(argument)+ elif not suffix and index + 1 < len(arguments) and (+ flag in required or re.match(r"[0-9]", arguments[index + 1].value)+ ):+ index += 1+ operand = arguments[index]+ elif flag in required:+ return None+ elif flag not in flags:+ if not token.literal and flag in _DYNAMIC_STARTS:+ return _UnknownOptions(options, arguments[index:])+ return None+ if operand is not None and validate is not None and not validate(flag, operand):+ return None+ options.pop(flag, None)+ options.pop("+" + flag, None)+ options[("+" if argument[0] == "+" else "") + flag] = operand+ if flag in stop_after and (flag in required or flag in numeric) and not options.keys() & set(stop_unless):+ return options, arguments[index + 1:]+ index += 1+ # Zsh print changes to echo's option grammar after the complete -R+ # word, not halfway through a bundle such as -RvNAME or -Rf FORMAT.+ if options.keys() & set(stop_after) and not options.keys() & set(stop_unless):+ return options, arguments[index:]+ return options, []+++def _option_cases(+ arguments: list[_Token], flags: str = "", required: str = "", *,+ numeric: str = "", plus: bool = False,+ long: Mapping[str, tuple[str, str]] | None = None, stop_after: str = "",+ stop_unless: str = "",+ writer_flags: str | tuple[str, ...] = "", state_flags: str = "", state_values: str = "",+ zero_values: str = "",+ validate: Callable[[str, _Token], bool] | None = None,+) -> Iterator[tuple[dict[str, _Token | None], list[_Token]]]:+ """Project an unknown option onto this builtin's existing argument roles.++ This is an existential safety check, not an expansion guess. An unknown+ word can end options, leave flags unchanged, enable a writer attribute, or+ consume one following word in a known option's argument role. Static data+ boundaries still terminate parsing. Retain only options the consumer+ actually inspects, not combinations of irrelevant prompts/fds/delimiters.+ Equivalent states are visited once.+ """+ def remember(options: dict[str, _Token | None], flag: str, value: _Token | None) -> None:+ base = flag.removeprefix("+") if flag != "+" else flag+ if base not in state_flags + state_values + zero_values:+ return+ options.pop(base, None)+ options.pop("+" + base, None)+ if base in zero_values:+ value = _Token("0", "0") if value is not None and re.fullmatch(r"0+(?:\.0*)?", value.value) else None+ elif base not in state_values:+ value = None+ options[flag] = value++ pending = [(arguments, {})]+ seen = set()+ while pending:+ remaining, prefix = pending.pop()+ key = (tuple(remaining), tuple(sorted(prefix.items())))+ if key in seen:+ continue+ seen.add(key)+ parsed = _options(+ remaining, flags, required, numeric=numeric, plus=plus,+ long=long, stop_after="" if prefix.keys() & set(stop_unless) else stop_after,+ stop_unless=stop_unless,+ validate=validate,+ )+ if parsed is None:+ continue+ current = parsed.options if isinstance(parsed, _UnknownOptions) else parsed[0]+ options = dict(prefix)+ for flag, value in current.items():+ remember(options, flag, value)+ if not isinstance(parsed, _UnknownOptions):+ yield options, parsed[1]+ continue+ token, *tail = parsed.remaining+ # A whole dynamic word can itself be an operand (e.g. printf's+ # format). A visibly option-prefixed word cannot provide a format.+ if not token.value.startswith(("-", "+") if plus else ("-",)):+ yield options, parsed.remaining+ yield options, tail # The dynamic word could be --.+ pending.append((tail, options))+ if plus and not token.value.startswith("-"):+ # A dynamic +/- bundle may remove existing query attributes.+ options = {key: value for key, value in options.items() if key not in {"f", "F", "p", "+"}}+ pending.append((tail, options))+ for bundle in writer_flags:+ pending.append((tail, {**options, **dict.fromkeys(bundle)}))+ if tail:+ for flag in required + numeric:+ if validate is not None and not validate(flag, tail[0]):+ continue # Only this possible operand role is invalid.+ changed = dict(options)+ remember(changed, flag, tail[0])+ if flag in stop_after:+ yield changed, tail[1:]+ else:+ pending.append((tail[1:], changed))+++def _destination(value: str, names: frozenset[str]) -> set[str]:+ """Inspect an actual lvalue, including explicit writes in an array index."""+ match = re.fullmatch(rf"({_NAME})(?:\[(.*)\])?", value, re.DOTALL)+ if not match:+ return set()+ found = {match[1]} & names+ if match[2] is not None:+ found.update(_arithmetic_writers(match[2], names))+ return found+++def _assignment_writers(value: str, names: frozenset[str], *, arithmetic: bool = False) -> set[str]:+ match = _WRITER.match(value)+ if not match:+ return set()+ found = _destination(value[:match.end() - 1].removesuffix("+"), names)+ if arithmetic:+ found.update(_arithmetic_writers(value[match.end():], names))+ return found+++def _array_reader_writers(+ arguments: list[_Token], analysis: _WrittenCode, dialect: str,+) -> set[str]:+ names = analysis.names+ found: set[str] = set()+ for options, operands in _option_cases(arguments, "t", "dnOsuCc", state_values="C"):+ found.update(_destination(operands[0].value if operands else "MAPFILE", names))+ callback = options.get("C")+ if callback is not None:+ # Only an explicit callback is code. Named functions are never followed.+ analysis.add("source", callback.value, dialect)+ if found >= names:+ break+ return found+++def _read_writers(+ arguments: list[_Token], names: frozenset[str], dialect: str, *, getln: bool = False,+) -> set[str]:+ cases = (+ _option_cases(+ arguments, "ecnAlE" if getln else "rszpqAclneE", "" if getln else "du",+ numeric="" if getln else "kt", writer_flags="A", state_flags="eAkqz", state_values="t",+ )+ if dialect == "zsh" else _option_cases(+ arguments, "ersE", "adinNptu", writer_flags="t", state_values="a", zero_values="t",+ )+ )+ found: set[str] = set()+ for options, operands in cases:+ if dialect == "zsh":+ timeout = options.get("t")+ if timeout is not None:+ found.update(_arithmetic_writers(timeout.value, names))+ if "e" in options:+ continue # Echo-only, unlike Bash's Readline -e.+ targets = [arg.value for arg in operands] or ["reply" if "A" in options else "REPLY"]+ targets[0] = targets[0].partition("?")[0] or ("reply" if "A" in options else "REPLY")+ if getln or options.keys() & {"A", "k", "q", "z"}:+ targets = targets[:1]+ else:+ timeout = options.get("t")+ if timeout is not None and re.fullmatch(r"0+(?:\.0*)?", timeout.value):+ continue # Bash's -t 0 checks availability without assigning.+ array = options.get("a")+ targets = [array.value] if array is not None else [arg.value for arg in operands] or ["REPLY"]+ found.update(set().union(*(_destination(value, names) for value in targets)))+ if found >= names:+ break+ return found+++def _declaration_writers(command: str, arguments: list[_Token], names: frozenset[str], dialect: str) -> set[str]:+ if dialect == "zsh":+ flags, numeric_options, default = _ZSH_DECLARATIONS[command]+ if default:+ arguments = [_Token("-" + default, "-" + default)] + arguments+ cases = _option_cases(+ arguments, flags, numeric=numeric_options, plus=True,+ writer_flags="iT", state_flags="fFpi+ET",+ )+ elif command in {"export", "readonly"}:+ cases = _option_cases(arguments, "afAnp", state_flags="fFp")+ else:+ cases = _option_cases(arguments, "aAfFgIilnprtux", plus=True, writer_flags="i", state_flags="fFpi+")+ found: set[str] = set()+ for options, operands in cases:+ if options.keys() & {"f", "+f"}:+ continue+ if dialect != "zsh" and options.keys() & {"F", "+F"}:+ continue+ if ("p" in options or "+" in options) and (dialect == "zsh" or command not in {"export", "readonly"}):+ continue+ numeric = "i" in options or (dialect == "zsh" and bool(options.keys() & {"E", "F"}))+ if dialect == "zsh" and numeric:+ found.update(arg.value for arg in operands if arg.value in names)+ if dialect == "zsh" and "T" in options and 2 <= len(operands) <= 3:+ for arg in operands[:2]:+ found.update(_destination(arg.value.partition("=")[0], names))+ # Attribute-only declarations are not newly stored credentials. An explicit+ # integer RHS or array subscript is an evaluation context, quoted or not.+ found.update(set().union(*(+ _assignment_writers(+ arg.value, names,+ arithmetic=numeric,+ )+ for arg in operands+ )))+ if found >= names:+ break+ return found+++def _printf_slots(format: str, dialect: str) -> list[tuple[int, str]]:+ """Locate format arguments without rendering, expanding, or evaluating.++ Track *, precision, %% and format reuse so %s data is never a %n target.+ Zsh's explicit positional slots are syntax only; numeric fields are known+ arithmetic contexts in Zsh, but not in Bash/POSIX printf.+ """+ slots: list[tuple[int, str]] = []+ cursor = 0+ index = 0++ def argument(position: int, kind: str) -> int:+ nonlocal cursor+ explicit = re.match(r"([1-9][0-9]*)\$", format[position:]) if dialect == "zsh" else None+ slot = int(explicit[1]) - 1 if explicit else cursor+ if not explicit:+ cursor += 1+ slots.append((slot, kind))+ return position + len(explicit[0]) if explicit else position++ while index < len(format):+ if format[index] == "\\":+ index += 2+ continue+ if format[index] != "%":+ index += 1+ continue+ index += 1+ if index < len(format) and format[index] == "%":+ index += 1+ continue+ explicit = re.match(r"[1-9][0-9]*\$", format[index:]) if dialect == "zsh" else None+ value_position = index+ if explicit:+ index += len(explicit[0])+ while index < len(format) and format[index] in "#0- +'":+ index += 1+ for precision in (False, True):+ if precision:+ if index >= len(format) or format[index] != ".":+ break+ index += 1+ if index < len(format) and format[index] == "*":+ index = argument(index + 1, "number")+ else:+ while index < len(format) and format[index].isdigit():+ index += 1+ while index < len(format) and format[index] in "hjlLtz":+ index += 1+ if index >= len(format):+ break+ conversion = format[index]+ time_format = False+ if conversion == "(" and dialect != "zsh":+ end = _group_end(format, index, "(", ")")+ if format[end:end + 1] != "T":+ break+ index = end+ conversion = "T"+ time_format = True+ if conversion not in "csbqQndiouxXeEfFgGaA" and not time_format:+ break+ kind = "destination" if conversion == "n" else (+ "number" if conversion in "diouxXeEfFgGaAT" else "data"+ )+ argument(value_position if explicit else index, kind)+ index += 1+ return slots+++def _printf_writers(arguments: list[_Token], names: frozenset[str], dialect: str) -> set[str]:+ found: set[str] = set()+ for options, operands in _option_cases(arguments, required="v", state_values="v"):+ found.update(_printf_operand_writers(options, operands, names, dialect))+ if found >= names:+ break+ return found+++def _printf_operand_writers(+ options: dict[str, _Token | None], operands: list[_Token], names: frozenset[str], dialect: str,+ *, reuse: bool = True,+) -> set[str]:+ if not operands:+ return set()+ destination = options.get("v")+ found = _destination(destination.value, names) if destination is not None else set()+ format, values = operands[0], operands[1:]+ if not format.literal:+ # A dynamic format might use %n or an existing Zsh numeric slot.+ # Inspect explicit writers only, never infer the format's value.+ found.update(arg.value for arg in values if arg.value in names)+ if dialect == "zsh":+ for arg in values:+ if arg.value[:1] not in {"'", '"'}:+ found.update(_arithmetic_writers(arg.value, names))+ return found+ slots = _printf_slots(format.value, dialect)+ stride = max((slot + 1 for slot, _ in slots), default=0)+ if not stride:+ return found+ for start in range(0, len(values), stride) if reuse else (0,):+ for slot, kind in slots:+ if start + slot >= len(values):+ continue+ value = values[start + slot].value+ if kind == "destination" and value in names:+ found.add(value)+ elif kind == "number" and dialect == "zsh" and value[:1] not in {"'", '"'}:+ found.update(_arithmetic_writers(value, names))+ return found+++_ENV_LONG_OPTIONS = {+ "ignore-environment": ("i", "none"), "null": ("0", "none"), "debug": ("v", "none"),+ "unset": ("u", "required"), "chdir": ("C", "required"),+ "split-string": ("S", "required"), "argv0": ("a", "required"),+ "quoting-style": ("quoting", "required"),+ "block-signal": ("block", "optional"), "default-signal": ("default", "optional"),+ "ignore-signal": ("ignore", "optional"), "list-signal-handling": ("list", "none"),+}+++def _env_split(text: str) -> list[_Token] | None:+ """The literal argv subset of GNU env -S, not shell tokenization.++ In particular, ; is data, # starts a comment only at a word boundary,+ and \\_ is an argument separator (a space inside double quotes). Preserve+ ${NAME} symbolically: it is never resolved from the process environment.+ """+ words: list[_Token] = []+ word: list[str] = []+ started = False+ literal = True+ quote: str | None = None+ index = 0++ def finish() -> None:+ nonlocal started, literal+ if started:+ value = "".join(word)+ words.append(_Token(value, value, literal))+ word.clear()+ started, literal = False, True++ while index < len(text):+ char = text[index]+ index += 1+ if char in "'\"" and (quote is None or char == quote):+ quote = char if quote is None else None+ started = True+ continue+ if quote is None and char in " \t\n\v\f\r":+ finish()+ continue+ if char == "#" and not started:+ break+ if char == "\\" and (quote != "'" or text[index:index + 1] in {"\\", "'"}):+ if index == len(text):+ return None+ char = text[index]+ index += 1+ if char == "_" and quote != '"':+ finish()+ continue+ if char == "c":+ if quote == '"':+ return None+ break+ escapes = {"_": " ", "f": "\f", "n": "\n", "r": "\r", "t": "\t", "v": "\v"}+ if char not in escapes and char not in "\"#$'\\":+ return None+ char = escapes.get(char, char)+ elif char == "$" and quote != "'":+ expansion = re.match(rf"\{{{_NAME}\}}", text[index:])+ if expansion is None:+ return None+ char += expansion[0]+ index += len(expansion[0])+ literal = False+ word.append(char)+ started = True+ if quote is not None:+ return None+ finish()+ return words+++def _env_writers(arguments: list[_Token], names: frozenset[str]) -> set[str]:+ # Each split consumes a written operand in a known/possible -S role.+ # Only the finite literal argv grammar is inspected; expansion never+ # manufactures more source, and command arguments are not shell code.+ found: set[str] = set()+ pending = [arguments]+ seen = set()+ while pending:+ arguments = pending.pop()+ key = tuple(arguments)+ if key in seen:+ continue+ seen.add(key)+ for options, operands in _option_cases(+ arguments, "iv0", "uCSa", long=_ENV_LONG_OPTIONS, stop_after="S", state_values="S",+ ):+ split = options.get("S")+ if split is not None:+ values = _env_split(split.value)+ if values is not None:+ pending.append(values + operands)+ continue+ for token in operands:+ if token.value == "-" and not found:+ continue # Historical env - (empty environment).+ name, separator, _ = token.value.partition("=")+ if not separator:+ break # Even a dynamic command name anchors command data.+ if name in names:+ found.add(name)+ if found >= names:+ return found+ return found+++def _command_writers(tokens: list[_Token], analysis: _WrittenCode, dialect: str) -> None:+ if len(tokens) == 1 and tokens[0].operator:+ return # A statement separator is syntax, never a command name.+ names, found = analysis.names, analysis.found+ for token in tokens:+ analysis.embedded(token, dialect)+ words: list[_Token] = []+ redirect = False+ for index, token in enumerate(tokens):+ if token.operator and token.raw not in _SEPARATORS:+ target = tokens[index + 1] if index + 1 < len(tokens) else None+ if token.fd_name and target and not target.operator and not (+ token.raw in {"<&", ">&"} and target.literal and target.value == "-"+ ):+ found.update({token.fd_name} & names)+ redirect = True+ elif redirect:+ redirect = False+ else:+ words.append(token)+ while words and words[0].raw in {"if", "then", "elif", "else", "while", "until", "do", "!"}:+ words = words[1:]+ if not words:+ return+ if words[0].raw in {"for", "select"}:+ if len(words) > 1 and words[1].value in names:+ found.add(words[1].value)+ return+ if dialect != "zsh" and words[0].is_code("coproc") and len(words) > 1:+ compound = {"{", "(", "((", "if", "while", "until", "for", "select", "case", "[["}+ named = len(words) > 2 and words[2].raw in compound+ target = words[1].value if named else "COPROC"+ if (not named or words[1].literal) and re.fullmatch(_NAME, target):+ found.update({target, target + "_PID"} & names)+ words = words[2 if named else 1:]+ analysis.add("source", " ".join(word.raw for word in words), dialect)+ return+ while words:+ match = _WRITER.match(words[0].raw.replace("\\\r\n", "").replace("\\\n", ""))+ if not match:+ break+ found.update(_assignment_writers(words[0].value, names))+ words = words[1:]+ # Wrappers select argv, not fresh shell source. In particular an assignment+ # word after `command` is a command name, not a prefix assignment.+ analysis.add("argv", tuple(words), dialect)+++def _argv_writers(words: list[_Token], analysis: _WrittenCode, dialect: str) -> None:+ command = words[0].value+ if command in {"command", "builtin"}:+ for options, operands in _option_cases(+ words[1:], "pvV" if command == "command" else "", state_flags="vV",+ ):+ if not options.keys() & {"v", "V"}:+ analysis.add("argv", tuple(operands), dialect)+ elif dialect == "zsh" and words[0].raw in {"noglob", "-", "exec"}:+ if command != "exec":+ analysis.add("argv", tuple(words[1:]), dialect)+ else:+ for _, operands in _option_cases(words[1:], "cl", "a"):+ analysis.add("argv", tuple(operands), dialect)+ else:+ analysis.found.update(_builtin_writers(words, analysis, dialect))+++def _zsh_optionless_cases(arguments: list[_Token]) -> Iterator[list[_Token]]:+ """One optional --, never letter options or repeated boundary stripping.++ execbuiltin owns this envelope for eval/numeric controls; BINF_HANDLES_OPTS+ leaves trap's equivalent once-only boundary to bin_trap itself.+ """+ if arguments and arguments[0].value == "--":+ yield arguments[1:]+ return+ yield arguments+ if arguments and not arguments[0].literal and re.match(r"-{0,2}[$`~{*?\[]", arguments[0].value):+ yield arguments[1:] # The unknown initial word could supply --.+++def _trap_writers(arguments: list[_Token], analysis: _WrittenCode, dialect: str) -> None:+ if dialect == "zsh":+ # Zsh has only the optional --, not Bash's query flags.+ cases = (({}, operands) for operands in _zsh_optionless_cases(arguments))+ else:+ cases = _option_cases(arguments, "lpP", state_flags="lpP")+ for options, operands in cases:+ if not options and len(operands) >= 2 and operands[0].value not in {"", "-"}:+ analysis.add("source", operands[0].value, dialect)+++def _bind_source(text: str) -> str:+ """Readline's explicit -x delimiter grammar, not ordinary binding macros."""+ text = text.lstrip()+ if not text.startswith('"'):+ return ""+ end = _quoted_end(text, 0, '"')+ tail = text[end:]+ if not tail or tail[0] not in ": \t":+ return ""+ colon = tail.startswith(":")+ tail = tail[1:].lstrip()+ if tail.startswith('"'):+ end = _quoted_end(tail, 0, '"')+ return tail[1:end - 1] if tail[end - 1:end] == '"' else ""+ return tail if colon else ""+++def _completion_value_valid(flag: str, token: _Token) -> bool:+ """Reject only proven-invalid operands, at each option occurrence.++ complete.def validates before replacing a previous option or applying a+ query. Unknown values are not invalid. -F sets WORD.flags=0 and calls+ check_identifier(..., posixly_correct) plus shell_break_chars; without+ inferring POSIX mode only those literal break characters prove rejection.+ This never resolves the named function or treats its name as code.+ """+ if not token.literal:+ return True+ if flag == "V":+ return re.fullmatch(_NAME, token.value) is not None+ if flag == "F":+ return not any(char in "()<>;&| \t\n" for char in token.value)+ if flag == "A":+ return token.value in {+ "alias", "arrayvar", "binding", "builtin", "command", "directory", "disabled", "enabled",+ "export", "file", "function", "group", "helptopic", "hostname", "job", "keyword",+ "running", "service", "setopt", "shopt", "signal", "stopped", "user", "variable",+ }+ if flag == "o":+ return token.value in {+ "bashdefault", "default", "dirnames", "filenames", "fullquote",+ "noquote", "nosort", "nospace", "plusdirs",+ }+ return True+++def _completion_writers(command: str, arguments: list[_Token], analysis: _WrittenCode, dialect: str) -> set[str]:+ found: set[str] = set()+ flags = "abcdefgjksuv" + ("prDEI" if command == "complete" else "")+ required = "oAGWPSXFC" + ("V" if command == "compgen" else "")+ # Independent roles must not retain a Cartesian product of callback,+ # word-list, target and unrelated option values under dynamic options.+ for flag, role in (("V", "target"), ("C", "source"), ("W", "expansion")):+ if flag == "V" and command == "complete":+ continue+ for options, operands in _option_cases(+ arguments, flags, required, state_flags="prDEI", state_values=flag,+ writer_flags="D" if command == "complete" else "",+ validate=_completion_value_valid,+ ):+ if command == "complete" and (+ options.keys() & {"p", "r"} or not (operands or options.keys() & {"D", "E", "I"})+ ):+ continue+ if (token := options.get(flag)) is not None:+ if role == "target":+ found.update({token.value} & analysis.names)+ else:+ analysis.add(role, token.value, dialect)+ return found+++def _jobs_writers(arguments: list[_Token], analysis: _WrittenCode, dialect: str) -> None:+ # -x is rejected if a display format was already set, but -xl is valid.+ form = False+ for token in arguments:+ if not token.literal or not token.value.startswith("-") or token.value == "--":+ break+ for flag in token.value[1:]:+ if flag == "x" and form:+ return+ form |= flag in "lpn"+ for options, operands in _option_cases(arguments, "lpnxrs", writer_flags="x", state_flags="x"):+ if "x" in options:+ analysis.add("argv", tuple(operands), dialect)+++def _fc_writers(arguments: list[_Token], analysis: _WrittenCode, dialect: str) -> None:+ zsh = dialect == "zsh"+ for options, _ in _option_cases(+ arguments, "aAdDEfiIlLmnpPrRWs" if zsh else "lnrs",+ "et" if zsh else "e", state_flags="lspPRWA" if zsh else "ls", state_values="e",+ ):+ editor = options.get("e")+ if editor is not None and editor.value != "-" and not options.keys() & {"l", "s", "p", "P", "R", "W", "A"}:+ analysis.add("source", editor.value, dialect)+++def _zsh_print_writers(arguments: list[_Token], names: frozenset[str]) -> set[str]:+ found: set[str] = set()+ for role in ("v", "f"):+ for options, operands in _option_cases(+ arguments, "abcDilmnNoOpPrRsSz", "CfuvxX",+ stop_after="R", stop_unless="f", state_values=role, state_flags="zsScCpuvfr",+ ):+ special = options.keys() & {"z", "s", "S", "v"}+ if (+ len(special) > 1+ or (options.keys() & {"z", "s", "S"} and options.keys() & {"c", "C"})+ or (special and options.keys() & {"p", "u"})+ ):+ continue+ if (token := options.get(role)) is not None:+ if role == "v":+ found.update(_destination(token.value, names))+ elif "S" not in options:+ found.update(_printf_operand_writers(+ {}, [token, *operands], names, "zsh", reuse="r" not in options,+ ))+ if found >= names:+ return found+ return found+++def _zsh_test_writers(arguments: list[_Token], names: frozenset[str], bracket: bool) -> set[str]:+ if bracket:+ if not arguments or arguments[-1].value != "]":+ return set()+ arguments = arguments[:-1]+ # Only -t has a numeric-evaluation operand. Binary comparisons consume+ # their two data operands (including -eq's decimal input), not shell code.+ words = [arg.value for arg in arguments]+ found: set[str] = set()+ index = 0+ nesting = 0+ primary = True+ binary = {"=", "==", "!=", "<", ">", "-eq", "-ne", "-lt", "-le", "-gt", "-ge", "-nt", "-ot", "-ef"}+ while index < len(words):+ if not primary:+ if words[index] == ")" and nesting:+ nesting -= 1+ elif words[index] in {"-a", "-o"}:+ primary = True+ else:+ return set()+ index += 1+ continue+ if index + 2 < len(words) and words[index + 1] in binary:+ index += 3+ elif words[index] in {"!", "("}:+ nesting += words[index] == "("+ index += 1+ continue+ elif re.fullmatch(r"-[abcdefghkLnpOrRsStuvwxzUGNHSV]", words[index]) and index + 1 < len(words):+ if words[index] == "-t":+ found.update(_arithmetic_writers(words[index + 1], names))+ index += 2+ else:+ index += 1+ primary = False+ return found if not primary and not nesting else set()+++def _zsh_writers(command: str, arguments: list[_Token], analysis: _WrittenCode) -> set[str]:+ names = analysis.names+ found: set[str] = set()+ if command == "print":+ return _zsh_print_writers(arguments, names)+ if command == "set":+ for options, _ in _option_cases(+ arguments, _ZSH_OPTION_FLAGS, "Ao", plus=True, stop_after="A", state_values="A",+ ):+ target = options.get("A") or options.get("+A")+ if target is not None:+ found.update(_destination(target.value, names))+ elif command == "shift":+ for _, operands in _option_cases(arguments, "p"):+ if operands:+ found.update(_arithmetic_writers(operands[0].value, names))+ # The first word may be an array instead of a count. Inspect+ # written names, never look up the variable's runtime type.+ found.update(arg.value for arg in operands if arg.value in names)+ elif command in {"break", "continue", "return", "exit", "bye", "logout"}:+ for operands in _zsh_optionless_cases(arguments):+ if len(operands) == 1:+ found.update(_arithmetic_writers(operands[0].value, names))+ elif command in {"test", "["}:+ found.update(_zsh_test_writers(arguments, names, command == "["))+ elif command == "emulate":+ for options, operands in _option_cases(arguments, "lLR", state_flags="lL"):+ if options.keys() & {"l", "L"} or not operands:+ continue+ for local, rest in _option_cases(+ operands[1:], _ZSH_OPTION_FLAGS, "co", state_values="c",+ ):+ if not rest and (body := local.get("c")) is not None:+ # bin_emulate changes options then calls bin_eval; it does+ # not replace Zsh's builtin table with the named shell's.+ analysis.add("source", body.value, "zsh")+ elif command == "zmodload":+ for options, operands in _option_cases(+ arguments, "AFRILabcfdilmsue", "P", writer_flags=("FL",),+ state_flags="FRALleubcfpd", state_values="P",+ ):+ if (+ operands and "F" in options and options.keys() & {"l", "L"}+ and not options.keys() & {"e", "u", "b", "c", "f", "p", "A", "R"}+ and (target := options.get("P")) is not None+ ):+ found.update({target.value} & names)+ return found+++def _builtin_writers(words: list[_Token], analysis: _WrittenCode, dialect: str) -> set[str]:+ names = analysis.names+ found: set[str] = set()+ command = words[0].value+ arguments = words[1:]+ if command in _DECLARATIONS or (dialect == "zsh" and command in {"integer", "float"}):+ found.update(_declaration_writers(command, arguments, names, dialect))+ elif command == "env":+ found.update(_env_writers(arguments, names))+ elif command == "read" or (dialect == "zsh" and command == "getln"):+ found.update(_read_writers(arguments, names, dialect, getln=command == "getln"))+ elif dialect != "zsh" and command in {"mapfile", "readarray"}:+ found.update(_array_reader_writers(arguments, analysis, dialect))+ elif command == "unset":+ for options, operands in _option_cases(arguments, "fmv" if dialect == "zsh" else "fnv", state_flags="f"):+ if "f" not in options:+ found.update(set().union(*(_destination(arg.value, names) for arg in operands)))+ if found >= names:+ break+ elif command == "getopts":+ for _, operands in _option_cases(arguments):+ if len(operands) >= 2:+ found.update({"OPTARG", "OPTIND", operands[1].value} & names)+ if found >= names:+ break+ elif command == "printf":+ found.update(_printf_writers(arguments, names, dialect))+ elif command == "let":+ found.update(_arithmetic_writers(" ".join(arg.value for arg in arguments), names))+ elif command == "eval":+ if dialect == "zsh":+ for operands in _zsh_optionless_cases(arguments):+ analysis.add("source", " ".join(arg.value for arg in operands), dialect)+ else:+ for _, operands in _option_cases(arguments):+ analysis.add("source", " ".join(arg.value for arg in operands), dialect)+ elif command == "trap":+ _trap_writers(arguments, analysis, dialect)+ elif command == "fc":+ _fc_writers(arguments, analysis, dialect)+ elif dialect != "zsh":+ if command == "wait":+ for options, _ in _option_cases(arguments, "fn", "p", state_values="p"):+ if (target := options.get("p")) is not None:+ found.update(_destination(target.value, names))+ elif command == "bind":+ for options, _ in _option_cases(arguments, "lvpVPsSX", "fmqurx", state_flags="r", state_values="x"):+ if "r" not in options and (body := options.get("x")) is not None:+ analysis.add("source", _bind_source(body.value), dialect)+ elif command in {"complete", "compgen"}:+ found.update(_completion_writers(command, arguments, analysis, dialect))+ elif command == "jobs":+ _jobs_writers(arguments, analysis, dialect)+ else:+ found.update(_zsh_writers(command, arguments, analysis))+ return found+++def _written_names(text: str, names: frozenset[str], *, dialect: str = "bash") -> set[str]:+ analysis = _WrittenCode(names)+ analysis.add("source", text, dialect)+ return analysis.run()+++def _parse(path: Path, before: bytes, names: frozenset[str]) -> ShellProfile:+ text = before.decode("utf-8")+ # Only fixed-path provenance selects grammar, never $SHELL or live state.+ # POSIX .profile also recognizes explicit Bash extension writers as unsafe;+ # it does not claim those extensions would execute in every POSIX shell.+ dialect = "zsh" if path.name in _ZSH_PROFILES else "posix" if path.name == ".profile" else "bash"+ assignments: list[ShellAssignment] = []+ issues: list[ShellIssue] = []+ values: dict[str, str] = {}+ stack: list[str] = []+ pending_function = False+ continuation = False+ uncertain = False+ for statement in _statements(text):+ tokens = statement.tokens+ if not tokens:+ continue+ raw = text[statement.start:statement.end]+ words = tokens[1:] if tokens[0].is_code("export") else tokens+ match = _ASSIGNMENT.match(words[0].raw) if len(words) == 1 else None+ if (+ match and match[1] in names and words[0].literal+ and not (stack or pending_function or continuation or uncertain or statement.data)+ and "\n" not in raw.removesuffix("\n")+ and "\x00" not in raw+ ):+ name = match[1]+ value = words[0].value[len(name) + 1:]+ start = len(text[:statement.start].encode("utf-8"))+ end = start + len(raw.encode("utf-8"))+ assignments.append(ShellAssignment(name, value, start, end))+ if name in values and values[name] != value:+ issues.append(ShellIssue((name,), "ambiguous_shell", statement.line))+ values[name] = value+ continuation = False+ continue+ written = _written_names(raw, names, dialect=dialect)+ arithmetic = "))" in stack or any(token.raw == "((" for token in tokens)+ segments = _segments(tokens)+ index = 0+ while index < len(segments):+ segment = segments[index]+ first = segment[0].raw+ if statement.data:+ index += 1+ continue+ if stack and stack[-1] == "]]":+ if any(token.raw == "]]" for token in segment):+ stack.pop()+ index += 1+ continue+ if (+ len(segment) == 1 and re.fullmatch(_NAME, first)+ and index + 2 < len(segments)+ and [part[0].raw for part in segments[index + 1:index + 3]] == ["(", ")"]+ ):+ pending_function = True+ index += 3+ continue+ if first == "function":+ pending_function = True+ elif first in {"coproc", "repeat"}:+ # These shell-specific compound forms have no single portable+ # terminator. Their following assignments are unsupported.+ uncertain = True+ elif first in {"if", "for", "select", "while", "until", "case", "foreach", "{", "(", "((", "[["}:+ closer = {+ "if": "fi", "for": "done", "select": "done", "while": "done",+ "until": "done", "case": "esac", "foreach": "end",+ "{": "}", "(": ")", "((": "))", "[[": "]]",+ }[first]+ stack.append(closer)+ if first == "[[" and any(token.raw == "]]" for token in segment[1:]):+ stack.pop()+ pending_function = False+ elif first in {"fi", "done", "esac", "end", "}", ")", "))", "]]"}:+ if stack and stack[-1] == first:+ stack.pop()+ elif first != ")" or not stack or stack[-1] != "esac":+ uncertain = True+ index += 1+ if arithmetic:+ written.update(_arithmetic_writers(raw, names))+ if written:+ issues.append(ShellIssue(tuple(sorted(written)), "dynamic_shell", statement.line))+ if not statement.data:+ continuation = tokens[-1].raw in {"&&", "||", "|", "|&", "\\"}+ # Multiline expansions can contain arbitrary nested shell grammar.+ # Never resume accepting literals on a guessed closing delimiter.+ if any("\n" in token.raw and token.expansions for token in tokens):+ uncertain = True+ return ShellProfile(path, before, tuple(assignments), tuple(issues))+++def read_shell_profiles(home: Path | None, names: frozenset[str]) -> tuple[ShellProfile, ...]:+ """Read only the eight fixed paths, retaining absent paths as consent guards."""+ from .migration_journal import NativeFileEdit, TakeoverStateError++ root = Path.home() if home is None else home+ profiles: list[ShellProfile] = []+ for name in SHELL_PROFILE_NAMES:+ path = (root / name).absolute()+ try:+ snapshot = NativeFileEdit.plan(path, None)+ before = snapshot.before+ profile = _parse(path, before, names) if before is not None else ShellProfile(path, None, (), ())+ profile = replace(profile, mode=snapshot.mode)+ except (OSError, UnicodeError, TakeoverStateError):+ profile = ShellProfile(path, None, (), (ShellIssue(tuple(sorted(names)), "unreadable", None),))+ profiles.append(profile)+ return tuple(profiles)+++def cleanup_shell_profile(profile: ShellProfile, selected: Mapping[str, str]) -> NativeFileEdit:+ """Plan exact consented line removals, without rereading or writing a file."""+ from .migration_journal import NativeFileEdit, TakeoverStateError++ if any(set(issue.names).intersection(selected) for issue in profile.issues):+ raise TakeoverStateError("shell configuration requires manual migration")+ removals: list[ShellAssignment] = []+ for assignment in profile.assignments:+ if assignment.name not in selected:+ continue+ if assignment.value != selected[assignment.name]:+ raise TakeoverStateError("shell configuration value changed")+ removals.append(assignment)+ after = profile.before+ for assignment in sorted(removals, key=lambda item: item.start, reverse=True):+ if after is None or not 0 <= assignment.start < assignment.end <= len(after):+ raise TakeoverStateError("invalid shell configuration snapshot")+ after = after[:assignment.start] + after[assignment.end:]+ return NativeFileEdit(+ profile.path, profile.before, after, profile.mode,+ profile.mode if profile.before is not None and profile.before != after else None,+ )diff --git a/core/handlers/model_hub/revocations.py b/core/handlers/model_hub/revocations.pyindex e0317cbd3..4c83c6345 100644--- a/core/handlers/model_hub/revocations.py+++ b/core/handlers/model_hub/revocations.py@@ -14,6 +14,7 @@ from .state_file import write_state_documentRevocationOperation = Literal["revoke_credential",+ "revoke_api_key_credential","cleanup_orphaned_oauth_material",]_REVOCATION_OPERATIONS = frozenset(get_args(RevocationOperation))diff --git a/core/handlers/model_hub/service.py b/core/handlers/model_hub/service.pyindex 92c8df110..deef66d7b 100644--- a/core/handlers/model_hub/service.py+++ b/core/handlers/model_hub/service.py@@ -94,6 +94,7 @@ from .identifiers import OPENCODE_PROVIDER_BY_NATIVE_PROTOCOL, canonical_model_ifrom .migration import (MigrationConflictError,MigrationCredentialsInvalidError,+ MigrationReauthorizationRequiredError,apply_native_migration,prepare_takeover_reauthentication,recover_native_migration,@@ -405,7 +406,7 @@ class UnavailableEngineAdapter:async def provision_credential(self, vendor: str, protocol: str, secret: str, base_url: str | None,- *, on_reserved: Callable[[str], None] | None = None,+ *, auth_scheme: str | None = None, on_reserved: Callable[[str], None] | None = None,) -> str:raise EngineUnavailableError@@ -427,18 +428,25 @@ class UnavailableEngineAdapter:async def matches_api_key_credential(self, credential_ref: str, vendor: str, protocol: str,secret: str, base_url: str | None,+ *, auth_scheme: str | None = None,) -> bool:raise EngineUnavailableError+ async def credential_auth_scheme(self, credential_ref: str) -> str | None:+ raise EngineUnavailableError+async def provision_transient_credential(self, vendor: str, secret: str, base_url: str | None,- *, on_reserved: Callable[[str], None] | None = None,+ *, auth_scheme: str | None = None, on_reserved: Callable[[str], None] | None = None,) -> str:raise EngineUnavailableErrorasync def revoke_credential(self, credential_ref: str) -> None:raise EngineUnavailableError+ async def revoke_api_key_credential(self, credential_ref: str) -> None:+ raise EngineUnavailableError+async def sync_sources(self, bindings) -> None:raise EngineUnavailableError@@ -640,9 +648,11 @@ async def _provision_transient_credential_with_cancellation_ownership(vendor: str,key: str,base_url: str | None,- *, on_reserved: Callable[[str], None] | None = None,+ *, auth_scheme: str | None = None, on_reserved: Callable[[str], None] | None = None,) -> str:- options = {"on_reserved": on_reserved} if on_reserved is not None else {}+ options: dict[str, Any] = {"on_reserved": on_reserved} if on_reserved is not None else {}+ if auth_scheme is not None:+ options["auth_scheme"] = auth_schemereturn await _acquire_credential_ref_with_cancellation_ownership(service,service.adapter.provision_transient_credential(vendor, key, base_url, **options),@@ -1285,6 +1295,13 @@ class ModelHubService:continueif not cleaned:continue+ elif pending.operation == "revoke_api_key_credential":+ try:+ await self.adapter.revoke_api_key_credential(pending.credential_ref)+ except Exception:+ # Only successful exact-namespace cleanup establishes+ # absence. Metadata errors are not retirement proof.+ continueelse:try:await self.adapter.revoke_credential(@@ -1671,6 +1688,7 @@ class ModelHubService:payload: Mapping[str, Any],*,require_proven: bool = False,+ auth_scheme: str | None = None,on_reserved: Callable[[str], None] | None = None,) -> SourceObservation:if set(payload) - {"vendor", "base_url", "key", "protocol"}:@@ -1690,6 +1708,7 @@ class ModelHubService:vendor,key.strip(),base_url,+ **({"auth_scheme": auth_scheme} if auth_scheme is not None else {}),on_reserved=on_reserved,)try:@@ -1716,10 +1735,11 @@ class ModelHubService:async def _require_proven_source_payload(self,payload: Mapping[str, Any],- *, on_reserved: Callable[[str], None] | None = None,+ *, auth_scheme: str | None = None, on_reserved: Callable[[str], None] | None = None,) -> SourceObservation:return await self._observe_source_payload(payload, require_proven=True, on_reserved=on_reserved,+ **({"auth_scheme": auth_scheme} if auth_scheme is not None else {}),)async def _provision_oauth_credential(@@ -3222,12 +3242,16 @@ class ModelHubService:raise ModelHubError("discovery_failed")old_credential_ref = source.credential_ref+ auth_scheme = await self._engine_call(+ self.adapter.credential_auth_scheme(old_credential_ref)+ )replacement_ref = await self._engine_call(self.adapter.provision_credential(source.vendor,source.protocol,key,source.base_url,+ **({"auth_scheme": auth_scheme} if auth_scheme is not None else {}),))committed = False@@ -3238,7 +3262,20 @@ class ModelHubService:source.masked_credential = _mask_credential(key)discovered = await self._discover(source)if old_credential_ref != replacement_ref:- self.revocations.add(source.id, old_credential_ref)+ if any(+ pending.source_id == source.id+ and pending.credential_ref == old_credential_ref+ and pending.operation == "revoke_credential"+ for pending in self.revocations.list()+ ):+ # Replay also discards intents for the current active+ # ref. Do that here for a pre-upgrade generic intent:+ # syncing the damaged old credential is not required+ # to establish that it is still the committed ref.+ self.revocations.remove(source.id, old_credential_ref)+ self.revocations.add(+ source.id, old_credential_ref, operation="revoke_api_key_credential",+ )old_revocation_recorded = Trueremoved_hops, interrupted = await self._finalize_successful_discovery(previous,@@ -3277,7 +3314,7 @@ class ModelHubService:if old_credential_ref != replacement_ref:try:- await self.adapter.revoke_credential(old_credential_ref)+ await self.adapter.revoke_api_key_credential(old_credential_ref)except Exception:passelse:@@ -6421,6 +6458,8 @@ class ModelHubService:applied, added_to = await await_owned_task(task)except MigrationCredentialsInvalidError:raise ModelHubError("migration_credentials_invalid", status=409) from None+ except MigrationReauthorizationRequiredError:+ raise ModelHubError("migration_reauthorization_required", status=409) from Noneexcept NativeMigrationBlockedError:raise ModelHubError("migration_native_busy", status=409) from Noneexcept (NativeOAuthPermissionError, PermissionError):diff --git a/docs/plans/model-hub-contracts/adapter-interface.py b/docs/plans/model-hub-contracts/adapter-interface.pyindex 311ab5b74..22792ca75 100644--- a/docs/plans/model-hub-contracts/adapter-interface.py+++ b/docs/plans/model-hub-contracts/adapter-interface.py@@ -450,6 +450,7 @@ class EngineAdapter(Protocol):secret: str,base_url: str | None,*,+ auth_scheme: str | None = None,on_reserved: Callable[[str], None] | None = None,) -> str:"""Store an API-key secret in the ENGINE-OWNED credential store and@@ -461,7 +462,8 @@ class EngineAdapter(Protocol):reservation and before any secret bytes are written. A callback failureforbids the write. Hub OAuth credentials never pass through here —they are created engine-side by the OAuth flow and surfaced via- ``OAuthFlowState.credential_ref``."""+ ``OAuthFlowState.credential_ref``. ``auth_scheme`` optionally preserves+ an explicit static transport; omission retains legacy semantics."""...async def provision_oauth_credential(@@ -511,10 +513,21 @@ class EngineAdapter(Protocol):protocol: str,secret: str,base_url: str | None,+ *,+ auth_scheme: str | None = None,) -> bool:"""Compare transient native material inside custody; return no secrets."""...+ async def credential_auth_scheme(self, credential_ref: str) -> str | None:+ """Resolve immutable static transport without requiring the old secret.++ A safe missing/content-corrupt private document may recover from its+ ref. Readable wrong-kind/unknown/conflicting metadata, unsafe paths and+ I/O errors reject. The replacement still needs normal key validation.+ """+ ...+async def retarget_api_key_credential(self,credential_ref: str,@@ -539,7 +552,17 @@ class EngineAdapter(Protocol):...async def revoke_credential(self, credential_ref: str) -> None:- """Release the stored credential (source deletion / key replacement)."""+ """Release a stored credential, preserving OAuth auth-file ordering."""+ ...++ async def revoke_api_key_credential(self, credential_ref: str) -> None:+ """Retire an unbound ref under durable API-key-only caller intent.++ Remove only the exact safe private ref namespace, even if the old+ document is missing/content-corrupt. Reject readable identity conflicts+ and unsafe paths. Never delete watched OAuth auth files or call OAuth+ management APIs. Return only after durable cleanup; failure is retryable.+ """...async def provision_transient_credential(@@ -548,6 +571,7 @@ class EngineAdapter(Protocol):secret: str,base_url: str | None,*,+ auth_scheme: str | None = None,on_reserved: Callable[[str], None] | None = None,) -> str:"""Provision an unbound credential for an unsaved observation.diff --git a/docs/plans/model-hub-contracts/migration-scan.schema.json b/docs/plans/model-hub-contracts/migration-scan.schema.jsonindex 9eaffbae1..fcec6b4e8 100644--- a/docs/plans/model-hub-contracts/migration-scan.schema.json+++ b/docs/plans/model-hub-contracts/migration-scan.schema.json@@ -26,7 +26,19 @@"notes_key": { "type": ["string", "null"], "description": "i18n key for the row's secondary line" },"vendor": { "type": "string", "description": "Additive presentation metadata (ratified 2026-09-19). The provider this credential belongs to — an OpenCode row carries its native provider id. Optional: a client that does not receive it falls back to masked_detail and a generic icon, and never infers provider identity from `backend`." },"display_name": { "type": "string", "description": "Additive presentation metadata. The server's own label for the provider. Not guaranteed friendly — OpenCode currently emits the provider id — so a client maps known vendors to brand names and falls back to this label, then to the slug." },- "masked_credential": { "type": ["string", "null"], "description": "Additive presentation metadata. The masked credential alone, produced by the same masking the composed masked_detail used, so a client can render provider and key separately without re-parsing that string. Never a plaintext secret; null on rows that have no credential of their own." }+ "masked_credential": { "type": ["string", "null"], "description": "Additive presentation metadata. The masked credential alone, produced by the same masking the composed masked_detail used, so a client can render provider and key separately without re-parsing that string. Never a plaintext secret; null on rows that have no credential of their own." },+ "source_paths": {+ "type": "array",+ "items": { "type": "string" },+ "uniqueItems": true,+ "description": "Additive persisted-file provenance. Locators only, never file contents or environment values. Empty/absent retains the client's generic backend label."+ },+ "required_backends": {+ "type": "array",+ "items": { "enum": ["claude", "codex", "opencode"] },+ "uniqueItems": true,+ "description": "Backend-group consent closure for a shared persisted assignment. Empty/absent means this row's backend alone. The server independently requires the entire closure."+ }}}}diff --git a/docs/plans/model-hub-e2e-test-plan.md b/docs/plans/model-hub-e2e-test-plan.mdindex 95c1a709b..597096277 100644--- a/docs/plans/model-hub-e2e-test-plan.md+++ b/docs/plans/model-hub-e2e-test-plan.md@@ -93,6 +93,7 @@ Out of scope (documented, not dropped silently):| F1 | seed hermetic HOME with native claude/codex/opencode configs; scan | complete Claude API-key custody is importable; access-only Codex OAuth and unsupported OpenCode credentials remain visible blockers; no credential material is returned | assert || F2 | apply the selected complete backend group | Claude API-key custody is committed and its direct native auth is cleaned; blocked Codex/OpenCode material and unrelated Claude settings remain unchanged | assert || F3 | first open `/settings/models` after upgrade with importable items | banner visible (B2 — currently only in wizard: expected-fail until mounted) | fix-first |+| F4 | already-Hub agents with populated runtime auth; import saved CLI/shell files, then repair damaged private custody | runtime values neither supply nor block import; dynamic writers refuse without cleanup; static Bearer survives migration and replacement through actual CPA even when the old private file is missing/corrupt; recorded/unknown OAuth history survives later batches and refuses speculative reprovision; public/wrong-key proof preserves files and unrelated bytes/modes | assert |### G. Guards and contract hygiene (C51–C53, B3,B7,B13,B16)| ID | Steps | Expect | Status |@@ -227,6 +228,18 @@ lane-to-lane.every seeded native file and the empty Source list. This is a fixture-onlycontract: production authentication evidence and other suites' defaultsare unchanged.+ `required_auth_scheme: "protocol"|"bearer"` defaults to `"protocol"` and+ leaves that behavior unchanged. Opting into `"bearer"` models a custom+ Anthropic relay accepting only the exact Authorization bearer value, not+ the same value sent as `x-api-key`. F2 uses a saved `ANTHROPIC_AUTH_TOKEN`+ and this gate, keeping its positive and six-file-preserving negative+ controls consistent with the pinned engine's custom-endpoint transport.+ The persisted-migration suite uses this+ to check that a saved `ANTHROPIC_AUTH_TOKEN` keeps its authentication+ semantics through proof and actual CPA requests; missing/wrong credentials+ still return 401, and authenticated schema probes still return 400.+ Custom API-key-header settings/shell controls must instead show the explicit+ transport blocker before any upstream request, preserving files and Sources.- The Playwright lane consumes this ONLY over HTTP via env`VIBE_E2E_MOCK_UPSTREAM_URL`; it never imports the Python module. Mock-dependent specs skip with a clear message when the env is absent.diff --git a/docs/plans/model-hub-persisted-migration.md b/docs/plans/model-hub-persisted-migration.mdnew file mode 100644index 000000000..ffde9df06--- /dev/null+++ b/docs/plans/model-hub-persisted-migration.md@@ -0,0 +1,214 @@+# Persisted native configuration migration++## Goal and scope++Migrate user-consented, persisted native authentication into Model Hub without+treating the controller's inherited environment or Hub launch overrides as+credentials to import or as blanket blockers. This fixes a reproduced regression+where all three already-Hub backends were disabled in the migration dialog.+No real credentials, native CLI, production-state testing, deployment, or restart+is authorized by this implementation task.++## Boundary contract++- Discovery reads persisted CLI configuration, existing supported native stores,+ and user shell startup files. Runtime authentication environment values are+ neither candidates nor credential suppliers. Environment variables which+ locate configuration directories remain path configuration, not credentials.+- Shell discovery never executes, sources, expands, or evaluates shell code.+ Support literal assignments in `.profile`, `.bash_profile`, `.bash_login`,+ `.bashrc`, `.zshenv`, `.zprofile`, `.zshrc`, and `.zlogin`. Quoted literal+ values, comments, Unicode, and CRLF must preserve their meaning and bytes.+ Dynamic credential expressions, control flow, command substitutions and+ ambiguous values cannot become guessed credentials; report a specific+ source and remediation, leaving all bytes untouched. Ordinary unrelated+ shell commands/includes are not credential candidates and are never+ followed. This is an inventory of direct persisted assignments, not an+ emulation of shell execution or a recursive scan of arbitrary scripts.+ The bounded writer surface is derived from the Bash 5.3 and Zsh 5.9 core+ command manuals/source, not from whichever branches already exist in the+ parser. It includes explicit named-output/declaration destinations, written+ code/expansion operands, and explicit target-bearing shell syntax (including+ named file-descriptor allocation and named Bash coprocs). Core aliases and+ command modifiers belong to that inventory too. A checked-in coverage ledger+ classifies every core command/alias and relevant syntax as a role handler,+ ordinary data/query, or an explicit exclusion, with primary source version/+ digest and independent positive/negative consuming cases. Tests must detect+ unclassified entries and missing role coverage.+ Classify argument roles, including attached/combined destination options,+ array destinations, documented default destinations and explicit arithmetic+ writes. Code, expansion-only text, destinations, data, queries and filenames+ are distinct roles. In particular, `source`/`.` take a filename and argv,+ not an embedded shell program; neither an assignment-shaped filename nor an+ argument is code, and the included file is never followed. Known callback+ bodies are inspected only as written; named functions and external content+ are not resolved. Output/query-only options do not write.+ Zsh numeric declarations (`integer`/`float`, `typeset -i/-E/-F`) can coerce+ an existing explicitly named value even without `=`; inventory those targets+ as dynamic, never as guessed numeric credentials. Ordinary attribute-only+ export/readonly declarations remain non-candidates. Written tied-declaration+ destinations are local evidence, not authority to follow a tie across+ commands. Written `emulate -c` bodies retain Zsh's core role inventory:+ emulation changes options, not the interpreter or builtin table. Do not+ reinterpret `emulate sh` as Bash/POSIX commands or simulate compatibility+ option state; it does not change the startup file's persistent dialect.+ Bash/POSIX startup paths and Zsh startup paths use their respective option+ semantics. `.profile` conservatively recognizes explicit Bash extensions+ as ambiguous without claiming that every POSIX shell executes them.+ Nested written code operands must retain the same role analysis: reaching+ an analysis limit cannot silently mean "no writer". Prefer a finite iterative+ traversal; any necessary limit must preserve an actionable uncertainty in+ the known code context, not manufacture credential values.+ This remains bounded syntax recognition, not execution, alias/nameref+ resolution, effective precedence, history replay, implicit shell-state+ simulation or arbitrary expansion. Module-provided commands and user-defined+ extensions are excluded; that does not exclude a core command's explicit+ named output such as `zmodload -P`. Unknown ordinary commands are not blocked+ merely because their data mentions a credential variable. The guarantee is+ inventory of persisted literals and refusal of explicit related writers+ within this fixed surface, not restoration of the effective environment or+ proof that an arbitrary startup script cannot regenerate a credential.+- File references to environment variables resolve only from unambiguous+ persisted literal assignments, never `os.environ`. Unresolved references+ describe a missing persistent value, not a permission failure.+- Saved API keys use the existing whitespace-normalization policy once at+ inventory construction. Proof, identity reuse and permanent custody consume+ that same value. Cleanup compares static API-key fields using that same+ canonical value, while exact raw file snapshots and shell assignments remain+ separate consent/cleanup evidence. Canonical equality never substitutes for+ an exact snapshot check, including whitespace-only concurrent changes.+ Unsupported OAuth fields and environment references are not reinterpreted+ as normalized static credentials.+- Every credential keeps its original target and authentication semantics.+ A static bearer token is not assumed to be an API-key header or an OAuth grant.+ Any unsupported transport must have an honest, actionable row-level reason.+ For custom Anthropic endpoints, an opt-in private `auth_scheme="bearer"`+ follows the immutable credential ref through transient proof, provisioning,+ discovery, probes, reuse, replacement, retarget and engine configuration.+ Absent metadata keeps legacy behavior; unknown schemes fail closed. Official+ Anthropic endpoint Bearer and tokens triggering the pinned engine's+ `sk-ant-oat` OAuth heuristic are not reinterpreted or imported.+ Conversely, saved Anthropic API-key-header credentials for custom endpoints+ cannot migrate: the pinned engine sends Bearer there. Migration refuses+ this mismatch before proof/reuse/custody and preserves the original files;+ it never treats accepting both headers as permission to change semantics.+ This migration-only check does not change legacy public Source behavior.+ Public Source/Binding/Record shapes stay unchanged. The shared pure validator+ is `validate_api_key_auth_scheme(vendor, protocol, base_url, secret,+ auth_scheme)` in `api_key_vendors.py`; `protocol=None` is only for transient+ credentials. Adapter provision/transient/matching gain an optional keyword;+ private `_require_proven_source_payload` forwards it separately from public+ source payloads. Existing calls omit that keyword when it is absent.+ The first released explicit Bearer implementation also binds the scheme to+ an opaque immutable `cred_auth_bearer_<32 random hex characters>` ref. Only+ runtime state decodes this reserved namespace; unknown/malformed tags refuse.+ Legacy API-key and OAuth ref generation is unchanged. Every full credential+ consumer verifies tagged-ref/metadata agreement; deleting metadata's scheme+ cannot downgrade a tagged credential. Valid pre-release plain-ref Bearer+ metadata remains readable; replacement/retarget creates a fresh tagged ref.+ Complete loss of such an unpublished untagged Bearer record is unrecoverable+ from its ref alone and is not a supported upgrade guarantee.+ Replacement's scheme accessor does not require the old secret. For safe+ missing or content-corrupt documents it may use the ref: published plain+ API-key refs mean legacy, tagged refs mean Bearer. Readable documents still+ require API-key kind and known consistent scheme; unsafe paths, permissions,+ symlinks and genuine I/O errors never fall back. The replacement key passes+ ordinary target/scheme validation and authenticated discovery before commit.+ Existing revocation journal operation `revoke_api_key_credential` and the+ same-named adapter method carry explicit API-key-only retirement intent.+ They remove only the unbound ref's safe private namespace, durably, without+ requiring its old secret or deleting any OAuth auth-name file. The generic+ OAuth cleanup sequence is unchanged; unreadable/invalid is not proof of+ absence and cannot discard a pending retirement.+- Existing backend custody, authenticated proof, source reuse, cooperative+ lease/drain, durable journal, no-op guards, and recovery remain authoritative.+ Shared shell assignments must not be removed for unconsented consumers.+ Do not replace the takeover state machine or add a parallel journal.+- Consent binds the complete relevant file snapshots, including absent paths+ that could introduce a new grant. Cleanup removes only consented assignment+ lines, preserves unrelated bytes, and uses existing `NativeFileEdit` recovery.+ A changed file, new relevant layer, or ambiguous assignment fails closed+ before exposure. Already exposed grants never roll back to old credentials.+ Compare-only guards do not represent a native write or grant exposure, and+ rollback never restores their bytes. Recheck them after runtime start as well.+ Shell edits retain captured permission bits through journal serialization,+ forward/reverse writes and interrupted-write replay. The transaction writes+ bytes and mode on a temporary inode, rechecks the source and publishes them+ together; replay verifies the target and completes file/directory durability,+ never chmods a published path. Optional `before_mode` records actual captured+ evidence, not a legacy default. This is compare-before-write, not a claim of+ cross-process atomic CAS. Compare-only guards never claim permissions;+ Avibe-owned journal/state files remain owner-private.+ Atomic publication mechanics remain owned by `config.atomic_io.write_atomic`.+ Optional `mode` defaults to `0600`; optional `before_replace` runs after the+ temporary bytes/mode are durable, immediately before publication. The caller+ owns source validation and strict directory durability. Guard failure cleans+ only the unpublished temporary file. No-op/replay paths do not call this writer.+ Shared-reference guards cover configuration/profile consumers, not unrelated+ backend credential-store bytes. Credential snapshots stay backend-scoped.+ Completed receipts retain optional opaque `inventory_ids`, distinct from+ snapshot-bound consent IDs. Fresh consent for the exact restored old+ credential/target may clean it again only while the receipt's current Hub+ Source/ref ownership still matches; it must never reprovision an old OAuth+ grant. Legacy receipts remain readable: their original leaf IDs supply the+ same exact inventory proof, after fresh full-file consent and ownership+ validation. An unrelated or changed grant is not inferred to match.+ Matching applies per consented item, not to the whole selection, so adding+ another backend or restoring a subset cannot reprovision an old grant.+ The existing receipt Source bundle is retained and verified as a whole,+ without inventing a one-to-one item/Source mapping. A changed OAuth snapshot+ for a backend already covered by that receipt cannot prove new authorization:+ refuse cleanup/provisioning and direct the user to existing Hub reauthentication.+ An entirely Source-free receipt is an empty-only confirmation and does not+ assert OAuth custody for that batch. Mixed receipts cannot prove which opaque native+ container was empty: conservatively require Hub reauthentication there.+ A cleaned container revision alone does not establish absence of prior OAuth.+ Optional private `oauth_custody_backends: list[str]` preserves a monotonic+ reauthorization requirement across subsequent completed batches, including+ API-only and empty batches. It is bounded by the supported native OAuth+ backends (Claude and Codex), not+ a secret/grant history or another custody owner. The journal produces/merges+ and validates it; fresh-consent apply consumes it before proof/provision or+ cleanup. Exact last-bundle matches retain the existing cleanup-only path.+ Earlier-batch unmatched OAuth requires Hub reauthorization even if its old+ Source was removed or changed. An existing legacy receipt without the field+ has unknown overwritten history: conservatively mark all supported backends,+ including when that last receipt is Source-free. A genuinely new history+ writes an explicit empty list when no OAuth custody was transferred. Thus+ legacy upgrade can require an extra OAuth authorization, never speculative+ reprovisioning of a restored native refresh grant. Completion/recovery must+ carry this evidence monotonically across receipt-save/active-journal-forget+ interruptions and terminal needs-auth outcomes.+- Public scan adds `source_paths: list[str]` (display-safe file locators, never+ secret content). Existing payload fields remain compatible. Blocked rows+ use specific `settings.models.migration.blocked.*` keys; UI shows every+ distinct source/reason instead of only the first group warning. An empty+ inventory is not represented by a fake unselectable credential.+- Public `required_backends: list[str]` adds explicit shared-file consent.+ Empty/absent means the row's own backend. Connected backend groups consuming+ the same `(file, variable)` are selected together by UI; blockers propagate+ across that closure with their concrete source/reason. The server independently+ rejects partial consent before provisioning, and guards against a new consumer+ appearing during proof. This reuses backend custody rather than creating+ another transaction owner.+ Backend grouping is not proof that every reference was removed. Cleanup+ checks the planned native after-images: preserve endpoint-only shell values+ still referenced by another configuration; refuse removal of an authentication+ variable with surviving consumers, with a source-specific reason on its+ existing row. No fake credential is created for a base-only provider.+- Do not loosen actual Keychain permission errors or silently discard an+ unsupported persisted credential to make an entire backend selectable.++## Ownership and validation++Orchestrator owns scanner/planner/lifecycle integration and final delivery.+Independent lanes may own a pure shell parser, UI presentation, and read-only+credential transport audit only after this contract is committed.++Required evidence: fixture reproduction of populated inherited environment+with already-Hub agents; literal shell discovery/reference resolution;+dynamic/refusal cases without execution; exact cleanup/reverse/recovery and+concurrent edits; public/wrong-key rejection with native files preserved;+UI selection and source-specific instructions; focused Python and UI tests,+changed-file Ruff, UI build, then current-head automatic Codex review and CI.+Real-account OAuth/Keychain acceptance remains manual and unperformed.diff --git a/docs/plans/model-hub-shell-core-coverage.md b/docs/plans/model-hub-shell-core-coverage.mdnew file mode 100644index 000000000..8c678b6da--- /dev/null+++ b/docs/plans/model-hub-shell-core-coverage.md@@ -0,0 +1,330 @@+# Bounded shell migration: pinned core role coverage++This ledger starts from upstream command tables, not parser dispatch. It is a+maintenance gate for persisted literal inventory and explicit related writers,+not a proof of an effective shell environment. Only literal top-level complete+assignment lines are importable; every role below only produces a refusal.+No shell, callback, module, included file, history or environment is executed.++## Primary sources and reproducible census++Sources were downloaded/read, never built or run. All paths below are relative+to these pinned distributions, so this ledger has no workstation dependency.++```text+Bash 5.3: https://ftp.gnu.org/gnu/bash/bash-5.3.tar.gz+SHA256: 0d5cd86965f869a26cf64f4b71be7b96f90a3ba8b3d74e27e8e9d9d5550f31ba+Zsh 5.9: https://github.com/zsh-users/zsh/tree/zsh-5.9+Doc/Zsh/builtins.yo: 9460fd0ca006f6d81d6ab6996ae27ae55a1d5d16dfea3a6a436d820cf81ce2bb+Src/builtin.c: f0de0056497f68ef6f1f3ee8b2f778673c616480cf5ffd74a5638729ef615537+Doc/Zsh/grammar.yo: 68ed5dcd2b8e6d6c29ada28770b29b077e1686cc6e1ee16fc2ff6e01472c6b65+Src/cond.c: da1f17d35d703120455c55124250af820f755b8ad62f276a6632d731a13d8952+Src/module.c: a0a407648c7cc7f451fb620c246cfb0e5d6d9356953602f9a36c86d42e4cf830+```++Bash census: collect every `$BUILTIN ` directive from `builtins/*.def`, then+deduplicate. There are **77** spellings, including the help-only `reserved.def`+entries; this is not a claim of 77 executable builtins. Zsh census: collect+`BUILTIN` and `BIN_PREFIX` names in `Src/builtin.c`, excluding the three+conditional debug entries `hashinfo`, `mem`, `patdebug`: **76** names.+The test pins independent SHA256s of sorted newline-joined upstream names,+rejects duplicate/missing/unclassified rows, checks handler bindings, and+requires both writer and nonwriter consuming cases for every role. It needs+neither source downloads nor a shell executable in CI.++For Bash rows, short `.def` locators are under `builtins/`; Zsh handler names+are in `Src/builtin.c` unless otherwise specified. The supplemental syntax+rows are the authorized finite syntax surface, not another full grammar census.++`role`: explicit output, code, expansion, argv or target syntax.+`none`: no relevant role under this boundary (ordinary/query/data).+`excluded`: behavior depends on deliberately unmodelled content or state.++## Complete pinned catalogue++| Dialect | Name | Class | Roles | Primary locator / disposition |+| --- | --- | --- | --- | --- |+| bash | ! | role | prefix | reserved.def; pipeline prefix |+| bash | % | excluded | - | reserved.def; job state |+| bash | (( ... )) | role | arithmetic | reserved.def; arithmetic syntax |+| bash | . | excluded | - | source.def; filename and argv, no following |+| bash | : | none | - | colon.def |+| bash | [ | none | - | test.def; decimal/string operands |+| bash | [[ ... ]] | excluded | - | reserved.def; no full condition/pattern grammar |+| bash | alias | excluded | - | alias.def; alias resolution |+| bash | bg | none | - | fg_bg.def |+| bash | bind | role | bind | bind.def; bashline.c bind_keyseq_to_unix_command |+| bash | break | none | - | break.def; integer operand, not evaluation |+| bash | builtin | role | wrapper | builtin.def; argv |+| bash | caller | none | - | caller.def |+| bash | case | role | nested | reserved.def; written body only |+| bash | cd | none | - | cd.def; implicit state excluded |+| bash | command | role | wrapper | command.def; argv, not queries |+| bash | compgen | role | compgen-target,compgen-source,compgen-expansion | complete.def; pcomplete.c |+| bash | complete | role | complete-source,complete-expansion | complete.def; pcomplete.c |+| bash | compopt | none | - | complete.def; completion options only |+| bash | continue | none | - | break.def |+| bash | coproc | role | coproc | reserved.def; parse.y; execute_cmd.c |+| bash | declare | role | declaration | declare.def |+| bash | dirs | none | - | pushd.def |+| bash | disown | none | - | jobs.def |+| bash | echo | none | - | echo.def |+| bash | enable | excluded | - | enable.def; custom/loadable builtins |+| bash | eval | role | eval-source,code-worklist | eval.def |+| bash | exec | excluded | - | exec.def; external commands, redirections handled separately |+| bash | exit | none | - | exit.def |+| bash | export | role | declaration | setattr.def |+| bash | false | none | - | colon.def |+| bash | fc | role | fc | fc.def; explicit editor only, no history reconstruction |+| bash | fg | none | - | fg_bg.def |+| bash | for | role | loop-target,nested | reserved.def |+| bash | for (( | role | arithmetic,nested | reserved.def |+| bash | function | role | nested | reserved.def; written body, no resolution |+| bash | getopts | role | getopts | getopts.def |+| bash | hash | none | - | hash.def |+| bash | help | none | - | help.def |+| bash | history | excluded | - | history.def; stored history is not inline code |+| bash | if | role | nested | reserved.def |+| bash | jobs | role | jobs | jobs.def execute_list_with_replacements; argv |+| bash | kill | none | - | kill.def |+| bash | let | role | arithmetic | let.def |+| bash | local | role | declaration | declare.def |+| bash | logout | none | - | exit.def |+| bash | mapfile | role | array-reader,array-callback | mapfile.def |+| bash | popd | none | - | pushd.def |+| bash | printf | role | printf-target,printf-format | printf.def |+| bash | pushd | none | - | pushd.def |+| bash | pwd | none | - | cd.def |+| bash | read | role | read-output | read.def |+| bash | readarray | role | array-reader,array-callback | mapfile.def |+| bash | readonly | role | declaration | setattr.def |+| bash | return | none | - | return.def |+| bash | select | role | loop-target,nested | reserved.def |+| bash | set | none | - | set.def; positional and option state excluded |+| bash | shift | none | - | shift.def; positional state excluded |+| bash | shopt | none | - | shopt.def |+| bash | source | excluded | - | source.def; filename and argv, no following |+| bash | suspend | none | - | suspend.def |+| bash | test | none | - | test.def; decimal/string operands |+| bash | time | role | prefix | reserved.def |+| bash | times | none | - | times.def |+| bash | trap | role | trap | trap.def; written action only |+| bash | true | none | - | colon.def |+| bash | type | none | - | type.def |+| bash | typeset | role | declaration | declare.def |+| bash | ulimit | none | - | ulimit.def |+| bash | umask | none | - | umask.def |+| bash | unalias | excluded | - | alias.def; alias resolution |+| bash | unset | role | unset | set.def |+| bash | until | role | nested | reserved.def |+| bash | variables | excluded | - | reserved.def; implicit special state |+| bash | wait | role | wait | wait.def; unset before jobs, last -p, array lvalue |+| bash | while | role | nested | reserved.def |+| bash | { ... } | role | nested | reserved.def |+| zsh | - | role | zsh-prefix | BIN_PREFIX |+| zsh | . | excluded | - | bin_dot; filename and argv |+| zsh | : | none | - | bin_true |+| zsh | [ | role | test-fd | bin_test; Src/cond.c COND_ISTTY |+| zsh | alias | excluded | - | bin_alias; alias resolution |+| zsh | autoload | excluded | - | bin_functions; external/named functions |+| zsh | bg | none | - | bin_fg |+| zsh | break | role | zsh-control | bin_break; arithmetic before control transfer |+| zsh | builtin | role | wrapper | BIN_PREFIX |+| zsh | bye | role | zsh-control | bin_break |+| zsh | cd | none | - | bin_cd; implicit state excluded |+| zsh | chdir | none | - | bin_cd |+| zsh | command | role | wrapper | BIN_PREFIX |+| zsh | continue | role | zsh-control | bin_break |+| zsh | declare | role | declaration,numeric-declaration,tied | bin_typeset |+| zsh | dirs | none | - | bin_dirs |+| zsh | disable | none | - | bin_enable |+| zsh | disown | none | - | bin_fg |+| zsh | echo | none | - | bin_print; no format/target options |+| zsh | emulate | role | emulate | bin_emulate; written local -c only |+| zsh | enable | excluded | - | bin_enable; enabled/custom command state |+| zsh | eval | role | eval-source,code-worklist | bin_eval |+| zsh | exec | role | zsh-prefix | BIN_PREFIX; argv0 is data |+| zsh | exit | role | zsh-control | bin_break |+| zsh | export | role | declaration,numeric-declaration,tied | bin_typeset |+| zsh | false | none | - | bin_false |+| zsh | fc | role | zsh-fc | bin_fc; fcedit passes editor text to execstring |+| zsh | fg | none | - | bin_fg |+| zsh | float | role | numeric-declaration | bin_typeset; default E |+| zsh | functions | excluded | - | bin_functions; named-function resolution |+| zsh | getln | role | getln | bin_read; default zr, echo control |+| zsh | getopts | role | getopts | bin_getopts |+| zsh | hash | none | - | bin_hash |+| zsh | history | excluded | - | bin_fc; default l, no history reconstruction |+| zsh | integer | role | numeric-declaration | bin_typeset; default i |+| zsh | jobs | none | - | bin_fg; no Bash -x |+| zsh | kill | none | - | bin_kill |+| zsh | let | role | arithmetic | bin_let |+| zsh | local | role | declaration,numeric-declaration,tied | bin_typeset |+| zsh | logout | role | zsh-control | table maximum arity 1; bin_break evaluates it |+| zsh | noglob | role | zsh-prefix | BIN_PREFIX |+| zsh | popd | none | - | bin_cd |+| zsh | print | role | print-target,print-format | bin_print; -R/query/conflict boundaries |+| zsh | printf | role | printf-target,printf-format | bin_print; percent-n and numeric format slots |+| zsh | pushd | none | - | bin_cd |+| zsh | pushln | none | - | bin_print; default nz, no target/format options |+| zsh | pwd | none | - | bin_pwd |+| zsh | r | excluded | - | bin_fc; implicit history replay |+| zsh | read | role | read-output,read-timeout | bin_read; optional numeric timeout evaluation |+| zsh | readonly | role | declaration,numeric-declaration,tied | bin_typeset |+| zsh | rehash | none | - | bin_hash |+| zsh | return | role | zsh-control | bin_break |+| zsh | set | role | set-array | bin_set; +/-A, no positional inference |+| zsh | setopt | none | - | bin_setopt |+| zsh | shift | role | shift | bin_shift; explicit count/named arrays |+| zsh | source | excluded | - | bin_dot; filename and argv |+| zsh | suspend | none | - | bin_suspend |+| zsh | test | role | test-fd | bin_test; Src/cond.c decimal comparisons are data |+| zsh | times | none | - | bin_times |+| zsh | trap | role | zsh-trap | bin_trap |+| zsh | true | none | - | bin_true |+| zsh | ttyctl | none | - | bin_ttyctl |+| zsh | type | none | - | bin_whence |+| zsh | typeset | role | declaration,numeric-declaration,tied | bin_typeset |+| zsh | umask | none | - | bin_umask |+| zsh | unalias | excluded | - | bin_unhash; alias resolution |+| zsh | unfunction | excluded | - | bin_unhash; named-function resolution |+| zsh | unhash | none | - | bin_unhash |+| zsh | unset | role | unset | bin_unset; no pattern-to-variable expansion |+| zsh | unsetopt | none | - | bin_setopt |+| zsh | wait | none | - | bin_fg; no Bash -p |+| zsh | whence | none | - | bin_whence |+| zsh | where | none | - | bin_whence |+| zsh | which | none | - | bin_whence |+| zsh | zcompile | excluded | - | bin_zcompile; external source/file content |+| zsh | zmodload | role | zmodload | Src/module.c bin_zmodload_features; output only |+| syntax | assignment | role | assignment | Bash parse.y; Zsh grammar.yo; direct literal cleanup only |+| syntax | named-fd | role | fd | Bash redir.c redir_varassign; Zsh grammar.yo |+| syntax | parameter-assignment | role | parameter-assignment | Bash bashref.texi; Zsh expansion roles |+| syntax | command-substitution | role | substitution | written source, not its output |+| syntax | include-operands | role | include | only outer expansions; filename/argv never code |+| syntax | other-grammar | excluded | - | no full interpreter, condition/pattern/module grammar |++## Role-to-handler and consuming-test bindings++Every role below is a `CASES` tag in+`tests/test_model_hub_shell_core_inventory.py`. Each tag has both positive+and query/data/invalid controls, consumed by exact cleanup and the actual+fixture migration service. Every positive also runs standalone writer scan.+The pre-existing shell and writer-role suites remain additional coverage.++| Role | Handler |+| --- | --- |+| wait | _builtin_writers |+| compgen-target | _completion_writers |+| compgen-source | _completion_writers |+| compgen-expansion | _completion_writers |+| complete-source | _completion_writers |+| complete-expansion | _completion_writers |+| trap | _trap_writers |+| bind | _bind_source |+| jobs | _jobs_writers |+| fc | _fc_writers |+| coproc | _command_writers |+| fd | _command_writers |+| include | _WrittenCode |+| code-worklist | _WrittenCode |+| print-target | _zsh_print_writers |+| print-format | _zsh_print_writers |+| set-array | _zsh_writers |+| tied | _declaration_writers |+| getln | _read_writers |+| shift | _zsh_writers |+| read-timeout | _read_writers |+| test-fd | _zsh_test_writers |+| zsh-trap | _trap_writers |+| emulate | _zsh_writers |+| zsh-fc | _fc_writers |+| zmodload | _zsh_writers |+| zsh-prefix | _argv_writers |+| numeric-declaration | _declaration_writers |+| zsh-control | _zsh_writers |+| declaration | _declaration_writers |+| wrapper | _argv_writers |+| read-output | _read_writers |+| getopts | _builtin_writers |+| unset | _builtin_writers |+| printf-target | _printf_writers |+| printf-format | _printf_operand_writers |+| arithmetic | _arithmetic_writers |+| substitution | _WrittenCode |+| parameter-assignment | _WrittenCode |+| assignment | _assignment_writers |+| eval-source | _builtin_writers |+| loop-target | _command_writers |+| nested | _parse |+| prefix | _segments |+| array-reader | _array_reader_writers |+| array-callback | _array_reader_writers |++## Invariants and deliberate limits++- Bash `wait` may unset before checking jobs or `-n`; its target allows array+ lvalues. Bash 5.3 `compgen -V` requires an identifier, not an array lvalue.+- `-C` completion callbacks and trap/editor/Readline command parts are source.+ `-W` is only expansion text: literal `KEY=value` is data. `jobs -x` and+ command modifiers route argv, never prefix assignments. Includes have no+ source role, even for an assignment-shaped filename or argument.+ `pcomplete.c` gen_wordlist_matches splits/dequotes words and expands them,+ not source statements. Expansion-only traversal uses the shared word lexer+ without source comments or heredoc dispatch: `#` cannot hide a later written+ substitution, while single quotes and escaped dollars remain protective.+- Completion F/V/A/o operands are validated as each option is consumed,+ including overwritten occurrences. Unknown values are not proven invalid.+ `complete.def` clears F's WORD flags, then uses `general.c` check_identifier+ with runtime POSIX mode and `syntax.h` shell_break_chars. Without inferring+ that mode, hyphens, equals signs, digits and empty F values do not prove+ rejection; break characters do. F remains a function name, never code.+ V still requires an identifier; A and o use the complete pinned tables+ (including Bash 5.3 `fullquote`). Independent fixtures cover every named+ action/option, unknown values, repeated occurrences and query/data boundaries.+- Named/default Bash coprocs account for both the array and `_PID`. The simple+ command form has no named-coproc slot. Following `execute_cmd.c`, a written+ name is validated after static dequoting; dynamic names are not resolved or+ replaced with the default name. A named fd requires unquoted logically+ adjacent braces, including backslash-newline removal as in `parse.y`.+ Raw spans/byte offsets stay unchanged; real whitespace, quoted braces and+ closing an existing fd do not allocate a named descriptor.+- Zsh numeric coercion has a written destination even without `=`; ordinary+ attribute-only export/readonly remains nonwriting. Tied declarations inspect+ only the two written targets, never propagate a tie/nameref to another line.+- Profile filenames select dialect. `.profile` conservatively inventories+ explicit Bash extension writers without claiming POSIX execution. `emulate`+ changes Zsh options, not its builtin table: every recognized written `-c`+ body keeps Zsh roles, including with sh/ksh/csh/unknown/dynamic mode names.+ No compatibility-option state or host environment is inferred.+- Zsh `eval` has a NULL option alphabet in `Src/builtin.c`: `execbuiltin`+ removes only one initial standalone `--`, then `bin_eval` joins written+ source. Other dash-leading text is not an invalid option. An unknown initial+ word can supply that one boundary, but it does not enable letter options.+ Tests distinguish Bash invalid options, Zsh command/data text, split source,+ quoted/unknown boundaries, and a second `--` that must remain source.+ Numeric control commands share this envelope before their maximum-one+ operand check. Trap's BINF_HANDLES_OPTS registration bypasses the generic+ envelope, but bin_trap owns the equivalent single `--` removal. All three+ use one bounded helper with at most two cases; no repeated stripping or+ environment lookup occurs.+- Zsh `bin_print` guards format reuse with `!OPT_ISSET(ops,'r')`, documented+ in `builtins.yo` under printf. Print's projected role state retains `r` and+ consumes only one format pass when present, including positional/star slots.+ Unknown formats retain their possible explicit target/numeric roles;+ ordinary print and printf still reuse the format.+- The worklist keys dialect, role and written source/argv. It has no recursion+ cutoff. Tests bound visited contexts for 1100 nested evals and 200 duplicate+ callbacks and 200 repeated substitutions after `#`; both the related writer+ and echo/data controls run. Option tests+ bound call counts, not wall time; role projection does not retain combinations+ of unrelated callback/format/target values.+- Excluded: modules/custom builtins; alias, nameref and named-function+ resolution; include/history/runtime-environment inference; expansion outputs,+ computed/pattern lvalues; implicit shell-special state; execution order,+ reachability and command success. Reader/default destinations already in the+ finite contract remain recognized. `env` keeps its existing bounded GNU+ argv/assignment handler; it is not presented as a Bash or Zsh core builtin.+- This gate cannot prove source interpretation or effective shell behavior.+ Any new upstream version/role requires source review and independent fixtures,+ not regeneration of the census from production dispatch.diff --git a/tests/e2e/drivers/mock_llm_upstream.py b/tests/e2e/drivers/mock_llm_upstream.pyindex 359e2b879..415b8b371 100644--- a/tests/e2e/drivers/mock_llm_upstream.py+++ b/tests/e2e/drivers/mock_llm_upstream.py@@ -197,6 +197,7 @@ class MockUpstreamState:self._config: dict[str, Any] = {"auth": "ok","required_api_key": None,+ "required_auth_scheme": "protocol","stream": "healthy","models": [copy.deepcopy(model) for model in DEFAULT_MODELS],"protocol": "openai_chat",@@ -215,6 +216,7 @@ class MockUpstreamState:unknown = set(update) - {"auth","required_api_key",+ "required_auth_scheme","stream","models","protocol",@@ -234,6 +236,8 @@ class MockUpstreamState:not isinstance(required_key, str) or not required_key.strip()):raise ConfigurationError("required_api_key must be a non-empty string or null")+ if candidate["required_auth_scheme"] not in ("protocol", "bearer"):+ raise ConfigurationError("unsupported required_auth_scheme")if candidate["stream"] not in STREAM_BEHAVIORS:raise ConfigurationError("unsupported stream behavior")if candidate["protocol"] not in PROTOCOLS:@@ -386,14 +390,18 @@ class MockLLMUpstreamHandler(BaseHTTPRequestHandler):"""Optional value check for synthetic credentials on data endpoints."""required_key = config["required_api_key"]if required_key is not None:+ use_api_key_header = (+ config["protocol"] == "anthropic"+ and config["required_auth_scheme"] == "protocol"+ )supplied = (self.headers.get("x-api-key")- if config["protocol"] == "anthropic"+ if use_api_key_headerelse self.headers.get("authorization"))expected = (required_key- if config["protocol"] == "anthropic"+ if use_api_key_headerelse f"Bearer {required_key}")if supplied != expected:diff --git a/tests/e2e/test_model_hub_migration.py b/tests/e2e/test_model_hub_migration.pyindex b2338bc4f..2c2502111 100644--- a/tests/e2e/test_model_hub_migration.py+++ b/tests/e2e/test_model_hub_migration.py@@ -36,7 +36,7 @@ def _seed_native_configs(app, upstream_url: str) -> list[Path]:json.dumps({"env": {- "ANTHROPIC_API_KEY": SYNTHETIC_MIGRATION_API_KEY,+ "ANTHROPIC_AUTH_TOKEN": SYNTHETIC_MIGRATION_API_KEY,"ANTHROPIC_BASE_URL": upstream_url,},"permissions": {"allow": ["Read"]},@@ -183,7 +183,7 @@ def test_f2_apply_takes_over_selected_key_and_preserves_blocked_native_material("anthropic",models=[{"id": "claude-sonnet-4-6"}],)- mock_llm_upstream.configure(required_api_key=seeded_api_key)+ mock_llm_upstream.configure(required_api_key=seeded_api_key, required_auth_scheme="bearer")launch, seeded_paths = _launch_seeded_app(model_hub_app_factory, mock_llm_upstream)@@ -256,7 +256,11 @@ def test_f2_apply_takes_over_selected_key_and_preserves_blocked_native_material(for request in mock_llm_upstream.requests()if request["path"] == "/v1/models"]- assert any(headers.get("x-api-key") == seeded_api_key for headers in catalogue_headers)+ assert any(+ headers.get("authorization") == f"Bearer {seeded_api_key}"+ and "x-api-key" not in headers+ for headers in catalogue_headers+ )assert any(not headers.get("x-api-key") and not headers.get("authorization")for headers in catalogue_headers@@ -275,7 +279,7 @@ def test_f2_unproven_key_keeps_all_native_material(mock_llm_upstream, "anthropic",models=[{"id": "claude-sonnet-4-6"}],)- mock_llm_upstream.configure(required_api_key=required_api_key)+ mock_llm_upstream.configure(required_api_key=required_api_key, required_auth_scheme="bearer")launch, seeded_paths = _launch_seeded_app(model_hub_app_factory, mock_llm_upstream,)diff --git a/tests/e2e/test_model_hub_migration_persisted.py b/tests/e2e/test_model_hub_migration_persisted.pynew file mode 100644index 000000000..59d3d45bc--- /dev/null+++ b/tests/e2e/test_model_hub_migration_persisted.py@@ -0,0 +1,434 @@+"""Persisted sources under a populated runtime environment, over HTTP/IPC."""++from __future__ import annotations++import json++import pytest+import yaml++from config.v2_config import V2Config+from core.handlers.model_hub.migration_journal import NativeTakeoverJournal+from tests.e2e.test_model_hub_migration import _write+from tests.e2e.test_model_hub_mock_upstream import _json_request+from tests.e2e.test_model_hub_sources import _configure_protocol++pytestmark = pytest.mark.e2e_model_hub++KEY = "fixture-persisted-migration-static-key"+RUNTIME_ENV = {+ "ANTHROPIC_API_KEY": "fixture-runtime-anthropic",+ "ANTHROPIC_AUTH_TOKEN": "fixture-runtime-bearer",+ "ANTHROPIC_BASE_URL": "http://127.0.0.1:9",+ "OPENAI_API_KEY": "fixture-runtime-openai",+ "OPENAI_BASE_URL": "http://127.0.0.1:9",+}+++def _seed_hub(app) -> None:+ app._initialize_config()+ path = app.avibe_home / "config/config.json"+ payload = json.loads(path.read_text())+ config = V2Config.default()+ config.model_hub.enabled = True+ for backend in ("claude", "codex", "opencode"):+ config.model_hub.agents[backend].mode = "hub"+ payload["model_hub"] = config.model_hub.to_payload()+ _write(path, json.dumps(payload))+++def _assert_cpa_bearer_consumption(app, upstream, key: str) -> None:+ """Read only fixture engine output; consume it through the actual CPA."""+ status = app.client.get("/api/models/runtime/status").json()["runtime"]["status"]+ listening = status["listening"]+ [config_path] = list((app.avibe_home / "runtime/model-hub/state/instances").glob("*/config.yaml"))+ engine_config = yaml.safe_load(config_path.read_text())+ [credential] = engine_config["claude-api-key"]+ upstream.reset_requests()+ result, _, body = _json_request(+ f"http://{listening['host']}:{listening['port']}", "/v1/messages",+ method="POST",+ headers={"X-Api-Key": engine_config["api-keys"][0]},+ body={+ "model": f"{credential['prefix']}/mock-model",+ "max_tokens": 16,+ "messages": [{"role": "user", "content": "fixture only"}],+ },+ timeout=20,+ )+ assert result == 200, body+ [request] = [r for r in upstream.requests() if r["path"] == "/v1/messages"]+ assert request["headers"]["authorization"] == f"Bearer {key}"+ assert "x-api-key" not in request["headers"]++[email protected]("shape", [+ "claude-shell", "claude-bearer", "codex-shell", "opencode-file",+ "claude-shell-padded", "codex-shell-padded",+ "claude-bearer-padded", "codex-file-padded", "opencode-file-padded",+])+def test_f4_persisted_configuration_migrates_with_runtime_auth_present(+ model_hub_app_factory, mock_llm_upstream, shape,+):+ """F4: inherited values neither supply nor block a persisted credential."""+ backend = shape.split("-")[0]+ shell_fixture = "-shell" in shape+ protocol = "anthropic" if backend == "claude" else "openai_responses"+ _configure_protocol(mock_llm_upstream, protocol, models=[{"id": "mock-model"}])+ mock_llm_upstream.configure(+ required_api_key=KEY,+ required_auth_scheme="bearer" if backend == "claude" else "protocol",+ )+ source_paths = []+ retained = "# 保留终端偏好\nexport EDITOR='vim'\n"++ def seed(app):+ _seed_hub(app)+ saved_key = f" \t{KEY} \t" if shape.endswith("-padded") else KEY+ if shell_fixture:+ prefix = "ANTHROPIC" if backend == "claude" else "OPENAI"+ credential_name = "ANTHROPIC_AUTH_TOKEN" if backend == "claude" else "OPENAI_API_KEY"+ path = app.home / ".bashrc"+ _write(path, retained + (+ f"export {credential_name}='{saved_key}'\n"+ f"export {prefix}_BASE_URL='{mock_llm_upstream.url}'\n"+ ))+ path.chmod(0o640)+ elif shape.startswith("claude-bearer"):+ path = app.home / ".claude/settings.json"+ _write(path, json.dumps({+ "env": {"ANTHROPIC_AUTH_TOKEN": saved_key, "ANTHROPIC_BASE_URL": mock_llm_upstream.url},+ "permissions": {"allow": ["Read"]},+ }))+ elif shape == "codex-file-padded":+ path = app.home / ".codex/config.toml"+ _write(path, (+ '[model_providers.fixture]\nname="Preserved fixture"\n'+ f"experimental_bearer_token={json.dumps(saved_key)}\n"+ f"base_url={json.dumps(mock_llm_upstream.url)}\n"+ ))+ else:+ path = app.home / ".config/opencode/opencode.json"+ _write(path, json.dumps({+ "provider": {"openai": {"options": {"apiKey": saved_key, "baseURL": mock_llm_upstream.url}}},+ "theme": "system",+ }))+ source_paths.append(path)++ with model_hub_app_factory(extra_env=RUNTIME_ENV, before_start=seed) as app:+ before_env = dict(app.env)+ response = app.client.post("/api/models/migration/scan", {})+ assert response.status == 200, response.json()+ rows = response.json()["scan"]["items"]+ assert len(rows) == 1, rows+ assert rows[0]["backend"] == backend+ assert rows[0]["selected"] is True+ assert rows[0]["proposed_action"] == "import"+ assert rows[0]["source_paths"] == [str(source_paths[0])]+ assert KEY not in json.dumps(rows)+ applied = app.client.post("/api/models/migration/apply", {"item_ids": [rows[0]["id"]]})+ assert applied.status == 200, applied.json()+ assert applied.json()["applied"] == 1+ [source] = applied.json()["sources"]+ assert source["supply_channel"] == "hub"+ assert app.env == before_env+ if shell_fixture:+ assert source_paths[0].read_bytes() == retained.encode()+ assert source_paths[0].stat().st_mode & 0o777 == 0o640+ elif shape.startswith("claude-bearer"):+ assert json.loads(source_paths[0].read_text()) == {"env": {}, "permissions": {"allow": ["Read"]}}+ elif shape == "codex-file-padded":+ assert KEY not in source_paths[0].read_text()+ assert "Preserved fixture" in source_paths[0].read_text()+ else:+ assert json.loads(source_paths[0].read_text())["theme"] == "system"+ assert app.client.post("/api/models/migration/scan", {}).json()["scan"]["items"] == []++ if backend == "claude":+ # Test the pinned engine consumer, not just direct discovery proof.+ _assert_cpa_bearer_consumption(app, mock_llm_upstream, KEY)++[email protected]("required", [None, "fixture-rejected-key"], ids=["public", "wrong-key"])+def test_f4_unproven_shell_key_is_not_removed(+ model_hub_app_factory, mock_llm_upstream, required,+):+ """F4: a readable static file is not proof of credential authentication."""+ _configure_protocol(mock_llm_upstream, "anthropic")+ mock_llm_upstream.configure(required_api_key=required, required_auth_scheme="bearer")+ paths = []++ def seed(app):+ _seed_hub(app)+ path = app.home / ".zshrc"+ _write(path, (+ f"export ANTHROPIC_AUTH_TOKEN='{KEY}'\n"+ f"export ANTHROPIC_BASE_URL='{mock_llm_upstream.url}'\n"+ ))+ paths.append(path)++ with model_hub_app_factory(extra_env=RUNTIME_ENV, before_start=seed) as app:+ before = paths[0].read_bytes()+ rows = app.client.post("/api/models/migration/scan", {}).json()["scan"]["items"]+ assert len(rows) == 1 and rows[0]["proposed_action"] == "import"+ result = app.client.post("/api/models/migration/apply", {"item_ids": [rows[0]["id"]]})+ assert result.status == 409, result.json()+ assert paths[0].read_bytes() == before+ assert app.client.get("/api/models/sources").json()["sources"] == []++[email protected]("shape", ["settings", "shell"])+def test_f4_custom_api_key_transport_is_refused_without_cleanup(+ model_hub_app_factory, mock_llm_upstream, shape,+):+ """F4: discovery cannot prove a header the pinned engine will not send."""+ _configure_protocol(mock_llm_upstream, "anthropic")+ mock_llm_upstream.configure(required_api_key=KEY, required_auth_scheme="protocol")+ paths = []++ def seed(app):+ _seed_hub(app)+ if shape == "shell":+ path = app.home / ".zshrc"+ _write(path, (+ f"export ANTHROPIC_API_KEY='{KEY}'\n"+ f"export ANTHROPIC_BASE_URL='{mock_llm_upstream.url}'\n"+ ))+ else:+ path = app.home / ".claude/settings.json"+ _write(path, json.dumps({+ "env": {"ANTHROPIC_API_KEY": KEY, "ANTHROPIC_BASE_URL": mock_llm_upstream.url},+ }))+ paths.append(path)++ with model_hub_app_factory(extra_env=RUNTIME_ENV, before_start=seed) as app:+ before = paths[0].read_bytes()+ response = app.client.post("/api/models/migration/scan", {})+ assert response.status == 200, response.json()+ [row] = response.json()["scan"]["items"]+ assert row["selected"] is False+ assert row["notes_key"] == "settings.models.migration.blocked.transport"+ refused = app.client.post("/api/models/migration/apply", {"item_ids": [row["id"]]})+ assert refused.status == 409, refused.json()+ assert paths[0].read_bytes() == before+ assert app.client.get("/api/models/sources").json()["sources"] == []+ assert mock_llm_upstream.requests() == []++[email protected]("profile,writer", [+ (".bashrc", "printf -vOPENAI_API_KEY %s fixture-dynamic"),+ (".bashrc", "read -raOPENAI_API_KEY"),+ (".bashrc", "printf '%n' OPENAI_API_KEY"),+ (".bashrc", "read 'OPENAI_API_KEY[0]'"),+ (".bashrc", 'read "-$FLAGS" OPENAI_API_KEY'),+ (".bashrc", 'printf "$OPTIONS" OPENAI_API_KEY %s fixture-dynamic'),+ (".zshrc", "read -p OPENAI_API_KEY"),+ (".bashrc", "wait -npOPENAI_API_KEY 123"),+ (".bashrc", 'wait "$OPTIONS" OPENAI_API_KEY 123'),+ (".bashrc", "compgen -V OPENAI_API_KEY -W fixture"),+ (".bashrc", "trap 'read OPENAI_API_KEY' DEBUG"),+ (".bashrc", "complete -W '$(read OPENAI_API_KEY)' fixture-command"),+ (".bashrc", "jobs -x read OPENAI_API_KEY"),+ (".bashrc", "exec {OPENAI_API_KEY}>/fixture/data"),+ (".bashrc", "coproc OPENAI_API_KEY { :; }"),+ (".bashrc", "eval " * 32 + "read OPENAI_API_KEY"),+ (".zshrc", "print -v OPENAI_API_KEY fixture"),+ (".zshrc", "set -A OPENAI_API_KEY fixture"),+ (".zshrc", "integer OPENAI_API_KEY"),+ (".zshrc", "getln OPENAI_API_KEY"),+ (".zshrc", "shift 1 OPENAI_API_KEY"),+ (".zshrc", "zmodload -FL -P OPENAI_API_KEY zsh/parameter"),+ (".zshrc", "emulate zsh -c 'read OPENAI_API_KEY'"),+ (".zshrc", "emulate sh -c 'read -p OPENAI_API_KEY'"),+ (".zshrc", "emulate ksh -c 'print -v OPENAI_API_KEY fixture'"),+ (".zshrc", "logout 'OPENAI_API_KEY=1'"),+ (".zshrc", "fc -e 'read OPENAI_API_KEY'"),+ (".bashrc", "compgen -W '# $((OPENAI_API_KEY=1))'"),+ (".bashrc", "coproc 'OPENAI_API_KEY' { :; }"),+ (".bashrc", ": {OPENAI_API_KEY}\\\n>/fixture/data"),+ (".bashrc", ": {OPENAI_\\\nAPI_KEY}>/fixture/data"),+ (".bashrc", 'complete -F "$FUNCTION" -C \'read OPENAI_API_KEY\' fixture'),+ (".zshrc", 'trap "$BOUNDARY" \'read OPENAI_API_KEY\' DEBUG'),+ (".zshrc", 'return "$BOUNDARY" \'OPENAI_API_KEY=1\''),+ (".zshrc", "eval '-Q; read OPENAI_API_KEY'"),+])+def test_f4_explicit_dynamic_writer_refuses_http_apply_without_proof(+ model_hub_app_factory, mock_llm_upstream, profile, writer,+):+ _configure_protocol(mock_llm_upstream, "openai_responses")+ mock_llm_upstream.configure(required_api_key=KEY)+ paths = []++ def seed(app):+ _seed_hub(app)+ path = app.home / profile+ _write(path, (+ f"export OPENAI_API_KEY='{KEY}'\n"+ f"export OPENAI_BASE_URL='{mock_llm_upstream.url}'\n"+ f"{writer}\n"+ ))+ paths.append(path)++ with model_hub_app_factory(extra_env=RUNTIME_ENV, before_start=seed) as app:+ before = paths[0].read_bytes()+ scan = app.client.post("/api/models/migration/scan", {})+ assert scan.status == 200, scan.json()+ [row] = scan.json()["scan"]["items"]+ assert row["selected"] is False+ assert row["notes_key"] == "settings.models.migration.blocked.dynamic_shell"+ refused = app.client.post("/api/models/migration/apply", {"item_ids": [row["id"]]})+ assert refused.status == 409, refused.json()+ assert paths[0].read_bytes() == before+ assert app.client.get("/api/models/sources").json()["sources"] == []+ assert mock_llm_upstream.requests() == []++[email protected]("profile,data", [+ (".bashrc", "source 'OPENAI_API_KEY=fixture-script'"),+ (".bashrc", "rg --fixed-strings OPENAI_API_KEY /fixture/input"),+ (".bashrc", "compgen -W OPENAI_API_KEY=fixture-data"),+ (".bashrc", "wait -p unrelated OPENAI_API_KEY"),+ (".bashrc", "eval " * 32 + "echo OPENAI_API_KEY"),+ (".zshrc", "print -R -v OPENAI_API_KEY"),+ (".zshrc", "emulate sh -c 'wait -p OPENAI_API_KEY'"),+ (".zshrc", "emulate sh -c 'mapfile OPENAI_API_KEY'"),+ (".zshrc", "print -rf '%n' unrelated OPENAI_API_KEY"),+ (".zshrc", "print -r -f '%d' 1 'OPENAI_API_KEY=2'"),+])+def test_f4_shell_data_roles_allow_http_migration_without_changing_data(+ model_hub_app_factory, mock_llm_upstream, profile, data,+):+ """Written argv data is preserved, not executed or promoted into code."""+ _configure_protocol(mock_llm_upstream, "openai_responses", models=[{"id": "mock-model"}])+ mock_llm_upstream.configure(required_api_key=KEY)+ paths = []+ retained = f"# 保留数据与换行\r\n{data}\r\n".encode()++ def seed(app):+ _seed_hub(app)+ path = app.home / profile+ _write(path, (+ f"export OPENAI_API_KEY='{KEY}'\r\n"+ f"export OPENAI_BASE_URL='{mock_llm_upstream.url}'\r\n"+ ))+ path.write_bytes(path.read_bytes() + retained)+ path.chmod(0o640)+ paths.append(path)++ with model_hub_app_factory(extra_env=RUNTIME_ENV, before_start=seed) as app:+ before_env = dict(app.env)+ response = app.client.post("/api/models/migration/scan", {})+ assert response.status == 200, response.json()+ [row] = response.json()["scan"]["items"]+ assert row["selected"] is True+ assert row["proposed_action"] == "import"+ applied = app.client.post("/api/models/migration/apply", {"item_ids": [row["id"]]})+ assert applied.status == 200, applied.json()+ assert applied.json()["applied"] == 1+ assert len(applied.json()["sources"]) == 1+ assert paths[0].read_bytes() == retained+ assert paths[0].stat().st_mode & 0o777 == 0o640+ assert app.env == before_env+ assert app.client.post("/api/models/migration/scan", {}).json()["scan"]["items"] == []++[email protected]("damage", ["missing", "corrupt"])+def test_f4_replace_migrated_bearer_without_old_secret_retains_cpa_transport(+ model_hub_app_factory, mock_llm_upstream, damage,+):+ _configure_protocol(mock_llm_upstream, "anthropic", models=[{"id": "mock-model"}])+ mock_llm_upstream.configure(required_api_key=KEY, required_auth_scheme="bearer")++ def seed(app):+ _seed_hub(app)+ _write(app.home / ".profile", (+ f"export ANTHROPIC_AUTH_TOKEN='{KEY}'\n"+ f"export ANTHROPIC_BASE_URL='{mock_llm_upstream.url}'\n"+ ))++ with model_hub_app_factory(extra_env=RUNTIME_ENV, before_start=seed) as app:+ [row] = app.client.post("/api/models/migration/scan", {}).json()["scan"]["items"]+ applied = app.client.post("/api/models/migration/apply", {"item_ids": [row["id"]]})+ assert applied.status == 200, applied.json()+ [source] = applied.json()["sources"]+ assert source["credential_ref"].startswith("cred_auth_bearer_")+ _assert_cpa_bearer_consumption(app, mock_llm_upstream, KEY)+ config_path = app.avibe_home / "config/config.json"+ agents_before = json.loads(config_path.read_text())["model_hub"]["agents"]+ old_path = (+ app.avibe_home / "runtime/model-hub/state/credentials"+ / f"{source['credential_ref']}.json"+ )+ assert old_path.is_file()+ if damage == "missing":+ old_path.unlink()+ else:+ old_path.write_bytes(b"{fixture-corrupt")+ replacement = "fixture-replacement-bearer-key"+ mock_llm_upstream.configure(required_api_key=replacement)+ response = app.client.put(+ f"/api/models/sources/{source['id']}/credential", {"key": replacement},+ )+ assert response.status == 200, response.json()+ updated = response.json()["source"]+ assert updated["id"] == source["id"]+ assert updated["credential_ref"] != source["credential_ref"]+ assert updated["credential_ref"].startswith("cred_auth_bearer_")+ assert json.loads(config_path.read_text())["model_hub"]["agents"] == agents_before+ assert not old_path.exists()+ pending = app.avibe_home / "state/model_hub_pending_revocations.json"+ assert json.loads(pending.read_text()) == []+ _assert_cpa_bearer_consumption(app, mock_llm_upstream, replacement)++[email protected]("history", ["marked", "legacy-unknown"])+def test_f4_oauth_history_fence_survives_later_api_batch_over_http(+ model_hub_app_factory, mock_llm_upstream, history,+):+ """Synthetic recorded custody is consumed by real Controller IPC apply."""+ _configure_protocol(mock_llm_upstream, "openai_responses", models=[{"id": "mock-model"}])+ mock_llm_upstream.configure(required_api_key=KEY)+ paths = []++ def seed(app):+ _seed_hub(app)+ _write(app.home / ".config/opencode/opencode.json", json.dumps({+ "provider": {"openai": {"options": {"apiKey": KEY, "baseURL": mock_llm_upstream.url}}},+ }))+ native = app.home / ".codex/auth.json"+ _write(native, json.dumps({+ "auth_mode": "chatgpt",+ "tokens": {+ "access_token": "fixture-restored-codex-access",+ "refresh_token": "fixture-restored-codex-refresh",+ "account_id": "fixture-codex-account",+ },+ }))+ _write(app.home / ".codex/config.toml", 'cli_auth_credentials_store = "file"\n')+ receipt = app.avibe_home / "state/native-takeover/last-completed.json"+ NativeTakeoverJournal(receipt).save({+ "version": 1, "phase": "complete", "backends": ["claude"],+ "items": [{"id": "fixture-old-confirmation", "backend": "claude", "kind": "oauth_native"}],+ "source_ids": [], "source_credentials": {}, "outcome": "success",+ **({"oauth_custody_backends": ["codex"]} if history == "marked" else {}),+ })+ paths.extend([native, receipt])++ with model_hub_app_factory(extra_env=RUNTIME_ENV, before_start=seed) as app:+ before = paths[0].read_bytes()+ scan = app.client.post("/api/models/migration/scan", {})+ assert scan.status == 200, scan.json()+ [api] = [item for item in scan.json()["scan"]["items"] if item["backend"] == "opencode"]+ applied = app.client.post("/api/models/migration/apply", {"item_ids": [api["id"]]})+ assert applied.status == 200, applied.json()+ assert "codex" in json.loads(paths[1].read_text())["oauth_custody_backends"]+ sources_before = app.client.get("/api/models/sources").json()["sources"]+ [oauth] = app.client.post("/api/models/migration/scan", {}).json()["scan"]["items"]+ assert oauth["backend"] == "codex" and oauth["proposed_action"] == "import"+ mock_llm_upstream.reset_requests()+ refused = app.client.post("/api/models/migration/apply", {"item_ids": [oauth["id"]]})+ assert refused.status == 409, refused.json()+ assert refused.json()["error"] == "migration_reauthorization_required"+ assert paths[0].read_bytes() == before+ assert app.client.get("/api/models/sources").json()["sources"] == sources_before+ assert mock_llm_upstream.requests() == []diff --git a/tests/e2e/test_model_hub_mock_upstream.py b/tests/e2e/test_model_hub_mock_upstream.pyindex bad1cc527..a7c555c8a 100644--- a/tests/e2e/test_model_hub_mock_upstream.py+++ b/tests/e2e/test_model_hub_mock_upstream.py@@ -770,6 +770,35 @@ def test_mock_upstream_required_key_uses_the_configured_protocol_header(assert _json_request(mock_llm_upstream.url, "/v1/models")[0] == 200[email protected]("surface", ["models", "probe", "buffered"])[email protected]("header", ["missing", "wrong", "x-api-key", "bearer"])+def test_mock_upstream_bearer_opt_in_preserves_authentication_shape(+ mock_llm_upstream, surface, header,+) -> None:+ """A custom Anthropic relay can require Bearer, not either auth header."""+ key = "fixture-static-bearer"+ _configure(+ mock_llm_upstream.url, protocol="anthropic",+ required_api_key=key, required_auth_scheme="bearer",+ )+ headers = {+ "missing": {},+ "wrong": {"Authorization": "Bearer fixture-wrong"},+ "x-api-key": {"X-Api-Key": key},+ "bearer": {"Authorization": f"Bearer {key}"},+ }[header]+ status, _, _ = _json_request(+ mock_llm_upstream.url,+ "/v1/models" if surface == "models" else "/v1/messages",+ method="GET" if surface == "models" else "POST",+ body=None if surface == "models" else (+ {} if surface == "probe" else PROTOCOL_CASES["anthropic"]["body"]+ ),+ headers=headers,+ )+ assert status == (401 if header != "bearer" else 400 if surface == "probe" else 200)++@pytest.mark.parametrize("protocol", PROTOCOL_CASES)@pytest.mark.parametrize(("behavior", "status"),@@ -1024,6 +1053,9 @@ def test_mock_upstream_control_validation_capture_envelope_and_reset({"required_api_key": 123},{"required_api_key": False},{"required_api_key": []},+ {"required_auth_scheme": "anything"},+ {"required_auth_scheme": None},+ {"required_auth_scheme": []},{"models": [""]},{"model_errors": []},{"model_errors": {"": "model_not_found"}},diff --git a/tests/scenario_harness/model_hub.py b/tests/scenario_harness/model_hub.pyindex 2d854e72c..38daeda91 100644--- a/tests/scenario_harness/model_hub.py+++ b/tests/scenario_harness/model_hub.py@@ -152,6 +152,9 @@ class ModelHubScenarioAdapter:async def gateway_token(self) -> str:return "scenario-engine-token"+ async def credential_auth_scheme(self, credential_ref: str) -> str | None:+ return None+async def provision_transient_credential(self,vendor: str,@@ -184,6 +187,9 @@ class ModelHubScenarioAdapter:async def revoke_credential(self, credential_ref: str) -> None:self.revoked.append(credential_ref)+ async def revoke_api_key_credential(self, credential_ref: str) -> None:+ await self.revoke_credential(credential_ref)+async def sync_sources(self, bindings) -> None:self.synced.append(tuple(bindings))diff --git a/tests/scenarios/INDEX.yaml b/tests/scenarios/INDEX.yamlindex 56958b3d2..7ad6001cc 100644--- a/tests/scenarios/INDEX.yaml+++ b/tests/scenarios/INDEX.yaml@@ -131,6 +131,7 @@ capabilities:- tests/scenarios/model_hub/test_model_hub_native_oauth_scenarios.py- tests/e2e/test_model_hub_guards.py- tests/e2e/test_model_hub_migration.py+ - tests/e2e/test_model_hub_migration_persisted.py- tests/e2e/test_model_hub_mock_upstream.py- tests/e2e/test_model_hub_routing.py- tests/e2e/test_model_hub_backend_settings.py@@ -147,6 +148,7 @@ capabilities:- tests/test_model_hub_admission.py- tests/test_model_hub_config.py- tests/test_model_hub_legacy_opencode_migration.py+ - tests/test_model_hub_persisted_migration.py- tests/test_member_management_parity.py- tests/test_model_hub_provenance.py- tests/test_model_hub_retention.pydiff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yamlindex d8dc80f68..6b60bc599 100644--- a/tests/scenarios/model_hub/catalog.yaml+++ b/tests/scenarios/model_hub/catalog.yaml@@ -46,9 +46,11 @@ capability:- tests/scenarios/model_hub/test_model_hub_live_resolution_scenarios.py- tests/scenarios/model_hub/test_model_hub_turn_contract_scenarios.py- tests/scenarios/model_hub/test_model_hub_migration_scenarios.py+ - tests/test_model_hub_persisted_migration.py- tests/scenarios/model_hub/test_model_hub_native_oauth_scenarios.py- tests/e2e/test_model_hub_guards.py- tests/e2e/test_model_hub_migration.py+ - tests/e2e/test_model_hub_migration_persisted.py- tests/e2e/test_model_hub_mock_upstream.py- tests/e2e/test_model_hub_routing.py- tests/e2e/test_model_hub_backend_settings.py@@ -759,6 +761,17 @@ scenarios:related_tests:- ui/src/components/settings/models/migrationScan.test.ts::counts an OpenCode key, which never arrives as kind api_key- ui/src/components/settings/models/MigrationDialog.test.tsx::applies only complete backend groups with one migration action+ - id: MH-MIG-005+ name: Persisted migration ignores runtime authentication and preserves shared-file custody+ status: covered+ kind: migration+ layer: scenario+ test: tests/test_model_hub_persisted_migration.py::test_mh_mig_005_runtime_auth_cannot_disable_saved_opencode_keys+ related_tests:+ - tests/test_model_hub_persisted_migration.py::test_runtime_auth_alone_is_not_a_saved_configuration+ - tests/test_model_hub_persisted_migration.py::test_shell_saved_key_takeover_keeps_unrelated_bytes_and_runtime+ - tests/test_model_hub_persisted_migration.py::test_new_shell_layer_during_provision_cannot_escape_consent+ - tests/test_model_hub_persisted_migration.py::test_shared_shell_key_requires_both_backend_consents- id: MH-TAKEOVER-001name: Recoverable failures walk the configured route and exhaust only at its endstatus: covered@@ -1110,6 +1123,18 @@ scenarios:reason: first-visit banner rendering is not executable from the pytest API layerowning_layer: Playwright lane (ui/e2e)evidence: docs/plans/model-hub-e2e-test-plan.md#f-migration-c61c67+ - id: F4+ name: Saved CLI and shell credentials migrate while runtime authentication stays untouched+ status: assert+ kind: migration+ layer: e2e-auto+ test: tests/e2e/test_model_hub_migration_persisted.py::test_f4_persisted_configuration_migrates_with_runtime_auth_present+ related_tests:+ - tests/e2e/test_model_hub_migration_persisted.py::test_f4_unproven_shell_key_is_not_removed+ - tests/e2e/test_model_hub_migration_persisted.py::test_f4_custom_api_key_transport_is_refused_without_cleanup+ - tests/e2e/test_model_hub_migration_persisted.py::test_f4_explicit_dynamic_writer_refuses_http_apply_without_proof+ - tests/e2e/test_model_hub_migration_persisted.py::test_f4_replace_migrated_bearer_without_old_secret_retains_cpa_transport+ - tests/e2e/test_model_hub_migration_persisted.py::test_f4_oauth_history_fence_survives_later_api_batch_over_http- id: G1name: A guard echo missing agents remains confirmablestatus: assertdiff --git a/tests/scenarios/model_hub/observations.yaml b/tests/scenarios/model_hub/observations.yamlindex 15665fb01..3f59a457a 100644--- a/tests/scenarios/model_hub/observations.yaml+++ b/tests/scenarios/model_hub/observations.yaml@@ -1,6 +1,12 @@version: 2capability: model_hubobservations:+ - id: MH-OBS-010+ source: local-regression-persisted-migration-2026-09-20+ summary: Already-Hub backends were all disabled because inherited process authentication was represented as migration blockers. Saved Claude AUTH_TOKEN was mislabeled as permission failure. Migration must inventory persisted files independently of runtime injection, and preserve Bearer transport for supported custom Anthropic endpoints rather than treating it as x-api-key or OAuth.+ affects:+ - MH-MIG-005+ - F4- id: MH-OBS-009source: codex-0.154.0-hermetic-native-wire-2026-09-19summary: Route-bound bearer tokens caused model changes to replace shared Codex processes. Per-turn responsesapiClientMetadata survives concurrent threads, tool continuations, retries and local automatic compaction in both the native metadata header and body. The header provides identity before body parsing; a legacy remote compact wire-only probe carries metadata only in its header.diff --git a/tests/scenarios/model_hub/test_model_hub_migration_scenarios.py b/tests/scenarios/model_hub/test_model_hub_migration_scenarios.pyindex 7bc03b8cf..cc1363cc6 100644--- a/tests/scenarios/model_hub/test_model_hub_migration_scenarios.py+++ b/tests/scenarios/model_hub/test_model_hub_migration_scenarios.py@@ -84,6 +84,7 @@ class MigrationAdapter:self.activated: list[str] = []self.validated: list[str] = []self.keys: dict[str, tuple[str, str, str, str | None]] = {}+ self.auth_schemes: dict[str, str | None] = {}async def start(self):return EngineStatus(EngineHealth.OK, "fixture", True, "127.0.0.1", 32199, None)@@ -98,20 +99,29 @@ class MigrationAdapter:assert credential_ref in self.activatedself.validated.append(credential_ref)- async def matches_api_key_credential(self, credential_ref, vendor, protocol, secret, base_url):- return self.keys.get(credential_ref) == (vendor, protocol, secret, base_url)+ async def matches_api_key_credential(+ self, credential_ref, vendor, protocol, secret, base_url, *, auth_scheme=None,+ ):+ return (+ self.keys.get(credential_ref) == (vendor, protocol, secret, base_url)+ and self.auth_schemes.get(credential_ref) == auth_scheme+ )++ async def credential_auth_scheme(self, credential_ref):+ return self.auth_schemes.get(credential_ref)async def provision_transient_credential(self,vendor: str,secret: str,base_url: str | None,- *, on_reserved=None,+ *, on_reserved=None, auth_scheme=None,) -> str:credential_ref = f"cred_observation_{len(self.transient_refs) + 1}"if on_reserved:on_reserved(credential_ref)self.transient_refs.append(credential_ref)+ self.auth_schemes[credential_ref] = auth_schemereturn credential_refasync def observe_source(@@ -146,13 +156,14 @@ class MigrationAdapter:protocol: str,secret: str,base_url: str | None,- *, on_reserved=None,+ *, on_reserved=None, auth_scheme=None,) -> str:credential_ref = f"cred_migration_{len(self.provisioned) + 1}"if on_reserved:on_reserved(credential_ref)self.provisioned.append((vendor, len(secret), credential_ref))self.keys[credential_ref] = (vendor, protocol, secret, base_url)+ self.auth_schemes[credential_ref] = auth_schemereturn credential_refasync def provision_oauth_credential(@@ -278,7 +289,7 @@ def _write_claude(home: Path, *, malformed: bool = False) -> None:{"env": {"ANTHROPIC_API_KEY": "sk-ant-test-123456789",- "ANTHROPIC_BASE_URL": "https://anthropic.example/v1",+ "ANTHROPIC_BASE_URL": "https://api.anthropic.com/v1",},"permissions": {"allow": ["Read"]},},@@ -907,9 +918,17 @@ def test_opencode_auth_only_custom_provider_without_base_url_is_visible_blocker(assert item.selected is False-def test_claude_auth_token_requires_reauth_without_changing_header_semantics([email protected]("base_url,token,allowed", [+ ("https://bearer-relay.example/v1", "bearer-test-123456", True),+ ("https://api.anthropic.com", "bearer-test-123456", False),+ ("https://bearer-relay.example/v1", "sk-ant-oat-fixture", False),+])+def test_claude_auth_token_preserves_supported_static_header_semantics(monkeypatch: pytest.MonkeyPatch,tmp_path: Path,+ base_url: str,+ token: str,+ allowed: bool,) -> None:native_home = tmp_path / "native-home"_write(@@ -917,8 +936,8 @@ def test_claude_auth_token_requires_reauth_without_changing_header_semantics(json.dumps({"env": {- "ANTHROPIC_AUTH_TOKEN": "bearer-test-123456",- "ANTHROPIC_BASE_URL": "https://bearer-relay.example/v1",+ "ANTHROPIC_AUTH_TOKEN": token,+ "ANTHROPIC_BASE_URL": base_url,}}),@@ -927,10 +946,20 @@ def test_claude_auth_token_requires_reauth_without_changing_header_semantics(service, store, adapter = _service(tmp_path)[item] = service.migration_scan()["items"]+ assert item["selected"] is allowed+ assert token not in json.dumps(item)+ if allowed:+ assert item["proposed_action"] == "import"+ result = asyncio.run(service.migration_apply([item["id"]]))+ assert result["applied"] == 1+ [source] = store.config.sources+ assert adapter.auth_schemes[source.credential_ref] == "bearer"+ assert adapter.keys[source.credential_ref][2:] == (token, base_url)+ assert service.migration_scan()["items"] == []+ return+assert item["proposed_action"] == "reauth"- assert item["selected"] is False- assert item["notes_key"] == "settings.models.migration.blocked.credential"- assert "bearer-test-123456" not in json.dumps(item)+ assert item["notes_key"] == "settings.models.migration.blocked.token"with pytest.raises(ModelHubError) as error:asyncio.run(service.migration_apply([item["id"]]))diff --git a/tests/test_atomic_io.py b/tests/test_atomic_io.pyindex d60461a68..978edb501 100644--- a/tests/test_atomic_io.py+++ b/tests/test_atomic_io.py@@ -164,3 +164,114 @@ def test_write_atomic_does_not_require_optional_os_attributes(write_atomic(target, "portable")assert target.read_text(encoding="utf-8") == "portable"++[email protected]("mode", [0o400, 0o600, 0o640, 0o644, 0o755])+def test_explicit_mode_is_durable_before_guard_and_publication(tmp_path, monkeypatch, mode):+ target = tmp_path / "profile"+ target.write_bytes(b"before")+ target.chmod(0o600)+ flushed = []+ fsync = os.fsync++ def record_sync(descriptor):+ info = os.fstat(descriptor)+ flushed.append((info.st_ino, info.st_mode & 0o777))+ fsync(descriptor)++ def guard():+ assert target.read_bytes() == b"before"+ [temporary] = tmp_path.glob(".profile.*")+ assert temporary.read_bytes() == b"after"+ info = temporary.stat()+ assert info.st_mode & 0o777 == mode+ assert (info.st_ino, mode) in flushed++ monkeypatch.setattr(atomic_io.os, "fsync", record_sync)+ write_atomic(target, b"after", mode=mode, before_replace=guard)+ assert target.read_bytes() == b"after"+ assert target.stat().st_mode & 0o777 == mode+ assert list(tmp_path.glob(".profile.*")) == []+++def test_failed_before_replace_guard_preserves_external_state_and_cleans_temporary(tmp_path):+ target = tmp_path / "profile"+ target.write_bytes(b"before")++ def guard():+ target.write_bytes(b"external state")+ target.chmod(0o600)+ raise RuntimeError("fixture consent changed")++ with pytest.raises(RuntimeError, match="fixture consent changed"):+ write_atomic(target, b"after", mode=0o644, before_replace=guard)+ assert target.read_bytes() == b"external state"+ assert target.stat().st_mode & 0o777 == 0o600+ assert list(tmp_path.glob(".profile.*")) == []++[email protected]("mode", [-1, 0o1000, True, "0644", None])+def test_invalid_explicit_mode_cannot_create_or_replace_state(tmp_path, mode):+ target = tmp_path / "uncreated" / "state"+ with pytest.raises(ValueError):+ write_atomic(target, b"no write", mode=mode)+ assert list(tmp_path.iterdir()) == []++[email protected]("failure", ["fchmod", "fsync", "replace"])+def test_explicit_mode_failure_cleans_temporary_without_changing_target(tmp_path, monkeypatch, failure):+ target = tmp_path / "profile"+ target.write_bytes(b"before")+ target.chmod(0o600)++ def fail(*args, **kwargs):+ raise OSError("fixture publication failed")++ monkeypatch.setattr(atomic_io.os, failure, fail)+ with pytest.raises(OSError):+ write_atomic(target, b"after", mode=0o644)+ assert target.read_bytes() == b"before"+ assert target.stat().st_mode & 0o777 == 0o600+ assert list(tmp_path.glob(".profile.*")) == []+++def test_explicit_mode_fallback_changes_only_the_unpublished_inode(tmp_path, monkeypatch):+ target = tmp_path / "profile"+ calls = []+ chmod = os.chmod+ monkeypatch.delattr(atomic_io.os, "fchmod")++ def record_chmod(path, mode):+ assert path != target+ calls.append(mode)+ chmod(path, mode)++ monkeypatch.setattr(atomic_io.os, "chmod", record_chmod)+ write_atomic(target, b"after", mode=0o640)+ assert calls == [0o640]+ assert target.stat().st_mode & 0o777 == 0o640++[email protected]("mask", [0o200, 0o400, 0o777])[email protected]("mode", [0o600, 0o640])+def test_captured_publication_mode_is_not_narrowed_by_umask(tmp_path, mask, mode):+ target = tmp_path / "profile"+ target.write_bytes(b"before")+ previous_mask = os.umask(mask)+ try:+ write_atomic(target, b"after", mode=mode)+ finally:+ os.umask(previous_mask)+ assert target.read_bytes() == b"after"+ assert target.stat().st_mode & 0o777 == mode+++def test_default_publication_mode_is_exact_under_restrictive_umask(tmp_path):+ target = tmp_path / "state"+ previous_mask = os.umask(0o777)+ try:+ write_atomic(target, b"state")+ finally:+ os.umask(previous_mask)+ assert target.read_bytes() == b"state"+ assert target.stat().st_mode & 0o777 == 0o600diff --git a/tests/test_model_hub_api.py b/tests/test_model_hub_api.pyindex a77c017a3..fc6c32ac9 100644--- a/tests/test_model_hub_api.py+++ b/tests/test_model_hub_api.py@@ -272,6 +272,9 @@ class FakeAdapter:async def credential_supports_refresh(self, credential_ref):return credential_ref in self.refreshable_credential_refs+ async def credential_auth_scheme(self, credential_ref):+ return None+async def provision_transient_credential(self, vendor, secret, base_url):self.secret_lengths.append(len(secret))self.credential_count += 1@@ -280,6 +283,9 @@ class FakeAdapter:async def revoke_credential(self, credential_ref):self.revoked.append(credential_ref)+ async def revoke_api_key_credential(self, credential_ref):+ await self.revoke_credential(credential_ref)+async def cleanup_orphaned_oauth_material(self, credential_ref):self.orphan_cleanup_calls.append(credential_ref)return self.orphan_cleanup_succeedsdiff --git a/tests/test_model_hub_migration_auth_mirror.py b/tests/test_model_hub_migration_auth_mirror.pyindex e84670a00..14974093d 100644--- a/tests/test_model_hub_migration_auth_mirror.py+++ b/tests/test_model_hub_migration_auth_mirror.py@@ -370,6 +370,8 @@ def _migration_fixture(monkeypatch, tmp_path):path = tmp_path / "fixture-config.json"monkeypatch.setattr(paths, "get_config_path", lambda: path)config = _config()+ # Use a supported API-key target; pure mapper cases still cover relays.+ config.agents.claude.base_url = "https://api.anthropic.com/fixture-path"config.save(config_path=path)service, _, adapter = _service(tmp_path, migration_home=home)service.store = V2ModelHubConfigStore()diff --git a/tests/test_model_hub_migration_auth_recovery.py b/tests/test_model_hub_migration_auth_recovery.pyindex 6713c99e9..445508e13 100644--- a/tests/test_model_hub_migration_auth_recovery.py+++ b/tests/test_model_hub_migration_auth_recovery.py@@ -41,7 +41,7 @@ def _persistent_takeover(monkeypatch, tmp_path, *, oauth=False):else:config.agents.claude.auth_mode = "api_key"config.agents.claude.api_key = "fixture-old-key"- config.agents.claude.base_url = "https://fixture.example"+ config.agents.claude.base_url = "https://api.anthropic.com/fixture-path"config.agents.claude.auth_mode_set = Trueconfig.save(config_path=config_path)service, _, adapter = _service(tmp_path, migration_home=home)diff --git a/tests/test_model_hub_migration_bearer.py b/tests/test_model_hub_migration_bearer.pynew file mode 100644index 000000000..a222fcf1f--- /dev/null+++ b/tests/test_model_hub_migration_bearer.py@@ -0,0 +1,363 @@+"""Static migration transport remains private, immutable and credential-gated."""++import asyncio+import json+from contextlib import asynccontextmanager+from dataclasses import replace+from types import SimpleNamespace+from unittest.mock import AsyncMock++import pytest+import yaml+from aiohttp import web++from config.v2_config import ModelHubSourceConfig+from core.handlers.model_hub.adapter import SourceBinding+from core.handlers.model_hub.service import ModelHubError+from tests.test_model_hub_api import _service+from vibe.model_hub_runtime.adapter import CLIProxyEngineAdapter+from vibe.model_hub_runtime.api_key_vendors import validate_api_key_auth_scheme+from vibe.model_hub_runtime.client import probe_models+from vibe.model_hub_runtime.config import write_engine_config+from vibe.model_hub_runtime.state import EngineStateError, EngineStateStore, RuntimeSecrets+++KEY = "fixture-bearer-value"+CUSTOM = "https://relay.example"+++def _adapter(tmp_path):+ store = EngineStateStore(tmp_path / "engine")+ supervisor = SimpleNamespace(+ state_store=store, client_if_running=lambda: None, restart_if_running=lambda: None,+ invalidate_configs=store.clear_runtime_configs,+ )+ return CLIProxyEngineAdapter(supervisor=supervisor, state_store=store)+++def _binding(ref, url=CUSTOM):+ return SourceBinding(+ source_id="src_bearerfixture", vendor="anthropic", protocol="anthropic",+ base_url=url, credential_ref=ref, allowed_origins=(),+ model_ids=("claude-sonnet-4-6",),+ )+++def _service_with_custody(tmp_path):+ runtime = _adapter(tmp_path)+ service, config, adapter = _service(tmp_path)+ for name in (+ "provision_credential", "provision_transient_credential", "credential_auth_scheme",+ "retarget_api_key_credential", "matches_api_key_credential",+ "observe_source", "discover_models", "revoke_credential", "revoke_api_key_credential", "sync_sources",+ ):+ setattr(adapter, name, getattr(runtime, name))+ return service, config, runtime+++@asynccontextmanager+async def _relay(*, public_models=False, protocol_success=False, scheme="bearer"):+ requests = []+ accepted = {KEY}++ def supplied(request):+ requests.append((request.method, request.path, dict(request.headers)))+ assert request.headers.get("anthropic-version") == "2023-06-01"+ if scheme == "bearer":+ assert "x-api-key" not in request.headers+ return request.headers.get("Authorization", "").removeprefix("Bearer ")+ assert "Authorization" not in request.headers+ return request.headers.get("x-api-key", "")++ async def messages(request):+ key = supplied(request)+ body = await request.json()+ assert "model" not in body+ if key not in accepted:+ return web.json_response(+ {"type": "error", "error": {"type": "authentication_error", "message": "invalid token"}},+ status=401,+ )+ if protocol_success:+ return web.json_response({"type": "message", "content": []})+ return web.json_response(+ {"type": "error", "error": {"type": "invalid_request_error", "message": "model: Field required"}},+ status=400,+ )++ async def models(request):+ key = supplied(request)+ if not public_models and key not in accepted:+ return web.json_response({"error": "authentication required"}, status=401)+ return web.json_response({"data": [{"id": "claude-sonnet-4-6"}]})++ app = web.Application()+ app.router.add_post("/v1/messages", messages)+ app.router.add_get("/v1/models", models)+ runner = web.AppRunner(app)+ await runner.setup()+ try:+ site = web.TCPSite(runner, "127.0.0.1", 0)+ await site.start()+ origin = f"http://127.0.0.1:{site._server.sockets[0].getsockname()[1]}"+ yield origin, requests, accepted+ finally:+ await runner.cleanup()++[email protected]("changes", [+ {"auth_scheme": "auto"}, {"auth_scheme": "x-api-key"}, {"auth_scheme": ""},+ {"auth_scheme": []}, {"vendor": "custom"}, {"vendor": "openai"},+ {"protocol": "openai_chat"}, {"protocol": "openai_responses"},+ {"base_url": None}, {"base_url": "https://api.anthropic.com"},+ {"base_url": "https://API.ANTHROPIC.COM:443/v1"},+ {"base_url": "https://api.anthropic.com"},+ {"base_url": "https://[email protected]"}, {"base_url": "https://relay.example:wrong"},+ {"base_url": "https://relay.example:65536"}, {"base_url": "https://relay.example:0"},+ {"base_url": "https://%61pi.anthropic.com"}, {"base_url": "https://relay.example\\evil"},+ {"base_url": "https://relay.example\n"}, {"base_url": "file:///fixture"},+ {"secret": "before-sk-ant-oat-after"}, {"secret": ""},+ {"secret": "fixture\r\nAuthorization: extra"},+])+def test_explicit_scheme_refuses_unsupported_transport_without_secret_errors(changes):+ args = dict(vendor="anthropic", protocol="anthropic", base_url=CUSTOM, secret=KEY, auth_scheme="bearer")+ args.update(changes)+ with pytest.raises(ValueError, match="^unsupported API key authentication scheme$"):+ validate_api_key_auth_scheme(**args)++[email protected]("url", [+ CUSTOM, "https://relay.example/api.anthropic.com",+ "https://api.anthropic.com.relay.example", "http://127.0.0.1:54321",+ "http://[::1]:54321", "https://relay.example:8443",+ "https://api.anthropic.com:8443", "http://api.anthropic.com",+ "https://api.anthropic.com.",+])+def test_custom_authority_is_parsed_and_transient_protocol_can_be_unknown(url):+ assert validate_api_key_auth_scheme("anthropic", None, url, KEY, "bearer") == "bearer"+ assert validate_api_key_auth_scheme("anthropic", "anthropic", url, KEY, "bearer") == "bearer"+++def test_invalid_scheme_is_refused_before_reservation_or_write(tmp_path):+ store = EngineStateStore(tmp_path / "absent")+ reserved = []+ with pytest.raises(EngineStateError, match="unsupported API key authentication scheme"):+ store.store_api_key(+ KEY, vendor="anthropic", protocol="anthropic", base_url=CUSTOM,+ auth_scheme="other", on_reserved=reserved.append,+ )+ assert not reserved+ assert not store.root.exists()+++def test_metadata_roundtrip_reuse_and_legacy_absence(tmp_path):+ async def scenario():+ adapter = _adapter(tmp_path)+ explicit = await adapter.provision_credential(+ "anthropic", "anthropic", KEY, CUSTOM, auth_scheme="bearer",+ )+ legacy = await adapter.provision_credential("anthropic", "anthropic", KEY, CUSTOM)+ assert "auth_scheme" not in adapter.state_store.credential_metadata(legacy)+ assert await adapter.credential_auth_scheme(legacy) is None+ assert await adapter.credential_auth_scheme(explicit) == "bearer"+ assert not await adapter.credential_supports_refresh(explicit)+ for ref, scheme, matches in (+ (explicit, "bearer", True), (explicit, None, False),+ (legacy, "bearer", False), (legacy, None, True),+ ):+ assert await adapter.matches_api_key_credential(+ ref, "anthropic", "anthropic", KEY, CUSTOM, auth_scheme=scheme,+ ) is matches+ reopened = EngineStateStore(adapter.state_store.root)+ assert reopened.credential_metadata(explicit)["auth_scheme"] == "bearer"+ assert reopened.read_api_key(legacy) == KEY+ bindings = [_binding(explicit)]+ reopened.sync_sources(bindings)+ assert "auth_scheme" not in json.loads((reopened.root / "sources.json").read_text())["sources"][0]++ asyncio.run(scenario())++[email protected]("change", [+ {"auth_scheme": "unknown"}, {"vendor": "openai"}, {"protocol": "openai_chat"},+ {"base_url": "https://api.anthropic.com"}, {"value": "sk-ant-oat-fixture"},+ {"value": None}, {"protocol": None},+])+def test_malformed_explicit_metadata_cannot_supply_transport_or_readiness(tmp_path, change):+ store = EngineStateStore(tmp_path / "engine")+ ref = store.store_api_key(KEY, vendor="anthropic", protocol="anthropic", base_url=CUSTOM, auth_scheme="bearer")+ path = store.root / "credentials" / f"{ref}.json"+ original = json.loads(path.read_text())+ path.write_text(json.dumps({**original, **change}))+ with pytest.raises(EngineStateError, match="unsupported API key authentication scheme"):+ store.credential_metadata(ref)+ with pytest.raises(EngineStateError):+ store.sync_sources([_binding(ref)])+ assert not store.has_current_source_credential(+ ref, source_id="src_bearerfixture", kind="api_key",+ vendor="anthropic", protocol="anthropic", base_url=CUSTOM,+ )+++def test_retarget_keeps_scheme_and_refuses_official_before_new_custody(tmp_path):+ async def scenario():+ adapter = _adapter(tmp_path)+ ref = await adapter.provision_credential("anthropic", "anthropic", KEY, CUSTOM, auth_scheme="bearer")+ before = adapter.state_store.credential_metadata(ref)+ replacement = await adapter.retarget_api_key_credential(+ ref, "anthropic", "anthropic", "https://another-relay.example",+ )+ assert replacement != ref+ assert await adapter.credential_auth_scheme(replacement) == "bearer"+ assert adapter.state_store.credential_metadata(ref) == before+ paths = set((adapter.state_store.root / "credentials").iterdir())+ with pytest.raises(EngineStateError):+ await adapter.retarget_api_key_credential(ref, "anthropic", "anthropic", "https://api.anthropic.com")+ assert set((adapter.state_store.root / "credentials").iterdir()) == paths++ asyncio.run(scenario())++[email protected]("protocol_success,public_models,key,outcome", [+ (False, False, KEY, "observed"),+ (False, False, "fixture-wrong-key", "authentication_failed"),+ (False, True, KEY, "adapter_error"),+ (True, False, KEY, "observed"),+])+def test_real_http_observation_discovery_and_credentialless_contrast(+ tmp_path, protocol_success, public_models, key, outcome,+):+ async def scenario():+ adapter = _adapter(tmp_path)+ async with _relay(protocol_success=protocol_success, public_models=public_models) as (origin, requests, _):+ ref = await adapter.provision_transient_credential("anthropic", key, origin, auth_scheme="bearer")+ with pytest.raises(ValueError, match="unsupported API key authentication scheme"):+ await adapter.observe_source("anthropic", origin, ref, ("openai_chat", "anthropic"))+ assert requests == []+ result = await adapter.observe_source("anthropic", origin, ref, ("anthropic",))+ assert result.outcome.value == outcome+ assert (result.authenticated is True) == (outcome == "observed")+ if outcome == "observed":+ models = await adapter.discover_models("anthropic", "anthropic", origin, ref)+ assert [model.id for model in models] == ["claude-sonnet-4-6"]+ assert requests[0][:2] == ("POST", "/v1/messages")+ assert requests[0][2]["Authorization"] == f"Bearer {key}"+ if not protocol_success and key == KEY:+ assert requests[1][2]["Authorization"] == f"Bearer {KEY}"+ assert "Authorization" not in requests[2][2]+ for _, _, headers in requests:+ assert "x-api-key" not in {name.lower() for name in headers}++ asyncio.run(scenario())++[email protected]("public_models,key", [(False, KEY), (True, KEY), (False, "fixture-wrong-key")])+def test_private_service_observation_provisions_and_cleans_same_scheme(tmp_path, public_models, key):+ async def scenario():+ service, _, runtime = _service_with_custody(tmp_path)+ reserved = []+ async with _relay(public_models=public_models) as (origin, requests, _):+ pending = service._require_proven_source_payload(+ {"vendor": "anthropic", "base_url": origin, "key": key},+ auth_scheme="bearer", on_reserved=reserved.append,+ )+ if not public_models and key == KEY:+ assert (await pending).authenticated is True+ else:+ with pytest.raises(ModelHubError):+ await pending+ assert len(reserved) == 1+ assert runtime.state_store.credential_metadata_if_present(reserved[0]) is None+ assert len(requests) == (3 if key == KEY else 1)++ asyncio.run(scenario())+++def test_public_payloads_cannot_select_private_scheme(tmp_path):+ async def scenario():+ service, _, adapter = _service(tmp_path)+ adapter.provision_transient_credential = AsyncMock(side_effect=AssertionError("unexpected custody"))+ payload = {"vendor": "anthropic", "base_url": CUSTOM, "key": KEY, "auth_scheme": "bearer"}+ with pytest.raises(ModelHubError):+ await service._observe_source_payload(payload)+ with pytest.raises(ModelHubError):+ await service.create_source({"kind": "api_key", **payload})+ adapter.provision_transient_credential.assert_not_awaited()++ asyncio.run(scenario())+++def test_legacy_callers_keep_original_header_and_optional_keyword_absence(tmp_path):+ async def scenario():+ async with _relay(scheme="legacy") as (origin, requests, _):+ result = await probe_models(vendor="anthropic", protocol="anthropic", base_url=origin, secret=KEY)+ assert result[0].id == "claude-sonnet-4-6"+ assert requests[0][2]["x-api-key"] == KEY+ service, _, adapter = _service(tmp_path)+ # The existing fake deliberately has no auth_scheme keyword.+ result = await service._require_proven_source_payload({"vendor": "anthropic", "key": KEY})+ assert result.authenticated is True+ assert adapter.revoked++ asyncio.run(scenario())++[email protected]("scheme", [None, "bearer"])+def test_service_replace_preserves_private_scheme_through_actual_discovery(tmp_path, scheme):+ async def scenario():+ service, config, runtime = _service_with_custody(tmp_path)+ async with _relay(scheme=scheme or "legacy") as (origin, requests, accepted):+ ref = await runtime.provision_credential("anthropic", "anthropic", KEY, origin, auth_scheme=scheme)+ source = ModelHubSourceConfig.from_payload({+ "id": "src_bearerfixture", "kind": "api_key", "vendor": "anthropic",+ "display_name": "Fixture", "protocol": "anthropic", "base_url": origin,+ "supply_channel": "hub", "billing": "metered",+ "state": {"status": "standby"}, "models": [], "credential_ref": ref,+ })+ config.config.sources.append(source)+ runtime.state_store.sync_sources([_binding(ref, origin)])+ if scheme:+ old_config = config.config.to_payload()+ old_metadata = runtime.state_store.credential_metadata(ref)+ with pytest.raises(ModelHubError):+ await service.replace_credential(source.id, {"key": "sk-ant-oat-fixture"})+ assert config.config.to_payload() == old_config+ assert runtime.state_store.credential_metadata(ref) == old_metadata+ assert requests == []+ accepted.add("fixture-replacement")+ answer = await service.replace_credential(source.id, {"key": "fixture-replacement"})+ replacement = answer["source"]["credential_ref"]+ assert replacement != ref+ assert await runtime.credential_auth_scheme(replacement) == scheme+ assert runtime.state_store.credential_metadata_if_present(ref) is None+ assert "auth_scheme" not in answer["source"]+ assert requests[0][2].get("Authorization" if scheme else "x-api-key") == (+ "Bearer fixture-replacement" if scheme else "fixture-replacement"+ )++ asyncio.run(scenario())+++def test_engine_config_keeps_supported_cpa_transport_and_refuses_reinterpretation(tmp_path):+ store = EngineStateStore(tmp_path / "engine")+ ref = store.store_api_key(KEY, vendor="anthropic", protocol="anthropic", base_url=CUSTOM, auth_scheme="bearer")+ source = store.sync_sources([_binding(ref)])[0]+ path = tmp_path / "instance" / "config.yaml"++ def write(record):+ write_engine_config(+ path, host="127.0.0.1", port=12345, auth_dir=store.auth_dir,+ runtime_secrets=RuntimeSecrets("fixture-management", "fixture-gateway"),+ sources=[record], state_store=store,+ )++ write(source)+ entry = yaml.safe_load(path.read_text())["claude-api-key"][0]+ assert entry["api-key"] == KEY+ assert entry["base-url"] == CUSTOM+ assert "headers" not in entry+ before = path.read_bytes()+ with pytest.raises(EngineStateError):+ write(replace(source, base_url="https://api.anthropic.com"))+ assert path.read_bytes() == before+ assert path.stat().st_mode & 0o777 == 0o600diff --git a/tests/test_model_hub_migration_custody_history.py b/tests/test_model_hub_migration_custody_history.pynew file mode 100644index 000000000..dd946dd22--- /dev/null+++ b/tests/test_model_hub_migration_custody_history.py@@ -0,0 +1,482 @@+"""Completed custody history uses synthetic homes and guarded native stores."""++from __future__ import annotations++import asyncio+import hashlib+import json+from pathlib import Path++import psutil+import pytest++from core.handlers.model_hub.adapter import OAuthCredentialRejectedError+from core.handlers.model_hub.migration import MigrationConflictError, prepare_takeover_reauthentication+from core.handlers.model_hub.migration_journal import NativeTakeoverJournal, TakeoverStateError+from core.handlers.model_hub.service import ModelHubError+from tests.scenarios.model_hub.test_model_hub_migration_scenarios import (+ _service,+ _write,+ _write_claude_oauth,+ _write_codex_oauth,+)+from tests.test_model_hub_persisted_inventory import home # noqa: F401 - hermetic fixture+from vibe import native_oauth_store++[email protected](autouse=True)+def _guard_native_boundaries(home, monkeypatch):+ # The shared home fixture also refuses Security store and subprocess calls.+ def forbidden(*args, **kwargs):+ raise AssertionError("real credential/process boundary reached")++ monkeypatch.setattr(psutil, "process_iter", forbidden)+ monkeypatch.setattr(native_oauth_store, "_security_bindings", forbidden)+ monkeypatch.setenv("USER", "fixture-user")+ monkeypatch.setenv("CLAUDE_SECURESTORAGE_CONFIG_DIR", "")+++def _oauth(home: Path, backend: str) -> Path:+ (_write_claude_oauth if backend == "claude" else _write_codex_oauth)(home)+ return home / (".claude/.credentials.json" if backend == "claude" else ".codex/auth.json")+++def _api(home: Path, backend: str, key: str = "fixture-second") -> Path:+ if backend == "opencode":+ path = home / ".config/opencode/opencode.json"+ _write(path, json.dumps({"provider": {"openai": {"options": {"apiKey": key}}}}))+ elif backend == "claude":+ path = home / ".claude/settings.json"+ _write(path, json.dumps({"env": {"ANTHROPIC_API_KEY": key}}))+ else:+ path = home / ".codex/config.toml"+ _write(path, f'[model_providers.fixture]\nexperimental_bearer_token="{key}"\n')+ return path+++def _empty(home: Path, backend: str) -> tuple[str, str]:+ if backend == "claude":+ locator = ("Claude Code-credentials", "fixture-user")+ else:+ _write(home / ".codex/config.toml", 'cli_auth_credentials_store="keyring"\n')+ account = "cli|" + hashlib.sha256(str((home / ".codex").resolve()).encode()).hexdigest()[:16]+ locator = ("Codex Auth", account)+ native_oauth_store._KEYCHAIN_STORE.items[locator] = (+ '{"mcpOAuth":{"keep":"保留"}}', "fixture-empty",+ )+ return locator+++def _ids(service, backend: str | None = None) -> list[str]:+ return [+ row["id"] for row in service.migration_scan()["items"]+ if backend is None or row["backend"] == backend+ ]+++def _apply(service, backend: str | None = None) -> dict:+ ids = _ids(service, backend)+ assert ids+ return asyncio.run(service.migration_apply(ids))+++def _legacy(service) -> None:+ receipt = service.migration_journal.completed()+ receipt.pop("oauth_custody_backends")+ for row, identity in zip(receipt["items"], receipt.pop("inventory_ids"), strict=True):+ row["id"] = identity+ NativeTakeoverJournal(service.migration_journal.path.with_name("last-completed.json")).save(receipt)+++def _counts(adapter) -> tuple[int, int, int]:+ return len(adapter.provisioned), len(adapter.oauth_provisioned), len(adapter.observed)+++def _assert_refusal(service, store, adapter, paths: tuple[Path, ...], *, code=None) -> None:+ before = [path.read_bytes() for path in paths], store.config.to_payload()+ counts = _counts(adapter)+ with pytest.raises(ModelHubError) as failure:+ _apply(service)+ if code is not None:+ assert failure.value.code == code+ assert before == ([path.read_bytes() for path in paths], store.config.to_payload())+ assert _counts(adapter) == counts+ assert service.migration_journal.load() is None++[email protected]("backend", ["claude", "codex"])[email protected]("middle", ["other_backend", "same_backend", "subset", "empty"])[email protected]("legacy", [False, True])+def test_completed_custody_survives_later_batches(home, tmp_path, backend, middle, legacy):+ native = _oauth(home, backend)+ original = native.read_bytes()+ if middle == "subset":+ other = _api(home, "opencode")+ other_original = other.read_bytes()+ service, store, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ if legacy:+ _legacy(service)+ if middle == "subset":+ other.write_bytes(other_original)+ elif middle == "empty":+ _empty(home, "codex" if backend == "claude" else "claude")+ else:+ _api(home, backend if middle == "same_backend" else "opencode")+ _apply(service)+ receipt = service.migration_journal.completed()+ assert not any(row["backend"] == backend and row["kind"] == "oauth_native" for row in receipt["items"])+ assert backend in receipt["oauth_custody_backends"]+ # A new controller must consume durable evidence, not an in-memory flag.+ restarted, _, _ = _service(tmp_path, migration_home=home)+ restarted.store, restarted.adapter = store, adapter+ native.write_bytes(original)+ _assert_refusal(+ restarted, store, adapter, (native,), code="migration_reauthorization_required",+ )+ assert len(adapter.oauth_provisioned) == 1++[email protected]("backend", ["claude", "codex"])[email protected]("middle", ["api", "empty"])[email protected]("source_change", ["none", "ref", "deleted"])+def test_legacy_receipt_cannot_reconstruct_already_overwritten_oauth(+ home, tmp_path, backend, middle, source_change,+):+ native = _oauth(home, backend)+ original = native.read_bytes()+ service, store, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ source_id = store.config.sources[0].id+ if middle == "api":+ _api(home, "opencode")+ else:+ _empty(home, "codex" if backend == "claude" else "claude")+ _apply(service)+ # This is the on-disk shape left when an older release already overwrote+ # the original receipt before upgrade. No item-to-Source guess can fix it.+ _legacy(service)+ if middle == "empty":+ assert service.migration_journal.completed()["source_ids"] == []+ if source_change == "ref":+ store.config.sources[0].credential_ref = "cred_fixture_reauthenticated"+ elif source_change == "deleted":+ store.config.sources = [source for source in store.config.sources if source.id != source_id]+ for agent in store.config.agents.values():+ agent.sources.order = [value for value in agent.sources.order if value != source_id]+ native.write_bytes(original)+ _assert_refusal(service, store, adapter, (native,), code="migration_reauthorization_required")+ assert len(adapter.oauth_provisioned) == 1++[email protected]("backend", ["claude", "codex"])[email protected]("legacy", [False, True])[email protected]("changed_sibling", [False, True])+def test_latest_exact_bundle_keeps_cleanup_only_and_whole_ref_guard(+ home, tmp_path, backend, legacy, changed_sibling,+):+ native = _oauth(home, backend)+ original = native.read_bytes()+ _api(home, "opencode")+ service, store, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ if legacy:+ _legacy(service)+ native.write_bytes(original)+ if changed_sibling:+ sibling = next(source for source in store.config.sources if source.kind == "api_key")+ sibling.credential_ref = "cred_fixture_reauthenticated"+ _assert_refusal(service, store, adapter, (native,), code="migration_item_conflict")+ else:+ counts = _counts(adapter)+ previous = store.config.to_payload()+ _apply(service)+ assert _counts(adapter) == counts+ assert store.config.to_payload() == previous+ assert service.migration_scan()["items"] == []+ expected = ["claude", "codex"] if legacy else [backend]+ assert service.migration_journal.completed()["oauth_custody_backends"] == expected++[email protected]("first", ["api", "empty"])[email protected]("legacy", [False, True])+def test_explicit_empty_marker_distinguishes_new_from_unknown_legacy_history(home, tmp_path, first, legacy):+ if first == "api":+ _api(home, "opencode")+ else:+ _empty(home, "claude")+ service, store, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ assert service.migration_journal.completed()["oauth_custody_backends"] == []+ if legacy:+ _legacy(service)+ native = _oauth(home, "codex")+ if legacy:+ _assert_refusal(service, store, adapter, (native,), code="migration_reauthorization_required")+ else:+ _apply(service)+ assert len(adapter.oauth_provisioned) == 1+ assert service.migration_journal.completed()["oauth_custody_backends"] == ["codex"]+++def test_new_history_allows_both_first_oauth_backends_and_unions_markers(home, tmp_path):+ _api(home, "opencode")+ service, _, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ for backend, expected in (("claude", ["claude"]), ("codex", ["claude", "codex"])):+ _oauth(home, backend)+ _apply(service)+ assert service.migration_journal.completed()["oauth_custody_backends"] == expected+ assert len(adapter.oauth_provisioned) == 2+++def test_unknown_history_is_carried_through_later_api_and_empty_completions(home, tmp_path):+ _api(home, "opencode")+ service, store, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ _legacy(service)+ _api(home, "codex")+ _apply(service)+ _empty(home, "claude")+ _apply(service)+ assert service.migration_journal.completed()["source_ids"] == []+ assert service.migration_journal.completed()["oauth_custody_backends"] == ["claude", "codex"]+ native = _oauth(home, "codex")+ _assert_refusal(service, store, adapter, (native,), code="migration_reauthorization_required")+ assert not adapter.oauth_provisioned+++def test_prior_custody_refusal_precedes_every_proof_in_a_fresh_mixed_selection(home, tmp_path):+ native = _oauth(home, "codex")+ original = native.read_bytes()+ service, store, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ _api(home, "opencode")+ _apply(service)+ native.write_bytes(original)+ other = _api(home, "claude")+ _assert_refusal(service, store, adapter, (native, other), code="migration_reauthorization_required")++[email protected]("change", ["metadata", "tokens"])+def test_earlier_backend_custody_never_guesses_new_native_authorization(home, tmp_path, change):+ native = _oauth(home, "claude")+ original = json.loads(native.read_text())+ service, store, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ _api(home, "opencode")+ _apply(service)+ if change == "metadata":+ original["fixture_metadata"] = True+ else:+ original["claudeAiOauth"].update({+ "accessToken": "fixture-new-access", "refreshToken": "fixture-new-refresh",+ })+ _write(native, json.dumps(original))+ _assert_refusal(service, store, adapter, (native,), code="migration_reauthorization_required")++[email protected]("outcome", ["needs_auth", "reauth_requested"])+def test_terminal_outcome_keeps_backend_fence_after_another_batch(home, tmp_path, outcome):+ native = _oauth(home, "claude")+ original = native.read_bytes()+ service, store, adapter = _service(tmp_path, migration_home=home)+ validate = adapter.validate_oauth_credential++ async def unavailable(ref):+ if outcome == "needs_auth":+ raise OAuthCredentialRejectedError()+ raise RuntimeError("fixture inconclusive validation")++ adapter.validate_oauth_credential = unavailable+ with pytest.raises(ModelHubError):+ _apply(service)+ if outcome == "reauth_requested":+ source = store.config.sources[0]+ asyncio.run(prepare_takeover_reauthentication(service, source.id))+ assert service.migration_journal.completed()["outcome"] == outcome+ assert service.migration_journal.completed()["oauth_custody_backends"] == ["claude"]+ adapter.validate_oauth_credential = validate+ _api(home, "opencode")+ _apply(service)+ native.write_bytes(original)+ _assert_refusal(service, store, adapter, (native,), code="migration_reauthorization_required")+++def test_legacy_active_custody_recovers_without_reapplying_new_grant_gate(home, tmp_path):+ _api(home, "opencode")+ service, store, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ native = _oauth(home, "claude")+ original = native.read_bytes()+ validate = adapter.validate_oauth_credential++ async def unavailable(ref):+ raise RuntimeError("fixture validation interrupted before upgrade")++ adapter.validate_oauth_credential = unavailable+ with pytest.raises(ModelHubError):+ _apply(service)+ assert service.migration_journal.load()["phase"] == "exposed"+ # Model an old release's active handoff plus its older marker-free+ # receipt. Recovery already owns that grant; this is not fresh consent.+ _legacy(service)+ adapter.validate_oauth_credential = validate+ restarted, _, _ = _service(tmp_path, migration_home=home)+ restarted.store, restarted.adapter = store, adapter+ counts = _counts(adapter)+ asyncio.run(restarted.recover_runtime_intent())+ assert _counts(adapter) == counts+ assert restarted.migration_journal.load() is None+ assert restarted.migration_journal.completed()["oauth_custody_backends"] == ["claude", "codex"]+ native.write_bytes(original)+ _apply(restarted)+ assert _counts(adapter) == counts+ assert restarted.migration_scan()["items"] == []++[email protected]("first", ["api", "oauth", "empty"])+def test_first_receipt_interrupted_forget_preserves_explicit_new_history(home, tmp_path, monkeypatch, first):+ if first == "api":+ _api(home, "opencode")+ elif first == "oauth":+ _oauth(home, "claude")+ else:+ _empty(home, "claude")+ service, store, adapter = _service(tmp_path, migration_home=home)+ assert service.migration_journal.completed() is None+ ids = _ids(service)++ def crash():+ raise OSError("fixture first receipt saved before forgetting")++ with monkeypatch.context() as patch:+ patch.setattr(service.migration_journal, "forget", crash)+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply(ids))+ expected = ["claude"] if first == "oauth" else []+ assert service.migration_journal.completed()["oauth_custody_backends"] == expected+ counts = _counts(adapter)+ restarted, _, _ = _service(tmp_path, migration_home=home)+ restarted.store, restarted.adapter = store, adapter+ if first == "empty":+ # This source-free transaction never became exposed. Repeated forget+ # failure left rollback pending; recovery finishes it and reports the+ # original conflict once, without erasing the completed empty marker.+ assert restarted.migration_journal.load()["phase"] == "reverting"+ with pytest.raises(MigrationConflictError):+ asyncio.run(restarted.recover_runtime_intent())+ assert restarted.migration_journal.load() is None+ asyncio.run(restarted.recover_runtime_intent())+ assert asyncio.run(restarted.migration_apply(ids))["applied"] == 1+ assert _counts(adapter) == counts+ assert restarted.migration_journal.completed()["oauth_custody_backends"] == expected+ assert restarted.migration_journal.load() is None++[email protected]("boundary", ["before_save", "after_save", "before_forget", "after_forget"])[email protected]("legacy,rejected", [(False, False), (True, False), (False, True)])+def test_completion_crash_replays_custody_union_once(home, tmp_path, monkeypatch, boundary, legacy, rejected):+ native = _oauth(home, "claude")+ original = native.read_bytes()+ service, store, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ if legacy:+ _legacy(service)+ if rejected:+ _oauth(home, "codex")++ async def reject(ref):+ raise OAuthCredentialRejectedError()++ adapter.validate_oauth_credential = reject+ else:+ _api(home, "opencode")+ ids = _ids(service)+ save, forget = NativeTakeoverJournal.save, NativeTakeoverJournal.forget+ triggered = False++ def crash_save(self, payload):+ nonlocal triggered+ if self.path.name == "last-completed.json" and boundary.endswith("save") and not triggered:+ triggered = True+ if boundary == "after_save":+ save(self, payload)+ raise OSError("fixture receipt publication crash")+ save(self, payload)++ def crash_forget(self):+ nonlocal triggered+ if self.path == service.migration_journal.path and boundary.endswith("forget") and not triggered:+ triggered = True+ if boundary == "after_forget":+ forget(self)+ raise OSError("fixture active journal removal crash")+ forget(self)++ with monkeypatch.context() as patch:+ patch.setattr(NativeTakeoverJournal, "save", crash_save)+ patch.setattr(NativeTakeoverJournal, "forget", crash_forget)+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply(ids))+ assert triggered+ counts = _counts(adapter)+ restarted, _, _ = _service(tmp_path, migration_home=home)+ restarted.store, restarted.adapter = store, adapter+ asyncio.run(restarted.recover_runtime_intent())+ if rejected:+ with pytest.raises(ModelHubError):+ asyncio.run(restarted.migration_apply(ids))+ else:+ assert asyncio.run(restarted.migration_apply(ids))["applied"] == 1+ assert _counts(adapter) == counts+ assert restarted.migration_journal.load() is None+ receipt = restarted.migration_journal.completed()+ assert receipt["oauth_custody_backends"] == (["claude", "codex"] if legacy or rejected else ["claude"])+ assert receipt["outcome"] == ("needs_auth" if rejected else "success")+ native.write_bytes(original)+ _assert_refusal(restarted, store, adapter, (native,), code="migration_reauthorization_required")++[email protected]("marker", [+ None, "", {}, 1, True, ["opencode"], ["unknown"], [None], [[]], [{"bad": True}],+ ["claude", "claude"], ["claude", "codex", "claude"],+])[email protected]("operation", ["load", "save"])+def test_invalid_custody_evidence_is_not_empty_or_legacy(home, tmp_path, marker, operation):+ _api(home, "opencode")+ service, store, adapter = _service(tmp_path, migration_home=home)+ _apply(service)+ receipt = service.migration_journal.completed()+ receipt["oauth_custody_backends"] = marker+ journal = NativeTakeoverJournal(service.migration_journal.path.with_name("last-completed.json"))+ if operation == "save":+ original = journal.path.read_bytes()+ with pytest.raises(TakeoverStateError):+ journal.save(receipt)+ assert journal.path.read_bytes() == original+ else:+ journal.path.write_text(json.dumps(receipt))+ original = journal.path.read_bytes()+ with pytest.raises(TakeoverStateError):+ journal.load()+ counts, previous = _counts(adapter), store.config.to_payload()+ native = _oauth(home, "claude")+ before = native.read_bytes()+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply(["mig_fixture_untrusted"]))+ assert native.read_bytes() == before+ assert _counts(adapter) == counts and store.config.to_payload() == previous+ assert journal.path.read_bytes() == original+++def test_present_marker_cannot_hide_current_bundle_oauth_evidence(home, tmp_path):+ _oauth(home, "claude")+ service, _, _ = _service(tmp_path, migration_home=home)+ _apply(service)+ receipt = service.migration_journal.completed()+ receipt["oauth_custody_backends"] = []+ assert NativeTakeoverJournal.oauth_custody_backends(receipt) == frozenset({"claude"})+ assert "oauth_custody_backends" not in json.dumps(service.migration_scan())+ stored = service.migration_journal.path.with_name("last-completed.json").read_text()+ assert "claude-refresh-token" not in stored and "claude-oauth-token" not in storeddiff --git a/tests/test_model_hub_migration_review_boundaries.py b/tests/test_model_hub_migration_review_boundaries.pynew file mode 100644index 000000000..2ff32dcf1--- /dev/null+++ b/tests/test_model_hub_migration_review_boundaries.py@@ -0,0 +1,500 @@+"""Review regressions consume only synthetic profiles and native stores."""++from __future__ import annotations++import asyncio+import json+import os+import stat+from pathlib import Path++import pytest++from config.v2_config import ModelHubSourceConfig+from core.handlers.model_hub import migration_journal+from core.handlers.model_hub.migration_journal import NativeFileEdit, TakeoverStateError+from core.handlers.model_hub.migration_shell import cleanup_shell_profile, read_shell_profiles+from core.handlers.model_hub.service import ModelHubError+from tests.scenarios.model_hub.test_model_hub_migration_scenarios import _service, _write+from tests.test_model_hub_migration_transport import KEY as HTTP_KEY+from tests.test_model_hub_migration_transport import _real_http_adapter, _upstream+from tests.test_model_hub_persisted_inventory import home # noqa: F401 - isolated fixture+++KEY = "fixture-canonical-key"+RAW_KEY = f" \t{KEY} \t"+++def _seed(home: Path, shape: str) -> tuple[str, Path]:+ backend = shape.split("-")[0]+ name = {+ "claude-api": "ANTHROPIC_API_KEY",+ "claude-bearer": "ANTHROPIC_AUTH_TOKEN",+ "codex-api": "OPENAI_API_KEY",+ "codex-alias": "CODEX_API_KEY",+ "opencode-api": "OPENROUTER_API_KEY",+ }.get(shape, "FIXTURE_SAVED_KEY")+ profile = home / ".profile"+ _write(profile, f"# 保留偏好\r\nexport {name}='{RAW_KEY}'\r\n")+ if shape == "claude-bearer":+ with profile.open("a") as handle:+ handle.write("export ANTHROPIC_BASE_URL='https://fixture.example'\n")+ elif shape == "codex-reference":+ _write(home / ".codex/config.toml", '[model_providers.fixture]\nenv_key="FIXTURE_SAVED_KEY"\n')+ elif shape == "opencode-reference":+ _write(home / ".config/opencode/opencode.json", json.dumps({+ "provider": {"openai": {"options": {"apiKey": "{env:FIXTURE_SAVED_KEY}"}}},+ }))+ return backend, profile++[email protected]("shape", [+ "claude-api", "claude-bearer", "codex-api", "codex-alias",+ "opencode-api", "codex-reference", "opencode-reference",+])+def test_persisted_key_proof_and_custody_use_one_value(home, tmp_path, shape):+ backend, profile = _seed(home, shape)+ service, store, adapter = _service(tmp_path, migration_home=home)+ proof_values = []+ transient = adapter.provision_transient_credential++ async def capture_proof(vendor, secret, base_url, **kwargs):+ proof_values.append(secret)+ return await transient(vendor, secret, base_url, **kwargs)++ adapter.provision_transient_credential = capture_proof+ rows = [row for row in service.migration_scan()["items"] if row["backend"] == backend]+ assert len(rows) == 1 and rows[0]["proposed_action"] == "import"+ result = asyncio.run(service.migration_apply([rows[0]["id"]]))+ assert result["applied"] == 1+ [source] = store.config.sources+ assert proof_values == [adapter.keys[source.credential_ref][2]] == [KEY]+ assert profile.read_bytes() == "# 保留偏好\r\n".encode()+ assert service.migration_journal.load() is None++[email protected]("shape", ["claude-bearer", "codex-api", "codex-reference", "opencode-reference"])+def test_padded_persisted_key_reuses_the_proven_canonical_credential(home, tmp_path, shape):+ backend, profile = _seed(home, shape)+ service, store, adapter = _service(tmp_path, migration_home=home)+ vendor = "anthropic" if backend == "claude" else "openai"+ protocol = "anthropic" if backend == "claude" else "openai_responses"+ base_url = "https://fixture.example" if backend == "claude" else None+ scheme = "bearer" if backend == "claude" else None+ ref = asyncio.run(adapter.provision_credential(vendor, protocol, KEY, base_url, auth_scheme=scheme))+ store.config.sources.append(ModelHubSourceConfig.from_payload({+ "id": "src_paddingfixture", "kind": "api_key", "vendor": vendor,+ "display_name": "Existing fixture", "protocol": protocol,+ "base_url": base_url, "supply_channel": "hub", "billing": "metered",+ "state": {"status": "standby"}, "models": [], "credential_ref": ref,+ }))+ [row] = service.migration_scan()["items"]+ asyncio.run(service.migration_apply([row["id"]]))+ assert len(adapter.provisioned) == len(store.config.sources) == 1+ assert store.config.sources[0].credential_ref == ref+ assert adapter.observed and not adapter.transient_refs+ assert profile.read_bytes() == "# 保留偏好\r\n".encode()+++def test_padded_bearer_key_remains_usable_after_actual_http_proof_and_custody(home, tmp_path):+ async def scenario():+ async with _upstream("bearer") as (origin, requests):+ profile = home / ".profile"+ _write(profile, (+ f"export ANTHROPIC_AUTH_TOKEN=' \t{HTTP_KEY} \t'\n"+ f"export ANTHROPIC_BASE_URL='{origin}'\n"+ ))+ service, store, _ = _service(tmp_path, migration_home=home)+ runtime = _real_http_adapter(service, tmp_path)+ [row] = service.migration_scan()["items"]+ assert row["proposed_action"] == "import"+ await service.migration_apply([row["id"]])+ [source] = store.config.sources+ assert runtime.state_store.read_api_key(source.credential_ref) == HTTP_KEY+ assert await runtime.credential_auth_scheme(source.credential_ref) == "bearer"+ requests.clear()+ models = await runtime.discover_models("anthropic", "anthropic", origin, source.credential_ref)+ assert models+ assert any(headers.get("authorization") == f"Bearer {HTTP_KEY}" for _, headers in requests)+ assert all("x-api-key" not in headers for _, headers in requests)+ assert profile.read_bytes() == b""++ asyncio.run(scenario())+++def test_whitespace_only_shell_assignments_are_not_credentials(home, tmp_path):+ profile = home / ".profile"+ _write(profile, "export OPENAI_API_KEY=' \t '\n")+ before = profile.read_bytes()+ service, _, adapter = _service(tmp_path, migration_home=home)+ assert service.migration_scan()["items"] == []+ assert not adapter.provisioned+ assert profile.read_bytes() == before++[email protected]("selected_backend", ["claude", "codex", "opencode"])+def test_prepared_journal_contains_only_selected_credential_store_guards(home, tmp_path, selected_backend):+ unrelated = {+ "claude": (home / ".claude/.credentials.json", {+ "claudeAiOauth": {"accessToken": "fixture-unrelated-claude", "refreshToken": "fixture-refresh-claude"},+ }),+ "codex": (home / ".codex/auth.json", {+ "tokens": {"access_token": "fixture-unrelated-codex", "refresh_token": "fixture-refresh-codex"},+ }),+ "opencode": (home / ".local/share/opencode/auth.json", {+ "google": {"type": "oauth", "access": "fixture-unrelated-opencode", "refresh": "fixture-refresh-opencode"},+ }),+ }+ for backend, (path, payload) in unrelated.items():+ if backend != selected_backend:+ _write(path, json.dumps(payload))+ profile = home / ".profile"+ variable = {"claude": "ANTHROPIC_API_KEY", "codex": "OPENAI_API_KEY", "opencode": "OPENROUTER_API_KEY"}+ _write(profile, f"export {variable[selected_backend]}='{KEY}'\n")+ service, _, adapter = _service(tmp_path, migration_home=home)+ saved = []+ save = service.migration_journal.save++ def record_prepared(record):+ if record["phase"] == "prepared":+ saved.append(json.loads(json.dumps(record)))+ save(record)++ service.migration_journal.save = record_prepared+ original = {path: path.read_bytes() for backend, (path, _) in unrelated.items() if backend != selected_backend}+ rows = [row for row in service.migration_scan()["items"] if row["backend"] == selected_backend]+ assert len(rows) == 1 and rows[0]["proposed_action"] == "import"+ asyncio.run(service.migration_apply([rows[0]["id"]]))+ assert saved and adapter.provisioned+ for record in saved:+ edits = [NativeFileEdit.from_payload(payload) for payload in record["files"]]+ guarded = {edit.path for edit in edits}+ assert not guarded.intersection(original)+ assert unrelated[selected_backend][0] in guarded+ # Cross-backend config consumers, including absent layers, remain bound.+ assert {+ home / ".claude/settings.json",+ home / ".codex/config.toml",+ home / ".config/opencode/opencode.json",+ } <= guarded+ for edit in edits:+ assert b"fixture-unrelated-" not in (edit.before or b"")+ assert b"fixture-refresh-" not in (edit.before or b"")+ assert {path: path.read_bytes() for path in original} == original+ assert profile.read_bytes() == b""++[email protected]("mode", [0o600, 0o640, 0o644, 0o750, 0o755])+def test_shell_mode_survives_journal_forward_reverse_and_replay(home, mode):+ path = home / ".bashrc"+ before = f"# 保留偏好\nexport OPENAI_API_KEY='{KEY}'\n".encode()+ path.write_bytes(before)+ path.chmod(mode)+ [profile] = [p for p in read_shell_profiles(home, frozenset({"OPENAI_API_KEY"})) if p.path == path]+ edit = cleanup_shell_profile(profile, {"OPENAI_API_KEY": KEY})+ assert edit.mode == mode+ for reverse in (False, False, True, True):+ NativeFileEdit.from_payload(edit.to_payload()).apply(reverse=reverse)+ assert path.read_bytes() == (before if reverse else "# 保留偏好\n".encode())+ assert stat.S_IMODE(path.stat().st_mode) == mode++[email protected]("reverse", [False, True])+def test_shell_mode_recovers_after_interrupted_atomic_publication(home, monkeypatch, reverse):+ path = home / ".bashrc"+ before = f"export OPENAI_API_KEY='{KEY}'\n# keep\n".encode()+ path.write_bytes(before)+ path.chmod(0o755)+ [profile] = [p for p in read_shell_profiles(home, frozenset({"OPENAI_API_KEY"})) if p.path == path]+ payload = cleanup_shell_profile(profile, {"OPENAI_API_KEY": KEY}).to_payload()+ replace = os.replace++ def interrupt_after_replace(source, target):+ replace(source, target)+ if Path(target) == path:+ raise InterruptedError("fixture interrupted after publication")++ with monkeypatch.context() as patch:+ patch.setattr(os, "replace", interrupt_after_replace)+ with pytest.raises(InterruptedError):+ NativeFileEdit.from_payload(payload).apply()+ assert path.read_bytes() == b"# keep\n"+ assert stat.S_IMODE(path.stat().st_mode) == 0o755+ NativeFileEdit.from_payload(payload).apply(reverse=reverse)+ assert path.read_bytes() == (before if reverse else b"# keep\n")+ assert stat.S_IMODE(path.stat().st_mode) == 0o755+++def test_compare_only_guard_does_not_claim_file_permissions(home, monkeypatch):+ path = home / ".bashrc"+ path.write_bytes(b"# untouched\n")+ path.chmod(0o755)+ guard = NativeFileEdit(path, path.read_bytes(), path.read_bytes())++ def forbidden(*args, **kwargs):+ pytest.fail("a compare-only guard cannot change permissions")++ monkeypatch.setattr(os, "chmod", forbidden)+ monkeypatch.setattr(os, "fchmod", forbidden)+ guard.apply()+ guard.apply(reverse=True)+ assert stat.S_IMODE(path.stat().st_mode) == 0o755++[email protected]("replacement", ["before_open", "after_open"])+def test_mode_replay_rejects_replaced_content_without_chmod(home, monkeypatch, replacement):+ path = home / ".bashrc"+ path.write_bytes(b"before\n")+ path.chmod(0o644)+ edit = NativeFileEdit.plan(path, b"after\n")+ edit.apply()+ original_open = os.open+ changed = False++ def replace():+ private = home / "fixture-replacement"+ private.write_bytes(b"new private content\n")+ private.chmod(0o600)+ private.replace(path)++ def racing_open(target, flags, *args, **kwargs):+ nonlocal changed+ if Path(target) == path and not changed:+ changed = True+ if replacement == "before_open":+ replace()+ descriptor = original_open(target, flags, *args, **kwargs)+ if replacement == "after_open":+ replace()+ return descriptor+ return original_open(target, flags, *args, **kwargs)++ monkeypatch.setattr(os, "open", racing_open)+ with pytest.raises(TakeoverStateError):+ NativeFileEdit.from_payload(edit.to_payload()).apply()+ assert path.read_bytes() == b"new private content\n"+ assert stat.S_IMODE(path.stat().st_mode) == 0o600++[email protected]("operation", ["check", "apply"])+def test_selected_shell_mode_change_invalidates_planned_edit(home, operation):+ path = home / ".bashrc"+ before = f"export OPENAI_API_KEY='{KEY}'\n# keep\n".encode()+ path.write_bytes(before)+ path.chmod(0o644)+ [profile] = [p for p in read_shell_profiles(home, frozenset({"OPENAI_API_KEY"})) if p.path == path]+ payload = cleanup_shell_profile(profile, {"OPENAI_API_KEY": KEY}).to_payload()+ path.chmod(0o600)+ with pytest.raises(TakeoverStateError):+ getattr(NativeFileEdit.from_payload(payload), operation)()+ assert path.read_bytes() == before+ assert stat.S_IMODE(path.stat().st_mode) == 0o600++[email protected]("boundary", ["provision_credential", "ensure_installed"])+def test_service_rejects_tightened_shell_permissions_without_restoring_them(home, tmp_path, boundary):+ _, path = _seed(home, "codex-api")+ path.chmod(0o644)+ original = path.read_bytes()+ service, store, adapter = _service(tmp_path, migration_home=home)+ method = getattr(adapter, boundary)++ async def tighten(*args, **kwargs):+ result = await method(*args, **kwargs)+ path.chmod(0o600)+ return result++ setattr(adapter, boundary, tighten)+ [row] = service.migration_scan()["items"]+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([row["id"]]))+ assert path.read_bytes() == original+ assert stat.S_IMODE(path.stat().st_mode) == 0o600+ assert not store.config.sources+ assert not adapter.synced+++def test_owned_mode_replay_fsyncs_already_restored_permissions(home, monkeypatch):+ path = home / ".bashrc"+ path.write_bytes(b"before\n")+ path.chmod(0o644)+ edit = NativeFileEdit.plan(path, b"after\n")+ fsync, replace = os.fsync, os.replace++ def interrupt_after_publication(source, target):+ replace(source, target)+ if Path(target) == path:+ raise InterruptedError("fixture publication before directory sync")++ with monkeypatch.context() as patch:+ patch.setattr(os, "replace", interrupt_after_publication)+ with pytest.raises(InterruptedError):+ edit.apply()+ assert path.read_bytes() == b"after\n"+ flushed = []++ def record_fsync(descriptor):+ if stat.S_ISREG(os.fstat(descriptor).st_mode):+ flushed.append(os.fstat(descriptor).st_ino)+ fsync(descriptor)++ monkeypatch.setattr(os, "fsync", record_fsync)+ NativeFileEdit.from_payload(edit.to_payload()).apply()+ assert path.stat().st_ino in flushed++[email protected]("failure", ["fchmod", "fsync", "replace"])+def test_unpublished_mode_failure_preserves_original_and_removes_temporary(home, monkeypatch, failure):+ path = home / ".bashrc"+ path.write_bytes(b"before\n")+ path.chmod(0o644)+ edit = NativeFileEdit.plan(path, b"after\n")+ baseline = set(home.iterdir())++ def fail(*args, **kwargs):+ raise OSError("fixture temporary publication failure")++ monkeypatch.setattr(os, failure, fail)+ with pytest.raises(OSError):+ edit.apply()+ assert path.read_bytes() == b"before\n"+ assert stat.S_IMODE(path.stat().st_mode) == 0o644+ assert set(home.iterdir()) == baseline+++def test_source_chmod_during_temporary_write_rejects_without_widening(home, monkeypatch):+ path = home / ".bashrc"+ path.write_bytes(b"before\n")+ path.chmod(0o644)+ edit = NativeFileEdit.plan(path, b"after\n")+ fsync = os.fsync+ baseline = set(home.iterdir())++ def tighten_source(descriptor):+ path.chmod(0o600)+ fsync(descriptor)++ monkeypatch.setattr(os, "fsync", tighten_source)+ with pytest.raises(TakeoverStateError):+ edit.apply()+ # Rollback cannot claim this untouched source's mode either.+ with pytest.raises(TakeoverStateError):+ edit.apply(reverse=True)+ assert path.read_bytes() == b"before\n"+ assert stat.S_IMODE(path.stat().st_mode) == 0o600+ assert set(home.iterdir()) == baseline+++def test_legacy_edit_default_mode_is_not_before_mode_evidence(home):+ path = home / ".bashrc"+ path.write_bytes(b"before\n")+ path.chmod(0o644)+ edit = NativeFileEdit.from_payload(NativeFileEdit(path, b"before\n", b"after\n").to_payload())+ edit.check()+ edit.apply()+ assert path.read_bytes() == b"after\n"+ assert stat.S_IMODE(path.stat().st_mode) == 0o600+++def test_reverse_uses_captured_before_mode_when_target_mode_differs(home):+ path = home / ".profile"+ path.write_bytes(b"before\n")+ path.chmod(0o640)+ edit = NativeFileEdit(path, b"before\n", b"after\n", 0o600, 0o640)+ edit.apply()+ assert stat.S_IMODE(path.stat().st_mode) == 0o600+ NativeFileEdit.from_payload(edit.to_payload()).apply(reverse=True)+ assert path.read_bytes() == b"before\n"+ assert stat.S_IMODE(path.stat().st_mode) == 0o640+ NativeFileEdit.from_payload(edit.to_payload()).apply(reverse=True)+++def test_captured_private_shell_mode_survives_restrictive_umask(home):+ path = home / ".profile"+ path.write_bytes(b"before\n")+ path.chmod(0o600)+ edit = NativeFileEdit.plan(path, b"after\n")+ previous_mask = os.umask(0o200)+ try:+ edit.apply()+ NativeFileEdit.from_payload(edit.to_payload()).apply(reverse=True)+ finally:+ os.umask(previous_mask)+ assert path.read_bytes() == b"before\n"+ assert stat.S_IMODE(path.stat().st_mode) == 0o600+++def test_deleted_target_replay_completes_directory_durability(home, monkeypatch):+ path = home / ".fixture"+ path.write_bytes(b"fixture\n")+ edit = NativeFileEdit.plan(path, None)+ edit.apply()+ flushed = []+ monkeypatch.setattr(migration_journal, "_fsync_directory", flushed.append)+ edit.apply()+ assert flushed == [home]++[email protected]("builtin", ["mapfile", "readarray"])[email protected]("arguments", [+ "OPENAI_API_KEY", "-t OPENAI_API_KEY", "-tn2 OPENAI_API_KEY",+ "-d '' -n 2 -O 0 -s 1 -t -u 3 -C callback -c 1 OPENAI_API_KEY",+ "-d: -n2 -O0 -s1 -u3 -Ccallback -c1 -- OPENAI_API_KEY",+])[email protected]("prior_literal", [False, True])+def test_array_readers_are_dynamic_credential_writers(home, builtin, arguments, prior_literal):+ path = home / ".bashrc"+ text = (f"export OPENAI_API_KEY='{KEY}'\n" if prior_literal else "")+ text += f"{builtin} {arguments} < /fixture/not-read\n"+ path.write_text(text)+ [profile] = [p for p in read_shell_profiles(home, frozenset({"OPENAI_API_KEY"})) if p.path == path]+ assert "OPENAI_API_KEY" not in profile.values+ assert any(issue.reason == "dynamic_shell" and "OPENAI_API_KEY" in issue.names for issue in profile.issues)+ with pytest.raises(TakeoverStateError):+ cleanup_shell_profile(profile, {"OPENAI_API_KEY": KEY})+ assert path.read_text() == text++[email protected]("builtin", ["mapfile", "readarray"])[email protected]("arguments", [+ "-d OPENAI_API_KEY unrelated",+ "-C OPENAI_API_KEY -c 1 unrelated",+ "-t unrelated < OPENAI_API_KEY",+])+def test_array_reader_option_operands_and_redirections_are_not_destinations(home, builtin, arguments):+ path = home / ".bashrc"+ path.write_text(f"{builtin} {arguments}\nexport OPENAI_API_KEY='{KEY}'\n")+ [profile] = [p for p in read_shell_profiles(home, frozenset({"OPENAI_API_KEY"})) if p.path == path]+ assert profile.values == {"OPENAI_API_KEY": KEY}+ assert profile.issues == ()++[email protected]("writer", [+ "builtin mapfile -t OPENAI_API_KEY",+ "command readarray -- OPENAI_API_KEY",+ "if true; then mapfile -t OPENAI_API_KEY; fi",+ "f() { readarray OPENAI_API_KEY; }",+ 'echo "$(mapfile OPENAI_API_KEY)"',+ "eval 'readarray OPENAI_API_KEY'",+ "mapfile -C 'export OPENAI_API_KEY=dynamic' unrelated",+ "readarray -tC'export OPENAI_API_KEY=dynamic' unrelated",+])+def test_array_reader_nested_and_explicit_callback_writers_are_blocked(home, tmp_path, writer):+ path = home / ".bashrc"+ text = f"export OPENAI_API_KEY='{KEY}'\n{writer}\n"+ path.write_text(text)+ service, store, adapter = _service(tmp_path, migration_home=home)+ [row] = service.migration_scan()["items"]+ assert row["proposed_action"] == "reauth" and not row["selected"]+ assert row["notes_key"] == "settings.models.migration.blocked.dynamic_shell"+ assert path.read_text() == text+ assert not adapter.provisioned and not store.config.sources++[email protected]("builtin", ["mapfile", "readarray"])+def test_array_reader_default_destination_is_also_a_persisted_reference(home, builtin):+ path = home / ".bashrc"+ path.write_text(f"MAPFILE=fixture\n{builtin} -t < /fixture/not-read\n")+ [profile] = [p for p in read_shell_profiles(home, frozenset({"MAPFILE"})) if p.path == path]+ assert profile.values == {}+ assert profile.issues[0].names == ("MAPFILE",)diff --git a/tests/test_model_hub_migration_shell.py b/tests/test_model_hub_migration_shell.pynew file mode 100644index 000000000..334a858fe--- /dev/null+++ b/tests/test_model_hub_migration_shell.py@@ -0,0 +1,379 @@+"""Hermetic grammar and journal contracts for persisted shell migration."""++from dataclasses import FrozenInstanceError+from pathlib import Path++import pytest++from core.handlers.model_hub.migration_journal import NativeFileEdit, TakeoverStateError+from core.handlers.model_hub.migration_shell import (+ SHELL_PROFILE_NAMES,+ ShellAssignment,+ ShellIssue,+ cleanup_shell_profile,+ read_shell_profiles,+)+++KEY = "OPENAI_API_KEY"+OTHER = "ANTHROPIC_API_KEY"+NAMES = frozenset({KEY, OTHER})+++def _profile(tmp_path, content: str | bytes):+ path = tmp_path / ".bashrc"+ path.write_bytes(content.encode() if isinstance(content, str) else content)+ return next(profile for profile in read_shell_profiles(tmp_path, NAMES) if profile.path == path)++[email protected]("line,value", [+ ("OPENAI_API_KEY=fixture\n", "fixture"),+ ("export OPENAI_API_KEY=fixture\n", "fixture"),+ ("\t export\tOPENAI_API_KEY='fixture space' # retain semantics\n", "fixture space"),+ ('OPENAI_API_KEY="fixture space"\r\n', "fixture space"),+ ("OPENAI_API_KEY='fixture # $HOME `echo value` $(command) {literal}'\n",+ "fixture # $HOME `echo value` $(command) {literal}"),+ ('OPENAI_API_KEY="fixture \\$HOME \\`quoted\\` \\\\ \\""\n', 'fixture $HOME `quoted` \\ "'),+ ('OPENAI_API_KEY="literal\\q"\n', "literal\\q"),+ (r"OPENAI_API_KEY=fixture\ space\#hash" + "\n", "fixture space#hash"),+ ("OPENAI_API_KEY=fixture#hash\n", "fixture#hash"),+ ("OPENAI_API_KEY='前缀'\"密钥\"suffix\r\n", "前缀密钥suffix"),+ ("OPENAI_API_KEY=\n", ""),+ ("OPENAI_API_KEY=''\n", ""),+ ('OPENAI_API_KEY=""', ""),+ ("OPENAI_API_KEY=fixture", "fixture"),+])+def test_complete_literal_line_has_exact_value_and_byte_span(tmp_path, line, value):+ prefix = "# 中文上下文\r\n"+ profile = _profile(tmp_path, prefix + line)+ assert profile.values == {KEY: value}+ assert profile.issues == ()+ assignment, = profile.assignments+ assert assignment.start == len(prefix.encode())+ assert assignment.end == len((prefix + line).encode())+ assert profile.before[assignment.start:assignment.end] == line.encode()+ assert cleanup_shell_profile(profile, {KEY: value}).after == prefix.encode()++[email protected]("text", [+ 'OPENAI_API_KEY="$LIVE_KEY"\n',+ "OPENAI_API_KEY=${LIVE_KEY:-fallback}\n",+ "OPENAI_API_KEY=$(printf fixture)\n",+ 'OPENAI_API_KEY="prefix$(printf fixture)suffix"\n',+ "OPENAI_API_KEY=`printf fixture`\n",+ "OPENAI_API_KEY=~/.key\n",+ "OPENAI_API_KEY=$'fixture\\n'\n",+ "OPENAI_API_KEY=$((1 + 2))\n",+ "OPENAI_API_KEY={one,two}\n",+ "OPENAI_API_KEY+=fixture\n",+ "OPENAI_API_KEY[0]=fixture\n",+ "OPENAI_API_KEY=fixture command\n",+ "OPENAI_API_KEY=fixture; echo done\n",+ "OPENAI_API_KEY=fixture OTHER=value\n",+ 'export "OPENAI_API_KEY=fixture"\n',+ "export OPENAI_API_KEY=fixture ANTHROPIC_API_KEY=other\n",+ "readonly OPENAI_API_KEY=fixture\n",+ "declare -x OPENAI_API_KEY=fixture\n",+ "typeset OPENAI_API_KEY=fixture\n",+ "local OPENAI_API_KEY=fixture\n",+ "env OPENAI_API_KEY=fixture command\n",+ "builtin export OPENAI_API_KEY=fixture\n",+ "command export OPENAI_API_KEY=fixture\n",+ "if true; then\n OPENAI_API_KEY=fixture\nfi\n",+ "if true; then export OPENAI_API_KEY=fixture; fi\n",+ "for item in one two; do\nOPENAI_API_KEY=fixture\ndone\n",+ "while true; do\nOPENAI_API_KEY=fixture\ndone\n",+ "until true; do\nOPENAI_API_KEY=fixture\ndone\n",+ "case x in\nx)\nOPENAI_API_KEY=fixture\n;;\nesac\n",+ "f() {\nOPENAI_API_KEY=fixture\n}\n",+ "f()\n{\nOPENAI_API_KEY=fixture\n}\n",+ "function f {\nOPENAI_API_KEY=fixture\n}\n",+ "function f { export OPENAI_API_KEY=fixture; }\n",+ "{\nOPENAI_API_KEY=fixture\n}\n",+ "(\nOPENAI_API_KEY=fixture\n)\n",+ "true &&\nOPENAI_API_KEY=fixture\n",+ "true ||\n# comment\nOPENAI_API_KEY=fixture\n",+ "true |\nOPENAI_API_KEY=fixture\n",+ "OPENAI_API_KEY=\\\nfixture\n",+ "export \\\nOPENAI_API_KEY=fixture\n",+ 'OPENAI_API_KEY="multi\nline"\n',+ "OPENAI_API_KEY='unterminated\n",+ "cat <<EOF\nOPENAI_API_KEY=fixture\nEOF\n",+ "cat <<'EOF'\nexport OPENAI_API_KEY=fixture\nEOF\n",+ "cat <<-EOF\n\tOPENAI_API_KEY=fixture\n\tEOF\n",+ "eval 'export OPENAI_API_KEY=fixture'\n",+ "printf -v OPENAI_API_KEY '%s' fixture\n",+ "read -r OPENAI_API_KEY\n",+ "unset OPENAI_API_KEY\n",+ "for OPENAI_API_KEY in fixture; do :; done\n",+ 'echo "${OPENAI_API_KEY:=fixture}"\n',+ "let 'OPENAI_API_KEY=1'\n",+ "(( OPENAI_API_KEY = 1 ))\n",+ 'echo "$(export OPENAI_API_KEY=fixture)"\n',+ "! {\nOPENAI_API_KEY=fixture\n}\n",+ "if true; then if true; then :; fi\nOPENAI_API_KEY=fixture\nfi\n",+ "if if true; then :; fi; then\nOPENAI_API_KEY=fixture\nfi\n",+ "((\nOPENAI_API_KEY = 1\n))\n",+ "[[\nOPENAI_API_KEY=fixture\n]]\n",+ "foreach x (one two)\nOPENAI_API_KEY=fixture\nend\n",+ "repeat 3 {\nOPENAI_API_KEY=fixture\n}\n",+ "coproc worker {\nOPENAI_API_KEY=fixture\n}\n",+ "{ :; } always {\nOPENAI_API_KEY=fixture\n}\n",+ "true &&\\\nOPENAI_API_KEY=fixture\n",+ "OP\\\nENAI_API_KEY=fixture\n",+ "export \\\r\nOPENAI_API_KEY=fixture\r\n",+ 'echo "$(( OPENAI_API_KEY = 1 ))"\n',+ 'echo "$(echo "$(export OPENAI_API_KEY=fixture)")"\n',+ "cat <<EOF\nOPENAI_API_KEY=fixture\n",+ 'echo "${OTHER:-$(export OPENAI_API_KEY=fixture)}"\n',+ "2>/dev/null OPENAI_API_KEY=fixture\n",+ "eval " * 15 + "export OPENAI_API_KEY=fixture\n",+])+def test_nonliteral_or_non_top_level_writers_are_targeted_issues(tmp_path, text):+ profile = _profile(tmp_path, text)+ assert KEY not in profile.values+ assert profile.assignments == ()+ assert any(KEY in issue.names and issue.reason == "dynamic_shell" for issue in profile.issues)+ assert all(issue.line and issue.line >= 1 for issue in profile.issues)+ with pytest.raises(TakeoverStateError, match="manual migration"):+ cleanup_shell_profile(profile, {KEY: "fixture"})+ assert profile.path.read_bytes() == text.encode()++[email protected]("text", [+ "# export OPENAI_API_KEY=fixture\n",+ 'echo "OPENAI_API_KEY=fixture"\n',+ "echo OPENAI_API_KEY=fixture\n",+ "printf '%s' 'export OPENAI_API_KEY=fixture'\n",+ 'echo "$OPENAI_API_KEY"\n',+ "echo ${OPENAI_API_KEY:-default}\n",+ "export OPENAI_API_KEY\n",+ "alias example='export OPENAI_API_KEY=fixture'\n",+ "OTHER='OPENAI_API_KEY=fixture'\n",+ 'OTHER="if ; ( ) { source file; OPENAI_API_KEY=fixture"\n',+ "echo 'if'\necho '{'\necho 'f()'\necho '<<EOF'\n",+ 'echo "multiline\nOPENAI_API_KEY=fixture\nstring"\n',+ "eval \"echo 'OPENAI_API_KEY=fixture'\"\n",+ 'echo "$(echo OPENAI_API_KEY=fixture)"\n',+ 'echo "$( (echo \'OPENAI_API_KEY=fixture\') )"\n',+ ". ~/.other\n",+ "source ~/.other\n",+ "source 'OPENAI_API_KEY=fixture'\n",+ "test -f ~/.bashrc && . ~/.bashrc\n",+ "if [ -f ~/.bashrc ]; then\n. ~/.bashrc\nfi\n",+ 'export PATH="$HOME/bin:$PATH"\n',+ 'cd "$HOME"\nset -o vi\numask 022\n',+ '[[ -n "$HOME" ]]\n',+ '[[ -n "$HOME"\n&& -d "$HOME" ]]\n',+ 'if [[ -n "$HOME" ]]; then :; fi\n',+ "env printf '%s' OPENAI_API_KEY=fixture\n",+ "read -p OPENAI_API_KEY unrelated\n",+ "printf '%s' -v OPENAI_API_KEY\n",+ 'echo "${OTHER:-OPENAI_API_KEY=fixture}"\n',+ 'f() if true; then echo "OPENAI_API_KEY=quoted"; fi\n',+ 'echo 2>/dev/null OPENAI_API_KEY=fixture\n',+])+def test_references_comments_and_quoted_data_are_not_credential_writers(tmp_path, text):+ profile = _profile(tmp_path, text + "OPENAI_API_KEY=fixture\n")+ assert profile.values == {KEY: "fixture"}+ assert profile.issues == ()+ assert len(profile.assignments) == 1+ assert cleanup_shell_profile(profile, {KEY: "fixture"}).after == text.encode()+++def test_ordinary_include_never_follows_unknown_script(tmp_path):+ included = tmp_path / "outside"+ included.write_text("export OPENAI_API_KEY=not-scanned\n")+ profile = _profile(tmp_path, f'. "{included}"\n')+ assert profile.values == {}+ assert profile.assignments == ()+ assert profile.issues == ()+ assert cleanup_shell_profile(profile, {KEY: "not-scanned"}).after == profile.before+++def test_matching_duplicates_are_removable_but_differing_duplicates_are_ambiguous(tmp_path):+ profile = _profile(tmp_path, "OPENAI_API_KEY=fixture\nexport OPENAI_API_KEY='fixture'\n")+ assert profile.values == {KEY: "fixture"}+ assert len(profile.assignments) == 2+ assert cleanup_shell_profile(profile, {KEY: "fixture"}).after == b""+ profile = _profile(tmp_path, "OPENAI_API_KEY=fixture\nOPENAI_API_KEY=different\n")+ assert profile.values == {}+ assert len(profile.assignments) == 2+ issue, = profile.issues+ assert issue.names == (KEY,)+ assert issue.reason == "ambiguous_shell"+ assert issue.line == 2+ with pytest.raises(TakeoverStateError):+ cleanup_shell_profile(profile, {KEY: "fixture"})+++def test_invalid_name_is_excluded_without_blocking_an_unrelated_name(tmp_path):+ content = (+ "OPENAI_API_KEY=fixture\n"+ 'if true; then\nOPENAI_API_KEY="$DYNAMIC"\nfi\n'+ "ANTHROPIC_API_KEY=other\n"+ )+ profile = _profile(tmp_path, content)+ assert profile.values == {OTHER: "other"}+ assert cleanup_shell_profile(profile, {OTHER: "other"}).after == content.rsplit("ANTHROPIC", 1)[0].encode()+ with pytest.raises(TakeoverStateError):+ cleanup_shell_profile(profile, {KEY: "fixture"})+++def test_multiple_heredocs_do_not_change_outer_shell_structure(tmp_path):+ prefix = (+ "cat <<'ONE' <<-TWO\n"+ "if this were code; then {\n"+ "ONE\n"+ "\techo '}'\n"+ "\tTWO\n"+ )+ profile = _profile(tmp_path, prefix + "OPENAI_API_KEY=fixture\n")+ assert profile.values == {KEY: "fixture"}+ assert profile.issues == ()+ assert cleanup_shell_profile(profile, {KEY: "fixture"}).after == prefix.encode()+++def test_cleanup_round_trips_through_journal_without_reformatting(tmp_path):+ before = (+ "# 注释\r\nexport OPENAI_API_KEY='fixture-密钥' # consented\r\n"+ "\r\nexport PATH=\"$HOME/bin:$PATH\"\nANTHROPIC_API_KEY=keep\n"+ "export OPENAI_API_KEY=fixture-密钥\n# 尾部"+ ).encode()+ after = (+ "# 注释\r\n\r\nexport PATH=\"$HOME/bin:$PATH\"\n"+ "ANTHROPIC_API_KEY=keep\n# 尾部"+ ).encode()+ profile = _profile(tmp_path, before)+ edit = cleanup_shell_profile(profile, {KEY: "fixture-密钥"})+ assert edit.before == before+ assert edit.after == after+ assert profile.path.read_bytes() == before+ restored = NativeFileEdit.from_payload(edit.to_payload())+ assert restored == edit+ restored.check()+ restored.apply()+ restored.apply()+ assert profile.path.read_bytes() == after+ restored.check(applied=True)+ restored.apply(reverse=True)+ restored.apply(reverse=True)+ assert profile.path.read_bytes() == before+++def test_noop_guards_and_selected_value_mismatch(tmp_path):+ profile = _profile(tmp_path, "OPENAI_API_KEY=fixture\n")+ for selected in ({}, {OTHER: "absent"}):+ edit = cleanup_shell_profile(profile, selected)+ assert edit.before == edit.after == profile.before+ edit.check()+ edit.apply()+ with pytest.raises(TakeoverStateError, match="value changed") as error:+ cleanup_shell_profile(profile, {KEY: "mismatched-secret"})+ assert "mismatched-secret" not in str(error.value)+ assert "fixture" not in str(error.value)+++def test_changed_or_new_file_fails_compare_before_write(tmp_path):+ profile = _profile(tmp_path, "OPENAI_API_KEY=fixture\n")+ edit = cleanup_shell_profile(profile, {KEY: "fixture"})+ profile.path.write_bytes(b"concurrent edit\n")+ with pytest.raises(TakeoverStateError, match="changed"):+ edit.apply()+ assert profile.path.read_bytes() == b"concurrent edit\n"+ absent, = [profile for profile in read_shell_profiles(tmp_path, NAMES) if profile.path.name == ".profile"]+ guard = cleanup_shell_profile(absent, {})+ assert guard.before is guard.after is None+ absent.path.write_bytes(b"OPENAI_API_KEY=new\n")+ with pytest.raises(TakeoverStateError, match="changed"):+ guard.check()+++def test_all_eight_absent_paths_are_returned_in_order(tmp_path):+ profiles = read_shell_profiles(tmp_path, NAMES)+ assert tuple(profile.path.name for profile in profiles) == SHELL_PROFILE_NAMES+ assert len(profiles) == 8+ for profile in profiles:+ assert profile.path.is_absolute()+ assert profile.before is None+ assert profile.values == {}+ assert profile.assignments == profile.issues == ()+ edit = cleanup_shell_profile(profile, {})+ assert edit.before is edit.after is None+ edit.apply()+ assert not profile.path.exists()+++def test_none_home_and_ambient_credentials_are_hermetic(monkeypatch, tmp_path):+ monkeypatch.setattr(Path, "home", lambda: tmp_path)+ monkeypatch.setenv(KEY, "fixture-runtime-must-not-be-read")+ (tmp_path / ".profile").write_bytes(b"OPENAI_API_KEY=stored\n")+ profiles = read_shell_profiles(None, NAMES)+ assert profiles[0].values == {KEY: "stored"}+ assert all(profile.path.parent == tmp_path for profile in profiles)++[email protected]("kind", ["symlink", "dangling", "directory", "unreadable", "encoding"])+def test_unreadable_and_nonregular_files_have_sanitized_issues(monkeypatch, tmp_path, kind):+ target = tmp_path / ".profile"+ if kind == "symlink":+ outside = tmp_path / "outside"+ outside.write_bytes(b"OPENAI_API_KEY=must-not-read\n")+ target.symlink_to(outside)+ elif kind == "dangling":+ target.symlink_to(tmp_path / "absent")+ elif kind == "directory":+ target.mkdir()+ elif kind == "encoding":+ target.write_bytes(b"\xffOPENAI_API_KEY=must-not-read\n")+ else:+ target.write_bytes(b"OPENAI_API_KEY=must-not-read\n")+ real_read = Path.read_bytes+ def read(path):+ if path == target and kind in {"symlink", "dangling", "directory"}:+ pytest.fail("nonregular profile was followed")+ if path == target and kind == "unreadable":+ raise PermissionError("sensitive exception content")+ return real_read(path)+ monkeypatch.setattr(Path, "read_bytes", read)+ profile = read_shell_profiles(tmp_path, NAMES)[0]+ assert profile.before is None+ assert profile.assignments == ()+ assert profile.values == {}+ issue, = profile.issues+ assert issue.names == tuple(sorted(NAMES))+ assert issue.reason == "unreadable"+ assert issue.line is None+ assert "sensitive" not in repr(profile) + repr(issue)+ with pytest.raises(TakeoverStateError):+ cleanup_shell_profile(profile, {KEY: "must-not-read"})+++def test_snapshots_are_immutable_and_reprs_do_not_expose_contents(tmp_path):+ profile = _profile(tmp_path, "OPENAI_API_KEY=fixture-private-value\n")+ assignment, = profile.assignments+ issue = ShellIssue((KEY,), "dynamic_shell", 1)+ for value, field in [(profile, "before"), (assignment, "value"), (issue, "reason")]:+ with pytest.raises(FrozenInstanceError):+ setattr(value, field, "replacement")+ assert "fixture-private-value" not in repr(profile) + repr(assignment) + repr(issue)+ assert "fixture-private-value" not in repr(cleanup_shell_profile(profile, profile.values))+ detached = profile.values+ detached[KEY] = "replacement"+ assert profile.values == {KEY: "fixture-private-value"}+ assert isinstance(assignment, ShellAssignment)+++def test_parser_never_executes_profile_commands(monkeypatch, tmp_path):+ import os+ import subprocess++ def forbidden(*args, **kwargs):+ pytest.fail("the shell parser attempted to execute a process")+ monkeypatch.setattr(subprocess, "Popen", forbidden)+ monkeypatch.setattr(os, "system", forbidden)+ marker = tmp_path / "executed"+ profile = _profile(tmp_path, f"OPENAI_API_KEY=$(touch {marker})\n")+ assert profile.values == {}+ assert profile.issues[0].reason == "dynamic_shell"+ assert not marker.exists()diff --git a/tests/test_model_hub_migration_static_cleanup.py b/tests/test_model_hub_migration_static_cleanup.pynew file mode 100644index 000000000..df4460fc8--- /dev/null+++ b/tests/test_model_hub_migration_static_cleanup.py@@ -0,0 +1,246 @@+"""Canonical static credentials keep exact, fixture-owned cleanup evidence."""++from __future__ import annotations++import asyncio+import json+from dataclasses import replace++import pytest++from config.v2_config import ModelHubSourceConfig+from core.handlers.model_hub.migration import scan_native_configs+from core.handlers.model_hub.migration_files import plan_native_cleanup+from core.handlers.model_hub.migration_journal import NativeFileEdit, TakeoverStateError+from core.handlers.model_hub.service import ModelHubError+from tests.scenarios.model_hub.test_model_hub_migration_scenarios import _service, _write+from tests.test_model_hub_migration_transport import KEY as HTTP_KEY+from tests.test_model_hub_migration_transport import _real_http_adapter, _upstream+from tests.test_model_hub_persisted_inventory import home # noqa: F401 - isolated fixture+++KEY = "fixture-static-canonical"+RAW = f" \t{KEY} \t"+SHAPES = (+ *(f"claude-{location}-{kind}" for location in ("user", "backup", "project", "local")+ for kind in ("api", "bearer")),+ "codex-config", "codex-project", "codex-auth",+ "opencode-config", "opencode-project", "opencode-auth",+)+CONFIG_SHAPES = tuple(shape for shape in SHAPES if not shape.endswith("-auth"))+++def _seed(home, shape, raw=RAW):+ roots = ()+ if shape.startswith("claude"):+ _, location, kind = shape.split("-")+ root = home+ if location in ("project", "local"):+ root = home / "project"+ roots = (root,)+ filename = {+ "backup": ".avibe-oauth-settings-env-backup.json",+ "local": "settings.local.json",+ }.get(location, "settings.json")+ env = {+ "ANTHROPIC_AUTH_TOKEN" if kind == "bearer" else "ANTHROPIC_API_KEY": raw,+ "KEEP_FIXTURE": "preserved",+ }+ if kind == "bearer":+ env["ANTHROPIC_BASE_URL"] = "https://fixture-relay.example"+ path = root / ".claude" / filename+ _write(path, json.dumps({"env": env, "unrelated": "preserved"}))+ elif shape.startswith("codex"):+ if shape == "codex-auth":+ path = home / ".codex/auth.json"+ _write(path, json.dumps({"OPENAI_API_KEY": raw, "unrelated": "preserved"}))+ else:+ root = home / "project" if shape == "codex-project" else home+ roots = (root,) if root != home else ()+ path = root / ".codex/config.toml"+ _write(path, '[model_providers.fixture]\nname="preserved"\n'+ f"experimental_bearer_token={json.dumps(raw)}\n")+ elif shape == "opencode-auth":+ path = home / ".local/share/opencode/auth.json"+ _write(path, json.dumps({+ "openai": {"type": "api", "key": raw},+ }))+ else:+ root = home / "project" if shape == "opencode-project" else home / ".config/opencode"+ roots = (root,) if shape == "opencode-project" else ()+ path = root / "opencode.json"+ _write(path, json.dumps({+ "provider": {"openai": {"options": {"apiKey": raw, "timeout": 1234}}},+ "unrelated": "preserved",+ }))+ return path, roots+++def _items(service, roots):+ return scan_native_configs(+ service.store.load(), home=service.migration_home,+ project_roots=roots, mask_credential=lambda _: "masked",+ )+++def _selected(items):+ [item] = items+ assert item.proposed_action == "import"+ return item++[email protected]("shape", SHAPES)[email protected]("raw", [KEY, RAW, f"\u00a0{KEY}\u2003"])+def test_static_proof_and_custody_use_canonical_value(home, tmp_path, shape, raw):+ path, roots = _seed(home, shape, raw)+ service, store, adapter = _service(tmp_path, migration_home=home)+ service.migration_project_roots = lambda: roots+ proof_values = []+ transient = adapter.provision_transient_credential++ async def capture(vendor, secret, base_url, **kwargs):+ proof_values.append(secret)+ return await transient(vendor, secret, base_url, **kwargs)++ adapter.provision_transient_credential = capture+ item = _selected(_items(service, roots))+ assert item.secret == KEY+ assert raw not in json.dumps(item.to_payload()) and KEY not in repr(item)+ result = asyncio.run(service.migration_apply([item.id]))+ assert result["applied"] == 1+ [source] = store.config.sources+ assert proof_values == [adapter.keys[source.credential_ref][2]] == [KEY]+ assert adapter.auth_schemes[source.credential_ref] == item.auth_scheme+ assert KEY.encode() not in path.read_bytes()+ if shape == "opencode-auth":+ assert json.loads(path.read_bytes()) == {}+ else:+ assert b"preserved" in path.read_bytes()+ assert service.migration_journal.load() is None++[email protected]("shape", CONFIG_SHAPES)+def test_static_cleanup_retains_exact_raw_snapshot_for_reverse(home, tmp_path, shape):+ path, roots = _seed(home, shape)+ before = path.read_bytes()+ service, _, _ = _service(tmp_path, migration_home=home)+ [item] = _items(service, roots)+ edits = plan_native_cleanup([item], home=home, project_roots=roots)+ [owned] = [edit for edit in edits if edit.path == path]+ assert owned.before == before+ assert KEY.encode() not in owned.after+ for edit in edits:+ NativeFileEdit.from_payload(edit.to_payload()).apply()+ for edit in reversed(edits):+ NativeFileEdit.from_payload(edit.to_payload()).apply(reverse=True)+ assert path.read_bytes() == before++[email protected]("shape", CONFIG_SHAPES)+def test_static_canonical_value_reuses_existing_ref(home, tmp_path, shape):+ path, roots = _seed(home, shape)+ service, store, adapter = _service(tmp_path, migration_home=home)+ service.migration_project_roots = lambda: roots+ [item] = _items(service, roots)+ ref = asyncio.run(adapter.provision_credential(+ item.vendor, item.protocol, KEY, item.base_url, auth_scheme=item.auth_scheme,+ ))+ store.config.sources.append(ModelHubSourceConfig.from_payload({+ "id": "src_staticfixture", "kind": "api_key", "vendor": item.vendor,+ "display_name": "Existing fixture", "protocol": item.protocol,+ "base_url": item.base_url, "supply_channel": "hub", "billing": "metered",+ "state": {"status": "standby"}, "models": [], "credential_ref": ref,+ }))+ asyncio.run(service.migration_apply([item.id]))+ assert len(adapter.provisioned) == len(store.config.sources) == 1+ assert store.config.sources[0].credential_ref == ref+ assert adapter.observed and not adapter.transient_refs+ assert KEY.encode() not in path.read_bytes()++[email protected]("shape", CONFIG_SHAPES)+def test_other_canonical_credential_does_not_authorize_cleanup(home, tmp_path, shape):+ path, roots = _seed(home, shape)+ before = path.read_bytes()+ service, _, adapter = _service(tmp_path, migration_home=home)+ [item] = _items(service, roots)+ with pytest.raises(TakeoverStateError, match="another native credential"):+ plan_native_cleanup([replace(item, secret="fixture-other")], home=home, project_roots=roots)+ assert path.read_bytes() == before and not adapter.provisioned++[email protected]("shape", CONFIG_SHAPES)[email protected]("when", ["after-scan", "during-proof"])+def test_padding_only_change_never_bypasses_exact_consent(home, tmp_path, shape, when):+ path, roots = _seed(home, shape)+ service, store, adapter = _service(tmp_path, migration_home=home)+ service.migration_project_roots = lambda: roots+ [item] = _items(service, roots)+ changed = None++ def change():+ nonlocal changed+ _seed(home, shape, f" {KEY} ")+ changed = path.read_bytes()++ if when == "after-scan":+ change()+ else:+ observe = adapter.observe_source++ async def mutate(*args, **kwargs):+ change()+ return await observe(*args, **kwargs)++ adapter.observe_source = mutate+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([item.id]))+ assert changed is not None and path.read_bytes() == changed+ assert not store.config.sources+ assert not adapter.oauth_provisioned+ assert service.migration_journal.load() is None+ if when == "after-scan":+ assert not adapter.provisioned and not adapter.observed+ else:+ assert {entry[2] for entry in adapter.provisioned} <= set(adapter.revoked)++[email protected]("value", [" \t ", "fixture-other", ["fixture"], {"key": KEY}, 123])[email protected]("field", ["ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "CLAUDE_CODE_OAUTH_TOKEN"])+def test_unselected_settings_value_still_refuses_cleanup(home, tmp_path, value, field):+ _seed(home, "claude-user-api", KEY)+ # A real second native layer, not a new supported filename.+ project = home / "project"+ other = project / ".claude/settings.local.json"+ _write(other, json.dumps({"env": {field: value}}))+ before = other.read_bytes()+ service, _, adapter = _service(tmp_path, migration_home=home)+ selected = next(item for item in _items(service, (project,)) if item.secret == KEY)+ with pytest.raises(TakeoverStateError, match="another native credential"):+ plan_native_cleanup([selected], home=home, project_roots=(project,))+ assert other.read_bytes() == before+ assert not adapter.provisioned+++def test_padded_settings_bearer_uses_proven_header_after_custody(home, tmp_path):+ async def scenario():+ async with _upstream("bearer") as (origin, requests):+ path = home / ".claude/settings.json"+ _write(path, json.dumps({"env": {+ "ANTHROPIC_AUTH_TOKEN": f" \t{HTTP_KEY} \t",+ "ANTHROPIC_BASE_URL": origin, "KEEP_FIXTURE": "preserved",+ }}))+ service, store, _ = _service(tmp_path, migration_home=home)+ runtime = _real_http_adapter(service, tmp_path)+ [row] = service.migration_scan()["items"]+ await service.migration_apply([row["id"]])+ [source] = store.config.sources+ assert runtime.state_store.read_api_key(source.credential_ref) == HTTP_KEY+ assert await runtime.credential_auth_scheme(source.credential_ref) == "bearer"+ requests.clear()+ assert await runtime.discover_models("anthropic", "anthropic", origin, source.credential_ref)+ assert any(headers.get("authorization") == f"Bearer {HTTP_KEY}" for _, headers in requests)+ assert all("x-api-key" not in headers for _, headers in requests)+ assert json.loads(path.read_bytes()) == {"env": {"KEEP_FIXTURE": "preserved"}}++ asyncio.run(scenario())diff --git a/tests/test_model_hub_migration_transport.py b/tests/test_model_hub_migration_transport.pynew file mode 100644index 000000000..607396166--- /dev/null+++ b/tests/test_model_hub_migration_transport.py@@ -0,0 +1,419 @@+"""Migration admission preserves native auth semantics; fixtures never use accounts."""++from __future__ import annotations++import asyncio+import json+from contextlib import asynccontextmanager+from dataclasses import replace+from unittest.mock import AsyncMock++import aiohttp+import pytest+from aiohttp import web++from config.v2_config import ModelHubSourceConfig, normalize_model_hub_base_url+from core.handlers.model_hub import migration+from core.handlers.model_hub.service import ModelHubError+from tests.scenarios.model_hub.test_model_hub_migration_scenarios import (+ _isolate_native_home,+ _service,+ _write,+)+from tests.test_model_hub_migration_bearer import _adapter+from vibe.model_hub_runtime import api_key_vendors+++KEY = "fixture-static-key"+CUSTOM = "https://fixture-relay.example"+OFFICIAL = "https://api.anthropic.com"+BLOCKED = "settings.models.migration.blocked.transport"+ENTRIES = (+ "claude-settings", "claude-backup", "claude-project", "claude-project-local",+ "shell", "opencode-config", "opencode-auth", "opencode-env",+ "opencode-custom", "opencode-catalog", "opencode-shadow", "legacy",+)+++def native(monkeypatch, tmp_path):+ home = tmp_path / "native"+ _isolate_native_home(monkeypatch, home)+ monkeypatch.setenv("XDG_DATA_HOME", str(home / ".local/share"))+ monkeypatch.setattr(migration, "read_native_oauth", lambda *args, **kwargs: None)+ return home+++def _seed(home, entry, *, base_url=CUSTOM, secret=KEY, bearer=False):+ paths = []+ roots = ()+ legacy = None++ def write(path, payload):+ _write(path, payload if isinstance(payload, str) else json.dumps(payload))+ paths.append(path)++ if entry.startswith("claude"):+ root = home+ if "project" in entry:+ root = home / "project"+ roots = (root,)+ filename = "settings.local.json" if entry.endswith("local") else "settings.json"+ if entry == "claude-backup":+ filename = ".avibe-oauth-settings-env-backup.json"+ env = {"ANTHROPIC_AUTH_TOKEN" if bearer else "ANTHROPIC_API_KEY": secret}+ if base_url is not None:+ env["ANTHROPIC_BASE_URL"] = base_url+ write(root / ".claude" / filename, {"env": env, "unrelated": "保留"})+ elif entry == "shell":+ name = "ANTHROPIC_AUTH_TOKEN" if bearer else "ANTHROPIC_API_KEY"+ write(home / ".bashrc", (+ "# 保留终端设置\r\n"+ f"export {name}='{secret}'\r\n"+ + (f"export ANTHROPIC_BASE_URL='{base_url}'\r\n" if base_url is not None else "")+ ))+ elif entry == "legacy":+ legacy = {"claude": {"api_key": secret, "base_url": base_url}}+ else:+ provider = "custom" if entry in {"opencode-custom", "opencode-catalog"} else "anthropic"+ options = {"apiKey": secret}+ if base_url is not None:+ options["baseURL"] = base_url+ definition = {"options": options}+ if entry == "opencode-env":+ write(home / ".profile", f"export FIXTURE_API_KEY='{secret}'\n")+ options["apiKey"] = "{env:FIXTURE_API_KEY}"+ if entry == "opencode-custom":+ definition["npm"] = "@ai-sdk/anthropic"+ if entry == "opencode-catalog":+ options.pop("baseURL", None)+ write(home / ".cache/opencode/models.json", {+ provider: {"npm": "@ai-sdk/anthropic", "api": base_url},+ })+ if entry in {"opencode-auth", "opencode-shadow"}:+ if entry == "opencode-auth":+ options.pop("apiKey")+ write(home / ".local/share/opencode/auth.json", {+ provider: {"type": "api", "key": secret if entry == "opencode-auth" else secret + "-shadow"},+ })+ write(home / ".config/opencode/opencode.json", {"provider": {provider: definition}})+ return paths, roots, legacy+++def _scan(service, roots=(), legacy=None):+ service.migration_project_roots = lambda: roots+ return migration.scan_native_configs(+ service.store.load(), home=service.migration_home,+ mask_credential=lambda value: "masked-fixture",+ validate_base_url=normalize_model_hub_base_url,+ project_roots=roots, legacy_auth=legacy,+ )++[email protected]("entry", ENTRIES)[email protected]("base_url,secret", [+ (CUSTOM, KEY), (OFFICIAL, "fixture-sk-ant-oat-static"),+])+def test_all_anthropic_native_producers_refuse_unpreservable_transport(+ tmp_path, native, entry, base_url, secret,+):+ paths, roots, legacy = _seed(native, entry, base_url=base_url, secret=secret)+ before = {path: path.read_bytes() for path in paths}+ service, store, adapter = _service(tmp_path, migration_home=native)+ if legacy:+ store.native_auth_snapshot = lambda backends: legacy+ rows = _scan(service, roots, legacy)+ assert rows+ assert all(row.proposed_action == "reauth" and not row.selected for row in rows)+ assert all(row.notes_key == BLOCKED for row in rows)+ assert secret not in json.dumps([row.to_payload() for row in rows])+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([row.id for row in rows]))+ assert {path: path.read_bytes() for path in paths} == before+ assert not store.config.sources+ assert not adapter.provisioned and not adapter.transient_refs+ assert not adapter.observed+ assert service.migration_journal.load() is None++[email protected]("entry", ENTRIES)+def test_official_api_key_still_migrates_from_every_native_producer(tmp_path, native, entry):+ _, roots, legacy = _seed(native, entry, base_url=OFFICIAL)+ service, store, adapter = _service(tmp_path, migration_home=native)+ adapter.observed_protocols["custom"] = "anthropic"+ if legacy:+ store.native_auth_snapshot = lambda backends: legacy+ rows = _scan(service, roots, legacy)+ assert rows and all(row.proposed_action == "import" for row in rows)+ result = asyncio.run(service.migration_apply([row.id for row in rows]))+ assert result["applied"] == len(rows)+ assert all(source.protocol == "anthropic" for source in store.config.sources)+ assert all(scheme is None for scheme in adapter.auth_schemes.values())++[email protected]("url,allowed", [+ (None, True), (OFFICIAL, True), ("https://API.ANTHROPIC.COM:443/v1", True),+ ("https://api.anthropic.com:/v1", True),+ ("https://api.anthropic.com:0443", False),+ ("https://api.anthropic.com:8443", False), ("http://api.anthropic.com", False),+ ("https://api.anthropic.com.", False),+ ("https://api.anthropic.com.fixture.example", False),+ ("https://fixture.example/api.anthropic.com", False),+ ("https://[email protected]", False),+ ("https://%61pi.anthropic.com", False), ("https://api.anthropic.com:wrong", False),+ ("https://api.anthropic.com\\fixture", False), ("https://api.anthropic.com\n", False),+ ("https://api.anthropic.com", False),+])+def test_migration_gate_uses_same_validated_cpa_origin_as_explicit_bearer(url, allowed):+ validate = api_key_vendors.validate_migration_api_key_transport+ if allowed:+ assert validate("anthropic", "anthropic", url, KEY, None) is None+ else:+ with pytest.raises(ValueError, match="^unsupported native API key transport$"):+ validate("anthropic", "anthropic", url, KEY, None)+ # The existing public/credential-store default stays permissive.+ assert api_key_vendors.validate_api_key_auth_scheme("anthropic", "anthropic", url, KEY, None) is None++[email protected]("secret", ["sk-ant-oat-fixture", "fixture-sk-ant-oat-inside"])+def test_migration_oauth_heuristic_is_never_reinterpreted_as_static(secret):+ validate = api_key_vendors.validate_migration_api_key_transport+ for url, scheme in ((OFFICIAL, None), (CUSTOM, None), (CUSTOM, "bearer")):+ with pytest.raises(ValueError, match="^unsupported native API key transport$"):+ validate("anthropic", "anthropic", url, secret, scheme)+ assert validate("openai", "openai_chat", CUSTOM, secret, None) is None++[email protected]("vendor,url,scheme,allowed", [+ ("anthropic", CUSTOM, "bearer", True),+ ("anthropic", "https://api.anthropic.com:0443", "bearer", True),+ ("anthropic", OFFICIAL, "bearer", False),+ ("anthropic", CUSTOM, "unknown", False),+ ("custom", CUSTOM, "bearer", False),+ ("minimax", None, None, False),+ ("custom", None, None, False),+ ("custom", OFFICIAL, None, True),+])+def test_migration_validation_reuses_explicit_scheme_and_catalog_target(vendor, url, scheme, allowed):+ validate = api_key_vendors.validate_migration_api_key_transport+ if allowed:+ assert validate(vendor, "anthropic", url, KEY, scheme) is None+ else:+ with pytest.raises(ValueError, match="^unsupported native API key transport$"):+ validate(vendor, "anthropic", url, KEY, scheme)+++def _source(ref, *, vendor="anthropic", protocol="anthropic", base_url=CUSTOM):+ return ModelHubSourceConfig.from_payload({+ "id": "src_transportfixture", "kind": "api_key", "vendor": vendor,+ "display_name": "Transport fixture", "protocol": protocol,+ "base_url": base_url, "supply_channel": "hub", "billing": "metered",+ "state": {"status": "standby"}, "models": [], "credential_ref": ref,+ })++[email protected]("reuse", [False, True])+def test_apply_entry_revalidates_before_proof_or_source_reuse(tmp_path, native, reuse):+ _seed(native, "claude-settings", base_url=OFFICIAL)+ service, store, adapter = _service(tmp_path, migration_home=native)+ [item] = _scan(service)+ item = replace(item, base_url=CUSTOM)+ if reuse:+ store.config.sources.append(_source("cred_existingfixture"))+ adapter.matches_api_key_credential = AsyncMock(return_value=True)+ service._require_proven_source_payload = AsyncMock()+ service._require_proven_observation = AsyncMock()+ with pytest.raises(migration.MigrationConflictError):+ asyncio.run(migration._prepare_takeover(+ service, store.config, [item], mask_credential=lambda value: "masked-fixture",+ validate_base_url=normalize_model_hub_base_url,+ ))+ adapter.matches_api_key_credential.assert_not_awaited()+ service._require_proven_source_payload.assert_not_awaited()+ service._require_proven_observation.assert_not_awaited()+ assert not adapter.provisioned+ assert service.migration_journal.load() is None++[email protected]("reuse", [False, True])[email protected]("native_protocol,observed", [+ ("anthropic", "openai_chat"), ("openai_chat", "anthropic"),+])+def test_observation_cannot_change_native_auth_semantics(+ tmp_path, native, reuse, native_protocol, observed,+):+ _seed(native, "opencode-custom", base_url=OFFICIAL)+ path = native / ".config/opencode/opencode.json"+ if native_protocol == "openai_chat":+ _write(path, json.dumps({"provider": {"custom": {+ "npm": "@ai-sdk/openai-compatible",+ "options": {"apiKey": KEY, "baseURL": OFFICIAL},+ }}}))+ before = path.read_bytes()+ service, store, adapter = _service(tmp_path, migration_home=native)+ adapter.observed_protocols["custom"] = observed+ if reuse:+ ref = asyncio.run(adapter.provision_credential("custom", native_protocol, KEY, OFFICIAL))+ store.config.sources.append(_source(ref, vendor="custom", protocol=native_protocol, base_url=OFFICIAL))+ adapter.provisioned.clear()+ [row] = service.migration_scan()["items"]+ assert row["proposed_action"] == "import"+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([row["id"]]))+ assert path.read_bytes() == before+ assert not adapter.provisioned+ assert service.migration_journal.load() is None+++def test_observation_may_change_openai_dialect_without_changing_bearer_auth(tmp_path, native):+ path = native / ".config/opencode/opencode.json"+ _write(path, json.dumps({"provider": {"custom": {+ "npm": "@ai-sdk/openai-compatible",+ "options": {"apiKey": KEY, "baseURL": CUSTOM},+ }}}))+ service, store, adapter = _service(tmp_path, migration_home=native)+ adapter.observed_protocols["custom"] = "openai_responses"+ [row] = service.migration_scan()["items"]+ assert asyncio.run(service.migration_apply([row["id"]]))["applied"] == 1+ assert store.config.sources[0].protocol == "openai_responses"+++@asynccontextmanager+async def _upstream(scheme):+ requests = []++ async def consume(request):+ headers = {key.lower(): value for key, value in request.headers.items()}+ requests.append((request.path, headers))+ valid = (+ headers.get("x-api-key") == KEY and "authorization" not in headers+ if scheme == "x-api-key" else+ headers.get("authorization") == f"Bearer {KEY}" and "x-api-key" not in headers+ )+ if not valid:+ return web.json_response({"type": "error", "error": {+ "type": "authentication_error", "message": "invalid credential",+ }}, status=401)+ if request.method == "GET":+ return web.json_response({"data": [{"id": "claude-fixture"}]})+ body = await request.json()+ if "model" not in body:+ return web.json_response({"type": "error", "error": {+ "type": "invalid_request_error", "message": "model: Field required",+ }}, status=400)+ return web.json_response({"type": "message", "content": [{"type": "text", "text": "fixture-ok"}]})++ app = web.Application()+ app.router.add_post("/v1/messages", consume)+ app.router.add_get("/v1/models", consume)+ runner = web.AppRunner(app)+ await runner.setup()+ try:+ site = web.TCPSite(runner, "127.0.0.1", 0)+ await site.start()+ origin = f"http://127.0.0.1:{site._server.sockets[0].getsockname()[1]}"+ yield origin, requests+ finally:+ await runner.cleanup()+++def _real_http_adapter(service, tmp_path):+ runtime = _adapter(tmp_path)+ for method in (+ "provision_credential", "provision_transient_credential", "matches_api_key_credential",+ "credential_auth_scheme", "observe_source", "discover_models", "revoke_credential",+ "sync_sources",+ ):+ setattr(service.adapter, method, getattr(runtime, method))+ return runtime++[email protected]("entry", ["claude-settings", "shell", "opencode-custom"])[email protected]("reuse", [False, True])+def test_strict_custom_x_api_key_never_becomes_successful_migration(+ tmp_path, native, entry, reuse,+):+ async def scenario():+ async with _upstream("x-api-key") as (origin, requests):+ paths, _, _ = _seed(native, entry, base_url=origin)+ before = {path: path.read_bytes() for path in paths}+ service, store, _ = _service(tmp_path, migration_home=native)+ runtime = _real_http_adapter(service, tmp_path)+ if reuse:+ vendor = "custom" if entry == "opencode-custom" else "anthropic"+ ref = await runtime.provision_credential(vendor, "anthropic", KEY, origin)+ store.config.sources.append(_source(ref, vendor=vendor, base_url=origin))+ config_before = store.config.to_payload()+ credentials_before = set((runtime.state_store.root / "credentials").glob("*.json"))+ # Actual consuming HTTP witness: this upstream accepts the native+ # header, and rejects the pinned CPA custom-origin Bearer header.+ async with aiohttp.ClientSession() as client:+ for headers, status in (({"x-api-key": KEY}, 200), ({"Authorization": f"Bearer {KEY}"}, 401)):+ async with client.post(origin + "/v1/messages", headers=headers, json={"model": "claude-fixture"}) as response:+ assert response.status == status+ requests.clear()+ # Demonstrate the old observation path can prove this key using+ # x-api-key. That proof must not license destructive migration.+ proof = await service._require_proven_source_payload({+ "vendor": "anthropic", "key": KEY, "base_url": origin,+ })+ assert proof.authenticated and proof.protocol == "anthropic"+ assert any(headers.get("x-api-key") == KEY for _, headers in requests)+ requests.clear()+ rows = service.migration_scan()["items"]+ with pytest.raises(ModelHubError):+ await service.migration_apply([row["id"] for row in rows])+ assert all(row["notes_key"] == BLOCKED for row in rows)+ assert requests == []+ assert {path: path.read_bytes() for path in paths} == before+ assert store.config.to_payload() == config_before+ assert set((runtime.state_store.root / "credentials").glob("*.json")) == credentials_before+ assert service.migration_journal.load() is None++ asyncio.run(scenario())+++def test_real_observation_cannot_promote_custom_key_to_anthropic_transport(tmp_path, native):+ async def scenario():+ async with _upstream("x-api-key") as (origin, requests):+ path = native / ".config/opencode/opencode.json"+ _write(path, json.dumps({"provider": {"custom": {+ "npm": "@ai-sdk/openai-compatible",+ "options": {"apiKey": KEY, "baseURL": origin},+ }}}))+ before = path.read_bytes()+ service, store, _ = _service(tmp_path, migration_home=native)+ runtime = _real_http_adapter(service, tmp_path)+ [row] = service.migration_scan()["items"]+ assert row["proposed_action"] == "import"+ with pytest.raises(ModelHubError):+ await service.migration_apply([row["id"]])+ assert any(headers.get("x-api-key") == KEY for _, headers in requests)+ assert path.read_bytes() == before+ assert not store.config.sources+ assert not list((runtime.state_store.root / "credentials").glob("*.json"))+ assert service.migration_journal.load() is None++ asyncio.run(scenario())++[email protected]("entry", ["claude-settings", "shell"])+def test_custom_bearer_keeps_actual_proof_discovery_and_cleanup(tmp_path, native, entry):+ async def scenario():+ async with _upstream("bearer") as (origin, requests):+ paths, _, _ = _seed(native, entry, base_url=origin, bearer=True)+ service, store, _ = _service(tmp_path, migration_home=native)+ runtime = _real_http_adapter(service, tmp_path)+ rows = service.migration_scan()["items"]+ result = await service.migration_apply([row["id"] for row in rows])+ assert result["applied"] == 1+ [source] = store.config.sources+ assert await runtime.credential_auth_scheme(source.credential_ref) == "bearer"+ assert [model.id for model in source.models] == ["claude-fixture"]+ assert all("x-api-key" not in headers for _, headers in requests)+ assert any(headers.get("authorization") == f"Bearer {KEY}" for _, headers in requests)+ assert any("authorization" not in headers for _, headers in requests)+ assert all(KEY.encode() not in path.read_bytes() for path in paths)+ assert service.migration_scan()["items"] == []++ asyncio.run(scenario())diff --git a/tests/test_model_hub_native_takeover.py b/tests/test_model_hub_native_takeover.pyindex be4e7c640..12342d8c5 100644--- a/tests/test_model_hub_native_takeover.py+++ b/tests/test_model_hub_native_takeover.py@@ -175,9 +175,10 @@ def test_unavailable_runtime_preserves_native_oauth_before_custody(monkeypatch,assert not service.migration_blocked_backends[email protected]("legacy_receipt", [False, True])@pytest.mark.parametrize("backend", ["opencode", "codex", "claude"])def test_completed_receipt_recleans_resurrected_credentials_without_reimport(- monkeypatch, tmp_path, backend,+ monkeypatch, tmp_path, backend, legacy_receipt,):home = tmp_path / "native"_isolate_native_home(monkeypatch, home)@@ -197,12 +198,27 @@ def test_completed_receipt_recleans_resurrected_credentials_without_reimport(service, store, adapter = _service(tmp_path, migration_home=home)ids = [row["id"] for row in service.migration_scan()["items"]]assert asyncio.run(service.migration_apply(ids))["applied"] == 1+ if legacy_receipt:+ # Published receipts predate snapshot-bound consent. Their item IDs+ # encode the same grant/target inventory, without the new display data.+ receipt = service.migration_journal.completed()+ for row, identity in zip(receipt["items"], receipt.pop("inventory_ids"), strict=True):+ row["id"] = identity+ row.pop("source_paths", None)+ row.pop("required_backends", None)+ NativeTakeoverJournal(service.migration_journal.path.with_name("last-completed.json")).save(receipt)current = store.config.to_payload()provisions = (len(adapter.provisioned), len(adapter.oauth_provisioned))# This is an external native writer, not an Avibe-owned auth operation.native.write_bytes(original)- assert [row["id"] for row in service.migration_scan()["items"]] == ids+ rescanned_ids = [row["id"] for row in service.migration_scan()["items"]]+ if rescanned_ids != ids:+ # Cleanup may have changed other configuration layers. Fresh consent+ # is required, but it must not provision the restored old OAuth again.+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply(ids))+ ids = rescanned_idsassert asyncio.run(service.migration_apply(ids))["applied"] == 1assert service.migration_scan()["items"] == []assert store.config.to_payload() == current@@ -228,6 +244,84 @@ def test_completed_receipt_does_not_authorize_new_unconsented_native_key(monkeypassert len(adapter.provisioned) == 1[email protected]("legacy_receipt", [False, True])[email protected]("selection", ["mixed", "subset"])+def test_receipt_retains_known_oauth_with_a_different_fresh_selection(+ monkeypatch, tmp_path, legacy_receipt, selection,+):+ home = tmp_path / "native"+ _isolate_native_home(monkeypatch, home)+ _write_codex_oauth(home)+ native = home / ".codex/auth.json"+ original = native.read_bytes()+ other = home / ".config/opencode/opencode.json"+ other_payload = '{"provider":{"openai":{"options":{"apiKey":"fixture-new-openai"}}}}'+ if selection == "subset":+ _write(other, other_payload)+ service, store, adapter = _service(tmp_path, migration_home=home)+ ids = [row["id"] for row in service.migration_scan()["items"]]+ asyncio.run(service.migration_apply(ids))+ receipt = service.migration_journal.completed()+ if legacy_receipt:+ for row, identity in zip(receipt["items"], receipt.pop("inventory_ids"), strict=True):+ row["id"] = identity+ NativeTakeoverJournal(service.migration_journal.path.with_name("last-completed.json")).save(receipt)+ old_refs = {source.id: source.credential_ref for source in store.config.sources}+ native.write_bytes(original)+ if selection == "mixed":+ _write(other, other_payload)+ rows = service.migration_scan()["items"]+ assert {row["backend"] for row in rows} == (+ {"codex", "opencode"} if selection == "mixed" else {"codex"}+ )+ result = asyncio.run(service.migration_apply([row["id"] for row in rows]))+ assert result["applied"] == len(rows)+ assert len(adapter.oauth_provisioned) == 1+ assert len(adapter.provisioned) == 1+ assert len(store.config.sources) == 2+ assert old_refs.items() <= {+ source.id: source.credential_ref for source in store.config.sources+ }.items()+ assert service.migration_scan()["items"] == []+ assert service.migration_journal.load() is None+ assert adapter.revoked == []++[email protected]("legacy_receipt", [False, True])[email protected]("change", ["metadata", "tokens"])+def test_receipt_cannot_prove_a_second_native_oauth_authorization(+ monkeypatch, tmp_path, legacy_receipt, change,+):+ home = tmp_path / "native"+ _isolate_native_home(monkeypatch, home)+ _write_codex_oauth(home)+ native = home / ".codex/auth.json"+ original = json.loads(native.read_text())+ service, store, adapter = _service(tmp_path, migration_home=home)+ ids = [row["id"] for row in service.migration_scan()["items"]]+ asyncio.run(service.migration_apply(ids))+ receipt = service.migration_journal.completed()+ if legacy_receipt:+ for row, identity in zip(receipt["items"], receipt.pop("inventory_ids"), strict=True):+ row["id"] = identity+ NativeTakeoverJournal(service.migration_journal.path.with_name("last-completed.json")).save(receipt)+ if change == "metadata":+ original["fixture_unrelated_metadata"] = True+ else:+ original["tokens"]["access_token"] = "fixture-new-native-access"+ original["tokens"]["refresh_token"] = "fixture-new-native-refresh"+ _write(native, json.dumps(original))+ before = native.read_bytes(), store.config.to_payload()+ rows = service.migration_scan()["items"]+ with pytest.raises(ModelHubError) as failure:+ asyncio.run(service.migration_apply([row["id"] for row in rows]))+ assert failure.value.code == "migration_reauthorization_required"+ assert (native.read_bytes(), store.config.to_payload()) == before+ assert len(adapter.oauth_provisioned) == 1+ assert service.migration_journal.load() is None+ assert adapter.revoked == []++def test_restart_recovers_exposed_handoff_without_native_tokens(monkeypatch, tmp_path):home = tmp_path / "native"_write_claude_oauth(home)diff --git a/tests/test_model_hub_persisted_inventory.py b/tests/test_model_hub_persisted_inventory.pynew file mode 100644index 000000000..246619295--- /dev/null+++ b/tests/test_model_hub_persisted_inventory.py@@ -0,0 +1,520 @@+"""Persisted migration discovery and consent never depend on runtime secrets."""++import asyncio+import json+import os+import subprocess+from dataclasses import replace+from pathlib import Path++import pytest++from config.v2_config import ModelHubConfig+from core.handlers.model_hub.migration import scan_native_configs+from core.handlers.model_hub.migration_files import (+ NativeReferenceError,+ native_config_references,+ plan_native_cleanup,+)+from core.handlers.model_hub.migration_journal import NativeFileEdit, TakeoverStateError+from core.handlers.model_hub.service import ModelHubError+from tests.scenarios.model_hub.test_model_hub_migration_scenarios import (+ _isolate_native_home,+ _service,+ _write,+)+from tests.test_native_oauth_store import FakeKeychain+from vibe import native_oauth_store+++def home(tmp_path, monkeypatch):+ result = tmp_path / "native"+ result.mkdir()+ _isolate_native_home(monkeypatch, result)+ monkeypatch.setenv("XDG_DATA_HOME", str(result / ".local/share"))+ monkeypatch.setenv("CLAUDE_SECURESTORAGE_CONFIG_DIR", str(result / ".claude"))+ monkeypatch.delenv("OPENCODE_CONFIG", raising=False)+ monkeypatch.delenv("OPENCODE_CONFIG_DIR", raising=False)+ monkeypatch.setattr(native_oauth_store, "_KEYCHAIN_STORE", FakeKeychain())++ def forbidden(*args, **kwargs):+ raise AssertionError("real native credential boundary reached")++ for name in ("metadata", "read", "write", "delete"):+ monkeypatch.setattr(native_oauth_store._SecurityKeychainStore, name, forbidden)+ monkeypatch.setattr(subprocess, "run", forbidden)+ monkeypatch.setattr(subprocess.Popen, "__init__", forbidden)+ monkeypatch.setattr(os, "system", forbidden)+ monkeypatch.setattr(asyncio, "create_subprocess_exec", forbidden)+ monkeypatch.setattr(asyncio, "create_subprocess_shell", forbidden)+ return result+++def scan(home):+ return scan_native_configs(ModelHubConfig(), home=home, mask_credential=lambda _: "masked")+++def config(home, backend, providers):+ if backend == "codex":+ path = home / ".codex/config.toml"+ _write(path, providers)+ else:+ path = home / ".config/opencode/opencode.json"+ _write(path, json.dumps({"provider": providers}))+ return path+++def test_runtime_authentication_is_never_read(home, monkeypatch):+ names = (+ "ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "CLAUDE_CODE_OAUTH_TOKEN",+ "ANTHROPIC_BASE_URL", "OPENAI_API_KEY", "CODEX_API_KEY", "OPENAI_BASE_URL",+ "OPENROUTER_API_KEY", "OPENCODE_CONFIG_CONTENT", "CUSTOM_FIXTURE_KEY",+ )+ for name in names:+ monkeypatch.setenv(name, "fixture-inherited")+ original = os.environ.get++ def guarded(name, *args):+ assert name not in names, f"runtime credential read: {name}"+ return original(name, *args)++ monkeypatch.setattr(os.environ, "get", guarded)+ assert scan(None) == []+ path = config(home, "codex", '[model_providers.fixture]\nenv_key = "CUSTOM_FIXTURE_KEY"\n')+ [row] = scan(None)+ assert row.notes_key.endswith(".reference")+ assert row.source_paths == (str(path),)++[email protected]("backend", ["codex", "opencode"])+def test_custom_reference_uses_only_saved_literal_and_cleanup(home, monkeypatch, backend):+ shell = home / ".zshrc"+ shell.write_bytes(b"# keep \xe4\xb8\xad\xe6\x96\x87\r\nexport CUSTOM_FIXTURE_KEY='fixture-saved'\r\nalias ll='ls -l'\r\n")+ monkeypatch.setenv("CUSTOM_FIXTURE_KEY", "fixture-inherited")+ if backend == "codex":+ path = config(home, backend, '[model_providers.fixture]\nenv_key = "CUSTOM_FIXTURE_KEY"\n')+ else:+ path = config(home, backend, {"openai": {"options": {"apiKey": "{env:CUSTOM_FIXTURE_KEY}"}}})+ [row] = scan(home)+ assert row.secret == "fixture-saved"+ assert set(row.source_paths) == {str(path), str(shell)}+ assert row.to_payload()["source_paths"] == list(row.source_paths)+ assert "fixture-saved" not in json.dumps(row.to_payload())+ assert "fixture-saved" not in repr(row)+ before = shell.read_bytes()+ edits = plan_native_cleanup([row], home=home)+ for edit in edits:+ edit.apply()+ assert shell.read_bytes() == b"# keep \xe4\xb8\xad\xe6\x96\x87\r\nalias ll='ls -l'\r\n"+ assert os.environ["CUSTOM_FIXTURE_KEY"] == "fixture-inherited"+ for edit in reversed(edits):+ NativeFileEdit.from_payload(edit.to_payload()).apply(reverse=True)+ assert shell.read_bytes() == before++[email protected]("contents,reason", [+ ("export CUSTOM_FIXTURE_KEY=$(echo forbidden)\n", "dynamic_shell"),+ ("export CUSTOM_FIXTURE_KEY=one\nexport CUSTOM_FIXTURE_KEY=two\n", "ambiguous_shell"),+ ("", "reference"),+])+def test_persisted_reference_refusals_are_specific(home, contents, reason):+ shell = home / ".profile"+ shell.write_text(contents)+ path = config(home, "opencode", {"openai": {"options": {"apiKey": "{env:CUSTOM_FIXTURE_KEY}"}}})+ [row] = scan(home)+ assert row.notes_key.endswith(f".{reason}")+ assert str(path) in row.source_paths+ if contents:+ assert str(shell) in row.source_paths+ assert not row.selected+ assert shell.read_text() == contents+++def test_auth_file_fallback_ignores_runtime_reference(home, monkeypatch):+ monkeypatch.setenv("CUSTOM_FIXTURE_KEY", "runtime")+ config(home, "opencode", {"openai": {"options": {"apiKey": "{env:CUSTOM_FIXTURE_KEY}"}}})+ auth = home / ".local/share/opencode/auth.json"+ _write(auth, json.dumps({"openai": {"type": "api", "key": "fixture-auth"}}))+ [row] = scan(home)+ assert row.secret == "fixture-auth"+ assert str(auth) in row.source_paths+ for edit in plan_native_cleanup([row], home=home):+ edit.apply()+ assert scan(home) == []+++def test_conflicting_shell_profiles_never_guess_key_target(home):+ (home / ".profile").write_text("export OPENAI_API_KEY=fixture-one\nexport OPENAI_BASE_URL=https://one.example/v1\n")+ (home / ".zshrc").write_text("export OPENAI_API_KEY=fixture-two\nexport OPENAI_BASE_URL=https://two.example/v1\n")+ [row] = scan(home)+ assert row.notes_key.endswith(".ambiguous_shell")+ assert set(row.source_paths) == {str(home / ".profile"), str(home / ".zshrc")}+++def test_standalone_literals_preserve_unselected_backend_and_journal_guards(home):+ shell = home / ".profile"+ shell.write_text(+ "# keep\nexport ANTHROPIC_API_KEY=fixture-claude\n"+ "export ANTHROPIC_BASE_URL=https://anthropic.example\n"+ "export OPENAI_API_KEY=fixture-codex\n"+ )+ rows = scan(home)+ selected = [row for row in rows if row.backend == "claude"]+ edits = plan_native_cleanup(selected, home=home)+ absent = next(edit for edit in edits if edit.path == home / ".bashrc")+ assert absent.before is None and absent.after is None+ before = shell.read_bytes()+ for edit in edits:+ NativeFileEdit.from_payload(edit.to_payload()).apply()+ assert shell.read_text() == "# keep\nexport OPENAI_API_KEY=fixture-codex\n"+ for edit in reversed(edits):+ edit.apply(reverse=True)+ assert shell.read_bytes() == before+ (home / ".bashrc").write_text("export OPENAI_API_KEY=concurrent\n")+ with pytest.raises(TakeoverStateError):+ absent.check()+++def shared(home):+ shell = home / ".zshrc"+ shell.write_text("export OPENAI_API_KEY=fixture-shared\n")+ path = config(home, "opencode", {"openai": {"options": {"apiKey": "{env:OPENAI_API_KEY}"}}})+ return shell, path+++def test_shared_consent_is_public_and_partial_selection_stops_before_provision(home, tmp_path):+ shell, path = shared(home)+ service, store, adapter = _service(tmp_path, migration_home=home)+ rows = service.migration_scan()["items"]+ assert {row["backend"] for row in rows} == {"codex", "opencode"}+ assert all(row["required_backends"] == ["codex", "opencode"] for row in rows)+ before = shell.read_bytes(), path.read_bytes()+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([rows[0]["id"]]))+ assert not adapter.provisioned and not adapter.transient_refs+ assert (shell.read_bytes(), path.read_bytes()) == before+ assert asyncio.run(service.migration_apply([row["id"] for row in rows]))["applied"] == 2+ assert len(adapter.provisioned) == 1+ assert len(store.config.sources) == 1+ assert shell.read_bytes() == b""+++def test_new_shared_consumer_during_provision_preserves_original_sources(home, tmp_path):+ shell = home / ".profile"+ shell.write_text("export OPENAI_API_KEY=fixture-selected\n")+ service, store, adapter = _service(tmp_path, migration_home=home)+ [row] = service.migration_scan()["items"]+ provision = adapter.provision_credential+ before = shell.read_bytes()++ async def race(*args, **kwargs):+ ref = await provision(*args, **kwargs)+ config(home, "opencode", {"openai": {"options": {"apiKey": "{env:OPENAI_API_KEY}"}}})+ return ref++ adapter.provision_credential = race+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([row["id"]]))+ assert shell.read_bytes() == before+ assert not store.config.sources+ assert len(adapter.revoked) == len(adapter.provisioned) == 1+++def test_consumer_added_after_scan_invalidates_old_consent(home, tmp_path):+ (home / ".profile").write_text("export OPENAI_API_KEY=fixture-selected\n")+ service, _, adapter = _service(tmp_path, migration_home=home)+ [row] = service.migration_scan()["items"]+ config(home, "opencode", {"openai": {"options": {"apiKey": "{env:OPENAI_API_KEY}"}}})+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([row["id"]]))+ assert not adapter.provisioned++[email protected]("name,reason", [+ ("apiKeyHelper", "helper"), ("CLAUDE_CODE_OAUTH_TOKEN", "token"),+])+def test_claude_unsupported_credential_names_remain_specific(home, name, reason):+ path = home / ".claude/settings.json"+ payload = {name: "fixture"} if name == "apiKeyHelper" else {"env": {name: "fixture"}}+ _write(path, json.dumps(payload))+ [row] = scan(home)+ assert row.notes_key.endswith(f".{reason}")+ assert row.source_paths == (str(path),)++[email protected]("location", ["settings", "shell"])[email protected]("target,secret,allowed", [+ ("https://relay.example/anthropic", "fixture-bearer", True),+ ("https://api.anthropic.com", "fixture-bearer", False),+ ("https://relay.example/anthropic", "sk-ant-oat-fixture", False),+])+def test_anthropic_bearer_semantics_are_explicit_and_private(home, location, target, secret, allowed):+ if location == "shell":+ path = home / ".profile"+ path.write_text(f"export ANTHROPIC_AUTH_TOKEN={secret}\nexport ANTHROPIC_BASE_URL={target}\n")+ else:+ path = home / ".claude/settings.json"+ _write(path, json.dumps({"env": {"ANTHROPIC_AUTH_TOKEN": secret, "ANTHROPIC_BASE_URL": target}}))+ [row] = scan(home)+ assert row.selected is allowed+ assert row.source_paths == (str(path),)+ if allowed:+ assert row.auth_scheme == "bearer"+ assert row.secret == secret+ else:+ assert row.notes_key.endswith(".token")+ assert secret not in json.dumps(row.to_payload())+ assert secret not in repr(row)+ assert "auth_scheme" not in row.to_payload()+++def test_claude_key_and_bearer_are_both_retained(home):+ path = home / ".claude/settings.json"+ _write(path, json.dumps({"env": {+ "ANTHROPIC_API_KEY": "fixture-api-key", "ANTHROPIC_AUTH_TOKEN": "fixture-bearer",+ "ANTHROPIC_BASE_URL": "https://relay.example/anthropic",+ }}))+ rows = scan(home)+ assert {(row.secret, row.auth_scheme) for row in rows} == {+ ("fixture-api-key", None), ("fixture-bearer", "bearer"),+ }+ assert len({row.id for row in rows}) == 2+++def test_bearer_scheme_reaches_proof_and_custody_and_prevents_wrong_reuse(home, tmp_path):+ path = home / ".claude/settings.json"+ _write(path, json.dumps({"env": {+ "ANTHROPIC_AUTH_TOKEN": "fixture-same-bytes",+ "ANTHROPIC_BASE_URL": "https://relay.example/anthropic",+ }}))+ service, store, adapter = _service(tmp_path, migration_home=home)+ provision = adapter.provision_credential+ transient = adapter.provision_transient_credential+ matches = adapter.matches_api_key_credential+ schemes = {}+ proof_schemes = []++ async def provision_with_scheme(*args, **kwargs):+ scheme = kwargs.pop("auth_scheme", None)+ ref = await provision(*args, **kwargs)+ schemes[ref] = scheme+ return ref++ async def transient_with_scheme(*args, **kwargs):+ proof_schemes.append(kwargs.pop("auth_scheme", None))+ return await transient(*args, **kwargs)++ async def matches_with_scheme(ref, *args, auth_scheme=None):+ return schemes.get(ref) == auth_scheme and await matches(ref, *args)++ adapter.provision_credential = provision_with_scheme+ adapter.provision_transient_credential = transient_with_scheme+ adapter.matches_api_key_credential = matches_with_scheme+ # A pre-existing legacy Source has the same bytes/target but a different+ # header scheme. It must not be reused for the new saved Bearer token.+ from config.v2_config import ModelHubSourceConfig+ legacy_ref = asyncio.run(adapter.provision_credential(+ "anthropic", "anthropic", "fixture-same-bytes", "https://relay.example/anthropic",+ ))+ store.config.sources.append(ModelHubSourceConfig.from_payload({+ "id": "src_legacyfixture", "kind": "api_key", "vendor": "anthropic",+ "display_name": "Legacy fixture", "protocol": "anthropic",+ "base_url": "https://relay.example/anthropic",+ "supply_channel": "hub", "billing": "metered",+ "state": {"status": "standby"}, "models": [], "credential_ref": legacy_ref,+ }))+ rows = service.migration_scan()["items"]+ ids = [row["id"] for row in rows]+ assert asyncio.run(service.migration_apply(ids))["applied"] == 1+ assert proof_schemes == ["bearer"]+ assert set(schemes.values()) == {None, "bearer"}+ assert len(store.config.sources) == len(adapter.provisioned) == 2+ assert "auth_scheme" not in json.dumps(store.config.to_payload())+ assert asyncio.run(service.migration_apply(ids))["applied"] == 1+ assert len(adapter.provisioned) == 2+++def test_deduplication_unions_all_source_paths(home):+ shell = home / ".profile"+ shell.write_text("export CUSTOM_FIXTURE_KEY=fixture-one\n")+ first = config(home, "opencode", {"openai": {"options": {"apiKey": "{env:CUSTOM_FIXTURE_KEY}"}}})+ second = first.with_suffix(".jsonc")+ _write(second, first.read_text())+ [row] = scan(home)+ assert set(row.source_paths) == {str(first), str(second), str(shell)}+++def test_empty_literal_does_not_create_a_credential(home):+ (home / ".profile").write_text("export OPENAI_API_KEY=''\n")+ assert scan(home) == []+++def test_empty_assignment_conflicts_with_a_different_profile_value(home):+ (home / ".profile").write_text("export OPENAI_API_KEY=''\n")+ (home / ".zshrc").write_text("export OPENAI_API_KEY=fixture-key\n")+ [row] = scan(home)+ assert row.notes_key.endswith(".ambiguous_shell")+++def test_blocked_header_consumer_is_part_of_shared_consent(home):+ (home / ".profile").write_text("export OPENAI_API_KEY=fixture-shared\n")+ config(home, "opencode", {"openai": {"options": {+ "headers": {"Authorization": "Bearer {env:OPENAI_API_KEY}"},+ }}})+ rows = scan(home)+ assert {row.backend for row in rows} == {"codex", "opencode"}+ assert all(row.required_backends == ("codex", "opencode") for row in rows)+ [blocked] = [row for row in rows if row.backend == "opencode"]+ assert blocked.notes_key.endswith(".headers")+++def test_malformed_unselected_config_cannot_hide_a_shell_consumer(home):+ (home / ".profile").write_text("export OPENAI_API_KEY=fixture-shared\n")+ path = home / ".config/opencode/opencode.json"+ _write(path, '{"provider":')+ rows = scan(home)+ [codex] = [row for row in rows if row.backend == "codex"]+ assert codex.notes_key.endswith(".config")+ assert not codex.selected+ assert str(path) in codex.source_paths+++def test_absent_profile_created_during_native_key_proof_blocks_cleanup(home, tmp_path):+ path = home / ".claude/settings.json"+ _write(path, json.dumps({"env": {"ANTHROPIC_API_KEY": "fixture-selected"}}))+ service, store, adapter = _service(tmp_path, migration_home=home)+ [row] = service.migration_scan()["items"]+ provision = adapter.provision_credential+ before = path.read_bytes()++ async def race(*args, **kwargs):+ ref = await provision(*args, **kwargs)+ (home / ".profile").write_text("export ANTHROPIC_API_KEY=fixture-concurrent\n")+ return ref++ adapter.provision_credential = race+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([row["id"]]))+ assert path.read_bytes() == before+ assert not store.config.sources+ assert len(adapter.revoked) == len(adapter.provisioned) == 1++[email protected]("selected_opencode_vendor", [False, True])+def test_live_base_reference_survives_actual_planned_cleanup(home, tmp_path, selected_opencode_vendor):+ shell = home / ".profile"+ key_line = b"export OPENAI_API_KEY=fixture-codex\r\n"+ base_line = b"export OPENAI_BASE_URL=https://fixture.example/v1\r\n"+ shell.write_bytes(key_line + base_line)+ providers = {"openai": {"options": {"baseURL": "{env:OPENAI_BASE_URL}"}}}+ if selected_opencode_vendor:+ providers["zhipuai"] = {+ "npm": "@ai-sdk/openai-compatible",+ "options": {"apiKey": "fixture-zhipu", "baseURL": "https://fixture-zhipu.example/v1"},+ }+ path = config(home, "opencode", providers)+ original = path.read_bytes()+ service, _, adapter = _service(tmp_path, migration_home=home)+ rows = service.migration_scan()["items"]+ assert len(rows) == 1 + selected_opencode_vendor+ assert all(row["proposed_action"] == "import" for row in rows)+ assert all("shell_auth_variables" not in row for row in rows)+ assert asyncio.run(service.migration_apply([row["id"] for row in rows]))["applied"] == len(rows)+ assert shell.read_bytes() == base_line+ after = json.loads(path.read_bytes())+ assert after["provider"]["openai"] == providers["openai"]+ if selected_opencode_vendor:+ assert "options" not in after["provider"]["zhipuai"]+ else:+ assert path.read_bytes() == original+ assert len(adapter.provisioned) == len(rows)++[email protected]("root_reference", [False, True])+def test_live_auth_reference_without_a_row_blocks_before_any_write(home, tmp_path, root_reference):+ shell = home / ".profile"+ shell.write_text("export OPENAI_API_KEY=fixture-codex\n")+ if root_reference:+ path = config(home, "opencode", {})+ _write(path, json.dumps({"instructions": ["{env:OPENAI_API_KEY}"]}))+ else:+ path = config(home, "opencode", {"openai": {"options": {"baseURL": "{env:OPENAI_API_KEY}"}}})+ original = shell.read_bytes(), path.read_bytes()+ service, store, adapter = _service(tmp_path, migration_home=home)+ [row] = service.migration_scan()["items"]+ assert row["backend"] == "codex"+ assert row["notes_key"].endswith(".reference")+ assert str(path) in row["source_paths"]+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([row["id"]]))+ assert (shell.read_bytes(), path.read_bytes()) == original+ assert not adapter.provisioned and not adapter.transient_refs+ assert not store.config.sources++[email protected]("same_name_for_base", [False, True])+def test_same_backend_surviving_reference_uses_auth_role_over_base_role(home, same_name_for_base):+ shell = home / ".profile"+ shell.write_text("export CUSTOM_FIXTURE_KEY=https://fixture.example/v1\n")+ selected_options = {"apiKey": "{env:CUSTOM_FIXTURE_KEY}"}+ if same_name_for_base:+ selected_options["baseURL"] = "{env:CUSTOM_FIXTURE_KEY}"+ path = config(home, "opencode", {+ "openai": {"options": selected_options},+ "orphan": {"options": {"baseURL": "{env:CUSTOM_FIXTURE_KEY}"}},+ })+ [row] = scan(home)+ assert row.shell_auth_variables == ("CUSTOM_FIXTURE_KEY",)+ assert row.notes_key.endswith(".reference")+ original = shell.read_bytes(), path.read_bytes()+ # Planner enforcement is independent of scan presentation and backend+ # selection: selecting OpenCode does not remove the orphan provider.+ with pytest.raises(NativeReferenceError) as error:+ plan_native_cleanup([replace(row, proposed_action="import", selected=True)], home=home)+ assert error.value.references == {"CUSTOM_FIXTURE_KEY": (str(path),)}+ assert (shell.read_bytes(), path.read_bytes()) == original+++def test_only_base_variable_with_live_reference_is_retained_for_same_backend(home):+ shell = home / ".profile"+ key_line = "export CUSTOM_FIXTURE_KEY=fixture-key\n"+ base_line = "export CUSTOM_FIXTURE_BASE=https://fixture.example/v1\n"+ shell.write_text(key_line + base_line)+ path = config(home, "opencode", {+ "openai": {"options": {+ "apiKey": "{env:CUSTOM_FIXTURE_KEY}", "baseURL": "{env:CUSTOM_FIXTURE_BASE}",+ }},+ "orphan": {"options": {"baseURL": "{env:CUSTOM_FIXTURE_BASE}"}},+ })+ [row] = scan(home)+ assert row.proposed_action == "import"+ assert row.shell_auth_variables == ("CUSTOM_FIXTURE_KEY",)+ edits = plan_native_cleanup([row], home=home)+ config_edit = next(edit for edit in edits if edit.path == path)+ references = native_config_references("opencode", json.loads(config_edit.after))+ assert references == {"CUSTOM_FIXTURE_BASE"}+ for edit in edits:+ edit.apply()+ assert shell.read_text() == base_line+ assert "CUSTOM_FIXTURE_KEY" not in references+++def test_cross_project_reference_with_no_row_is_also_preserved(home, tmp_path):+ shell = home / ".profile"+ shell.write_text(+ "export OPENAI_API_KEY=fixture-key\n"+ "export OPENAI_BASE_URL=https://fixture.example/v1\n"+ )+ project = tmp_path / "project"+ path = project / "opencode.jsonc"+ _write(path, '{"provider":{"openai":{"options":{"baseURL":"{env:OPENAI_BASE_URL}"}}}}')+ original = path.read_bytes()+ rows = scan_native_configs(+ ModelHubConfig(), home=home, project_roots=(project,), mask_credential=lambda _: "masked",+ )+ assert len(rows) == 1+ for edit in plan_native_cleanup(rows, home=home, project_roots=(project,)):+ edit.apply()+ assert shell.read_text() == "export OPENAI_BASE_URL=https://fixture.example/v1\n"+ assert path.read_bytes() == originaldiff --git a/tests/test_model_hub_persisted_migration.py b/tests/test_model_hub_persisted_migration.pynew file mode 100644index 000000000..9cc452835--- /dev/null+++ b/tests/test_model_hub_persisted_migration.py@@ -0,0 +1,248 @@+"""Persisted migration boundaries; never inspect real stores or launch a CLI."""++from __future__ import annotations++import asyncio+import json+import os+from pathlib import Path++import pytest++from core.handlers.model_hub import migration+from core.handlers.model_hub.service import ModelHubError+from tests.scenarios.model_hub.test_model_hub_migration_scenarios import (+ _isolate_native_home,+ _service,+)++[email protected](autouse=True)+def isolated_native_boundary(monkeypatch, tmp_path):+ home = tmp_path / "native"+ _isolate_native_home(monkeypatch, home)+ monkeypatch.setenv("XDG_DATA_HOME", str(home / ".local/share"))+ monkeypatch.setattr(Path, "home", lambda: home)+ monkeypatch.setattr(migration, "read_native_oauth", lambda *args, **kwargs: None)+ return home+++def write(path: Path, payload: object) -> None:+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text(+ payload if isinstance(payload, str) else json.dumps(payload),+ encoding="utf-8",+ )+++def test_runtime_auth_alone_is_not_a_saved_configuration(monkeypatch, tmp_path):+ service, store, adapter = _service(tmp_path, migration_home=None)+ service.migration_home = None+ for name in ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "OPENAI_API_KEY"):+ monkeypatch.setenv(name, "fixture-runtime-not-an-import")+ monkeypatch.setenv("ANTHROPIC_BASE_URL", "http://127.0.0.1:12345")+ assert all(agent.mode == "hub" for agent in store.load().agents.values())+ assert service.migration_scan()["items"] == []+ assert adapter.provisioned == []+++def test_mh_mig_005_runtime_auth_cannot_disable_saved_opencode_keys(+ monkeypatch, tmp_path, isolated_native_boundary,+):+ """MH-MIG-005: runtime auth cannot supply or block saved file migration."""+ home = isolated_native_boundary+ path = home / ".config/opencode/opencode.json"+ write(path, {"provider": {+ "anthropic": {"options": {"apiKey": "fixture-saved-anthropic"}},+ "openai": {"options": {"apiKey": "fixture-saved-openai"}},+ }})+ service, _, adapter = _service(tmp_path, migration_home=None)+ service.migration_home = None+ monkeypatch.setenv("ANTHROPIC_API_KEY", "fixture-runtime-anthropic")+ monkeypatch.setenv("OPENAI_API_KEY", "fixture-runtime-openai")+ rows = service.migration_scan()["items"]+ assert len(rows) == 2+ assert all(row["proposed_action"] == "import" and row["selected"] for row in rows)+ assert all(row["source_paths"] == [str(path)] for row in rows)+ result = asyncio.run(service.migration_apply([row["id"] for row in rows]))+ assert result["applied"] == 2+ assert len(adapter.provisioned) == 2+ assert "fixture-runtime" not in json.dumps(adapter.keys)+ assert all(not provider.get("options") for provider in json.loads(path.read_text())["provider"].values())+ assert os.environ["ANTHROPIC_API_KEY"] == "fixture-runtime-anthropic"+ assert os.environ["OPENAI_API_KEY"] == "fixture-runtime-openai"+++def test_shell_saved_key_takeover_keeps_unrelated_bytes_and_runtime(+ monkeypatch, tmp_path, isolated_native_boundary,+):+ home = isolated_native_boundary+ profile = home / ".bashrc"+ retained = "# 我的终端设置\r\nexport EDITOR='vim'\r\n"+ write(profile, retained + (+ "export ANTHROPIC_AUTH_TOKEN='fixture-shell-key'\r\n"+ "export ANTHROPIC_BASE_URL='https://fixture.example'\r\n"+ ))+ service, store, adapter = _service(tmp_path, migration_home=home)+ monkeypatch.setenv("ANTHROPIC_API_KEY", "fixture-runtime-preserved")+ rows = service.migration_scan()["items"]+ assert len(rows) == 1 and rows[0]["proposed_action"] == "import"+ assert rows[0]["source_paths"] == [str(profile)]+ assert "fixture-shell-key" not in json.dumps(rows)+ ids = [row["id"] for row in rows]+ asyncio.run(service.migration_apply(ids))+ assert profile.read_bytes() == retained.encode()+ assert len(store.load().sources) == 1+ assert store.load().sources[0].base_url == "https://fixture.example"+ assert list(adapter.keys.values())[0][2] == "fixture-shell-key"+ assert service.migration_scan()["items"] == []+ asyncio.run(service.migration_apply(ids))+ assert len(adapter.provisioned) == 1+ assert os.environ["ANTHROPIC_API_KEY"] == "fixture-runtime-preserved"+++def test_new_shell_layer_during_provision_cannot_escape_consent(+ monkeypatch, tmp_path, isolated_native_boundary,+):+ home = isolated_native_boundary+ profile = home / ".bashrc"+ write(profile, "export ANTHROPIC_API_KEY='fixture-original-key'\n")+ original = profile.read_bytes()+ service, store, adapter = _service(tmp_path, migration_home=home)+ ids = [row["id"] for row in service.migration_scan()["items"]]+ provision = adapter.provision_credential++ async def new_layer(*args, **kwargs):+ ref = await provision(*args, **kwargs)+ write(home / ".profile", "export ANTHROPIC_API_KEY='fixture-new-key'\n")+ return ref++ monkeypatch.setattr(adapter, "provision_credential", new_layer)+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply(ids))+ assert profile.read_bytes() == original+ assert "fixture-new-key" in (home / ".profile").read_text()+ assert not store.load().sources+ assert adapter.revoked+++def test_shared_shell_key_requires_both_backend_consents(+ tmp_path, isolated_native_boundary,+):+ home = isolated_native_boundary+ profile = home / ".profile"+ write(profile, "export OPENAI_API_KEY='fixture-shared-key'\n")+ config_path = home / ".config/opencode/opencode.json"+ write(config_path, {"provider": {"openai": {"options": {"apiKey": "{env:OPENAI_API_KEY}"}}}})+ service, store, adapter = _service(tmp_path, migration_home=home)+ rows = service.migration_scan()["items"]+ assert {row["backend"] for row in rows} == {"codex", "opencode"}+ assert all(set(row["required_backends"]) == {"codex", "opencode"} for row in rows)+ before = profile.read_bytes(), config_path.read_bytes()+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([row["id"] for row in rows if row["backend"] == "opencode"]))+ assert adapter.provisioned == []+ assert (profile.read_bytes(), config_path.read_bytes()) == before+ asyncio.run(service.migration_apply([row["id"] for row in rows]))+ assert profile.read_bytes() == b""+ assert "apiKey" not in json.dumps(json.loads(config_path.read_text()))+ assert len(store.load().sources) == 1+ source = store.load().sources[0]+ assert all(source.id in store.load().agents[backend].sources.order for backend in ("codex", "opencode"))++[email protected]("has_grant", [False, True])[email protected]("boundary", ["install", "start"])+def test_external_noop_guard_change_never_owns_rollback_bytes(+ monkeypatch, tmp_path, isolated_native_boundary, has_grant, boundary,+):+ from core.handlers.model_hub.adapter import EngineEnsureResult, EngineHealth, EngineStatus+ from tests.test_native_oauth_store import FakeKeychain+ from vibe import native_oauth_store++ home = isolated_native_boundary+ settings = home / ".claude/settings.json"+ if has_grant:+ write(settings, {"env": {"ANTHROPIC_API_KEY": "fixture-before-exposure"}})+ else:+ keychain = FakeKeychain()+ monkeypatch.setenv("USER", "fixture-user")+ keychain.items[("Claude Code-credentials", "fixture-user")] = (+ '{"mcpOAuth":{"keep":"fixture"}}', "fixture-original",+ )+ monkeypatch.setattr(native_oauth_store, "_KEYCHAIN_STORE", keychain)+ monkeypatch.setattr(migration, "read_native_oauth", native_oauth_store.read_native_oauth)+ profile = home / ".zshrc"+ write(profile, "# original preference\n")+ service, store, adapter = _service(tmp_path, migration_home=home)+ store.config.agents["claude"].mode = "direct"+ before_settings = settings.read_bytes() if settings.exists() else None+ ids = [row["id"] for row in service.migration_scan()["items"]]+ assert len(ids) == 1+ status = EngineStatus(EngineHealth.OK, "fixture", True, "127.0.0.1", 32199, None)++ async def install(**kwargs):+ if boundary == "install":+ profile.write_text("# external preference during install\n")+ return EngineEnsureResult(status, False)++ async def start():+ if boundary == "start":+ profile.write_text("# external preference during start\n")+ return status++ adapter.ensure_installed = install+ adapter.start = start+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply(ids))+ assert profile.read_text() == f"# external preference during {boundary}\n"+ assert service.migration_journal.completed() is None+ if has_grant and boundary == "start":+ assert service.migration_journal.load()["phase"] == "exposed"+ assert len(store.config.sources) == 1+ assert settings.read_bytes() != before_settings+ assert "claude" in service.migration_blocked_backends+ else:+ assert service.migration_journal.load() is None+ assert not store.config.sources+ assert store.config.agents["claude"].mode == "direct"+ assert not service.migration_blocked_backends+ assert (settings.read_bytes() if settings.exists() else None) == before_settings++[email protected]("changed_identity", ["new-grant", "new-target", "replaced-hub-ref"])+def test_receipt_replay_requires_same_grant_target_and_current_hub_ref(+ monkeypatch, tmp_path, isolated_native_boundary, changed_identity,+):+ home = isolated_native_boundary+ path = home / ".claude/settings.json"+ payload = {"env": {+ "ANTHROPIC_AUTH_TOKEN": "fixture-original",+ "ANTHROPIC_BASE_URL": "https://fixture-original.example",+ }}+ write(path, payload)+ service, store, adapter = _service(tmp_path, migration_home=home)+ [original] = service.migration_scan()["items"]+ asyncio.run(service.migration_apply([original["id"]]))+ original_ref = store.config.sources[0].credential_ref+ if changed_identity == "new-grant":+ payload["env"]["ANTHROPIC_AUTH_TOKEN"] = "fixture-new"+ elif changed_identity == "new-target":+ payload["env"]["ANTHROPIC_BASE_URL"] = "https://fixture-changed.example"+ else:+ store.config.sources[0].credential_ref = "cred_changedfixture"+ write(path, payload)+ [current] = service.migration_scan()["items"]+ if changed_identity == "replaced-hub-ref":+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([current["id"]]))+ assert len(adapter.provisioned) == 1+ assert json.loads(path.read_text()) == payload+ else:+ assert current["id"] != original["id"]+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([original["id"]]))+ asyncio.run(service.migration_apply([current["id"]]))+ assert len(adapter.provisioned) == 2+ assert store.config.sources[0].credential_ref == original_ref+ assert len(store.config.sources) == 2diff --git a/tests/test_model_hub_replacement_transport.py b/tests/test_model_hub_replacement_transport.pynew file mode 100644index 000000000..c65c9c6e0--- /dev/null+++ b/tests/test_model_hub_replacement_transport.py@@ -0,0 +1,544 @@+"""Replacement recovers transport, never infers absence from unreadable custody."""++import asyncio+import json+import os+import re+from pathlib import Path+from unittest.mock import AsyncMock++import pytest++from config.v2_config import ModelHubSourceConfig+from core.handlers.model_hub.revocations import CredentialRevocationJournal+from core.handlers.model_hub.service import ModelHubError+from tests.test_model_hub_migration_bearer import (+ CUSTOM,+ KEY,+ _adapter,+ _binding,+ _relay,+ _service_with_custody,+)+from vibe.model_hub_runtime.config import write_engine_config+from vibe.model_hub_runtime import state as state_module+from vibe.model_hub_runtime.state import EngineStateError, EngineStateStore, RuntimeSecrets+++def _document(store, ref):+ return store.root / "credentials" / f"{ref}.json"+++def _damage(store, ref, damage):+ path = _document(store, ref)+ payload = json.loads(path.read_text())+ if damage == "missing":+ path.unlink()+ elif damage == "bad_json":+ path.write_text("{")+ elif damage == "non_object":+ path.write_text("[]")+ elif damage == "invalid_utf8":+ path.write_bytes(b"\xff")+ elif damage == "missing_value":+ payload.pop("value")+ path.write_text(json.dumps(payload))+ elif damage == "empty_value":+ payload["value"] = ""+ path.write_text(json.dumps(payload))+ elif damage == "invalid_value":+ payload["value"] = []+ path.write_text(json.dumps(payload))+ elif damage == "wrongkind":+ payload.update(kind="oauth", auth_name="unrelated.json")+ path.write_text(json.dumps(payload))+ elif damage == "unknown_kind":+ payload["kind"] = "future-kind"+ path.write_text(json.dumps(payload))+ elif damage == "unknown_scheme":+ payload["auth_scheme"] = "future-scheme"+ path.write_text(json.dumps(payload))+ elif damage == "missing_scheme":+ payload.pop("auth_scheme")+ path.write_text(json.dumps(payload))+ elif damage == "null_scheme":+ payload["auth_scheme"] = None+ path.write_text(json.dumps(payload))+ elif damage == "permissions":+ path.chmod(0o644)+ elif damage == "directory_permissions":+ path.parent.chmod(0o755)+ elif damage == "symlink":+ target = path.with_name("fixture-target.json")+ path.rename(target)+ path.symlink_to(target)+ else:+ raise AssertionError(damage)+++async def _source(tmp_path, origin, scheme, *, plain_bearer=False):+ service, config, runtime = _service_with_custody(tmp_path)+ ref = await runtime.provision_credential(+ "anthropic", "anthropic", KEY, origin, auth_scheme=scheme,+ )+ if plain_bearer:+ old_ref = ref+ ref = "cred_" + "b" * 32+ _document(runtime.state_store, old_ref).rename(_document(runtime.state_store, ref))+ source = ModelHubSourceConfig.from_payload({+ "id": "src_bearerfixture", "kind": "api_key", "vendor": "anthropic",+ "display_name": "Fixture", "protocol": "anthropic", "base_url": origin,+ "supply_channel": "hub", "billing": "metered",+ "state": {"status": "standby"}, "models": [], "credential_ref": ref,+ })+ config.config.sources.append(source)+ runtime.state_store.sync_sources([_binding(ref, origin)])+ return service, config, runtime, source, ref+++def test_new_bearer_namespace_is_immutable_and_reserved_before_secret(tmp_path):+ store = EngineStateStore(tmp_path / "engine")+ reserved = []++ def on_reserved(ref):+ assert re.fullmatch(r"cred_auth_bearer_[0-9a-f]{32}", ref)+ assert len(store._credential_namespace_paths(ref)) == 4+ for path in store._credential_namespace_paths(ref):+ assert json.loads(path.read_text()) == {"kind": "reservation", "credential_ref": ref}+ reserved.append(ref)++ ref = store.store_api_key(+ KEY, vendor="anthropic", protocol="anthropic", base_url=CUSTOM,+ auth_scheme="bearer", on_reserved=on_reserved,+ )+ assert reserved == [ref]+ legacy = store.store_api_key(KEY)+ assert re.fullmatch(r"cred_[0-9a-f]{32}", legacy)++[email protected]("scheme", [None, "bearer"])[email protected]("damage", [+ "missing", "bad_json", "non_object", "invalid_utf8",+ "missing_value", "empty_value", "invalid_value",+])+def test_replacement_recovers_without_old_secret_and_retires_namespace(tmp_path, scheme, damage):+ async def scenario():+ async with _relay(scheme=scheme or "legacy") as (origin, requests, accepted):+ service, config, runtime, source, ref = await _source(tmp_path, origin, scheme)+ _damage(runtime.state_store, ref, damage)+ accepted.add("fixture-replacement")+ result = await service.replace_credential(source.id, {"key": "fixture-replacement"})+ new_ref = result["source"]["credential_ref"]+ assert new_ref != ref+ assert await runtime.credential_auth_scheme(new_ref) == scheme+ assert config.config.sources[0].credential_ref == new_ref+ assert "auth_scheme" not in result["source"]+ assert requests and requests[0][2].get("Authorization" if scheme else "x-api-key") == (+ "Bearer fixture-replacement" if scheme else "fixture-replacement"+ )+ assert not service.revocations.list()+ assert all(not path.exists() for path in runtime.state_store._credential_namespace_paths(ref))++ asyncio.run(scenario())++[email protected]("damage", [+ "wrongkind", "unknown_kind", "unknown_scheme", "missing_scheme", "null_scheme",+ "permissions", "directory_permissions", "symlink",+])+def test_replacement_refuses_uncertain_identity_before_new_secret_or_request(tmp_path, damage):+ async def scenario():+ async with _relay() as (origin, requests, _):+ service, config, runtime, source, ref = await _source(tmp_path, origin, "bearer")+ _damage(runtime.state_store, ref, damage)+ before = config.config.to_payload()+ service.adapter.provision_credential = AsyncMock(side_effect=AssertionError("must not provision"))+ with pytest.raises(ModelHubError):+ await service.replace_credential(source.id, {"key": "fixture-replacement"})+ assert config.config.to_payload() == before+ assert not requests+ service.adapter.provision_credential.assert_not_awaited()+ assert not service.revocations.list()+ if damage == "directory_permissions":+ assert _document(runtime.state_store, ref).parent.stat().st_mode & 0o777 == 0o755++ asyncio.run(scenario())++[email protected]("ref", [+ "cred_auth_future_" + "a" * 32, "cred_auth_bearer_abcdef",+ "cred_auth_bearer_" + "A" * 32, "cred_auth_", "../cred_fixture",+])+def test_reserved_unknown_or_malformed_refs_never_recover_as_legacy(tmp_path, ref):+ async def scenario():+ adapter = _adapter(tmp_path)+ with pytest.raises(EngineStateError):+ await adapter.credential_auth_scheme(ref)+ with pytest.raises(EngineStateError):+ await adapter.revoke_api_key_credential(ref)+ assert not adapter.state_store.root.exists()++ asyncio.run(scenario())++[email protected]("operation", ["replace", "retarget"])+def test_intact_unpublished_plain_bearer_upgrades_only_in_fresh_ref(tmp_path, operation):+ async def scenario():+ async with _relay() as (origin, requests, accepted):+ service, _, runtime, source, ref = await _source(+ tmp_path, origin, "bearer", plain_bearer=True,+ )+ assert await runtime.credential_auth_scheme(ref) == "bearer"+ old = _document(runtime.state_store, ref).read_bytes()+ if operation == "replace":+ accepted.add("fixture-replacement")+ result = await service.replace_credential(source.id, {"key": "fixture-replacement"})+ new_ref = result["source"]["credential_ref"]+ assert requests+ else:+ new_ref = await runtime.retarget_api_key_credential(ref, "anthropic", "anthropic", origin)+ assert _document(runtime.state_store, ref).read_bytes() == old+ assert re.fullmatch(r"cred_auth_bearer_[0-9a-f]{32}", new_ref)++ asyncio.run(scenario())++[email protected]("consumer", [+ "metadata", "read_key", "reuse", "retarget", "sync", "config", "discover",+ "observe", "refresh", "oauth_inventory", "ready",+])+def test_real_consumers_cannot_downgrade_tagged_metadata(tmp_path, consumer):+ async def scenario():+ async with _relay() as (origin, requests, _):+ runtime = _adapter(tmp_path)+ ref = await runtime.provision_credential(+ "anthropic", "anthropic", KEY, origin, auth_scheme="bearer",+ )+ store = runtime.state_store+ binding = _binding(ref, origin)+ record = store.sync_sources([binding])[0]+ _damage(store, ref, "wrongkind" if consumer == "oauth_inventory" else "missing_scheme")+ if consumer == "ready":+ assert not store.has_current_source_credential(+ ref, source_id=binding.source_id, kind="api_key", vendor="anthropic",+ protocol="anthropic", base_url=origin,+ )+ else:+ with pytest.raises(EngineStateError):+ if consumer == "metadata":+ store.credential_metadata(ref)+ elif consumer == "read_key":+ store.read_api_key(ref)+ elif consumer == "reuse":+ await runtime.matches_api_key_credential(ref, "anthropic", "anthropic", KEY, origin)+ elif consumer == "retarget":+ await runtime.retarget_api_key_credential(ref, "anthropic", "anthropic", origin)+ elif consumer == "sync":+ store.sync_sources([binding])+ elif consumer == "config":+ write_engine_config(+ tmp_path / "config.yaml", host="127.0.0.1", port=23456,+ auth_dir=store.auth_dir, state_store=store,+ runtime_secrets=RuntimeSecrets("fixture-management", "fixture-gateway"),+ sources=[record],+ )+ elif consumer == "discover":+ await runtime.discover_models("anthropic", "anthropic", origin, ref)+ elif consumer == "observe":+ await runtime.observe_source("anthropic", origin, ref, ("anthropic",))+ elif consumer == "refresh":+ await runtime.credential_supports_refresh(ref)+ elif consumer == "oauth_inventory":+ store.oauth_credential_ref("unrelated.json")+ assert not requests++ asyncio.run(scenario())++[email protected]("scheme", [None, "bearer"])+def test_recovered_scheme_does_not_bypass_new_key_authentication(tmp_path, scheme):+ async def scenario():+ async with _relay(scheme=scheme or "legacy") as (origin, requests, _):+ service, config, runtime, source, ref = await _source(tmp_path, origin, scheme)+ _damage(runtime.state_store, ref, "bad_json")+ with pytest.raises(ModelHubError):+ await service.replace_credential(source.id, {"key": "fixture-wrong-key"})+ assert requests+ assert config.config.sources[0].credential_ref == ref+ assert _document(runtime.state_store, ref).read_text() == "{"+ assert list((runtime.state_store.root / "credentials").glob("cred_*.json")) == [+ _document(runtime.state_store, ref)+ ]+ assert not service.revocations.list()++ asyncio.run(scenario())++[email protected]("damage", ["missing", "bad_json", "missing_value"])+def test_typed_retirement_only_removes_unbound_exact_namespace(tmp_path, damage):+ async def scenario():+ runtime = _adapter(tmp_path)+ store = runtime.state_store+ ref = await runtime.provision_credential("anthropic", "anthropic", KEY, CUSTOM, auth_scheme="bearer")+ store.sync_sources([_binding(ref)])+ _damage(store, ref, damage)+ with pytest.raises(EngineStateError, match="still bound"):+ await runtime.revoke_api_key_credential(ref)+ store.replace_sources([])+ store.audit_auth_permissions()+ auth = store.auth_dir / "unrelated.json"+ store._secure_write_json(auth, {"fixture": "must survive"})+ other = await runtime.provision_credential("anthropic", "anthropic", KEY, CUSTOM)+ paths = store._credential_namespace_paths(ref)+ for path in paths[1:]:+ store._secure_write_json(path, {"fixture": "owned interrupted temp", "auth_name": auth.name})+ runtime.supervisor.client_if_running = lambda: pytest.fail("API cleanup must not inspect OAuth engine")+ await runtime.revoke_api_key_credential(ref)+ await runtime.revoke_api_key_credential(ref)+ assert all(not path.exists() for path in paths)+ assert auth.exists()+ assert store.read_api_key(other) == KEY++ asyncio.run(scenario())++[email protected]("damage", ["wrongkind", "unknown_kind", "unknown_scheme", "missing_scheme", "permissions", "symlink"])+def test_replay_failure_is_not_false_absence_and_retains_durable_intent(tmp_path, damage):+ async def scenario():+ service, _, runtime, _, ref = await _source(tmp_path, CUSTOM, "bearer")+ runtime.state_store.replace_sources([])+ service.store.config.sources.clear()+ _damage(runtime.state_store, ref, damage)+ service.revocations.add("src_bearerfixture", ref, operation="revoke_api_key_credential")+ service.revocations = CredentialRevocationJournal(service.revocations.path)+ service._engine_synced = False+ await service._ensure_engine_synced()+ assert len(service.revocations.list()) == 1+ assert _document(runtime.state_store, ref).exists()++ asyncio.run(scenario())+++def test_postcommit_retirement_failure_replays_typed_intent_after_restart(tmp_path):+ async def scenario():+ async with _relay() as (origin, _, accepted):+ service, config, runtime, source, ref = await _source(tmp_path, origin, "bearer")+ _damage(runtime.state_store, ref, "bad_json")+ accepted.add("fixture-replacement")+ service.adapter.revoke_api_key_credential = AsyncMock(side_effect=OSError("fixture I/O"))+ answer = await service.replace_credential(source.id, {"key": "fixture-replacement"})+ assert config.config.sources[0].credential_ref == answer["source"]["credential_ref"]+ pending = CredentialRevocationJournal(service.revocations.path).list()+ assert [(entry.credential_ref, entry.operation) for entry in pending] == [+ (ref, "revoke_api_key_credential"),+ ]+ restarted, restored, reopened_runtime = _service_with_custody(tmp_path)+ restored.save(type(config.config).from_payload(config.config.to_payload()))+ restarted.adapter.revoke_credential = AsyncMock(side_effect=AssertionError("must use typed operation"))+ await restarted._ensure_engine_synced()+ assert not restarted.revocations.list()+ assert not _document(reopened_runtime.state_store, ref).exists()+ assert await reopened_runtime.credential_auth_scheme(answer["source"]["credential_ref"]) == "bearer"++ asyncio.run(scenario())++[email protected]("error", [PermissionError("fixture denied"), OSError("fixture I/O")])+def test_real_read_error_is_not_content_corruption_or_retirement_absence(tmp_path, monkeypatch, error):+ async def scenario():+ async with _relay() as (origin, requests, _):+ service, config, runtime, source, ref = await _source(tmp_path, origin, "bearer")+ path = _document(runtime.state_store, ref)+ original_open = os.open++ def fail_read(target, flags, *args, **kwargs):+ if Path(target) == path:+ raise error+ return original_open(target, flags, *args, **kwargs)++ monkeypatch.setattr(state_module.os, "open", fail_read)+ service.adapter.provision_credential = AsyncMock(side_effect=AssertionError("must not provision"))+ with pytest.raises(ModelHubError):+ await service.replace_credential(source.id, {"key": "fixture-replacement"})+ assert not requests+ service.adapter.provision_credential.assert_not_awaited()+ config.config.sources.clear()+ runtime.state_store.replace_sources([])+ service.revocations.add(source.id, ref, operation="revoke_api_key_credential")+ await service._ensure_engine_synced()+ assert len(service.revocations.list()) == 1+ assert path.exists()++ asyncio.run(scenario())++[email protected]("unsafe", ["symlink", "permissions", "directory"])+def test_retirement_preflights_all_exact_paths_and_never_touches_oauth(tmp_path, unsafe):+ async def scenario():+ runtime = _adapter(tmp_path)+ store = runtime.state_store+ ref = await runtime.provision_credential("anthropic", "anthropic", KEY, CUSTOM, auth_scheme="bearer")+ _damage(store, ref, "bad_json")+ paths = store._credential_namespace_paths(ref)+ store.audit_auth_permissions()+ auth = store.auth_dir / "unrelated.json"+ store._secure_write_json(auth, {"fixture": "keep"})+ before = auth.read_bytes()+ if unsafe == "symlink":+ paths[1].symlink_to(auth)+ elif unsafe == "permissions":+ store._secure_write_json(paths[1], {"fixture": "unsafe"})+ paths[1].chmod(0o644)+ else:+ paths[1].mkdir(mode=0o700)+ store._secure_write_json(paths[2], {"fixture": "also keep until safe"})+ with pytest.raises(EngineStateError):+ await runtime.revoke_api_key_credential(ref)+ assert _document(store, ref).read_text() == "{"+ assert paths[2].exists()+ assert auth.read_bytes() == before++ asyncio.run(scenario())+++def test_unlink_then_failed_fsync_keeps_intent_until_absence_is_durable(tmp_path, monkeypatch):+ async def scenario():+ async with _relay() as (origin, _, accepted):+ service, config, runtime, source, ref = await _source(tmp_path, origin, "bearer")+ accepted.add("fixture-replacement")+ path = _document(runtime.state_store, ref)+ real_fsync = EngineStateStore._fsync_directory+ fail = True+ fences = []++ def fsync(directory):+ if directory == path.parent and not path.exists():+ fences.append(directory)+ if fail:+ raise OSError("fixture lost directory flush")+ real_fsync(directory)++ monkeypatch.setattr(EngineStateStore, "_fsync_directory", staticmethod(fsync))+ answer = await service.replace_credential(source.id, {"key": "fixture-replacement"})+ assert config.config.sources[0].credential_ref == answer["source"]["credential_ref"]+ assert not path.exists()+ assert len(service.revocations.list()) == 1+ assert fences+ service.revocations = CredentialRevocationJournal(service.revocations.path)+ service._engine_synced = False+ await service._ensure_engine_synced()+ assert len(service.revocations.list()) == 1+ fail = False+ count = len(fences)+ await service._ensure_engine_synced()+ assert len(fences) > count+ assert not service.revocations.list()++ asyncio.run(scenario())+++def test_retirement_intent_is_durable_before_source_commit_and_preserved_on_crash(tmp_path, monkeypatch):+ async def scenario():+ async with _relay() as (origin, _, accepted):+ service, config, runtime, source, ref = await _source(tmp_path, origin, "bearer")+ accepted.add("fixture-replacement")+ save = config.save+ calls = []++ def assert_intent_before_save(value):+ if value.sources[0].credential_ref != ref:+ pending = CredentialRevocationJournal(service.revocations.path).list()+ assert [(row.credential_ref, row.operation) for row in pending] == [+ (ref, "revoke_api_key_credential"),+ ]+ calls.append(value.sources[0].credential_ref)+ save(value)++ monkeypatch.setattr(config, "save", assert_intent_before_save)+ service.adapter.revoke_api_key_credential = AsyncMock(side_effect=OSError("fixture crash"))+ await service.replace_credential(source.id, {"key": "fixture-replacement"})+ assert calls and calls[-1] == config.config.sources[0].credential_ref+ assert _document(runtime.state_store, ref).exists()+ assert len(CredentialRevocationJournal(service.revocations.path).list()) == 1++ asyncio.run(scenario())+++def test_failed_retirement_journal_fence_does_not_commit_or_delete_old_ref(tmp_path, monkeypatch):+ async def scenario():+ async with _relay() as (origin, _, accepted):+ service, config, runtime, source, ref = await _source(tmp_path, origin, "bearer")+ accepted.add("fixture-replacement")++ def fail_fence():+ raise OSError("fixture journal flush failed")++ monkeypatch.setattr(service.revocations, "_sync_directory", fail_fence)+ with pytest.raises(OSError, match="fixture journal flush failed"):+ await service.replace_credential(source.id, {"key": "fixture-replacement"})+ assert config.config.sources[0].credential_ref == ref+ assert runtime.state_store.read_api_key(ref) == KEY+ assert {path.stem for path in (runtime.state_store.root / "credentials").glob("cred_*.json")} == {ref}+ # A written-but-unflushed old-ref intent cannot retire the still+ # active credential. Restart reconciliation removes that stale row.+ await service._ensure_engine_synced()+ assert not service.revocations.list()+ assert runtime.state_store.read_api_key(ref) == KEY++ asyncio.run(scenario())+++def test_replacement_can_recover_with_active_preupgrade_generic_intent(tmp_path):+ async def scenario():+ async with _relay() as (origin, _, accepted):+ service, config, runtime, source, ref = await _source(tmp_path, origin, "bearer")+ _damage(runtime.state_store, ref, "bad_json")+ service.revocations.add(source.id, ref)+ accepted.add("fixture-replacement")+ answer = await service.replace_credential(source.id, {"key": "fixture-replacement"})+ assert config.config.sources[0].credential_ref == answer["source"]["credential_ref"]+ assert not service.revocations.list()+ assert not _document(runtime.state_store, ref).exists()++ asyncio.run(scenario())+++def test_old_ref_used_by_another_source_is_not_retired_until_unbound(tmp_path):+ async def scenario():+ async with _relay() as (origin, _, accepted):+ service, config, runtime, source, ref = await _source(tmp_path, origin, "bearer")+ alias = ModelHubSourceConfig.from_payload({**source.to_payload(), "id": "src_aliasfixture"})+ config.config.sources.append(alias)+ accepted.add("fixture-replacement")+ await service.replace_credential(source.id, {"key": "fixture-replacement"})+ assert len(service.revocations.list()) == 1+ assert runtime.state_store.read_api_key(ref) == KEY+ await service._ensure_engine_synced()+ assert len(service.revocations.list()) == 1+ assert runtime.state_store.read_api_key(ref) == KEY+ config.config.sources = [entry for entry in config.config.sources if entry.id != alias.id]+ await service._ensure_engine_synced()+ assert not service.revocations.list()+ assert not _document(runtime.state_store, ref).exists()++ asyncio.run(scenario())+++def test_plain_oauth_identity_cannot_be_repaired_or_retired_as_api_key(tmp_path):+ async def scenario():+ runtime = _adapter(tmp_path)+ store = runtime.state_store+ store.audit_auth_permissions()+ auth = store.auth_dir / "fixture-oauth.json"+ store._secure_write_json(auth, {"fixture": "must survive"})+ ref = store.bind_oauth_credential("src_oauthfixture", "anthropic", auth.name)+ assert re.fullmatch(r"cred_[0-9a-f]{32}", ref)+ runtime.supervisor.client_if_running = lambda: pytest.fail("must not call OAuth management")+ with pytest.raises(EngineStateError):+ await runtime.credential_auth_scheme(ref)+ with pytest.raises(EngineStateError):+ await runtime.revoke_api_key_credential(ref)+ assert store.credential_metadata(ref)["kind"] == "oauth"+ assert auth.exists()++ asyncio.run(scenario())diff --git a/tests/test_model_hub_resolution.py b/tests/test_model_hub_resolution.pyindex a97d3fa3f..72ba886b8 100644--- a/tests/test_model_hub_resolution.py+++ b/tests/test_model_hub_resolution.py@@ -202,6 +202,9 @@ class FakeAdapter:async def gateway_token(self):return "local-test-token"+ async def credential_auth_scheme(self, credential_ref):+ return None+async def provision_credential(self, vendor, protocol, secret, base_url):self.provisioned.append(secret)return f"cred_{len(self.provisioned):08d}"@@ -234,6 +237,9 @@ class FakeAdapter:await self.revoke_block.wait()self.revoked.append(credential_ref)+ async def revoke_api_key_credential(self, credential_ref):+ await self.revoke_credential(credential_ref)+async def credential_supports_refresh(self, credential_ref):self.capability_queries.append(credential_ref)return credential_ref in self.refreshable_credential_refsdiff --git a/tests/test_model_hub_shell_core_inventory.py b/tests/test_model_hub_shell_core_inventory.pynew file mode 100644index 000000000..fec6313eb--- /dev/null+++ b/tests/test_model_hub_shell_core_inventory.py@@ -0,0 +1,787 @@+"""Pinned core argument roles; shell fixture strings are never executed."""++import asyncio+import hashlib+from pathlib import Path+import shlex+import socket++import pytest++from core.handlers.model_hub import migration_shell as shell+from core.handlers.model_hub.service import ModelHubError+from tests.scenarios.model_hub.test_model_hub_migration_scenarios import _service+from tests.test_model_hub_persisted_inventory import home # noqa: F401+from tests.test_model_hub_shell_writer_roles import (+ KEY,+ forbid_process_inventory, # noqa: F401+ seed,+ test_builtin_roles_guard_exact_cleanup as check_cleanup,+ test_builtin_roles_reach_scan_and_apply as check_service,+)+++def case(role, label, line, writer, *, name=KEY, path=".bashrc"):+ return pytest.param(role, path, name, line + "\n", writer, id=f"{role}-{label}")+++CASES = [+ *[+ case("wait", label, line, writer)+ for label, line, writer in [+ ("separate", "wait -n -p OPENAI_API_KEY", True),+ ("compact", "wait -npOPENAI_API_KEY", True),+ ("without-jobs", "wait -p OPENAI_API_KEY", True),+ ("last-writer", "wait -p other -pOPENAI_API_KEY", True),+ ("last-data", "wait -p OPENAI_API_KEY -p other", False),+ ("array", "wait -p 'OPENAI_API_KEY[0]'", True),+ ("index", "wait -p 'other[OPENAI_API_KEY=1]'", True),+ ("dynamic", 'wait "-$FLAGS" OPENAI_API_KEY -n', True),+ ("operand", "wait OPENAI_API_KEY", False),+ ("end", "wait -- -p OPENAI_API_KEY", False),+ ("invalid", "wait -Q -p OPENAI_API_KEY", False),+ ("quoted", "echo 'wait -npOPENAI_API_KEY'", False),+ ]+ ],+ *[+ case("compgen-target", label, line, writer)+ for label, line, writer in [+ ("separate", "compgen -V OPENAI_API_KEY -A variable", True),+ ("compact", "compgen -VOPENAI_API_KEY -W fixture", True),+ ("no-matches", "compgen -V OPENAI_API_KEY", True),+ ("array-invalid", "compgen -V 'OPENAI_API_KEY[0]' -W fixture", False),+ ("word-data", "compgen -W 'OPENAI_API_KEY=new'", False),+ ("invalid", "complete -V OPENAI_API_KEY -W fixture", False),+ ]+ ],+ *[+ case("trap", label, line, writer)+ for label, line, writer in [+ ("assignment", "trap 'OPENAI_API_KEY=new' EXIT", True),+ ("command", "trap -- 'read OPENAI_API_KEY' EXIT", True),+ ("echo", "trap 'echo OPENAI_API_KEY=new' EXIT", False),+ ("query", "trap -p 'OPENAI_API_KEY=new' EXIT", False),+ ("reset", "trap - EXIT", False),+ ("missing-signal", "trap 'OPENAI_API_KEY=new'", False),+ ]+ ],+ *[+ case("bind", label, line, writer)+ for label, line, writer in [+ ("command", r"""bind -x '"\C-x":OPENAI_API_KEY=new'""", True),+ ("with-display", r"""bind -X -x '"\C-x":OPENAI_API_KEY=new'""", True),+ ("macro", r"""bind '"\C-x":"OPENAI_API_KEY=new"'""", False),+ ("echo", r"""bind -x '"\C-x":echo OPENAI_API_KEY=new'""", False),+ ("unbind-first", r"""bind -r '\C-x' -x '"\C-x":OPENAI_API_KEY=new'""", False),+ ("query", "bind -X", False),+ ]+ ],+ *[+ case(f"{command}-{role}", label, line, writer)+ for command in ("complete", "compgen")+ for role, label, options, writer in [+ ("source", "assignment", "-C 'OPENAI_API_KEY=new'", True),+ ("source", "echo", "-C 'echo OPENAI_API_KEY=new'", False),+ ("expansion", "parameter", "-W '${OPENAI_API_KEY:=new}'", True),+ ("expansion", "arithmetic", "-W '$((OPENAI_API_KEY=1))'", True),+ ("expansion", "substitution", "-W '$(read OPENAI_API_KEY)'", True),+ ("expansion", "word-data", "-W 'OPENAI_API_KEY=new'", False),+ ("expansion", "prefix-data", "-P '${OPENAI_API_KEY:=new}'", False),+ ("expansion", "glob-data", "-G '${OPENAI_API_KEY:=new}'", False),+ ("source", "function-name", "-F 'OPENAI_API_KEY=new'", False),+ ]+ for line in [f"{command} {options}" + (" fixture" if command == "complete" else "")]+ ],+ case("complete-source", "query", "complete -p -C 'OPENAI_API_KEY=new' fixture", False),+ case("complete-expansion", "remove", "complete -r -W '${OPENAI_API_KEY:=new}' fixture", False),+ case("complete-source", "missing-name", "complete -C 'OPENAI_API_KEY=new'", False),+ case("jobs", "read", "jobs -x read OPENAI_API_KEY", True),+ case("jobs", "printf", "jobs -x printf -vOPENAI_API_KEY %s fixture", True),+ case("jobs", "not-source", "jobs -x OPENAI_API_KEY=new", False),+ case("jobs", "echo", "jobs -x echo 'OPENAI_API_KEY=new'", False),+ case("jobs", "not-x", "jobs read OPENAI_API_KEY", False),+ case("jobs", "invalid", "jobs -lx read OPENAI_API_KEY", False),+ case("fc", "editor", "fc -e 'printf -vOPENAI_API_KEY %s fixture' 1", True),+ case("fc", "list", "fc -le 'printf -vOPENAI_API_KEY %s fixture' 1", False),+ case("fc", "history", "fc -se 'printf -vOPENAI_API_KEY %s fixture' 1", False),+ case("fc", "echo", "fc -e 'echo OPENAI_API_KEY=new' 1", False),+ case("coproc", "named", "coproc OPENAI_API_KEY { :; }", True),+ case("coproc", "pid", "coproc WORKER { :; }", True, name="WORKER_PID"),+ case("coproc", "default", "coproc { :; }", True, name="COPROC"),+ case("coproc", "default-pid", "coproc { :; }", True, name="COPROC_PID"),+ case("coproc", "argument", "coproc echo OPENAI_API_KEY", False),+ case("coproc", "echo", "echo 'coproc OPENAI_API_KEY { :; }'", False),+ *[+ case("fd", f"allocate-{index}", f": {{OPENAI_API_KEY}}{operator}fixture", True)+ for index, operator in enumerate((">", "<", ">>", "<>", ">|", ">&", "<<<"))+ ],+ case("fd", "close", ": {OPENAI_API_KEY}>&-", False),+ case("fd", "close-input", ": {OPENAI_API_KEY}<&-", False),+ case("fd", "whitespace", ": {OPENAI_API_KEY} >fixture", False),+ case("fd", "quoted", ": '{OPENAI_API_KEY}'>fixture", False),+ *[+ case("include", f"{command}-{index}", line, False)+ for command in ("source", ".")+ for index, line in enumerate((+ f"{command} OPENAI_API_KEY=new",+ f"{command} 'read OPENAI_API_KEY'",+ f"{command} other 'OPENAI_API_KEY=new'",+ ))+ ],+ *[+ case("code-worklist", f"{depth}-{command}", "eval " * depth + f"{command} OPENAI_API_KEY", writer)+ for depth in (1, 12, 16, 32, 100)+ for command, writer in (("read", True), ("echo", False))+ ],+ *[+ case(role, label, line, writer, path=".zshrc")+ for role, label, line, writer in [+ ("print-target", "separate", "print -v OPENAI_API_KEY fixture-new", True),+ ("print-target", "compact", "print -vOPENAI_API_KEY fixture-new", True),+ ("print-target", "bundle", "print -rvOPENAI_API_KEY -- fixture-new", True),+ ("print-target", "empty", "print -v OPENAI_API_KEY", True),+ ("print-target", "ordinary", "print -- OPENAI_API_KEY", False),+ ("print-target", "unrelated", "print -v unrelated OPENAI_API_KEY", False),+ ("print-target", "R-data", "print -R -v OPENAI_API_KEY fixture-new", False),+ ("print-target", "conflict", "print -zv OPENAI_API_KEY fixture-new", False),+ ("print-format", "n", "print -f '%n' OPENAI_API_KEY", True),+ ("print-format", "number", "print -f '%d' 'OPENAI_API_KEY=2'", True),+ ("print-format", "data", "print -f '%s' OPENAI_API_KEY", False),+ ("print-format", "history", "print -S 'export OPENAI_API_KEY=new'", False),+ ("set-array", "separate", "set -A OPENAI_API_KEY fixture-new", True),+ ("set-array", "compact", "set -AOPENAI_API_KEY fixture-new", True),+ ("set-array", "plus", "set +A OPENAI_API_KEY fixture-new", True),+ ("set-array", "clear", "set -A OPENAI_API_KEY", True),+ ("set-array", "query", "set -A", False),+ ("set-array", "positionals", "set -- OPENAI_API_KEY fixture-new", False),+ ("set-array", "unrelated", "set -A unrelated OPENAI_API_KEY", False),+ ("tied", "second", "typeset -T unrelated=fixture-new OPENAI_API_KEY", True),+ ("tied", "first", "typeset -T OPENAI_API_KEY unrelated", True),+ ("tied", "query", "typeset -pT unrelated=fixture-new OPENAI_API_KEY", False),+ ("getln", "target", "getln OPENAI_API_KEY", True),+ ("getln", "echo", "getln -e OPENAI_API_KEY", False),+ ("shift", "count", "shift 'OPENAI_API_KEY=1' unrelated", True),+ ("shift", "array", "typeset -a OPENAI_API_KEY\nshift 1 OPENAI_API_KEY", True),+ ("shift", "default", "typeset -a OPENAI_API_KEY\nshift OPENAI_API_KEY", True),+ ("shift", "unrelated", "shift 1 unrelated", False),+ ("read-timeout", "evaluation", "read -t '1+(OPENAI_API_KEY=2)' unrelated", True),+ ("read-timeout", "reference", "read -t '1+OPENAI_API_KEY' unrelated", False),+ ("test-fd", "evaluation", "test -t 'OPENAI_API_KEY=1'", True),+ ("test-fd", "bracket", "[ -t 'OPENAI_API_KEY=1' ]", True),+ ("test-fd", "decimal", "test 'OPENAI_API_KEY=2' -eq 2", False),+ ("test-fd", "string", "[ 'OPENAI_API_KEY=2' = 'OPENAI_API_KEY=2' ]", False),+ ("zsh-trap", "source", "trap 'export OPENAI_API_KEY=new' DEBUG", True),+ ("zsh-trap", "read", "trap -- 'read OPENAI_API_KEY' DEBUG", True),+ ("zsh-trap", "data", "trap 'echo OPENAI_API_KEY' DEBUG", False),+ ("zsh-trap", "reset", "trap - DEBUG", False),+ ("zsh-trap", "query", "trap", False),+ ("emulate", "source", "emulate zsh -c 'export OPENAI_API_KEY=new'", True),+ ("emulate", "read", "emulate sh -c 'read OPENAI_API_KEY'", True),+ ("emulate", "query", "emulate -l zsh", False),+ ("emulate", "invalid", "emulate -L zsh -c 'OPENAI_API_KEY=2'", False),+ ("emulate", "data", "emulate zsh -c 'echo OPENAI_API_KEY'", False),+ ("emulate", "local-zsh", "emulate zsh -c 'read -p OPENAI_API_KEY'", True),+ ("emulate", "local-sh", "emulate sh -c 'read -p OPENAI_API_KEY unrelated'", True),+ ("zsh-fc", "source", "fc -e 'OPENAI_API_KEY=new; :'", True),+ ("zsh-fc", "read", "fc -e 'read OPENAI_API_KEY; :'", True),+ ("zsh-fc", "list", "fc -l -e 'OPENAI_API_KEY=new; :'", False),+ ("zsh-fc", "data", "fc -e 'echo OPENAI_API_KEY'", False),+ ("zsh-fc", "no-editor", "fc -e -", False),+ ("zmodload", "target", "zmodload -FL -P OPENAI_API_KEY zsh/parameter", True),+ ("zmodload", "unrelated", "zmodload -FL -P unrelated zsh/parameter", False),+ ("zmodload", "query", "zmodload -FL zsh/parameter", False),+ ("zmodload", "existence", "zmodload -FLe -P OPENAI_API_KEY zsh/parameter", False),+ ("zsh-prefix", "noglob", "noglob read OPENAI_API_KEY", True),+ ("zsh-prefix", "exec", "exec builtin read OPENAI_API_KEY", True),+ ("zsh-prefix", "argv0", "exec -a fixture-process builtin read OPENAI_API_KEY", True),+ ("zsh-prefix", "dash", "- builtin read OPENAI_API_KEY", True),+ ("zsh-prefix", "noglob-data", "noglob echo 'OPENAI_API_KEY=new'", False),+ ("zsh-prefix", "exec-data", "exec echo 'OPENAI_API_KEY=new'", False),+ ("zsh-prefix", "argv0-data", "exec -a OPENAI_API_KEY=new echo", False),+ ("zsh-prefix", "dash-data", "- echo 'OPENAI_API_KEY=new'", False),+ ]+ ],+ *[+ case("numeric-declaration", f"{index}-{label}", f"{command} {operand}", writer, path=".zshrc")+ for index, command in enumerate(("integer", "float", "typeset -i", "typeset -E", "typeset -F"))+ for label, operand, writer in (+ ("coercion", KEY, True),+ ("assignment", KEY + "=2", True),+ ("rhs", "unrelated='OPENAI_API_KEY=2'", True),+ ("query", "-p " + KEY, False),+ ("reference", "unrelated='OPENAI_API_KEY'", False),+ )+ ],+ *[+ case("zsh-control", f"{command}-{label}", f"{command} {operand}", writer, path=".zshrc")+ for command in ("break", "continue", "return", "exit", "bye", "logout")+ for label, operand, writer in (+ ("evaluation", "'OPENAI_API_KEY=1'", True),+ ("reference", "OPENAI_API_KEY", False),+ ("invalid-arity", "'OPENAI_API_KEY=1' extra", False),+ )+ ],+ *[+ case("declaration", f"{path}-{command}-{writer}", line, writer, path=path)+ for path in (".bashrc", ".zshrc")+ for command in ("declare", "typeset", "local", "export", "readonly")+ for line, writer in (+ (f"{command} -- OPENAI_API_KEY=new", True),+ (f"{command} -p OPENAI_API_KEY", False),+ )+ ],+ *[+ case("wrapper", f"{path}-{command}-{writer}", f"{command} {body}", writer, path=path)+ for path in (".bashrc", ".zshrc")+ for command in ("command", "builtin")+ for body, writer in (("read OPENAI_API_KEY", True), ("echo OPENAI_API_KEY=new", False))+ ],+ *[+ case(role, f"{path}-{index}", line, writer, path=path)+ for path in (".bashrc", ".zshrc")+ for index, (role, line, writer) in enumerate([+ ("read-output", "read OPENAI_API_KEY", True),+ ("read-output", "read unrelated", False),+ ("getopts", "getopts x OPENAI_API_KEY", True),+ ("getopts", "getopts OPENAI_API_KEY unrelated", False),+ ("unset", "unset OPENAI_API_KEY", True),+ ("unset", "unset -f OPENAI_API_KEY", False),+ ("printf-target", "printf -vOPENAI_API_KEY %s value", True),+ ("printf-target", "printf %s OPENAI_API_KEY", False),+ ("printf-format", "printf '%n' OPENAI_API_KEY", True),+ ("printf-format", "printf '%s' OPENAI_API_KEY", False),+ ("arithmetic", "let 'OPENAI_API_KEY=1'", True),+ ("arithmetic", "let 'OPENAI_API_KEY+1'", False),+ ("arithmetic", "(( OPENAI_API_KEY=1 ))", True),+ ("arithmetic", "(( OPENAI_API_KEY+1 ))", False),+ ("substitution", 'echo "$(read OPENAI_API_KEY)"', True),+ ("substitution", 'echo "$(echo OPENAI_API_KEY)"', False),+ ("parameter-assignment", 'echo "${OPENAI_API_KEY:=new}"', True),+ ("parameter-assignment", 'echo "${OPENAI_API_KEY:-new}"', False),+ ("assignment", 'OPENAI_API_KEY="$VALUE"', True),+ ("assignment", "echo 'OPENAI_API_KEY=value'", False),+ ("eval-source", "eval -- 'read OPENAI_API_KEY'", True),+ ("eval-source", "eval -- 'echo OPENAI_API_KEY'", False),+ ("eval-source", "eval -Q 'read OPENAI_API_KEY'", False),+ ("loop-target", "for OPENAI_API_KEY in value; do :; done", True),+ ("loop-target", "for other in OPENAI_API_KEY; do :; done", False),+ ("nested", "if true; then read OPENAI_API_KEY; fi", True),+ ("nested", "if true; then echo OPENAI_API_KEY; fi", False),+ ("nested", "f() { read OPENAI_API_KEY; }", True),+ ("nested", "f() { echo OPENAI_API_KEY; }", False),+ ("prefix", "! read OPENAI_API_KEY", True),+ ("prefix", "! echo OPENAI_API_KEY", False),+ ])+ ],+ *[+ case(role, f"{command}-{writer}", line, writer)+ for command in ("mapfile", "readarray")+ for role, line, writer in (+ ("array-reader", f"{command} OPENAI_API_KEY", True),+ ("array-reader", f"{command} unrelated", False),+ ("array-callback", f"{command} -C 'read OPENAI_API_KEY' unrelated", True),+ ("array-callback", f"{command} -C 'echo OPENAI_API_KEY' unrelated", False),+ )+ ],+ case("test-fd", "invalid-extra", "test foo -t 'OPENAI_API_KEY=1'", False, path=".zshrc"),+ case("test-fd", "logical", "test -n x -a -t 'OPENAI_API_KEY=1'", True, path=".zshrc"),+ case("print-format", "format-before-R", "print -f %s -R -vOPENAI_API_KEY data", True, path=".zshrc"),+ case("include", "expansion-before-filename", 'source "$(read OPENAI_API_KEY)"', True),+ case("coproc", "parenthesized", "coproc OPENAI_API_KEY ( : )", True),+ case("coproc", "body", "coproc other { read OPENAI_API_KEY; }", True),+ case("coproc", "condition-body", "coproc other while read OPENAI_API_KEY; do :; done", True),+ case("coproc", "condition-data", "coproc other while echo OPENAI_API_KEY; do :; done", False),+ *[+ case(role, f"dynamic-{index}", line, True, path=path)+ for index, (role, path, line) in enumerate([+ ("compgen-target", ".bashrc", 'compgen "$FLAGS" OPENAI_API_KEY'),+ ("complete-source", ".bashrc", """complete "$FLAGS" -C 'read OPENAI_API_KEY'"""),+ ("complete-source", ".bashrc", """complete "$FLAGS" 'read OPENAI_API_KEY' fixture"""),+ ("compgen-expansion", ".bashrc", """compgen "$FLAGS" '${OPENAI_API_KEY:=new}'"""),+ ("trap", ".bashrc", """trap "$FLAGS" 'read OPENAI_API_KEY' EXIT"""),+ ("bind", ".bashrc", """bind "$FLAGS" '"key":read OPENAI_API_KEY'"""),+ ("fc", ".bashrc", """fc "$FLAGS" 'read OPENAI_API_KEY' 1"""),+ ("print-target", ".zshrc", 'print "$FLAGS" OPENAI_API_KEY'),+ ("set-array", ".zshrc", 'set "$FLAGS" OPENAI_API_KEY'),+ ("zmodload", ".zshrc", 'zmodload "$FLAGS" -P OPENAI_API_KEY zsh/parameter'),+ ])+ ],+ *[+ case("numeric-declaration", f"alias-{command}", f"{command} -i OPENAI_API_KEY", True, path=".zshrc")+ for command in ("declare", "local", "export", "readonly")+ ],+ *[+ case("tied", f"alias-{command}", f"{command} -T other=new OPENAI_API_KEY", True, path=".zshrc")+ for command in ("declare", "local", "export", "readonly")+ ],+ case("numeric-declaration", "integer-invalid", "integer -E OPENAI_API_KEY", False, path=".zshrc"),+ case("numeric-declaration", "float-invalid", "float -i OPENAI_API_KEY", False, path=".zshrc"),+ case("bind", "whitespace-command", """bind -x '"key" "read OPENAI_API_KEY"'""", True),+ case("bind", "missing-separator", """bind -x '"key"read OPENAI_API_KEY'""", False),+ case("jobs", "x-before-format", "jobs -xl read OPENAI_API_KEY", True),+ case("jobs", "later-invalid-x", "jobs -xlx read OPENAI_API_KEY", False),+ case("complete-source", "invalid-action", "complete -A invalid -C 'read OPENAI_API_KEY' fixture", False),+ case("zmodload", "no-list", "zmodload -F -P OPENAI_API_KEY zsh/parameter", False, path=".zshrc"),+ case("wait", "not-zsh", "wait -p OPENAI_API_KEY", False, path=".zshrc"),+ case("print-target", "R-same-bundle", "print -RvOPENAI_API_KEY data", True, path=".zshrc"),+ case("print-format", "R-format-same-bundle", "print -Rf '%n' OPENAI_API_KEY", True, path=".zshrc"),+ *[+ case("emulate", f"core-table-{mode}-{index}", f"emulate {mode} -c '{body}'", writer, path=".zshrc")+ for mode in ("sh", "ksh", "csh", "unrecognized", '"$MODE"')+ for index, (body, writer) in enumerate((+ ("read -p OPENAI_API_KEY", True),+ ("print -vOPENAI_API_KEY data", True),+ ("wait -p OPENAI_API_KEY", False),+ ("mapfile OPENAI_API_KEY", False),+ ("echo OPENAI_API_KEY", False),+ ))+ ],+ *[+ case(+ f"{command}-source", f"validation-{flag}-{index}",+ f"{command} -{flag} {value} -C 'read OPENAI_API_KEY'"+ + (" fixture" if command == "complete" else ""), valid,+ )+ for command in ("complete", "compgen")+ for flag, values in (+ ("F", (+ ('"$FUNCTION"', True), ("fixture-function", True), ("'not=a_name'", True),+ ("fixture.function", True), ("'9'", True), ("''", True),+ ("'$FUNCTION'", True), ("'bad name'", False), ("'bad(name'", False),+ ("'bad;name'", False), ("'bad|name'", False), ("'bad\\nname'".replace("\\n", "\n"), False),+ )),+ ("V", (+ ('"$ARRAY"', True), ("unrelated", True), ("'other[0]'", False),+ ("'not=a_name'", False), ("'9'", False), ("''", False),+ ) if command == "compgen" else ()),+ ("A", (+ ('"$ACTION"', True), ("variable", True), ("''", False), ("not-an-action", False),+ )),+ ("o", (+ ('"$OPTION"', True), ("fullquote", True), ("''", False), ("not-an-option", False),+ )),+ )+ for index, (value, valid) in enumerate(values)+ ],+ *[+ case(+ f"{command}-expansion", f"validation-{flag}-word-{writer}",+ f"{command} -{flag} \"${variable}\" -W '{body}'"+ + (" fixture" if command == "complete" else ""), writer,+ )+ for command in ("complete", "compgen")+ for flag, variable in (("F", "FUNCTION"), ("A", "ACTION"), ("o", "OPTION"))+ for body, writer in (+ ("$(read OPENAI_API_KEY)", True),+ ("${OPENAI_API_KEY:=new}", True),+ ("OPENAI_API_KEY=new", False),+ ("$(echo OPENAI_API_KEY)", False),+ )+ ],+ *[+ case(+ f"{command}-source", f"validation-order-{flag}-{index}",+ f"{command} {options} -C 'read OPENAI_API_KEY'"+ + (" fixture" if command == "complete" else ""), writer,+ )+ for command, flag, invalid, valid in (+ ("complete", "F", "'bad name'", "fixture-function"),+ ("compgen", "V", "'other[0]'", "unrelated"),+ ("complete", "A", "not-an-action", "variable"),+ ("compgen", "o", "not-an-option", "fullquote"),+ )+ for index, (options, writer) in enumerate((+ (f"-{flag} {invalid} -{flag} {valid}", False),+ (f"-{flag} {valid} -{flag} {invalid}", False),+ (f'-{flag} "$VALUE" -{flag} {invalid}', False),+ (f'-{flag} {invalid} "$FLAGS"', False),+ (f'"-{flag}$VALUE"', True),+ (f'-- -{flag} {invalid}', False),+ (f'fixture -{flag} {invalid}', False),+ ))+ ],+ *[+ case(+ "complete-source", f"validation-upstream-{flag}-{value}",+ f"complete -{flag} {value} -C 'read OPENAI_API_KEY' fixture", True,+ )+ for flag, values in (+ ("A", (+ "alias", "arrayvar", "binding", "builtin", "command", "directory", "disabled", "enabled",+ "export", "file", "function", "group", "helptopic", "hostname", "job", "keyword",+ "running", "service", "setopt", "shopt", "signal", "stopped", "user", "variable",+ )),+ ("o", ("bashdefault", "default", "dirnames", "filenames", "fullquote", "noquote", "nosort", "nospace", "plusdirs")),+ )+ for value in values+ ],+ case("complete-source", "validation-dynamic-query", """complete -p -F "$FN" -C 'read OPENAI_API_KEY' fixture""", False),+ case("complete-source", "validation-dynamic-remove", """complete -r -F "$FN" -C 'read OPENAI_API_KEY' fixture""", False),+ case("complete-source", "validation-data-boundary", """complete -C 'read OPENAI_API_KEY' "$FLAGS" -F 'bad name' fixture""", True),+ case("complete-source", "validation-function-not-code", """complete -F 'OPENAI_API_KEY=new' fixture""", False),+ case("compgen-target", "validation-unknown-no-invented-target", """compgen -V "$ARRAY" -W fixture""", False),+ case("compgen-target", "validation-literal-target-code-format", """compgen -V OPENAI_API_KEY -F "$FUNCTION" """, True),+ *[+ case("eval-source", f"envelope-{path}-{index}", line, writer, path=path)+ for path in (".bashrc", ".zshrc")+ for index, (line, writer) in enumerate((+ ("eval '-Q; read OPENAI_API_KEY'", path == ".zshrc"),+ ("eval '--; read OPENAI_API_KEY'", path == ".zshrc"),+ ("eval '-Q; echo OPENAI_API_KEY'", False),+ ("eval '--; echo OPENAI_API_KEY'", False),+ ("eval -Q 'read OPENAI_API_KEY'", False),+ ("eval -- '-Q; read OPENAI_API_KEY'", True),+ ("eval -- '--; read OPENAI_API_KEY'", True),+ ("eval -- -- 'read OPENAI_API_KEY'", False),+ ("eval '--' 'read OPENAI_API_KEY'", True),+ ("eval -- -- '; read OPENAI_API_KEY'", True),+ ("eval -- -- '; echo OPENAI_API_KEY'", False),+ ("eval '-Q;' 'read OPENAI_API_KEY'", path == ".zshrc"),+ ('eval "$FLAGS" "read OPENAI_API_KEY"', True),+ ('eval "-$FLAGS" "read OPENAI_API_KEY"', True),+ ('eval "$FLAGS" "echo OPENAI_API_KEY"', False),+ ('eval -- "$FLAGS" "read OPENAI_API_KEY"', False),+ ("""eval '-$FLAGS' 'read OPENAI_API_KEY'""", False),+ ('eval "-Q$FLAGS" "read OPENAI_API_KEY"', False),+ ))+ ],+ *[+ case(+ f"{command}-expansion", f"boundary-word-{index}",+ f"{command} -W {shlex.quote(text)}" + (" fixture" if command == "complete" else ""), writer,+ )+ for command in ("complete", "compgen")+ for index, (text, writer) in enumerate((+ ("# $((OPENAI_API_KEY=1))", True),+ ("# $(read OPENAI_API_KEY)", True),+ ("# ${OPENAI_API_KEY:=new}", True),+ ("# `read OPENAI_API_KEY`", True),+ ("<<EOF\n# $(read OPENAI_API_KEY)\nEOF", True),+ ("# $(# source comment\nread OPENAI_API_KEY)", True),+ ('# "$(read OPENAI_API_KEY)"', True),+ ("# '$((OPENAI_API_KEY=1))'", False),+ ("# '$(read OPENAI_API_KEY)'", False),+ (r"# \$(read OPENAI_API_KEY)", False),+ (r"# \${OPENAI_API_KEY:=new}", False),+ ("# OPENAI_API_KEY=1", False),+ ("# ((OPENAI_API_KEY=1))", False),+ ("# $(echo OPENAI_API_KEY)", False),+ ("# $(# read OPENAI_API_KEY\n:)", False),+ ("<<EOF\n# '$(read OPENAI_API_KEY)'\nEOF", False),+ ))+ ],+ *[+ case("coproc", f"boundary-name-{index}-{pid}", f"coproc {name} {{ :; }}", True, name=target)+ for index, name in enumerate((+ "'WORKER'", '"WORKER"', "WOR'KER'",+ r"WOR\KER", "WOR\\\nKER",+ ))+ for pid, target in ((False, "WORKER"), (True, "WORKER_PID"))+ ],+ *[+ case("coproc", f"boundary-shape-{index}", line, writer, name=name)+ for index, (line, name, writer) in enumerate((+ ("coproc 'OPENAI_API_KEY' ( : )", KEY, True),+ ("coproc 'OPENAI_API_KEY' while :; do :; done", KEY, True),+ ("coproc echo 'OPENAI_API_KEY'", KEY, False),+ ("coproc 'OPENAI_API_KEY' echo data", KEY, False),+ ('coproc "$NAME" { :; }', KEY, False),+ ('coproc "$NAME" { :; }', "COPROC", False),+ ("coproc 'not-a-name' { :; }", "COPROC_PID", False),+ ('coproc "$NAME" { read OPENAI_API_KEY; }', KEY, True),+ ("coproc 'WORKER' { echo OPENAI_API_KEY; }", KEY, False),+ ("coproc echo 'OPENAI_API_KEY'", "COPROC", True),+ ))+ ],+ *[+ case("fd", f"boundary-continuation-{index}-{operator}-{ending!r}", f": {target}{operator}fixture", True)+ for ending in ("\n", "\r\n")+ for index, target in enumerate((+ "{OPENAI_API_KEY}\\" + ending,+ "{OPENAI_\\" + ending + "API_KEY}",+ "\\" + ending + "{OPENAI_API_KEY}",+ "{OPENAI_\\" + ending + "API_KEY}\\" + ending,+ ))+ for operator in (">", ">&", "<<<")+ ],+ *[+ case("fd", f"boundary-data-{index}", line, False)+ for index, line in enumerate((+ ": {OPENAI_API_KEY}\\\n >fixture",+ ": {OPENAI_API_KEY} \\\n>fixture",+ ": {OPENAI_API_KEY}\\\n\t>fixture",+ ": '{OPENAI_API_KEY}'\\\n>fixture",+ ': "{OPENAI_\\\nAPI_KEY}">fixture',+ ": {OPENAI_API_KEY}\\\n>&-",+ ": {OPENAI_\\\nAPI_KEY}<&'-'",+ ": {OPENAI_API_KEY}\\\n>&\\\n-",+ ": {OPENAI_API_KEY}\\\n>",+ ))+ ],+ *[+ case("zsh-control", f"boundary-{command}-{index}", f"{command} {operands}", writer, path=".zshrc")+ for command in ("break", "continue", "return", "exit", "bye", "logout")+ for index, (operands, writer) in enumerate((+ ('"$BOUNDARY" \'OPENAI_API_KEY=1\'', True),+ ('"-$BOUNDARY" \'OPENAI_API_KEY=1\'', True),+ ("-- 'OPENAI_API_KEY=1'", True),+ ('"$BOUNDARY" \'OPENAI_API_KEY+1\'', False),+ ('-- "$BOUNDARY" \'OPENAI_API_KEY=1\'', False),+ ('"$BOUNDARY" -- \'OPENAI_API_KEY=1\'', False),+ ("-- -- 'OPENAI_API_KEY=1'", False),+ ('"fixed$BOUNDARY" \'OPENAI_API_KEY=1\'', False),+ ('"$BOUNDARY" \'OPENAI_API_KEY=1\' extra', False),+ ))+ ],+ *[+ case("zsh-trap", f"boundary-{index}", line, writer, path=".zshrc")+ for index, (line, writer) in enumerate((+ ('trap "$BOUNDARY" \'read OPENAI_API_KEY\' DEBUG', True),+ ('trap "-$BOUNDARY" \'print -v OPENAI_API_KEY value\' DEBUG', True),+ ('trap "$BOUNDARY" \'read OPENAI_API_KEY\'', False),+ ('trap "$BOUNDARY" \'echo OPENAI_API_KEY\' DEBUG', False),+ ('trap -- "$BOUNDARY" \'read OPENAI_API_KEY\' DEBUG', False),+ ('trap "$BOUNDARY" -- \'read OPENAI_API_KEY\' DEBUG', False),+ ("trap -- -- 'read OPENAI_API_KEY' DEBUG", False),+ ('trap "fixed$BOUNDARY" \'read OPENAI_API_KEY\' DEBUG', False),+ ('trap "$BOUNDARY" "print -rf %n unrelated OPENAI_API_KEY" DEBUG', False),+ ('trap "$BOUNDARY" "print -rf %n OPENAI_API_KEY unrelated" DEBUG', True),+ ))+ ],+ *[+ case("print-format", f"boundary-reuse-{index}", line, writer, path=".zshrc")+ for index, (line, writer) in enumerate((+ ("print -rf '%n' unrelated OPENAI_API_KEY", False),+ ("print -r -f '%d' 1 'OPENAI_API_KEY=2'", False),+ ("print -r -f '%s%n' value unrelated ignored OPENAI_API_KEY", False),+ ("print -rf '%n' OPENAI_API_KEY unrelated", True),+ ("print -rf '%d' 'OPENAI_API_KEY=1' 2", True),+ ("print -rf '%2$n' unrelated OPENAI_API_KEY unused", True),+ ("print -rf '%2$n' unrelated other OPENAI_API_KEY", False),+ ("print -rf '%*d%n' 1 2 unrelated OPENAI_API_KEY", False),+ ("print -rf '%*d%n' 1 2 OPENAI_API_KEY unrelated", True),+ ("print -rf '%*d' 'OPENAI_API_KEY=1' 2", True),+ ("print -f '%n' unrelated OPENAI_API_KEY", True),+ ("printf '%n' unrelated OPENAI_API_KEY", True),+ ("print -rvOPENAI_API_KEY -f '%n' unrelated OPENAI_API_KEY", True),+ ('print -rf "$FORMAT" unrelated OPENAI_API_KEY', True),+ ('print -r "$FLAGS" -f "%n" unrelated OPENAI_API_KEY', False),+ # A preceding unknown -f can consume -r as its format operand.+ ('print "$FLAGS" -r -f "%n" unrelated OPENAI_API_KEY', True),+ ))+ ],+]++[email protected](autouse=True)+def forbid_network(monkeypatch):+ def forbidden(*args, **kwargs):+ raise AssertionError("network is not part of the shell inventory fixture")++ monkeypatch.setattr(socket.socket, "connect", forbidden)+ monkeypatch.setattr(socket.socket, "connect_ex", forbidden)+ monkeypatch.setattr(socket, "create_connection", forbidden)++[email protected]("role,filename,name,line,writer", CASES)+def test_core_roles_cleanup(home, role, filename, name, line, writer):+ check_cleanup(home, filename, name, line, writer)++[email protected]("role,filename,name,line,writer", CASES)+def test_core_roles_actual_apply(home, tmp_path, role, filename, name, line, writer):+ check_service(home, tmp_path, filename, name, line, writer)++[email protected]("role,filename,name,line,writer", [c for c in CASES if c.values[-1]])+def test_core_standalone_writer_scan(home, tmp_path, role, filename, name, line, writer):+ path, _ = seed(home, filename, name, line)+ path.write_text(line)+ profiles = shell.read_shell_profiles(home, frozenset({name}))+ profile = next(p for p in profiles if p.path == path)+ assert profile.values == {}+ assert any(name in issue.names for issue in profile.issues)+ service, store, adapter = _service(tmp_path, migration_home=home)+ rows = service.migration_scan()["items"]+ assert rows and all(row["proposed_action"] == "reauth" for row in rows)+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([row["id"] for row in rows]))+ assert not adapter.provisioned and not adapter.oauth_provisioned and not adapter.transient_refs+ assert not adapter.observed and not store.config.sources+ assert path.read_bytes() == line.encode()++[email protected]("filename,line", [+ (".bashrc", "wait -p OPENAI_API_KEY"),+ (".bashrc", "compgen -VOPENAI_API_KEY -W fixture"),+ (".bashrc", "trap 'read OPENAI_API_KEY' EXIT"),+ (".bashrc", "eval " * 32 + "read OPENAI_API_KEY"),+ (".bashrc", "compgen -W '# $(read OPENAI_API_KEY)'"),+ (".bashrc", "coproc 'OPENAI_API_KEY' { :; }"),+ (".bashrc", ": {OPENAI_API_KEY}\\\n>fixture"),+ (".zshrc", 'return "$BOUNDARY" "OPENAI_API_KEY=1"'),+ (".zshrc", "print -rf '%n' OPENAI_API_KEY unrelated"),+])+def test_core_independent_backend(home, tmp_path, filename, line):+ path = home / filename+ kept = "export OPENAI_API_KEY=fixture-codex\n" + line + "\n"+ independent = "export ANTHROPIC_API_KEY=fixture-claude\n"+ # coproc's existing conservative post-compound top-level boundary is not+ # part of destination dequoting. Use a proven top-level independent row.+ path.write_text(independent + kept if line.startswith("coproc") else kept + independent)+ service, store, adapter = _service(tmp_path, migration_home=home)+ rows = {row["backend"]: row for row in service.migration_scan()["items"]}+ assert rows["codex"]["notes_key"].endswith(".dynamic_shell")+ assert rows["claude"]["proposed_action"] == "import"+ assert asyncio.run(service.migration_apply([rows["claude"]["id"]]))["applied"] == 1+ assert len(adapter.provisioned) == len(store.config.sources) == 1+ assert path.read_text() == kept++[email protected]("depth", [16, 100, 1100])[email protected]("writer", [True, False])[email protected]("dialect", ["bash", "zsh"])+def test_written_code_work_is_linear_in_context_count(monkeypatch, depth, writer, dialect):+ original = shell._WrittenCode.visit+ calls = 0++ def counted(self, *args):+ nonlocal calls+ calls += 1+ assert calls <= 2 * (depth + 1)+ return original(self, *args)++ monkeypatch.setattr(shell._WrittenCode, "visit", counted)+ text = "eval " * depth + ("read" if writer else "echo") + " OPENAI_API_KEY"+ assert shell._written_names(text, frozenset({KEY}), dialect=dialect) == ({KEY} if writer else set())++[email protected]("writer", [True, False])+def test_duplicate_code_contexts_are_visited_once(monkeypatch, writer):+ original = shell._WrittenCode.visit+ calls = 0++ def counted(self, *args):+ nonlocal calls+ calls += 1+ assert calls <= 4+ return original(self, *args)++ monkeypatch.setattr(shell._WrittenCode, "visit", counted)+ body = ("read" if writer else "echo") + " OPENAI_API_KEY"+ text = "; ".join([f"trap '{body}' EXIT"] * 200)+ assert shell._written_names(text, frozenset({KEY})) == ({KEY} if writer else set())++[email protected]("writer", [True, False])+def test_duplicate_expansions_after_hash_share_code_work(monkeypatch, writer):+ original = shell._WrittenCode.visit+ calls = 0++ def counted(self, *args):+ nonlocal calls+ calls += 1+ assert calls <= 3+ return original(self, *args)++ monkeypatch.setattr(shell._WrittenCode, "visit", counted)+ command = "read" if writer else "echo"+ analysis = shell._WrittenCode(frozenset({KEY}))+ analysis.add("expansion", "# " + f"$({command} {KEY}) " * 200, "bash")+ assert analysis.run() == ({KEY} if writer else set())+++def test_optionless_envelope_has_at_most_two_once_only_cases():+ words = [shell._Token(f"$BOUNDARY{index}", f"$BOUNDARY{index}", literal=False) for index in range(20)]+ assert list(shell._zsh_optionless_cases(words)) == [words, words[1:]]+ assert list(shell._zsh_optionless_cases([shell._Token("--", "--"), *words])) == [words]+++def test_fd_logical_continuation_keeps_original_cleanup_offsets(home):+ path = home / ".bashrc"+ prefix = "# 保留偏好\r\n: {WORKER}\\\r\n>fixture\r\n".encode()+ assignment = b"export OPENAI_API_KEY='fixture-literal'\r\n"+ suffix = b"# unchanged tail\r\n"+ before = prefix + assignment + suffix+ path.write_bytes(before)+ profile = next(p for p in shell.read_shell_profiles(home, frozenset({KEY})) if p.path == path)+ assert profile.values == {KEY: "fixture-literal"} and not profile.issues+ [stored] = profile.assignments+ assert (stored.start, stored.end) == (len(prefix), len(prefix + assignment))+ edit = shell.cleanup_shell_profile(profile, profile.values)+ edit = type(edit).from_payload(edit.to_payload())+ edit.apply()+ assert path.read_bytes() == prefix + suffix+ edit.apply(reverse=True)+ assert path.read_bytes() == before+++def test_code_identity_keeps_role_and_local_dialect():+ analysis = shell._WrittenCode(frozenset({KEY}))+ analysis.add("expansion", "OPENAI_API_KEY=new", "bash")+ assert analysis.run() == set()+ analysis.add("source", "OPENAI_API_KEY=new", "bash")+ assert analysis.run() == {KEY}+ analysis = shell._WrittenCode(frozenset({KEY}))+ analysis.add("source", "read -p OPENAI_API_KEY unrelated", "posix")+ assert analysis.run() == set()+ analysis.add("source", "read -p OPENAI_API_KEY unrelated", "zsh")+ assert analysis.run() == {KEY}++[email protected]("command,dialect", [+ ("wait", "bash"), ("compgen", "bash"), ("complete", "bash"), ("bind", "bash"),+ ("fc", "bash"), ("print", "zsh"), ("zmodload", "zsh"),+])[email protected]("count", [4, 10, 20])+def test_catalogue_dynamic_options_project_one_value_role(monkeypatch, command, dialect, count):+ original = shell._options+ calls = 0++ def counted(*args, **kwargs):+ nonlocal calls+ calls += 1+ assert calls <= 12 * (count + 2) ** 2+ return original(*args, **kwargs)++ monkeypatch.setattr(shell, "_options", counted)+ flags = " ".join(f'"-$FLAGS{index}"' for index in range(count))+ assert shell._written_names(f"{command} {flags} unrelated", frozenset({KEY}), dialect=dialect) == set()+++def test_pinned_upstream_census_and_role_coverage():+ """An independent upstream-name digest, not a census of parser dispatch."""+ ledger = (Path(__file__).parents[1] / "docs/plans/model-hub-shell-core-coverage.md").read_text()+ commands = {"bash": {}, "zsh": {}, "syntax": {}}+ bindings = {}+ for line in ledger.splitlines():+ cells = [cell.strip().strip("`") for cell in line.strip("|").split("|")]+ if line.startswith("| ") and len(cells) == 5 and cells[0] in commands:+ dialect, name, classification, roles, evidence = cells+ assert name not in commands[dialect], (dialect, name)+ assert classification in {"role", "none", "excluded"}+ assert evidence+ commands[dialect][name] = (classification, set(roles.split(",")) if roles != "-" else set())+ elif line.startswith("| ") and len(cells) == 2 and cells[0] not in {"Role", "---"}:+ role, handler = cells+ assert role not in bindings+ assert hasattr(shell, handler), handler+ bindings[role] = handler+ # Bash includes help-only reserved.def entries; Zsh excludes 3 debug+ # conditionals. Digests were computed directly from pinned upstream source.+ for dialect, count, digest in (+ ("bash", 77, "fc4f5f0bd0d9a5a3942e2561f683af19150b8ea87f5628c2127f494bd6535e4f"),+ ("zsh", 76, "7cbb7e282474cd2085c5ac22929cd0405c4cb08cbb1eabb66afe367144559312"),+ ):+ assert len(commands[dialect]) == count+ assert hashlib.sha256("\n".join(sorted(commands[dialect])).encode()).hexdigest() == digest+ covered = {}+ for item in CASES:+ role, _, _, _, writer = item.values+ covered.setdefault(role, set()).add(writer)+ assert set(bindings) == set(covered)+ assert all(outcomes == {True, False} for outcomes in covered.values())+ for census in commands.values():+ for classification, roles in census.values():+ assert bool(roles) == (classification == "role")+ assert roles <= bindings.keys()diff --git a/tests/test_model_hub_shell_writer_roles.py b/tests/test_model_hub_shell_writer_roles.pynew file mode 100644index 000000000..f7b215f22--- /dev/null+++ b/tests/test_model_hub_shell_writer_roles.py@@ -0,0 +1,433 @@+"""Builtin argument roles, consumed by the real migration scan/apply boundary.++Shell text is data only. The imported home fixture isolates all paths, uses+FakeKeychain, and forbids subprocess/native credential access. The service's+fixture guard and adapter never inspect processes or start a real service.+"""++import asyncio++import psutil+import pytest++from core.handlers.model_hub.migration_journal import NativeFileEdit, TakeoverStateError+from core.handlers.model_hub.migration_shell import cleanup_shell_profile, read_shell_profiles+from core.handlers.model_hub.service import ModelHubError+from tests.scenarios.model_hub.test_model_hub_migration_scenarios import _service+from tests.test_model_hub_persisted_inventory import home # noqa: F401+++KEY = "OPENAI_API_KEY"+++def case(label, line, writer, *, name=KEY, path=".profile"):+ return pytest.param(path, name, line + "\n", writer, id=label)+++CASES = [+ # Unknown option roles are not proof of invalid syntax. These strings are+ # never expanded; each writer is supported by an explicit possible slot.+ *[+ case(f"dynamic-option-{command}", line, True)+ for command, line in [+ ("read", 'read "-$FLAGS" OPENAI_API_KEY'),+ ("printf", 'printf "-$FLAGS" OPENAI_API_KEY %s new'),+ ("declare", 'declare "-$FLAGS" OPENAI_API_KEY=new'),+ ("typeset", 'typeset "-$FLAGS" OPENAI_API_KEY=new'),+ ("export", 'export "-$FLAGS" OPENAI_API_KEY=new'),+ ("readonly", 'readonly "-$FLAGS" OPENAI_API_KEY=new'),+ ("mapfile", 'mapfile "-$FLAGS" OPENAI_API_KEY'),+ ("readarray", 'readarray "-$FLAGS" OPENAI_API_KEY'),+ ("unset", 'unset "-$FLAGS" OPENAI_API_KEY'),+ ("getopts", 'getopts "-$FLAGS" a OPENAI_API_KEY -a'),+ ("command", 'command "-$FLAGS" printf -vOPENAI_API_KEY %s new'),+ ("env", 'env "-$FLAGS" OPENAI_API_KEY=new command'),+ ]+ ],+ *[+ case(f"dynamic-whole-{command}", line, True)+ for command, line in [+ ("read", 'read "$FLAGS" OPENAI_API_KEY'),+ ("printf", 'printf "$FLAGS" OPENAI_API_KEY %s new'),+ ("declare", 'declare "$FLAGS" OPENAI_API_KEY=new'),+ ("typeset", 'typeset "$FLAGS" OPENAI_API_KEY=new'),+ ("export", 'export "$FLAGS" OPENAI_API_KEY=new'),+ ("readonly", 'readonly "$FLAGS" OPENAI_API_KEY=new'),+ ("mapfile", 'mapfile "$FLAGS" OPENAI_API_KEY'),+ ("readarray", 'readarray "$FLAGS" OPENAI_API_KEY'),+ ("unset", 'unset "$FLAGS" OPENAI_API_KEY'),+ ("getopts", 'getopts "$FLAGS" a OPENAI_API_KEY -a'),+ ("command", 'command "$FLAGS" printf -vOPENAI_API_KEY %s new'),+ ("builtin", 'builtin "$FLAGS" printf -vOPENAI_API_KEY %s new'),+ ("env", 'env "$FLAGS" OPENAI_API_KEY=new command'),+ ]+ ],+ case("dynamic-integer-role", 'declare "$FLAGS" other="OPENAI_API_KEY=1"', True),+ case("dynamic-local-role", 'f() { local "-$FLAGS" OPENAI_API_KEY=new; }', True),+ case("dynamic-callback-role", 'mapfile "$FLAGS" "OPENAI_API_KEY=1" unrelated', True),+ case("dynamic-array-index-role", 'read "$FLAGS" "other[OPENAI_API_KEY=1]"', True),+ case("dynamic-zsh-read", 'read "-$FLAGS" OPENAI_API_KEY', True, path=".zshrc"),+ case("dynamic-zsh-integer", 'typeset "$FLAGS" other="OPENAI_API_KEY=1"', True, path=".zshrc"),+ case("dynamic-default-read", 'read "$FLAGS"', True, name="REPLY"),+ case("dynamic-default-mapfile", 'mapfile "$FLAGS"', True, name="MAPFILE"),+ case("dynamic-default-getopts", 'getopts "$FLAGS" a other -a x', True, name="OPTARG"),+ # Literal invalid options, explicit --, and already anchored data remain+ # nonwriters. In particular a possible option is not arbitrary shell code.+ case("dynamic-control-static-read", "read '-$FLAGS' OPENAI_API_KEY", False),+ case("dynamic-control-static-printf", "printf '-$FLAGS' OPENAI_API_KEY %s new", False),+ case("dynamic-control-static-declare", "declare '-$FLAGS' OPENAI_API_KEY=new", False),+ case("dynamic-control-format", 'printf %s "-$FLAGS" OPENAI_API_KEY', False),+ case("dynamic-control-echo", 'echo "read -$FLAGS OPENAI_API_KEY"', False),+ case("dynamic-control-unrelated", 'read "-$FLAGS" unrelated', False),+ case("dynamic-control-prompt", 'read -p "$PROMPT" unrelated', False),+ case("dynamic-control-prompt-name", 'read -p "$OPENAI_API_KEY" unrelated', False),+ case("dynamic-control-env-command", 'env echo "$FLAGS" OPENAI_API_KEY=new', False),+ case("dynamic-control-wrapper-command", 'command echo "$FLAGS" OPENAI_API_KEY=new', False),+ case("dynamic-control-wrapper-assignment-data", 'command "$FLAGS" OPENAI_API_KEY=new', False),+ case("dynamic-control-builtin-assignment-data", 'builtin "$FLAGS" OPENAI_API_KEY=new', False),+ case("dynamic-control-static-wrapper-assignment-data", 'command OPENAI_API_KEY=new', False),+ case("dynamic-control-wrapper-query", 'command -v "$FLAGS" printf -vOPENAI_API_KEY %s new', False),+ case("dynamic-control-wrapper-end", 'command -- "$FLAGS" printf -vOPENAI_API_KEY %s new', False),+ case("dynamic-control-env-end", 'env -- "$FLAGS" OPENAI_API_KEY=new', False),+ case("dynamic-control-printf-end", 'printf -- "-$FLAGS" %s OPENAI_API_KEY', True),+ case("dynamic-control-getopts-end", 'getopts -- "$FLAGS" unrelated OPENAI_API_KEY', False),+ case("dynamic-control-declare-end", 'declare -- "$FLAGS" other="OPENAI_API_KEY=1"', False),+ case("dynamic-control-declare-query", 'declare -p OPENAI_API_KEY="$VALUE"', False),+ case("dynamic-control-unset-query", 'unset -f OPENAI_API_KEY', False),+ case("dynamic-control-data-not-code", 'read "$FLAGS" "printf -vOPENAI_API_KEY %s new"', False),+ case("dynamic-control-callback-echo", 'mapfile "$FLAGS" "echo OPENAI_API_KEY" unrelated', False),+ case("dynamic-control-env-data", 'env -a "$FLAGS" echo OPENAI_API_KEY=new', False),+ case("dynamic-control-static-invalid-first", 'read -Q "$FLAGS" OPENAI_API_KEY', False),+ case("dynamic-control-attached-mapfile-data", 'mapfile "-d$DELIMITER" "OPENAI_API_KEY=1" unrelated', False),+ case("dynamic-control-attached-mapfile-callback-data", 'mapfile "-C$CALLBACK" "OPENAI_API_KEY=1"', False),+ case("dynamic-control-attached-env-data", 'env "-a$ARGV0" echo OPENAI_API_KEY=new', False),+ case("dynamic-control-attached-env-long-data", 'env "--chdir=$DIR" echo OPENAI_API_KEY=new', False),+ case("dynamic-control-attached-query", 'command "-v$FLAGS" printf -vOPENAI_API_KEY %s new', False),+ case("dynamic-control-static-operand", 'printf "literal$FORMAT" OPENAI_API_KEY', True),+ case("dynamic-control-static-command", 'command "echo$SUFFIX" printf -vOPENAI_API_KEY %s new', False),+ case("dynamic-control-anchored-env-assignment", 'env OTHER="$VALUE" echo OPENAI_API_KEY=new', False),+ case("dynamic-control-anchored-declare-data", 'declare other="$VALUE" unrelated="OPENAI_API_KEY=1"', False),+ case("dynamic-control-static-query-plus", 'declare -p +x OPENAI_API_KEY=new', False),+ case("dynamic-control-static-query-dash", 'declare -p "-$FLAGS" OPENAI_API_KEY=new', False),+ case("dynamic-control-static-query-attached", 'declare "-p$FLAGS" OPENAI_API_KEY=new', False),+ case("dynamic-control-static-invalid-prefix-read", 'read "-Q$FLAGS" OPENAI_API_KEY', False),+ case("dynamic-control-static-invalid-prefix-printf", 'printf "-Q$FLAGS" OPENAI_API_KEY %s new', False),+ case("dynamic-control-static-invalid-prefix-declare", 'declare "-Q$FLAGS" OPENAI_API_KEY=new', False),+ case("dynamic-control-known-read-timeout", 'read -t0 OPENAI_API_KEY', False),+ case("dynamic-control-zsh-known-echo", 'read -e "$FLAGS" OPENAI_API_KEY', False, path=".zshrc"),+ case("dynamic-overrides-timeout-default", 'read -t0 "$FLAGS"', True, name="REPLY"),+ case("dynamic-zsh-format-evaluation", 'printf "$FLAGS" "OPENAI_API_KEY=1"', True, path=".zshrc"),+ case("dynamic-bash-format-data", 'printf "$FLAGS" "OPENAI_API_KEY=1"', False, path=".bashrc"),+ *[+ case(+ f"dynamic-repeated-{command}-{'writer' if writer else 'data'}",+ command + " " + " ".join(f'"-$FLAGS{index}"' for index in range(10))+ + (" OPENAI_API_KEY" if writer else " unrelated"),+ writer,+ )+ for command in ("read", "mapfile")+ for writer in (True, False)+ ],+ case("printf-compact", "printf -vOPENAI_API_KEY '%s' fixture-new", True),+ case("printf-separate", "printf -v OPENAI_API_KEY '%s' fixture-new", True),+ case("printf-array", "printf -v 'OPENAI_API_KEY[0]' '%s' fixture-new", True),+ case("printf-format-data", "printf '%s' -vOPENAI_API_KEY", False),+ case("printf-endoptions-data", "printf -- -vOPENAI_API_KEY", False),+ case("read-compact-array", "read -aOPENAI_API_KEY", True),+ case("read-bundle-array", "read -raOPENAI_API_KEY", True),+ case("read-array-element", "read 'OPENAI_API_KEY[0]'", True),+ case("read-separate-array", "read -a OPENAI_API_KEY", True),+ case("read-combined-prompt-data", "read -rp OPENAI_API_KEY unrelated", False),+ case("read-separate-prompt-data", "read -r -p OPENAI_API_KEY unrelated", False),+ case("read-ignored-extra-names", "read -a unrelated OPENAI_API_KEY", False),+ case("read-default-reply", "read -r", True, name="REPLY"),+ case("getopts-endoptions", "getopts -- 'a:' OPENAI_API_KEY", True),+ case("getopts-separate", "getopts 'a:' OPENAI_API_KEY", True),+ case("getopts-optstring-data", "getopts -- OPENAI_API_KEY unrelated", False),+ case("getopts-implicit-arg", "getopts 'a:' unrelated -a fixture-new", True, name="OPTARG"),+ case("getopts-implicit-index", "getopts 'a:' unrelated -a fixture-new", True, name="OPTIND"),+ case("unset-array-element", "unset 'OPENAI_API_KEY[0]'", True),+ case("unset-function-data", "unset -f OPENAI_API_KEY", False),+ case("command-query-read", "command -v read OPENAI_API_KEY", False),+ case("command-query-printf", "command -V printf -v OPENAI_API_KEY '%s' fixture-new", False),+ case("command-writer-read", "command read OPENAI_API_KEY", True),+ case("declare-query-data", "declare -p OPENAI_API_KEY=fixture-new", False),+ case("declare-function-data", "declare -f OPENAI_API_KEY=fixture-new", False),+ case("declare-value", "declare -x OPENAI_API_KEY=fixture-new", True),+ case("export-no-value", "export OPENAI_API_KEY", False),+ case("env-split-string", "env -S 'OPENAI_API_KEY=fixture-new unrelated'", True),+ case("env-attached-split-string", "env -S'OPENAI_API_KEY=fixture-new unrelated'", True),+ case("env-assignment", "env OPENAI_API_KEY=fixture-new unrelated", True),+ case("env-option-data", "env -u OPENAI_API_KEY unrelated", False),+ case("echo-quoted-data", "echo 'printf -vOPENAI_API_KEY %s fixture-new'", False),+ case("include-ignored", "source ~/.other", False),+ case("mapfile-bundle-control", "mapfile -tn1 OPENAI_API_KEY", True),+ case("mapfile-callback-data", "mapfile -C 'echo OPENAI_API_KEY' unrelated", False),+ case("printf-percent-n", "printf '%n' OPENAI_API_KEY", True),+ case("printf-percent-n-second", "printf '%s%n' text OPENAI_API_KEY", True),+ case("printf-percent-n-other", "printf '%s%n' OPENAI_API_KEY unrelated", False),+ case("printf-percent-literal", "printf '%%n' OPENAI_API_KEY", False),+ case("printf-last-v-writer", "printf -vunrelated -vOPENAI_API_KEY '%s' fixture-new", True),+ case("printf-last-v-data", "printf -v OPENAI_API_KEY -v unrelated '%s' fixture-new", False),+ case("read-last-a-data", "read -a OPENAI_API_KEY -a unrelated", False),+ case("read-last-a-writer", "read -a unrelated -raOPENAI_API_KEY", True),+ case("eval-compact", "eval 'printf -vOPENAI_API_KEY %s fixture-new'", True),+ case("callback-compact", "mapfile -C 'printf -vOPENAI_API_KEY %s fixture-new' unrelated", True),+ case("function-compact", "f() { printf -vOPENAI_API_KEY '%s' fixture-new; }", True),+ case("conditional-compact", "if true; then read -raOPENAI_API_KEY; fi", True),+ case("zsh-read-p-destination", "read -p OPENAI_API_KEY", True, path=".zshrc"),+ case("zsh-read-question-destination", "read 'OPENAI_API_KEY?prompt'", True, path=".zshrc"),+ case("zsh-read-t-optional", "read -t OPENAI_API_KEY", True, path=".zshrc"),+ case("zsh-read-e-display", "read -e OPENAI_API_KEY", False, path=".zshrc"),+ case("export-p-assignment", "export -p OPENAI_API_KEY=fixture-new", True),+ case("export-f-function", "export -f OPENAI_API_KEY=fixture-new", False),+ case("typeset-p-query", "typeset -p OPENAI_API_KEY=fixture-new", False),+ case("declare-plain-attribute", "declare -x OPENAI_API_KEY", False),+ case("mapfile-default-other", "mapfile -tn1 unrelated", False),+ case("arithmetic-control", "let 'OPENAI_API_KEY=1'", True),+ case("loop-control", "for OPENAI_API_KEY in fixture; do :; done", True),+ case("declare-integer-rhs", "declare -i unrelated='OPENAI_API_KEY=1'", True),+ case("typeset-integer-rhs", "typeset -i unrelated='OPENAI_API_KEY=1'", True),+ case("declare-string-rhs", "declare unrelated='OPENAI_API_KEY=1'", False),+ case("declare-integer-query", "declare -pi unrelated='OPENAI_API_KEY=1'", False),+ case("declare-array-index", "declare 'unrelated[OPENAI_API_KEY=1]=x'", True),+ case("read-array-index", "read 'unrelated[OPENAI_API_KEY=1]'", True),+ case("printf-array-index", "printf -v 'unrelated[OPENAI_API_KEY=1]' '%s' x", True),+ case("echo-array-index-data", "echo 'unrelated[OPENAI_API_KEY=1]'", False),+ # Option arity, end markers and command-specific output/query modes.+ case("printf-quoted-option", "printf '-vOPENAI_API_KEY' '%s' x", True),+ case("printf-double-dash", "printf -vOPENAI_API_KEY -- '%s' x", True),+ case("printf-no-format", "printf -vOPENAI_API_KEY", False),+ case("printf-empty-format", "printf -vOPENAI_API_KEY ''", True),+ case("read-endoptions", "read -r -- OPENAI_API_KEY", True),+ case("read-attached-prompt", "read -rpOPENAI_API_KEY unrelated", False),+ case("read-combined-initial", "read -rei OPENAI_API_KEY unrelated", False),+ case("read-combined-delimiter", "read -rd OPENAI_API_KEY unrelated", False),+ case("read-attached-fd", "read -ru0 OPENAI_API_KEY", True),+ case("read-readline", "read -e OPENAI_API_KEY", True, path=".bashrc"),+ case("read-zero-timeout", "read -rt0 OPENAI_API_KEY", False, path=".bashrc"),+ case("getopts-argv-data", "getopts 'x:' unrelated -x OPENAI_API_KEY", False),+ case("getopts-no-name", "getopts OPENAI_API_KEY", False),+ case("unset-variable", "unset -v -- OPENAI_API_KEY", True),+ case("unset-function-array-data", "unset -f 'unrelated[OPENAI_API_KEY=1]'", False),+ case("command-combined-query", "command -pv printf -vOPENAI_API_KEY %s x", False),+ case("command-exec-options", "command -p -- printf -vOPENAI_API_KEY %s x", True),+ case("builtin-exec-options", "builtin -- printf -vOPENAI_API_KEY %s x", True),+ case("query-still-has-expansions", 'command -v "$(printf -vOPENAI_API_KEY %s x)"', True),+ case("declaration-combined-query", "declare -px OPENAI_API_KEY=fixture-new", False),+ case("declaration-functions-query", "declare -F OPENAI_API_KEY=fixture-new", False),+ case("declaration-endoptions", "declare -x -- 'OPENAI_API_KEY=fixture-new'", True),+ case("declaration-disable-integer", "declare -i +i unrelated='OPENAI_API_KEY=1'", False),+ case("declaration-combined-integer", "declare -xi unrelated='OPENAI_API_KEY=1'", True),+ case("readonly-p-assignment", "readonly -p OPENAI_API_KEY=fixture-new", True),+ case("readonly-function-data", "readonly -f OPENAI_API_KEY=fixture-new", False),+ case("local-integer", "f() { local -i unrelated='OPENAI_API_KEY=1'; }", True),+ # The format decoder locates arguments; it never renders or evaluates them.+ case("printf-width-slot", "printf '%*s%n' 8 text OPENAI_API_KEY", True),+ case("printf-width-data", "printf '%*s%n' 8 OPENAI_API_KEY unrelated", False),+ case("printf-precision-slot", "printf '%.*s%n' 8 text OPENAI_API_KEY", True),+ case("printf-reused-format", "printf '%s%n' first other second OPENAI_API_KEY", True),+ case("printf-n-and-v", "printf -vunrelated '%n' OPENAI_API_KEY", True),+ case("printf-time-data", "printf '%(%n)T' OPENAI_API_KEY", False),+ case("printf-invalid-format", "printf '%T%n' data OPENAI_API_KEY", False),+ case("printf-dynamic-format", 'printf "$FORMAT" OPENAI_API_KEY', True),+ case("printf-bash-numeric-data", "printf '%d' 'OPENAI_API_KEY=1'", False, path=".bashrc"),+ case("printf-zsh-numeric-expression", "printf '%d' 'OPENAI_API_KEY=1'", True, path=".zshrc"),+ case("printf-zsh-character-data", """printf '%d' "'OPENAI_API_KEY=1" """, False, path=".zshrc"),+ case("printf-zsh-positional", "printf '%2$n%1$s' text OPENAI_API_KEY", True, path=".zshrc"),+ case("printf-zsh-positional-data", "printf '%2$n%1$s' OPENAI_API_KEY unrelated", False, path=".zshrc"),+ case("printf-zsh-positional-width", "printf '%2$*1$s%3$n' 2 text OPENAI_API_KEY", True, path=".zshrc"),+ # env -S has an argv grammar, not shell grammar. Includes are still ignored.+ case("env-long-split", "env --split-string='OPENAI_API_KEY=fixture-new other'", True),+ case("env-bundle-split", "env -iS'OPENAI_API_KEY=fixture-new other'", True),+ case("env-split-command-data", "env -S 'echo OPENAI_API_KEY=fixture-new'", False),+ case("env-split-semicolon-data", "env -S 'echo; OPENAI_API_KEY=fixture-new'", False),+ case("env-split-comment-data", "env -S '# OPENAI_API_KEY=fixture-new'", False),+ case("env-split-embedded-hash", "env -S 'OTHER=x#x OPENAI_API_KEY=fixture-new other'", True),+ case("env-split-underscore", r"env -S 'OTHER=x\_OPENAI_API_KEY=fixture-new\_other'", True),+ case("env-split-stop", r"env -S 'echo\c OPENAI_API_KEY=fixture-new'", False),+ case("env-split-symbolic-empty", "env -S '${MAYBE_EMPTY} OPENAI_API_KEY=fixture-new other'", True),+ case("env-split-symbolic-name", "env -S '${PREFIX}_API_KEY=fixture-new other'", False),+ case("env-split-array-data", "env -S 'other[OPENAI_API_KEY=1]=x command'", False),+ case("env-nonshell-name-then-assignment", "env other-name=x OPENAI_API_KEY=fixture-new command", True),+ case("env-argv0-data", "env -a OPENAI_API_KEY=fixture-new command", False),+ case("env-chdir-data", "env -C OPENAI_API_KEY=fixture-new command", False),+ case("env-split-chdir-data", "env -S '-C OPENAI_API_KEY=fixture-new command'", False),+ case("env-two-splits-after-command", "env -S echo -S 'OPENAI_API_KEY=fixture-new command'", False),+ case("env-two-splits-before-command", "env -S '-i' -S 'OPENAI_API_KEY=fixture-new command'", True),+ # Default targets and callbacks never cause unrelated variables to be claimed.+ case("mapfile-implicit", "mapfile -tn1", True, name="MAPFILE"),+ case("readarray-endoptions", "readarray -tdx -- OPENAI_API_KEY", True),+ case("mapfile-delimiter-data", "mapfile -d OPENAI_API_KEY unrelated", False),+ case("mapfile-overridden-callback", "mapfile -C 'OPENAI_API_KEY=1' -C echo unrelated", False),+ case("mapfile-final-callback", "mapfile -C echo -C 'printf -vOPENAI_API_KEY %s x' unrelated", True),+ case("mapfile-default-other-name", "mapfile -t", False),+ # Fixed Zsh paths select Zsh argument roles even if the ambient shell differs.+ case("zsh-read-p", "read -p OPENAI_API_KEY", True, path=".zshenv"),+ case("zsh-read-t", "read -t OPENAI_API_KEY", True, path=".zprofile"),+ case("zsh-read-prompt", "read 'OPENAI_API_KEY?prompt'", True, path=".zlogin"),+ case("zsh-read-prompt-data", "read 'unrelated?OPENAI_API_KEY'", False, path=".zshrc"),+ case("zsh-read-prompt-default", "read '?prompt'", True, name="REPLY", path=".zshrc"),+ case("zsh-read-array-default", "read -A", True, name="reply", path=".zshrc"),+ case("zsh-read-array-ignores-rest", "read -A unrelated OPENAI_API_KEY", False, path=".zshrc"),+ case("zsh-read-echo-and-assign", "read -E OPENAI_API_KEY", True, path=".zshrc"),+ case("zsh-read-optional-bundle", "read -rk OPENAI_API_KEY", True, path=".zshrc"),+ case("zsh-declare-float", "typeset -F2 other='OPENAI_API_KEY=1'", True, path=".zshrc"),+ case("zsh-declare-query", "typeset -p1 OPENAI_API_KEY=fixture-new", False, path=".zshrc"),+ case("bash-login-read-prompt", "read -p OPENAI_API_KEY unrelated", False, path=".bash_login"),+ case("bash-profile-read-prompt", "read -p OPENAI_API_KEY unrelated", False, path=".bash_profile"),+]++[email protected](autouse=True)+def forbid_process_inventory(home, monkeypatch):+ def forbidden(*args, **kwargs):+ raise AssertionError("real process inventory reached")++ monkeypatch.setattr(psutil, "process_iter", forbidden)+ monkeypatch.setattr(psutil, "pids", forbidden)+++def seed(home, filename, name, line):+ path = home / filename+ before = (f"# 保留偏好\r\nexport {name}=fixture-literal\r\n" + line).encode()+ path.write_bytes(before)+ if name != KEY:+ config = home / ".codex/config.toml"+ config.parent.mkdir(parents=True, exist_ok=True)+ config.write_text(f'[model_providers.fixture]\nenv_key = "{name}"\n')+ return path, before++[email protected]("filename,name,line,writer", CASES)+def test_builtin_roles_guard_exact_cleanup(home, filename, name, line, writer):+ path, before = seed(home, filename, name, line)+ profile = next(p for p in read_shell_profiles(home, frozenset({name})) if p.path == path)+ assert len(profile.assignments) == 1+ if writer:+ assert profile.values == {}+ assert any(issue.names == (name,) and issue.reason == "dynamic_shell" for issue in profile.issues)+ with pytest.raises(TakeoverStateError):+ cleanup_shell_profile(profile, {name: "fixture-literal"})+ else:+ assert profile.values == {name: "fixture-literal"}+ assert not profile.issues+ edit = NativeFileEdit.from_payload(cleanup_shell_profile(profile, profile.values).to_payload())+ edit.apply()+ assert path.read_bytes() == ("# 保留偏好\r\n" + line).encode()+ edit.apply(reverse=True)+ assert path.read_bytes() == before++[email protected]("filename,name,line,writer", CASES)+def test_builtin_roles_reach_scan_and_apply(home, tmp_path, filename, name, line, writer):+ path, before = seed(home, filename, name, line)+ service, store, adapter = _service(tmp_path, migration_home=home)+ [row] = service.migration_scan()["items"]+ if writer:+ assert row["notes_key"] == "settings.models.migration.blocked.dynamic_shell"+ assert row["proposed_action"] == "reauth"+ assert not row["selected"]+ with pytest.raises(ModelHubError):+ asyncio.run(service.migration_apply([row["id"]]))+ assert path.read_bytes() == before+ assert not adapter.provisioned and not adapter.oauth_provisioned and not adapter.transient_refs+ assert not store.config.sources+ else:+ assert row["proposed_action"] == "import"+ assert asyncio.run(service.migration_apply([row["id"]]))["applied"] == 1+ assert len(adapter.provisioned) == len(store.config.sources) == 1+ assert path.read_bytes() == ("# 保留偏好\r\n" + line).encode()+++def test_profile_dialect_never_comes_from_runtime_shell(home, monkeypatch):+ import os++ monkeypatch.setenv("SHELL", "/fixture/not-a-shell")+ original_get = os.environ.get++ def guarded(name, *args):+ assert name not in {"SHELL", KEY}, "runtime shell/credential read"+ return original_get(name, *args)++ monkeypatch.setattr(os.environ, "get", guarded)+ (home / ".bashrc").write_text("read -p OPENAI_API_KEY unrelated\n")+ (home / ".zshrc").write_text("read -p OPENAI_API_KEY\n")+ profiles = {p.path.name: p for p in read_shell_profiles(home, frozenset({KEY}))}+ assert profiles[".bashrc"].issues == ()+ assert profiles[".zshrc"].issues[0].names == (KEY,)++[email protected]("writer", [+ "printf -vOPENAI_API_KEY %s value",+ "read -raOPENAI_API_KEY",+ "printf '%n' OPENAI_API_KEY",+ "declare -i unrelated='OPENAI_API_KEY=1'",+ "env -S 'OPENAI_API_KEY=fixture-new command'",+ 'read "-$FLAGS" OPENAI_API_KEY',+ 'printf "$FLAGS" OPENAI_API_KEY %s new',+ 'declare "$FLAGS" other="OPENAI_API_KEY=1"',+])+def test_unrelated_consented_backend_can_still_migrate(home, tmp_path, writer):+ path = home / ".profile"+ kept = "export OPENAI_API_KEY=fixture-codex\n" + writer + "\n"+ path.write_text(kept + "export ANTHROPIC_API_KEY=fixture-claude\n")+ service, store, adapter = _service(tmp_path, migration_home=home)+ rows = {row["backend"]: row for row in service.migration_scan()["items"]}+ assert rows["codex"]["notes_key"].endswith(".dynamic_shell")+ assert rows["claude"]["proposed_action"] == "import"+ result = asyncio.run(service.migration_apply([rows["claude"]["id"]]))+ assert result["applied"] == 1+ assert len(adapter.provisioned) == len(store.config.sources) == 1+ assert path.read_text() == kept++[email protected]("command", ["read", "mapfile"])[email protected]("count", [4, 6, 8, 10, 20, 40])[email protected]("writer", [False, True])+def test_dynamic_option_states_keep_only_writer_roles(monkeypatch, command, count, writer):+ from core.handlers.model_hub import migration_shell as shell++ original = shell._options+ calls = 0++ def counted(*args, **kwargs):+ nonlocal calls+ calls += 1+ # A deterministic quadratic ceiling, including the negative case in+ # which no writer result can short-circuit. No production budget exists.+ assert calls <= (count + 2) ** 2+ return original(*args, **kwargs)++ monkeypatch.setattr(shell, "_options", counted)+ arguments = " ".join(f'"-$FLAGS{index}"' for index in range(count))+ target = KEY if writer else "unrelated"+ assert shell._written_names(f"{command} {arguments} {target}", frozenset({KEY})) == (+ {KEY} if writer else set()+ )++[email protected]("command", ["read", "mapfile", "printf"])+def test_dynamic_option_complete_result_short_circuits(monkeypatch, command):+ from core.handlers.model_hub import migration_shell as shell++ original = shell._options+ calls = 0++ def counted(*args, **kwargs):+ nonlocal calls+ calls += 1+ assert calls <= 2+ return original(*args, **kwargs)++ monkeypatch.setattr(shell, "_options", counted)+ line = f'{command} "$FLAGS" {KEY} %s new'+ assert shell._written_names(line, frozenset({KEY})) == {KEY}+++def test_command_wrappers_keep_argv_roles_without_recursion():+ from core.handlers.model_hub import migration_shell as shell++ prefix = "command " * 1100+ assert shell._written_names(prefix + f"{KEY}=new", frozenset({KEY})) == set()+ assert shell._written_names(prefix + f"read {KEY}", frozenset({KEY})) == {KEY}diff --git a/tests/test_native_oauth_store.py b/tests/test_native_oauth_store.pyindex 4c9f3a7a9..16b410dae 100644--- a/tests/test_native_oauth_store.py+++ b/tests/test_native_oauth_store.py@@ -824,7 +824,8 @@ def test_claude_legacy_snapshots_keep_selection_and_coalesced_file_guards(assert expanded[0].proposed_action == "import"assert expanded[0].oauth_material["refresh_token"] == "refresh-secret"edits = plan_native_cleanup(expanded, home=tmp_path, project_roots=())- assert {edit.path for edit in edits} == {primary, legacy}+ assert {primary, legacy} <= {edit.path for edit in edits}+ assert all(edit.before == edit.after for edit in edits if edit.path not in {primary, legacy})[guard] = [edit for edit in edits if edit.path == primary]assert guard.before is guard.after is Nonefor edit in edits:@@ -988,6 +989,50 @@ def test_claude_noncredential_keychain_without_file_oauth_is_not_exportable(assert fake_keychain.write_calls == fake_keychain.delete_calls == [][email protected]("first_grant,other_source", [(True, False), (False, False), (False, True)])+def test_completed_metadata_revision_does_not_prove_no_prior_oauth(+ tmp_path: Path, fake_keychain: FakeKeychain, monkeypatch: pytest.MonkeyPatch,+ first_grant: bool, other_source: bool,+) -> None:+ from core.handlers.model_hub.service import ModelHubError+ from tests.scenarios.model_hub.test_model_hub_migration_scenarios import (+ _isolate_native_home, _service, _write,+ )++ home = tmp_path / "native"+ _isolate_native_home(monkeypatch, home)+ monkeypatch.setenv("USER", "fixture-user")+ locator = ("Claude Code-credentials", "fixture-user")+ oauth = json.dumps(_claude_payload())+ empty = json.dumps({"mcpOAuth": _claude_payload()["mcpOAuth"]})+ fake_keychain.items[locator] = (oauth if first_grant else empty, "fixture-initial")+ if other_source:+ _write(home / ".config/opencode/opencode.json",+ '{"provider":{"openai":{"options":{"apiKey":"fixture-other"}}}}')+ service, config_store, adapter = _service(tmp_path, migration_home=home)+ rows = service.migration_scan()["items"]+ asyncio.run(service.migration_apply([row["id"] for row in rows]))+ assert "claude" in service.migration_journal.completed()["clean_native_stores"]+ provisions = len(adapter.oauth_provisioned)+ fake_keychain.items[locator] = (oauth, "fixture-restored")+ fake_keychain.mdates[locator] += 1+ before = config_store.config.to_payload()+ rows = service.migration_scan()["items"]+ if first_grant or other_source:+ # A mixed bundle cannot prove that this opaque container was empty.+ with pytest.raises(ModelHubError) as failure:+ asyncio.run(service.migration_apply([row["id"] for row in rows]))+ assert failure.value.code == "migration_reauthorization_required"+ assert len(adapter.oauth_provisioned) == provisions+ assert fake_keychain.items[locator] == (oauth, "fixture-restored")+ assert config_store.config.to_payload() == before+ else:+ # An entirely source-free confirmation transferred no old OAuth.+ asyncio.run(service.migration_apply([row["id"] for row in rows]))+ assert len(adapter.oauth_provisioned) == 1+ assert service.migration_journal.load() is None++def test_codex_file_scan_is_exportable_and_cleanup_preserves_unrelated_fields(tmp_path: Path) -> None:auth_path = tmp_path / ".codex" / "auth.json"auth_path.parent.mkdir()diff --git a/tests/test_native_process_inventory.py b/tests/test_native_process_inventory.pyindex 81ae2a8cb..0cd610c89 100644--- a/tests/test_native_process_inventory.py+++ b/tests/test_native_process_inventory.py@@ -152,7 +152,9 @@ def test_real_guard_blocks_wrapped_cli_before_takeover(monkeypatch, tmp_path, baif backend != "opencode":native.auth_mode = "api_key"native.api_key = "fixture-native-key"- native.base_url = "https://fixture.example"+ # Reach the process guard with a supported native transport. Custom+ # Anthropic API-key headers are refused before takeover begins.+ native.base_url = "https://api.anthropic.com" if backend == "claude" else "https://fixture.example"else:auth = Path.home() / ".config/opencode/opencode.json"auth.parent.mkdir(parents=True)@@ -177,8 +179,9 @@ def test_real_guard_blocks_wrapped_cli_before_takeover(monkeypatch, tmp_path, bamonkeypatch.setattr(psutil, "process_iter", inventory)before = paths.get_config_path().read_bytes()- ids = [row["id"] for row in service.migration_scan()["items"] if row["backend"] == backend]- assert ids+ rows = [row for row in service.migration_scan()["items"] if row["backend"] == backend]+ assert rows and all(row["proposed_action"] == "import" for row in rows)+ ids = [row["id"] for row in rows]with pytest.raises(ModelHubError) as error:asyncio.run(service.migration_apply(ids))assert error.value.code == "migration_native_busy"diff --git a/ui/src/components/settings/models/MigrationDialog.test.tsx b/ui/src/components/settings/models/MigrationDialog.test.tsxindex 4ec7cfdb8..98e26bcfe 100644--- a/ui/src/components/settings/models/MigrationDialog.test.tsx+++ b/ui/src/components/settings/models/MigrationDialog.test.tsx@@ -114,7 +114,7 @@ describe('MigrationDialog — the settings default', () => {await waitFor(() => expect(within(dialog).getByText('OpenAI')).toBeTruthy());expect(within(dialog).getByText(/Claude 账号登录/)).toBeTruthy();expect((within(dialog).getByRole('checkbox', { name: /sk-ant-…4b7e/ }) as HTMLButtonElement).disabled).toBe(true);- expect(within(dialog).getByText('Use the existing Add flow for this CLI, then scan again.')).toBeTruthy();+ expect(within(dialog).getByText('This credential cannot be imported. Use the existing Add flow in Model Hub to add this source manually.')).toBeTruthy();expect((within(dialog).getByRole('button', { name: 'Start migration' }) as HTMLButtonElement).disabled).toBe(false);});@@ -238,8 +238,9 @@ describe('MigrationDialog — the settings default', () => {['migration_native_busy', 'Close the CLI or finish its tasks, then retry.'],['migration_permission_needed', 'Allow credential access, then retry.'],['migration_recovery_pending', 'Migration is unfinished. Retry to continue.'],- ['migration_item_conflict', 'The native configuration changed. Scan again.'],+ ['migration_item_conflict', 'Migration could not verify the saved configuration or its credentials. Check the files and authentication before retrying, or add the source manually in Model Hub.'],['migration_configuration_blocked', 'Adjust the native configuration, then scan again.'],+ ['migration_reauthorization_required', 'This backend was already migrated, and the changed native login cannot be verified as a new authorization. The original files are unchanged. Sign in again for the existing source in Model Hub instead of importing this login.'],] as const)('maps %s to concise localized copy', async (code, message) => {vi.spyOn(modelsApi, 'scanMigration').mockResolvedValue({ items: [{ ...CODEX_KEY }] });vi.spyOn(modelsApi, 'applyMigration').mockRejectedValue(new ApiCallError(code));@@ -253,6 +254,27 @@ describe('MigrationDialog — the settings default', () => {await waitFor(() => expect(showToast).toHaveBeenCalledWith(message, 'error'));});+ it('keeps selection and explains configuration or credential verification failure in Chinese', async () => {+ await i18n.changeLanguage('zh');+ const onClose = vi.fn();+ vi.spyOn(modelsApi, 'scanMigration').mockResolvedValue({ items: [{ ...CODEX_KEY }] });+ vi.spyOn(modelsApi, 'applyMigration').mockRejectedValue(new ApiCallError('migration_item_conflict'));+ renderDialog({ onClose });+ const user = userEvent.setup();++ const dialog = await screen.findByRole('dialog');+ await within(dialog).findByText('OpenAI');+ await user.click(within(dialog).getByRole('button', { name: '开始迁移' }));++ await waitFor(() => expect(showToast).toHaveBeenCalledWith(+ '无法验证已保存的配置或凭据。请检查配置文件和认证是否有效后重试,或在模型网关中手动添加供应商。',+ 'error',+ ));+ expect(onClose).not.toHaveBeenCalled();+ expect(within(dialog).getByRole('checkbox').getAttribute('aria-checked')).toBe('true');+ expect((within(dialog).getByRole('button', { name: '开始迁移' }) as HTMLButtonElement).disabled).toBe(false);+ });+it.each([['en', 'Authentication has expired. Sign in again in Model Hub.'],['zh', '认证已过期,请在模型网关中重新登录。'],@@ -313,10 +335,10 @@ describe('MigrationDialog — the settings default', () => {});it.each([- ['settings.models.migration.blocked.config', "Adjust this CLI's configuration, then scan again."],- ['settings.models.migration.blocked.environment', 'Remove this CLI’s environment-based credentials, then retry.'],- ['settings.models.migration.blocked.credential', 'Allow credential access, then retry.'],- ['settings.models.migration.blocked.unknown', 'Use the existing Add flow for this CLI, then scan again.'],+ ['settings.models.migration.blocked.config', "Check this CLI's saved configuration, then scan again."],+ ['settings.models.migration.blocked.environment', 'This environment-based authentication cannot be imported. Save the key in a supported configuration file, then scan again.'],+ ['settings.models.migration.blocked.credential', 'This credential could not be read or is not supported. Check credential access and the saved authentication format, then scan again.'],+ ['settings.models.migration.blocked.unknown', 'This credential cannot be imported. Use the existing Add flow in Model Hub to add this source manually.'],] as const)('maps blocked note %s to actionable copy', async (notes_key, message) => {const blocked = { ...REAUTH, notes_key };serve([CODEX_KEY, blocked]);@@ -328,11 +350,11 @@ describe('MigrationDialog — the settings default', () => {});});-describe('MigrationDialog — approved copy', () => {+describe('MigrationDialog — persisted authentication', () => {it.each([- ['en', 'After migration, CLI authentication will be fully managed by Model Hub.'],- ['zh', '迁移后,CLI的认证信息将完全交由模型网关管理'],- ] as const)('renders the %s consequence sentence exactly', async (language, sentence) => {+ ['en', 'Import authentication saved in configuration files, shell startup files, or supported credential stores, without reading runtime environment values. After migration, Model Hub will manage the selected authentication.'],+ ['zh', '导入配置文件、Shell 启动文件或受支持凭据库中已保存的认证,不读取运行中的环境变量。迁移后,所选认证由模型网关管理。'],+ ] as const)('explains the saved source boundary and consequence in %s', async (language, sentence) => {await i18n.changeLanguage(language);serve([CODEX_KEY]);renderDialog();@@ -340,6 +362,153 @@ describe('MigrationDialog — approved copy', () => {const dialog = await screen.findByRole('dialog');expect(within(dialog).getByText(sentence)).toBeTruthy();});++ const blockedReasons = [+ {+ reason: 'dynamic_shell',+ path: '/home/用户/.zshrc',+ en: /expressions or commands.*literal values.*never executes shell code/,+ zh: /表达式或命令.*固定值.*不会执行 Shell 代码/,+ },+ {+ reason: 'ambiguous_shell',+ path: '/home/用户/.profile',+ en: /Conflicting saved assignments.*listed files.*unambiguous literal value/,+ zh: /列出的文件.*互相冲突.*固定值.*统一相关配置/,+ },+ {+ reason: 'unreadable',+ path: '/home/用户/.bashrc',+ en: /source file could not be read.*Avibe has read access/,+ zh: /无法读取此配置文件.*Avibe 是否有读取权限/,+ },+ {+ reason: 'reference',+ path: '/home/用户/.claude/settings.json',+ en: /referenced variable.*no unambiguous saved literal value.*save.*key.*listed configuration or shell startup file/,+ zh: /引用的变量.*固定值.*列出的配置文件或 Shell 启动文件.*保存对应的密钥/,+ },+ {+ reason: 'helper',+ path: '/home/用户/.claude/settings.local.json',+ en: /supplied by a command.*supported static configuration.*add the source manually/,+ zh: /由命令提供.*静态配置.*手动添加供应商/,+ },+ {+ reason: 'token',+ path: '/home/用户/.zshenv',+ en: /login token or authentication format cannot be imported.*Add a source.*sign in again/,+ zh: /登录令牌或认证格式暂不支持导入.*添加供应商.*重新登录/,+ },+ {+ reason: 'headers',+ path: '/home/用户/.zprofile',+ en: /Custom credential headers cannot be imported.*supported authentication method/,+ zh: /自定义认证请求头.*受支持的认证方式/,+ },+ {+ reason: 'transport',+ path: '/home/用户/.bashrc',+ en: /cannot send.*saved authentication in the same way.*original files are unchanged.*permissions will not fix/,+ zh: /无法按此接口原有的方式发送认证.*原配置文件未改动.*修改文件权限不能解决/,+ },+ ] as const;++ it.each(['en', 'zh'] as const)('shows every blocked file and its specific %s remedy while preserving backend selection', async (language) => {+ await i18n.changeLanguage(language);+ serve([+ ...blockedReasons.map(({ reason, path }) => ({+ ...REAUTH,+ id: `blocked_${reason}`,+ notes_key: `settings.models.migration.blocked.${reason}`,+ source_paths: [path],+ })),+ SUBSCRIPTION,+ { ...CODEX_KEY, source_paths: ['/home/用户/.codex/auth.json'] },+ ]);+ renderDialog();+ const user = userEvent.setup();++ const dialog = await screen.findByRole('dialog');+ const status = await within(dialog).findByRole('status');+ const warnings = within(status).getAllByRole('listitem');+ expect(warnings).toHaveLength(blockedReasons.length);+ for (const { path, ...reason } of blockedReasons) {+ const warning = warnings.find((element) => (+ within(element).queryByText(path) && within(element).queryByText(reason[language])+ ));+ expect(warning).toBeTruthy();+ expect(within(warning!).getByText(reason[language])).toBeTruthy();+ }+ expect(status.textContent).not.toMatch(/Allow credential access|请允许访问凭据|Remove this CLI|清除.*环境变量/);+ for (const row of within(dialog).getAllByRole('checkbox', { name: /sk-ant-…4b7e|Claude 账号登录/ })) {+ expect((row as HTMLButtonElement).disabled).toBe(true);+ expect(row.getAttribute('aria-checked')).toBe('false');+ }+ const supportedRow = within(dialog).getByRole('checkbox', { name: /OpenAI.*\/home\/用户\/\.codex\/auth\.json/ });+ expect((supportedRow as HTMLButtonElement).disabled).toBe(false);+ expect(supportedRow.getAttribute('aria-checked')).toBe('true');+ await user.click(within(dialog).getByRole('button', { name: language === 'en' ? 'Start migration' : '开始迁移' }));+ await waitFor(() => expect(applied).toEqual([[CODEX_KEY.id]]));+ });++ it('deduplicates only matching source/reason pairs, keeping different files and reasons', async () => {+ const dynamic: MigrationItem = {+ ...REAUTH,+ notes_key: 'settings.models.migration.blocked.dynamic_shell',+ source_paths: ['/home/用户/.zshrc', '/home/用户/.profile', '/home/用户/.zshrc'],+ };+ serve([+ dynamic,+ { ...dynamic, id: 'duplicate', source_paths: ['/home/用户/.zshrc'] },+ { ...dynamic, id: 'different_reason', notes_key: 'settings.models.migration.blocked.reference', source_paths: ['/home/用户/.zshrc'] },+ ]);+ renderDialog();++ const dialog = await screen.findByRole('dialog');+ const status = await within(dialog).findByRole('status');+ const warnings = within(status).getAllByRole('listitem');+ expect(warnings).toHaveLength(3);+ expect(within(status).getAllByText('/home/用户/.zshrc')).toHaveLength(2);+ expect(within(status).getAllByText('/home/用户/.profile')).toHaveLength(1);+ expect(within(status).getAllByText(/expressions or commands/)).toHaveLength(2);+ expect(within(status).getAllByText(/referenced variable/)).toHaveLength(1);+ expect((within(dialog).getByRole('button', { name: 'Start migration' }) as HTMLButtonElement).disabled).toBe(true);+ });++ it('keeps distinct blocked legacy rows visible without inventing file locators', async () => {+ serve([+ { ...REAUTH, notes_key: 'settings.models.migration.blocked.credential' },+ { ...REAUTH, id: 'legacy_second', masked_detail: 'sk-ant-…5678', notes_key: 'settings.models.migration.blocked.environment' },+ ]);+ renderDialog();++ const dialog = await screen.findByRole('dialog');+ const status = await within(dialog).findByRole('status');+ const warnings = within(status).getAllByRole('listitem');+ expect(warnings).toHaveLength(2);+ expect(within(warnings[0]).getByText('Claude Code configuration')).toBeTruthy();+ expect(within(warnings[0]).getByText(/could not be read or is not supported/)).toBeTruthy();+ expect(within(warnings[1]).getByText('Claude Code configuration')).toBeTruthy();+ expect(within(warnings[1]).getByText(/Save the key in a supported configuration file/)).toBeTruthy();+ expect(status.textContent).not.toMatch(/\.json|\.zshrc|\.profile/);+ });++ it.each([+ ['en', 'No configuration found to migrate.', 'Start migration'],+ ['zh', '没有检测到可迁移的配置。', '开始迁移'],+ ] as const)('renders an empty %s scan without fake credential rows or warnings', async (language, emptyMessage, action) => {+ await i18n.changeLanguage(language);+ serve([]);+ renderDialog();++ const dialog = await screen.findByRole('dialog');+ expect(await within(dialog).findByText(emptyMessage)).toBeTruthy();+ expect(within(dialog).queryAllByRole('checkbox')).toHaveLength(0);+ expect(within(dialog).queryAllByRole('status')).toHaveLength(0);+ expect((within(dialog).getByRole('button', { name: action }) as HTMLButtonElement).disabled).toBe(true);+ expect(modelsApi.applyMigration).not.toHaveBeenCalled();+ });});describe('MigrationDialog — how a row names itself', () => {@@ -379,4 +548,168 @@ describe('MigrationDialog — how a row names itself', () => {expect(within(dialog).getByText('sk-…3456 · OpenCode configuration')).toBeTruthy();expect(within(dialog).queryByText(/auth\.json|opencode\.json/)).toBeNull();});++ it('shows each explicit file locator for an importable source', async () => {+ const paths = [+ '/home/用户/配置 文件夹/very-long-project-name/.config/opencode/opencode.json',+ '/home/用户/.zshrc',+ ];+ serve([{ ...LEGACY, source_paths: [...paths, paths[0]] }]);+ renderDialog();++ const dialog = await screen.findByRole('dialog');+ const checkbox = await within(dialog).findByRole('checkbox', {+ name: '自建中转 · sk-…abcd · ' + paths.join(' · '),+ });+ expect((checkbox as HTMLButtonElement).disabled).toBe(false);+ expect(checkbox.getAttribute('aria-checked')).toBe('true');+ for (const path of paths) {+ expect(within(dialog).getAllByText(path)).toHaveLength(1);+ }+ expect(within(dialog).queryByText('OpenCode configuration')).toBeNull();+ });++ it('uses the legacy source description when file metadata is empty', async () => {+ serve([{ ...CODEX_KEY, source_paths: [] }]);+ renderDialog();++ const dialog = await screen.findByRole('dialog');+ expect(await within(dialog).findByText('sk-…9f21 · Codex configuration')).toBeTruthy();+ expect(within(dialog).getByRole('checkbox', { name: 'OpenAI · sk-…9f21 · Codex configuration' })).toBeTruthy();+ });+});++describe('MigrationDialog — shared persisted files', () => {+ const linked: MigrationItem[] = [+ { ...CODEX_KEY, source_paths: ['/home/用户/.profile'], required_backends: ['codex', 'opencode'] },+ { ...LEGACY, source_paths: ['/home/用户/.profile'], required_backends: ['codex', 'opencode'] },+ {+ ...LEGACY,+ id: 'mig_opencode_second',+ masked_detail: 'Second source · sk-…5678',+ source_paths: ['/home/用户/.config/opencode/auth.json'],+ required_backends: ['codex', 'opencode'],+ },+ ];++ it.each(['en', 'zh'] as const)('expands entry-point scope and couples every row in the linked group in %s', async (language) => {+ await i18n.changeLanguage(language);+ serve([...linked, SUBSCRIPTION]);+ renderDialog({ eligible: (item) => item.id === CODEX_KEY.id });+ const user = userEvent.setup();++ const dialog = await screen.findByRole('dialog');+ await within(dialog).findByText('OpenAI');+ const checkboxes = within(dialog).getAllByRole('checkbox');+ expect(checkboxes).toHaveLength(3);+ expect(within(dialog).queryByText('Anthropic')).toBeNull();+ const sharedMessage = language === 'en'+ ? /share authentication saved in files.*must migrate together: Codex, OpenCode.*Changing one selection updates the others/+ : /共用文件中保存的认证.*需要一起迁移:Codex, OpenCode.*同时更新全部关联后端/;+ expect(within(dialog).getAllByText(sharedMessage)).toHaveLength(2);+ for (const checkbox of checkboxes) expect(checkbox.getAttribute('aria-checked')).toBe('true');++ const start = within(dialog).getByRole('button', { name: language === 'en' ? 'Start migration' : '开始迁移' });+ await user.click(checkboxes[0]);+ for (const checkbox of checkboxes) expect(checkbox.getAttribute('aria-checked')).toBe('false');+ expect((start as HTMLButtonElement).disabled).toBe(true);++ await user.click(checkboxes[2]);+ for (const checkbox of checkboxes) expect(checkbox.getAttribute('aria-checked')).toBe('true');+ await user.click(start);+ await waitFor(() => expect(applied).toEqual([linked.map((item) => item.id)]));+ });++ it('leaves the whole linked group unchecked when any consuming row was not selected', async () => {+ serve(linked.map((item, index) => ({ ...item, selected: index !== 1 })));+ renderDialog();+ const user = userEvent.setup();++ const dialog = await screen.findByRole('dialog');+ await within(dialog).findByText('OpenAI');+ const checkboxes = within(dialog).getAllByRole('checkbox');+ for (const checkbox of checkboxes) expect(checkbox.getAttribute('aria-checked')).toBe('false');+ const start = within(dialog).getByRole('button', { name: 'Start migration' });+ expect((start as HTMLButtonElement).disabled).toBe(true);++ await user.click(checkboxes[0]);+ await user.click(start);+ await waitFor(() => expect(applied).toEqual([linked.map((item) => item.id)]));+ });++ it('shows a linked backend blocker in each affected group, even outside the entry-point scope', async () => {+ serve([+ ...linked,+ {+ ...LEGACY,+ id: 'blocked_helper',+ proposed_action: 'keep_native',+ notes_key: 'settings.models.migration.blocked.helper',+ source_paths: ['/home/用户/.config/opencode/opencode.json'],+ required_backends: ['codex', 'opencode'],+ },+ SUBSCRIPTION,+ ]);+ renderDialog({ eligible: (item) => item.id === CODEX_KEY.id });++ const dialog = await screen.findByRole('dialog');+ await within(dialog).findByText('OpenAI');+ const warnings = within(dialog).getAllByRole('status');+ expect(warnings).toHaveLength(2);+ for (const warning of warnings) {+ expect(within(warning).getByText('/home/用户/.config/opencode/opencode.json')).toBeTruthy();+ expect(within(warning).getByText(/Credentials supplied by a command cannot be imported/)).toBeTruthy();+ }+ const checkboxes = within(dialog).getAllByRole('checkbox');+ expect(checkboxes).toHaveLength(4);+ for (const checkbox of checkboxes) {+ expect((checkbox as HTMLButtonElement).disabled).toBe(true);+ expect(checkbox.getAttribute('aria-checked')).toBe('false');+ }+ expect((within(dialog).getByRole('button', { name: 'Start migration' }) as HTMLButtonElement).disabled).toBe(true);+ expect(modelsApi.applyMigration).not.toHaveBeenCalled();+ });++ it('keeps a transitive three-backend closure visible and selected from one entry point', async () => {+ const items: MigrationItem[] = [SUBSCRIPTION, CODEX_KEY, LEGACY].map((item) => ({+ ...item,+ source_paths: item.backend === 'codex' ? ['/home/用户/.profile', '/home/用户/.zshrc'] : ['/home/用户/.profile'],+ required_backends: ['claude', 'codex', 'opencode'],+ }));+ serve(items);+ renderDialog({ eligible: (item) => item.backend === 'claude' });+ const user = userEvent.setup();++ const dialog = await screen.findByRole('dialog');+ await within(dialog).findByText('OpenAI');+ const checkboxes = within(dialog).getAllByRole('checkbox');+ expect(checkboxes).toHaveLength(3);+ expect(within(dialog).getAllByText(/must migrate together: Claude Code, Codex, OpenCode/)).toHaveLength(3);+ await user.click(checkboxes[2]);+ for (const checkbox of checkboxes) expect(checkbox.getAttribute('aria-checked')).toBe('false');+ await user.click(checkboxes[1]);+ await user.click(within(dialog).getByRole('button', { name: 'Start migration' }));+ await waitFor(() => expect(applied).toEqual([items.map((item) => item.id)]));+ });++ it.each([+ ['missing', undefined],+ ['empty', []],+ ] as const)('retains independent backend selection when required_backends is %s', async (_description, required) => {+ serve([CODEX_KEY, LEGACY].map((item) => ({+ ...item,+ required_backends: required ? [...required] : undefined,+ })));+ renderDialog();+ const user = userEvent.setup();++ const dialog = await screen.findByRole('dialog');+ await within(dialog).findByText('OpenAI');+ expect(within(dialog).queryByText(/must migrate together/)).toBeNull();+ const checkboxes = within(dialog).getAllByRole('checkbox');+ await user.click(checkboxes[0]);+ expect(checkboxes[1].getAttribute('aria-checked')).toBe('true');+ await user.click(within(dialog).getByRole('button', { name: 'Start migration' }));+ await waitFor(() => expect(applied).toEqual([[LEGACY.id]]));+ });});diff --git a/ui/src/components/settings/models/MigrationDialog.tsx b/ui/src/components/settings/models/MigrationDialog.tsxindex d826245be..ae84dbc3d 100644--- a/ui/src/components/settings/models/MigrationDialog.tsx+++ b/ui/src/components/settings/models/MigrationDialog.tsx@@ -1,6 +1,7 @@-// One-click native credential takeover. Each backend is one custody boundary:-// unsupported native rows stay visible and block that backend as a group.+// One-click native credential takeover. Each backend is one custody boundary;+// shared persisted assignments link those boundaries into one consent group.import * as React from 'react';+import type { TFunction } from 'i18next';import { ArrowDownToLine, Bot, KeyRound, Loader2, Sparkles } from 'lucide-react';import { useTranslation } from 'react-i18next';@@ -25,15 +26,15 @@ import type { AgentBackend, MigrationItem } from './types';const BACKEND_ORDER: AgentBackend[] = ['claude', 'codex', 'opencode'];-// Where a row's credential was read from, named only as far as the payload can-// honestly support. An OpenCode key may come from the config file or from-// auth.json and `backend` + `kind` cannot tell the two apart, so all three say-// "<assistant> configuration" rather than claiming a specific store.-const SOURCE_KEY: Record<AgentBackend, TranslationKey> = {+// Older payloads cannot identify the exact file or store. Name only the+// backend's configuration when the server supplies no file locators.+const SOURCE_KEY = {claude: 'settings.models.migration.source.claude',codex: 'settings.models.migration.source.codex',opencode: 'settings.models.migration.source.opencode',-};+} as const satisfies Record<AgentBackend, TranslationKey>;++const sourcePaths = (item: MigrationItem): string[] => [...new Set(item.source_paths ?? [])];/** The provider a row belongs to, or `null` when the server sent no metadata —* in which case the row falls back to `masked_detail` rather than guessing an@@ -69,8 +70,10 @@ const ItemRow: React.FC<{// to put above them, so the composed detail stays the title as before.const title = provider ?? item.masked_detail;const maskedKey = provider ? (item.masked_credential?.trim() || item.masked_detail) : '';- const origin = t(SOURCE_KEY[item.backend]);+ const paths = sourcePaths(item);+ const origin = paths.length === 0 ? t(SOURCE_KEY[item.backend]) : '';const detail = [maskedKey, origin].filter(Boolean).join(' · ');+ const label = [title, detail, ...paths].filter(Boolean).join(' · ');return (<divclassName={cn(@@ -79,7 +82,7 @@ const ItemRow: React.FC<{)}><div className="flex min-w-0 flex-1 items-center gap-3">- <Checkbox checked={checked} onCheckedChange={onToggle} disabled={!selectable} label={detail ? `${title} · ${detail}` : title} />+ <Checkbox checked={checked} onCheckedChange={onToggle} disabled={!selectable} label={label} /><span className={cn('flex size-9 shrink-0 items-center justify-center rounded-[10px]', ACCENT_TILE[accent])}>{vendor? <VendorGlyph vendor={providerVendorId(vendor)} className={cn('h-[14px] w-auto shrink-0 aspect-[10/7]', ACCENT_ICON[accent])} />@@ -88,6 +91,9 @@ const ItemRow: React.FC<{<div className="flex min-w-0 flex-1 flex-col gap-0.5"><span className="truncate text-[14px] font-semibold text-foreground">{title}</span>{detail && <span className="truncate text-[12px] text-muted">{detail}</span>}+ {paths.map((path) => (+ <span key={path} className="break-all font-mono text-[12px] text-muted">{path}</span>+ ))}</div></div></div>@@ -96,6 +102,26 @@ const ItemRow: React.FC<{const DEFAULT_SCOPE = () => true;const isImportable = (item: MigrationItem) => item.proposed_action === 'import';++function requiredBackends(items: MigrationItem[], backends: Iterable<AgentBackend>): Set<AgentBackend> {+ const required = new Set(backends);+ // Resolve from every row of an included backend, not just the eligible+ // entry-point rows. Scope filters cannot conceal a shared-file consumer.+ let expanded = true;+ while (expanded) {+ expanded = false;+ for (const item of items) {+ if (!required.has(item.backend)) continue;+ for (const backend of item.required_backends ?? []) {+ if (required.has(backend)) continue;+ required.add(backend);+ expanded = true;+ }+ }+ }+ return required;+}+const MIGRATION_ERROR_KEYS: Record<string, TranslationKey> = {migration_native_busy: 'settings.models.migration.errors.nativeBusy',migration_permission_needed: 'settings.models.migration.errors.permissionNeeded',@@ -103,22 +129,41 @@ const MIGRATION_ERROR_KEYS: Record<string, TranslationKey> = {migration_item_conflict: 'settings.models.migration.errors.itemConflict',migration_configuration_blocked: 'settings.models.migration.errors.configurationBlocked',migration_credentials_invalid: 'settings.models.migration.errors.credentialsInvalid',+ migration_reauthorization_required: 'settings.models.migration.errors.reauthorizationRequired',};-const BLOCKED_NOTE_KEYS = new Set([+const BLOCKED_NOTE_KEYS = new Set<string>(['settings.models.migration.blocked.config','settings.models.migration.blocked.environment','settings.models.migration.blocked.credential',-]);+ 'settings.models.migration.blocked.dynamic_shell',+ 'settings.models.migration.blocked.ambiguous_shell',+ 'settings.models.migration.blocked.unreadable',+ 'settings.models.migration.blocked.reference',+ 'settings.models.migration.blocked.helper',+ 'settings.models.migration.blocked.token',+ 'settings.models.migration.blocked.headers',+ 'settings.models.migration.blocked.transport',+] satisfies TranslationKey[]);const BLOCKED_FALLBACK_KEY = 'settings.models.migration.blocked.fallback' satisfies TranslationKey;-function blockedMessage(- t: ReturnType<typeof useTranslation>['t'],+function blockedMessages(+ t: TFunction,rows: MigrationItem[],-): string {- const noteKey = rows- .map((item) => item.notes_key)- .find((key): key is string => Boolean(key && BLOCKED_NOTE_KEYS.has(key)));- return serverText(t, noteKey, BLOCKED_FALLBACK_KEY) ?? '';+): { key: string; source: string; message: string }[] {+ const messages = new Map<string, { key: string; source: string; message: string }>();+ for (const item of rows) {+ const noteKey = item.notes_key && BLOCKED_NOTE_KEYS.has(item.notes_key) ? item.notes_key : undefined;+ const message = serverText(t, noteKey, BLOCKED_FALLBACK_KEY) ?? '';+ const paths = sourcePaths(item);+ // Older scans cannot identify a file. Keep their backend-level locator,+ // with each distinct reason, without duplicating an account or key title.+ const sources = paths.length > 0 ? paths : [t(SOURCE_KEY[item.backend])];+ for (const source of sources) {+ const key = JSON.stringify([source, item.notes_key ?? BLOCKED_FALLBACK_KEY]);+ messages.set(key, { key, source, message });+ }+ }+ return [...messages.values()];}export const MigrationDialog: React.FC<{@@ -126,9 +171,8 @@ export const MigrationDialog: React.FC<{onClose: () => void;/** Fired after a successful apply so callers can refresh sources/agents. */onApplied?: (applied: number) => void;- /** Scopes the dialog to backends represented by an entry point. It never- * hides other native rows from those backends: a blocked row must remain- * visible so the whole CLI cannot be partially migrated. */+ /** Scopes the entry point, then includes every native row and required+ * backend so shared-file custody and blockers remain explicit. */eligible?: (item: MigrationItem) => boolean;}> = ({ open, onClose, onApplied, eligible }) => {const { t } = useTranslation();@@ -137,8 +181,6 @@ export const MigrationDialog: React.FC<{const [items, setItems] = React.useState<MigrationItem[]>([]);const [loading, setLoading] = React.useState(true);const [applying, setApplying] = React.useState(false);- const eligibleRef = React.useRef(eligible);- eligibleRef.current = eligible;const aliveRef = React.useRef(true);React.useEffect(() => {aliveRef.current = true;@@ -158,33 +200,7 @@ export const MigrationDialog: React.FC<{.scanMigration().then((scan) => {if (cancelled) return;- const scopePredicate = eligibleRef.current ?? DEFAULT_SCOPE;- const scopedBackends = new Set(- scan.items.filter(scopePredicate).map((item) => item.backend),- );- const importable = scan.items.filter(- (item) => scopedBackends.has(item.backend) && isImportable(item),- );- const blockedBackends = new Set(- scan.items- .filter((item) => scopedBackends.has(item.backend) && !isImportable(item))- .map((item) => item.backend),- );- const backendSelection = new Map<AgentBackend, boolean>();- for (const backend of BACKEND_ORDER) {- const rows = importable.filter((item) => item.backend === backend);- if (rows.length > 0) {- backendSelection.set(- backend,- !blockedBackends.has(backend) && rows.every((item) => item.selected),- );- }- }- setItems(scan.items.map((item) => (- scopedBackends.has(item.backend) && isImportable(item)- ? { ...item, selected: backendSelection.get(item.backend) ?? false }- : item- )));+ setItems(scan.items);setLoading(false);}).catch(() => {@@ -198,18 +214,24 @@ export const MigrationDialog: React.FC<{}, [open, showToast, t]);const scopePredicate = eligible ?? DEFAULT_SCOPE;- const scopedBackends = new Set(+ const scopedBackends = requiredBackends(+ items,items.filter(scopePredicate).map((item) => item.backend),);const candidates = items.filter((item) => scopedBackends.has(item.backend));const grouped = BACKEND_ORDER.map((backend) => {const rows = candidates.filter((item) => item.backend === backend);const importRows = rows.filter(isImportable);- const blockedRows = rows.filter((item) => !isImportable(item));+ const required = requiredBackends(items, [backend]);+ const linkedRows = candidates.filter((item) => required.has(item.backend));+ const linkedImportRows = linkedRows.filter(isImportable);+ const blockedRows = linkedRows.filter((item) => !isImportable(item));return {backend,rows,importRows,+ required,+ linkedImportRows,blockedRows,blocked: blockedRows.length > 0,};@@ -219,7 +241,7 @@ export const MigrationDialog: React.FC<{.filter((group) => (!group.blocked&& group.importRows.length > 0- && group.importRows.every((item) => item.selected)+ && group.linkedImportRows.every((item) => item.selected))).map((group) => group.backend));@@ -227,9 +249,9 @@ export const MigrationDialog: React.FC<{setItems((prev) => {const group = grouped.find((item) => item.backend === backend);if (!group || group.blocked || group.importRows.length === 0) return prev;- const selected = group.importRows.every((item) => item.selected);+ const selected = group.linkedImportRows.every((item) => item.selected);return prev.map((item) => (- item.backend === backend && isImportable(item)+ group.required.has(item.backend) && isImportable(item)? { ...item, selected: !selected }: item));@@ -290,12 +312,29 @@ export const MigrationDialog: React.FC<{<span className="px-1 font-mono text-[11px] font-semibold uppercase tracking-normal text-muted">{t(`settings.models.backends.${group.backend}`, { defaultValue: group.backend })}</span>+ {group.required.size > 1 && (+ <p className="px-1 text-[12px] leading-relaxed text-muted">+ {t('settings.models.migration.sharedFiles', {+ backends: BACKEND_ORDER+ .filter((backend) => group.required.has(backend))+ .map((backend) => t(`settings.models.backends.${backend}`))+ .join(', '),+ })}+ </p>+ )}{group.blocked && (<divrole="status"className="rounded-lg border border-gold/40 bg-gold/[0.08] px-3 py-2 text-[12px] leading-relaxed text-foreground">- {blockedMessage(t, group.blockedRows)}+ <ul className="flex flex-col gap-2">+ {blockedMessages(t, group.blockedRows).map(({ key, source, message }) => (+ <li key={key} className="flex flex-col gap-0.5">+ <span className="break-all font-mono">{source}</span>+ <span>{message}</span>+ </li>+ ))}+ </ul></div>)}{group.rows.map((item) => (diff --git a/ui/src/components/settings/models/types.ts b/ui/src/components/settings/models/types.tsindex fc0bb7f4d..91010e49f 100644--- a/ui/src/components/settings/models/types.ts+++ b/ui/src/components/settings/models/types.ts@@ -462,6 +462,12 @@ export type MigrationItem = {/** The already-masked credential that also appears inside `masked_detail`,* carried separately so provider and key can render as two elements. */masked_credential?: string | null;+ /** Display-safe persisted source file locators, never credential content.+ * Older servers omit this metadata. */+ source_paths?: string[];+ /** Server-computed transitive closure of backends sharing persisted shell+ * assignments. They must migrate together. Missing/empty means this backend. */+ required_backends?: AgentBackend[];};export type MigrationScan = { items: MigrationItem[] };diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.jsonindex 6b87a950a..1e97b283b 100644--- a/ui/src/i18n/en.json+++ b/ui/src/i18n/en.json@@ -1604,24 +1604,34 @@"migration": {"open": "Migrate configuration","title": "Migrate to the Model Hub",- "subtitle": "After migration, CLI authentication will be fully managed by Model Hub.",+ "subtitle": "Import authentication saved in configuration files, shell startup files, or supported credential stores, without reading runtime environment values. After migration, Model Hub will manage the selected authentication.","empty": "No configuration found to migrate.",+ "sharedFiles": "These backends share authentication saved in files and must migrate together: {{backends}}. Changing one selection updates the others.","scanFailed": "Couldn't scan native configs, please retry","applyFailed": "Migration failed, please retry","blocked": {- "config": "Adjust this CLI's configuration, then scan again.",- "environment": "Remove this CLI’s environment-based credentials, then retry.",- "credential": "Allow credential access, then retry.",- "fallback": "Use the existing Add flow for this CLI, then scan again."+ "config": "Check this CLI's saved configuration, then scan again.",+ "environment": "This environment-based authentication cannot be imported. Save the key in a supported configuration file, then scan again.",+ "credential": "This credential could not be read or is not supported. Check credential access and the saved authentication format, then scan again.",+ "dynamic_shell": "This file uses expressions or commands that cannot be safely read. Save the required authentication as literal values in a supported configuration file, then scan again. Migration never executes shell code.",+ "ambiguous_shell": "Conflicting saved assignments were found. Resolve the assignments in the listed files to an unambiguous literal value, then scan again.",+ "unreadable": "This source file could not be read. Check that it exists and that Avibe has read access, then scan again.",+ "reference": "A referenced variable has no unambiguous saved literal value. Configure and save the corresponding key in the listed configuration or shell startup file, then scan again.",+ "helper": "Credentials supplied by a command cannot be imported. Save them in a supported static configuration or add the source manually in Model Hub.",+ "token": "This login token or authentication format cannot be imported. Add a source in Model Hub or sign in again for that source.",+ "headers": "Custom credential headers cannot be imported. Use a supported authentication method or add the source manually in Model Hub.",+ "transport": "Model Hub cannot send this endpoint's saved authentication in the same way. The original files are unchanged. Use an authentication method supported by both the provider and Model Hub; changing file permissions will not fix this.",+ "fallback": "This credential cannot be imported. Use the existing Add flow in Model Hub to add this source manually."},"applying": "Migrating…","errors": {"nativeBusy": "Close the CLI or finish its tasks, then retry.","permissionNeeded": "Allow credential access, then retry.","recoveryPending": "Migration is unfinished. Retry to continue.",- "itemConflict": "The native configuration changed. Scan again.",+ "itemConflict": "Migration could not verify the saved configuration or its credentials. Check the files and authentication before retrying, or add the source manually in Model Hub.","configurationBlocked": "Adjust the native configuration, then scan again.",- "credentialsInvalid": "Authentication has expired. Sign in again in Model Hub."+ "credentialsInvalid": "Authentication has expired. Sign in again in Model Hub.",+ "reauthorizationRequired": "This backend was already migrated, and the changed native login cannot be verified as a new authorization. The original files are unchanged. Sign in again for the existing source in Model Hub instead of importing this login."},"applied_one": "Migration complete","applied_other": "Migration complete",diff --git a/ui/src/i18n/zh.json b/ui/src/i18n/zh.jsonindex c295c5f1e..9c55e8e87 100644--- a/ui/src/i18n/zh.json+++ b/ui/src/i18n/zh.json@@ -1604,24 +1604,34 @@"migration": {"open": "迁移配置","title": "迁移到模型网关",- "subtitle": "迁移后,CLI的认证信息将完全交由模型网关管理",+ "subtitle": "导入配置文件、Shell 启动文件或受支持凭据库中已保存的认证,不读取运行中的环境变量。迁移后,所选认证由模型网关管理。","empty": "没有检测到可迁移的配置。",+ "sharedFiles": "这些后端共用文件中保存的认证,需要一起迁移:{{backends}}。勾选或取消其中一个,会同时更新全部关联后端。","scanFailed": "扫描 Agent 配置失败,请重试","applyFailed": "迁移失败,请重试","blocked": {- "config": "请先调整此 CLI 的配置,再重新扫描。",- "environment": "请先清除该 CLI 的环境变量认证配置,再重试。",- "credential": "请允许访问凭据,再重试。",- "fallback": "请先用现有的添加流程处理此 CLI,再重新扫描。"+ "config": "请检查此 CLI 已保存的配置,再重新扫描。",+ "environment": "暂时无法导入此环境变量认证。请将密钥保存到受支持的配置文件,再重新扫描。",+ "credential": "无法读取此凭据,或其认证形式暂不受支持。请检查凭据访问权限和已保存的认证格式,再重新扫描。",+ "dynamic_shell": "此文件通过表达式或命令设置认证,无法安全读取。请将需要迁移的认证以固定值保存到受支持的配置文件,再重新扫描。迁移不会执行 Shell 代码。",+ "ambiguous_shell": "列出的文件中存在互相冲突的赋值。请确认应使用的固定值,统一相关配置后重新扫描。",+ "unreadable": "无法读取此配置文件。请检查文件是否存在,以及 Avibe 是否有读取权限,再重新扫描。",+ "reference": "引用的变量没有明确的已保存固定值。请在列出的配置文件或 Shell 启动文件中配置并保存对应的密钥,再重新扫描。",+ "helper": "无法导入由命令提供的凭据。请将凭据保存为受支持的静态配置,或在模型网关中手动添加供应商。",+ "token": "此登录令牌或认证格式暂不支持导入。请在模型网关中添加供应商,或为该供应商重新登录。",+ "headers": "暂不支持导入自定义认证请求头。请使用受支持的认证方式,或在模型网关中手动添加供应商。",+ "transport": "模型网关目前无法按此接口原有的方式发送认证,原配置文件未改动。请使用供应商和模型网关都支持的认证方式;修改文件权限不能解决此问题。",+ "fallback": "暂时无法导入此认证,请使用模型网关现有的添加流程手动添加供应商。"},"applying": "迁移中…","errors": {"nativeBusy": "请先关闭 CLI 或完成任务,再重试。","permissionNeeded": "请允许访问凭据,再重试。","recoveryPending": "迁移尚未完成,请重试以继续。",- "itemConflict": "原生配置已变化,请重新扫描。",+ "itemConflict": "无法验证已保存的配置或凭据。请检查配置文件和认证是否有效后重试,或在模型网关中手动添加供应商。","configurationBlocked": "请先调整原生配置,再重新扫描。",- "credentialsInvalid": "认证已过期,请在模型网关中重新登录。"+ "credentialsInvalid": "认证已过期,请在模型网关中重新登录。",+ "reauthorizationRequired": "此后端已迁移过,无法确认改动后的原生登录是否是新授权。原配置文件未改动,请在模型网关中为已有供应商重新登录,不要再次导入这份登录。"},"applied_one": "迁移完成","applied_other": "迁移完成",diff --git a/vibe/model_hub_runtime/adapter.py b/vibe/model_hub_runtime/adapter.pyindex 06e74c178..ddba51763 100644--- a/vibe/model_hub_runtime/adapter.py+++ b/vibe/model_hub_runtime/adapter.py@@ -44,7 +44,7 @@ from vibe.model_hub_runtime.client import (probe_models,upstream_api_url,)-from vibe.model_hub_runtime.api_key_vendors import pinned_api_key_protocol+from vibe.model_hub_runtime.api_key_vendors import pinned_api_key_protocol, validate_api_key_auth_schemefrom vibe.model_hub_runtime.installer import (INSTALL_ALREADY_RUNNING_REASON,INSTALL_PLATFORM_UNSUPPORTED_REASON,@@ -724,10 +724,12 @@ async def _probe_protocol_response(protocol: str,base_url: str | None,secret: str,+ auth_scheme: str | None = None,timeout: float = 15.0,) -> _ProtocolEvidence:"""Require a response from the candidate protocol's distinct request path."""+ validate_api_key_auth_scheme(vendor, protocol, base_url, secret, auth_scheme)root = base_url or _OFFICIAL_BASE_URLS.get(vendor)if not root:raise EngineClientError("source requires a base URL for protocol observation")@@ -744,11 +746,10 @@ async def _probe_protocol_response("Accept": "application/json",}if protocol == "anthropic":- headers = {- "x-api-key": secret,- "anthropic-version": "2023-06-01",- "Accept": "application/json",- }+ headers["anthropic-version"] = "2023-06-01"+ if auth_scheme != "bearer":+ headers.pop("Authorization")+ headers["x-api-key"] = secretclient_timeout = aiohttp.ClientTimeout(total=timeout)async def observe() -> _ProtocolEvidence:async with aiohttp.ClientSession(timeout=client_timeout) as session:@@ -923,6 +924,7 @@ async def _authenticate_with_models_witness(protocol: str,base_url: str | None,secret: str,+ auth_scheme: str | None = None,) -> _ModelsWitnessResult:"""Read the owned interface's model listing as the authentication witness.@@ -957,12 +959,14 @@ async def _authenticate_with_models_witness(listing, so one call both authenticates the source and populates it."""+ scheme_options = {"auth_scheme": auth_scheme} if auth_scheme is not None else {}try:models = await probe_models(vendor=vendor,protocol=protocol,base_url=base_url,secret=secret,+ **scheme_options,)except EngineClientError as exc:if exc.status_code in _AUTHENTICATION_ERROR_STATUSES:@@ -974,6 +978,7 @@ async def _authenticate_with_models_witness(protocol=protocol,base_url=base_url,secret=None,+ **scheme_options,)except EngineClientError as refusal:if refusal.status_code in _AUTHENTICATION_ERROR_STATUSES:@@ -1724,6 +1729,7 @@ class CLIProxyEngineAdapter:secret: str,base_url: str | None,*,+ auth_scheme: str | None = None,on_reserved: Callable[[str], None] | None = None,) -> str:return await run_owned_in_thread(@@ -1732,6 +1738,7 @@ class CLIProxyEngineAdapter:vendor=vendor,protocol=protocol,base_url=base_url,+ **({"auth_scheme": auth_scheme} if auth_scheme is not None else {}),on_reserved=on_reserved,)@@ -1896,6 +1903,8 @@ class CLIProxyEngineAdapter:protocol: str,secret: str,base_url: str | None,+ *,+ auth_scheme: str | None = None,) -> bool:return await asyncio.to_thread(self.state_store.matches_api_key_credential,@@ -1904,8 +1913,12 @@ class CLIProxyEngineAdapter:protocol,secret,base_url,+ **({"auth_scheme": auth_scheme} if auth_scheme is not None else {}),)+ async def credential_auth_scheme(self, credential_ref: str) -> str | None:+ return await asyncio.to_thread(self.state_store.credential_auth_scheme, credential_ref)+async def retarget_api_key_credential(self,credential_ref: str,@@ -1934,6 +1947,7 @@ class CLIProxyEngineAdapter:vendor=normalized_vendor,protocol=protocol,base_url=base_url,+ **({"auth_scheme": metadata["auth_scheme"]} if metadata.get("auth_scheme") is not None else {}),)async def credential_supports_refresh(self, credential_ref: str) -> bool:@@ -1949,25 +1963,34 @@ class CLIProxyEngineAdapter:secret: str,base_url: str | None,*,+ auth_scheme: str | None = None,on_reserved: Callable[[str], None] | None = None,) -> str:"""Store an unbound observation key until the observation settles.The observation seam determines protocol from upstream responses, so the- temporary record deliberately uses a neutral engine-store protocol marker;- ``observe_source`` reads only the opaque ref's secret and never treats that- marker as a protocol conclusion.+ legacy temporary record uses a neutral engine-store protocol marker.+ Explicit Bearer is restricted to the Anthropic interface. Neither marker+ supplies authentication evidence; observation still needs upstream proof."""+ validate_api_key_auth_scheme(vendor, None, base_url, secret, auth_scheme)return await run_owned_in_thread(self.state_store.store_api_key,secret,vendor=vendor,- protocol="openai_chat",+ protocol="anthropic" if auth_scheme == "bearer" else "openai_chat",base_url=base_url,+ **({"auth_scheme": auth_scheme} if auth_scheme is not None else {}),on_reserved=on_reserved,)+ async def revoke_api_key_credential(self, credential_ref: str) -> None:+ await asyncio.to_thread(self.state_store.assert_credential_unbound, credential_ref)+ await asyncio.to_thread(self.state_store.credential_auth_scheme, credential_ref)+ await asyncio.to_thread(self.supervisor.invalidate_configs)+ await asyncio.to_thread(self.state_store.revoke_api_key_credential, credential_ref)+async def revoke_credential(self, credential_ref: str) -> None:await asyncio.to_thread(self.state_store.assert_credential_unbound,@@ -2091,6 +2114,7 @@ class CLIProxyEngineAdapter:protocol=protocol,base_url=normalized_base_url,secret=secret,+ **({"auth_scheme": metadata["auth_scheme"]} if metadata.get("auth_scheme") is not None else {}),)except EngineClientError as exc:raise ModelDiscoveryError("model discovery failed") from exc@@ -2133,12 +2157,19 @@ class CLIProxyEngineAdapter:if metadata.get("vendor") != normalized_vendor:raise EngineStateError("credential does not match observation target")credential_kind = metadata.get("kind")+ auth_scheme = metadata.get("auth_scheme")+ scheme_options = {"auth_scheme": auth_scheme} if auth_scheme is not None else {}secret: str | None = Noneoauth_auth: _AuthRecord | None = Noneif credential_kind == "api_key":if metadata.get("base_url") != normalized_base_url:raise EngineStateError("credential does not match observation target")secret = await asyncio.to_thread(self.state_store.read_api_key, credential_ref)+ if auth_scheme is not None:+ for protocol in protocol_order:+ validate_api_key_auth_scheme(+ normalized_vendor, protocol, base_url, secret, auth_scheme,+ )elif credential_kind == "oauth":if normalized_base_url is not None:raise EngineStateError("credential does not match observation target")@@ -2185,6 +2216,7 @@ class CLIProxyEngineAdapter:protocol=protocol,base_url=base_url,secret=secret or "",+ **scheme_options,)elif credential_kind == "oauth" and owner_scoped_protocol:# A pinned subscription has no upstream to probe: the engine@@ -2226,6 +2258,7 @@ class CLIProxyEngineAdapter:protocol=protocol,base_url=base_url,secret=secret or "",+ **scheme_options,)if witness.credential is _ModelsWitness.REJECTED:received_rejection = True@@ -2264,6 +2297,7 @@ class CLIProxyEngineAdapter:protocol=proved_protocol,base_url=base_url,secret=secret or "",+ **scheme_options,)else:models = await self.discover_models(@@ -2304,6 +2338,7 @@ class CLIProxyEngineAdapter:protocol=proved_protocol,base_url=base_url,secret=secret or "",+ **scheme_options,)else:models = await self.discover_models(diff --git a/vibe/model_hub_runtime/api_key_vendors.py b/vibe/model_hub_runtime/api_key_vendors.pyindex a2e104ae4..6402ad219 100644--- a/vibe/model_hub_runtime/api_key_vendors.py+++ b/vibe/model_hub_runtime/api_key_vendors.py@@ -6,6 +6,7 @@ import jsonfrom dataclasses import dataclassfrom functools import lru_cachefrom pathlib import Path+from urllib.parse import urlsplitfrom config.v2_config import normalize_model_hub_base_url, normalize_model_hub_vendor_id@@ -19,6 +20,96 @@ _LEGACY_OFFICIAL_BASE_URLS = {}+def _validated_cpa_anthropic_origin(base_url: str | None) -> bool:+ """Share CPA's authority gate, rejecting ambiguous HTTP URL spellings."""+ if not isinstance(base_url, str) or any(ord(char) <= 32 or ord(char) == 127 for char in base_url):+ raise ValueError+ normalized = normalize_model_hub_base_url(base_url)+ if normalized is None:+ raise ValueError+ parsed = urlsplit(normalized)+ hostname = parsed.hostname+ if not hostname or "%" in parsed.netloc or "\\" in parsed.netloc or parsed.port == 0:+ raise ValueError+ ascii_hostname = hostname.encode("idna").decode("ascii").lower()+ if ascii_hostname == "api.anthropic.com" and hostname.lower() != ascii_hostname:+ # Python's HTTP client can normalize these to the official host while+ # Go's URL authority gate sees the original spelling. Admit neither.+ raise ValueError+ return (+ parsed.scheme == "https"+ and hostname.lower() == "api.anthropic.com"+ # CPA compares URL.Port() as text: :0443 is not its official origin.+ and (parsed.port is None or parsed.netloc.rsplit(":", 1)[-1] == "443")+ )+++def validate_api_key_auth_scheme(+ vendor: str,+ protocol: str | None,+ base_url: str | None,+ secret: str | None,+ auth_scheme: str | None,+) -> str | None:+ """Validate explicit static transport without reinterpreting legacy keys.++ A missing protocol is only for an unbound observation credential. A missing+ secret is only for the credentialless contrast of an already validated+ transport. The pinned CPA sends ordinary custom Claude keys as Bearer, but+ interprets ``sk-ant-oat`` anywhere in a key as OAuth independently of kind.+ """+ if auth_scheme is None:+ return None+ try:+ if (+ auth_scheme != "bearer"+ or not isinstance(vendor, str)+ or vendor.strip().lower() != "anthropic"+ or protocol not in (None, "anthropic")+ ):+ raise ValueError+ if _validated_cpa_anthropic_origin(base_url):+ raise ValueError+ if secret is not None and (+ not isinstance(secret, str)+ or not secret.strip()+ or "sk-ant-oat" in secret+ or any(ord(char) < 32 or ord(char) == 127 for char in secret)+ ):+ raise ValueError+ except (TypeError, ValueError):+ raise ValueError("unsupported API key authentication scheme") from None+ return "bearer"+++def validate_migration_api_key_transport(+ vendor: str,+ protocol: str,+ base_url: str | None,+ secret: str | None,+ auth_scheme: str | None,+) -> None:+ """Admit native static auth only when proof and pinned CPA preserve it.++ This is a migration gate, not a new default for public Sources or legacy+ credential metadata. Anthropic SDK/API_KEY inputs mean x-api-key; CPA uses+ that header only at its official origin. Static keys matching its OAuth+ heuristic are unsafe at either origin, regardless of successful proof.+ """+ try:+ validate_api_key_auth_scheme(vendor, protocol, base_url, secret, auth_scheme)+ if protocol != "anthropic":+ return+ if not isinstance(secret, str) or not secret.strip() or "sk-ant-oat" in secret:+ raise ValueError+ if auth_scheme is None and not _validated_cpa_anthropic_origin(+ base_url if base_url is not None else official_api_key_base_url(vendor)+ ):+ raise ValueError+ except (TypeError, ValueError):+ raise ValueError("unsupported native API key transport") from None++@dataclass(frozen=True)class APIKeyVendorCatalogEntry:id: strdiff --git a/vibe/model_hub_runtime/client.py b/vibe/model_hub_runtime/client.pyindex 24ab7d4aa..1ccdbc405 100644--- a/vibe/model_hub_runtime/client.py+++ b/vibe/model_hub_runtime/client.py@@ -42,7 +42,7 @@ from core.handlers.model_hub.stream_wire import (ProtocolUsageReport,observe_buffered_protocol_response,)-from vibe.model_hub_runtime.api_key_vendors import official_api_key_base_urls+from vibe.model_hub_runtime.api_key_vendors import official_api_key_base_urls, validate_api_key_auth_schemefrom vibe.model_hub_runtime.state import SourceRecord@@ -757,6 +757,7 @@ async def probe_models(protocol: str,base_url: str | None,secret: str | None,+ auth_scheme: str | None = None,timeout: float = 15.0,) -> tuple[DiscoveredModel, ...]:"""Probe the one allowlisted models path without redirecting credentials.@@ -767,6 +768,7 @@ async def probe_models(arrives belongs to anyone who asks and therefore attests to no credential."""normalized_vendor = vendor.strip().lower()+ validate_api_key_auth_scheme(normalized_vendor, protocol, base_url, secret, auth_scheme)root = base_url or _OFFICIAL_BASE_URLS.get(normalized_vendor)if not root:raise EngineClientError("source requires a base URL for model discovery")@@ -781,7 +783,7 @@ async def probe_models(# stays on the uncredentialed request: it must differ in exactly one way.headers["anthropic-version"] = "2023-06-01"if secret is not None:- if protocol == "anthropic":+ if protocol == "anthropic" and auth_scheme != "bearer":headers["x-api-key"] = secretelse:headers["Authorization"] = f"Bearer {secret}"diff --git a/vibe/model_hub_runtime/config.py b/vibe/model_hub_runtime/config.pyindex a2b93dbad..aaf1582ff 100644--- a/vibe/model_hub_runtime/config.py+++ b/vibe/model_hub_runtime/config.py@@ -8,7 +8,7 @@ import yamlfrom config.atomic_io import write_atomicfrom config.v2_config import normalize_model_hub_base_url-from vibe.model_hub_runtime.api_key_vendors import official_api_key_base_url+from vibe.model_hub_runtime.api_key_vendors import official_api_key_base_url, validate_api_key_auth_schemefrom vibe.model_hub_runtime.state import EngineStateError, EngineStateStore, RuntimeSecrets, SourceRecord@@ -73,6 +73,12 @@ def _append_source(payload: dict[str, Any], source: SourceRecord, store: EngineS# OAuth credentials are engine auth files, not YAML credential values.returnapi_key = store.read_api_key(source.credential_ref)+ try:+ validate_api_key_auth_scheme(+ source.vendor, source.protocol, source.base_url, api_key, credential.get("auth_scheme"),+ )+ except ValueError:+ raise EngineStateError("unsupported API key authentication scheme") from Nonereasoning_by_model = dict(source.model_reasoning_efforts)models = []for model in dict.fromkeys((*source.model_ids, *source.route_model_ids)):diff --git a/vibe/model_hub_runtime/state.py b/vibe/model_hub_runtime/state.pyindex a5235de34..5aa96f478 100644--- a/vibe/model_hub_runtime/state.py+++ b/vibe/model_hub_runtime/state.py@@ -16,12 +16,16 @@ from typing import Any, Callable, Sequencefrom config.atomic_io import write_atomicfrom config.v2_config import normalize_model_hub_base_url-from vibe.model_hub_runtime.api_key_vendors import official_api_key_base_url+from vibe.model_hub_runtime.api_key_vendors import (+ official_api_key_base_url,+ validate_api_key_auth_scheme,+)logger = logging.getLogger(__name__)_CREDENTIAL_REF_RE = re.compile(r"^cred_[A-Za-z0-9_-]{6,128}$")+_BEARER_CREDENTIAL_REF_RE = re.compile(r"^cred_auth_bearer_[0-9a-f]{32}$")_SOURCE_ID_RE = re.compile(r"^src_[a-z0-9]{8,}$")_PROTOCOLS = {"anthropic", "openai_responses", "openai_chat"}@@ -164,6 +168,7 @@ class EngineStateStore:vendor: str = "custom",protocol: str = "openai_chat",base_url: str | None = None,+ auth_scheme: str | None = None,on_reserved: Callable[[str], None] | None = None,) -> str:if not isinstance(value, str) or not value:@@ -173,9 +178,16 @@ class EngineStateStore:raise EngineStateError("credential vendor is empty")if protocol not in _PROTOCOLS:raise EngineStateError("unsupported source protocol")+ try:+ validate_api_key_auth_scheme(+ normalized_vendor, protocol, base_url, value, auth_scheme,+ )+ except ValueError:+ raise EngineStateError("unsupported API key authentication scheme") from Nonenormalized_base_url = _validated_base_url(base_url)with self._lock:- credential_ref = f"cred_{secrets.token_hex(16)}"+ prefix = "cred_auth_bearer_" if auth_scheme == "bearer" else "cred_"+ credential_ref = f"{prefix}{secrets.token_hex(16)}"credential_path, credential_tmp, stage_path, stage_tmp = (self._reserve_credential_namespace(credential_ref))@@ -190,6 +202,7 @@ class EngineStateStore:"protocol": protocol,"base_url": normalized_base_url,"value": value,+ **({"auth_scheme": auth_scheme} if auth_scheme is not None else {}),},temporary_path=credential_tmp,credential_ref=credential_ref,@@ -443,17 +456,24 @@ class EngineStateStore:protocol: str,secret: str,base_url: str | None,+ *,+ auth_scheme: str | None = None,) -> bool:"""Compare transient native material with an engine-owned API key."""metadata = self.credential_metadata(credential_ref)if metadata.get("kind") != "api_key":return False+ try:+ validate_api_key_auth_scheme(vendor, protocol, base_url, secret, auth_scheme)+ except ValueError:+ raise EngineStateError("unsupported API key authentication scheme") from Nonenormalized_base_url = _validated_base_url(base_url)if (metadata.get("vendor") != vendor.strip().lower()or metadata.get("protocol") != protocolor metadata.get("base_url") != normalized_base_url+ or metadata.get("auth_scheme") != auth_scheme):return Falseif not isinstance(secret, str):@@ -604,13 +624,80 @@ class EngineStateStore:if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:raise EngineStateError("credential permissions are unsafe")payload = self._read_json(path)- kind = payload.get("kind") if payload else None- if kind == "reservation" and payload.get("credential_ref") == credential_ref:+ if payload is None:+ return None+ if payload.get("kind") == "reservation" and payload.get("credential_ref") == credential_ref:return None- if kind not in {"api_key", "oauth"}:- raise EngineStateError("credential is unavailable")+ _validate_credential_auth_scheme(credential_ref, payload)return payload+ def credential_auth_scheme(self, credential_ref: str) -> str | None:+ """Recover replacement transport without requiring the previous secret.++ Only a safe missing or content-corrupt document permits ref fallback.+ Readable identity conflicts, unsafe paths and I/O failures stay errors.+ Plain refs predate explicit transport; intact unpublished Bearer+ metadata is supported, but its complete loss cannot be reconstructed.+ """+ with self._lock:+ ref_scheme = _credential_ref_auth_scheme(credential_ref)+ payload = self._read_replacement_credential_document(credential_ref)+ if payload is None:+ return ref_scheme+ _validate_credential_auth_scheme(credential_ref, payload, require_secret=False)+ if payload.get("kind") != "api_key":+ raise EngineStateError("API key credential is unavailable")+ return payload.get("auth_scheme")++ def _private_credential_directory_present(self, directory: Path) -> bool:+ """Observe owned directories without creating or repairing them."""+ for path in (self.root, directory):+ try:+ mode = path.lstat().st_mode+ except FileNotFoundError:+ return False+ except OSError as exc:+ raise EngineStateError("unable to inspect credential directory") from exc+ if not stat.S_ISDIR(mode) or stat.S_IMODE(mode) != 0o700:+ raise EngineStateError("credential directory permissions are unsafe")+ return True++ def _read_replacement_credential_document(+ self, credential_ref: str,+ ) -> dict[str, Any] | None:+ """Read safe identity evidence; only missing/corrupt contents are absent."""+ _credential_ref_auth_scheme(credential_ref)+ directory = self.root / "credentials"+ if not self._private_credential_directory_present(directory):+ return None+ path = directory / f"{credential_ref}.json"+ try:+ mode = path.lstat().st_mode+ except FileNotFoundError:+ return None+ except OSError as exc:+ raise EngineStateError("unable to inspect credential file") from exc+ if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:+ raise EngineStateError("credential permissions are unsafe")+ try:+ # Do not turn an open/read failure into content corruption. Ref+ # recovery is safe only after a private regular file was read.+ descriptor = os.open(+ path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0),+ )+ with os.fdopen(descriptor, "rb") as handle:+ mode = os.fstat(handle.fileno()).st_mode+ if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:+ raise EngineStateError("credential permissions are unsafe")+ raw = handle.read()+ except OSError as exc:+ raise EngineStateError("unable to read credential file") from exc+ try:+ payload = json.loads(raw)+ except ValueError:+ return None+ return payload if isinstance(payload, dict) else None+def has_current_source_credential(self,credential_ref: str,@@ -629,8 +716,7 @@ class EngineStateStore:remain entirely engine-owned and are not opened by this observation."""try:- if not isinstance(credential_ref, str) or _CREDENTIAL_REF_RE.fullmatch(credential_ref) is None:- return False+ _credential_ref_auth_scheme(credential_ref)_validated_source_id(source_id)credential_kind = {"api_key": "api_key", "subscription": "oauth"}.get(kind)if credential_kind is None or not isinstance(vendor, str) or protocol not in _PROTOCOLS:@@ -653,7 +739,12 @@ class EngineStateStore:payload.get("kind") != credential_kind or payload.get("vendor") != normalized_vendor):return False+ _validate_credential_auth_scheme(credential_ref, payload)if credential_kind == "api_key":+ validate_api_key_auth_scheme(+ normalized_vendor, protocol, normalized_base_url,+ payload.get("value"), payload.get("auth_scheme"),+ )return (payload.get("protocol") == protocoland payload.get("base_url") == normalized_base_url@@ -706,6 +797,30 @@ class EngineStateStore:self._remove_private_file_if_present(temporary_path)self._remove_private_file_if_present(path)+ def revoke_api_key_credential(self, credential_ref: str) -> None:+ """Retire a known API-key ref, never a watched OAuth auth file.++ The caller's durable API-key-only intent permits cleanup when the old+ document is missing/content-corrupt. Readable identity conflicts still+ reject. Exact private namespace ownership does not depend on a secret.+ """+ with self._lock:+ self.assert_credential_unbound(credential_ref)+ self.credential_auth_scheme(credential_ref)+ paths = self._credential_namespace_paths(credential_ref, create=False)+ present_directories = {+ directory+ for directory in {path.parent for path in paths}+ if self._private_credential_directory_present(directory)+ }+ # Validate every owned path before deleting any, including temp and+ # staging remnants. Never follow an auth_name in document contents.+ for path in paths:+ self._assert_private_file(path, "engine state path is unsafe")+ for path in reversed(paths):+ if path.parent in present_directories:+ self._remove_private_file_if_present(path)+def clear_runtime_configs(self) -> None:"""Remove persisted engine configs after any credential is revoked."""with self._lock:@@ -778,8 +893,7 @@ class EngineStateStore:entry.chmod(0o600)def _credential_path(self, credential_ref: str) -> Path:- if _CREDENTIAL_REF_RE.fullmatch(credential_ref) is None:- raise EngineStateError("invalid credential reference")+ _credential_ref_auth_scheme(credential_ref)self._ensure_private_dir(self.root)credentials_dir = self.root / "credentials"self._ensure_private_dir(credentials_dir)@@ -788,9 +902,18 @@ class EngineStateStore:def _credential_namespace_paths(self,credential_ref: str,+ *,+ create: bool = True,) -> tuple[Path, Path, Path, Path]:- credential_path = self._credential_path(credential_ref)- stage_path = self._oauth_stage_path(credential_ref)+ _credential_ref_auth_scheme(credential_ref)+ credential_path = (+ self._credential_path(credential_ref)+ if create else self.root / "credentials" / f"{credential_ref}.json"+ )+ stage_path = (+ self._oauth_stage_path(credential_ref)+ if create else self.oauth_staging_dir / f"{credential_ref}.json"+ )return (credential_path,credential_path.with_name(f".{credential_path.name}.tmp"),@@ -821,8 +944,7 @@ class EngineStateStore:return pathsdef _oauth_stage_path(self, credential_ref: str) -> Path:- if _CREDENTIAL_REF_RE.fullmatch(credential_ref) is None:- raise EngineStateError("invalid credential reference")+ _credential_ref_auth_scheme(credential_ref)self._ensure_private_dir(self.oauth_staging_dir)return self.oauth_staging_dir / f"{credential_ref}.json"@@ -838,8 +960,10 @@ class EngineStateStore:credential_ref = path.stemif path.suffix != ".json" or _CREDENTIAL_REF_RE.fullmatch(credential_ref) is None:raise EngineStateError("credential state contains an unsafe entry")+ _credential_ref_auth_scheme(credential_ref)payload = self._read_json(path)if payload and payload.get("kind") == "oauth":+ _validate_credential_auth_scheme(credential_ref, payload)result.append((credential_ref, payload))return result@@ -1113,6 +1237,52 @@ class EngineStateStore:raise EngineStateError(message)+def _credential_ref_auth_scheme(credential_ref: str) -> str | None:+ """Decode private immutable transport identity; callers keep refs opaque."""+ if not isinstance(credential_ref, str) or _CREDENTIAL_REF_RE.fullmatch(credential_ref) is None:+ raise EngineStateError("invalid credential reference")+ if credential_ref.startswith("cred_auth_"):+ if _BEARER_CREDENTIAL_REF_RE.fullmatch(credential_ref) is None:+ raise EngineStateError("unsupported credential reference authentication scheme")+ return "bearer"+ return None+++def _validate_credential_auth_scheme(+ credential_ref: str,+ payload: dict[str, Any],+ *,+ require_secret: bool = True,+) -> None:+ ref_scheme = _credential_ref_auth_scheme(credential_ref)+ kind = payload.get("kind")+ if not isinstance(kind, str) or kind not in {"api_key", "oauth"}:+ raise EngineStateError("credential is unavailable")+ scheme = payload.get("auth_scheme")+ if ref_scheme is not None and (kind != "api_key" or scheme != ref_scheme):+ raise EngineStateError("unsupported API key authentication scheme")+ if kind == "oauth":+ if scheme is not None:+ raise EngineStateError("unsupported API key authentication scheme")+ return+ try:+ # Metadata-only replacement validates transport independently of the+ # old value. The new value must pass ordinary provisioning/discovery.+ validate_api_key_auth_scheme(+ payload.get("vendor"), payload.get("protocol"), payload.get("base_url"),+ payload.get("value") if require_secret else None, scheme,+ )+ if scheme is not None and (+ payload.get("protocol") != "anthropic"+ or (require_secret and (+ not isinstance(payload.get("value"), str) or not payload["value"]+ ))+ ):+ raise ValueError+ except ValueError:+ raise EngineStateError("unsupported API key authentication scheme") from None++def _validated_oauth_auth_name(value: str) -> str:normalized = value.strip()if (