Skip to content

backends: split the Claude backend's startTurn #1360

Closed

Description

@schlessera

Part of #1342.

Context

Delivered prerequisite outputs: #1037. Their output references remain relevant, but they are no longer active blockers.

startTurn is about 670 lines (packages/ui-backend-claude/src/turn-runner.ts:119-788). #1037 has landed, so the function now runs the Claude runtime in the isolated worker. Its closure declares about 30 let bindings (sawResult, refused, withheld, terminal, terminalSent, the hostRelay* group, workerFailure, finishWorker, …) shared by nested closures such as emitTerminal, releaseTerminal, unsaved and limitFailure. The subscription-gate generator is already a module-level function (gatedPrompt, same file:821), called from startTurn at :471.

Scope

Split along the boundaries that exist today:

  • a TurnTerminal object that owns the terminal flags and their emit/release rules;
  • a host inference relay starter that owns the hostRelay* state;
  • the stream loop and result reporting, each taking explicit inputs.

Out of scope

Acceptance criteria

  • No function in turn-runner.ts exceeds ~150 lines.
  • runBackendContract passes for the Claude backend unchanged.

Verification

bun run test packages/ui-backend-claude

Contract impact

None.

2026-10-24: replaced the pre-worker phase decomposition with the current terminal-state, relay and stream boundaries from the 2026-10-22 source check, refreshed the startTurn range, and left permission/SDK assembly to #2519.

Activity

  1. added
    type: refactor
    area: backends
    priority: p3
    agent-ready
    blocked
    on Oct 9, 2026
  2. schlessera commented on Oct 10, 2026

    @schlessera
    OwnerAuthor

    Unblocked: #1037 is closed, so scripts/sync-project.ts is removing the blocked label.

  3. removed
    blocked
    on Oct 10, 2026
  4. 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:

    • startTurn is now 633 lines, packages/ui-backend-claude/src/turn-runner.ts:102-734. The body says about 460 lines at :90-550; Epic: the turn path's largest functions are split into units that can be reviewed and tested alone #1342's table has the current range.
    • The closure declares 30 let bindings, including sawResult, refused, withheld, terminal, terminalSent, the hostRelay* group, workerFailure and finishWorker. Nested closures share them: emitTerminal, releaseTerminal, unsaved, limitFailure and the gated-prompt check. Phases that already exist could be extracted:
      • a TurnTerminal object that owns the terminal flags and their emit rules;
      • a host inference relay starter;
      • the subscription-gate callback (gatedPrompt, about 120 lines).
    • Churn since the package was created: turn-runner.ts 30 commits, against 48 for sdk-options.ts and 43 for permission-hooks.ts.

    The review also found two more oversized functions in the same backend that this issue does not cover: createPermissionWiring (permission-hooks.ts:42-590, 549 lines) and assembleClaudeSdkTurn (sdk-options.ts:143-482, 340 lines). They are filed as #2519 under #1342, so this one can stay scoped to turn-runner.ts.

  5. self-assigned this
    on Nov 3, 2026
  6. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Claiming for implementation (claim token claim-1360-696954). Branch refactor/1360-split-claude-start-turn from epic/1342-turn-path-split @ 2b9a69c60; the PR will target that epic integration branch.

    Collision report for packages/ui-backend-claude/src/turn-runner.ts: path collision with draft #3371 (epic #3234 batch, edits turn-runner.ts); the rest are closure-only overlaps.

  7. added this to the 0.45.0 milestone on Nov 3, 2026
  8. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Implementation is in #3447 (ready), against the epic integration branch epic/1342-turn-path-split at 2efaf6c23, head dd3396e6f. This issue closes when the epic batch lands on main. The largest function in turn-runner.ts is now 85 lines (was 712). On this host the full Claude package suite fails the same way on base and branch: the same 49 real-worker/sandbox failures. The provider-history guard and the host-relay route tests can't run here, so hosted CI is their proof. Milestone 0.45.0.

  9. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Delivered to epic/1342-turn-path-split as b93ced200 via #3447, after ready-candidate proof passed on the exact epic base. This issue closes when the epic batch lands on main.

  10. removed their assignment
    on Nov 3, 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: backendspriority: p3type: refactor

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions