protocols.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """Ports used by the orchestration domain."""
  2. from __future__ import annotations
  3. from dataclasses import dataclass
  4. from typing import Any, Dict, List, Optional, Protocol, Sequence
  5. from .models import (
  6. ArtifactSnapshot,
  7. AttemptSubmission,
  8. BackgroundOperation,
  9. EventDraft,
  10. EventPage,
  11. ExecutionStats,
  12. TaskLedger,
  13. )
  14. @dataclass(frozen=True)
  15. class CommitResult:
  16. revision: int
  17. ledger: TaskLedger
  18. @dataclass(frozen=True)
  19. class WorkerRunResult:
  20. trace_id: str
  21. status: str
  22. summary: str = ""
  23. error: Optional[str] = None
  24. execution_stats: Optional[ExecutionStats] = None
  25. @dataclass(frozen=True)
  26. class ValidatorRunResult:
  27. trace_id: str
  28. status: str
  29. summary: str = ""
  30. error: Optional[str] = None
  31. execution_stats: Optional[ExecutionStats] = None
  32. class TaskStore(Protocol):
  33. async def load(self, root_trace_id: str) -> TaskLedger: ...
  34. async def commit(
  35. self,
  36. ledger: TaskLedger,
  37. expected_revision: int,
  38. idempotency_key: Optional[str] = None,
  39. event: Optional[EventDraft] = None,
  40. ) -> CommitResult: ...
  41. async def list_events(
  42. self,
  43. root_trace_id: str,
  44. cursor: Optional[str] = None,
  45. limit: int = 100,
  46. ) -> EventPage: ...
  47. async def list_recoverable(self, root_trace_id: str) -> List[BackgroundOperation]: ...
  48. class ArtifactStore(Protocol):
  49. async def freeze(
  50. self,
  51. root_trace_id: str,
  52. attempt_id: str,
  53. submission: AttemptSubmission,
  54. ) -> ArtifactSnapshot: ...
  55. async def get(self, root_trace_id: str, snapshot_id: str) -> ArtifactSnapshot: ...
  56. async def get_for_attempt(self, root_trace_id: str, attempt_id: str) -> Optional[ArtifactSnapshot]: ...
  57. async def list_orphans(
  58. self,
  59. root_trace_id: str,
  60. known_attempt_ids: Sequence[str],
  61. ) -> List[ArtifactSnapshot]: ...
  62. async def cleanup_orphans(
  63. self,
  64. root_trace_id: str,
  65. known_attempt_ids: Sequence[str],
  66. ) -> List[str]: ...
  67. class AgentExecutor(Protocol):
  68. async def run_worker(self, context: Dict[str, Any]) -> WorkerRunResult: ...
  69. async def run_validator(self, context: Dict[str, Any]) -> ValidatorRunResult: ...
  70. async def stop(self, trace_id: str) -> bool: ...
  71. class ToolPolicy(Protocol):
  72. def resolve(self, config: Any, preset: Any, registry: Any) -> Any: ...
  73. def authorize(self, role: Any, tool_name: str, resolved_policy: Any) -> Any: ...
  74. class EventSink(Protocol):
  75. async def emit(self, root_trace_id: str, event_type: str, payload: Dict[str, Any]) -> None: ...
  76. __all__ = [
  77. "CommitResult", "WorkerRunResult", "ValidatorRunResult", "TaskStore",
  78. "ArtifactStore", "AgentExecutor", "ToolPolicy", "EventSink",
  79. ]