A worker attempt (run_agent_attempt) is the execution unit for a single issue. It manages the lifecycle of a workspace, a session, and multiple agent turns:
- Workspace Creation: A unique workspace is created for the issue's identifier.
- Hooks: The service executes a
before_run hook on the workspace path. - Session Management: An
app_server session is started using the workspace path. - The Turn Loop: The worker enters a loop that continues until the issue reaches a terminal state, the issue is no longer routable, or
max_turns (defined in config) is reached.- Prompt Building: A prompt is constructed using the workflow template, issue data, and current turn number.
- Turn Execution:
app_server.run_turn executes the prompt. It accepts an on_message callback to send updates back to the orchestrator. - State Refresh: After each turn, the worker refreshes the issue state from the tracker to check if work is complete.
- Cleanup: Once the loop terminates, the session is stopped, and an
after_run hook is executed (best-effort) to clean up the workspace.
// Simplified Worker Attempt Lifecycle
function run_agent_attempt(issue, attempt, orchestrator_channel):
workspace = workspace_manager.create_for_issue(issue.identifier)
run_hook("before_run", workspace.path)
session = app_server.start_session(workspace=workspace.path)
while turn_number < max_turns:
prompt = build_turn_prompt(workflow_template, issue, attempt, turn_number, max_turns)
turn_result = app_server.run_turn(
session=session,
prompt=prompt,
issue=issue,
on_message=(msg) -> send(orchestrator_channel, {codex_update, issue.id, msg})
)
// ... refresh issue state and check loop conditions
app_server.stop_session(session)
run_hook_best_effort("after_run", workspace.path)