Repository navigation
Open
Labels
agent-readyContext and acceptance criteria are complete enough for an agent to take this unattended.area: backendsAgentBackend implementations and the seam they share.priority: p3Someday. Needs a driving use case before it starts.type: refactorStructure changes; behaviour does not. No changeset-visible effect.
Description
Context
The pi worker pipe is validated in one direction only.
- Worker → server is checked. Frames are parsed through a strict zod union (
piWorkerMessage,packages/ui-backend-pi/src/worker-protocol.ts:64). - Server → worker is not. The worker reads every frame as
raw as any(packages/ui-backend-pi/src/worker-entry.ts:225). This coversinit,answer,thinking,abort,promptandfollowUp.Initis a hand-written interface (worker-entry.ts:19-36).- The server builds
initas an untyped object literal and sends it throughsend(value: unknown)(packages/ui-backend-pi/src/worker-session.ts:117-119,:259). - Nothing ties the two sides together at compile time. A field renamed on one side reaches the worker as
undefined, and nothing fails.
The same file also reads pi's saved session transcript through untyped casts. The resume probes that decide whether a tool call was already made or answered use (entry.message as any) at worker-session.ts:280 and :296-299. :408 casts msg.toolName as any to test it against BRAIN_APPLICATION_TOOLS. An upstream transcript shape change would make those probes silently return false. #1344 targets exactly this kind of unchecked boundary read.
Reproduce:
grep -n "raw as any" packages/ui-backend-pi/src/worker-entry.ts
grep -n "as any" packages/ui-backend-pi/src/worker-session.tsScope
- Add a
serverToWorkerMessagezod discriminated union and anInitschema toworker-protocol.ts. The worker parses every inbound frame with it and refuses a malformed frame with a named error. - Type the server's
sendwith the inferred union, so the compiler checksinitand the other frames. - Replace the transcript probes with a typed helper (for example
isAssistantToolCall(entry, id)/isToolResultFor(entry, id)) that narrows with a schema or type guard. Replace thetoolName as anywith a typed membership check.
Out of scope
- Splitting
openPiWorkerSession(the sibling pi split issue in this review). - Changing any frame's fields or semantics, or the pipe framing and size limits (
PI_PIPE_MAX_BYTES). - The Claude backend's transcript reads (
packages/ui-backend-claude/src/history.ts), which Epic: rows and transcripts that cross a storage or SDK boundary are parsed, not asserted #1344 tracks separately.
Acceptance criteria
-
grep -n "as any" packages/ui-backend-pi/src/worker-entry.ts packages/ui-backend-pi/src/worker-session.tsreturns nothing. - A test sends a malformed
init(missingsessionId) to the worker entry and asserts a named refusal. It fails on the tree before the change, where the worker proceeds withundefined. - A test feeds the resume probe a transcript entry with an unexpected
messageshape and asserts a typed result rather than a thrownTypeErroror a silentfalse. - Removing a field from the server's
initliteral failstsc(recorded in the PR). - Root
tests/pi-worker-runtime.test.tsand the pi package suite pass.
Verification
bun run test packages/ui-backend-pi
bun run test tests/pi-worker-runtime.test.ts
bun run typecheckContract impact
None. The worker pipe is internal to @schlessera/brain-backend-pi. A malformed frame from a mismatched server is now refused instead of misread, so a patch changeset is needed.
Found by a repository review on 2026-10-22.
Metadata
Metadata
Assignees
Labels
agent-readyContext and acceptance criteria are complete enough for an agent to take this unattended.area: backendsAgentBackend implementations and the seam they share.priority: p3Someday. Needs a driving use case before it starts.type: refactorStructure changes; behaviour does not. No changeset-visible effect.
Activity