Skip to content

Epic: the turn path's largest functions are split into units that can be reviewed and tested alone #1342

Closed
9 of 9 issues completed

Description

@schlessera

Outcome

No function on the chat turn path (client message dispatch, retained session run, backend turn start, client chat store, WebSocket client) exceeds roughly 200 lines, and each extracted unit has direct tests rather than being reachable only through the whole closure.

Why now

Found by the 2026-10-09 code-quality review of main @ 0c69e0cb. The turn path is where concurrency bugs live and where most open fixes land (#910, #1328, #1032). Today each change there means reviewing one of these:

Current named implementation declarations at inspected main 35217d283b849b5ef81700d5d4c2ec7486405d12 (inclusive AST ranges, overload signatures excluded):

Function Lines Location
handleClientMessage 30 packages/ui-server/src/ws/dispatch.ts:16-45
runRetainedSession 73 packages/ui-server/src/ws/run-session.ts:96-168
startTurn 90 packages/ui-backend-claude/src/turn-runner.ts:121-210
createChatStore 15 packages/ui-react/src/stores/chat-state.ts:712-726
createWebSocketClient 167 packages/ui-react/src/connection.ts:29-195
makeBridge 44 packages/ui-server/src/ws/bridge.ts:34-77
openPiWorkerSession 176 packages/ui-backend-pi/src/worker-session.ts:58-233
createBrainTools 26 packages/ui-backend-pi/src/tools.ts:221-246
Pi startTurn 6 packages/ui-backend-pi/src/turn-runner.ts:43-48
createPermissionWiring 47 packages/ui-backend-claude/src/permission-hooks.ts:626-672
assembleClaudeSdkTurn 135 packages/ui-backend-claude/src/sdk-options.ts:383-517

Every native child is now closed, including #2516 (makeBridge), whose PR #3449 merged into the epic integration branch. The current main implementation is below the size bound; the recorded source measurement alone is not fresh runtime proof. The short Pi startTurn wrapper delegates to its inner runTurn; wrapper size alone is not evidence that all extracted units have direct tests or that the complete runtime/browser definition of done holds.

Shared context

  • The repo already shows the target pattern: packages/ui-react/src/websocket-handlers/ splits inbound frame handling per kind.
  • Stores are per-root vanilla zustand stores built in createBrainStores (packages/ui-react/src/stores/create-stores.ts:21-47). Slices must keep that per-root construction.
  • The wire protocol is validated on every inbound frame (packages/ui-sdk/src/schemas.ts); the split must not move validation later.
  • Real-runtime tests exist for all of these paths (ui-server mounts the real app in 41 test files). They are the behavioural guard.

Constraints and non-goals

  • Pure refactor: no protocol, HTTP or store-hook signature change. useRootStore selectors keep working.
  • No new state library, no framework change (ROADMAP "Deliberately not doing").
  • Do not combine with the open behavioural fixes; land refactors separately so a regression bisects to one change.

Breakdown

Native children #1358–#1362 cover the original five functions. The later review adds #2516 (makeBridge), #2517 (Pi worker/tool construction and turn start), and #2519 (Claude permission/SDK assembly). #2515 owns the prerequisite coordinator encapsulation; retain each child’s explicit dependencies instead of treating parent membership as a blocker. These later functions are part of the same turn-path size/testability outcome. createApp remains standalone #2514 because it is outside this turn path.

Definition of done

Each listed function is under ~200 lines, each extracted unit has at least one direct test, and the existing runtime/browser suites pass unchanged.

2026-10-11 citation reconciliation at main 62085604026ed3849aa818081185f17c9b0808ea: refreshed startTurn's source range after completed-response checkpoint accounting. Refactor boundaries and behavior-preservation requirements are unchanged; no implementation or readiness is claimed.

2026-10-13 source reconciliation: refreshed complete named function ranges after governed-flow integration; refactor boundaries and behavior-preservation requirements remain unchanged.

2026-10-14 source review at main 4af7b2f20a43d23260c2ee5e3bac3896aee971bd: refreshed runRetainedSession to its complete current declaration/body range (178–766, 589 lines) and checked the other table ranges. The refactor boundaries and native child prerequisites are unchanged; no runtime or refactor-completion result is claimed.

2026-10-14 source reconciliation at main 5cf1d548f7bd3a314eb36394dc5db0268db97da6: refreshed the active table after #1832. runRetainedSession spans its complete declaration/body at 178–769 (592 lines). The older dated range is historical; the other four named function ranges and refactor scope are unchanged. No refactor completion or new runtime proof is claimed.

2026-10-16 source review at main 603a754b9e229f9b96c1aaf2b18917374b6483b0: verified the complete declaration/body boundaries and refreshed the active function table after subsequent turn-path changes. Historical size receipts retain their original source identities. The refactor scope, behavior-preservation criteria and native child prerequisites are unchanged; source inspection supplies no refactor completion or new runtime proof.

2026-10-20 triage: parsed the current named declarations with the installed TypeScript compiler API and refreshed the active complete inclusive ranges/counts and per-root store factory pointer. Historical size receipts keep their original SHA. Native children, refactor boundaries, priority and absence of epic readiness are unchanged; no runtime execution or refactor completion is claimed.

2026-10-22 triage: folded the later repository-review breakdown into the active function table and scope after verifying all eleven complete declaration/body ranges with the TypeScript parser. The native list already contains the added children; no new attachment or completed refactor is claimed.

2026-10-24 continued triage at main dc5d3c73282c29a3f0098306d398743ee18ec791: Parsed all eleven named function bodies with the installed TypeScript 6.0.3 compiler API and refreshed complete inclusive ranges/counts after current turn-path changes. Refactor boundaries, direct-test requirements and child dependencies are unchanged; this is source measurement, not refactor delivery or runtime proof.

2026-10-24 triage at main af6300478a07379178bd30221a31c0b917df2283: Recomputed all eleven implementation-body ranges with the TypeScript AST at this main. The retained-session, Claude turn-runner and SDK-options body sizes changed; overload signatures are excluded. The existing refactor boundaries and epic status remain.

2026-10-24 triage at main 77f35ef1ba34e4fab7092ada5362ae799852ea24: Recomputed all eleven complete named function bodies with the TypeScript AST. Updated retained-session, bridge, Pi tool-factory and Claude SDK-assembly boundaries after reference tools; existing refactor scope and epic-not-ready status remain.

2026-10-25 triage at main 827861e1fa71cd4a26b52f1ef0f96b849381acdf: Verified moved current-source anchors and refreshed navigation; historical receipts and unresolved prerequisites retain their identities. No new runtime proof is claimed.

2026-10-25 triage at main ab746a434c4ada11734ffea4e208d1d6211f1aca: Refreshed every changed function range and inclusive line count against the current TypeScript declaration/body nodes; no refactoring or readiness change is performed.

2026-10-25 triage at main 541b8d98328a760290ac6dd4a7f065af76153408: Verified the changed dispatch/session-run declaration/body ranges and inclusive line counts with the TypeScript syntax tree. Only the source map changes; no refactor or readiness is performed.

2026-10-25 triage at main dcd3b336dbade5e29e8a131eeeb5a0d9cffe60ff: Recomputed changed backend function ranges and inclusive line counts from the current TypeScript declaration/body nodes after the lifecycle fixes. No refactor or readiness is performed.

2026-10-25 triage at main c670449d3bf1b0639e50ce554d728961f4313add: Recomputed the changed dispatch and bridge declaration/body boundaries and inclusive line counts using the installed TypeScript syntax tree. This is source measurement, not refactor delivery or runtime proof.

2026-10-29 source measurement at main cb5db6e901e9d3d312bd3a125e03be6140faf98f: recomputed all eleven implementation declaration/body ranges with the installed TypeScript AST. Updated changed dispatch, WebSocket client, bridge and Pi tool-factory counts; no refactor, runtime proof or epic readiness is claimed.

2026-10-29 triage at main 36074ef4c5d14bb41108a26160a8ab8bbe1d4e87: verified moved named source anchors and refreshed current navigation. Historical receipts, scope, unresolved prerequisites and native membership remain unchanged; no new runtime proof is claimed.

2026-11-02 source measurement at main a84d9bfbcb4e608b504aa0a9feb1144ad9a0b77e: recomputed the named function declaration/body ranges with installed TypeScript 6.0.3, including Pi’s returned startTurn function expression. Refreshed changed inclusive counts/ranges in the active table; historic measurements retain their SHA. The direct-test and behavior-preserving refactor requirements, native ownership and absence of epic readiness remain. This is source measurement, not a refactor or runtime result.

2026-11-04 triage at main 53b862123d7b8e4c9d039859fabdc9fc6ad4cd36: Recomputed all eleven named implementation declarations with the installed TypeScript AST and reconciled native child closure; direct-test and complete-runtime requirements remain. No new runtime qualification or implementation is claimed.

2026-11-05 triage at main 35217d283b849b5ef81700d5d4c2ec7486405d12: recomputed all eleven complete implementation ranges with the installed TypeScript AST, reconciled #2516 closure and verified current native membership. All eleven listed functions meet the size bound. This is a closure candidate subject to the unchanged direct-test and complete-runtime definition of done; no suite rerun, epic closure or readiness is claimed.

Sub-issues

Sub-issues

9 of 9 Issues completed
Loading 9 sub-issues

Activity

  1. added
    type: refactor
    area: ui-server
    area: ui-react
    area: backends
    priority: p2
    epic
    on Oct 9, 2026
  2. schlessera commented on Oct 9, 2026

    @schlessera
    OwnerAuthor

    Planned order (2026-10-09): #1358 (dispatch table) can start now. #1359 waits on #1364. #1361 and #1362 wait on #1380 (layering fix touching chat-state.ts and connection.ts). #1360 waits on #1037. Land each refactor separately from behavioural fixes on the same files (#910, #1328).

  3. schlessera commented on Oct 13, 2026

    @schlessera
    OwnerAuthor

    2026-10-13 triage: verified complete current named function ranges with the TypeScript parser on 857ab01d1; refreshed the review table without changing its refactor outcome, priority or child prerequisites.

  4. schlessera commented on Oct 14, 2026

    @schlessera
    OwnerAuthor

    2026-10-14 source review at main 4af7b2f20a43d23260c2ee5e3bac3896aee971bd: refreshed runRetainedSession to its complete current declaration/body range (178–766, 589 lines) and checked the other table ranges. The refactor boundaries and native child prerequisites are unchanged; no runtime or refactor-completion result is claimed.

  5. schlessera commented on Oct 14, 2026

    @schlessera
    OwnerAuthor

    2026-10-14 citation review at main 5cf1d548f: #1832 adds three lines to runRetainedSession. Updated the active function table to 178–769 (592 lines); preserved historical review receipts and all refactor prerequisites. No readiness or runtime-completion change.

  6. 9 remaining items

  7. schlessera commented on Oct 24, 2026

    @schlessera
    OwnerAuthor

    2026-10-24 triage at main af6300478a07379178bd30221a31c0b917df2283: Recomputed all eleven implementation-body ranges with the TypeScript AST at this main. The retained-session, Claude turn-runner and SDK-options body sizes changed; overload signatures are excluded. The existing refactor boundaries and epic status remain.

  8. schlessera commented on Oct 24, 2026

    @schlessera
    OwnerAuthor

    2026-10-24 triage at main 77f35ef1ba34e4fab7092ada5362ae799852ea24: Recomputed all eleven complete named function bodies with the TypeScript AST. Updated retained-session, bridge, Pi tool-factory and Claude SDK-assembly boundaries after reference tools; existing refactor scope and epic-not-ready status remain.

  9. schlessera commented on Oct 25, 2026

    @schlessera
    OwnerAuthor

    2026-10-25 triage at main 827861e1fa71cd4a26b52f1ef0f96b849381acdf: Verified moved current-source anchors and refreshed navigation; historical receipts and unresolved prerequisites retain their identities. No new runtime proof is claimed.

  10. schlessera commented on Oct 25, 2026

    @schlessera
    OwnerAuthor

    2026-10-25 triage at main ab746a434c4ada11734ffea4e208d1d6211f1aca: Refreshed every changed function range and inclusive line count against the current TypeScript declaration/body nodes; no refactoring or readiness change is performed.

  11. schlessera commented on Oct 25, 2026

    @schlessera
    OwnerAuthor

    2026-10-25 triage at main 541b8d98328a760290ac6dd4a7f065af76153408: Verified the changed dispatch/session-run declaration/body ranges and inclusive line counts with the TypeScript syntax tree. Only the source map changes; no refactor or readiness is performed.

  12. schlessera commented on Oct 25, 2026

    @schlessera
    OwnerAuthor

    2026-10-25 triage at main dcd3b336dbade5e29e8a131eeeb5a0d9cffe60ff: Recomputed changed backend function ranges and inclusive line counts from the current TypeScript declaration/body nodes after the lifecycle fixes. No refactor or readiness is performed.

  13. schlessera commented on Oct 25, 2026

    @schlessera
    OwnerAuthor

    2026-10-25 triage at main c670449d3bf1b0639e50ce554d728961f4313add: Recomputed the changed dispatch and bridge declaration/body boundaries and inclusive line counts using the installed TypeScript syntax tree. This is source measurement, not refactor delivery or runtime proof.

  14. added this to the 0.45.0 milestone on Nov 2, 2026
  15. added a commit that references this issue on Nov 3, 2026
  16. schlessera commented on Nov 4, 2026

    @schlessera
    OwnerAuthor

    Epic state — 2026-11-04 (inspected main 53b862123d7b8e4c9d039859fabdc9fc6ad4cd36)

    Eight native children are closed; #2516 (makeBridge) is the remaining open child. Recomputed the eleven named implementation declaration/body ranges with the installed TypeScript AST: ten are below roughly 200 lines, while makeBridge remains 746 lines. Wrapper size alone does not prove every extracted unit has direct tests; the full existing runtime/browser definition of done still applies. The active source table is updated without a new runtime qualification claim.

    The epic remains outside agent-ready; work continues through its native children.

  17. added 3 commits that reference this issue on Nov 4, 2026
  18. schlessera commented on Nov 5, 2026

    @schlessera
    OwnerAuthor

    Epic state — 2026-11-05 triage. All nine native children are closed. The eleven named implementation functions measured with the TypeScript AST at inspected main 35217d283b849b5ef81700d5d4c2ec7486405d12 are below roughly 200 lines; makeBridge is 44 lines. The body now reflects that source and child state. Reported as a closure candidate: direct-test and complete-runtime criteria still apply, and this pass reran no suite or closed the epic.

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

    area: backendsarea: ui-reactarea: ui-serverepicpriority: p2type: refactor

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions