Skip to content

[Bug]Deep clone of entire message history causes 30+ second delay with 200 messages #10187

Closed

Description

@illuxiza

Description

Deep clone of entire message history causes 30+ second delay with 200 messages

Description

In long sessions (~200 messages), sending a new message takes 30+ seconds before getting any response. The delay is caused by a deep clone of the entire message history on every loop iteration.

Location

packages/opencode/src/session/prompt.ts, line 573:

const sessionMessages = clone(msgs)

Why this is slow

The clone() function from remeda recursively copies:

  • Every message object
  • Every part (text, tool calls, attachments)
  • All nested content including large tool outputs

With 200 messages × 5 parts each = 1000+ objects deep cloned per iteration.

Plugins

No response

OpenCode version

Latest

Steps to reproduce

  1. Build up a session with 100+ messages (long refactoring task)
  2. Send a new message
  3. Wait 30+ seconds before the model starts responding
  4. Observe the delay increases with message count

Screenshot and/or share link

No response

Operating System

macOS 26.2

Terminal

wave

Activity

  1. changed the title [-]Deep clone of entire message history causes 30+ second delay with 200 messages[/-] [+][Bug]Deep clone of entire message history causes 30+ second delay with 200 messages[/+] on Jan 23, 2026
  2. added
    perfIndicates a performance issue or need for optimization
    on Jan 23, 2026
  3. github-actions commented on Jan 23, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    Feel free to ignore if none of these address your specific case.

  4. added a commit that references this issue on Jan 23, 2026
    a0aef5b
  5. Jaakkonen commented on Feb 19, 2026

    @Jaakkonen

    Reproduced this issue on latest main commit and took a flamegraph with bun run --cpu-prof src/index.ts serve while continuing a long session.

    Image

    This causes 100% CPU usage and really slow conversation. The PR #10190 seems to be fixing this issue properly.

    After fixing that cloning issue the flamegraph looks something like

    Image

    This can be fixed with a 1-line patch like const sessionMessages = clone(msgs) -> const sessionMessages = step > 1 && lastFinished ? structuredClone(msgs) : msgs utilizing Bun fast structuredClone.

  6. github-actions commented on Apr 21, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingperfIndicates a performance issue or need for optimization

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions