Use `anyio.to_thread.run_sync` for CLI key management

Merged
KL
2mo ago
+9-6

Why we make these changes

Related to #7112. Five CLI key-store operations use asyncio.to_thread, which requires an asyncio event loop. Use AnyIO for these operations. This does not resolve the issue's separate Agent.run() failure under Trio.

New public surface

None.

User-visible behavior

src/pydantic_clai2/pydantic_clai2/ui/menus/set_menu.py :: set_command()
-└─ src/pydantic_clai2/pydantic_clai2/config/api_keys.py :: set_api_key() -> asyncio.to_thread(load_keys/save_key)
+└─ src/pydantic_clai2/pydantic_clai2/config/api_keys.py :: set_api_key() -> to_thread.run_sync(load_keys/save_key, abandon_on_cancel=True)

The same bridge changes key selection in /keys, plugins, and the settings key picker. The write guarded by finish_write() retains asyncio.to_thread because its coordination depends on an asyncio task.

Verification

  • CI and deep review passed on commit 4611bdc285308f5dec5be3c9784956abc33a4d15; Ruff and pre-commit passed locally.
  • Existing key-management tests and plugin-key tests ran on asyncio in CI. This PR adds no tests, and these tests do not prove Trio compatibility. Local collection requires termflow, which is absent from this checkout.

What changes for existing users

The five changed operations keep their asyncio behavior and cancellation semantics and can also use Trio's thread bridge.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver. (Not applicable.)
  • PR title is fit for the release changelog.

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.