Repository navigation
fix(daemon): split router subscription fanout - #11490
Conversation
Co-authored-by: Orca <[email protected]>
Greptile SummaryThis PR extracts the subscription fan-out logic from
Confidence Score: 5/5Safe to merge — this is a mechanical extraction of existing logic into a new class with no behavior changes. The refactoring faithfully preserves all subscription semantics: the adapter list is snapshot-captured at construction time (correct, since current/legacy are never mutated after construction), the onData/onExit listener arrays work identically, dispose() remains idempotent via splice(0), and the three disposal paths in the router (dispose, disposeRouterOnly, disconnectOnly) all delegate correctly. The onBackgroundStreamEvent and onWriteUnavailable paths return caller-owned unsubscribers exactly as before. The test suite passed with 1164 tests, typecheck is clean, and the lint violation that motivated this change is resolved. Files Needing Attention: No files require special attention.
|
| Filename | Overview |
|---|---|
| src/main/daemon/daemon-pty-adapter-subscription-fanout.ts | New class faithfully extracts the onData/onExit fan-out and delegates onBackgroundStreamEvent/onWriteUnavailable directly to adapters; dispose() is idempotent via splice(0); no behavior change from the original router logic. |
| src/main/daemon/daemon-pty-router.ts | Cleanly delegates all subscription APIs to the new fanout class; all three dispose paths preserved; allAdapters() snapshot taken at construction is correct since current/legacy are never mutated after construction. |
Class Diagram
%%{init: {'theme': 'neutral'}}%%
classDiagram
class DaemonPtyRouter {
-current: DaemonPtyAdapter
-legacy: DaemonPtyAdapter[]
-sessionAdapters: Map~string, DaemonPtyAdapter~
-subscriptions: DaemonPtyAdapterSubscriptionFanout
+onData(callback) () => void
+onExit(callback) () => void
+onBackgroundStreamEvent(callback) () => void
+onWriteUnavailable(callback) () => void
+onReplay(callback) () => void
+dispose() void
+disposeRouterOnly() void
+disconnectOnly() Promise~void~
}
class DaemonPtyAdapterSubscriptionFanout {
-adapters: readonly DaemonPtyAdapter[]
-unsubscribers: Array~()=>void~
-dataListeners: Array~callback~
-exitListeners: Array~callback~
+onData(callback) () => void
+onExit(callback) () => void
+onBackgroundStreamEvent(callback) () => void
+onWriteUnavailable(callback) () => void
+onReplay(callback) () => void
+dispose() void
}
class DaemonPtyAdapter {
+onData(callback) () => void
+onExit(callback) () => void
+onBackgroundStreamEvent(callback) () => void
+onWriteUnavailable(callback) () => void
}
DaemonPtyRouter --> DaemonPtyAdapterSubscriptionFanout : delegates subscriptions
DaemonPtyAdapterSubscriptionFanout --> DaemonPtyAdapter : subscribes to each adapter
DaemonPtyRouter --> DaemonPtyAdapter : routes commands (write, spawn, etc.)
Reviews (2): Last reviewed commit: "fix(daemon): resolve merge conflicts wit..." | Re-trigger Greptile
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughUpdates PTY event payload contracts to use shared router event types. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: ed921de2-ca14-4f6c-a7d6-cde3e165c97d
📒 Files selected for processing (2)
src/main/daemon/daemon-pty-adapter-subscription-fanout.tssrc/main/daemon/daemon-pty-router.ts
| this.unsubscribers.push( | ||
| adapter.onData((payload) => { | ||
| for (const listener of this.dataListeners) { | ||
| listener(payload) | ||
| } | ||
| }), | ||
| adapter.onExit((payload) => { | ||
| onAdapterExit(payload.id) | ||
| for (const listener of this.exitListeners) { | ||
| listener(payload) | ||
| } | ||
| }) | ||
| ) | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Iterating listener arrays live risks skipping a listener on reentrant unsubscribe.
onData/onExit unsubscribe by splicing dataListeners/exitListeners in place (Lines 49-53, 75-79). If a listener synchronously calls its own returned unsubscribe (or unsubscribes an earlier-registered listener) while the constructor's for (const listener of this.dataListeners) / for (const listener of this.exitListeners) dispatch loop is running, the array shifts and the next listener is silently skipped for that event.
🐛 Proposed fix: dispatch over a snapshot
adapter.onData((payload) => {
- for (const listener of this.dataListeners) {
+ for (const listener of [...this.dataListeners]) {
listener(payload)
}
}),
adapter.onExit((payload) => {
onAdapterExit(payload.id)
- for (const listener of this.exitListeners) {
+ for (const listener of [...this.exitListeners]) {
listener(payload)
}
})Also applies to: 72-80
Keep subscription fanout extraction and use DaemonPtyRouter data/exit event types from main.
Co-authored-by: Orca <[email protected]>
Description
Extract the daemon PTY adapter subscription fan-out into
DaemonPtyAdapterSubscriptionFanout. The router still subscribes current-first followed by legacy adapters, delegates the same subscription APIs, drains every child disposer idempotently, and preserves all routing fallback behavior.This reduces
daemon-pty-router.tsfrom 304 oxlint-counted lines to 258 without adding a suppression or changing the max-lines cap. It unblocks static analysis for the other open release PRs whose verify gate currently fails only because main's static-analysis job is red.ELI5
The router had grown just past the repository's file-size rule, so every PR inherited a lint failure from main. This moves one self-contained job—forwarding terminal event subscriptions across daemon adapters—into its own named class without changing what the router does.
Proof of fix
RED on fetched
origin/main:GREEN at
c57d3d16b6:The router is now 332 raw lines and 258 lines under oxlint's
skipBlankLines/skipCommentscount, providing 42 lines of headroom.Proof of no regression
No UI change; screenshots are not applicable.
Made with Orca 🐋