Skip to content

fix(agent-server): use the session key as VSCode token on /api/init - #5282

Merged
jlav merged 3 commits into
mainfrom
jl/vscode-token-after-init
Sep 30, 2026
Merged

jlav merged 3 commits into
mainfrom
jl/vscode-token-after-init

Conversation

@jlav

@jlav jlav commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

With OH_DEFERRED_INIT=1 set, the agent server starts VS Code with a random token. When not using OH_DEFERRED_INIT, the VS Code token is just the same as the SESSION_API_KEY.

I'm adjusting the agent server to use the session API key sent in the /init request 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/init never 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/url and cache the answer.

An orchestrator that boots pods with a bootstrap SESSION_API_KEY and sends a new key in /api/init hits the same 403. VSCode keeps the bootstrap key as its token.

Summary

  • /api/init switches 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.
  • The restart needs VSCodeService.stop() to actually stop VSCode. The openvscode-server launcher runs node as a child, and stop() only signaled the launcher, so node kept running on the port. VSCode now starts in its own process group, and stop() signals the whole group and waits for the port.

Issue Number

Fixes #5400

How to Test

Unit tests:

uv run pytest tests/agent_server/test_vscode_service.py tests/agent_server/test_init_router.py

Start the -python image CI publishes for this PR in deferred-init mode:

docker run --rm -p 8000:8000 -p 8001:8001 \
  -e OH_DEFERRED_INIT=1 -e OH_SECRET_KEY=init-key \
  ghcr.io/openhands/agent-server:<tag>-python --host 0.0.0.0 --port 8000

Then, from another shell:

curl -X POST localhost:8000/api/init \
  -H 'X-Init-API-Key: init-key' -H 'Content-Type: application/json' \
  -d '{"session_api_keys": ["user-session-key"], "conversations_path": "/tmp/conversations", "bash_events_dir": "/tmp/bash_events"}'

curl localhost:8000/api/vscode/url -H 'X-Session-API-Key: user-session-key'
curl -s -o /dev/null -w '%{http_code}\n' 'localhost:8001/?tkn=user-session-key'

The URL's tkn should be user-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

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

Notes

Clients that read /api/vscode/url see no difference. It answers 503 until /api/init finishes, 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

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime 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:6e715e6-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:6e715e6-golang-amd64
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-golang-amd64
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-golang-amd64
ghcr.io/openhands/agent-server:6e715e6-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:6e715e6-golang-arm64
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-golang-arm64
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-golang-arm64
ghcr.io/openhands/agent-server:6e715e6-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:6e715e6-java-amd64
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-java-amd64
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-java-amd64
ghcr.io/openhands/agent-server:6e715e6-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:6e715e6-java-arm64
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-java-arm64
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-java-arm64
ghcr.io/openhands/agent-server:6e715e6-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:6e715e6-python-amd64
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-python-amd64
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-python-amd64
ghcr.io/openhands/agent-server:6e715e6-python-node-runtime-amd64
ghcr.io/openhands/agent-server:6e715e6-python-arm64
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-python-arm64
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-python-arm64
ghcr.io/openhands/agent-server:6e715e6-python-node-runtime-arm64
ghcr.io/openhands/agent-server:6e715e6-python-minimal-amd64
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-python-minimal-amd64
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-python-minimal-amd64
ghcr.io/openhands/agent-server:6e715e6-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:6e715e6-python-minimal-arm64
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-python-minimal-arm64
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-python-minimal-arm64
ghcr.io/openhands/agent-server:6e715e6-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:6e715e6-python-slim-amd64
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-python-slim-amd64
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-python-slim-amd64
ghcr.io/openhands/agent-server:6e715e6-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:6e715e6-python-slim-arm64
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-python-slim-arm64
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-python-slim-arm64
ghcr.io/openhands/agent-server:6e715e6-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:6e715e6-golang
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-golang
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-golang
ghcr.io/openhands/agent-server:6e715e6-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:6e715e6-java
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-java
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-java
ghcr.io/openhands/agent-server:6e715e6-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:6e715e6-python-minimal
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-python-minimal
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-python-minimal
ghcr.io/openhands/agent-server:6e715e6-python-node-runtime-minimal
ghcr.io/openhands/agent-server:6e715e6-python-slim
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-python-slim
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-python-slim
ghcr.io/openhands/agent-server:6e715e6-python-node-runtime-slim
ghcr.io/openhands/agent-server:6e715e6-python
ghcr.io/openhands/agent-server:6e715e62385b8eaefa5ec23edd9f3e1398f7d472-python
ghcr.io/openhands/agent-server:jl-vscode-token-after-init-python
ghcr.io/openhands/agent-server:6e715e6-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 6e715e6-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., 6e715e6-python-amd64) are also available if needed

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   init_router.py151597%180, 182, 186, 188, 190
   vscode_service.py1241191%71–73, 86–87, 180, 189, 232, 261–262, 289
TOTAL462511882759% 

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

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

Comment thread tests/agent_server/test_init_router.py Resolved
@jlav
jlav force-pushed the jl/vscode-token-after-init branch from 99f96d8 to 89ce3c4 Compare September 30, 2026 13:44
@jlav
jlav merged commit d0f9500 into main Sep 30, 2026
59 of 62 checks passed
@jlav
jlav deleted the jl/vscode-token-after-init branch September 30, 2026 15:16
@github-actions github-actions Bot mentioned this pull request Sep 30, 2026
1 of 7 tasks
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.

[Bug]: VSCode rejects the session key after /api/init on a deferred-init agent-server

3 participants