Skip to content

fix: use single transaction for session fork#16317

Open
uriva wants to merge 1 commit intoanomalyco:devfrom
uriva:fix/fork-performance
Open

fix: use single transaction for session fork#16317
uriva wants to merge 1 commit intoanomalyco:devfrom
uriva:fix/fork-performance

Conversation

@uriva
Copy link

@uriva uriva commented Mar 6, 2026

Issue for this PR

Closes #16311

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Session fork was calling updateMessage() and updatePart() individually for every message and part. Each call runs its own SQLite transaction (with fsync), Zod schema validation, and publishes an SSE bus event. For a long session with 100 messages and ~1000 parts, that's ~1,100 separate transactions, validations, and events.

Replaced with a single Database.transaction() that reads messages/parts directly from the DB and inserts them in bulk. No Zod re-validation needed (data is already valid from the DB), no per-row bus events (the session.created event is sufficient for clients).

How did you verify your code works?

  • Typecheck passes (bun turbo typecheck)
  • Code review: message IDs are remapped via idMap, parent IDs updated correctly, parts associated with new message IDs, data copied as-is from source rows

Screenshots / recordings

N/A — backend-only change, no UI changes.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Mar 6, 2026
@uriva
Copy link
Author

uriva commented Mar 6, 2026

@thdxr @adamdotdevin — small perf fix, single file change. Fork was doing M+P separate SQLite transactions for long sessions, now it's one. Happy to address any feedback!

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Mar 6, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2026

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: /fork is incredibly slow for long sessions

1 participant