test_production_runtime_observation.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from __future__ import annotations
  2. from pathlib import Path
  3. from production_build_agents.production import runtime
  4. class _Graph:
  5. def __init__(self, saved: dict, result: dict | None = None) -> None:
  6. self.saved = saved
  7. self.result = result
  8. self.invoke_calls = 0
  9. def get_state(self, _config):
  10. return type("_Snapshot", (), {"values": self.saved})()
  11. def invoke(self, _state, _config):
  12. self.invoke_calls += 1
  13. return self.result or self.saved
  14. class _Observation:
  15. created: list["_Observation"] = []
  16. def __init__(self, **payload) -> None:
  17. self.payload = payload
  18. self.finished: list[tuple[dict, bool]] = []
  19. self.__class__.created.append(self)
  20. def __enter__(self):
  21. return self
  22. def finish(self, result, *, graph_invoked: bool) -> None:
  23. self.finished.append((dict(result), graph_invoked))
  24. def __exit__(self, *_args):
  25. return False
  26. def _paths(tmp_path: Path) -> tuple[Path, Path]:
  27. delivery = tmp_path / "GlobalDataStageDelivery.v1.json"
  28. anchor = tmp_path / "anchor.png"
  29. delivery.write_text("{}", encoding="utf-8")
  30. anchor.write_bytes(b"\x89PNG")
  31. return delivery, anchor
  32. def test_runtime_reports_completed_noop_without_invoking_graph(
  33. monkeypatch,
  34. tmp_path: Path,
  35. ) -> None:
  36. delivery, anchor = _paths(tmp_path)
  37. saved = {
  38. "run_id": "round-production",
  39. "protocol_version": "0.7",
  40. "status": "COMPLETED",
  41. "phase": "FINALIZE",
  42. "max_plan_revisions": 5,
  43. }
  44. graph = _Graph(saved)
  45. _Observation.created.clear()
  46. monkeypatch.setattr(runtime, "create_production_graph", lambda **_k: graph)
  47. monkeypatch.setattr(runtime, "ProductionObservation", _Observation)
  48. monkeypatch.setattr(runtime, "validate_resume_identity", lambda *_a, **_k: None)
  49. monkeypatch.setattr(
  50. runtime,
  51. "validate_completed_production_run",
  52. lambda *_a, **_k: None,
  53. )
  54. result = runtime.run_production_graph(
  55. delivery,
  56. output_root=tmp_path / "runs",
  57. thread_id="round-production",
  58. shared_visual_anchor_path=anchor,
  59. registry_dir=tmp_path / "registry",
  60. )
  61. observation = _Observation.created[0]
  62. assert result == saved
  63. assert graph.invoke_calls == 0
  64. assert observation.payload["execution_mode"] == "completed_noop"
  65. assert observation.finished == [(saved, False)]
  66. def test_runtime_reports_fresh_graph_execution(
  67. monkeypatch,
  68. tmp_path: Path,
  69. ) -> None:
  70. delivery, anchor = _paths(tmp_path)
  71. completed = {
  72. "run_id": "fresh-production",
  73. "protocol_version": "0.7",
  74. "status": "COMPLETED",
  75. "phase": "FINALIZE",
  76. "max_plan_revisions": 5,
  77. }
  78. graph = _Graph({}, completed)
  79. _Observation.created.clear()
  80. monkeypatch.setattr(runtime, "create_production_graph", lambda **_k: graph)
  81. monkeypatch.setattr(runtime, "ProductionObservation", _Observation)
  82. monkeypatch.setattr(
  83. runtime,
  84. "record_run_invocation",
  85. lambda *_a, **_k: None,
  86. )
  87. result = runtime.run_production_graph(
  88. delivery,
  89. output_root=tmp_path / "runs",
  90. thread_id="fresh-production",
  91. shared_visual_anchor_path=anchor,
  92. registry_dir=tmp_path / "registry",
  93. )
  94. observation = _Observation.created[0]
  95. assert result == completed
  96. assert graph.invoke_calls == 1
  97. assert observation.payload["execution_mode"] == "fresh"
  98. assert observation.finished == [(completed, True)]