Skip to content

ui-server: split runRetainedSession into lifecycle stages #1359

Closed

Description

@schlessera

Part of #1342.

Blocked by #2515

Context

runRetainedSession was 791 lines (packages/ui-server/src/ws/run-session.ts:196-986 at epic-branch 2b9a69c60), mixing setup, admission, the event pump, send ordering, failure classification and teardown. It declares many mutable let flags (budgetFailure, journalFailure, scopeStopReason, cancelledAdmission, revokedAdmission, …) that together encode the turn outcome and are resolved by one nested ternary. The budget_warning/budget_block governance-event payload is built by hand at four sites with an inconsistent actor.

Sequencing: #2515 lands first. It makes coordinator state private (this file holds 6 direct mutation sites plus coordinator.startingSessions += 1) and removes the host → conversation → run-session → host value import cycle, so the stages extracted here call intent methods. Send order is already per-host state (nextSendOrder, packages/ui-server/src/ws/host.ts:280, delivered by #1364).

Scope

  • Extract setup, admission (governed hold, budget, journal), event pumping, send ordering, failure classification and teardown into named functions with explicit inputs. Admission and failure classification return a discriminated outcome instead of mutable flags.
  • One helper builds the budget governance-event payload.

Out of scope

Acceptance criteria

  • The function is under ~150 lines; each stage has a direct test.
  • Session runtime tests, including reconnect and abort, pass unchanged.

Verification

bun run test packages/ui-server

Contract impact

None.

2026-10-24: replaced the #1364 blocker (closed; send order is per-host) with #2515, and folded the 2026-10-22 source check (current range, outcome flags, budget-event duplication, coordinator sequencing) into Context and Scope.

2026-11-03: citation check against epic/1342-turn-path-split at 2b9a69c60 (TypeScript AST). The function spanned run-session.ts:196-986, 791 lines, not :196-898. The four hand-built budget-event sites were at :539, :616, :623 and :870. #2515 is delivered on that branch (#3289). Scope and acceptance criteria are unchanged.

Activity

  1. added
    type: refactor
    area: ui-server
    priority: p3
    agent-ready
    on Oct 9, 2026
  2. added
    blocked
    on Oct 9, 2026
  3. 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:

    The Blocked by #1364 line still stands: #1364 is open.

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

    @schlessera
    OwnerAuthor

    Claiming this for implementation (claim token claim-1359-1793667878-4055).

    Branch: refactor/1359-split-retained-session-stages, started from epic/1342-turn-path-split at 2b9a69c60 under the maintainer's epic-branch ruling. #2515 is delivered on that branch (#3289), so Blocked by #2515 and blocked stay until the epic lands on main. The PR targets the epic branch.

    Collisions (bun scripts/collisions.ts --paths packages/ui-server/src/ws/run-session.ts): open PRs against main also change this file. They are #3342, #3386, #3404 and #3407. Everything else showed up only as test-closure overlap. Whichever change lands second will need a rebase.

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

    @schlessera
    OwnerAuthor

    Implementation is in #3415 (ready). It targets epic/1342-turn-path-split at base 2b9a69c60; head 01cc8a891. Hosted proof run: 41571716330.

    runRetainedSession drops from 791 lines to 73. Its stages live in packages/ui-server/src/ws/session-run/, and each has a direct test. Admission and failure classification return discriminated outcomes. One budgetEvent() helper serves all four budget-event sites, each keeping its current values. Local evidence: check:pr passes, mutation receipts are in the PR, and the full ui-server comparison against the base shows no persistent new failures. Worker-boundary tests and the provider-history guard can't run on the local host, so they rely on hosted CI.

    This issue stays open with Blocked by #2515 and blocked until the epic batch lands on main. The assignment stays while the PR is live.

  8. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

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

  9. 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: ui-serverblockedpriority: p3type: refactor

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions