Skip to content

backends: split pi's openPiWorkerSession and createBrainTools, each about 600 lines #2517

Closed

Description

@schlessera

Context

The pi backend's turn path has two functions of about 600 lines each, measured with the TypeScript compiler API at dcf5341aa.

openPiWorkerSession (packages/ui-backend-pi/src/worker-session.ts:36-630) is one 595-line closure. It handles:

  • admission, sandbox setup and worker launch;
  • cleanup;
  • receipt persistence and replay;
  • turn-temp promotion;
  • the protocol reader;
  • an RPC dispatcher, dispatch (worker-session.ts:353-486, 134 lines). It is one if chain over eight methods: unlock, lock, fileObservation, syncAuth, permission, bridge, readBase, plus the fall-through application path at :473.

The closure declares 29 let bindings. Its lines are very dense (for example :117-119, :167-169), which makes the permission and receipt code hard to review.

createBrainTools (packages/ui-backend-pi/src/tools.ts:303-903) is one 601-line function. It defines 13 tool literals inline (read_file at :352 through brain_read_base at :891) together with the routing layer that sends application tools to the server.

startTurn (packages/ui-backend-pi/src/turn-runner.ts:39-272, 234 lines) is just over the epic's bound.

#1342's table names the Claude backend's startTurn (#1360) but not pi's. pi has its own worker-based turn start, which belongs under the same outcome.

Scope

  • Split worker-session.ts into:
    • worker lifecycle and launch;
    • an RPC handler table keyed by method, one handler per method;
    • receipt persistence and replay.
  • Split tools.ts into file tools, brain read tools and the application-routing layer. Each tool definition becomes a named factory.
  • Bring pi's startTurn under ~200 lines.

Out of scope

  • Validating server→worker messages and removing the transcript as any probes (the sibling pi protocol-typing issue from this review).
  • Removing the sessionFactory test seam, createPermissionGate or the legacy brain writers (the test-reality epic from this review).
  • Replacing core internal imports (backends: the pi backend still reads the brain through core's internal entry #1398).
  • Any change to tool names, schemas, results, permission decisions or the worker protocol.

Acceptance criteria

  • No function in packages/ui-backend-pi/src/worker-session.ts, tools.ts or turn-runner.ts exceeds ~200 lines (reported by a TypeScript-AST span check in the PR).
  • RPC dispatch is a table. An unknown method is still refused, proven by a direct test.
  • Each RPC handler and each tool factory has at least one direct test. One is proven by a recorded mutation of the permission handler's application-tool branch.
  • runBackendContract for pi and root tests/pi-worker-runtime.test.ts pass unchanged.

Verification

bun run test packages/ui-backend-pi
bun run test tests/pi-worker-runtime.test.ts
bun run typecheck

Contract impact

None. This is a pure internal refactor of @schlessera/brain-backend-pi. The worker protocol and the tool contracts are unchanged.

Found by a repository review on 2026-10-22.

Activity

  1. added
    type: refactor
    area: backends
    priority: p3
    agent-ready
    on Oct 21, 2026
  2. self-assigned this
    on Nov 3, 2026
  3. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Claiming for implementation (token claim-2517-04458). Branch refactor/2517-split-pi-worker-session-tools from epic/1342-turn-path-split; the PR targets that epic integration branch. Open PRs on the epic branch (#3447, #3449, #3459) touch the Claude backend and ui-server bridge; this work stays inside packages/ui-backend-pi.

  4. added this to the 0.45.0 milestone on Nov 3, 2026
  5. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Implementation is up in #3468, against the epic integration branch epic/1342-turn-path-split (base b93ced200, head 657cea7d0). It is ready, and hosted proof is pending. The issue stays open and closes when the epic batch lands on main.

    Split:

    • openPiWorkerSession: 698 → 176 lines, with launch, the RPC handler table, receipts and the protocol reader in their own modules.
    • createBrainTools: 612 → 26 lines, with one named factory per tool.
    • pi startTurn: 235 → 159 lines.

    Proven locally:

    • The tool roster is byte-identical against a fixture recorded before the split.
    • An unknown RPC method is refused, with a direct test.
    • Every RPC handler and every tool factory has a direct test.
    • A mutation of the permission handler's application-tool branch makes its test fail.

    Hosted CI only: tests/pi-worker-runtime.test.ts and the pi provider-history keyless guard cannot run on the local host, which has no user namespaces. Both fail identically on the untouched base there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: backendspriority: p3type: refactor

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions