@@ -4,18 +4,18 @@ Practical reference for new and migrated Effect code in `packages/opencode`. |
4 | 4 |
|
5 | 5 | ## Choose scope |
6 | 6 |
|
7 | | -Use the shared runtime for process-wide services with one lifecycle for the whole app. |
| 7 | +Use `InstanceState` (from `src/effect/instance-state.ts`) for services that need per-directory state, per-instance cleanup, or project-bound background work. InstanceState uses a `ScopedCache` keyed by directory, so each open project gets its own copy of the state that is automatically cleaned up on disposal. |
8 | 8 |
|
9 | | -Use `src/effect/instances.ts` for services that are created per directory or need `InstanceContext`, per-project state, or per-instance cleanup. |
| 9 | +Use `makeRunPromise` (from `src/effect/run-service.ts`) to create a per-service `ManagedRuntime` that lazily initializes and shares layers via a global `memoMap`. |
10 | 10 |
|
11 | | -- Shared runtime: config readers, stateless helpers, global clients |
12 | | -- Instance-scoped: watchers, per-project caches, session state, project-bound background work |
| 11 | +- Global services (no per-directory state): Account, Auth, Installation, Truncate |
| 12 | +- Instance-scoped (per-directory state via InstanceState): File, FileTime, FileWatcher, Format, Permission, Question, Skill, Snapshot, Vcs, ProviderAuth |
13 | 13 |
|
14 | | -Rule of thumb: if two open directories should not share one copy of the service, it belongs in `Instances`. |
| 14 | +Rule of thumb: if two open directories should not share one copy of the service, it needs `InstanceState`. |
15 | 15 |
|
16 | 16 | ## Service shape |
17 | 17 |
|
18 | | -For a fully migrated module, use the public namespace directly: |
| 18 | +Every service follows the same pattern — a single namespace with the service definition, layer, `runPromise`, and async facade functions: |
19 | 19 |
|
20 | 20 | ```ts |
21 | 21 | export namespace Foo { |
@@ -28,53 +28,52 @@ export namespace Foo { |
28 | 28 | export const layer = Layer.effect( |
29 | 29 | Service, |
30 | 30 | Effect.gen(function* () { |
31 | | - return Service.of({ |
32 | | - get: Effect.fn("Foo.get")(function* (id) { |
33 | | - return yield* ... |
34 | | - }), |
| 31 | + // For instance-scoped services: |
| 32 | + const state = yield* InstanceState.make<State>( |
| 33 | + Effect.fn("Foo.state")(() => Effect.succeed({ ... })), |
| 34 | + ) |
| 35 | + |
| 36 | + const get = Effect.fn("Foo.get")(function* (id: FooID) { |
| 37 | + const s = yield* InstanceState.get(state) |
| 38 | + // ... |
35 | 39 | }) |
| 40 | + |
| 41 | + return Service.of({ get }) |
36 | 42 | }), |
37 | 43 | ) |
38 | 44 |
|
39 | | - export const defaultLayer = layer.pipe(Layer.provide(FooRepo.defaultLayer)) |
| 45 | + // Optional: wire dependencies |
| 46 | + export const defaultLayer = layer.pipe(Layer.provide(FooDep.layer)) |
| 47 | + |
| 48 | + // Per-service runtime (inside the namespace) |
| 49 | + const runPromise = makeRunPromise(Service, defaultLayer) |
| 50 | + |
| 51 | + // Async facade functions |
| 52 | + export async function get(id: FooID) { |
| 53 | + return runPromise((svc) => svc.get(id)) |
| 54 | + } |
40 | 55 | } |
41 | 56 | ``` |
42 | 57 |
|
43 | 58 | Rules: |
44 | 59 |
|
45 | | -- Keep `Interface`, `Service`, `layer`, and `defaultLayer` on the owning namespace |
46 | | -- Export `defaultLayer` only when wiring dependencies is useful |
47 | | -- Use the direct namespace form once the module is fully migrated |
48 | | - |
49 | | -## Temporary mixed-mode pattern |
| 60 | +- Keep everything in one namespace, one file — no separate `service.ts` / `index.ts` split |
| 61 | +- `runPromise` goes inside the namespace (not exported unless tests need it) |
| 62 | +- Facade functions are plain `async function` — no `fn()` wrappers |
| 63 | +- Use `Effect.fn("Namespace.method")` for all Effect functions (for tracing) |
| 64 | +- No `Layer.fresh` — InstanceState handles per-directory isolation |
50 | 65 |
|
51 | | -Prefer a single namespace whenever possible. |
| 66 | +## Schema → Zod interop |
52 | 67 |
|
53 | | -Use a `*Effect` namespace only when there is a real mixed-mode split, usually because a legacy boundary facade still exists or because merging everything immediately would create awkward cycles. |
| 68 | +When a service uses Effect Schema internally but needs Zod schemas for the HTTP layer, derive Zod from Schema using the `zod()` helper from `@/util/effect-zod`: |
54 | 69 |
|
55 | 70 | ```ts |
56 | | -export namespace FooEffect { |
57 | | - export interface Interface { |
58 | | - readonly get: (id: FooID) => Effect.Effect<Foo, FooError> |
59 | | - } |
60 | | - |
61 | | - export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/Foo") {} |
| 71 | +import { zod } from "@/util/effect-zod" |
62 | 72 |
|
63 | | - export const layer = Layer.effect(...) |
64 | | -} |
65 | | -``` |
66 | | - |
67 | | -Then keep the old boundary thin: |
68 | | - |
69 | | -```ts |
70 | | -export namespace Foo { |
71 | | - export function get(id: FooID) { |
72 | | - return runtime.runPromise(FooEffect.Service.use((svc) => svc.get(id))) |
73 | | - } |
74 | | -} |
| 73 | +export const ZodInfo = zod(Info) // derives z.ZodType from Schema.Union |
75 | 74 | ``` |
76 | 75 |
|
77 | | -Remove the `Effect` suffix when the boundary split is gone. |
| 76 | +See `Auth.ZodInfo` for the canonical example. |
78 | 77 |
|
79 | 78 | ## Scheduled Tasks |
80 | 79 |
|
@@ -107,30 +106,30 @@ That is fine for leaf files like `schema.ts`. Keep the service surface in the ow |
107 | 106 |
|
108 | 107 | ## Migration checklist |
109 | 108 |
|
110 | | -Done now: |
111 | | - |
112 | | -- [x] `AccountEffect` (mixed-mode) |
113 | | -- [x] `AuthEffect` (mixed-mode) |
114 | | -- [x] `TruncateEffect` (mixed-mode) |
115 | | -- [x] `Question` |
116 | | -- [x] `PermissionNext` |
117 | | -- [x] `ProviderAuth` |
118 | | -- [x] `FileWatcher` |
119 | | -- [x] `FileTime` |
120 | | -- [x] `Format` |
121 | | -- [x] `Vcs` |
122 | | -- [x] `Skill` |
123 | | -- [x] `Discovery` |
124 | | -- [x] `File` |
125 | | -- [x] `Snapshot` |
| 109 | +Fully migrated (single namespace, InstanceState where needed, flattened facade): |
| 110 | + |
| 111 | +- [x] `Account` — `account/index.ts` |
| 112 | +- [x] `Auth` — `auth/index.ts` (uses `zod()` helper for Schema→Zod interop) |
| 113 | +- [x] `File` — `file/index.ts` |
| 114 | +- [x] `FileTime` — `file/time.ts` |
| 115 | +- [x] `FileWatcher` — `file/watcher.ts` |
| 116 | +- [x] `Format` — `format/index.ts` |
| 117 | +- [x] `Installation` — `installation/index.ts` |
| 118 | +- [x] `Permission` — `permission/index.ts` |
| 119 | +- [x] `ProviderAuth` — `provider/auth.ts` |
| 120 | +- [x] `Question` — `question/index.ts` |
| 121 | +- [x] `Skill` — `skill/index.ts` |
| 122 | +- [x] `Snapshot` — `snapshot/index.ts` |
| 123 | +- [x] `Truncate` — `tool/truncate.ts` |
| 124 | +- [x] `Vcs` — `project/vcs.ts` |
| 125 | +- [x] `Discovery` — `skill/discovery.ts` |
126 | 126 |
|
127 | 127 | Still open and likely worth migrating: |
128 | 128 |
|
129 | 129 | - [ ] `Plugin` |
130 | 130 | - [ ] `ToolRegistry` |
131 | 131 | - [ ] `Pty` |
132 | 132 | - [ ] `Worktree` |
133 | | -- [ ] `Installation` |
134 | 133 | - [ ] `Bus` |
135 | 134 | - [ ] `Command` |
136 | 135 | - [ ] `Config` |
|