Repository navigation
fix(agent-server): update FastAPI root_path when /api/init delivers a web_url - #3855
Conversation
Dormant agent-server pods boot without a web_url, so the FastAPI app's root_path is set to '' at construction. InitService.initialize already swaps in a merged Config via app.state.config when POST /api/init delivers a per-user web_url, but never updates the app's root_path. FastAPI then writes that empty root_path into every request's ASGI scope, breaking OpenAPI/Swagger/ReDoc URLs on servers that are mounted behind a reverse proxy under a sub-path. Re-derive root_path from the merged config inside initialize, right before flipping state to 'ready', so the docs are correct the moment clients can hit endpoints. Co-authored-by: openhands <openhands@all-hands.dev>
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
Simplified root_path update logic to always set the new_root_path from the merged config.
all-hands-bot
left a comment
There was a problem hiding this comment.
✅ QA Report: PASS
Started the real deferred-init agent server before and after the PR, exercised /api/init with a mounted web_url, and confirmed the PR fixes OpenAPI/Swagger root-path behavior.
Does this PR achieve its stated goal?
Yes. The stated goal was to update FastAPI root_path when deferred init receives a per-user web_url, so OpenAPI and Swagger URLs reflect the reverse-proxy mount path immediately after /api/init. On base main, after a successful init the server still returned servers=None and Swagger pointed to /openapi.json; on PR commit 7d1584d, the same real HTTP flow returned servers=[{'url': '/agent-server-123/agent-server'}] and Swagger pointed to /agent-server-123/agent-server/openapi.json.
| Phase | Result |
|---|---|
| Environment Setup | ✅ Ran make build; dependencies installed and editable packages built successfully. |
| CI Status | 🟡 gh pr checks showed 34 successful, 1 skipped, 1 pending QA Changes, 0 failing. |
| Functional Verification | ✅ Real uv run python -m openhands.agent_server server + HTTP requests verified the before/after behavior. |
Functional Verification
The probe starts the actual agent-server with OH_DEFERRED_INIT=true, then uses real HTTP requests: GET /openapi.json, POST /api/init with web_url=https://example.com/agent-server-123/agent-server/, another GET /openapi.json, and GET /docs.
Test 1: Deferred init updates OpenAPI and Swagger root path
Step 1 — Reproduce baseline without the fix:
Ran git checkout --detach origin/main && /tmp/qa_deferred_init_root_path_probe.sh main-before-fix:
HEAD is now at 73d55539 Collapse integration report details in PR comments (#3854)
[main-before-fix] GET /openapi.json before status=200 servers=None
[main-before-fix] POST /api/init status=200 body={'state': 'ready', 'error': None}
[main-before-fix] GET /openapi.json after status=200 servers=None
[main-before-fix] GET /docs after status=200 swagger_openapi_url=/openapi.json
This confirms the bug: even after deferred init successfully receives the mounted web_url, OpenAPI still has no prefixed server entry and Swagger still targets the unmounted /openapi.json.
Step 2 — Apply the PR's changes:
Checked out the PR commit 7d1584df1ef97e8c8302919091e76e49789edae9.
Step 3 — Re-run with the fix in place:
Ran git checkout --detach 7d1584df1ef97e8c8302919091e76e49789edae9 && /tmp/qa_deferred_init_root_path_probe.sh pr-after-fix:
HEAD is now at 7d1584df Refactor root_path assignment in init_router.py
[pr-after-fix] GET /openapi.json before status=200 servers=None
[pr-after-fix] POST /api/init status=200 body={'state': 'ready', 'error': None}
[pr-after-fix] GET /openapi.json after status=200 servers=[{'url': '/agent-server-123/agent-server'}]
[pr-after-fix] GET /docs after status=200 swagger_openapi_url=/agent-server-123/agent-server/openapi.json
This confirms the fix works: on the first post-init OpenAPI and docs requests, the reverse-proxy mount path from web_url is present.
Issues Found
None.
This QA review was created by an AI agent (OpenHands) on behalf of the user.
|
✅ Review complete. This review was performed through OpenHands Cloud Automation. You can log in and view the conversation here. |
all-hands-bot
left a comment
There was a problem hiding this comment.
🟡 Acceptable — targeted, correct fix with real test coverage and good evidence.
Summary
This PR fixes a real bug: after a warm-pool server receives POST /api/init with a web_url, FastAPI's root_path remained "" (the dormant boot value), so OpenAPI/Swagger/ReDoc generated doc links without the reverse-proxy mount prefix. The 5-line fix re-derives root_path from the merged config immediately before flipping state to ready, and the new test exercises the full dormant→init transition through the real TestClient.
One minor design suggestion is left inline (moving _get_root_path to config.py to break the circular-import dependency rather than deferring it); it does not block merging.
[RISK ASSESSMENT]
- [Overall PR] ✅ Risk Assessment: 🟢 LOW
Touches only the deferred-init code path (deferred_init=Trueservers). Normal server startup is unaffected. The change is additive (one attribute assignment after existing state updates), and the new test proves the happy path.
VERDICT:
✅ Worth merging: bug is real, fix is minimal and correct, test is genuine.
KEY INSIGHT:
_get_root_path lives in api.py but only depends on Config; moving it to config.py would be the cleanest long-term design.
Improve this review? If any feedback above seems incorrect or irrelevant to this repository, you can teach the reviewer to do better:
- Add a
.agents/skills/custom-codereview-guide.mdfile to your branch (or edit it if one already exists) with the/codereviewtrigger and the context the reviewer is missing. See the customization docs for the required frontmatter format.- Re-request a review — the reviewer reads guidelines from the PR branch, so your changes take effect immediately.
- When your PR is merged, the guideline file goes through normal code review by repository maintainers.
Resolve with AI? Install the iterate skill in your agent and run
/iterateto automatically drive this PR through CI, review, and QA until it's merge-ready.Was this review helpful? React with 👍 or 👎 to give feedback.
This review was generated by an AI agent (OpenHands) on behalf of the user through OpenHands Automation. View conversation
| self._app.state.config = new_config | ||
| self._app.state.conversation_service = service | ||
| # Re-derive root_path from the merged config so Doc URLS are valid | ||
| from openhands.agent_server.api import _get_root_path |
There was a problem hiding this comment.
🟡 Suggestion — move _get_root_path to config.py to eliminate the deferred import
_get_root_path takes only a Config and calls urlparse — it has no real dependency on the rest of api.py. It lives there today because that's where _create_fastapi_instance uses it, but the coupling is accidental.
Moving it to config.py (or a small utils.py) would:
- Let this import be a normal top-level import instead of a deferred one.
- Remove the risk of a real circular dependency if
api.pyis ever refactored. - Make the function discoverable to other modules that derive
root_pathfromConfig.
Not a blocker for this targeted bug fix, but worth a follow-up.
all-hands-bot
left a comment
There was a problem hiding this comment.
✅ QA Report: PASS
Started real agent-server processes on base and PR in deferred-init mode, called /api/init with a path-based web_url, and verified OpenAPI/Swagger docs now use the expected mount prefix.
Does this PR achieve its stated goal?
Yes. On origin/main, after the same /api/init request, GET /openapi.json still returned servers_after= None and /docs configured url: '/openapi.json', confirming the path-based docs bug. On PR commit b1e462e6d54dfaa4c9d08e1056bc326115ce32c1, the same flow returned servers_after= [{'url': '/agent-server-123/agent-server'}] and Swagger configured '/agent-server-123/agent-server/openapi.json', so the deferred-init web URL is reflected immediately in docs URLs.
| Phase | Result |
|---|---|
| Environment Setup | ✅ make build completed; real server started with OH_DEFERRED_INIT=true uv run python -m openhands.agent_server |
| CI Status | ⏳ 34 successful, 1 skipped, 1 pending (QA Changes by OpenHands/qa-changes), 0 failing/cancelled at refresh |
| Functional Verification | ✅ Reproduced the base bug and verified the PR fix via real HTTP requests to /openapi.json, /api/init, and /docs |
Functional Verification
Test 1: Deferred init updates OpenAPI and Swagger docs prefix
Step 1 — Reproduce / establish baseline without the fix:
Ran:
git checkout --detach origin/main
uv sync --dev --reinstall-package openhands-agent-server
OH_DEFERRED_INIT=true uv run python -m openhands.agent_server > /tmp/oh_base.log 2>&1 &
curl http://127.0.0.1:8000/openapi.json
curl -H 'Content-Type: application/json' \
-d '{"web_url":"https://example.com/agent-server-123/agent-server/","conversations_path":"/tmp/oh-qa-base/convs","bash_events_dir":"/tmp/oh-qa-base/bash"}' \
http://127.0.0.1:8000/api/init
curl http://127.0.0.1:8000/openapi.json
curl http://127.0.0.1:8000/docsObserved:
GET /openapi.json before init
HTTP_STATUS=200
servers_before= None
POST /api/init
HTTP_STATUS=200
{"state":"ready","error":null}
GET /openapi.json after init
HTTP_STATUS=200
servers_after= None
GET /docs after base init
HTTP_STATUS=200
swagger_openapi_url= '/openapi.json'
contains_prefix= False
This confirms the bug exists on base: the server accepts /api/init, but the OpenAPI schema and Swagger UI still do not include the /agent-server-123/agent-server mount prefix.
Step 2 — Apply the PR's changes:
Ran:
git checkout fix/deferred-init-root-path
git reset --hard b1e462e6d54dfaa4c9d08e1056bc326115ce32c1
uv sync --dev --reinstall-package openhands-agent-serverStep 3 — Re-run with the fix in place:
Ran the same server startup and HTTP flow against the PR build. Observed:
GET /openapi.json before init
HTTP_STATUS=200
servers_before= None
POST /api/init
HTTP_STATUS=200
{"state":"ready","error":null}
GET /openapi.json after init
HTTP_STATUS=200
servers_after= [{'url': '/agent-server-123/agent-server'}]
GET /docs after PR init
HTTP_STATUS=200
swagger_openapi_url= '/agent-server-123/agent-server/openapi.json'
contains_prefix= True
This confirms the fix works through the real server/API path: immediately after deferred init, OpenAPI advertises the derived root path and Swagger UI points to the prefixed OpenAPI URL expected behind path-based routing.
Issues Found
None.
This QA review was created by an AI agent (OpenHands) on behalf of the user.
… web_url (OpenHands#3855) Co-authored-by: openhands <openhands@all-hands.dev>
HUMAN:
Fixing openapi docs when using path based routing
AGENT:
Why
When
Config.deferred_init=True, the agent-server boots in dormant mode andPOST /api/initlater delivers a per-userweb_url(used to derive the FastAPIroot_pathfor OpenAPI / Swagger / ReDoc URLs on servers mounted behind areverse proxy under a sub-path, e.g.
/agent-server-123/agent-server/).InitService.initializealready swaps the merged config ontoapp.state.config,but never updated
app.root_path. Because FastAPI writesscope["root_path"] = self.root_pathon every request (FastAPI.__call__), the OpenAPI doc handlersserved
serversentries derived from the dormant""root_path — regardless ofthe per-user mount path. Docs and Swagger UI links were broken on warm-pool
servers that actually initialize.
Summary
root_pathfrom the merged config insideInitService.initialize, right before flipping state toready, so OpenAPI / Swagger / ReDoc URLs and the ASGI scoperoot_pathreflect the per-user mount path on the very first request after/api/init.test_root_path_updates_from_init_web_urlcovering the dormant→init transition through the real FastAPI app + TestClient.Issue Number
How to Test
Unit tests:
End-to-end against a real
uvicornserver (OH_DEFERRED_INIT=true, thenPOST /api/initwithweb_url="https://example.com/agent-server-123/agent-server/", thenGET /openapi.json):Pre-fix the post-init line read
openapi.servers=None(or[]); the per-user prefix only appeared in the schema after the FastAPI app was rebuilt.Video/Screenshots
In the screen shot below, see that an agent server started in deferred init mode shows the openapi docs when initialized with path based routing

Type
Notes
_get_root_pathimport is local toinitializeto avoid anapi.py↔init_router.pycircular import (api.pyalready importsInitService/init_routerat module load).getattr(self._app, "root_path", None)guard keeps existing unit tests that substitute aSimpleNamespacefor the FastAPI app working without modification.Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang: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:b1e462e-pythonRun
All tags pushed for this build
About Multi-Architecture Support
b1e462e-python) is a multi-arch manifest supporting both amd64 and arm64b1e462e-python-amd64) are also available if needed