Skip to content

[Bug]: ForwardCompatibleOptional leaves own undefined keys, breaking JSON RPC encoding for ServerSettings and ServerConfig #41218

Closed

Description

@gpu004

What I observed

ForwardCompatibleOptional (packages/contracts/src/baseSchemas.ts:48-62) decodes an unrecognized or invalid field value into an own property with value undefined ({ usageModelFilter: undefined }) instead of omitting the key.

When the server later sends that decoded object over RPC (Schema.toCodecJson), encoding fails with SchemaError(Expected JSON value). In practice, adding an invalid or null usageModelFilter in settings.json (which packages/contracts/src/settings.ts:1625-1626 explicitly documents as user-editable with invalid values ignored) — or downgrading after a newer worktreeSubmodules value is saved — silently passes server startup and then breaks server.getConfig, server.subscribeConfig, server.getSettings, and server.updateSettings for every connected client.

The same bug affects the other 4 ForwardCompatibleOptional fields:

  • ExecutionEnvironmentPlatform.machine (packages/contracts/src/environment.ts:50)
  • ExecutionEnvironmentCapabilities.serverInstallation (packages/contracts/src/environment.ts:158)
  • GitCommandError.reason (packages/contracts/src/git.ts:386)
  • ProviderAuthState.interaction and ProviderAuthState.credentialOwner (packages/contracts/src/providerSetup.ts:134-135)

Reproduction

  • Commit: main @ 9d3054a7bb
  • Environment: macOS 27.2.1, Node v26.13.1

Run from packages/contracts:

import * as Exit from "effect/Exit";
import * as Schema from "effect/Schema";
import { ServerConfigStreamEvent, ServerSettings } from "./src/index.ts";

const settingsCodec = Schema.toCodecJson(ServerSettings);
// settings.json hand-edited with an invalid value or null to clear it
const decoded = Schema.decodeUnknownSync(settingsCodec)({ usageModelFilter: "all" });
console.log("hasOwn:", Object.hasOwn(decoded, "usageModelFilter"), "value:", decoded.usageModelFilter);

const encoded = Schema.encodeUnknownExit(settingsCodec)(decoded);
console.log("ServerSettings encode:", Exit.isSuccess(encoded) ? "OK" : String(encoded.cause));

const streamExit = Schema.encodeUnknownExit(Schema.toCodecJson(ServerConfigStreamEvent))({
  version: 1,
  type: "settingsUpdated",
  payload: { settings: decoded },
});
console.log("settingsUpdated encode:", Exit.isSuccess(streamExit) ? "OK" : String(streamExit.cause));

Also reproduces identically with { usageModelFilter: null } or { projectSettingsOverrides: { p: { worktreeSubmodules: "future-mode" } } }.

Expected behavior

ForwardCompatibleOptional omits the property when decoding an unrecognized/invalid value (or omits undefined on encode, matching OmittedWhenNull at packages/contracts/src/baseSchemas.ts:78-95 via SchemaTransformation.transformOptional returning Option.none()). Any settings.json that decodes cleanly into ServerSettings should encode cleanly over JSON RPC.

Actual behavior

ForwardCompatibleOptional wraps Schema.optionalKey(Schema.Unknown.pipe(Schema.decodeTo(Schema.UndefinedOr(value), SchemaTransformation.transform(...)))). When the key is present with an invalid/unrecognized value, decode leaves an own property { [key]: undefined }. On wire encode, Schema.optionalKey sees the key is present and encodes undefined into Schema.Unknown, which Schema.toCodecJson rejects with SchemaError(Expected JSON value).

Evidence

Output from the reproduction snippet above:

hasOwn: true value: undefined
ServerSettings encode: Cause([Fail(SchemaError(Expected JSON value
  at ["usageModelFilter"]))])
settingsUpdated encode: Cause([Fail(SchemaError(Expected JSON value
  at ["payload"]["settings"]["usageModelFilter"]))])

Also verified end-to-end against ServerSettingsService + WsRpcGroup's Rpc.exitSchema(server.getSettings) codec:

  • service.getSettings succeeds with { usageModelFilter: undefined } in memory, while RPC wire encoding fails for server.getSettings, server.getConfig, server.subscribeConfig (snapshot and settingsUpdated), and server.updateSettings.
  • Even after an unrelated service.updateSettings(...) strips the key on disk via JSON.stringify, normalizeServerSettings (apps/server/src/serverSettings.ts) uses plain Schema.encodeEffect rather than toCodecJson, so the in-memory object keeps the own undefined property and server.updateSettings responses continue failing until server restart.

Additional investigation

  • How found: Property-based testing over packages/contracts schemas, minimized to the plain snippet above.
  • Verified: Confirmed on origin/main (9d3054a7bb); PR fix(contracts): older clients drop unknown values in RPC responses #40490 updated ForwardCompatibleArray in packages/contracts/src/baseSchemas.ts but left ForwardCompatibleOptional unchanged.
  • Duplicate check: Searched open and closed issues/PRs for ForwardCompatibleOptional, usageModelFilter, worktreeSubmodules, and "Expected JSON value" — no existing issue covers this.
  • Impact: High when triggered (breaks initial config/settings loading across web, desktop, and mobile until settings.json is fixed and the server restarts).
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions