Skip to content

improvement(mothership): shadow-check worker stream frames against the contract schemas - #10354

Merged
waleedlatif1 merged 2 commits into
stagingfrom
improvement/stream-v1-shadow-validation
Nov 23, 2026
Merged

waleedlatif1 merged 2 commits into
stagingfrom
improvement/stream-v1-shadow-validation

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Takes the regenerated mothership-stream-v1 contract, whose types are now inferred from Zod schemas (the worker side is the companion PR)

  • Frames the worker sends this server (the live leg and the run replay) go through parseIngestedStreamEventEnvelope. The existing validator still decides every frame, and the contract schema runs beside it. Behaviour is unchanged.

  • A disagreement in either direction:

    • increments copilot.stream_contract.disagreements, labelled by direction, event type and kind/phase;
    • logs the Zod issue paths, once per distinct disagreement per ten minutes.

    A failure of the check itself is logged under the same limit.

  • Synthetic file-preview frames are skipped. The browser and the Redis outbox reader are unchanged. The shadow lives in its own server-only module, so none of it reaches the client bundle

  • run-replay.ts validates the replay-end payload with the synced MothershipStreamV1ReplayEndPayloadSchema instead of a local copy

  • This is phase A of making the schema the single validator. Phase B switches server ingest to the schema only after the counter has stayed at zero schema_rejects on hosted traffic. The schema is stricter than today's per-type checks (required fields, enum values), and a rejected frame ends the turn.

Type of Change

  • Improvement

Testing

  • contract-shadow.test.ts: a representative frame for each kind of gap (accepted today, rejected by the schema). Each asserts the verdict and returned object are unchanged and the counter and log fire. Further tests cover the opposite direction, agreement (including replay_end on a live leg), skipped synthetic frames, the dedupe window, and a throwing meter logging once
  • Cost: one safeParse per frame, about the cost of the frame's JSON.parse (0.2–1.3 µs on the server)
  • Generated copies byte-identical to the worker sources (check:contract-sync)
  • tsc --noEmit and bun run lint clean; vitest over lib/mothership passes; test-pattern, test-type and audit checks pass
  • Reviewed for correctness (verdicts unchanged, server-only, zod 4.3.6 compatibility, ~1 KB gzip browser growth from the schema module) and with /simplify

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Companion: simstudioai/mothership#1008

…e contract schemas

Takes the regenerated mothership-stream-v1 contract, whose types are now
inferred from zod schemas. Frames the worker sends this server (the live
leg and the run replay) go through parseIngestedStreamEventEnvelope: the
existing validator still decides every frame, and the contract schema
runs beside it. A disagreement in either direction increments
copilot.stream_contract.disagreements (direction, event type, capped
kind/phase) and logs the zod issue paths, once per distinct disagreement
per ten minutes; a failure of the check itself is logged under the same
limit. Synthetic file-preview frames are skipped by their previewPhase.
The browser and the outbox reader are unchanged.

Tests pin a representative set of frames the validator accepts and the
schema rejects, one per kind of gap. The run replay reader validates
replay-end payloads with the synced MothershipStreamV1ReplayEndPayloadSchema
instead of a local copy of the same shape, and the shadow reuses the
contract parser's event-type set.
@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Nov 23, 2026 9:33pm UTC

Request Review

@github-actions github-actions Bot added the requires-mothership-merge Has a companion PR on the mothership/copilot side — merge in lockstep label Nov 23, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Cross-repo companion check

One or more companion PRs aren't merged into staging yet. Merging this without them will leave copilot and sim out of sync — merge them in lockstep.

  • ❌ simstudioai/mothership#1008 — OPEN, not merged (targets staging) — refactor(contracts): define mothership-stream-v1 as zod schemas

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 11 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Adds a server-side comparison between the existing worker-frame validator and the generated Zod schemas without changing which frames are accepted.

  • Worker stream frames now get runtime schemas and types from one contract.
  • Sim compares worker stream frames with the generated schemas without changing its verdict.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Worker frame] --> B[Existing validator]
  B --> C{Accepted only as synthetic preview?}
  C -->|Yes| G[Return existing verdict]
  C -->|No| D[Check generated schema]
  D --> E{Verdicts differ?}
  E -->|Yes| F[Count disagreement and rate-limit logs]
  E -->|No| G
  F --> G
Loading

Reviews (2) · Last reviewed commit: "improvement(mothership): tighten the str..." · Reviewed by Greptile

…d dedupe

The shadow skipped any accepted frame carrying a payload previewPhase, so
a worker frame with a stray previewPhase hid its schema rejection. It now
skips only a frame the contract check rejects, which is exactly one the
validator accepted as a synthetic file-preview frame; the previewPhase
test stays as the cheap prefilter. A test pins the stray-field case.

The log dedupe is an lru-cache (max 256, ttl the ten-minute window,
read against Date.now) instead of a hand-rolled TTL map, still reporting
the repeats skipped since the last line. Takes the regenerated protocol
contract, whose tool deadlines are positive integers, and documents the
new metric recorder and direction type with TSDoc.
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai 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.

No issues found across 11 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 3139326 into staging Nov 23, 2026
153 of 155 checks passed
@waleedlatif1
waleedlatif1 deleted the improvement/stream-v1-shadow-validation branch November 23, 2026 21:57

This branch was previously deployed

1 inactive deployment
Preview — 258b0b0b Deployed Nov 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

requires-mothership-merge Has a companion PR on the mothership/copilot side — merge in lockstep

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant