Skip to content

fix(agent-server): update FastAPI root_path when /api/init delivers a web_url - #3855

Merged
tofarr merged 4 commits into
mainfrom
fix/deferred-init-root-path
Jun 23, 2026
Merged

tofarr merged 4 commits into
mainfrom
fix/deferred-init-root-path

Conversation

@tofarr

@tofarr tofarr commented Jun 23, 2026 •

Copy link
Copy Markdown
Collaborator

HUMAN:

Fixing openapi docs when using path based routing


AGENT:

Why

When Config.deferred_init=True, the agent-server boots in dormant mode and
POST /api/init later delivers a per-user web_url (used to derive the FastAPI
root_path for OpenAPI / Swagger / ReDoc URLs on servers mounted behind a
reverse proxy under a sub-path, e.g. /agent-server-123/agent-server/).

InitService.initialize already swaps the merged config onto app.state.config,
but never updated app.root_path. Because FastAPI writes scope["root_path"] = self.root_path on every request (FastAPI.__call__), the OpenAPI doc handlers
served servers entries derived from the dormant "" root_path — regardless of
the per-user mount path. Docs and Swagger UI links were broken on warm-pool
servers that actually initialize.

Summary

  • Re-derive FastAPI root_path from the merged config inside InitService.initialize, right before flipping state to ready, so OpenAPI / Swagger / ReDoc URLs and the ASGI scope root_path reflect the per-user mount path on the very first request after /api/init.
  • Add test_root_path_updates_from_init_web_url covering the dormant→init transition through the real FastAPI app + TestClient.

Issue Number

How to Test

Unit tests:

uv run pytest tests/agent_server/test_init_router.py tests/agent_server/test_api.py -v

End-to-end against a real uvicorn server (OH_DEFERRED_INIT=true, then POST /api/init with web_url="https://example.com/agent-server-123/agent-server/", then GET /openapi.json):

Before /api/init:
  GET /openapi.json status=200
  openapi.servers=None

POST /api/init status=200 body={'state': 'ready', 'error': None}

After /api/init:
  GET /openapi.json status=200
  openapi.servers=[{'url': '/agent-server-123/agent-server'}]

PASS: root_path was updated from /api/init web_url

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
image

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • The _get_root_path import is local to initialize to avoid an api.py ↔ init_router.py circular import (api.py already imports InitService / init_router at module load).
  • The getattr(self._app, "root_path", None) guard keeps existing unit tests that substitute a SimpleNamespace for the FastAPI app working without modification.
  • No public API changes; no migration needed.

Agent Server images for this PR

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22-slim Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:b1e462e-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-b1e462e-python \
  ghcr.io/openhands/agent-server:b1e462e-python

All tags pushed for this build

ghcr.io/openhands/agent-server:b1e462e-golang-amd64
ghcr.io/openhands/agent-server:b1e462e6d54dfaa4c9d08e1056bc326115ce32c1-golang-amd64
ghcr.io/openhands/agent-server:fix-deferred-init-root-path-golang-amd64
ghcr.io/openhands/agent-server:b1e462e-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:b1e462e-golang-arm64
ghcr.io/openhands/agent-server:b1e462e6d54dfaa4c9d08e1056bc326115ce32c1-golang-arm64
ghcr.io/openhands/agent-server:fix-deferred-init-root-path-golang-arm64
ghcr.io/openhands/agent-server:b1e462e-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:b1e462e-java-amd64
ghcr.io/openhands/agent-server:b1e462e6d54dfaa4c9d08e1056bc326115ce32c1-java-amd64
ghcr.io/openhands/agent-server:fix-deferred-init-root-path-java-amd64
ghcr.io/openhands/agent-server:b1e462e-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:b1e462e-java-arm64
ghcr.io/openhands/agent-server:b1e462e6d54dfaa4c9d08e1056bc326115ce32c1-java-arm64
ghcr.io/openhands/agent-server:fix-deferred-init-root-path-java-arm64
ghcr.io/openhands/agent-server:b1e462e-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:b1e462e-python-amd64
ghcr.io/openhands/agent-server:b1e462e6d54dfaa4c9d08e1056bc326115ce32c1-python-amd64
ghcr.io/openhands/agent-server:fix-deferred-init-root-path-python-amd64
ghcr.io/openhands/agent-server:b1e462e-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:b1e462e-python-arm64
ghcr.io/openhands/agent-server:b1e462e6d54dfaa4c9d08e1056bc326115ce32c1-python-arm64
ghcr.io/openhands/agent-server:fix-deferred-init-root-path-python-arm64
ghcr.io/openhands/agent-server:b1e462e-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:b1e462e-golang
ghcr.io/openhands/agent-server:b1e462e6d54dfaa4c9d08e1056bc326115ce32c1-golang
ghcr.io/openhands/agent-server:fix-deferred-init-root-path-golang
ghcr.io/openhands/agent-server:b1e462e-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:b1e462e-java
ghcr.io/openhands/agent-server:b1e462e6d54dfaa4c9d08e1056bc326115ce32c1-java
ghcr.io/openhands/agent-server:fix-deferred-init-root-path-java
ghcr.io/openhands/agent-server:b1e462e-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:b1e462e-python
ghcr.io/openhands/agent-server:b1e462e6d54dfaa4c9d08e1056bc326115ce32c1-python
ghcr.io/openhands/agent-server:fix-deferred-init-root-path-python
ghcr.io/openhands/agent-server:b1e462e-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

  • Each variant tag (e.g., b1e462e-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., b1e462e-python-amd64) are also available if needed

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>
@github-actions

github-actions Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   init_router.py116397%153, 157, 159
TOTAL331111439156% 

Simplified root_path update logic to always set the new_root_path from the merged config.
@tofarr
tofarr marked this pull request as ready for review June 23, 2026 15:55
@tofarr
tofarr requested a review from all-hands-bot June 23, 2026 15:55

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

@tofarr
tofarr requested a review from all-hands-bot June 23, 2026 16:18

all-hands-bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Review complete.

This review was performed through OpenHands Cloud Automation. You can log in and view the conversation here.

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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=True servers). 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:

  1. Add a .agents/skills/custom-codereview-guide.md file to your branch (or edit it if one already exists) with the /codereview trigger and the context the reviewer is missing. See the customization docs for the required frontmatter format.
  2. Re-request a review — the reviewer reads guidelines from the PR branch, so your changes take effect immediately.
  3. 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 /iterate to 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.py is ever refactored.
  • Make the function discoverable to other modules that derive root_path from Config.

Not a blocker for this targeted bug fix, but worth a follow-up.

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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/docs

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= 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-server

Step 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.

@tofarr
tofarr merged commit cd74324 into main Jun 23, 2026
41 checks passed
@tofarr
tofarr deleted the fix/deferred-init-root-path branch June 23, 2026 16:40
pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 2026
… web_url (OpenHands#3855)

Co-authored-by: openhands <openhands@all-hands.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants