Practical reference for migrating data types in packages/opencode from
Zod-first definitions to Effect Schema with Zod compatibility shims.
Use Effect Schema as the source of truth for domain models, IDs, inputs,
outputs, and typed errors. Keep Zod available at existing HTTP, tool, and
compatibility boundaries by exposing a .zod static derived from the Effect
schema via @/util/effect-zod.
The long-term driver is specs/effect/http-api.md — once the HTTP server
moves to @effect/platform, every Schema-first DTO can flow through
HttpApi / HttpRouter without a zod translation layer, and the entire
effect-zod walker plus every .zod static can be deleted.
Use Schema.Class for structured data.
export class Info extends Schema.Class<Info>("Foo.Info")({
id: FooID,
name: Schema.String,
enabled: Schema.Boolean,
}) {
static readonly zod = zod(Info)
}
If the class cannot reference itself cleanly during initialization, use the
two-step withStatics pattern:
export const Info = Schema.Struct({
id: FooID,
name: Schema.String,
}).pipe(withStatics((s) => ({ zod: zod(s) })))
Use Schema.TaggedErrorClass for domain errors.
export class NotFoundError extends Schema.TaggedErrorClass<NotFoundError>()("FooNotFoundError", {
id: FooID,
}) {}
Keep branded/schema-backed IDs as Effect schemas and expose
static readonly zod for compatibility when callers still expect Zod.
Reuse named refinements instead of re-spelling z.number().int().positive()
in every schema. The effect-zod walker translates the Effect versions into
the corresponding zod methods, so JSON Schema output (type: integer,
exclusiveMinimum, pattern, format: uuid, …) is preserved.
const PositiveInt = Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThan(0))
const NonNegativeInt = Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThanOrEqualTo(0))
const HexColor = Schema.String.check(Schema.isPattern(/^#[0-9a-fA-F]{6}$/))
See test/util/effect-zod.test.ts for the full set of translated checks.
During migration, route validators, tool parameters, and any existing
Zod-based boundary should consume the derived .zod schema instead of
maintaining a second hand-written Zod schema.
The default should be:
.zod exists only as a compatibility surfaceIt is fine to keep a Zod-native schema temporarily when:
zod()When this happens, prefer leaving a short note or TODO rather than silently creating a parallel schema source of truth.
The walker in @/util/effect-zod exposes two explicit escape hatches for
cases the pure-Schema path cannot express. Each one stays in the codebase
only as long as its upstream or local dependency requires it — inline
comments document when each can be deleted.
ZodOverride annotationReplaces the entire derivation with a hand-crafted zod schema. Used when:
$ref metadata (e.g.
config/model-id.ts points at https://models.dev/...)ConfigAgent.Info, Log.Level)DeepMutable<T> in config/config.tsSchema.Struct produces readonly types. Some consumer code (notably the
Config service) mutates Info objects directly, so a readonly-stripping
utility is needed when casting the derived zod schema's output type.
Types.DeepMutable from effect-smol would be a drop-in, but it widens
unknown to {} in the fallback branch — a bug that affects any schema
using Schema.Record(String, Schema.Unknown).
Tracked upstream as effect:core/x228my: "Types.DeepMutable widens unknown
to {}." Once that lands, the local DeepMutable copy can be deleted and
Types.DeepMutable used directly.
Migrate in this order:
schema.ts filesInfo, Input, Output, and DTO types.zodThis keeps shared types canonical first and makes boundary updates mostly mechanical.
src/config/ ✅ completeAll of packages/opencode/src/config/ has been migrated. Files that still
import z do so only for local ZodOverride bridges or for z.ZodType
type annotations — the export const <Info|Spec> values are all Effect
Schema at source.
A file is considered "done" when:
Info, Input, Event, Definition, etc.)
are authored as Effect Schemazod() /
zodObject()), a z.ZodType type annotation, or a documented
ZodOverride escape hatch — never a hand-written parallel source of truthFiles that meet this bar but still carry a compat bridge are checked off with an inline note describing the bridge and what unblocks its removal.
src/*/schema.ts leaf modulesThese are the highest-priority next targets. Each is a small, self-contained schema module with a clear domain.
src/account/schema.tssrc/control-plane/schema.tssrc/permission/schema.tssrc/project/schema.tssrc/provider/schema.tssrc/pty/schema.tssrc/question/schema.tssrc/session/schema.tssrc/storage/schema.tssrc/sync/schema.tssrc/tool/schema.tssrc/util/schema.tsMajor cluster. Message + event types flow through the SSE API and every SDK output, so byte-identical SDK surface is critical.
Suggested order for this cluster, starting from the leaves that session.ts
and the SSE/event surface depend on:
src/session/schema.ts ✅ already migratedsrc/provider/schema.ts if message-v2.ts still relies on zod-first IDssrc/lsp/* schema leaves needed by LSP.Rangesrc/snapshot/* leaves used by Snapshot.FileDiffsrc/session/message-v2.tssrc/session/message.tssrc/session/prompt.tssrc/session/revert.tssrc/session/summary.tssrc/session/status.tssrc/session/todo.tssrc/session/session.tssrc/session/compaction.tsDependency sketch:
session.ts
|- project/schema.ts
|- control-plane/schema.ts
|- permission/schema.ts
|- snapshot/*
|- message-v2.ts
| |- provider/schema.ts
| |- lsp/*
| |- snapshot/*
| |- sync/index.ts
| `- bus/bus-event.ts
|- sync/index.ts
|- bus/bus-event.ts
`- util/update-schema.ts
Working rule for this cluster:
Session.Info after their nested pieces exist as
named Schema valuesmessage-v2.ts first-pass outline:
SessionID, MessageID, PartIDProviderID, ModelIDPartBasetime.start/endSnapshotPart, PatchPart, TextPart, ReasoningPartFilePart, AgentPart, CompactionPart, SubtaskPartFilePartSourceNamedError.create(...) shapes can stay temporarily if converting them to
Schema.TaggedErrorClass would force unrelated churnSyncEvent.define(...) and BusEvent.define(...) payloads can use
derived .zod at remaining zod-based HTTP/OpenAPI boundariesPossible later tightening after the Schema-first migration is stable:
promote repeated opaque strings and timestamp numbers into branded/newtype leaf schemas where that adds domain value without changing the wire format
[x] src/session/compaction.ts
[x] src/session/message-v2.ts
[x] src/session/message.ts
[x] src/session/prompt.ts
[x] src/session/revert.ts
[x] src/session/session.ts
[x] src/session/status.ts
[x] src/session/summary.ts
[x] src/session/todo.ts
src/provider/auth.tssrc/provider/models.tssrc/provider/provider.tsEach tool declares its parameters via a zod schema. Tools are consumed by both the in-process runtime and the AI SDK's tool-calling layer, so the emitted JSON Schema must stay byte-identical.
src/tool/apply_patch.tssrc/tool/bash.tssrc/tool/codesearch.tssrc/tool/edit.tssrc/tool/glob.tssrc/tool/grep.tssrc/tool/invalid.tssrc/tool/lsp.tssrc/tool/plan.tssrc/tool/question.tssrc/tool/read.tssrc/tool/registry.tssrc/tool/skill.tssrc/tool/task.tssrc/tool/todo.tssrc/tool/tool.tssrc/tool/webfetch.tssrc/tool/websearch.tssrc/tool/write.tsEvery file in src/server/routes/ uses hono-openapi with zod validators for
route inputs/outputs. Migrating these individually is the last step; most
will switch to .zod derived from the Schema-migrated domain types above,
which means touching them is largely mechanical once the domain side is
done.
src/server/error.tssrc/server/event.tssrc/server/projectors.tssrc/server/routes/control/index.tssrc/server/routes/control/workspace.tssrc/server/routes/global.tssrc/server/routes/instance/index.tssrc/server/routes/instance/config.tssrc/server/routes/instance/event.tssrc/server/routes/instance/experimental.tssrc/server/routes/instance/file.tssrc/server/routes/instance/mcp.tssrc/server/routes/instance/permission.tssrc/server/routes/instance/project.tssrc/server/routes/instance/provider.tssrc/server/routes/instance/pty.tssrc/server/routes/instance/question.tssrc/server/routes/instance/session.tssrc/server/routes/instance/sync.tssrc/server/routes/instance/tui.tsThe bigger prize for this group is the @effect/platform HTTP migration
described in specs/effect/http-api.md. Once that lands, every one of
these files changes shape entirely (HttpApi.endpoint(...) and friends),
so the Schema-first domain types become a prerequisite rather than a
sibling task.
Small / shared / control-plane / CLI. Mostly independent; can be done piecewise.
src/acp/agent.tssrc/agent/agent.tssrc/bus/bus-event.tssrc/bus/index.tssrc/cli/cmd/tui/config/tui-migrate.tssrc/cli/cmd/tui/config/tui-schema.tssrc/cli/cmd/tui/config/tui.tssrc/cli/cmd/tui/event.tssrc/cli/ui.tssrc/command/index.tssrc/control-plane/adaptors/worktree.tssrc/control-plane/types.tssrc/control-plane/workspace.tssrc/file/index.tssrc/file/ripgrep.tssrc/file/watcher.tssrc/format/index.tssrc/id/id.tssrc/ide/index.tssrc/installation/index.tssrc/lsp/client.tssrc/lsp/lsp.tssrc/mcp/auth.tssrc/patch/index.tssrc/plugin/github-copilot/models.tssrc/project/project.tssrc/project/vcs.tssrc/pty/index.tssrc/skill/index.tssrc/snapshot/index.tssrc/storage/db.tssrc/storage/storage.tssrc/sync/index.ts — public API (SyncEvent.define) is Schema-first; payloads() still derives zod for the remaining HTTP/OpenAPI boundarysrc/util/fn.tssrc/util/log.tssrc/util/update-schema.tssrc/worktree/index.tssrc/util/effect-zod.ts — the walker itself. Stays zod-importing forever
(it's what emits zod from Schema). Goes away only when the .zod
compatibility layer is no longer needed anywhere.@/util/effect-zod for all Schema → Zod conversion.packages/sdk/
openapi.json and packages/sdk/js/src/v2/gen/types.gen.ts) byte-identical
unless the change is deliberately user-visible.