Repository navigation
fix(agent-server): use the session key as VSCode token on /api/init - #5282
Conversation
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Scope: This change belongs in this repository — openhands-agent-server owns VSCodeService, the deferred-init InitService, and the /api/* contract, so no repository move or maintainer scope decision is needed.
Assessment: The design is sound and consistent with the existing non-deferred behavior (get_vscode_service() already seeds the token from session_api_keys[0]), and the process-group change correctly addresses the launcher/node split. Two concrete issues, plus a failing head check, block a clean approval.
1. The new test is environment-sensitive and fails whenever a session key is ambient (inline). tests/agent_server/test_init_router.py::TestEndToEndOverLifespan::test_vscode_url_stays_empty_without_vscode fails in this workspace. Reproduced on the exact head:
$ OH_SESSION_API_KEYS_0=leakvalue uv run pytest \
"tests/agent_server/test_init_router.py::TestEndToEndOverLifespan::test_vscode_url_stays_empty_without_vscode" -q
E AssertionError: assert 'http://localhost:8001/?tkn=leakvalue&folder=workspace' is None
1 failed
It passes with SESSION_API_KEY/OH_SESSION_API_KEYS_0 unset (1 passed). This is not cosmetic: an agent-server dev/test environment commonly runs with a session key set, which is exactly the configuration that trips it. Root cause and fix are detailed on the inline comment.
2. Live evidence for the claimed 403→302 fix is not in the PR. The repository's review guide requires authentic before/after runtime evidence for bug fixes whose failure only occurs through a real lifecycle/integration path — this is one (VSCode process restart under deferred init, container behavior). The PR body documents the docker/curl procedure and expected 302 vs 403, but includes no captured output, and the new restart test mocks create_subprocess_shell, so it never exercises a real node process or a real port release. Please attach the observed before/after results (the curl status codes and the /api/vscode/url payload on both the base and this head), or confirm a maintainer will run it.
3. The head has a failing required check. Validate PR description is failure on 99f96d83 — "Link an issue in the ## Issue Number section (e.g. Fixes #123). Newly opened issues must carry the ready-for-dev label." The PR declares Issue Number: None., so the description gate is red. Either link a ready-for-dev issue or get a maintainer to waive it before merge.
All other checks on this head succeed (including agent-server-tests, sdk-tests, cross-tests, workspace-tests, smoke-test, agent-server-api, pre-commit, security, REST API (OpenAPI)).
🔄 CHANGES REQUESTED
99f96d8 to
89ce3c4
Compare
HUMAN:
With
OH_DEFERRED_INIT=1set, the agent server starts VS Code with a random token. When not usingOH_DEFERRED_INIT, the VS Code token is just the same as theSESSION_API_KEY.I'm adjusting the agent server to use the session API key sent in the
/initrequest as the VS Code token. It's more consistent to use the session API key as the VS Code token, regardless of whether or not it was set through deferred init or not. It also saves a round trip in the enterprise app to retrieve the VS Code token, since today the enterprise app just assumes it to be the same as the session API key.AGENT:
Why
Warm-pool orchestrators hold each sandbox's session key. They build the VSCode URL from it, the same way they do for a server that boots with the key. VSCode uses the first session key as its token.
After deferred init, that URL doesn't work. A dormant server starts VSCode before it has a session key, so VSCode gets a random token.
/api/initnever changes it, so VSCode rejects the session key with a 403. The only way to get a working URL is to ask each sandbox for/api/vscode/urland cache the answer.An orchestrator that boots pods with a bootstrap
SESSION_API_KEYand sends a new key in/api/inithits the same 403. VSCode keeps the bootstrap key as its token.Summary
/api/initswitches VSCode's token to the first session key it delivers, restarting VSCode. After init, VSCode matches a server that booted with that key. A server whose VSCode isn't running keeps its token, so the minimal images, which don't ship VSCode, still report no URL.VSCodeService.stop()to actually stop VSCode. The openvscode-server launcher runs node as a child, andstop()only signaled the launcher, so node kept running on the port. VSCode now starts in its own process group, andstop()signals the whole group and waits for the port.Issue Number
Fixes #5400
How to Test
Unit tests:
Start the
-pythonimage CI publishes for this PR in deferred-init mode:Then, from another shell:
The URL's
tknshould beuser-session-key, and VSCode should answer 302. On main, the URL has a random token and VSCode answers 403.Video/Screenshots
N/A
Design Doc
N/A
Type
Notes
Clients that read
/api/vscode/urlsee no difference. It answers 503 until/api/initfinishes, so none of them can be holding the old token.🐳 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:6e715e6-pythonRun
All tags pushed for this build
About Multi-Architecture Support
6e715e6-python) is a multi-arch manifest supporting both amd64 and arm646e715e6-python-amd64) are also available if needed