Skip to content
Source
Sign inSign up
pingdotgg/t3code

feat(contracts): clients tolerate union members they don't know yet #15951

Mergedjuliusmarmingemerged 3 commits intomain fromrefs/pull/15951/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

Server→client unions grow, but clients decoded most of them as closed unions. When a newer server adds a member (the upcoming secret_request turn item, for example), every older client fails to decode the payload. For a thread snapshot that means "Could not synchronize the thread".

The repo had three hand-written workarounds for this (composer context, attachments, unknown thread events), plus ForwardCompatibleArray. That helper had a latent bug: it checked each element with the element's schema but passed the raw value through, so:

  • an element containing a DateTimeUtc was dropped, known ones included;
  • an element with a decoding default threw.

Every current call site happens to use plain string timestamps, so nothing was visibly broken yet.

What changed

New shared helpers in packages/contracts/src/baseSchemas.ts:

  • ForwardCompatibleUnion(members, tag) is the real members plus a catch-all.
    • A value whose tag this build doesn't know decodes to UnknownUnionMember, so TypeScript makes the caller handle it.
    • A known member that doesn't decode still fails, so real bugs stay visible.
    • Unknown members are decode-only; encoding one fails.
  • ForwardCompatibleUnionArray(members, tag) drops unknown members from an array.
  • hasUnknownUnionTag(members, tag) checks a raw payload, for envelopes that must skip unknown kinds.
  • ForwardCompatibleArray now decodes each element through its own schema (Schema.catchDecoding) and drops only elements that fail. Transformations survive, and the API is unchanged.

Turn items use these helpers:

  • Snapshots and history (turnItems, visibleTurnItems, history pages) drop items whose type the client doesn't know.
  • turn-item.updated events with an unknown type decode to the existing unknown-event case. The client skips them and still advances its resume cursor.

The server still decodes its own stored rows strictly. These helpers are for data clients receive.

Verification

  • baseSchemas.test.ts covers, under the JSON wire codec:
    • dates and decoding defaults inside ForwardCompatibleArray;
    • unknown members of ForwardCompatibleUnion;
    • known members with a broken payload still failing;
    • refusing to encode an unknown member;
    • array filtering.
  • Test totals: contracts 508 pass. Client-runtime plus the server suites that use these schemas: 2,127 pass.
  • Typecheck is clean in contracts, client-runtime, server, web and mobile.
  • Decoding speed is unchanged: about 21 ms per 5,000 history rows, before and after.

Follow-up

Moving UnknownContextRecord, ChatUnknownAttachment and the unknown-event case onto these helpers, and auditing the other closed unions clients decode, is a separate PR.

Opus 5.5 via Claude Code

🤖 Generated with Claude Code

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. juliusmarminge added labelsmacroscope-review

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This PR adds shared forward-compatible union infrastructure and changes existing snapshot, history, stream, and configuration decoding behavior, including automatic filtering of data unknown to the client. Because the behavior applies broadly on production paths rather than behind an opt-in boundary, the runtime blast radius merits human review.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. github-actions[bot](GitHub profile, opens in a new tab)commented

      Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ℹ️ The exact PR base did not have a successful artifact. Baseline uses the latest successful main measurement shown below.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire4.9 KiB5.0 KiB+40 B (+0.8%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB+40 B (+3.4%)2.0 KiB✅
      CodexLive turn WebSocket decoded20.8 KiB20.9 KiB+41 B (+0.2%)29.3 KiB✅
      CodexLive turn messages12+1 (+100.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.2 KiB21.2 KiB0 B (0.0%)29.3 KiB✅
      ClaudeLive turn messages220 (0.0%)8✅

      Baseline: 250e052 · PR result: 302872a · Source CI: success

      Scenario and decoded snapshot size

      10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

      • Codex decoded thread snapshot: 108.5 KiB
      • Claude decoded thread snapshot: 108.8 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    5. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

      Important

      Review skipped

      Review was skipped as selected files did not have any reviewable changes.

      ⚙️ Run configuration
      • Configuration used: Repository: pingdotgg/t3code/.coderabbit.yaml
      • Review profile: CHILL
      • Plan: Team
      • Run ID: 3f79efbe-826f-44f6-bb93-2f7bcb1230da
      📥 Commits

      Reviewing files that changed from the base of the PR and between 302872a89d8d83b3990f4a6764092bffe04204a9 and 302872a89d8d83b3990f4a6764092bffe04204a9.

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: Repository: pingdotgg/t3code/.coderabbit.yaml
      • Review profile: CHILL
      • Plan: Team
      • Run ID: cc88abc8-4467-46c9-a6f3-641153132ab3
      📥 Commits

      Reviewing files that changed from the base of the PR and between 5349dc9f71b04a3a8bf557a4da5e8665ef70bb6f and 302872a89d8d83b3990f4a6764092bffe04204a9.

      📒 Files selected for processing (2)
      • packages/contracts/src/baseSchemas.test.ts
      • packages/contracts/src/baseSchemas.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.


      📝 Walkthrough

      Walkthrough

      Shared schema codecs now support forward-compatible decoding of tagged unions and arrays. Orchestration projections and history pages filter unknown turn-item types, and unknown-item update events decode as unknown events. Malformed known turn-item payloads still fail decoding. A scheduled-automation test now narrows a selected item to user_message.

      Changes

      Turn-item compatibility decoding

      Layer / File(s)Summary
      Add forward-compatible codecs
      packages/contracts/src/baseSchemas.ts, packages/contracts/src/baseSchemas.test.ts
      ForwardCompatibleArray decodes elements individually and keeps successful schema transformations. New union codecs represent unknown tags and filter unknown members from arrays. Tests cover decoding, transformations, and encoding behavior.
      Apply compatibility decoding to orchestration data
      packages/contracts/src/orchestrationV2.ts, packages/contracts/src/orchestrationV2.test.ts
      Thread projections and history pages filter unknown turn-item types. Unknown-item turn-item.updated events decode as unknown-event. Tests verify these cases and confirm malformed known payloads still fail.

      Scheduled automation test narrowing

      Layer / File(s)Summary
      Narrow the selected turn item
      apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
      A type predicate narrows the selected item to user_message before the test accesses its fields.

      Priority: ➖ Normal

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Feature

      Merge Risk: ⚪ Minimal · up to 30287

      No actionable merge-blocking issue is established by the supplied evidence; normal checks remain appropriate before merging.

      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Description check⚠️ WarningThe description explains the problem, the change, and verification results. It does not include the required scope and approval information.Add a link to the triaged issue or maintainer discussion with explicit approval of the direction and scope. If this focused change qualifies as an obvious bug fix without prior approval, explain why.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly summarizes the main change: clients tolerate union members they do not recognize. It is concise and follows a conventional commit format.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 5…
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches
      📝 Generate docstrings
      • Commit to this branch
      • Create a new PR
      🧪 Generate unit tests (beta)
      • Commit to this branch
      • Create a new PR
      • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Comment `@coderabbitai help` to get the list of available commands.
    6. juliusmarminge changed the title

    7. juliusmarminge merged thisca3f973

    8. juliusmarminge closed this

Sign in to join this conversation.