Skip to content

backends: pi's server-to-worker messages and transcript resume probes are read through any #2518

Open

Description

@schlessera

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 covers init, answer, thinking, abort, prompt and followUp.
    • Init is a hand-written interface (worker-entry.ts:19-36).
    • The server builds init as an untyped object literal and sends it through send(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.ts

Scope

  • Add a serverToWorkerMessage zod discriminated union and an Init schema to worker-protocol.ts. The worker parses every inbound frame with it and refuses a malformed frame with a named error.
  • Type the server's send with the inferred union, so the compiler checks init and 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 the toolName as any with a typed membership check.

Out of scope

Acceptance criteria

  • grep -n "as any" packages/ui-backend-pi/src/worker-entry.ts packages/ui-backend-pi/src/worker-session.ts returns nothing.
  • A test sends a malformed init (missing sessionId) to the worker entry and asserts a named refusal. It fails on the tree before the change, where the worker proceeds with undefined.
  • A test feeds the resume probe a transcript entry with an unexpected message shape and asserts a typed result rather than a thrown TypeError or a silent false.
  • Removing a field from the server's init literal fails tsc (recorded in the PR).
  • Root tests/pi-worker-runtime.test.ts and the pi package suite pass.

Verification

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

Contract 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.

Activity

  1. added
    type: refactor
    area: backends
    priority: p3
    agent-ready
    on Oct 21, 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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions