Repository navigation
Description
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 oneifchain 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.tsinto:- worker lifecycle and launch;
- an RPC handler table keyed by
method, one handler per method; - receipt persistence and replay.
- Split
tools.tsinto file tools, brain read tools and the application-routing layer. Each tool definition becomes a named factory. - Bring pi's
startTurnunder ~200 lines.
Out of scope
- Validating server→worker messages and removing the transcript
as anyprobes (the sibling pi protocol-typing issue from this review). - Removing the
sessionFactorytest seam,createPermissionGateor 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.tsorturn-runner.tsexceeds ~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
permissionhandler's application-tool branch. -
runBackendContractfor pi and roottests/pi-worker-runtime.test.tspass unchanged.
Verification
bun run test packages/ui-backend-pi
bun run test tests/pi-worker-runtime.test.ts
bun run typecheckContract 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
schlessera commentedon Nov 3, 2026
Claiming for implementation (token
claim-2517-04458). Branchrefactor/2517-split-pi-worker-session-toolsfromepic/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 insidepackages/ui-backend-pi.schlessera commentedon Nov 3, 2026
Implementation is up in #3468, against the epic integration branch
epic/1342-turn-path-split(baseb93ced200, head657cea7d0). 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.startTurn: 235 → 159 lines.Proven locally:
Hosted CI only:
tests/pi-worker-runtime.test.tsand 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.refactor(backend-pi): split pi's openPiWorkerSession, createBrainTool…