@@ -268,6 +268,22 @@ class DriverState(Enum):
268268
269269
270270class SyncOrganicJobDriver :
271+ """
272+ Drives an organic job through its lifecycle via a state machine.
273+
274+ State flow:
275+ CONNECT → RESERVE_EXECUTOR → WAIT_JOB_ACCEPTED → SEND_JOB_ACCEPTED_RECEIPT
276+ → WAIT_EXECUTOR_READY → PREPARE_VOLUMES → WAIT_VOLUMES_READY
277+ → [WAIT_STREAMING_JOB_READY] → WAIT_EXECUTION_DONE → COLLECT_RESULTS → COMPLETE
278+
279+ Any state can transition to FAILED if an error occurs or a timeout is reached.
280+ The WAIT_STREAMING_JOB_READY state is only entered for streaming jobs.
281+
282+ The driver communicates with the miner via MinerClient and reports status updates
283+ to the facilitator via the status_callback. Receipts are created and persisted
284+ at key points (job started, accepted, finished) for the receipts protocol.
285+ """
286+
271287 def __init__ (
272288 self ,
273289 miner_client : MinerClient ,
@@ -505,9 +521,16 @@ def _wait_for_message(
505521 timeout_status_reason : HordeFailureReason ,
506522 ) -> _MinerMsgT | DriverState :
507523 """
508- Waits for a specific message type from the miner within a given timeout period.
509- Returns the received message if successful.
510- Returns a terminal DriverState if the timeout is reached or if an error message is received.
524+ Waits for a specific message type from the miner within a timeout.
525+
526+ Returns either:
527+ - The expected message type on success
528+ - DriverState.FAILED if timeout/error occurs (job is marked failed, events recorded)
529+
530+ Handles protocol-level errors (GenericError, UnauthorizedError) and job-level
531+ failures (V0DeclineJobRequest, V0JobFailedRequest, V0HordeFailedRequest) by
532+ transitioning to the FAILED state. Ignores manifest requests and messages
533+ for other jobs.
511534 """
512535 deadline = time .time () + timeout
513536 while True :
@@ -607,6 +630,10 @@ def _wait_for_message(
607630 )
608631
609632 def run (self ) -> None :
633+ """
634+ Execute the job lifecycle. Handles exceptions by marking the job as failed
635+ and always ensures cleanup (sending failure receipt, undoing allowance, closing connection).
636+ """
610637 try :
611638 self ._run ()
612639 except Exception as exc :
@@ -685,6 +712,10 @@ def _undo_allowance_if_failed(self) -> None:
685712 )
686713
687714 def _run (self ) -> None :
715+ """
716+ Main state machine loop. Each iteration executes the handler for the current
717+ state, which returns the next state. Terminates when COMPLETE or FAILED.
718+ """
688719 while True :
689720 match self ._state :
690721 case DriverState .CONNECT :
0 commit comments