effect
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEffect
Effect
Use current Effect v4 APIs and the production defaults in this skill. Established project conventions still take precedence unless the task is explicitly changing them.
使用本技能中的当前Effect v4 API和生产环境默认配置。除非任务明确要求更改,否则已确立的项目约定仍优先适用。
Source Rule
来源规则
Check these before guessing:
- the nearest and any project-local Effect practices doc
AGENTS.md - the project-pinned package source and version
effect - current upstream Effect source when the installed package does not answer the question
在猜测之前,请先检查以下内容:
- 最近的文件以及任何项目本地的Effect实践文档
AGENTS.md - 项目固定版本的包源码和版本信息
effect - 当已安装的包无法解答问题时,查看当前上游的Effect源码
Branch Chooser
分支选择器
Read only the branch references that match the task.
- Data models, schemas, brands, variants, optional keys, or decoders: read .
references/SCHEMA.md - Services, module surfaces, layers, runtime wiring, errors, , or test services: read
Effect.fn.references/SERVICES_LAYERS.md - Runtime config, env variables, , or
ConfigProvider: readlayerConfig.references/CONFIG.md - Retry, repeat, polling, backoff, jitter, rate-limit-aware policies, or pass loops: read .
references/SCHEDULING.md - Memoization, per-key TTL caches, deduplicating concurrent lookups, or request batching: read .
references/CACHING.md - Streams, event sources, async iterables, queues/pubsubs, pagination, backpressure, or stream consumers: read .
references/STREAMS.md - Outgoing HTTP calls, Effect HttpClient, status handling, or HTTP rate limiting: read .
references/HTTP_CLIENTS.md - Effect tests, time, sleeps, concurrency synchronization, or fakes: read .
references/TESTING.md
If a task spans several branches, read all matching files before editing.
仅阅读与任务匹配的分支参考内容:
- 数据模型、模式、品牌、变体、可选键或解码器:阅读。
references/SCHEMA.md - 服务、模块接口、层、运行时连接、错误、或测试服务:阅读
Effect.fn。references/SERVICES_LAYERS.md - 运行时配置、环境变量、或
ConfigProvider:阅读layerConfig。references/CONFIG.md - 重试、重复、轮询、退避、抖动、感知速率限制的策略或循环执行:阅读。
references/SCHEDULING.md - 记忆化、按键TTL缓存、并发查询去重或请求批处理:阅读。
references/CACHING.md - 流、事件源、异步迭代器、队列/发布订阅、分页、背压或流消费者:阅读。
references/STREAMS.md - 对外HTTP调用、Effect HttpClient、状态处理或HTTP速率限制:阅读。
references/HTTP_CLIENTS.md - Effect测试、时间、休眠、并发同步或模拟:阅读。
references/TESTING.md
如果任务涉及多个分支,请在编辑前阅读所有匹配的文件。
Core Defaults
核心默认配置
- Compose workflows with .
Effect.gen(function* () { ... }) - Define public service methods and non-trivial internal service methods with .
Effect.fn("Domain.operation") - Use only for internal helpers where stack-frame/span metadata is intentionally unnecessary.
Effect.fnUntraced - Prefer for application services when the codebase has not standardized on another current service-tag style.
Context.Service - Build real service implementations with and return
Layer.effect(Service, Effect.gen(...)).Service.of({ ... }) - Model records with plus a same-name
Schema.Struct(...).interface - Model typed Effect errors with .
Schema.TaggedErrorClass - Read runtime config through , not direct
Configaccess in application logic.process.env - Use for retry, repeat, polling, pacing, and backoff policies.
Schedule - Use for effectful sources that emit many values over time and need pull, backpressure, interruption, or transformation.
Stream - Prefer Effect HTTP client modules for outgoing HTTP in Effect applications when their typed errors, layers, and client transforms are useful.
- Prefer Effect-aware tests, explicit layers, and deterministic synchronization over sleeps.
- Prefer decoders and at untrusted boundaries; reserve throwing
schema.makeEffect(...)for trusted construction, and never use casts to skip validation.schema.make(...)
- 使用组合工作流。
Effect.gen(function* () { ... }) - 使用定义公共服务方法和非简易的内部服务方法。
Effect.fn("Domain.operation") - 仅在有意不需要栈帧/跨度元数据的内部辅助函数中使用。
Effect.fnUntraced - 当代码库尚未标准化其他当前服务标签风格时,优先为应用服务使用。
Context.Service - 使用构建真实的服务实现,并返回
Layer.effect(Service, Effect.gen(...))。Service.of({ ... }) - 使用搭配同名
Schema.Struct(...)建模记录。interface - 使用建模类型化的Effect错误。
Schema.TaggedErrorClass - 通过读取运行时配置,而非在应用逻辑中直接访问
Config。process.env - 使用处理重试、重复、轮询、调步和退避策略。
Schedule - 对于随时间emit多个值且需要拉取、背压、中断或转换的有副作用的源,使用。
Stream - 当Effect HTTP客户端模块的类型化错误、层和客户端转换有用时,优先在Effect应用中使用它们进行对外HTTP调用。
- 优先使用感知Effect的测试、显式层和确定性同步,而非休眠。
- 在不可信边界使用解码器和;仅在可信构造中保留抛出式的
schema.makeEffect(...),绝不要使用类型断言跳过验证。schema.make(...)
Quick Selection Guide
快速选择指南
- Ordinary object record: plus same-name
Schema.Struct(...).interface - Scalar ID/value object: constrained branded schema.
- Internal workflow decision or state: plus
Data.TaggedEnum<...>constructors and exhaustiveData.taggedEnum<...>().$match - Reusable boundary-crossing tagged variant: plus same-name
Schema.TaggedStruct(...).interface - Boundary-crossing tagged union: with
Schema.TaggedUnion(...),.cases, and.guards..match - External/custom discriminator such as :
typeplusSchema.Struct({ type: Schema.tag("variant"), ... })when union helpers are needed.Schema.toTaggedUnion("type") - Expected typed failure: .
Schema.TaggedErrorClass - Unknown boundary payload: .
Schema.decodeUnknownEffect(...) - Service boundary: plus
Context.Service<Service, Interface>()(...)plusLayer.effect(...).Service.of(...) - Public or non-trivial internal service method: .
Effect.fn("Domain.operation") - Runtime configuration: recipes read in layers; override with
Configin tests.ConfigProvider - Event source: consumed with
Streamand forked withStream.runForEach(...)in the owning layer.Effect.forkScoped - Queue-backed event source: for the producer boundary,
Queuefor consumers.Stream.fromQueue(...) - Broadcast event source: /
PubSuborStream.fromPubSub(...)for latest-value state.SubscriptionRef - Polling worker: , with typed pass failures handled before repeat.
runPass().pipe(Effect.repeat(Schedule.spaced(...))) - Retry transient operation: /
Effect.retry(...)with a boundedEffect.retryOrElse(...).Schedule - Keyed lookup cache with TTL and concurrent-lookup dedupe: prefer / exit-aware
Cache.make(...)when their lifecycle and eviction model fit.Cache.makeWith(...) - Memoize a single effect result: /
Effect.cached(...).Effect.cachedWithTTL(...) - Batch N keys into one backend call (only when a real batch endpoint exists): +
Effect.request(...).RequestResolver - HTTP request in an Effect application: prefer Effect plus request/response schema decoding.
HttpClient - HTTP transient retry: .
HttpClient.retryTransient(...) - Time-sensitive test: , not real sleeping.
TestClock - Concurrent/background test synchronization: ,
Deferred,Queue,Latch, or explicit test hooks.Ref
- 普通对象记录:搭配同名
Schema.Struct(...)。interface - 标量ID/值对象:受约束的品牌化模式。
- 内部工作流决策或状态:搭配
Data.TaggedEnum<...>构造函数和穷举Data.taggedEnum<...>()。$match - 可复用的跨边界标签变体:搭配同名
Schema.TaggedStruct(...)。interface - 跨边界标签联合:配合
Schema.TaggedUnion(...)、.cases和.guards。.match - 外部/自定义判别符(如):
type,当需要联合辅助工具时搭配Schema.Struct({ type: Schema.tag("variant"), ... })。Schema.toTaggedUnion("type") - 预期的类型化失败:。
Schema.TaggedErrorClass - 未知边界负载:。
Schema.decodeUnknownEffect(...) - 服务边界:搭配
Context.Service<Service, Interface>()(...)和Layer.effect(...)。Service.of(...) - 公共或非简易的内部服务方法:。
Effect.fn("Domain.operation") - 运行时配置:在层中读取配置;在测试中用
Config覆盖。ConfigProvider - 事件源:使用消费,并在所属层中用
Stream.runForEach(...)分叉。Effect.forkScoped - 队列支持的事件源:生产者边界使用,消费者使用
Queue。Stream.fromQueue(...) - 广播事件源:/
PubSub或用于最新值状态的Stream.fromPubSub(...)。SubscriptionRef - 轮询工作器:,在重复前处理类型化的执行失败。
runPass().pipe(Effect.repeat(Schedule.spaced(...))) - 重试临时操作:使用有界的
Schedule/Effect.retry(...)。Effect.retryOrElse(...) - 带TTL和并发查询去重的键值查找缓存:当/ 感知退出的
Cache.make(...)的生命周期和驱逐模型符合需求时优先使用。Cache.makeWith(...) - 记忆单个Effect结果:/
Effect.cached(...)。Effect.cachedWithTTL(...) - 将N个键批量处理为一次后端调用(仅当存在真实的批量端点时):+
Effect.request(...)。RequestResolver - Effect应用中的HTTP请求:优先使用Effect 搭配请求/响应模式解码。
HttpClient - HTTP临时重试:。
HttpClient.retryTransient(...) - 时间敏感的测试:,而非真实休眠。
TestClock - 并发/后台测试同步:、
Deferred、Queue、Latch或显式测试钩子。Ref
Boundary Rules
边界规则
- Keep HTTP handlers thin: decode input, read context, call services, map typed errors to transport responses.
- Keep business rules in services or domain functions, not transport handlers.
- Wrap HTTP clients, SDKs, CLIs, and external integrations in named effects at adapter boundaries.
- Decode persisted rows with Schema or SQL-specific helpers when values are not trivially trusted.
- Keep provider/network calls outside authoritative database transactions.
- Catch or retry only when the current boundary has a truthful response.
- Retry only when the operation has proven idempotency.
- Let exhausted failures remain visible unless the boundary has a real fallback.
- 保持HTTP处理程序精简:解码输入、读取上下文、调用服务、将类型化错误映射为传输响应。
- 将业务规则放在服务或领域函数中,而非传输处理程序里。
- 在适配器边界将HTTP客户端、SDK、CLI和外部集成包装为命名的effects。
- 当值并非完全可信时,使用Schema或SQL特定的辅助工具解码持久化行。
- 将提供者/网络调用放在权威数据库事务之外。
- 仅当当前边界有真实响应时才捕获或重试。
- 仅当操作已被证明是幂等的时才重试。
- 除非边界有真实的回退方案,否则让耗尽的失败保持可见。
Do Nots
禁止事项
- Do not use , non-null assertions, or unchecked casts to silence Effect typing problems.
as any - Do not introduce or
Schema.Classas default app data-modeling patterns.Schema.TaggedClass - Do not hand-roll error classes when
_tagfits.Schema.TaggedErrorClass - Do not use cause-level recovery when typed-error recovery is enough.
- Do not use or
Layer.mergeAll(...)as blind make-it-compile tools.provideMerge(...) - Do not hide required application authority, credentials, persistence, transports, or external services behind defaults.
Context.Reference - Do not add arbitrary to tests when a deterministic synchronization primitive is available.
Effect.sleep(...) - Do not hand-roll Map/TTL/prune caches or in-flight dedupe when fits.
effect/Cache
- 不要使用、非空断言或未检查的类型断言来掩盖Effect的类型问题。
as any - 不要将或
Schema.Class作为应用数据建模的默认模式。Schema.TaggedClass - 当适用时,不要手动编写
Schema.TaggedErrorClass错误类。_tag - 当类型化错误恢复足够时,不要使用原因级恢复。
- 不要将或
Layer.mergeAll(...)作为盲目让代码编译的工具。provideMerge(...) - 不要将所需的应用权限、凭据、持久化、传输或外部服务隐藏在默认值之后。
Context.Reference - 当有确定性同步原语可用时,不要在测试中添加任意的。
Effect.sleep(...) - 当适用时,不要手动编写Map/TTL/修剪缓存或进行飞行中请求去重。
effect/Cache