Skip to content

perf(presets): avoid redundant SDK imports during setup - #754

Merged
neubig merged 4 commits into
mainfrom
perf/preset-metadata-check
Nov 14, 2026
Merged

neubig merged 4 commits into
mainfrom
perf/preset-metadata-check

Conversation

@neubig

@neubig neubig commented Nov 13, 2026

Copy link
Copy Markdown
Member

Why

Fixes #668. Supersedes #719 by @claire-fang (fork PR whose CI could not run without approval and which conflicted with main); her two commits are kept unchanged on this branch.

Every preset run's setup.sh ended with python -c 'import openhands.sdk, openhands.tools, openhands.workspace' as an install check. That full cold import is the slowest part of setup, and the entrypoint (main.py) imports the same packages right after, so every run paid the import twice.

Summary

  • presets/prompt/setup.sh, presets/plugin/setup.sh, presets/plugin_spec/setup.sh (from perf(presets): avoid redundant SDK imports during setup #719): verify openhands-sdk, openhands-tools, openhands-workspace with importlib.metadata.version(). Missing distributions still print [setup] ERROR: OpenHands SDK is not importable from .venv and exit non-zero.
  • presets/setup.py (new here): the Windows counterpart of setup.sh, added on main after perf(presets): avoid redundant SDK imports during setup #719 was opened, had the same import check. It now uses the same metadata check, as AGENTS.md requires the two to stay in step.
  • Merged main and resolved the tests/test_preset_router.py conflict (kept main's skipif(os.name == "nt") for bash tests).
  • Tests: perf(presets): avoid redundant SDK imports during setup #719's 12 runtime cases (each setup.sh against a real venv whose openhands.* modules raise on import, with and without each distribution's metadata), 4 new runtime cases for setup.py, and test_setup_py_mirrors_setup_sh now asserts both use the metadata check and neither contains import openhands.

A broken install that has metadata but cannot import now fails in the entrypoint's own import, once, instead of in setup.

How to Test

uv run pytest tests/test_preset_router.py tests/test_plugin_specs.py

358 passed locally. Pre-commit (ruff, pycodestyle, pyright) passes.

Timing (.pr/issue-668/bench.sh): the prompt preset setup.sh from each revision in a fresh run directory, real PyPI install of SDK 1.62.0 with a warm uv cache, then the entrypoint's SDK import, 3 alternating runs each on Linux:

setup.sh setup + entrypoint import
main 14.8 / 16.1 / 15.2 s 22.5 / 26.1 / 22.3 s
this PR 1.8 / 0.9 / 1.0 s 17.6 / 15.2 / 13.2 s

Before / after

Before (main 949c001) After (this PR)
before after

Notes

Out of scope, per the issue triage: prebuilt/hardlinked venvs, UV_COMPILE_BYTECODE, a new progress phase, and making the SDK's litellm import lazy (SDK repo). .pr/ holds review evidence only and is removed on release.

This comment/PR was created by an AI agent (OpenHands) on behalf of Graham Neubig.

claire-fang and others added 4 commits November 8, 2026 15:43
Verify the three installed SDK distributions through package metadata.
Leave module initialization to the preset entrypoint and preserve the
existing nonzero setup failure when a required distribution is absent.

Cover all three setup scripts with isolated runtime regression tests.

Fixes #668
setup.py, setup.sh's Windows counterpart added on main after this branch
started, still imported the SDK as its install check. Use the same
importlib.metadata check, test it, and keep test_setup_py_mirrors_setup_sh in
step. Add timing evidence for #668 under .pr/.

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

Copy link
Copy Markdown
Contributor

📁 PR Artifacts Notice

This PR adds or changes temporary PR-specific documents under .pr/. They stay on this branch for review. After this PR merges, the next release PR removes them from main.

@github-actions github-actions Bot added the type: perf A performance improvement label Nov 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Warning

Your comment is too long (maximum is 65536 characters), so the coverage report was not added. See the job log for how to reduce it.

@neubig
neubig requested a review from all-hands-bot November 13, 2026 23:55
@neubig

neubig commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

@all-hands-bot Please review the latest head 22671ea. All CI checks are green and the branch merges cleanly with main. Before/after evidence is in the PR description.

This comment/PR was created by an AI agent (OpenHands) on behalf of Graham Neubig.

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

Copy link
Copy Markdown
Contributor

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

Correction: this supersedes an empty APPROVED review (id 5902653534) posted a minute earlier whose body failed to attach. The verdict below is the complete one.

Scope

In scope: AGENTS.md assigns openhands/automation/presets/*/setup.sh to this repository, and issue #668's triage names it as the owning repo with the metadata check as its endorsed "Fix 1". Linked issue #668 carries ready-for-dev and no release-blocker labels; the changed scripts ship in the automation image built by this PR's CI.

Review

The change replaces the full cold SDK import in all three setup.sh variants and the Windows presets/setup.py with importlib.metadata.version() checks, plus tests. Verification against the workspace:

  • Behavior parity. Missing distributions still print [setup] ERROR: OpenHands SDK is not importable from .venv and exit non-zero, so failure visibility at setup is unchanged for the common case. The detection is intentionally weaker (metadata-only, no import), and the trade-off is sound: main.py imports openhands.sdk, openhands.tools, and openhands.workspace immediately after setup, so a metadata-present-but-broken install now fails the run once at the entrypoint with a real traceback instead of twice. This matches AGENTS.md's split of duties (setup installs, entrypoint initializes).
  • setup.py/setup.sh parity. The Windows counterpart uses the identical metadata check, satisfying the AGENTS.md rule that the two stay in step; test_setup_py_mirrors_setup_sh now enforces both the metadata-check needles and the absence of import openhands in both scripts.
  • All three presets covered, including plugin_spec. presets/plugin_spec/setup.sh receives the same change, and _load_plugin_spec_preset_files() reads it (with PRESETS_DIR / "setup.py") into generated plugin-spec tarballs. with_current_preset_runner swaps the current runner into every preset tarball at dispatch, so existing automations pick this up without regeneration; the POSIX branch still drops setup.py as before.
  • Tests are real-path. _fake_run_venv builds an actual venv with real dist-info metadata whose openhands.* modules raise on import, and asserts setup never triggers an SDK import while still failing correctly when a distribution's metadata is absent (12 bash cases; 4 for setup.py). The setup.py test only stubs the external uv installer and the version API, not the metadata check itself. The 17 new/updated cases pass here, and the full focused suite (tests/test_preset_router.py + tests/test_plugin_specs.py) shows 279 passed with only the 27 fixture-collection errors that occur identically at base 949c001 in this Docker-less environment (verified by diffing error sets).
  • No unintended side effects. No changes to run-state, phases, callbacks, auth, or tarball contracts; the bench evidence in .pr/issue-668/ matches the PR body's numbers, and .pr/ use follows the repository's artifact policy with release-time cleanup.

No material findings. CI on 22671ead08f5dbccf689f49b402cd0bad3bdd439 is green.

✅ APPROVED

@neubig
neubig merged commit ace6563 into main Nov 14, 2026
10 checks passed
@openhands-release-bot openhands-release-bot Bot added the released: 1.33.0 Shipped in 1.33.0 label Nov 17, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.33.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released: 1.33.0 Shipped in 1.33.0 type: perf A performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: prompt-preset runs spend ~65s in setup on every run (full SDK import used as install check, paid twice)

4 participants