safe_worker_policy.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Private bootstrap policy for admission-owned workers: the frozen session config every
  2. config reader returns, plus (troubleshooting workers only) the safe/ignore-user-config bypass.
  3. Stdlib-only: hermes_cli's config/env layer imports it, so it ships in the installer's
  4. hermes_cli + i18n-kernel tail and must never import beyond the standard library.
  5. Bind once in a fresh exec, before config/agent/plugin imports. This is not a
  6. sandbox or a public launch switch: only the managed worker bootstrap consumes
  7. the owner's validated assignment. No environment flag grants this policy and
  8. no reset exists; helper threads see the same immutable snapshot.
  9. """
  10. from dataclasses import dataclass
  11. import json
  12. import os
  13. import sys
  14. from threading import Lock
  15. @dataclass(frozen=True, slots=True)
  16. class _SafeWorkerPolicy:
  17. pid: int
  18. safe_mode: bool
  19. ignore_user_config: bool
  20. config_json: str
  21. _policy: _SafeWorkerPolicy | None = None
  22. _bind_lock = Lock()
  23. _RUNTIME_IMPORTS = frozenset({
  24. "run_agent", "model_tools", "hermes_cli.config", "hermes_cli.env_loader",
  25. "hermes_cli.plugins", "providers", "agent.agent_init",
  26. })
  27. def _bind_safe_worker_policy(*, safe_mode: bool, ignore_user_config: bool,
  28. config: dict) -> _SafeWorkerPolicy:
  29. """Called by the private worker bootstrap, never by the owner or public CLI."""
  30. if type(safe_mode) is not bool or type(ignore_user_config) is not bool:
  31. raise TypeError("worker bypass policy fields must be booleans")
  32. if not (safe_mode or ignore_user_config):
  33. raise ValueError("ordinary execution binds its config with _bind_worker_config")
  34. return _bind(safe_mode, True, config)
  35. def _bind_worker_config(config: dict) -> _SafeWorkerPolicy:
  36. """An ordinary managed worker's frozen session config: every config reader returns it (the
  37. session never follows later profile edits), while plugins, MCP, hooks and providers load as
  38. usual — ``safe_worker_enabled()`` stays False. Same one-shot, pre-import rules as bypass."""
  39. return _bind(False, False, config)
  40. def _bind(safe_mode: bool, ignore_user_config: bool, config: dict) -> _SafeWorkerPolicy:
  41. global _policy
  42. if type(config) is not dict:
  43. raise TypeError("worker config must be a JSON object")
  44. serialized = json.dumps(config, allow_nan=False)
  45. with _bind_lock:
  46. if _policy is not None:
  47. raise RuntimeError("worker policy is already bound")
  48. if _RUNTIME_IMPORTS.intersection(sys.modules):
  49. raise RuntimeError("worker policy must bind before runtime imports")
  50. _policy = _SafeWorkerPolicy(os.getpid(), safe_mode, ignore_user_config, serialized)
  51. return _policy
  52. def _current_policy() -> _SafeWorkerPolicy | None:
  53. if _policy is not None and _policy.pid != os.getpid():
  54. raise RuntimeError("worker policy cannot cross a fork; use a fresh exec")
  55. return _policy
  56. def safe_worker_enabled() -> bool:
  57. policy = _current_policy()
  58. return policy is not None and policy.safe_mode
  59. def worker_config_snapshot() -> dict | None:
  60. """Return detached explicit config; None means the ordinary loader owns it."""
  61. policy = _current_policy()
  62. return json.loads(policy.config_json) if policy is not None else None