Skip to content

fix(daemon): split router subscription fanout - #11490

Merged
nwparker merged 2 commits into
mainfrom
fix/split-daemon-pty-router-max-lines
Jul 30, 2026
Merged

nwparker merged 2 commits into
mainfrom
fix/split-daemon-pty-router-max-lines

Conversation

@nwparker

@nwparker nwparker commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

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.ts from 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:

$ pnpm exec oxlint src/main/daemon/daemon-pty-router.ts
src/main/daemon/daemon-pty-router.ts:381:2: error eslint(max-lines): File has too many lines (304). help: Maximum allowed is 300.

GREEN at c57d3d16b6:

$ pnpm exec oxlint src/main/daemon/daemon-pty-router.ts
# 0 findings

The router is now 332 raw lines and 258 lines under oxlint's skipBlankLines / skipComments count, providing 42 lines of headroom.

$ pnpm exec oxlint
# 0 findings

Proof of no regression

$ pnpm exec vitest run --config config/vitest.config.ts src/main/daemon/
Test Files  77 passed | 2 skipped (79)
Tests       1164 passed | 5 skipped (1169)
$ pnpm run typecheck
# config/tsconfig.node.json, config/tsconfig.tc.cli.json, and config/tsconfig.tc.web.json passed
$ pnpm run check:max-lines-ratchet
max-lines ratchet OK — 354 grandfathered suppression(s), no new bypasses.

No UI change; screenshots are not applicable.

Made with Orca 🐋

@greptile-apps

greptile-apps Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

Greptile Summary

This PR extracts the subscription fan-out logic from DaemonPtyRouter into a new DaemonPtyAdapterSubscriptionFanout class, purely to reduce the router file's oxlint-counted line count below the repository's 300-line cap.

  • New class (daemon-pty-adapter-subscription-fanout.ts): Holds the onData/onExit listener arrays and the adapter-level subscriptions, exposes the same onData, onExit, onBackgroundStreamEvent, onWriteUnavailable, onReplay, and dispose surface that the router previously managed directly.
  • Router simplification (daemon-pty-router.ts): Replaces the inline subscription management (unsubscribers, dataListeners, exitListeners arrays and their three-site splice(0) drain) with a single this.subscriptions delegation; all three dispose paths (dispose, disposeRouterOnly, disconnectOnly) now call this.subscriptions.dispose() which is idempotent via splice(0).

Confidence Score: 5/5

Safe 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.

Important Files Changed

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.)
Loading

Reviews (2): Last reviewed commit: "fix(daemon): resolve merge conflicts wit..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1e59dc2b-928a-444c-8a1c-e0b70dce5ba5

📥 Commits

Reviewing files that changed from the base of the PR and between c57d3d1 and dee6442.

📒 Files selected for processing (2)
  • src/main/daemon/daemon-pty-adapter-subscription-fanout.ts
  • src/main/daemon/daemon-pty-router.ts

📝 Walkthrough

Walkthrough

Updates PTY event payload contracts to use shared router event types. DaemonPtyRouter now delegates adapter event registration and disposal to DaemonPtyAdapterSubscriptionFanout, removes session mappings when adapters exit, and uses the fan-out for router teardown paths.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the change and testing, but it does not follow the required template or include AI Review Report, Security Audit, and Notes sections. Rewrite the PR description using the repository template and add the missing AI Review Report, Security Audit, and Notes sections.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: extracting daemon PTY router subscription fanout into a new class.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 38e9581 and c57d3d1.

📒 Files selected for processing (2)
  • src/main/daemon/daemon-pty-adapter-subscription-fanout.ts
  • src/main/daemon/daemon-pty-router.ts

Comment on lines +30 to +43
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)
}
})
)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 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.
@nwparker
nwparker merged commit 37af457 into main Jul 30, 2026
@nwparker
nwparker deleted the fix/split-daemon-pty-router-max-lines branch July 30, 2026 07:49
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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