Skip to content

refactor: remove redundant D1 dual-writes from router#586

Merged
ColeMurray merged 1 commit intomainfrom
refactor/remove-router-d1-dual-writes
May 2, 2026
Merged

refactor: remove redundant D1 dual-writes from router#586
ColeMurray merged 1 commit intomainfrom
refactor/remove-router-d1-dual-writes

Conversation

@ColeMurray
Copy link
Copy Markdown
Owner

@ColeMurray ColeMurray commented May 2, 2026

Summary

  • Removes redundant synchronous D1 writes from 4 router handlers (handleArchiveSession, handleUnarchiveSession, handleUpdateSessionTitle, handleCancelChild) — the DO already syncs status to D1 via transitionSessionStatussyncSessionIndexStatus using waitUntil
  • Adds syncSessionIndexTitle to the DO (matching the existing syncSessionIndexStatus pattern) since title was the one case where the DO didn't previously sync to D1
  • These handlers are now pure DO proxies — the router no longer participates in D1 status/title coordination

Why this is safe

The web client doesn't rely on the router's synchronous D1 write for UI feedback:

  • Archive/unarchive/cancel use optimistic cache mutations + WebSocket session_status broadcasts
  • The DO's waitUntil D1 write handles eventual consistency for sidebar list queries
  • The previous dual-write pattern created a race between the router's synchronous write and the DO's background write to the same D1 row

Test plan

  • Updated lifecycle handler test to verify DO-side syncSessionIndexTitle is called with correct session ID and title
  • All 1039 existing control-plane tests pass
  • Typecheck passes
  • Lint passes

Summary by CodeRabbit

  • Refactor
    • Improved session management architecture by refactoring how session information is synchronized to the database, now using asynchronous background processing for better reliability.

The Durable Object already syncs status transitions to D1 via
syncSessionIndexStatus in transitionSessionStatus. The router's
synchronous D1 writes after calling the DO were redundant.

- Remove D1 writes from handleArchiveSession, handleUnarchiveSession,
  handleUpdateSessionTitle, and handleCancelChild
- Add syncSessionIndexTitle to the DO so title updates are synced
  to D1 from within the DO (matching the pattern used for status)
- Wire the new sync through SessionLifecycleHandlerDeps
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 480d2c22-17b5-4e33-a363-90912d252693

📥 Commits

Reviewing files that changed from the base of the PR and between ad7f4ea and b27f22f.

📒 Files selected for processing (4)
  • packages/control-plane/src/router.ts
  • packages/control-plane/src/session/durable-object.ts
  • packages/control-plane/src/session/http/handlers/session-lifecycle.handler.test.ts
  • packages/control-plane/src/session/http/handlers/session-lifecycle.handler.ts
💤 Files with no reviewable changes (1)
  • packages/control-plane/src/router.ts

📝 Walkthrough

Walkthrough

The PR moves D1 session index synchronization from synchronous router handlers to asynchronous background tasks via the Durable Object layer. Four router handlers no longer directly call index updates; instead, a new syncSessionIndexTitle callback is wired through the session lifecycle handler to the DO, where it executes asynchronously via ctx.waitUntil.

Changes

Session Index Synchronization Refactor

Layer / File(s) Summary
Interface & Dependencies
src/session/http/handlers/session-lifecycle.handler.ts
SessionLifecycleHandlerDeps gains required syncSessionIndexTitle(sessionId, title) callback to support async index synchronization.
Handler Logic
src/session/http/handlers/session-lifecycle.handler.ts
updateTitle handler computes the public session ID and calls syncSessionIndexTitle after persisting the title, enabling async index sync.
DO Wiring & Implementation
src/session/durable-object.ts
Lifecycle handler is configured with syncSessionIndexTitle callback that asynchronously updates the D1 session index via ctx.waitUntil, with background error logging.
Router Cleanup
src/router.ts
Removes synchronous D1 index update calls from four handlers: handleUpdateSessionTitle, handleArchiveSession, handleUnarchiveSession, and handleCancelChild. Handlers now return only the DO response.
Tests
src/session/http/handlers/session-lifecycle.handler.test.ts
Test harness adds syncSessionIndexTitle mock; "updates title" test now verifies the callback is invoked with public session ID and title.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A sync becomes async, and handlers grow light,
While Durable Objects hum through the night,
Index updates drift gently to background tasks true—
No waiting required when the router flows through! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: removing redundant D1 dual-writes from the router handlers, which is the primary objective of this refactoring.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/remove-router-d1-dual-writes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 5/8 reviews remaining, refill in 15 minutes and 1 second.

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 2, 2026

Terraform Validation Results

Step Status
Format
Init
Validate

Note: Terraform plan was skipped because secrets are not configured. This is expected for external contributors. See docs/GETTING_STARTED.md for setup instructions.

Pushed by: @ColeMurray, Action: pull_request

Copy link
Copy Markdown
Contributor

@open-inspect open-inspect Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This PR removes redundant router-side D1 writes for session archive/unarchive/title-update/cancel flows and moves the remaining title sync into the Durable Object, which keeps D1 coordination in a single place and avoids the prior dual-write race. Overall this looks correct: status flows already sync through transitionSessionStatus, and the new syncSessionIndexTitle matches that existing background-update pattern.

  • PR Title: refactor: remove redundant D1 dual-writes from router
  • PR Number: #586
  • Author: @ColeMurray
  • Files changed: 4
  • Additions/Deletions: +34 / -35

Critical Issues

None.

Suggestions

None.

Nitpicks

None.

Positive Feedback

  • Consolidating D1 synchronization into the DO removes duplicated responsibility and the synchronous/background write race from the router paths.
  • The title path now mirrors the existing status-sync pattern closely, which improves maintainability and makes the ownership boundary clearer.
  • The updated handler test covers the new title-sync dependency at the right seam without over-coupling to router behavior.

Questions

None.

Verdict

Approve

@ColeMurray ColeMurray merged commit 26f77fe into main May 2, 2026
18 checks passed
@ColeMurray ColeMurray deleted the refactor/remove-router-d1-dual-writes branch May 2, 2026 06:28
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.

1 participant