Skip to content

refactor(rpc): type outgoing params with a send-side shape, not the parsed one - #20028

Closed
Jinwoo-H wants to merge 3 commits into
rpc-step2c-operationfrom
rpc-step2e-send-types
Closed

Jinwoo-H wants to merge 3 commits into
rpc-step2c-operationfrom
rpc-step2e-send-types

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​6 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​5
Prod 6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​121 $\color{#cf222e}{\Huge{\mathbf{−}}}$​11 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​110

Stacked on #20018. Base is rpc-step2c-operation, so the diff here is only the send-side type. Independent of #20026, which is also stacked on #20018.

The bug this fixes

The descriptor typed outgoing params with RpcParams<M>, which is z.output — the shape the handler receives after parsing. Wrong for a sender in both directions: a field with .default(x) is required in the output type, so a sender that legitimately omits it fails to typecheck.

z.input is not the fix either, and the generated catalog already says why: requiredString is z.unknown().transform(...), so its input admits anything. repo.setBaseRef under z.input is { repo: unknown; ref: unknown } — no safety at all.

Approach: derive, don't declare

RpcSendParams<M> walks the schema and takes each of zod's two channels where it is honest:

  • key optionality from zod's own optin marker — the z.input rule, the one that understands .default and .optional
  • value types from z.output — the post-coercion contract the builders declare in their pipe target

Leaves fall back to z.output, i.e. today's behaviour.

Rejected (a), teaching the builders an input type: only 9 of 113 .unknown() sites in src/shared/rpc-contract/ live in rpc-param-primitives.ts. The other ~104 inline the z.unknown().transform().pipe() pattern directly — 11 in worktree-params.ts alone — so builder annotations would leave all of those fields at unknown.

Rejected (b), a parallel generated catalog: needs a runtime zod→TS-source serializer for 611 entries, far more surface to drift, same result.

Resolved types

repo.setBaseRef   { repo: string; ref: string; }
files.searchPaths { worktree: string; query?: string; limit?: number; excludePaths?: string[]; mode?: "quick-open" }
worktree.ps       { limit?: number; afterSnapshotId?: string | null; supportsWorktreeVisibilitySourceDefaults?: true }
ui.get            void
  • repo.setBaseRef — sender must pass real strings (z.input gave unknown)
  • files.searchPaths — sender may now omit query/limit; z.output had both required
  • worktree.ps — unchanged, still narrow
  • ui.get (params: null) — void, unchanged

Safety

A catalog-wide compile fence proves RpcParams<M> extends RpcSendParams<M> for all 611 methods — the send type never tightens the parsed shape, so nothing a sender could legally build before becomes illegal.

Across all 611, exactly one method degenerates to unknown: plugins.panelAction, whose schema literally is z.unknown().

Blast radius is small and one-directional: every changed method changes by letting a sender omit a key the host already defaults.

Mutations

Mutation Result
fall back to z.output TS2739: Type '{ worktree: string; }' is missing … query, limit
fall back to z.input 3 failures — two TS2578 Unused '@ts-expect-error' (the worktree: 42 and limit: 'ten' rejections go dead) and a TS2322 widening
client-side .parse on a params schema type-only boundary test fails

The second mutation is the interesting one: falling back to z.input doesn't just lose precision, it silently kills two pre-existing fences that were catching wrong-typed fields.

Gates

pnpm tc · typecheck:node|cli|web · mobile typecheck · mobile test 530 files / 4,372 passed · vitest run src/main/runtime/rpc 276 files / 2,379 passed · verify:rpc-params-catalog · check:code-quality:changed 0 new findings across 13 files · max-lines ratchet · oxfmt --check · oxlint 0.

Type-level only. No runtime change, no wire change, no product call site migrated.

…rpretation

An operation family declares its method, compatible reader, acceptance policy and
interpretation barrier once. The send classifies only a fulfilled envelope; transport
rejection stays on the promise channel as the original error object, so the cutover and
delivery-unknown predicates keep working and a Promise.all group still fails fast.
Multi-request families go through a post-barrier combinator that awaits every raw request
and then interprets in declared order.

No production call site is migrated: this lands as self-contained machinery so runtime
behaviour is provably untouched.

Claude-Session: https://claude.ai/code/session_01JNnE9qzUZMMnqpZWCqM3nb
…arsed one

The RpcOperation descriptor typed its params with RpcParams, which is
z.output — the shape the handler receives after parsing. A field with
.default(x) is required there, so a sender that legitimately omits it failed
to typecheck. z.input is not the fix: the params builders parse with
z.unknown() so a hostile client cannot crash the dispatcher, which collapses
every requiredString/OptionalString field to `unknown`.

RpcSendParams takes each channel where it is honest — key optionality from
zod's own `optin` marker, value types from z.output — by walking the schema
from the generated catalog, so it cannot drift from the dispatcher. 21 of 611
methods change, all by letting a sender omit a key the host already defaults.

Type-level only: no runtime code, no wire change, and the params contract
stays a type-only import so no client can parse a coercing schema.

Claude-Session: https://claude.ai/code/session_01JNnE9qzUZMMnqpZWCqM3nb

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ One latent correctness nit in the new RpcSendInput type — otherwise a clean, well-fenced, type-level refactor.

Reviewed changes

  • RpcSendParams / RpcSendInput (src/shared/rpc-contract/rpc-send-params.ts) — a new derived send-side params type that takes key optionality from zod's _zod.optin marker and value types from z.output.
  • Mobile send signatures (mobile/src/transport/rpc-operation.ts) — runRpcOperation, captureRpcOperationSettlement, and startRpcOperation now type params as RpcSendParams<Method>.
  • Compile fences (mobile/src/transport/rpc-operation-compile-fence.ts) — five new type-level fences, including a catalog-wide RpcParams<M> extends RpcSendParams<M> invariant and a sole-unknown-method guard.
  • Catalog comment — the generator and generated catalog both now point senders at RpcSendParams.

The derivation approach (key optionality from optin, value types from z.output) is sound and matches zod v4's own $InferObjectInput/$InferObjectOutput internals; I verified SendOptionalSchema equals zod's OptionalInSchema, that classic ZodObject exposes shape, and that .superRefine()/.refine() return this so the object-branch comment holds. The RpcParams extends RpcSendParams fence direction is correct — it proves nothing a sender could previously build becomes illegal.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

* tuple, lazy, intersection) fall through to z.output, which is what shipped before.
*/
export type RpcSendInput<Schema> =
Schema extends z.ZodOptional<infer Inner>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ZodExactOptional is structurally assignable to z.ZodOptional in zod v4, so this branch also matches .exactOptional() and types it T | undefined — but exactOptional forbids undefined as a value (only absence or the inner value is valid), so the send type over-admits an object the host would reject. No catalog method uses .exactOptional() today, so this is latent, and the catalog-wide fence only guards the opposite (RpcParams extends RpcSendParams) direction.

Technical details
# `RpcSendInput` over-admits `undefined` for `.exactOptional()`

## Affected sites
- src/shared/rpc-contract/rpc-send-params.ts:41 — `Schema extends z.ZodOptional<infer Inner>` also matches `ZodExactOptional` (classic `ZodExactOptional<T>` is assignable to `ZodOptional<T>`), appending `| undefined` where `exactOptional` forbids it.

## Required outcome
- A `.exactOptional()` field types as `field?: T` (no `undefined`), matching what the host accepts.

## Suggested approach (optional)
- Add a `Schema extends z.ZodExactOptional<infer Inner> ? RpcSendInput<Inner> : ...` branch above the `z.ZodOptional` branch. Verified locally: `{ x: undefined }` currently compiles against `z.object({ x: z.string().exactOptional() })`.

@Jinwoo-H

Copy link
Copy Markdown
Contributor Author

Superseded — this work is already in #20018 via 8644f647d0 (merge: incorporate closed boundary and send-side types), which is why GitHub reports this branch as conflicting. Closing rather than merging; rpc-send-params.ts and the catalog-wide RpcParams<M> extends RpcSendParams<M> fence ship with #20018.

Branch kept, not deleted.

@Jinwoo-H Jinwoo-H closed this Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant