Skip to content

[Bug]: A second server's first load of a shared conversation reverts meta.json to its stale startup copy #5629

Description

@enyst

I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.

Is there an existing issue for the same bug?

  • I have searched existing issues and this is not a duplicate.

Bug Description

With two agent servers on one conversations directory, the second reads every meta.json once at startup. Its first load of a conversation (takeover from a dead owner, or a claim after the owner released it) starts from that copy and writes it back to meta.json. A title or updated_at the owner stored in between is lost; per the code, tags and stored secrets revert the same way. Failover and rolling restarts on shared storage hit this.

Expected Behavior

After the second server loads the conversation, meta.json and its GET /api/conversations/{id} keep the owner's last title (after B) and updated_at.

Actual Behavior

The load claims the lease (generation 2 after a SIGKILL of the owner, 1 after a graceful stop), but meta.json is rewritten with the second server's startup title (before B) and older updated_at, and its GET returns before B.

Steps to Reproduce

  1. In a checkout of main after uv sync:
    export SHARED=$(mktemp -d) OH_SECRET_KEY=$(openssl rand -hex 32)
    export OH_CONVERSATIONS_PATH=$SHARED/conversations
    .venv/bin/python -m openhands.agent_server --port 8001 & A=$!
  2. Create a conversation on A (never run) and title it:
    H='Content-Type: application/json'
    ID=$(curl -s -X POST localhost:8001/api/conversations -H "$H" -d '{"agent_settings":{"agent_kind":"openhands","llm":{"model":"openai/x","api_key":"x","base_url":"http://127.0.0.1:9/v1"},"tools":[]},"workspace":{"kind":"LocalWorkspace","working_dir":"'"$SHARED"'/ws"},"autotitle":false}' | jq -r .id)
    curl -s -X PATCH localhost:8001/api/conversations/$ID -H "$H" -d '{"title":"before B"}'
  3. Start B on the same directory: .venv/bin/python -m openhands.agent_server --port 8002 &
  4. Rename on A: PATCH {"title":"after B"} as above returns {"success": true}; jq .title $OH_CONVERSATIONS_PATH/${ID//-/}/meta.json prints "after B".
  5. Stop A: kill $A (graceful) or kill -9 $A (takeover).
  6. Load on B: curl -s localhost:8002/api/conversations/$ID/events/count returns 200.
  7. jq '{title, updated_at}' $OH_CONVERSATIONS_PATH/${ID//-/}/meta.json and curl -s localhost:8002/api/conversations/$ID | jq .title both show before B.

With the verify-agent-server skill (#5621): control-agent-server map run --file F31 --only F31.takeover-keeps-metadata --fresh reports the known-bug bullet as an expected failure while the bug lasts.

Acceptance Criteria

  • Loading a persisted conversation reads meta.json from disk after the lease claim, so the previous owner's title, tags, secrets and updated_at survive in meta.json and the loading server's GET.
  • A regression test in tests/agent_server: a second ConversationService started before the first one renames the conversation keeps the newer title after loading it.
  • The F31.takeover-keeps-metadata map bullet passes with its # bug markers removed.

Installation Method

uv sync in a checkout of main

Version

main at 2804c12

Additional Context

Root cause (under openhands-agent-server/openhands/agent_server/):

  • __aenter__ reads the catalog once (conversation_service.py:2025, _load_catalog_sync :544); later only execution_status is refreshed (:843), and refresh_persisted_conversation (:924) is only called by the Docker runtime.
  • _get_or_load_event_service_locked passes that snapshot, record.stored, to _start_event_service (:1068-1070).
  • _start_event_service builds the EventService from it (:2272); EventService.start() claims the lease (event_service.py:1160), then save_meta() (conversation_service.py:2320) writes the stale copy over the newer meta.json, and the catalog record is replaced with it (:2339-2342).
  • EventService.load_meta() (event_service.py:279) is never called.

Suggested fix: when resuming (not is_new_conversation), re-read meta.json after the lease claim and before any write, e.g. call load_meta() in EventService.start() right after self._lease.claim() when meta.json exists, and have _start_event_service use event_service.stored afterwards (the autotitle check at :2296 reads the stale argument). Reading before the claim races the outgoing owner's last write.


OpenHands AI triage

The following comments and acceptance criteria were added by the OpenHands AI agent.

Triage

Real bug in the shared-storage load path, scoped to the agent-server package:
ConversationService.__aenter__ snapshots every meta.json once at startup
(conversation_service.py:2025 → _load_catalog_sync :544); on a later load
_get_or_load_event_service_locked passes that snapshot to _start_event_service
(:1068–1070), which builds the EventService from it (:2272). EventService.start()
claims the lease (event_service.py:1160) and _start_event_service then writes the
stale copy back with save_meta() (:2320) and replaces the catalog record (:2339–2342).
The previous owner's title, tags, stored secrets and updated_at are lost.
EventService.load_meta() (event_service.py:279) is the existing reader for
meta.json and is not on this path.

Scope: on resume (base_state.json exists, i.e. not is_new_conversation), re-read
meta.json from disk after the lease claim and before any write, and make the catalog
record, the subsequent save_meta(), the autotitle decision, and the composed
ConversationInfo all derive from that re-read copy. load_meta() and save_meta()
are the authoritative read/write mechanisms and should be reused rather than duplicated.

Non-goals: no change to lease semantics or RefreshPersistedConversation/Docker-runtime
behavior; no meta.json schema, persisted-settings version, or REST contract change; no
new public Python/REST API; no redesign of the catalog cache or a speculative cross-server
merge strategy. This issue should not prescribe the implementation beyond reusing those
existing helpers; the suggested fix is a lead, not a requirement.

Acceptance Criteria

  • After a second agent-server process claims a persisted conversation whose owner last wrote newer metadata (title, tags, stored secret, updated_at), the load leaves the owner's values intact in meta.json and in the loading server's GET /api/conversations/{id}. With the Steps to Reproduce, both jq .title $OH_CONVERSATIONS_PATH/${ID//-/}/meta.json and the GET report after B (for both graceful stop and SIGKILL takeover).
  • The re-read happens after the lease claim and before the first write: for a resume the EventService starts from the meta.json read on disk, not from the StoredConversation captured at ConversationService.__aenter__. The catalog record stored back, the autotitle check (stored.autotitle and stored.title is None), the subsequent save_meta(), and the returned ConversationInfo all reflect the re-read copy.
  • A new conversation (base_state.json absent) is unchanged: it does not re-read a pre-existing meta.json and still persists the creating request's metadata.
  • Regression test in tests/agent_server: a second ConversationService is constructed (loading the catalog) before a first service renames a conversation via update_conversation; after the first service is stopped and the second loads that conversation, the newer title (and updated_at) survive in both the on-disk meta.json and the returned ConversationInfo. The test drives the real ConversationService/EventService load path, not mocks.
  • Failure and edge cases: when the conversation is held by a live owner, the second server does not load it and leaves meta.json untouched; and a conversation whose meta.json cannot be read/parsed fails the load through the existing startup-failure cleanup rather than overwriting it with the stale startup copy.
  • Compatibility and lifecycle: no change to the meta.json schema, persisted-settings versions, or REST request/response shapes; secrets continue through the existing openhands.sdk.utils.pydantic_secrets + cipher path with no new redaction and no plaintext in logs; existing start-failure rollback/close behavior is preserved.
  • Validation: uv run pytest tests/agent_server passes including the new regression test, and the reproduction in Steps to Reproduce shows after B in both meta.json and the GET on the fix revision. Once Add verify-agent-server: an executable feature map and control CLI for the Agent Server API #5621 is on main and the verify-agent-server skill is available, control-agent-server map run --file F31 --only F31.takeover-keeps-metadata --fresh passes after removing that bullet's # bug markers.

Activity

  1. added
    bug
    on Oct 9, 2026
  2. added theissue type on Oct 9, 2026
  3. added
    ready-for-dev
    on Oct 9, 2026
  4. Jaxon1216 commented on Oct 13, 2026

    @Jaxon1216

    I’m working on a focused fix for this. I’ll reload persisted metadata after acquiring the lease, keep the catalog and returned conversation info aligned with that metadata, and cover graceful release plus forced takeover with a real two-server reproduction and regression tests. AI-assisted development.

  5. enyst commented on Oct 13, 2026

    @enyst
    MemberAuthor

    I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

    Going through the triage acceptance criteria above, with how #5777 handles each one. That PR is an independent fix, written at the maintainer's request for comparison with the open contributor PR, without reading it.

    • Re-read after the lease claim, before the first write. EventService.start() calls the existing load_meta() right after self._lease.claim() and before _scrub_persisted_credentials() (the first possible write) on every resume. The catalog record, save_meta(), the autotitle check (stored.autotitle and stored.title is None), the telemetry context and the returned ConversationInfo all use the re-read copy.
    • New conversations unchanged. The re-read is enabled only when _start_event_service is not called with is_new_conversation=True, so creation and forks still persist the request's metadata. No meta.json file is re-read for them.
    • Graceful stop and SIGKILL takeover. Both were run with the Steps to Reproduce on two real server processes. On main both show before B; on the PR both show after B, with the owner's updated_at in meta.json and the GET. The full output is in the PR.
    • Live owner. No change: the lease claim still raises ConversationLeaseHeldError before any read or write, so meta.json is untouched.
    • Unreadable meta.json. The load now fails through the existing startup-failure cleanup (close, no save_meta()) and leaves the file as it was. On main it was overwritten with the startup copy; a regression test covers this. A meta.json that is missing at load time keeps the catalog copy, which is the current behaviour.
    • Compatibility. No change to the schema, persisted-settings versions, REST shapes, leases or the Docker-runtime refresh. Secrets still go through the cipher in load_meta()/save_meta().
    • One thing that needed care. start_conversation on an existing id used to inject the ChatGPT auth fallback by patching the catalog record before loading. A plain re-read would have dropped it. It is now passed explicitly as resume_secrets and layered over the re-read secrets, the same precedence as before.
    • Validation. The regression test (a second ConversationService started before the owner's update_conversation) passes. F31.takeover-keeps-metadata showed xfail twice on main and xpass twice on the PR, and it passes with its # bug markers removed in the same PR. Add verify-agent-server: an executable feature map and control CLI for the Agent Server API #5621 is merged, so the map bullet is updated there.

    No point here seems to need a maintainer decision beyond choosing between the two PRs.


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugready-for-dev

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions