> ## Documentation Index
> Fetch the complete documentation index at: https://java.agentscope.io/llms.txt
> Use this file to discover all available pages before exploring further.

# How a Turn recovers from failures during tool use

> See who resumes each stage, which results survive, and when to wait, reconnect, retry a request, or verify a tool outcome.

A Turn is one submitted task. Once the service accepts a request, it can continue the same Turn after the client disconnects or an instance restarts. **Save the request ID (`requestId`) when submitting and the task ID (`turnId`) when the receipt arrives.** If submission times out, retry with the original request ID and input to retrieve the receipt. If you already have the Turn ID, use it to check the task or reconnect to its event stream.

This guide covers production deployments where Brain calls the model directly and Hands runs external tools. MQ delivers task dispatches, tool commands, and results. PostgreSQL stores accepted work and execution progress. Deployments with object storage may keep large results and recovery data there. Redis carries live previews. The tables follow the last successfully saved step and apply to each round of reasoning and tool use in a Turn.

![Turn recovery architecture. The API dispatches Turns through MQ. Brain calls the model and uses MQ to send tool commands to Hands and receive results. Services recover from persistent storage. Live previews travel from Brain through Redis to the API.](https://mintcdn.com/agent-scope/M91DlPJ8u8lUsXWx/imgs/durable-turn-recovery-en.svg?fit=max\&auto=format\&n=M91DlPJ8u8lUsXWx\&q=85\&s=e05825dba8be9250249d8a2e88bdc7d7)

Task dispatch, tool execution commands, cancellations, and results all follow the same MQ rule. Until the sender records confirmation that MQ accepted a message, it may send the original message again. Once the confirmation is recorded, MQ handles delivery and any redelivery. The recipient can recognize duplicates.

## Submission and model inference

| Where execution stopped | Who resumes the work and how | What you see and what to do |
| - | - | - |
| You submitted a request but received no receipt | If the service had yet to accept the request, your retry submits it. If it already accepted the request, the Turn can progress independently. The same request ID and input in the same Thread retrieve the original Turn. | **Retry the original request.** A timeout can appear while the task is already running. If you lost the request ID, check the Thread's tasks first. A new ID can create a second task. |
| The Turn was accepted, but Brain has yet to start it | The service retries dispatches whose MQ send confirmation is still outstanding. Once confirmation is recorded, MQ handles delivery. MQ also redelivers if Brain exits before claiming the task. | **Wait.** The Turn may appear queued. If an earlier Turn in the same Thread is paused or awaiting approval, resolve that Turn so the queue can advance. |
| Brain was preparing input and had yet to call the model | A Brain instance renews its execution lease while it is running. If it stops renewing and the lease expires, the Brain service schedules the same Turn. A replacement loads the saved input and context and starts this round of inference. | **Wait for takeover.** The first output may take longer. The original input is retained. |
| The model was generating, or Brain had received the full reply but had yet to save it | The Brain taking over requests this round again from the last saved recovery point. The model may generate a different answer. | **Wait.** Streaming text may pause and resume with different wording. Use the final saved message. |
| Brain saved the complete model reply but had yet to process its tool call | When the original instance's lease expires, the Brain service schedules a replacement. It reads the saved reply and handles the tool call or finishes the answer, without another model request for that reply. | **Wait.** The reply may appear before its tool call or approval prompt. Recovery proceeds even if you close the page. |

## Tool dispatch and execution

| Where execution stopped | Who resumes the work and how | What you see and what to do |
| - | - | - |
| Brain identified a tool call but the command had yet to reach MQ, or the send confirmation was still outstanding | Brain resumes permission checks and dispatch from the saved model reply or tool request. The service keeps the original tool execution ID when retrying a send, so Hands can recognize duplicates. | **Wait.** A tool request may be visible before any output appears. If prompted for approval or a manual result, answer the original interaction. |
| MQ accepted the command, but Hands had yet to receive it or exited before saving it | MQ delivers or redelivers the original command. Hands acknowledges consumption after saving the accepted work. | **Wait.** The tool may appear dispatched for a while. You can continue watching the original call. |
| Hands accepted the tool but had yet to start it | Hands normally schedules execution as soon as it accepts a call. If it restarts in between, an available Hands instance finds the saved work and starts the original call. | **Wait.** Tool logs may remain empty until execution starts. |
| The tool may have started, is running, or finished before Hands saved the result | Hands uses the original call identity to check the tool service. It follows work still in progress and retrieves completed results. It can safely retry a start only if the tool service guarantees that the same identity refers to the same operation. | **Allow time for verification.** A queryable tool may simply take longer to report its result. If the outcome is `UNKNOWN`, the external action may already have taken effect. Have an authorized operator verify it. |
| Hands stops after preparing a Sandbox call and before launching the tool | The replacement resumes the prepared operation in the original Sandbox. Concurrent launchers compete for one durable execution permit. | Keep the same execution ID. If the permit and outcome are both missing, inspect the original operation before retrying. |
| A Sandbox command lost its connection or log reading stopped partway through | Hands reconnects to the original Sandbox, checks the same command, and reads available output from the last saved position. The command may have continued while disconnected. Loss of the Sandbox or its execution record can leave the outcome unconfirmed. | **Follow the original call.** Logs may arrive late or show an explicit gap. If the outcome is `UNKNOWN`, provide the original execution ID for verification. |

A provider with idempotent Sandbox creation can reopen the resource identified by the original creation receipt. The local POSIX adapter atomically publishes the initialized workspace and its identity marker together. Once ready, a missing workspace requires reconciliation.

For example, a tool creates a support ticket and Hands restarts before saving the ticket number. If the external service can look up the ticket by the original call identity, Hands can recover its result. If lookup is unavailable and creation is not idempotent, issuing the command again could create a second ticket. An authorized operator should locate the original ticket and record the verified outcome against the original tool call.

## Tool results, subsequent inference, and completion

| Where execution stopped | Who resumes the work and how | What you see and what to do |
| - | - | - |
| Hands saved the tool result but had yet to send it to MQ or record send confirmation | Hands sends the saved result again if needed. Brain recognizes a duplicate if MQ received the earlier send. | **Wait for the result.** The external operation may have finished while the tool still appears active. You can leave the tool call as it is. |
| MQ accepted the result, but Brain had yet to receive it or exited before saving it | MQ delivers or redelivers the original result. If the completion message arrives ahead of earlier output, Brain saves it and applies the messages in order once MQ delivers the rest. | **Wait.** Output or completion may appear late. If MQ exhausts its delivery retries, an operator must inspect the dead-lettered message and redeliver it if appropriate. |
| Brain saved a tool result but had yet to start the next inference | Once the required results are ready, the service schedules the original Turn. Brain adds the saved results to the model context. If that step already finished, the Brain taking over continues with the next inference. | **Wait for the next reply.** The tool result may already be visible. With parallel calls, completed results are kept while Brain waits for the remaining required tools. |
| The next round of inference was interrupted | Brain resumes with the tool results already in context. It can reuse a saved complete model reply or request the current round again if the reply was still unsaved. | **Wait.** Subsequent wording may change. Completed tools from the preceding round keep their results. |
| The final reply was generated, but the Turn still appears active | Brain uses the saved reply and progress to finish any remaining work, such as saving workspace updates, then records the final Turn status. | **Wait for the final status.** The answer may become visible before the Turn shows completion. |
| The Turn ended, but the client missed the completion notification | The service retains the committed final status and reply. The client can query the original Turn ID. With SSE, use `Last-Event-ID` to reconnect from the last event position processed. | **Query or reconnect.** If the event cursor expired, read a current snapshot for the same scope and resume from its position. |

## When a task keeps waiting

| What you see | What lets the Turn progress |
| - | - |
| A pending approval, a request for a manual result, or an explicit pause | Answer the original interaction by approving, declining, or supplying the requested result. If the response receipt is uncertain, retry the same action with its original request ID. An explicitly paused Turn requires a resume. Outstanding approvals and tools still apply afterward. |
| An approval response or follow-up instruction was accepted, but progress has yet to change | Accepted input is saved. Brain reads it at the next point where it can apply input, including after a restart. Once you have the receipt, keep watching the original Turn. |
| Cancellation was accepted, but the tool still appears active | The service delivers the cancellation to Hands. The external tool must confirm it stopped or report its actual outcome. Keep watching the original Turn and have an authorized operator verify any unresolved effect. |
| A native tool was interrupted while executing in Brain | If a trustworthy result exists, Brain reuses it. If the tool may have affected an external system and its result is missing, an authorized operator must verify the original call and supply the outcome. An ordinary approval cannot resolve this wait. |
| A model or service error triggered retries, or the Turn ended as `FAILED` | Retryable errors follow the deployment's backoff policy within the same Turn. If retries are exhausted or the error is permanent, the service settles outstanding tools. Once failure is final, submit a new Turn if you intend to run the task again. |
| A parent task is still waiting on a child | The service locates the existing child task by its original identity. A parent Turn waiting for the result continues when it becomes available. Check the original child or the deadline for that wait. |

## When a service or connection is unavailable

| Failure | Impact and action |
| - | - |
| A Brain or Hands instance exits | An available compatible instance takes over. If all instances for that work are offline, an operator must restore service. Takeover time depends on the remaining execution lease, queued work, and capacity. You can keep checking the original Turn. |
| MQ is unavailable or a message enters the dead-letter queue | When MQ recovers, the service resumes sends whose confirmation is still outstanding, and MQ continues delivering accepted messages. An operator must inspect dead-lettered messages and redeliver them if appropriate. Keep watching the original task. |
| The client disconnects, its wait times out, or Redis previews stop | Reconnect to the original Turn. Missed live fragments may be unavailable, but saved messages, tool output, and the final status remain queryable. If the deployment connects to Redis at startup, replacement instances may also have to wait for Redis to recover. |
| PostgreSQL or the configured object store is unavailable | Submission, queries, or execution may stall. Once storage recovers, the service continues from the last saved point. Recovery also requires the task's Agent definition, tool bindings, and necessary workspace data. Permanent loss of required data can make the original Turn unrecoverable. |

For the APIs for Turn status, event subscriptions, pause and resume, and tool outcome verification, see the [Durable execution guide](https://java.agentscope.io/dev/en/docs/harness/durable.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.