Skip to content
Agents: this page is Markdown at /effect-handbook/troubleshooting/anti-patterns.md · llms.txt · reading guide View raw

Generated-Code Anti-Patterns ​

This is the list to check generated or pasted code against. Each row names the shape to search for, why it is wrong, the preferred shape, and the section that owns the explanation. For a reviewer's yes/no version of the same material, use the Review Checklists.

Effect 3 names that do not exist in Effect 4 ​

Generated code most often breaks on a name carried over from Effect 3. Each of these fails to compile against this edition's audited release; use the Effect 4 spelling instead.

Effect 3 nameEffect 4
Effect.catchAllEffect.catch
Effect.catchAllCauseEffect.catchCause
Effect.catchSomeEffect.catchIf or Effect.catchTag
Effect.eitherEffect.result (returns a Result)
Effect.tapErrorCauseEffect.tapCause
Effect.zipLeft / Effect.zipRightEffect.tap / Effect.andThen
Context.Tag, Effect.ServiceContext.Service — see Services, Context & Layers
Layer.scopedLayer.effect (it already handles a Scope requirement)
Schema.decodeUnknown / Schema.encodeUnknownSchema.decodeUnknownEffect / Schema.encodeUnknownEffect (or the Sync, Result, Exit, Option, and Promise variants) — see Schema
Schema.decode(schema) as a decoderSchema.decodeEffect(schema); in Effect 4 Schema.decode builds a transformation from getters
@effect/platform, @effect/rpc, @effect/sql, @effect/climodules inside effect: FileSystem and Path from "effect", HTTP from "effect/http", and so on; host Layers from @effect/platform-node — see Getting Started
NodeRuntime from "effect"NodeRuntime from @effect/platform-node

Anti-patterns: construction and running ​

Anti-patternWhy it is wrongPreferred shapeDetails
Effect.succeed(sideEffect()), Effect.fail(buildError())The argument is evaluated while the program is built; every run and retry replays one captured value.Effect.sync(() => ...), Effect.failSync, or Effect.suspend.Creating effects
Effect.sync(() => JSON.parse(text)) or any throwing code in Effect.syncsync asserts "cannot throw"; the throw becomes a defect, invisible to catchTag and retry.Effect.try({ try, catch }) with a real error mapper.Creating effects
Effect.promise(() => fetch(url))A rejection becomes a defect, and no signal is forwarded.Effect.tryPromise({ try: (signal) => fetch(url, { signal }), catch }).Creating effects
Effect.tryPromise({ try: () => sdk.call(x), ... })No signal parameter is declared, so no AbortController exists; timeouts and races abandon the I/O instead of cancelling it.Declare and forward signal in every adapter.Cancellable adapters
catch: (cause) => new SdkError({ cause }) for every rejectionA TypeError inside the adapter becomes an "expected outage" and gets retried.Recognize the failures the contract allows; Effect.die the rest.Cancellable adapters
async/await throughout Effect servicesNative Promise work escapes typed errors, services, cancellation, and test services.Compose Effects; wrap a real Promise boundary once with Effect.tryPromise.Sequencing with gen & fn
Effect.runPromise inside a service method or handlerCreates a root fiber with an empty context: interruption, provided services, and the typed failure are lost.Return Effect<A, E, R>; run at an owned edge.Running effects at an owned edge
Effect.runSync(parse(x)) while building a descriptionConstruction is no longer inert, and runSync throws at the first asynchronous boundary.Compose the effect; run once at the edge.Running effects at an owned edge
Effect.map(() => someEffect); an Effect built in a callback and discardedProduces a nested or dropped Effect that never runs.Effect.flatMap, Effect.andThen, or yield*.“My Effect never ran”
Point-free callbacks: Effect.forEach(ids, loadBand), Effect.map(f) with an overloaded f, flow(...)forEach calls f(element, index), so an optional second parameter silently receives the index; overloads and generics can be erased.Write the lambda: (id) => loadBand(id).Official guide: Guidelines
Effect.if, Effect.unless, Effect.whenEffect, Effect.loop, Effect.iterate, Effect.zipLeft, Effect.zipRightNot exported.Plain if / for in Effect.gen; Effect.when(conditionEffect); Effect.andThen / Effect.tap.Branching and looping
Date.now(), new Date(), Math.random() in domain workBypasses Clock/Random services and deterministic tests; direct workflow use also breaks replay determinism.Effect Clock/DateTime/Random APIs; TestClock and seeded services. Put nondeterminism in a Workflow Activity.Clock, Random
process.exit() in application codeSkips finalizers and the drain.Let the platform runMain translate the Exit into an exit code.Signals, exit codes, and the time budget

Anti-patterns: errors and recovery ​

Anti-patternWhy it is wrongPreferred shapeDetails
Effect.catch(() => fallback) everywhereErases domain distinctions and often hides operational failures.Recover by tag/reason at the layer that owns the policy; preserve unexpected failures.Selective recovery beyond one tag
Retrying every failure foreverRetries permanent failures and amplifies outages.Typed retryable errors, Schedule.while, jitter, and hard time/attempt bounds.Retry policy checklist
Retrying or defaulting a defectHides a bug behind a plausible value.Let it fail; report it at a process or request boundary.The three failure buckets
Effect.orDie to tidy an error union; Effect.die on an expected failureCallers lose a condition they could act on.Keep it typed; translate with Effect.mapError; declare it on the endpoint.Recovering from a mixed Cause
Effect.catchCause(() => Effect.void), Effect.ignoreCause in domain codeSwallows defects and interruption; can block shutdown.A guarded catchCause that re-fails, or catchDefect that re-dies the unknown.Typed failure versus defect and interruption
Translating interruption into a domain error, retrying it, or logging it as a faultInterruption is cancellation, not failure.Let it propagate; finalizers do the work.The three failure buckets
Option.getOrThrow, or re-throwing result.failure, in domain codeReintroduces the hidden branch the type removed.Effect.fromOption / Effect.fromResult, or collapse at a real boundary.Fallbacks and boundary exits
switch with default, or Match.orElse, in an error-policy matchTomorrow's variant is silently assigned to today's fallback.Match.exhaustive / Match.tagsExhaustive.Classifying an error union
Folding (Effect.match) or catching everything in a low-level functionSteals policy from every caller.Fold once, at the terminal boundary that owns the response.Folding both channels at a boundary
Mapping a timeout, pool exhaustion, or driver error to NotFound / 404Clients act on a lie.Only a query that succeeded and returned nothing is "not found".Designing the error model
A throw inside a service method or a test fakeIt becomes a defect; tests then exercise a different channel than production.Effect.fail(new DomainError(...)).Designing a service contract
A correct negative answer modeled as an errorRetry schedules and alerts treat a right answer as a fault.Return it in A.Designing the error model

Anti-patterns: services, Layers, and configuration ​

Anti-patternWhy it is wrongPreferred shapeDetails
Providing a live Layer or service inside business logicR looks clean while production I/O is hard-wired; tests reach the network and each call is its own build.Provide live implementations at application edges, fakes at test edges.Providing one value or building a graph
Per-request Effect.provide(DbLive); a ManagedRuntime, pool, or exporter per requestSibling provides each build the Layer: a pool per request.One enclosing build at the edge, or one ManagedRuntime per host.What is shared, and what is rebuilt
Rebuilding Layers inline at multiple branchesDifferent Layer identities may acquire duplicate pools/resources.Name and reuse one Layer value inside the composition graph.What is shared, and what is rebuilt
Layer.fresh or { local: true } "to fix the types"Duplicates pools, caches, and subscriptions.Read the remaining requirements and wire exactly that.Reading and composing the graph
Layer.succeed for something that needs teardownIt packages a finished value and cannot register a finalizer.Layer.effect with Effect.acquireRelease.Resourceful services
A service method typed Effect<A, E, SomeDependency>Every caller and test must provide what only the implementation needs.Capture dependencies in the Layer constructor; public methods have R = never.Designing a service contract
A service per helper, or one god serviceBoundaries that exist only for mocking, or none at all.One service per capability with a substitution reason.Designing a service contract
Module-level createServer(), Ref, or counterShared by every build of the Layer: two tests or tenants share state.Allocate during Layer acquisition.Own the listener
Casting R away, or as any on a LayerThe missing service fails at run time instead.Find the tag, provide its Layer at the right level.A service is still present in R
process.env reads in servicesA second, untyped definition of "valid deployment".One Config program, decoded during a Layer build.Designing the startup contract
Config.orElse(() => Config.succeed(d)) as a default; a logical-or fallbackMasks a malformed value; treats 0 and false as absent.Config.withDefault(d).Absence is not malformed input
Choosing live or fake infrastructure from whether a token is setA missing secret silently selects the fake in production.An explicit mode: Config.Literals(["live", "sandbox"], "MODE").Deciding required, optional, and defaulted
Layer.mergeAll(Service.layer, ConfigLive)The provider override does not reach its sibling.Service.layer.pipe(Layer.provide(ConfigLive)), or provide it outermost.Precedence and composition
Node platform imports (@effect/platform-node, node:*) in domain code or in a browser or edge bundleThe module now knows which host it runs in, and the bundle breaks elsewhere.Depend on the capability; import the platform package in one module at the edge.Keep platform and unstable imports behind a capability
Redacted.value early, then passing the string aroundNothing downstream can mask it again.Keep it Redacted until the single call that needs the bytes.Redacted

Anti-patterns: resources, fibers, and state ​

Anti-patternWhy it is wrongPreferred shapeDetails
Manual acquire/use without a finalizerFailure or interruption leaks the handle.Effect.acquireRelease, acquireUseRelease, a scoped Layer, Pool, ScopedCache, or another owner-aware primitive.Interruption & resource safety
Cleanup attached with Effect.tap / andThen, or written after yield*Runs on success only; misses failure and interruption.Effect.ensuring, Effect.onExit, Effect.acquireRelease.Success-only cleanup
One acquireRelease that opens two handlesA failure on the second leaks the first.One bracket per resource.One bracket per resource
An unbounded or fallible finalizerFinalizers run uninterruptibly: an unbounded finalizer is an unbounded shutdown.Bound it, then apply a stated failure policy.When cleanup can fail
Effect.forkDetach without a named owner and stop pathNothing closes, bounds, or observes the fiber.forkChild, forkScoped, forkIn, or a FiberSet owned by a Layer.Choosing a fork by its owner
An interior Effect.runFork or forkDetach "so the write survives the request"Shutdown cannot interrupt it, and it keeps using a pool after release.Transfer the work to a Layer-owned, bounded supervisor.Bridge into a host that is not Effect
Ref.get then Ref.setTwo transitions; a suspension between them loses updates.One Ref.update / Ref.modify; SynchronizedRef when the transition is effectful.One transition, one call
Mutating the object stored in a RefBypasses the atomic transition and changes values other fibers already read.Install a new immutable value in one update.One transition, one call
Reading a MutableRef, suspending, then writing from the stale valueAnother fiber can update it during the suspension; replacing it with separate Ref.get / Ref.set calls has the same race.One synchronous MutableRef.update, one Ref.update, or SynchronizedRef for an effectful transition.MutableRef
Non-idempotent I/O inside Effect.txA transaction body can rerun on conflict.Keep external effects outside the transaction.The mental model
Reusing a completed Deferred for the next generationThe next generation is "ready" instantly.A fresh Deferred per generation; complete it on every outcome.Deferred
Queue.shutdown as end-of-inputDiscards the buffer; on an open queue, interrupts takers/awaiters and rejects pending offers through their result values.Queue.end, then join the workers.Queue lifecycle
Readiness published before acquisition, or left true while drainingTraffic is routed to a process that cannot serve.Readiness is a Layer acquired last and released first.Liveness, readiness, and draining

Anti-patterns: concurrency, streams, and caching ​

Anti-patternWhy it is wrongPreferred shapeDetails
{ concurrency: "unbounded" } over uncontrolled inputCan exhaust sockets, memory, API quotas, or database connections.Use a numeric bound, shared Semaphore, bounded Queue/Stream stage, or Pool.Capacity is part of the contract
Queue.unbounded chosen because a test hungHides the deadlock and removes backpressure.Find the missing consumer or end signal; test with capacity 1.Queue lifecycle
dropping / sliding as a performance switch; an unchecked offerUnsafe resultSilent data loss.Treat it as a stated loss policy and count what was shed.Make loss observable
Stream.callback without bufferSizeThe buffer is unbounded by default.{ bufferSize, strategy }, and say which loss policy it is.Where buffering hides
Stream.runCollect on unknown or infinite inputMaterializes the full stream and may never return or exhaust memory.Incremental runForEach, a bounded Sink, batching, or streaming output.Running streams
runCollect then slice for a preview; take after bufferReads far more than the prefix.Stream.take(n) upstream of buffers and concurrent stages.Where buffering hides
Opening a handle before the stream and closing it "when done"Leaks on early stop or decode failure.Stream.unwrap over a scoped acquisition.Owning resources inside a stream
Stream.buffer({ capacity: n }) to get batches of nCapacity is flow-control depth, not batch size.Stream.grouped(n), groupedWithin(n, d), or rechunk(n).Where buffering hides
Stream.timeout as a failure detectorIt ends the stream with no error.Stream.timeoutOrElse with Stream.fail(...).Handling stream failures
Pairing resolver rows to entries by position, or skipping entries without a rowCallers receive each other's data, or die with "did not complete request".Index rows by id; iterate the entries and settle each.Resolver obligations
A hand-written promise map or lock for single-flightCache.get already shares one lookup among concurrent misses.Cache, or Effect.cached for a keyless effect.Which problem do you have?
Cache.make with a long TTL over a fallible lookupOne transient failure is replayed for the full TTL.Cache.makeWith with a short failure TTL.Failure and freshness policy
Whole records or un-normalized strings as cache keysEqual real-world things produce different keys.Normalize at ingress; a small key class.Keys are logical values

Anti-patterns: Schema and boundaries ​

Anti-patternWhy it is wrongPreferred shapeDetails
Schema.Number for every external numberAccepts NaN and infinities and says nothing about integer/range constraints.Schema.Finite, Schema.Int, Schema.Natural, BigDecimal/BigInt codecs, and explicit checks.Refinements
JSON.parse(raw) as Model, raw as EmployeeIdA cast validates nothing.Decode once at ingress; obtain brands through the decoder.Branded schemas
Effect.sync(() => Schema.decodeUnknownSync(S)(x))Bad input becomes a defect.Schema.decodeUnknownEffect(S)(x).Decoding and encoding — pick your result style
Decode, then Effect.orElseSucceed(() => default)Malformed input is hidden behind a plausible value.A declared decoding default for absence; fail on malformed input.Construction and deliberate fallbacks
withConstructorDefault expected to fill a missing key while decodingIt applies to make / new only.withDecodingDefault / withDecodingDefaultKey.Default values
Never choosing onExcessPropertyUndeclared keys are stripped silently.Decide per boundary and test the policy.Parse options are boundary policy
Decoding the same value again at internal function boundariesCost with no new information, and a second place for drift.Decode once at ingress; pass the Type.Schema Type and Encoded mismatch
Schema.RedactedFromValue without disallowEncode on a model that is also encodedEvery derived codec writes the secret back in clear text.Set the option, or keep secrets out of encodable models.Secrets that arrive through a schema
The endpoint's success schema is the table rowA persistence detail becomes a published contract.Separate DTOs; map in the use case or repository.Handlers are lazy adapters

Anti-patterns: HTTP, RPC, and SQL ​

Anti-patternWhy it is wrongPreferred shapeDetails
A handler that runs an Effect, builds a client, or yields SqlClientBreaks request ownership and absorbs policy no other entry point shares.Return one use-case Effect.Handlers are lazy adapters
Decoding the success schema before checking the statusA 404 body is reported as a schema failure.filterStatusOk or matchStatus first.Three failure classes
retryTransient on a client that sends mutationsIt replays a POST as readily as a GET.Two derived clients; idempotency keys the server honors.Retries, time budgets, and cancellation
Fetching a user-supplied URL directlyServer-side request forgery.Allow-list scheme and port, reject non-public addresses, re-validate every redirect.User-controlled destinations
Treating a valid token, a reachable route, or a client middleware as authorizationNone of them checks actor, action, tenant, and resource.Authenticate in middleware (401); authorize in the use case (403).Authentication is not authorization
A catch-all that serializes error.message into a responseLeaks internals; defects are already sanitized to a content-free 500.Declared errors with safe fields only.Status mapping is part of the contract
Mapping every RPC failure to one "RPC failed" errorDiscards whether a retry is safe and whose bug it is.Keep the six categories distinguishable.Keep six failure categories distinguishable
Effect.ignore / Effect.catch around a step inside withTransactionThe body succeeds, so the earlier writes commit.Let the failure leave the body; recover outside.Transactions
Sending an email, webhook, or model call inside a transactionIrreversible work inside a body that can roll back or retry.Intent row in the transaction; deliver after commit.External effects after commit (outbox)
Mapping every UniqueViolation to one domain errorThe wrong conflict is reported when another constraint fires.Check which constraint fired.Normalizing errors at a repository boundary
Editing an applied migration, or merging a lower-numbered one lateThe migrator compares only the latest recorded id; the file never runs.Append-only, higher ids.Operating migrations
Assuming exactly-once deliveryA crash can occur after an external commit and before acknowledgement/journaling.Idempotency keys, uniqueness constraints, transactions/outbox, and at-least-once-safe handlers.Failure windows and honest guarantees

Anti-patterns: observability and tests ​

Anti-patternWhy it is wrongPreferred shapeDetails
Metric attributes with unbounded cardinality: employee, request, or session ids, raw URLs, error messagesEvery distinct combination is a stored, billed time series.Bounded vocabularies; detail in logs and span attributes.Cardinality
Instance data in a span name: hris.fetchCompBand E-1042Backends group and index by name.A stable operation name; ids in attributes.Cardinality
Logging whole objects: config, headers, bodies, rows, promptsSecrets and personal data reach a production store.Build each record from named, allow-listed fields.Privacy
Logger.layer([Logger.consoleJson]) as a drop-in format swapReplaces the whole logger set; logs stop becoming span events.Add Logger.tracerLogger, or { mergeWithExisting: true }.Installing and swapping loggers
Layer.merge(App, ObservabilityLayer); an exporter per request or inside a libraryThe application is built without the telemetry references; exporters compete.One exporter graph at the root, provided beneath the application.Layer order and shutdown
The collector in a readiness check; SLIs computed from sampled spansA telemetry outage becomes an outage; ratios are wrong.Readiness from serving dependencies; SLIs from unsampled metrics.Telemetry is not readiness, liveness, or an audit log
An Effect returned from plain it(...)Vitest never runs it: a green test that proves nothing.it.effect.When green means nothing
Tests that assert elapsed milliseconds or pretty-printed failure stringsFlaky, and brittle across releases.TestClock timelines; structural assertions on Exit, _tag, and fields.Assert typed failures as data
Effect.yieldNow sprinkled to order fibersTurn counting passes until something adds a turn.A handshake per phase.Synchronize on phases, not on turns
Fakes that never fail; socket, signal, or filesystem behavior asserted only against a fakeThe failure path and the adapter are never exercised.Fakes honor the contract; adapters get a real fixture.In-memory test runtimes shipped with Effect
An identity withTransaction fakeCommits nothing and rolls back nothing, so atomicity is untested.A repository contract suite against a real database.Verification levels
Using declare const in a supposedly runnable exampleIt type-checks only because the actual dependency is missing.Label it contextual, or supply a complete fixture/runnable program.—

Anti-patterns: durable systems and AI ​

Anti-patternWhy it is wrongPreferred shapeDetails
Renaming an Activity, workflow tag, or durable primitive while runs are in flightThe name is the journal key: replay re-executes or orphans.Freeze persisted names; add a V2 identity.Replay, versioning, and rollout
Reading a feature flag or ambient config in a workflow bodyThe replay can take a different path.Capture it in an Activity or the payload.Replay, versioning, and rollout
An idempotency key generated per attemptIt deduplicates nothing.Derive it from the intent or item id.Failure windows and honest guarantees
Treating a lock or lease as a fenceA stalled owner keeps running after losing the claim.The sink rejects stale owners.A lease is not a fence
Taking actor, tenant, or approval from model outputModel output is untrusted input.Trusted request context only.Production rules for model calls
An agent loop with no turn limit, or tool handlers with default concurrencyUnbounded cost and load.A bounded loop with a typed exhaustion error; a numeric concurrency.Bound every agentic loop

Edition 4.1 · audited 2026-11-19 against Effect 4.1.4.