Skip to content

Add ConversationState.copy() method to deduplicate state-cloning logic #2885

@xingyaoww

Description

@xingyaoww

Why

The LocalConversation.fork() method (introduced in #2841) contains inline logic for deep-copying conversation state fields (events, agent_state, activated_knowledge_skills, stats, tags). Each field requires different copy semantics:

  • events: deep-copied via event.model_copy(deep=True) (mutable Pydantic models)
  • agent_state: deep-copied via copy.deepcopy() (arbitrary mutable values)
  • activated_knowledge_skills: shallow list copy (immutable str elements)
  • stats: model_copy(deep=True) (Pydantic model)
  • tags: shallow dict copy (immutable str keys/values)

This knowledge is currently embedded inside fork(). As more operations need to clone or snapshot state (e.g., checkpointing, branching, replay), this logic would be duplicated.

Proposal

Add a copy() (or snapshot()) method on ConversationState that encapsulates the field-by-field copy semantics and returns a new ConversationState with its own identity and independent mutable state. The method should:

  1. Accept an optional new ConversationID
  2. Deep-copy events, agent_state, and stats
  3. Shallow-copy immutable-element collections (activated_knowledge_skills, tags)
  4. Handle private attributes (_fs, _events, _lock) appropriately for the new instance
  5. Optionally accept reset_metrics=True to start fresh stats

This would let fork() delegate to state.copy(...) instead of managing the details inline.

Context

Raised during review of #2841 by @xingyaoww: #2841 (comment)

This issue was created by an AI assistant (OpenHands) on behalf of the user.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginvariantsthe design invariants of the codebase

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions