Skip to content

ui-server: replace the 41-case handleClientMessage switch with a per-message handler table #1358

Closed

Description

@schlessera

Part of #1342.

Blocked by #2515

Context

handleClientMessage is 590 lines (packages/ui-server/src/ws/dispatch.ts:88-677) with 42 case labels, one per client message kind, over shared closure state. The largest is tool_approval (about 100 lines).

Sequencing: #2515 lands first. It makes the TurnCoordinator collections private behind intent methods (settleAsk, settleApproval, settleLocation, …), so the handlers extracted here call those methods instead of each carrying raw map mutations. dispatch.ts holds 6 of the 30 direct mutation sites today.

Scope

  • A Record<ClientMessage["type"], Handler> table; each handler in its own module under packages/ui-server/src/ws/handlers/ with an explicit context argument.
  • Exhaustiveness checked by the type (a missing kind fails tsc).

Out of scope

  • Protocol changes; ordering or concurrency changes.
  • The wrong logged reason for a refused "always allow" policy grant (the rememberability rule is inlined in dispatch.ts and ws/bridge.ts). It is a behaviour fix, not part of this refactor.

Acceptance criteria

  • handleClientMessage is under ~60 lines.
  • Removing one handler entry fails typecheck (recorded in the PR).
  • Each handler has a direct test; existing ws runtime tests pass unchanged.

Verification

bun run test packages/ui-server
bun run typecheck

Contract impact

None.

2026-10-24: declared #2515 as the blocker (the coordinator encapsulation lands first, per the 2026-10-22 source check), folded that check's current range and case count into Context, and kept the policy-grant log reason out of scope.

Activity

  1. added
    type: refactor
    area: ui-server
    priority: p2
    agent-ready
    on Oct 9, 2026
  2. schlessera commented on Oct 21, 2026

    @schlessera
    OwnerAuthor

    Source check at dcf5341aa during the 2026-10-22 repository review (measured with the TypeScript compiler API). The citation in this body is out of date:

    • handleClientMessage is now 590 lines, packages/ui-server/src/ws/dispatch.ts:88-677. The body says 560 lines at :88-647; Epic: the turn path's largest functions are split into units that can be reviewed and tested alone #1342's table already has the current range.
    • The switch has 42 case labels, not 41. The largest is tool_approval (dispatch.ts:386-483, about 100 lines).
    • dispatch.ts:342-382 repeats the same get → match turn → delete → resolve/reject sequence four times, for location_response, location_error, mask_response and mask_error.

    New evidence relevant to sequencing: dispatch.ts is one of six files that mutate TurnCoordinator collections directly (6 of 30 sites). #2515 (under #1342) makes those collections private behind intent methods (settleAsk, settleApproval, settleLocation, …). If that lands first, the per-message handlers extracted here call intent methods instead of each carrying copies of the map invariants. Otherwise the handler split moves the raw mutations into a dozen new modules.

    The "always allow" rememberability rule is also inlined both here (dispatch.ts:438-467) and in ws/bridge.ts:304-305. The write-side reason ternary does not handle kind: "policy", so a refused policy grant is logged with the wrong reason. That is a behaviour fix and should stay out of this refactor, per the epic's constraint.

  3. added
    blocked
    on Oct 24, 2026
  4. self-assigned this
    on Nov 2, 2026
  5. added this to the 0.45.0 milestone on Nov 2, 2026
  6. schlessera commented on Nov 2, 2026

    @schlessera
    OwnerAuthor

    Claimed (token wi-1358-7d00ca1e) under the maintainer's epic-branch ruling: #2515 was delivered to epic/1342-turn-path-split by #3289. Blocked by #2515 and blocked stay until the epic lands on main. Branch refactor/1358-dispatch-handler-table from the epic tip 0c3b83762. PR #3305 targets the epic branch and closes this issue when the epic batch lands on main. Collisions: closure-only, no path overlap.

  7. schlessera commented on Nov 2, 2026

    @schlessera
    OwnerAuthor

    Delivered to the epic integration branch epic/1342-turn-path-split as 45fe0ecd2 via #3305, after ready-candidate proof passed against the exact epic base. This issue closes when the epic batch lands on main.

  8. removed their assignment
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: ui-serverblockedpriority: p2type: refactor

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions