Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions openhands-agent-server/openhands/agent_server/init_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ async def initialize(self, req: InitRequest) -> InitStatus:
self._entered_service = service
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.


new_root_path = _get_root_path(new_config)
self._app.root_path = new_root_path
mark_initialization_complete()
self._state = "ready"
logger.info("deferred_init: server transitioned to ready")
Expand Down
35 changes: 35 additions & 0 deletions tests/agent_server/test_init_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,41 @@ def test_dormant_503s_api_routes_until_init(self, tmp_path):
finally:
_reset_conversation_singleton()

def test_root_path_updates_from_init_web_url(self, tmp_path):
"""When /api/init delivers a ``web_url``, the FastAPI ``root_path``
must be re-derived from it so OpenAPI/Swagger/ReDoc URLs reflect the
external mount path (e.g. behind a reverse proxy)."""
_reset_conversation_singleton()
cfg = Config(
deferred_init=True,
conversations_path=tmp_path / "convs",
bash_events_dir=tmp_path / "bash",
)
app = create_app(cfg)
with TestClient(app) as client:
try:
# Dormant server has no web_url → empty root_path.
assert app.root_path == ""

resp = client.post(
"/api/init",
json={
"web_url": "https://example.com/agent-server-123/agent-server/",
"conversations_path": str(tmp_path / "u" / "convs"),
"bash_events_dir": str(tmp_path / "u" / "bash"),
},
)
assert resp.status_code == 200

# The FastAPI app's root_path must now match the prefix from
# the per-user web_url so OpenAPI doc URLs and Swagger asset
# links are correct under the reverse-proxy mount path.
assert app.root_path == "/agent-server-123/agent-server", (
f"root_path was not updated after /api/init: {app.root_path!r}"
)
finally:
_reset_conversation_singleton()

def test_init_api_key_required_when_configured(self, tmp_path):
_reset_conversation_singleton()
cfg = Config(
Expand Down
Loading