config_worker_save.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """What ``save_config`` persists when the process serves a frozen session config.
  2. A managed worker binds its session's creation-time config (``agent.safe_worker_policy``), so its
  3. ``load_config()`` is that snapshot, not the profile file. A read-modify-write in such a worker
  4. (an ``always`` approval, a connector enable) must land only what it changed on the LIVE file:
  5. writing the whole snapshot back would revert every profile edit made since the session was
  6. created. Outside a frozen worker this is ``save_config``'s ordinary ``merge_existing`` handling.
  7. """
  8. from __future__ import annotations
  9. import copy
  10. from typing import Any
  11. def rebase_for_save(config: dict[str, Any], live_raw: dict[str, Any], merge_existing: bool) -> dict[str, Any]:
  12. from hermes_cli import config as _config
  13. snapshot = _config._worker_config_snapshot()
  14. if snapshot is not None:
  15. return _three_way(live_raw or {}, config, _config.load_config(), _frozen_for_deletion(snapshot))
  16. if merge_existing and live_raw:
  17. return _config._merge_partial_save(live_raw, config)
  18. return config
  19. def _frozen_for_deletion(snapshot: dict[str, Any]) -> Any:
  20. """The snapshot branches that count as operator state.
  21. An ordinary snapshot only contains what the profile had at creation, so an explicit
  22. empty allowlist is a real branch. A bypass worker (safe mode, ``--ignore-user-config``)
  23. freezes a copy of the schema, so a branch that is still the schema default was never
  24. an operator choice and must not count as a deletion."""
  25. from agent.safe_worker_policy import _current_policy
  26. policy = _current_policy()
  27. if policy is None or not policy.ignore_user_config:
  28. return snapshot
  29. from hermes_cli.config_defaults import DEFAULT_CONFIG
  30. return _schema_default_branch(snapshot, DEFAULT_CONFIG)
  31. def _schema_default_branch(snapshot: Any, defaults: Any) -> Any:
  32. """*snapshot* without branches whose value is still the schema default."""
  33. if not isinstance(snapshot, dict) or not isinstance(defaults, dict):
  34. return copy.deepcopy(snapshot)
  35. kept: dict[str, Any] = {}
  36. for key, value in snapshot.items():
  37. if key not in defaults:
  38. kept[key] = copy.deepcopy(value)
  39. continue
  40. default = defaults[key]
  41. if value == default:
  42. continue
  43. if isinstance(value, dict) and isinstance(default, dict):
  44. nested = _schema_default_branch(value, default)
  45. if nested:
  46. kept[key] = nested
  47. else:
  48. kept[key] = copy.deepcopy(value)
  49. return kept
  50. _SAME_AS_BASE = object()
  51. def _three_way(live: Any, desired: Any, base: Any, frozen: Any = _SAME_AS_BASE) -> Any:
  52. """*live* with the caller's edits (*desired* relative to the *base* it loaded) applied.
  53. Mappings recurse; list edits add/remove items; a key the caller dropped is left alone. A
  54. mapping/list branch deleted (or retyped) on *live* since *base* stays as live has it: a
  55. leaf edit must not resurrect the stale snapshot subtree around it.
  56. *frozen* is the operator-owned snapshot under *base* (``load_config()`` merges the schema
  57. defaults over the raw snapshot). A branch in neither *frozen* nor *live* came only from the
  58. defaults, so nothing deleted it: the edit applies to the default (a fresh profile's first
  59. ``always`` grant, including one made in safe mode). A branch the operator set and the live
  60. file then lost stays deleted."""
  61. if frozen is _SAME_AS_BASE:
  62. frozen = base
  63. if desired == base:
  64. return copy.deepcopy(live)
  65. if isinstance(desired, dict) and isinstance(base, dict) and isinstance(live, dict):
  66. frozen_map = frozen if isinstance(frozen, dict) else {}
  67. merged = copy.deepcopy(live)
  68. for key, value in desired.items():
  69. if key not in base:
  70. merged[key] = copy.deepcopy(value)
  71. elif value != base[key]:
  72. kind = dict if isinstance(base[key], dict) else list if isinstance(base[key], list) else None
  73. current = live.get(key)
  74. if kind is not None and not isinstance(current, kind):
  75. if key in live or key in frozen_map:
  76. continue # concurrently deleted/replaced live branch: live wins
  77. current = copy.deepcopy(base[key]) # default-only branch
  78. merged[key] = _three_way(current, value, base[key], frozen_map.get(key, {}))
  79. return merged
  80. if isinstance(desired, list) and isinstance(base, list) and isinstance(live, list):
  81. removed = [item for item in base if item not in desired]
  82. kept = [item for item in live if item not in removed]
  83. return kept + [copy.deepcopy(item) for item in desired if item not in base and item not in kept]
  84. return copy.deepcopy(desired)