auth_external_process.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Executable lookup and launch-env policy for ``external_process`` (ACP) providers.
  2. A GUI/service launch (LaunchAgent, Desktop backend, Desktop over SSH) has a bare PATH, so the
  3. profile's command resolves through PATH, then the user-local bin, then Claude Code's install
  4. prefixes."""
  5. from __future__ import annotations
  6. import os
  7. from typing import Optional
  8. def resolve_external_process_command(command: str) -> Optional[str]:
  9. """Absolute path of the provider CLI ``command``, or ``None`` when nothing executable matches."""
  10. if not command:
  11. return None
  12. from hermes_platform.resolver import locate_command
  13. from hermes_platform.resolver.known_dirs import user_local_bin
  14. lookup = command
  15. if os.sep in command or (os.altsep and os.altsep in command):
  16. # An operator-configured relative path (./bin/agent) means the launch cwd, as it did
  17. # under shutil.which; anchor it so the resolver's absolute-only explicit check accepts it.
  18. lookup = os.path.abspath(os.path.expandvars(os.path.expanduser(command)))
  19. found = locate_command(lookup, known_dirs=user_local_bin()).command
  20. if found:
  21. # Known dirs are written with "/" (%USERPROFILE%/.local/bin); normalize so a Windows hit
  22. # reads as one native path in status and in the spawned argv.
  23. return os.path.normpath(found[0])
  24. from agent.anthropic_adapter import find_claude_code_cli
  25. return find_claude_code_cli(command)
  26. def external_process_launch_env_names() -> frozenset[str]:
  27. """Env names that choose an external-process provider's executable or argv, from every registered
  28. profile's ``process_command_env_vars`` and ``process_args_env_var``: executable authority, so the
  29. generic env writers (dashboard, MCP/connector installs, ``save_env_value``) must refuse them."""
  30. from providers import list_providers
  31. return frozenset(name for profile in list_providers() if profile.auth_type == "external_process"
  32. for name in (*profile.process_command_env_vars, profile.process_args_env_var) if name)
  33. def env_writer_launch_denylist() -> frozenset[str]:
  34. """:func:`external_process_launch_env_names` plus every left-core row's ``launch_env``: a plugin that is
  35. not loaded (absent, disabled, failed, quarantined) registers no profile to declare them."""
  36. from hermes_cli.left_core_migration import launch_env_names
  37. return external_process_launch_env_names() | launch_env_names()