From 150598522b5f56cec891e75e607e83aceccfb99f Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 24 Nov 2026 11:26:37 -0800 Subject: [PATCH 1/5] docs(mothership): say the stream validator is the one verdict on every frame --- apps/sim/lib/mothership/request/session/contract.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/sim/lib/mothership/request/session/contract.ts b/apps/sim/lib/mothership/request/session/contract.ts index d2e974c42e5..1c3ad271973 100644 --- a/apps/sim/lib/mothership/request/session/contract.ts +++ b/apps/sim/lib/mothership/request/session/contract.ts @@ -185,9 +185,11 @@ function isStreamScope(value: unknown): value is MothershipStreamV1StreamScope { // // Validates the envelope shell (v, seq, ts, stream, trace?, scope?) and that // `type` is one of the known event types with a non-null payload object. -// Per-payload-variant validation is intentionally lightweight: the server -// already performs strict schema validation; the client only needs enough -// structural checking to safely dispatch inside the switch statement. +// This is the one verdict on every stream frame: on Sim's server (worker ingest, +// Redis buffer reads) and in the browser. The generated zod schema is not +// enforced at runtime: the worker's emitter is only typed against it, and on +// worker ingest it only shadows this verdict, counting disagreements +// (contract-shadow.ts). /** The event types the contract names; a frame of any other type is invalid. */ export const KNOWN_EVENT_TYPES: ReadonlySet = new Set( From 9db2e3831237c07310eee73fc3b6c9dc9735209e Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 24 Nov 2026 11:26:44 -0800 Subject: [PATCH 2/5] test(mothership): pin the schema-accepts shadow case on a real frame --- .../request/session/contract-shadow.test.ts | 35 +++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/apps/sim/lib/mothership/request/session/contract-shadow.test.ts b/apps/sim/lib/mothership/request/session/contract-shadow.test.ts index caa7c8538de..0b676bd0f26 100644 --- a/apps/sim/lib/mothership/request/session/contract-shadow.test.ts +++ b/apps/sim/lib/mothership/request/session/contract-shadow.test.ts @@ -11,16 +11,6 @@ const { recordStreamContractDisagreement } = vi.hoisted(() => ({ })) vi.mock('@/lib/mothership/request/metrics', () => ({ recordStreamContractDisagreement })) -vi.mock('@/lib/mothership/request/session/contract', async () => { - const actual = await vi.importActual( - '@/lib/mothership/request/session/contract' - ) - return { - ...actual, - parsePersistedStreamEventEnvelope: vi.fn(actual.parsePersistedStreamEventEnvelope), - } -}) - const logger = getMockLogger('StreamContractShadow') const shell = { @@ -209,24 +199,25 @@ describe('parseIngestedStreamEventEnvelope', () => { ) it('keeps a rejection the schema would accept and reports it', () => { - const value = frame('tool', { ...toolCall, toolCallId: 'tool-accepted' }) - const rejection = { - ok: false as const, - reason: 'invalid_stream_event' as const, - message: 'A stream event failed validation.', - errors: ['hypothetical'], - } - vi.mocked(parsePersistedStreamEventEnvelope).mockReturnValueOnce(rejection) + // The validator rejects a null activityReceipt; the schema does not declare the key and + // strips it. This fails by design once the two are aligned: then pick another real split. + const value = frame('run', { kind: 'compaction_start', activityReceipt: null }) + const result = parseIngestedStreamEventEnvelope(value) - expect(parseIngestedStreamEventEnvelope(value)).toBe(rejection) + expect(result.ok).toBe(false) + expect(result).toEqual(parsePersistedStreamEventEnvelope(value)) expect(recordStreamContractDisagreement).toHaveBeenCalledExactlyOnceWith( 'schema_accepts', - 'tool', - 'call' + 'run', + 'compaction_start' ) expect(logger.warn).toHaveBeenCalledExactlyOnceWith( 'Stream contract schema disagrees with the stream validator', - expect.objectContaining({ direction: 'schema_accepts', validatorErrors: ['hypothetical'] }) + expect.objectContaining({ + direction: 'schema_accepts', + eventType: 'run', + variant: 'compaction_start', + }) ) }) From 19d1b649d0c1e28cc2a65d1cb5a6e7fefe4df876 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 24 Nov 2026 11:26:45 -0800 Subject: [PATCH 3/5] fix(workflows): offer the ollama model example only where ollama can run --- .../workflows/editing/validation-models.test.ts | 17 +++++++++++++++++ apps/sim/lib/workflows/editing/validation.ts | 11 ++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/apps/sim/lib/workflows/editing/validation-models.test.ts b/apps/sim/lib/workflows/editing/validation-models.test.ts index 1b864ccbd12..7617cc560b7 100644 --- a/apps/sim/lib/workflows/editing/validation-models.test.ts +++ b/apps/sim/lib/workflows/editing/validation-models.test.ts @@ -146,6 +146,23 @@ describe('model ids no provider declares', () => { const { errors } = validateModel('router_v2', 'llama3.1:8b') expect(errors[0]?.error).toContain('Unknown model id') }) + + it('offers an ollama/ namespace example only where Ollama can serve it', () => { + expect(validateModel('router_v2', 'best available model').errors[0]?.error).toContain( + '"ollama/llama3.1:8b"' + ) + + setEnvFlags({ isHosted: true }) + expect(validateModel('router_v2', 'best available model').errors[0]?.error).not.toContain( + 'ollama/' + ) + + setEnvFlags({ isHosted: false }) + envFlagsMockFns.getBlacklistedProvidersFromEnv.mockReturnValue(['ollama']) + expect(validateModel('router_v2', 'best available model').errors[0]?.error).not.toContain( + 'ollama/' + ) + }) }) describe('fallback model rows', () => { diff --git a/apps/sim/lib/workflows/editing/validation.ts b/apps/sim/lib/workflows/editing/validation.ts index 7df6f8bae2a..95022b6f386 100644 --- a/apps/sim/lib/workflows/editing/validation.ts +++ b/apps/sim/lib/workflows/editing/validation.ts @@ -1025,6 +1025,15 @@ export function validateValueForSubBlockType( const suggestions = suggestModelIdsForUnknownModel(trimmed) const suggestionText = suggestions.length > 0 ? ` Valid options include: ${suggestions.join(', ')}.` : '' + const namespaceExamples = [ + 'azure/my-deployment', + 'azure-anthropic/my-deployment', + 'bedrock/my-inference-profile', + 'vertex/my-model', + ...(isOllamaAvailable() ? ['ollama/llama3.1:8b'] : []), + ] + .map((example) => `"${example}"`) + .join(', ') return { valid: false, error: { @@ -1032,7 +1041,7 @@ export function validateValueForSubBlockType( blockType, field: fieldName, value, - error: `Unknown model id "${trimmed}" for block "${blockType}". Read components/blocks/${blockType}.json (the model.options array) for valid ids; prefer entries with recommended: true and avoid deprecated: true. For user-configured models, use a supported provider namespace, e.g. "azure/my-deployment", "azure-anthropic/my-deployment", "bedrock/my-inference-profile", "vertex/my-model", "ollama/llama3.1:8b", or "openrouter/provider/model".${suggestionText}`, + error: `Unknown model id "${trimmed}" for block "${blockType}". Read components/blocks/${blockType}.json (the model.options array) for valid ids; prefer entries with recommended: true and avoid deprecated: true. For user-configured models, use a supported provider namespace, e.g. ${namespaceExamples}, or "openrouter/provider/model".${suggestionText}`, }, } } From 07a1196d1de3e4ddfda4075cad047c07ead41edb Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 24 Nov 2026 11:29:54 -0800 Subject: [PATCH 4/5] fix(mothership): sync the resource owner strip and stream contract notes from the worker A resource frame whose owner carries a key a newer worker added no longer fails Sim's resource check (ResourcePayload): the owner now strips unknown keys. Copies of worker/packages/contracts resources.ts and mothership-stream-v1.ts. --- .../lib/mothership/generated/mothership-stream-v1.ts | 12 +++++++----- apps/sim/lib/mothership/generated/resources.ts | 7 +++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/apps/sim/lib/mothership/generated/mothership-stream-v1.ts b/apps/sim/lib/mothership/generated/mothership-stream-v1.ts index 7c03474a099..9fd393fe5d2 100644 --- a/apps/sim/lib/mothership/generated/mothership-stream-v1.ts +++ b/apps/sim/lib/mothership/generated/mothership-stream-v1.ts @@ -4,11 +4,13 @@ // The mothership-stream-v1 SSE wire: every `data:` frame the worker emits to Sim (each leg // ends with a literal `data: [DONE]` line), and the vocabulary Sim re-emits to the browser // (Sim adds `session` chat/title frames of its own). -// The zod schemas below are the single source (D122): every type is inferred from them, so the -// worker's emitter (apps/server/src/protocol/envelope.ts) fails typecheck on a frame that drifts, -// and a reader can validate a frame at runtime against the same definition. Each discriminant -// const below is checked against its payload union, because Sim's reader accepts exactly the -// const's values. Objects strip unknown keys: a newer emitter's extra field never fails a reader. +// The zod schemas below are the single source (D122): every payload type is inferred from them, +// so the worker's emitter (apps/server/src/protocol/envelope.ts) fails typecheck on a frame that +// drifts, and a reader can validate a frame at runtime against the same definition. Each +// discriminant const below is checked against its payload union, because Sim's reader accepts +// exactly the const's values. Objects strip unknown keys: a newer emitter's extra field never +// fails a reader, except in a `resource` payload's `resource.search.nativeQueries` entries, which +// reuse Sim's strict model tool-input schema. // Its own optional fields are `?: T` (`.exactOptional()`); the protocol.ts fields it spreads // are `?: T | undefined`. diff --git a/apps/sim/lib/mothership/generated/resources.ts b/apps/sim/lib/mothership/generated/resources.ts index db50ef5b859..67c322ab540 100644 --- a/apps/sim/lib/mothership/generated/resources.ts +++ b/apps/sim/lib/mothership/generated/resources.ts @@ -41,8 +41,11 @@ export const SearchResource = z.object({ }); export type SearchResource = z.infer; +/** Resource frames and tool output cross versions, so an owner's unknown keys strip. */ +const ResourceOwner = z.object(FileOwner.shape); + export const ResourceAddress = z.object({ - owner: FileOwner.optional(), + owner: ResourceOwner.optional(), workspaceId: z.uuid().optional(), workspaceName: z.string().optional(), type: ResourceType, @@ -93,7 +96,7 @@ export const ResourceChange = z.discriminatedUnion("op", [ resource: z.union([ z.object({ type: ResourceType, - owner: FileOwner.optional(), + owner: ResourceOwner.optional(), workspaceId: z.uuid().optional(), id: z.string().regex(/\S/).optional(), }), From c81cdbe0b9f1c8a217aaa9c1b23a89c5a5b3a7db Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 24 Nov 2026 11:44:18 -0800 Subject: [PATCH 5/5] docs(mothership): note that resource payloads use their generated schema --- apps/sim/lib/mothership/request/session/contract.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/apps/sim/lib/mothership/request/session/contract.ts b/apps/sim/lib/mothership/request/session/contract.ts index 1c3ad271973..b6f213e2582 100644 --- a/apps/sim/lib/mothership/request/session/contract.ts +++ b/apps/sim/lib/mothership/request/session/contract.ts @@ -186,10 +186,11 @@ function isStreamScope(value: unknown): value is MothershipStreamV1StreamScope { // Validates the envelope shell (v, seq, ts, stream, trace?, scope?) and that // `type` is one of the known event types with a non-null payload object. // This is the one verdict on every stream frame: on Sim's server (worker ingest, -// Redis buffer reads) and in the browser. The generated zod schema is not -// enforced at runtime: the worker's emitter is only typed against it, and on -// worker ingest it only shadows this verdict, counting disagreements -// (contract-shadow.ts). +// Redis buffer reads) and in the browser. Only a resource payload is checked +// against its generated schema (ResourcePayload). The generated envelope schema +// (MothershipStreamV1EventEnvelopeSchema) is not enforced at runtime: the +// worker's emitter is only typed against it, and on worker ingest it only +// shadows this verdict, counting disagreements (contract-shadow.ts). /** The event types the contract names; a frame of any other type is invalid. */ export const KNOWN_EVENT_TYPES: ReadonlySet = new Set(