Repository navigation
feat(agent-server): per-conversation runtime choice and runtime settings - #5977
Conversation
…ntime POC. With OH_CONVERSATION_RUNTIME_SELECTABLE=1 the server serves both runtimes: local routes keep their paths, docker routes are mounted again under a hidden /_docker prefix, and RuntimeDispatchMiddleware rewrites a request there when its conversation has a Docker provisioning manifest or when a start request sets conversation_runtime="docker". - StartConversationRequest.conversation_runtime (unset = server default) - ServerInfo.available_conversation_runtimes + selectable_conversation_runtime_v1 - ConversationRuntimeInfo.conversation_runtime
… live POC. Canvas stores these with PATCH /api/settings; unset values keep the startup Config: - Docker: retention days, disk budget, idle stop, memory, CPUs, browser (only narrows). The reclaimer re-reads its policy every pass and the idle-eviction loop its TTL every tick, so both loops always run. - Worktree: retention days (removes worktrees of inactive conversations), remove-on-delete (optionally deleting the openhands/<id> branch), and base "head" to branch from the checkout's HEAD instead of the default branch. server_info advertises runtime_settings_v1.
Worktree cleanup on delete landed on main (#5547), and the Docker knobs stay env-only for now, so misc_settings.runtime has nothing left to carry.
…ntime main dropped the Literal import from request.py, so the merged model failed to build.
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
Coverage Report •
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Runtime options become typed ConversationSettings fields (section "runtime") instead of reading misc_settings: worktree_base, and the container memory, CPU and browser for Docker conversations. They reach the server on StartConversationRequest (worktree_base, container); the Docker runtime records the container options in the conversation's provisioning manifest at first start and applies them on every container build. Unset options keep the server's Config defaults; the browser option can only turn the browser off.
The Runtime page has four fields; hiding three behind the All tier only made them harder to find.
- Worktree guidance names the ref the worktree really branched from; the base now lives on _WorktreePlan instead of a separate argument. - A retried Docker start (no container accepted the conversation yet) may change its container options; they reuse the request _prepare_forward already validated instead of validating it again. - serves_persisted_event_reads takes the conversation id, replacing the property plus *_for method pair. - Drop the unread ConversationRuntimeInfo.conversation_runtime field and the selectable_conversation_runtime_v1 capability, which repeated available_conversation_runtimes. - ConversationContainerOptions.browser is a plain bool (True can never turn on a missing browser); WorktreeBase is defined once in settings.
_container_options filtered with `v not in (None, True)`; since 1.0 == True in Python, container_cpus=1 was dropped and the server default applied instead. Send the browser only as an opt-out and drop only None values.
conversation_runtime (local | docker, unset by default) joins the "runtime" settings section, so Settings → Runtime can pick where new conversations run. create_request() forwards it only when set: unset keeps the server's default, and a single-runtime server never receives a runtime it would reject. ConversationRuntime is defined once and reused by StartConversationRequest.
container_memory accepted any string and container_cpus anything above
zero, so a value docker run refuses ("lots", 0.001) saved fine and then
failed every Docker conversation start with a 502. Both models now use
Docker's own memory syntax (DOCKER_MEMORY_PATTERN) and its 0.01 CPU
minimum, so the bad value is refused with a 422 when it is saved. The
descriptions say the options apply to new Docker conversations.
… options ConversationSettings.default_runtime (default "local") replaces the unreleased optional conversation_runtime setting and adds the worktree choice. The runtime's own options now depend on it: worktree_base shows for "worktree", the container limits and browser for "docker". To say "shows when a choice has this value", depends_on accepts "field=value" (documented on SettingsFieldMetadata). create_request() sends conversation_runtime="docker" or worktree=True; "local" sends nothing, so a Docker-only server is not handed a runtime it would reject.
- worktree_options (base, ref, branch_prefix) replaces the unreleased worktree_base request field, mirroring how `container` groups the Docker options. - A worktree can branch from a given branch, tag or commit (base "ref", fetched from origin first, like the default branch) besides the default branch and HEAD; the agent's guidance names it. - Its branch is <branch_prefix><conversation id>, "openhands/" by default. - Settings: worktree_base gains "ref" with worktree_base_ref (shown only then), plus worktree_branch_prefix, both under the worktree runtime. A ref or prefix may not start with "-", so git never reads it as an option, and "ref" without a ref is refused.
GET /api/worktrees lists every directory under the worktree root (also
leftovers no conversation uses) with its conversations, branch, size,
last activity, whether it holds uncommitted or untracked changes, and
whether one of its conversations is loaded.
DELETE /api/worktrees/{name} removes one only when it is clean and none
of its conversations is open (409 otherwise; 404 for anything that is
not a directory directly under the root). The branch stays. Before
removing, each conversation using it records the repository, path and
branch (StoredConversation.removed_worktree), and loading it again
re-creates the worktree from that branch, so the conversation keeps
working.
listWorktrees() and removeWorktree(name) wrap GET /api/worktrees and
DELETE /api/worktrees/{name}, so Canvas reaches them through a typed
client. The endpoint audit allow-lists both as client-ahead until the
pinned Agent Server release ships them.
The 204 went out with a JSON content type and no body, so the typed
client's JSON parse failed and Canvas reported an error for a removal
that had worked. DELETE /api/worktrees/{name} now returns
{"removed": true}, like DELETE /api/workspaces returns {"deleted": ...};
WorktreesClient.removeWorktree() returns it.
…n empty 204" This reverts commit 3f3af87.
…anup routes" This reverts commit 39d990b.
This reverts commit 248024a.
create_request() mapped default_runtime and the worktree/container options into every request it built, but its only caller is the OpenAI gateway, which starts conversations in-process: a "worktree" default gave every gateway conversation a worktree, and a "docker" one was silently ignored. Canvas builds its own start request from these settings, so the mapping goes. Also: `container` moves from ConversationConfig to StartConversationRequest so it is no longer written into every conversation's meta.json (the Docker runtime keeps it in the provisioning manifest); the retried-start test now really retries after a saved, failed start; server_info reuses ConversationRuntime; and small cleanups (an unused default, a clearer config description).
1ece686 to
b250627
Compare
…its route prefix Drops an unneeded postponed-annotations import, marks DOCKER_ROUTE_PREFIX as Final[str] and gives the registry a short docstring.
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Reviewed at head d7eafa8b75546c2a62d45e292762649360225b88 (21 files, +884/-36). Focused suites pass locally (tests/agent_server/docker_runtime, tests/agent_server/test_settings_router.py, tests/sdk/test_settings.py, tests/agent_server/test_conversation_service.py): 362 passed. The change is well-scoped for this repository (SDK + agent-server own the runtime/REST surface) and the additive, opt-in design keeps single-runtime deployments unchanged.
One material finding below, plus a design note for the human maintainer.
1. A collection POST /api/conversations for an existing Docker conversation can be routed to the local runtime
RuntimeDispatchMiddleware decides the POST /api/conversations target purely from the requested/default runtime; it never checks whether the conversation_id in the body already belongs to a Docker conversation. That endpoint is also the idempotent start/resume path (ConversationService.start_conversation reuses an existing id, and the Docker start route has explicit existing retry semantics), so a re-POST that omits conversation_runtime is misrouted whenever the server default is local. The per-id branch already does the right thing via registry.is_docker(...); the collection branch does not (inline comment).
2. Client container.memory/cpus replace the operator's Config caps
Noted as a known limitation in the PR body, and flagged only so the human maintainer can decide: on a shared/selectable server, an operator sets conversation_container_memory/cpus as policy, but any client can send larger values in StartConversationRequest.container and have them applied verbatim, so the operator's limit is not an upper bound. If selectable mode is expected to be multi-tenant, consider capping client options at the configured limits (or documenting that the operator must not rely on them as a ceiling).
CI
Head checks: 28 success, 1 skipped, 1 failure (unresolved-review-threads), which is the automation reacting to the PR's own open review threads rather than a code failure; commit statuses are still pending.
Verdict: the routing gap can produce a wrong-runtime result on resume, so this needs a change or an explicit maintainer decision before merge.
🔄 CHANGES REQUESTED
A POST /api/conversations whose conversation_id already has a Docker provisioning manifest is routed to the Docker routes regardless of the requested or default runtime, so a re-POST that omits conversation_runtime no longer reattaches the conversation as a host-local one. An explicit conversation_runtime="local" for such an id is rejected with 422.
…n-runtime The child-conversation fork from main plans its worktree with default options, so it keeps branching from the default branch.
juanmichelini
left a comment
There was a problem hiding this comment.
LGTM, please add to the docs that client-supplied container.memory/cpus replace the operator's Config caps rather than being capped by them.
ConversationContainerOptions.memory and cpus replace the operator's configured limits instead of being capped by them; the docstring and the field descriptions (and so the OpenAPI schema) now say so.
…tion schema openhands-sdk 1.57.0 (OpenHands/software-agent-sdk#5977) adds a runtime section to ConversationSettings. The settings UI only renders the sections each page names, so this is a test-only update.
HUMAN:
I want each conversation to be able to pick its runtime (local, worktree or Docker) instead of Docker being a server-wide flag, and to set a default runtime with its options from Canvas's Settings → Runtime. This PR is the agent-server side; the Canvas side is OpenHands/OpenHands#18436. Selecting a runtime per conversation is opt-in behind OH_CONVERSATION_RUNTIME_SELECTABLE, and without it nothing changes.
I'd like a careful review of the dispatch middleware in docker_runtime/hybrid.py and of the known limitation that a client's container limits replace the operator's instead of being capped by them.
AGENT:
Why
Today Docker is a server-wide startup flag (
OH_CONVERSATION_RUNTIME=docker): every conversation runs in a container or none does, and container limits exist only as env vars. Canvas wants users to pick where each conversation runs (OpenHands/OpenHands#18142) and to set a default runtime with its options in Settings → Runtime (OpenHands/OpenHands#18438). This PR is everything the agent-server needs for both, so one release unblocks Canvas.Summary
1. Choose the runtime per conversation (
OH_CONVERSATION_RUNTIME_SELECTABLE=1, opt-in)StartConversationRequest.conversation_runtime: "local" | "docker" | None; unset uses the server'sconversation_runtime.HybridConversationRegistrymounts the Docker routes again under/_docker, andRuntimeDispatchMiddlewaresends each request to the runtime that owns its conversation (a Docker conversation is one with a provisioning manifest; a create request goes where itsconversation_runtimesays).server_info.available_conversation_runtimestells clients what to offer.2. Runtime settings (typed
ConversationSettings, schema-driven, nomisc_settings)A new
runtimeschema section.default_runtimepicks the runtime; every other field only applies to (and is only shown for) one runtime, viadepends_on: "default_runtime=<value>".SettingsFieldMetadata.depends_onnow acceptsfield=valuealongside a plain boolean field.default_runtimelocallocalsends nothing new,worktreesendsworktree: true,dockersendsconversation_runtime: "docker"worktree_basedefault_branchgit fetch origin), the checkout'sHEAD, orrefworktree_base_refrefworktree_base=refworktree_branch_prefixopenhands/<prefix><conversation id>container_memorydocker run --memory; validated like Docker's own syntaxcontainer_cpusdocker run --cpus; minimum 0.01container_browserOH_ENABLE_BROWSER=0in the containerThey are launcher defaults: Canvas reads them and builds the start request itself.
ConversationSettings.create_request()deliberately leaves them out, so its in-process callers (the OpenAI gateway) keep running locally. Two new request fields carry the options:worktree_options: ConversationWorktreeOptions(base,ref,branch_prefix). The agent's worktree guidance names the branch point it was given.StartConversationRequest.container: ConversationContainerOptions(memory,cpus,browser), not persisted inmeta.json. The Docker runtime saves it in the conversation's provisioning manifest (a retried start may still change it until a container accepts the conversation) and applies it to every container it builds for that conversation; unset options keep the server'sConfiglimits.REST API contract changes
Compared with base OpenAPI
66af27073fb2for public/api/**paths.How a request flows
Issue Number
Part of OpenHands/OpenHands#18142 and OpenHands/OpenHands#18438 (supersedes PR 5979, merged in here)
How to Test
uv run pytest -n auto tests/agent_server tests/sdk/test_settings.py tests/sdk/conversation→ 3675 passed, 13 failed. The 13 (canvas_extensionsbackend, tmux cleanup) fail identically onmainon this machine.docker_runtime/test_hybrid.py: routing per conversation.docker_runtime/test_registry.py: container options override or keep the host limits.docker_runtime/test_routes.py: a start that was saved but failed in the container can be retried with new options; invalid limits → 422.test_conversation_service.py: worktree branch point (default branch,HEAD, ref), branch prefix, and the agent's guidance.tests/sdk/test_settings.py: runtime section schema,depends_on, validation, and thatcreate_request()leaves the runtime settings out..github/scripts/check_persisted_settings_compat.py: fixtures and the PyPI 1.54.0 baseline load; every new field has a default, so no schema-version bump.OH_CONVERSATION_RUNTIME_SELECTABLE=1: local and Docker conversations from the same server, and the Settings → Runtime page switching between runtimes. The worktreerefand branch-prefix options are covered by the tests above but have not been run by hand.Type
Notes
OH_CONVERSATION_RUNTIME_SELECTABLEand without the new request fields, local-only and Docker-only servers behave as before; older clients send nothing new.OH_CONVERSATION_IDLE_TTL_SECONDS), image, pids limit, start timeout, storage disk budget and retention.conversation_worktree_root, shared with forks, removed (branch kept) when the last conversation using them is deleted.config.conversation_runtime(tool catalog, profile validation, ACP model discovery) still describe the default runtime only; and a client'scontainer.memory/cpusreplace the operator'sConfiglimits rather than being capped by them.@openhands/typescript-clientfrom the release tag, raise Canvas'scompatibility.minimumAgentServer, and take the docs PR the SDK AGENTS.md asks for (docs(agent-canvas): per-conversation runtime and client container limits docs#983, draft) out of draft.🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:fcfca3c-pythonRun
All tags pushed for this build
About Multi-Architecture Support
fcfca3c-python) is a multi-arch manifest supporting both amd64 and arm64fcfca3c-python-amd64) are also available if needed