session_readiness.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Profile-bound setup checks without legacy session execution dispatch."""
  2. import asyncio
  3. import logging
  4. from pathlib import Path
  5. from hermes_state_runtime import RuntimeStoreError
  6. logger = logging.getLogger(__name__)
  7. async def readiness(authority, actor, params, *, runtime):
  8. if 'session:create' not in actor.capabilities or actor.profile_id != authority.profile_id:
  9. raise RuntimeStoreError('permission_denied')
  10. allowed = {'profile', 'provider'} if runtime else {'profile'}
  11. if (not isinstance(params, dict) or set(params) - allowed
  12. or any(not isinstance(value, str) for value in params.values())):
  13. raise RuntimeStoreError('invalid_params')
  14. if params.get('profile'):
  15. from fastapi import HTTPException
  16. from hermes_cli.web_server_profiles import _resolve_profile_dir
  17. try:
  18. home = _resolve_profile_dir(params['profile']).resolve()
  19. except HTTPException:
  20. raise RuntimeStoreError('profile_mismatch') from None
  21. if home != Path(authority.profile_id):
  22. raise RuntimeStoreError('profile_mismatch')
  23. def probe():
  24. from gateway.run import _profile_runtime_scope
  25. from hermes_cli.main import _has_any_provider_configured
  26. from hermes_cli.runtime_readiness import check_runtime_readiness
  27. with _profile_runtime_scope(Path(authority.profile_id)):
  28. try:
  29. if runtime:
  30. return check_runtime_readiness(params.get('provider') or None, strict_profile_scope=True)
  31. return {'provider_configured': bool(_has_any_provider_configured(strict_profile_scope=True))}
  32. except Exception as exc: # a readiness probe reports, never raises
  33. logger.debug('readiness probe failed', exc_info=True)
  34. if runtime:
  35. from hermes_cli.runtime_readiness import readiness_failure
  36. return readiness_failure(exc)
  37. return {'ok': False, 'error': str(exc)}
  38. return await asyncio.to_thread(probe)