test_orchestration_workbench_ports.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. from __future__ import annotations
  2. from agent.orchestration import (
  3. AgentRole,
  4. ArtifactRef,
  5. CriterionResult,
  6. DeterministicWorkerContext,
  7. DeterministicWorkerResult,
  8. OrchestrationConfig,
  9. RoleContextRequest,
  10. TaskCoordinator,
  11. ValidationVerdict,
  12. )
  13. from agent.orchestration.protocols import ValidatorRunResult
  14. from agent.orchestration.store import FileSystemArtifactStore, FileSystemTaskStore
  15. from agent.trace.models import Trace
  16. from agent.trace.store import FileSystemTraceStore
  17. import pytest
  18. class _TaskContext:
  19. async def render(self, root_trace_id: str, ledger: object) -> str:
  20. assert root_trace_id == "root"
  21. return '{"state_revision":"ledger:compact"}'
  22. class _RoleContext:
  23. def __init__(self) -> None:
  24. self.requests: list[RoleContextRequest] = []
  25. async def build(self, request: RoleContextRequest) -> dict[str, object]:
  26. self.requests.append(request)
  27. return {
  28. "state_revision": f"ledger:{request.ledger_revision}",
  29. "role": request.role.value,
  30. }
  31. class _DeterministicWorker:
  32. def __init__(self) -> None:
  33. self.calls: list[DeterministicWorkerContext] = []
  34. async def supports(self, context: DeterministicWorkerContext) -> bool:
  35. return True
  36. async def execute(
  37. self, context: DeterministicWorkerContext
  38. ) -> DeterministicWorkerResult:
  39. self.calls.append(context)
  40. return DeterministicWorkerResult(
  41. summary="Host assembled the immutable artifact",
  42. artifact_refs=(ArtifactRef(uri="memory://deterministic", version="1"),),
  43. )
  44. class _Executor:
  45. def __init__(self) -> None:
  46. self.coordinator: TaskCoordinator | None = None
  47. self.worker_calls = 0
  48. self.validator_calls = 0
  49. async def run_worker(self, context: dict[str, object]) -> object:
  50. self.worker_calls += 1
  51. raise AssertionError(
  52. "LLM Worker must not run when deterministic execution supports Task"
  53. )
  54. async def run_validator(self, context: dict[str, object]) -> ValidatorRunResult:
  55. self.validator_calls += 1
  56. assert context["role_context"]
  57. coordinator = self.coordinator
  58. assert coordinator is not None
  59. task_spec = context["task_spec"]
  60. assert isinstance(task_spec, dict)
  61. criteria = [
  62. CriterionResult(
  63. criterion_id=str(item["criterion_id"]),
  64. verdict=ValidationVerdict.PASSED,
  65. reason="independently checked",
  66. )
  67. for item in task_spec["acceptance_criteria"]
  68. ]
  69. await coordinator.submit_validation(
  70. {
  71. **context,
  72. "role": AgentRole.VALIDATOR.value,
  73. "trace_id": context["validator_trace_id"],
  74. "tool_call_id": "validator-submit",
  75. },
  76. ValidationVerdict.PASSED,
  77. criteria,
  78. "passed",
  79. [],
  80. [],
  81. [],
  82. "accept",
  83. )
  84. return ValidatorRunResult(str(context["validator_trace_id"]), "completed")
  85. @pytest.mark.asyncio
  86. async def test_context_ports_and_deterministic_worker_preserve_full_audit_chain(
  87. tmp_path,
  88. ) -> None:
  89. trace_store = FileSystemTraceStore(str(tmp_path / "traces"))
  90. await trace_store.create_trace(
  91. Trace(trace_id="root", mode="agent", task="mission", agent_role="planner")
  92. )
  93. role_context = _RoleContext()
  94. deterministic = _DeterministicWorker()
  95. executor = _Executor()
  96. task_store = FileSystemTaskStore(str(tmp_path / "ledger"))
  97. coordinator = TaskCoordinator(
  98. task_store,
  99. FileSystemArtifactStore(str(tmp_path / "artifacts")),
  100. trace_store,
  101. OrchestrationConfig(),
  102. executor=executor,
  103. task_context_provider=_TaskContext(),
  104. role_context_provider=role_context,
  105. deterministic_worker=deterministic,
  106. )
  107. executor.coordinator = coordinator
  108. await coordinator.ensure_ledger(
  109. "root",
  110. {
  111. "objective": "mission",
  112. "acceptance_criteria": [
  113. {"criterion_id": "root", "description": "complete", "hard": True}
  114. ],
  115. },
  116. )
  117. created = await coordinator.create_tasks(
  118. "root",
  119. [
  120. {
  121. "objective": "mechanical assembly",
  122. "acceptance_criteria": [
  123. {"criterion_id": "closed", "description": "closed", "hard": True}
  124. ],
  125. }
  126. ],
  127. )
  128. task_id = created["tasks"][0]["task_id"]
  129. cycle = (await coordinator.dispatch_tasks("root", [task_id]))[0]
  130. assert cycle.validation is not None
  131. assert cycle.validation.verdict is ValidationVerdict.PASSED
  132. assert executor.worker_calls == 0
  133. assert executor.validator_calls == 1
  134. assert len(deterministic.calls) == 1
  135. assert [item.role for item in role_context.requests] == [
  136. AgentRole.WORKER,
  137. AgentRole.VALIDATOR,
  138. ]
  139. assert (
  140. await coordinator.task_context("root") == '{"state_revision":"ledger:compact"}'
  141. )
  142. ledger = await task_store.load("root")
  143. attempt = ledger.attempts[cycle.attempt_id]
  144. assert attempt.submission is not None
  145. worker_trace = await trace_store.get_trace(attempt.worker_trace_id)
  146. assert worker_trace is not None
  147. assert worker_trace.status == "completed"
  148. assert worker_trace.total_tokens == 0
  149. assert worker_trace.context["deterministic_worker"] is True
  150. assert await trace_store.get_trace_messages(attempt.worker_trace_id) == []