Repository navigation
fix(agent-server): honor create_app config for the conversation service - #5686
Conversation
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
… config The non-deferred lifespan built its ConversationService with get_default_conversation_service(), which loads config from OH_* env and ignores the Config passed to create_app(). Tests that pass an isolated Config therefore persisted conversations to OH_CONVERSATIONS_PATH and posted webhooks to OH_WEBHOOKS_*; inside an OpenHands sandbox those point at the live deployment. Build the service from api.state.config, as the deferred /api/init path already does, and strip live-deployment env vars in pytest_configure. Co-authored-by: openhands <openhands@all-hands.dev>
a1db7da to
c8d59c8
Compare
|
Just discovered that #5592 has overlap. Will see if we can merge that instead and narrow the scope of this PR.... |
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Scope
The change belongs in this repository. It touches the Agent Server's own startup path (openhands-agent-server/openhands/agent_server/api.py) plus its tests, which this repo owns. No product/architecture decision is required.
What the change does
The non-deferred branch of api_lifespan now builds the conversation service with ConversationService.get_instance(config) from api.state.config instead of get_default_conversation_service(), which re-derives its config from the OH_* environment. POST /api/init already used ConversationService.get_instance(new_config) for deferred pods, so the legacy path now matches the deferred one. Production is unaffected: create_app() with no argument passes get_default_config(), the same config the old call resolved. The change is confined to startup wiring; no public symbol, REST/WebSocket contract, or persisted format changes.
Verification on this head (c8d59c88)
I reproduced the reported failure and its fix through the real lifespan, not constructed unit state:
- Base (
fd268089) fails. WithOH_CONVERSATIONS_PATHandOH_WEBHOOKS_0_BASE_URLset andcreate_app(config)given an isolated config, the service built by the lifespan used the env conversations dir and the env webhook (HONORS_APP_CONFIG: False), running against the base sources via a pinnedPYTHONPATH. - Head passes. The identical scenario on this head yields the app's conversations dir and no webhooks (
HONORS_APP_CONFIG: True), and the PR'stest_lifespan_conversation_service_uses_app_config_not_envpasses. This matches the reported in-process symptom: conversations/webhooks/session key leaking to the ambientOH_*deployment. - Focused suites on head:
test_api.py,test_env_parser.py,test_init_router.py,test_local_storage.py,test_sockets_service_getters.py,test_preload_modules.py→ 225 passed.tests/crossalso passes. - Env guard:
pytest_configureremoves the guarded variables before collection, including for a barepytest(testpaths) run, and leaves unrelated variables intact; tests that need those values set them explicitly. - Current-head CI: all required checks pass (
build-binary-and-test,pre-commit,Check OpenAPI Schema,sdk-tests,tools-tests,agent-server-tests,cross-tests).
Non-blocking context
- The non-required Validate PR description check fails on this head because
## Issue NumberisNone; the repo gate asks for a linked issue. It is not a required status check, so it does not block merge, but linking one would satisfy the gate. - The author has noted that #5592 overlaps and may supersede part of this work; that is a maintainer sequencing decision, not a code defect.
- The PR intentionally leaves the bash-events, VSCode, and tool-preload services on the global config as a follow-up; those use local paths/ports rather than a remote deployment, so the stated scope of the reported bug is met.
No material correctness, security, or compatibility defect found in the changed lines.
✅ APPROVED
HUMAN:
Running the test suite from a cloud sandbox created 17 conversations on our staging deployment because the test isolation was incomplete: the server ignored the tests' config and used the sandbox's settings.
AGENT:
Why
Without deferred init, the agent-server's startup built its conversation service with
get_default_conversation_service(). That reloads config from theOH_*environment variables and ignores theConfigpassed tocreate_app(). Tests that pass an isolated config (a temp conversations directory, no webhooks) therefore saved conversations toOH_CONVERSATIONS_PATHand sent conversation webhooks toOH_WEBHOOKS_*, authenticated withSESSION_API_KEY. On CI and laptops those variables are unset, so nothing happens. Inside an OpenHands sandbox they are the sandbox's own settings, so runningtests/agent_serverthere registered the test conversations with the real app server, under the sandbox owner's account.Startup now builds the service from
api.state.config, which is whatPOST /api/initalready does for deferred-init servers. The production server is unaffected: it runscreate_app()with no argument, which usesget_default_config(), the same config the old call loaded. As a second guard,tests/conftest.pyremoves the variables that point an agent server at, or authenticate it to, a live deployment. It does this inpytest_configure, before collection, because some modules read the config when they're imported.Summary
create_app(), not the environment.pytest_configureremoves webhook, conversations and bash-events path, session-key, internal-server-URL and LLM key-refresh variables.tests/agent_server/test_api.py; the existing startup tests now patchConversationService.get_instance.Issue Number
Fixes #5590
How to Test
uv run pytest tests/agent_server/test_api.py -k lifespan_conversation_service_uses_app_configsetsOH_CONVERSATIONS_PATHandOH_WEBHOOKS_0_BASE_URL, starts an app with its own config, and checks that the running service uses the app's conversations directory and has no webhooks.-n 2and dummy values for every guarded variable, a probe test saw all of them removed in the worker, while an unrelatedOH_*variable was kept.Video/Screenshots
N/A
Design Doc
N/A
Type
Notes
Startup still gets the bash events, VSCode and tool-preload services from the global config. They only touch local paths and ports, not a remote server, so they're left for a follow-up.
This PR was drafted by an AI agent on behalf of the user.
@jpshackelford can click here to continue refining the PR
🐳 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:c8d59c8-pythonRun
All tags pushed for this build
About Multi-Architecture Support
c8d59c8-python) is a multi-arch manifest supporting both amd64 and arm64c8d59c8-python-amd64) are also available if needed