warp icon indicating copy to clipboard operation
warp copied to clipboard

Fix optimistic-root persistence breaking conversation restore (QUALITY-774)

Open cephalonaut opened this issue 6 months ago • 1 comments

Description

Fix-forward for the bug introduced by #10794 where Agent Mode conversations can fail to restore after a quit/restart, sometimes producing the server error Response stream finished unexpectedly with internal error: Not found: Tried to add messages to non-existent task (id=).

Root cause. PR #10794 added eager persist_conversation_state calls at several sites (start_new_child_conversation, initialize_output_for_response_stream, assign_run_id_for_conversation, mark_conversation_as_remote_child) that fire while the root task is still Optimistic(Root). Task::source_for_persistence was synthesizing a stub api::Task for the optimistic root, which was written to agent_tasks keyed by the client-generated UUID. After the server upgraded the root, a second row was written keyed by the server task id; the stub row was never deleted. On restore, AIConversation::new_restored saw two parentless tasks and HashMap iteration order non-deterministically picked between them — when the stub won, restore left the conversation stuck with an empty root and subtask parent_task_id references pointing at a server root id that no longer existed in the task store.

Fix. Two coordinated changes:

  1. Writer side (Task::source_for_persistence): returns None for Optimistic(Root), matching the existing Optimistic(CLIAgent(_)) behavior. No new stub rows are written. AgentConversationData.root_task_is_optimistic is retained on the persistence model for backward-compatible deserialization of existing local DB rows, but is no longer written and is ignored on restore.

  2. Restore side (AIConversation::new_restored):

    • Two single-purpose constructors. new_restored is strict (returns Err(NoRootTask) for an empty tasks list) and is used by cloud-restore and fork-insert, where an empty payload is malformed input. new_restored_synthesizing_on_empty is a small wrapper that synthesizes a fresh in-progress Optimistic(Root) conversation when tasks is empty (mirroring AIConversation::new()) and is used by the local-DB restore path, where an empty agent_tasks row is the normal shape for child conversations persisted before their first server response.
    • Restore-side dedupe: when multiple parentless tasks exist (legacy DB case), prefer the candidate with non-empty messages. Silently heals pre-fix rows without a SQLite migration.

Removes Task::new_restored_optimistic_root and Task::is_optimistic_root_task; replaces the previous PersistedConversationFields helper by inlining the overlay logic into the single constructor. Adds a #[cfg(test)] pub(crate) helper for driving root upgrades in tests.

Linked Issue

QUALITY-774 — fix-forward of #10794.

  • [ ] The linked issue is labeled ready-to-spec or ready-to-implement.
  • [ ] Where appropriate, screenshots or a short video of the implementation are included below (especially for user-visible or UI changes).

Testing

  • New deterministic fail-first writer-side test test_persist_with_optimistic_root_emits_event_with_no_task_rows — proves the writer bug exists on master and disappears with the fix.
  • New defense-in-depth tests in app/src/ai/blocklist/history_model_tests.rs:
    • test_optimistic_root_upgrade_then_persist_emits_event_with_single_server_task_row
    • test_optimistic_root_restore_round_trip_yields_in_progress_optimistic_root
    • test_truncate_from_exchange_to_empty_persist_event_has_empty_updated_tasks
    • test_two_restart_cycles_keep_exactly_one_server_root_task_row
  • New restore-semantics tests in app/src/ai/agent/conversation_tests.rs:
    • restored_conversation_with_empty_task_list_creates_in_progress_optimistic_root
    • restored_conversation_ignores_legacy_root_task_is_optimistic_flag_with_non_empty_tasks
    • restored_conversation_ignores_legacy_root_task_is_optimistic_flag_with_empty_tasks
    • new_restored_with_empty_task_list_returns_no_root_task_error
    • test_new_restored_prefers_parentless_task_with_messages_over_empty_stub (50-iteration loop per Vec ordering to catch HashMap-order regressions)
  • Existing agent_conversation_data_roundtrips_optimistic_root_marker in crates/persistence/src/model.rs still passes, pinning back-compat deserialization of the legacy field.
  • ./script/presubmit passes end-to-end.

Note on legacy data: This PR does not physically remove pre-existing orphan stub rows in dev/preview local SQLite, but the restore-side dedupe silently ignores them. Affected internal users should not need to clear local AI history.

  • [x] I have manually tested my changes locally with ./script/run

Agent Mode

  • [x] Warp Agent Mode - This PR was created via Warp's AI Agent Mode

Conversation: https://staging.warp.dev/conversation/3fc61b76-c75b-4e86-bc4a-6f4534e44abb Plan: https://staging.warp.dev/drive/notebook/EAwKh7EVES70otTsIdkZ9u

cephalonaut avatar May 28 '26 05:05 cephalonaut

@cephalonaut

I'm starting a first review of this pull request.

You can view the conversation on Warp.

I completed the review and no human review was requested for this pull request.

Comment /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).

Powered by Oz

warp-for-oss[bot] avatar May 28 '26 05:05 warp-for-oss[bot]