diff --git a/docs/agent-relay/configuration.md b/docs/agent-relay/configuration.md index f72a3bee553..47459378c99 100644 --- a/docs/agent-relay/configuration.md +++ b/docs/agent-relay/configuration.md @@ -124,6 +124,7 @@ Refer to [Pool fields](#pool-fields) and [Agent Relay for Cursor](./cursor.md). | `template_parameters` | No | Fixed parameter values that Agent Relay sets on every build from this pool, as a map of parameter names to values. Use it for required or environment-specific parameters. Agent Relay doesn't log the values. | | `idle_release_timeout` | No | How long a session can go without a new message after the agent finishes a turn before it's released and its workspace is deleted, as a duration such as `30m`. Defaults to `1h`. `0` turns off idle release. Claude Code needs whole minutes, up to `168h`. Cursor needs whole seconds and logs a warning at startup for values below `5m`. Agent Relay refuses to start on a number without a unit. | | `template_version` | No | A version name or UUID of `template` that every build from this pool uses, including rebuilds. Leave it unset to use the active version. Refer to [Pin a template version](#pin-a-template-version). | +| `reap_delay` | No | Overrides [`--reap-delay`](#--reap-delay) for this pool, as a duration that isn't negative, such as `10m`. Set `0s` to turn off the delay for this pool. | Agent Relay also accepts an inline `secret` field in place of `secret_env`. Set exactly one of the two. @@ -278,6 +279,73 @@ The Helm chart always binds to `0.0.0.0` on `prometheus.port`. ## Workspace reaper +The reaper is the process that deletes relay workspaces when their sessions end or fail. +Two options control when it deletes a workspace: + +- `--reap-startup-grace` applies while a workspace starts. + It sets how long a new workspace has to build and start its worker before the reaper deletes it. +- `--reap-delay` applies after the reaper decides to delete a workspace. + It sets how long the reaper keeps the workspace first, so you can inspect it. + +The other options in this section limit how many workspaces the reaper handles at once and tune when it extends an attached workspace's autostop deadline. +For how the reaper works and which setting to change, refer to [Workspace lifecycle](./setup.md#workspace-lifecycle). + +### `--reap-startup-grace` + +| Property | Value | +|-------------|----------------------------------------------------| +| Type | duration | +| Environment | $CODER_AGENT_RELAY_REAP_STARTUP_GRACE | +| YAML | reapStartupGrace | +| Helm | reapStartupGrace | +| Default | 15m0s | + +How long a new workspace has to build and start its worker before the reaper deletes it. +The grace starts when the latest build is created, so it includes the time spent building. +Must be positive. + +- If a build is still pending or starting after the longer of this grace and the pool's `dispatch_deadline`, the reaper deletes the workspace. +- A workspace where the build succeeded but the worker still reports no status, or `pending`, fails after this grace. +- A short grace can fail workspaces where the agent connects slowly or the worker starts slowly. + Unless you run an isolated test deployment, keep the default. +- When the template orders the worker after setup scripts, this is the budget those scripts share with the build. + Refer to [Start the worker after workspace setup](./setup.md#start-the-worker-after-workspace-setup). + +The reaper acts on the first [sweep](./setup.md#workspace-lifecycle) after the grace passes, which can be 30 seconds or more later. +Raise the grace when a working template takes longer than 15 minutes to build and start its worker. +Refer to [Choose cleanup settings](./setup.md#choose-cleanup-settings). + +### `--reap-delay` + +| Property | Value | +|-------------|--------------------------------------------| +| Type | duration | +| Environment | $CODER_AGENT_RELAY_REAP_DELAY | +| YAML | reapDelay | +| Default | 0s | + +How long the reaper keeps a workspace after it decides to delete it, so you can inspect the workspace first. +The delay counts from when the latest build finished, whether that build succeeded, failed, or was canceled. +It doesn't count from when the worker exits, so a session that runs longer than the delay gets no extra time. +The delay applies when a worker exits cleanly, when a worker fails, and when a build fails or is canceled. +For a failed worker, the reaper also waits before it reports the failure to the provider, which postpones provider retries and the failure messages that developers see. +Must not be negative. +A pool's `reap_delay` overrides this option. + +- If a build fails or is still running when the pool's `dispatch_deadline` passes, Agent Relay deletes the workspace immediately. +- If autostop could stop the workspace before the delay ends, the reaper deletes it right away. +- A worker that never starts fails after the startup grace, so its delay might already have passed. +- If the provider sends the session again and Agent Relay rebuilds the workspace, the delay starts over from the new build. +- While the reaper keeps a workspace, each sweep counts a `delayed` verdict instead of `delete` or `failed`. + When the delay passes, the next sweep counts `delete` or `failed` and acts. + With `--log-level` set to `debug`, each kept workspace logs `workspace awaiting reap` with `reap_at`, the time when the delay ends. + +Set a delay while you debug a worker that fails soon after it starts, so you can open the workspace and read its logs before Agent Relay deletes it. +To keep a workspace where the worker fails later in a session, set a delay longer than the time between the build finishing and the worker failing. +Refer to [Choose cleanup settings](./setup.md#choose-cleanup-settings). +The Helm chart has no dedicated value for this option. +Set it with [`extraEnv`](#helm-values). + ### `--reap-workers` | Property | Value | @@ -292,24 +360,6 @@ Workspace statuses and session attachment arrive with the workspace list, so thi The Helm chart has no dedicated value for this option. Set it with [`extraEnv`](#helm-values). -### `--reap-startup-grace` - -| Property | Value | -|-------------|----------------------------------------------------| -| Type | duration | -| Environment | $CODER_AGENT_RELAY_REAP_STARTUP_GRACE | -| YAML | reapStartupGrace | -| Helm | reapStartupGrace | -| Default | 15m0s | - -How long the reaper waits, from the start of the latest build, for the build to finish and the worker to report in. -Past it, Agent Relay deletes a workspace that's still building, and fails the session of a built workspace where the worker never started. -A workspace that's still building also waits for its pool's `dispatch_deadline` when that's longer. -Must be positive. -A short grace might cut off slow agent connections and slow worker startups. -When the template orders the worker after setup scripts, this is the budget those scripts share with the build. -Refer to [Start the worker after workspace setup](./setup.md#start-the-worker-after-workspace-setup). - ### `--reap-keepalive-refresh-before` | Property | Value | diff --git a/docs/agent-relay/cursor.md b/docs/agent-relay/cursor.md index a4fbc405b80..48d4bb52f28 100644 --- a/docs/agent-relay/cursor.md +++ b/docs/agent-relay/cursor.md @@ -197,7 +197,8 @@ Worker startup after the build doesn't. - The default is `10m` and the maximum is `15m`. Set `0` to turn off the deadline. - When the deadline passes, Agent Relay deletes the workspace and the developer's chat shows `agent-relay: workspace build exceeded the spawn budget`. -- Regardless of the deadline, Agent Relay deletes a workspace that's still building after the [startup grace](./configuration.md#--reap-startup-grace), 15 minutes from its latest build by default. +- Regardless of the deadline, Agent Relay deletes a workspace that's still building after the longer of `dispatch_deadline` and the reaper's [startup grace](./configuration.md#--reap-startup-grace). + The grace is 15 minutes by default and starts when the latest build is created. Slow templates are the most common reason sessions don't start. Pre-pull images, avoid persistent volumes, and bake the Cursor CLI into the image with `install_cli = false`. @@ -341,7 +342,7 @@ The module reports the worker's state through `agent_relay_status`, and Agent Re | `done ` | The worker exited with that status. `0` is a normal release. | Delete on `0`, fail on anything else. | | `failed ` | The worker couldn't start, such as `runner-agent-missing` when the CLI is absent. | Fail. | -Both delete and fail remove the workspace. +Both delete and fail remove the workspace, after the [reap delay](./configuration.md#--reap-delay) if you set one. A failure is also recorded in logs and metrics, and ends the developer's turn in Cursor with an error if the turn is still waiting. After each turn, the worker waits for a follow-up for the pool's `idle_release_timeout`, then exits, and Agent Relay deletes the workspace. diff --git a/docs/agent-relay/monitoring/index.md b/docs/agent-relay/monitoring/index.md index d3e8fe57554..0eab39e3cb9 100644 --- a/docs/agent-relay/monitoring/index.md +++ b/docs/agent-relay/monitoring/index.md @@ -105,13 +105,13 @@ Shared dispatcher, reaper, and pool health metrics also carry a `provider` label ### Shared metrics -| Metric | What it tells you | -|-----------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `agent_relay_dispatcher_dispatches_total{result}` | Dispatch outcomes: `created`, `rebuilt`, `skipped_serving`, `skipped_deleting`, `expired`, or `failed`. | -| `agent_relay_dispatcher_claim_to_workspace_built_seconds` | Time from claim to a successful build. Compare it with the pool's build budget or deadline. | -| `agent_relay_reaper_workspaces{status}` | Workspaces by worker status, updated each sweep. Active sessions are `working` plus `serving`. | -| `agent_relay_reaper_verdicts_total{verdict}` | Reaper decisions, such as `keep`, `delete`, `failed`, and `stuck`. `stuck` means three delete builds failed in a row and the workspace needs manual cleanup. | -| `agent_relay_pool_healthy` | `1` if the pool's last verification passed, `0` if it failed or hasn't run yet. | +| Metric | What it tells you | +|-----------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `agent_relay_dispatcher_dispatches_total{result}` | Dispatch outcomes: `created`, `rebuilt`, `skipped_serving`, `skipped_deleting`, `expired`, or `failed`. | +| `agent_relay_dispatcher_claim_to_workspace_built_seconds` | Time from claim to a successful build. Compare it with the pool's build budget or deadline. | +| `agent_relay_reaper_workspaces{status}` | Workspaces by worker status, updated each sweep. Active sessions are `working` plus `serving`. | +| `agent_relay_reaper_verdicts_total{verdict}` | Reaper decisions: `keep`, `delete`, `failed`, `stuck`, and `delayed`. `stuck` means three delete builds failed in a row and the workspace needs manual cleanup. `delayed` means the reaper decided on `delete` or `failed` and is waiting for the workspace's [reap delay](../configuration.md#--reap-delay) to pass. It counts once per sweep while the reaper waits, not once per workspace. When the delay passes, the workspace counts as `delete` or `failed`. | +| `agent_relay_pool_healthy` | `1` if the pool's last verification passed, `0` if it failed or hasn't run yet. | ### Claude Code metrics diff --git a/docs/agent-relay/setup.md b/docs/agent-relay/setup.md index d187d59e20d..f24d06a2f03 100644 --- a/docs/agent-relay/setup.md +++ b/docs/agent-relay/setup.md @@ -397,13 +397,46 @@ For logs, metrics, and troubleshooting, refer to [Monitor Agent Relay](./monitor ## Workspace lifecycle -The provider module publishes the worker's state through the `agent_relay_status` metadata item. -Agent Relay's reaper reads it on each sweep: +Agent Relay cleans up the workspaces it creates with a background process called the reaper. +The reaper works in passes called sweeps. +In each sweep, it reads every relay workspace's `agent_relay_status` metadata item, which the provider module publishes, and acts on it. +After a sweep ends, the reaper waits 30 seconds before it starts the next one. +A sweep checks each pool in turn, so it takes longer when you have more pools and workspaces. +The reaper can act 30 seconds or more after a workspace reaches one of these states: -- When the worker exits normally, Agent Relay deletes the workspace. -- When the worker fails or disappears, Agent Relay deletes the workspace and records the failure. -- When a build fails or is canceled, Agent Relay deletes the workspace on the next sweep. -- Agent Relay deletes a workspace that is still building or not started after the [startup grace](./configuration.md#--reap-startup-grace), 15 minutes by default. +| Workspace state | What the reaper does | +|-----------------------------------------------------------------------------------------------------|------------------------------------------------------------------| +| The worker is starting, working, or serving a session | Keeps the workspace. | +| The worker exited normally | Deletes the workspace. | +| The worker failed or disappeared | Reports the failure to the provider, then deletes the workspace. | +| The build succeeded, but the worker hasn't started within the startup grace | Reports the failure to the provider, then deletes the workspace. | +| The build failed or was canceled | Deletes the workspace. | +| The build is still running after the longer of the startup grace and the pool's `dispatch_deadline` | Cancels the build, then deletes the workspace. | + +The startup grace is 15 minutes by default and starts when the latest build is created. +If you set a [reap delay](./configuration.md#--reap-delay), the reaper waits for it before it deletes a workspace or reports a failure, except in the cases that the option's reference lists. +If a delete fails three times in a row, the reaper stops and leaves the workspace for you to remove. +Refer to [A workspace is never deleted](./troubleshooting.md#a-workspace-is-never-deleted). + +### Choose cleanup settings + +Keep the defaults unless you need one of these outcomes: + +| Goal | Setting | Default | +|-------------------------------------------------------------------|------------------------------------------------------------------------------------------------|--------------------------------| +| Give a slow template more time to build and start its worker | [`--reap-startup-grace`](./configuration.md#--reap-startup-grace) | `15m` | +| Inspect a workspace where the worker failed soon after it started | [`--reap-delay`](./configuration.md#--reap-delay) for every pool, or `reap_delay` for one pool | `0s`, which deletes right away | +| Keep autostop from stopping a workspace during a session | The [session attachment metadata item](#keep-attached-workspaces-running) | Off | +| Release idle sessions sooner or later | The pool's [`idle_release_timeout`](./configuration.md#fields-for-every-pool) | `1h` | + +The other reaper options, [`--reap-workers`](./configuration.md#--reap-workers), [`--reap-keepalive-refresh-before`](./configuration.md#--reap-keepalive-refresh-before), and [`--reap-keepalive-bump-by`](./configuration.md#--reap-keepalive-bump-by), limit how many workspaces a sweep handles at once and tune when the reaper extends an attached workspace's autostop deadline. +You don't need them for the goals in this table. + +Each setting has a cost: + +- A longer startup grace also means a broken template takes longer to fail, so developers wait longer before they see an error. +- A reap delay postpones provider retries and the failure messages that developers see, and the kept workspaces keep using resources. + Set it while you debug, then set it back to `0s`. Keep these Coder settings in mind for relay templates: diff --git a/docs/agent-relay/troubleshooting.md b/docs/agent-relay/troubleshooting.md index 02d785f1d96..91a8d8f036b 100644 --- a/docs/agent-relay/troubleshooting.md +++ b/docs/agent-relay/troubleshooting.md @@ -12,12 +12,12 @@ To read logs and metrics, refer to [Monitor Agent Relay](./monitoring/index.md). ## Find where a failure happened -| Symptom | Where to look | -|-------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| A pool isn't serving | `/readyz` or `agent_relay_pool_healthy`, then the startup logs for the pool. A ready pool logs `template verified, polling` for Claude Code or `pool verified, serving` for Cursor. A pool that fails a check, such as a template without the module's parameters, logs `pool failed verification, retrying`. | -| A session never creates a workspace | Logs for the `session_id`, or the `session_uuid` for Claude Code. Look for identity resolution failures, a degraded pool, a dispatch error, or `job_error` on a failed build. | -| The workspace exists but the agent never starts | The workspace's build log, then the module logs in the workspace, then the `agent_relay_status` metadata on the workspace page in Coder. | -| A session ended but its workspace remains | Reaper decisions in the logs for the `workspace_id`. Agent Relay deletes on a sweep interval, and doesn't delete stopped workspaces. | +| Symptom | Where to look | +|-------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| A pool isn't serving | `/readyz` or `agent_relay_pool_healthy`, then the startup logs for the pool. A ready pool logs `template verified, polling` for Claude Code or `pool verified, serving` for Cursor. A pool that fails a check, such as a template without the module's parameters, logs `pool failed verification, retrying`. | +| A session never creates a workspace | Logs for the `session_id`, or the `session_uuid` for Claude Code. Look for identity resolution failures, a degraded pool, a dispatch error, or `job_error` on a failed build. | +| The workspace exists but the agent never starts | The workspace's build log, then the module logs in the workspace, then the `agent_relay_status` metadata on the workspace page in Coder. | +| A session ended but its workspace remains | Reaper decisions in the logs for the `workspace_id`. Agent Relay deletes on the next [sweep](./setup.md#workspace-lifecycle), which can be 30 seconds or more later, and doesn't delete stopped workspaces. With a [reap delay](./configuration.md#--reap-delay), the reaper has already decided to delete the workspace and keeps it until the delay passes, counted from when the latest build finished. While it waits, each sweep counts a `delayed` verdict, and debug logs show `workspace awaiting reap` with `reap_at`. | Module logs are in the workspace, under `$HOME/.coder-modules/coder/agent-relay-claude-code/logs/` or `$HOME/.coder-modules/coder/agent-relay-cursor/logs/`. They're deleted with the workspace.