Skip to content

fix(enterprise): atomically migrate legacy empty tool settings - #12

Merged
simonrosenberg merged 4 commits into
mainfrom
fix-legacy-empty-tools
Jul 29, 2026
Merged

simonrosenberg merged 4 commits into
mainfrom
fix-legacy-empty-tools

Conversation

@simonrosenberg

@simonrosenberg simonrosenberg commented Jul 27, 2026 •

Copy link
Copy Markdown
Member

Replacement for OpenHands/OpenHands#15343 after the Agent Canvas repository cutover.

HUMAN:

Replace legacy empty tool arrays with the default-tool sentinel through a durable database migration, so cloud and self-hosted upgrades can safely restore Agent Profile tool selection.

  • A human has tested these changes.

AGENT:

Rebased onto current main, covers the legacy user-settings source used by lazy user migration, and replaces stale full-document writes with atomic database updates.


Why

Before openhands-sdk 1.31.2, OpenHandsAgentSettings.tools defaulted to []. Ordinary cloud settings saves serialized that default into persisted settings.

SDK 1.31.2 changed the contract:

  • tools: null uses the default toolset.
  • tools: [] creates an intentionally bare agent.
  • A non-empty list uses that explicit toolset.

The persisted rows had no schema marker or migration. When OpenHands/OpenHands#15228 honored stored tool values during cloud launches, affected users lost terminal, file editor, task tracker, and browser tools. OpenHands/OpenHands#15339 reverted that behavior as containment.

Self-hosted installations may skip releases, so the compatibility boundary belongs in Alembic. The deprecated user_settings table must also be migrated because UserStore.migrate_user can still copy its agent_settings into new org and member rows after this revision has run.

Summary

  • Add Alembic revision 141, following current revision 140.
  • Migrate exact top-level tools: [] values to tools: null in:
    • user_settings.agent_settings
    • org.agent_settings
    • org_member.agent_settings_diff
  • Migrate both migrated and not-yet-migrated legacy user rows.
  • Use one set-based update per table; PostgreSQL applies jsonb_set to the current row value so concurrent changes are preserved.
  • Preserve every other setting in each JSON document.
  • Leave missing, null, and non-empty tool values unchanged.
  • Leave org.agent_profiles untouched, so profile-level tools: [] remains an intentional bare-agent configuration.
  • Verify that a normalized legacy user remains on default-tool semantics through the complete lazy UserStore.migrate_user path.
  • Assert that the PostgreSQL migration emits only atomic UPDATE statements and no read-then-write SELECT loop.

This migration does not re-land OpenHands/OpenHands#15228. It establishes the data precondition needed to reintroduce those launch features in a separate follow-up.

Issue Number

Closes #68.

How to Test

PYTHONPATH=".:${PYTHONPATH}" poetry run --project=enterprise pytest -q \
  -p no:ddtrace -p no:ddtrace.pytest_bdd -p no:ddtrace.pytest_benchmark \
  enterprise/tests/unit/test_migration_*.py \
  tests/unit/test_enterprise_migration_integrity.py

Result: 24 passed.

PYTHONPATH=".:${PYTHONPATH}" poetry run --project=enterprise pytest -q \
  -p no:ddtrace -p no:ddtrace.pytest_bdd -p no:ddtrace.pytest_benchmark \
  enterprise/tests/unit/test_user_store.py

Result: 70 passed with pre-existing aiosqlite event-loop shutdown warnings.

poetry run --project=enterprise pre-commit run \
  --config enterprise/dev_config/python/.pre-commit-config.yaml \
  --show-diff-on-failure --files \
  enterprise/migrations/versions/141_normalize_legacy_empty_tools.py \
  enterprise/tests/unit/test_migration_141_normalize_legacy_empty_tools.py

Result: all hooks passed, including Ruff, formatting, and mypy.

poetry run python scripts/check_enterprise_migration_integrity.py

Result: Enterprise migration integrity checks passed.

The migration was run directly and twice against PostgreSQL 16 with the production pg8000 driver. In a blocked-writer reproduction, revision 141 waited behind a concurrent org update, then preserved the writer’s new model and marker while changing only tools to JSON null. The second run made no further changes; all three target tables had zero legacy rows remaining, and an Agent Profile containing tools: [] remained unchanged.

For staging and production validation, record these counts before and after upgrading; all three should be zero afterward:

SELECT count(*) FROM user_settings
WHERE agent_settings::jsonb -> 'tools' = '[]'::jsonb;

SELECT count(*) FROM org
WHERE agent_settings::jsonb -> 'tools' = '[]'::jsonb;

SELECT count(*) FROM org_member
WHERE agent_settings_diff::jsonb -> 'tools' = '[]'::jsonb;

Then reapply the OpenHands/OpenHands#15228 behavior on a staging build and launch conversations from a default profile, an intentionally bare profile, and a profile with an explicit tool list.

Video/Screenshots

Not applicable; this is a backend data migration.

Type

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

Notes

The downgrade is intentionally a no-op: after upgrade, a migrated null cannot be distinguished from a null that was already legitimate. Converting null values back to empty lists would corrupt user intent.


Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-7534e1b

@github-actions github-actions Bot added the type: fix A bug fix label Jul 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ This PR contains migrations. Please synchronize before merging to prevent conflicts.

@github-actions

github-actions Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  enterprise/migrations/versions
  141_normalize_legacy_empty_tools.py 17-29, 37-46, 50
Project Total  

This report was generated by python-coverage-comment-action

@simonrosenberg

Copy link
Copy Markdown
Member Author

Repository-cutover replacement for OpenHands/OpenHands#15343; the validated commit is unchanged. All migration, lint, and Python/enterprise test checks pass. The only red check is the cutover-wide GHCR package authorization failure recorded at OpenHands/OpenHands#15396.

@simonrosenberg
simonrosenberg requested a review from jlav July 27, 2026 13:46
@simonrosenberg

Copy link
Copy Markdown
Member Author

Read-only pre-migration database baseline completed on 2026-07-27. Both connections reported transaction_read_only = on; deployed image is cloud-1.47.1 in both environments.

Environment user_settings.agent_settings org.agent_settings org_member.agent_settings_diff
Staging 0 58 14
Production 0 14,283 2,208

These are exact top-level tools: [] row counts. All three counts must be zero after revision 141 is deployed, before #13 is enabled.

@simonrosenberg
simonrosenberg marked this pull request as draft July 28, 2026 07:40
@simonrosenberg
simonrosenberg force-pushed the fix-legacy-empty-tools branch from 9d28790 to 5c9020a Compare July 28, 2026 07:46
@simonrosenberg

Copy link
Copy Markdown
Member Author

Concurrency fix pushed in 5c9020a. Revision 141 now performs one atomic, set-based JSON update per table instead of reading rows and writing stale full documents.

Validated against PostgreSQL 16 with the production pg8000 driver: the migration blocked behind a concurrent org-row writer, then preserved the writer's new model/marker while changing only tools to JSON null. A second run was idempotent and left zero matching rows. Local migration/integrity tests (24), user-store tests (70), Ruff, formatting, and mypy all pass.

@simonrosenberg
simonrosenberg marked this pull request as ready for review July 28, 2026 07:57
@simonrosenberg simonrosenberg changed the title fix(enterprise): migrate legacy empty tool settings fix(enterprise): atomically migrate legacy empty tool settings Jul 28, 2026
@simonrosenberg
simonrosenberg requested review from a team and saurya and removed request for a team July 28, 2026 08:52
@simonrosenberg simonrosenberg self-assigned this Jul 28, 2026

@jlav jlav left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this out against a real postgres database containing about twice the amount of data in the tables that this migration would touch in production, and the transaction took about 1.5 seconds.

In prod, these tables are seeing about 3 writes/min each. With such a low rate, I'm not expecting write contention to be much of an issue.

@simonrosenberg
simonrosenberg marked this pull request as draft July 29, 2026 10:31
@simonrosenberg
simonrosenberg force-pushed the fix-legacy-empty-tools branch from 5c9020a to 7534e1b Compare July 29, 2026 10:35
@simonrosenberg
simonrosenberg marked this pull request as ready for review July 29, 2026 10:49
@simonrosenberg
simonrosenberg merged commit 4f922a6 into main Jul 29, 2026
21 checks passed
@simonrosenberg
simonrosenberg deleted the fix-legacy-empty-tools branch July 29, 2026 11:40
@openhands-release-bot openhands-release-bot Bot added the released: 1.49.0 Shipped in 1.49.0 label Jul 30, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.49.0.

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

Labels

released: 1.49.0 Shipped in 1.49.0 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(enterprise): normalize legacy empty tool settings before honoring profile tools

3 participants