Skip to content

fix(sdk): add v1->v2 settings migration that canonicalizes agent_kind - #3069

Merged
xingyaoww merged 1 commit into
mainfrom
fix-agent-kind-llm-migration
May 5, 2026
Merged

xingyaoww merged 1 commit into
mainfrom
fix-agent-kind-llm-migration

Conversation

@neubig

@neubig neubig commented May 5, 2026 •

Copy link
Copy Markdown
Member

Why

Production incident: new cloud users could sign up but were 500'd on every action. Root cause as identified by the on-call team:

enterprise/server/routes/org_models.py — both OrgResponse.from_org and OrgDefaultsSettingsResponse.from_org validate persisted org.agent_settings using the SDK's deprecated AgentSettings class, which inherits from LLMAgentSettings and enforces agent_kind: Literal["llm"]. Stored data now carries the canonical agent_kind: "openhands", so Pydantic raises a literal_error and the route 500s.

Background: when the LLMAgentSettings → OpenHandsAgentSettings rename landed in v1.19.0, the canonical discriminator value flipped from "llm" to "openhands", but the persisted-settings schema_version was not bumped and no migration was registered. The cloud's from_org call sites already go through AgentSettings.from_persisted(...), which runs the migration framework — they just hit no migration that would normalize the discriminator, so the legacy/canonical mismatch leaks through to validation.

A point hot-fix already shipped on the cloud side (OpenHands/OpenHands#14263) that force-sets agent_kind = 'openhands' in OrgStore.get_agent_settings_from_org. That becomes redundant once this migration ships.

How migrations work in the SDK

The framework already exists in openhands-sdk/openhands/sdk/settings/model.py:

  • Both AgentSettings and ConversationSettings carry a schema_version: int field, with the current value pinned by module-level constants AGENT_SETTINGS_SCHEMA_VERSION / CONVERSATION_SETTINGS_SCHEMA_VERSION.
  • A persistence-only from_persisted(data) classmethod runs _apply_persisted_migrations(...), which reads schema_version (defaulting to 0), then walks a registered {old_version: migrator_callable} dict until it reaches the current version. Each migrator is (dict) -> dict and must advance schema_version strictly.
  • After migration, the discriminated-union validator (validate_agent_settings) routes the payload to the correct concrete class.

Until this PR there was exactly one migration: v0 -> v1 (added the agent_kind field). The v1.19.0 rename should have come with a v1 -> v2 migration, but didn't. This PR adds it.

Changes

  • Bumped AGENT_SETTINGS_SCHEMA_VERSION 1 → 2.
  • Registered _migrate_agent_settings_v1_to_v2 that rewrites legacy agent_kind: 'llm' → 'openhands'.
    • Safe because LLMAgentSettings is field-compatible with OpenHandsAgentSettings (it only narrows the discriminator literal).
    • Consistent with the existing deprecation: LLMAgentSettings is scheduled for removal in v1.22.0, so canonicalizing on read just hastens what was already going to happen.
    • After this migration, AgentSettings.from_persisted(...) consistently lands on the canonical OpenHandsAgentSettings/ACPAgentSettings variants regardless of how the data was originally persisted.
  • Updated the existing migration tests for the new current_version (the v0 → v1 → v2 chain bumps schema_version == 2 on the v0-entry-point test, and the v1-entry-point ACP test now also lands on schema_version == 2).
  • Updated the "rejects newer version" test from schema_version=2 to schema_version=3.
  • Added test_agent_settings_from_persisted_canonicalizes_legacy_llm_kind to exercise the new migration directly.

How to test

from openhands.sdk import AgentSettings, OpenHandsAgentSettings

legacy = AgentSettings.from_persisted(
    {"schema_version": 1, "agent_kind": "llm", "llm": {"model": "m"}}
)
assert isinstance(legacy, OpenHandsAgentSettings)
assert legacy.agent_kind == "openhands"
assert legacy.schema_version == 2

Type

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

Notes

  • Targeted at a v1.20.1 patch release. The package version bump itself is handled by the prepare-release.yml workflow per .github/workflows/README-RELEASE.md, so this PR intentionally does not touch any pyproject.toml versions.
  • Cloud follow-up (separate PR, not blocking): the OpenHands/OpenHands#14263 hot-fix that hard-codes agent_kind = 'openhands' in OrgStore.get_agent_settings_from_org can be reverted once this SDK release is consumed — the migration handles that case correctly through from_persisted.
  • Process improvement: the engineer who triaged the incident flagged that we don't have an automation that fails CI when a discriminator literal (or other persisted-shape contract) changes without a schema_version bump + matching migration. That's worth a follow-up — the existing api-breakage checker tracks public API surface, not persisted-shape semantics.

This PR was created by an AI agent (OpenHands) on behalf of @robert-brennan during the incident response.


Agent Server images for this PR

• 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 amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22-slim 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:6c23aab-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:6c23aab-golang-amd64
ghcr.io/openhands/agent-server:6c23aab-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:6c23aab-golang-arm64
ghcr.io/openhands/agent-server:6c23aab-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:6c23aab-java-amd64
ghcr.io/openhands/agent-server:6c23aab-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:6c23aab-java-arm64
ghcr.io/openhands/agent-server:6c23aab-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:6c23aab-python-amd64
ghcr.io/openhands/agent-server:6c23aab-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:6c23aab-python-arm64
ghcr.io/openhands/agent-server:6c23aab-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:6c23aab-golang
ghcr.io/openhands/agent-server:6c23aab-java
ghcr.io/openhands/agent-server:6c23aab-python

About Multi-Architecture Support

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

@github-actions

github-actions Bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/settings
   model.py4814890%263, 265, 277, 279, 304–307, 310, 323, 327, 333, 343, 349, 354, 530, 543, 554, 564, 568, 570, 572, 574, 576, 578, 580, 798, 800, 1015, 1082, 1198, 1234–1237, 1263, 1387, 1432, 1464, 1474, 1476, 1481, 1499, 1512, 1514, 1516, 1518, 1525
TOTAL24471707071% 

The v1.19.0 LLMAgentSettings -> OpenHandsAgentSettings rename changed
the canonical agent_kind discriminator from 'llm' to 'openhands' but
did not bump the persisted-settings schema_version or register a
migration. Persisted rows that still carry the legacy
agent_kind='llm' now break callers that validate against the canonical
OpenHandsAgentSettings class (or that route through the discriminated
union expecting routing to match the deployed code path).

This is an in-band incident triage:
- enterprise/server/routes/org_models.py OrgResponse.from_org and
  OrgDefaultsSettingsResponse.from_org go through AgentSettings
  from_persisted, which already runs schema migrations on load. They
  inherit the fix automatically once this migration ships.
- The point hot-fix in OpenHands/OpenHands#14263 (force
  agent_kind='openhands' in OrgStore.get_agent_settings_from_org)
  becomes redundant on top of this migration.

Changes:
- Bump AGENT_SETTINGS_SCHEMA_VERSION 1 -> 2.
- Register _migrate_agent_settings_v1_to_v2 that rewrites legacy
  agent_kind='llm' to 'openhands'. The two classes are field-
  compatible (LLMAgentSettings only narrows the discriminator
  literal), and LLMAgentSettings is scheduled for removal in v1.22.0,
  so canonicalizing on read is safe and consistent with the
  deprecation direction.
- Update existing migration tests for the new current_version (the
  v1 -> v2 step bumps schema_version on every from_persisted call,
  including the v0 entry-point and the ACP no-op path).
- Add test for the new canonicalization migration.

Co-authored-by: openhands <openhands@all-hands.dev>
@neubig
neubig force-pushed the fix-agent-kind-llm-migration branch from f88f559 to 6c23aab Compare May 5, 2026 19:49
@neubig neubig changed the title fix(sdk): tolerate canonical agent_kind in deprecated AgentSettings + bump schema to v2 fix(sdk): add v1->v2 settings migration that canonicalizes agent_kind May 5, 2026
@neubig
neubig marked this pull request as ready for review May 5, 2026 19:51

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

🟢 Good taste - Clean migration fix following established patterns

This PR correctly implements the missing v1→v2 schema migration to canonicalize the deprecated agent_kind: 'llm' discriminator to 'openhands'. The implementation follows the existing migration framework pattern and includes proper test coverage.

[RISK ASSESSMENT]

  • [Overall PR] ⚠️ Risk Assessment: 🟢 LOW

This is a targeted data migration that fixes a production incident. The change:

  • Follows the existing migration framework pattern (_migrate_agent_settings_v1_to_v2 registered at version 1)
  • Only canonicalizes a discriminator value between field-compatible classes (LLMAgentSettings → OpenHandsAgentSettings)
  • Is backward compatible (reads old persisted data correctly without losing fields)
  • Has good test coverage including the new migration path
  • Makes no changes to runtime agent behavior or evaluation-affecting logic

VERDICT:
✅ Worth merging: Clean fix for a production incident with appropriate test coverage and no design concerns.

KEY INSIGHT:
This demonstrates the importance of bumping schema versions and registering migrations whenever persisted data contracts change—even for "cosmetic" changes like renaming classes or narrowing discriminator literals. The missing v1→v2 migration turned a field-compatible refactor into a production incident. Consider adding a CI check that detects discriminator literal changes in persisted models and requires a corresponding schema version bump.

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

✅ QA Report: PASS

The migration correctly canonicalizes agent_kind: 'llm' → 'openhands' and resolves the production incident.

Does this PR achieve its stated goal?

Yes. The PR successfully adds a v1→v2 settings migration that canonicalizes the deprecated agent_kind: 'llm' discriminator to the canonical 'openhands' value. This fixes the production incident where new cloud users encountered 500 errors because persisted org settings had agent_kind: 'openhands' (the post-v1.19.0 canonical value), but validation code expected agent_kind: 'llm'. With this migration in place, AgentSettings.from_persisted(...) now handles both legacy and canonical discriminator values correctly, eliminating the validation error that was causing the 500s.

Phase Result
Environment Setup ✅ Dependencies installed, dev environment ready
CI Status ✅ 21 passing, 7 pending (builds), 0 failing
Functional Verification ✅ All migration scenarios verified end-to-end
Functional Verification

Test 1: Legacy v1 payload with agent_kind='llm'

Step 1 — Establish the test case:
Simulates org settings created before the v1.19.0 LLMAgentSettings → OpenHandsAgentSettings rename.

from openhands.sdk import AgentSettings, OpenHandsAgentSettings

legacy_v1 = AgentSettings.from_persisted(
    {"schema_version": 1, "agent_kind": "llm", "llm": {"model": "test-model"}}
)

Step 2 — Verify migration output:

Type: OpenHandsAgentSettings
agent_kind: openhands
schema_version: 2
llm.model: test-model

Result: ✅ Migration correctly canonicalized 'llm' → 'openhands' and bumped schema_version to 2.


Test 2: Production incident scenario (new user with agent_kind='openhands')

Step 1 — Reproduce the production failure scenario:
Simulates the exact case that caused 500 errors: org settings persisted with agent_kind: 'openhands' (the canonical value) but schema_version: 1 (because they were created after the rename but before this migration was added).

new_user_data = {
    "schema_version": 1,
    "agent_kind": "openhands",
    "llm": {"model": "anthropic/claude-sonnet-4"}
}
settings = AgentSettings.from_persisted(new_user_data)

Step 2 — Verify the fix handles it correctly:

Type: OpenHandsAgentSettings
agent_kind: openhands
schema_version: 2

Result: ✅ The migration handles already-canonical payloads correctly (no-op for the discriminator, but still bumps schema_version).


Test 3: v0→v1→v2 migration chain

Step 1 — Test the full migration chain:

legacy_v0 = AgentSettings.from_persisted({"llm": {"model": "v0-model"}})

Step 2 — Verify all migrations applied:

Type: OpenHandsAgentSettings
agent_kind: openhands
schema_version: 2

Result: ✅ The v0→v1→v2 migration chain works correctly.


Test 4: ACP agent (discriminator should not be affected)

Step 1 — Test non-LLM agent kind:

acp_v1 = AgentSettings.from_persisted(
    {
        "schema_version": 1,
        "agent_kind": "acp",
        "acp_command": ["npx", "-y", "claude-agent-acp"]
    }
)

Step 2 — Verify ACP discriminator unchanged:

Type: ACPAgentSettings
agent_kind: acp
schema_version: 2

Result: ✅ The migration correctly only rewrites 'llm' → 'openhands', leaving other discriminators untouched.


Test 5: Test suite validation

Ran the full settings test suite:

uv run pytest tests/sdk/test_settings.py -xvs

Output:

======================== 37 passed, 9 warnings in 0.11s ========================

Result: ✅ All 37 tests pass, including:

  • test_agent_settings_from_persisted_migrates_v0_llm_payload (updated to expect schema_version=2)
  • test_agent_settings_from_persisted_canonicalizes_legacy_llm_kind (new test for v1→v2 migration)
  • test_agent_settings_from_persisted_dispatches_current_acp_payload (updated to expect schema_version=2)
  • test_agent_settings_from_persisted_rejects_newer_schema_version (updated to reject v3, not v2)

Issues Found

None.

@xingyaoww
xingyaoww merged commit 4f419ce into main May 5, 2026
43 of 44 checks passed
@xingyaoww
xingyaoww deleted the fix-agent-kind-llm-migration branch May 5, 2026 20:23
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 2026
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.

5 participants