GeezerVison is showing github.com — Refactor workspace service boundaries (#25152) · Verdagon/opencode@0e9d928
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/control-plane/workspace.ts -40Lines changed: 0 additions & 40 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { Context, Effect, FiberMap, Layer, Schema, Stream } from "effect" | 2 | 2 | import { FetchHttpClient, HttpBody, HttpClient, HttpClientError, HttpClientRequest } from "effect/unstable/http" | 3 | | -import { fn } from "@/util/fn" | 4 | 3 | import { Database } from "@/storage/db" | 5 | 4 | import { asc } from "drizzle-orm" | 6 | 5 | import { eq } from "drizzle-orm" | @@ -24,7 +23,6 @@ import { Session } from "@/session/session" | 24 | 23 | import { SessionTable } from "@/session/session.sql" | 25 | 24 | import { SessionID } from "@/session/schema" | 26 | 25 | import { errorData } from "@/util/error" | 27 | | -import { makeRuntime } from "@/effect/run-service" | 28 | 26 | import { waitEvent } from "./util" | 29 | 27 | import { WorkspaceContext } from "./workspace-context" | 30 | 28 | import { NonNegativeInt, withStatics } from "@/util/schema" | @@ -857,42 +855,4 @@ function route(url: string | URL, path: string) { | 857 | 855 | return next | 858 | 856 | } | 859 | 857 |
| 860 | | -const { runPromise, runSync } = makeRuntime(Service, defaultLayer) | 861 | | - | 862 | | -export const create = fn(CreateInput.zod, (input) => runPromise((svc) => svc.create(input))) | 863 | | - | 864 | | -export const sessionRestore = fn(SessionRestoreInput.zod, (input) => runPromise((svc) => svc.sessionRestore(input))) | 865 | | - | 866 | | -export function list(project: Project.Info) { | 867 | | - return Database.use((db) => | 868 | | - db | 869 | | - .select() | 870 | | - .from(WorkspaceTable) | 871 | | - .where(eq(WorkspaceTable.project_id, project.id)) | 872 | | - .all() | 873 | | - .map(fromRow) | 874 | | - .sort((a, b) => a.id.localeCompare(b.id)), | 875 | | - ) | 876 | | -} | 877 | | - | 878 | | -export const get = fn(WorkspaceID.zod, (id) => runPromise((svc) => svc.get(id))) | 879 | | - | 880 | | -export const remove = fn(WorkspaceID.zod, (id) => runPromise((svc) => svc.remove(id))) | 881 | | - | 882 | | -export function status() { | 883 | | - return runSync((svc) => svc.status()) | 884 | | -} | 885 | | - | 886 | | -export function isSyncing(workspaceID: WorkspaceID) { | 887 | | - return runSync((svc) => svc.isSyncing(workspaceID)) | 888 | | -} | 889 | | - | 890 | | -export function waitForSync(workspaceID: WorkspaceID, state: Record<string, number>, signal?: AbortSignal) { | 891 | | - return runPromise((svc) => svc.waitForSync(workspaceID, state, signal)) | 892 | | -} | 893 | | - | 894 | | -export function startWorkspaceSyncing(projectID: ProjectID) { | 895 | | - void runPromise((svc) => svc.startWorkspaceSyncing(projectID)) | 896 | | -} | 897 | | - | 898 | 858 | export * as Workspace from "./workspace" |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/server/fence.ts +17-9Lines changed: 17 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,8 @@ import { EventSequenceTable } from "@/sync/event.sql" | 5 | 5 | import { Workspace } from "@/control-plane/workspace" | 6 | 6 | import type { WorkspaceID } from "@/control-plane/schema" | 7 | 7 | import * as Log from "@opencode-ai/core/util/log" | | 8 | +import { AppRuntime } from "@/effect/app-runtime" | | 9 | +import { Effect } from "effect" | 8 | 10 |
| 9 | 11 | const HEADER = "x-opencode-sync" | 10 | 12 | type State = Record<string, number> | @@ -54,18 +56,24 @@ export function parse(headers: Headers) { | 54 | 56 | ) as State | 55 | 57 | } | 56 | 58 |
| 57 | | -export async function wait(workspaceID: WorkspaceID, state: State, signal?: AbortSignal) { | 58 | | - log.info("waiting for state", { | 59 | | - workspaceID, | 60 | | - state, | 61 | | - }) | 62 | | - await Workspace.waitForSync(workspaceID, state, signal) | 63 | | - log.info("state fully synced", { | 64 | | - workspaceID, | 65 | | - state, | | 59 | +export function waitEffect(workspaceID: WorkspaceID, state: State, signal?: AbortSignal) { | | 60 | + return Effect.gen(function* () { | | 61 | + log.info("waiting for state", { | | 62 | + workspaceID, | | 63 | + state, | | 64 | + }) | | 65 | + yield* Workspace.Service.use((workspace) => workspace.waitForSync(workspaceID, state, signal)) | | 66 | + log.info("state fully synced", { | | 67 | + workspaceID, | | 68 | + state, | | 69 | + }) | 66 | 70 | }) | 67 | 71 | } | 68 | 72 |
| | 73 | +export async function wait(workspaceID: WorkspaceID, state: State, signal?: AbortSignal) { | | 74 | + await AppRuntime.runPromise(waitEffect(workspaceID, state, signal)) | | 75 | +} | | 76 | + | 69 | 77 | export const FenceMiddleware: MiddlewareHandler = async (c, next) => { | 70 | 78 | if (c.req.method === "GET" || c.req.method === "HEAD" || c.req.method === "OPTIONS") return next() | 71 | 79 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/server/proxy.ts +9-9Lines changed: 9 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ import * as Log from "@opencode-ai/core/util/log" | 4 | 4 | import * as Fence from "./fence" | 5 | 5 | import type { WorkspaceID } from "@/control-plane/schema" | 6 | 6 | import { Workspace } from "@/control-plane/workspace" | | 7 | +import { AppRuntime } from "@/effect/app-runtime" | 7 | 8 | import { ProxyUtil } from "./proxy-util" | 8 | 9 | import { Effect, Stream } from "effect" | 9 | 10 | import { FetchHttpClient, HttpBody, HttpClient, HttpClientRequest } from "effect/unstable/http" | @@ -69,18 +70,17 @@ function statusText(response: unknown) { | 69 | 70 | } | 70 | 71 |
| 71 | 72 | export function httpEffect(url: string | URL, extra: HeadersInit | undefined, req: Request, workspaceID: WorkspaceID) { | 72 | | - if (!Workspace.isSyncing(workspaceID)) { | 73 | | - return Effect.succeed( | 74 | | - new Response(`broken sync connection for workspace: ${workspaceID}`, { | | 73 | + return Effect.gen(function* () { | | 74 | + const syncing = yield* Workspace.Service.use((workspace) => workspace.isSyncing(workspaceID)) | | 75 | + if (!syncing) { | | 76 | + return new Response(`broken sync connection for workspace: ${workspaceID}`, { | 75 | 77 | status: 503, | 76 | 78 | headers: { | 77 | 79 | "content-type": "text/plain; charset=utf-8", | 78 | 80 | }, | 79 | | - }), | 80 | | - ) | 81 | | - } | | 81 | + }) | | 82 | + } | 82 | 83 |
| 83 | | - return Effect.gen(function* () { | 84 | 84 | const response = yield* HttpClient.execute( | 85 | 85 | HttpClientRequest.make(req.method as never)(url, { | 86 | 86 | headers: ProxyUtil.headers(req, extra), | @@ -100,7 +100,7 @@ export function httpEffect(url: string | URL, extra: HeadersInit | undefined, re | 100 | 100 | next.delete("content-encoding") | 101 | 101 | next.delete("content-length") | 102 | 102 |
| 103 | | - if (sync) yield* Effect.promise(() => Fence.wait(workspaceID, sync, req.signal)) | | 103 | + if (sync) yield* Fence.waitEffect(workspaceID, sync, req.signal) | 104 | 104 | const body = yield* Stream.toReadableStreamEffect(response.stream.pipe(Stream.catchCause(() => Stream.empty))) | 105 | 105 | return new Response(body, { | 106 | 106 | status: response.status, | @@ -114,7 +114,7 @@ export function httpEffect(url: string | URL, extra: HeadersInit | undefined, re | 114 | 114 | } | 115 | 115 |
| 116 | 116 | export function http(url: string | URL, extra: HeadersInit | undefined, req: Request, workspaceID: WorkspaceID) { | 117 | | - return Effect.runPromise(httpEffect(url, extra, req, workspaceID)) | | 117 | + return AppRuntime.runPromise(httpEffect(url, extra, req, workspaceID)) | 118 | 118 | } | 119 | 119 |
| 120 | 120 | export function websocket( | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/server/routes/control/workspace.ts +25-12Lines changed: 25 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { Hono } from "hono" | 2 | 2 | import { describeRoute, resolver, validator } from "hono-openapi" | 3 | 3 | import z from "zod" | | 4 | +import { Effect } from "effect" | 4 | 5 | import { listAdaptors } from "@/control-plane/adaptors" | 5 | 6 | import { Workspace } from "@/control-plane/workspace" | | 7 | +import { AppRuntime } from "@/effect/app-runtime" | 6 | 8 | import { WorkspaceAdaptorEntry } from "@/control-plane/types" | 7 | 9 | import { zodObject } from "@/util/effect-zod" | 8 | 10 | import { Instance } from "@/project/instance" | @@ -62,10 +64,14 @@ export const WorkspaceRoutes = lazy(() => | 62 | 64 | ), | 63 | 65 | async (c) => { | 64 | 66 | const body = c.req.valid("json") as Omit<Workspace.CreateInput, "projectID"> | 65 | | - const workspace = await Workspace.create({ | 66 | | - projectID: Instance.project.id, | 67 | | - ...body, | 68 | | - }) | | 67 | + const workspace = await AppRuntime.runPromise( | | 68 | + Workspace.Service.use((svc) => | | 69 | + svc.create({ | | 70 | + projectID: Instance.project.id, | | 71 | + ...body, | | 72 | + }), | | 73 | + ), | | 74 | + ) | 69 | 75 | return c.json(workspace) | 70 | 76 | }, | 71 | 77 | ) | @@ -87,7 +93,7 @@ export const WorkspaceRoutes = lazy(() => | 87 | 93 | }, | 88 | 94 | }), | 89 | 95 | async (c) => { | 90 | | - return c.json(Workspace.list(Instance.project)) | | 96 | + return c.json(await AppRuntime.runPromise(Workspace.Service.use((svc) => svc.list(Instance.project)))) | 91 | 97 | }, | 92 | 98 | ) | 93 | 99 | .get( | @@ -108,8 +114,11 @@ export const WorkspaceRoutes = lazy(() => | 108 | 114 | }, | 109 | 115 | }), | 110 | 116 | async (c) => { | 111 | | - const ids = new Set(Workspace.list(Instance.project).map((item) => item.id)) | 112 | | - return c.json(Workspace.status().filter((item) => ids.has(item.workspaceID))) | | 117 | + const result = await AppRuntime.runPromise( | | 118 | + Workspace.Service.use((svc) => Effect.all([svc.list(Instance.project), svc.status()])), | | 119 | + ) | | 120 | + const ids = new Set(result[0].map((item) => item.id)) | | 121 | + return c.json(result[1].filter((item) => ids.has(item.workspaceID))) | 113 | 122 | }, | 114 | 123 | ) | 115 | 124 | .delete( | @@ -138,7 +147,7 @@ export const WorkspaceRoutes = lazy(() => | 138 | 147 | ), | 139 | 148 | async (c) => { | 140 | 149 | const { id } = c.req.valid("param") | 141 | | - return c.json(await Workspace.remove(id)) | | 150 | + return c.json(await AppRuntime.runPromise(Workspace.Service.use((svc) => svc.remove(id)))) | 142 | 151 | }, | 143 | 152 | ) | 144 | 153 | .post( | @@ -174,10 +183,14 @@ export const WorkspaceRoutes = lazy(() => | 174 | 183 | directory: Instance.directory, | 175 | 184 | }) | 176 | 185 | try { | 177 | | - const result = await Workspace.sessionRestore({ | 178 | | - workspaceID: id, | 179 | | - ...body, | 180 | | - }) | | 186 | + const result = await AppRuntime.runPromise( | | 187 | + Workspace.Service.use((svc) => | | 188 | + svc.sessionRestore({ | | 189 | + workspaceID: id, | | 190 | + ...body, | | 191 | + }), | | 192 | + ), | | 193 | + ) | 181 | 194 | log.info("session restore route complete", { | 182 | 195 | workspaceID: id, | 183 | 196 | sessionID: body.sessionID, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/server/routes/instance/httpapi/handlers/sync.ts +8-3Lines changed: 8 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { startWorkspaceSyncing } from "@/control-plane/workspace" | | 1 | +import { Workspace } from "@/control-plane/workspace" | 2 | 2 | import * as InstanceState from "@/effect/instance-state" | 3 | 3 | import { Database } from "@/storage/db" | 4 | 4 | import { SyncEvent } from "@/sync" | @@ -9,15 +9,20 @@ import { eq } from "drizzle-orm" | 9 | 9 | import { lte } from "drizzle-orm" | 10 | 10 | import { not } from "drizzle-orm" | 11 | 11 | import { or } from "drizzle-orm" | 12 | | -import { Effect } from "effect" | | 12 | +import { Effect, Scope } from "effect" | 13 | 13 | import { HttpApiBuilder } from "effect/unstable/httpapi" | 14 | 14 | import { InstanceHttpApi } from "../api" | 15 | 15 | import { HistoryPayload, ReplayPayload } from "../groups/sync" | 16 | 16 |
| 17 | 17 | export const syncHandlers = HttpApiBuilder.group(InstanceHttpApi, "sync", (handlers) => | 18 | 18 | Effect.gen(function* () { | | 19 | + const workspace = yield* Workspace.Service | | 20 | + const scope = yield* Scope.Scope | | 21 | + | 19 | 22 | const start = Effect.fn("SyncHttpApi.start")(function* () { | 20 | | - startWorkspaceSyncing((yield* InstanceState.context).project.id) | | 23 | + yield* workspace | | 24 | + .startWorkspaceSyncing((yield* InstanceState.context).project.id) | | 25 | + .pipe(Effect.ignore, Effect.forkIn(scope)) | 21 | 26 | return true | 22 | 27 | }) | 23 | 28 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/server/routes/instance/httpapi/middleware/workspace-routing.ts +32-25Lines changed: 32 additions & 25 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,9 +75,9 @@ function shouldStayOnControlPlane(request: HttpServerRequest.HttpServerRequest, | 75 | 75 | function resolveWorkspace( | 76 | 76 | id: WorkspaceID | undefined, | 77 | 77 | envWorkspaceID: WorkspaceID | undefined, | 78 | | -): Effect.Effect<Workspace.Info | void> { | | 78 | +): Effect.Effect<Workspace.Info | void, never, Workspace.Service> { | 79 | 79 | if (!id || envWorkspaceID) return Effect.void | 80 | | - return Effect.promise(() => Workspace.get(id)) | | 80 | + return Workspace.Service.use((workspace) => workspace.get(id)) | 81 | 81 | } | 82 | 82 |
| 83 | 83 | function missingWorkspaceResponse(id: WorkspaceID): HttpServerResponse.HttpServerResponse { | @@ -99,9 +99,9 @@ function proxyRemote( | 99 | 99 | workspace: Workspace.Info, | 100 | 100 | target: RemoteTarget, | 101 | 101 | url: URL, | 102 | | -): Effect.Effect<HttpServerResponse.HttpServerResponse, never, Socket.WebSocketConstructor> { | | 102 | +): Effect.Effect<HttpServerResponse.HttpServerResponse, never, Socket.WebSocketConstructor | Workspace.Service> { | 103 | 103 | return Effect.gen(function* () { | 104 | | - const syncing = yield* Effect.sync(() => Workspace.isSyncing(workspace.id)) | | 104 | + const syncing = yield* Workspace.Service.use((svc) => svc.isSyncing(workspace.id)) | 105 | 105 | if (!syncing) { | 106 | 106 | return HttpServerResponse.text(`broken sync connection for workspace: ${workspace.id}`, { | 107 | 107 | status: 503, | @@ -113,10 +113,17 @@ function proxyRemote( | 113 | 113 | if (headers["upgrade"]?.toLowerCase() === "websocket") return yield* HttpApiProxy.websocket(request, proxyURL) | 114 | 114 | const response = yield* HttpApiProxy.http(proxyURL, target.headers, request) | 115 | 115 | const sync = Fence.parse(new Headers(response.headers)) | 116 | | - if (sync) | 117 | | - yield* Effect.promise(() => | 118 | | - Fence.wait(workspace.id, sync, request.source instanceof Request ? request.source.signal : undefined), | | 116 | + if (sync) { | | 117 | + const syncFailure = yield* Fence.waitEffect( | | 118 | + workspace.id, | | 119 | + sync, | | 120 | + request.source instanceof Request ? request.source.signal : undefined, | | 121 | + ).pipe( | | 122 | + Effect.as(undefined), | | 123 | + Effect.catch((error) => Effect.succeed(HttpServerResponse.text(error.message, { status: 503 }))), | 119 | 124 | ) | | 125 | + if (syncFailure) return syncFailure | | 126 | + } | 120 | 127 | return response | 121 | 128 | }) | 122 | 129 | } | @@ -125,7 +132,7 @@ function planWorkspaceRequest( | 125 | 132 | request: HttpServerRequest.HttpServerRequest, | 126 | 133 | url: URL, | 127 | 134 | workspace: Workspace.Info, | 128 | | -): Effect.Effect<RequestPlan> { | | 135 | +): Effect.Effect<RequestPlan, never, Workspace.Service> { | 129 | 136 | return Effect.gen(function* () { | 130 | 137 | const target = yield* resolveTarget(workspace) | 131 | 138 | if (target.type === "remote") return RequestPlan.Remote({ request, workspace, target, url }) | @@ -136,7 +143,7 @@ function planWorkspaceRequest( | 136 | 143 | function planRequest( | 137 | 144 | request: HttpServerRequest.HttpServerRequest, | 138 | 145 | sessionWorkspaceID?: WorkspaceID, | 139 | | -): Effect.Effect<RequestPlan> { | | 146 | +): Effect.Effect<RequestPlan, never, Workspace.Service> { | 140 | 147 | return Effect.gen(function* () { | 141 | 148 | const url = requestURL(request) | 142 | 149 | const envWorkspaceID = configuredWorkspaceID() | @@ -158,7 +165,7 @@ function planRequest( | 158 | 165 | function routeWorkspace<E>( | 159 | 166 | effect: Effect.Effect<HttpServerResponse.HttpServerResponse, E, WorkspaceRouteContext>, | 160 | 167 | plan: RequestPlan, | 161 | | -): Effect.Effect<HttpServerResponse.HttpServerResponse, E, Socket.WebSocketConstructor> { | | 168 | +): Effect.Effect<HttpServerResponse.HttpServerResponse, E, Socket.WebSocketConstructor | Workspace.Service> { | 162 | 169 | return RequestPlan.$match(plan, { | 163 | 170 | MissingWorkspace: ({ workspaceID }) => Effect.succeed(missingWorkspaceResponse(workspaceID)), | 164 | 171 | Remote: ({ request, workspace, target, url }) => proxyRemote(request, workspace, target, url), | @@ -167,50 +174,50 @@ function routeWorkspace<E>( | 167 | 174 | }) | 168 | 175 | } | 169 | 176 |
| 170 | | -function routeWorkspaceRequest<E>( | 171 | | - effect: Effect.Effect<HttpServerResponse.HttpServerResponse, E, WorkspaceRouteContext>, | 172 | | - request: HttpServerRequest.HttpServerRequest, | 173 | | - sessionWorkspaceID?: WorkspaceID, | 174 | | -): Effect.Effect<HttpServerResponse.HttpServerResponse, E, Socket.WebSocketConstructor> { | 175 | | - return Effect.flatMap(planRequest(request, sessionWorkspaceID), (plan) => routeWorkspace(effect, plan)) | 176 | | -} | 177 | | - | 178 | 177 | function routeHttpApiWorkspace<E>( | 179 | 178 | effect: Effect.Effect<HttpServerResponse.HttpServerResponse, E, WorkspaceRouteContext>, | 180 | 179 | ): Effect.Effect< | 181 | 180 | HttpServerResponse.HttpServerResponse, | 182 | 181 | E, | 183 | | - Session.Service | HttpServerRequest.HttpServerRequest | Socket.WebSocketConstructor | | 182 | + Session.Service | Workspace.Service | HttpServerRequest.HttpServerRequest | Socket.WebSocketConstructor | 184 | 183 | > { | 185 | 184 | return Effect.gen(function* () { | 186 | 185 | const request = yield* HttpServerRequest.HttpServerRequest | 187 | 186 | const sessionID = getWorkspaceRouteSessionID(requestURL(request)) | 188 | 187 | const session = sessionID | 189 | 188 | ? yield* Session.Service.use((svc) => svc.get(sessionID)).pipe(Effect.catchDefect(() => Effect.void)) | 190 | 189 | : undefined | 191 | | - return yield* routeWorkspaceRequest(effect, request, session?.workspaceID) | | 190 | + const plan = yield* planRequest(request, session?.workspaceID) | | 191 | + return yield* routeWorkspace(effect, plan) | 192 | 192 | }) | 193 | 193 | } | 194 | 194 |
| 195 | 195 | export const workspaceRoutingLayer = Layer.effect( | 196 | 196 | WorkspaceRoutingMiddleware, | 197 | 197 | Effect.gen(function* () { | 198 | 198 | const makeWebSocket = yield* Socket.WebSocketConstructor | | 199 | + const workspace = yield* Workspace.Service | 199 | 200 | return WorkspaceRoutingMiddleware.of((effect) => | 200 | | - routeHttpApiWorkspace(effect).pipe(Effect.provideService(Socket.WebSocketConstructor, makeWebSocket)), | | 201 | + routeHttpApiWorkspace(effect).pipe( | | 202 | + Effect.provideService(Socket.WebSocketConstructor, makeWebSocket), | | 203 | + Effect.provideService(Workspace.Service, workspace), | | 204 | + ), | 201 | 205 | ) | 202 | 206 | }), | 203 | 207 | ) | 204 | 208 |
| 205 | 209 | export const workspaceRouterMiddleware = HttpRouter.middleware<{ provides: WorkspaceRouteContext }>()( | 206 | 210 | Effect.gen(function* () { | 207 | 211 | const makeWebSocket = yield* Socket.WebSocketConstructor | | 212 | + const workspace = yield* Workspace.Service | 208 | 213 | return (effect) => | 209 | 214 | Effect.gen(function* () { | 210 | 215 | const request = yield* HttpServerRequest.HttpServerRequest | 211 | | - return yield* routeWorkspaceRequest(effect, request).pipe( | 212 | | - Effect.provideService(Socket.WebSocketConstructor, makeWebSocket), | 213 | | - ) | 214 | | - }) | | 216 | + const plan = yield* planRequest(request) | | 217 | + return yield* routeWorkspace(effect, plan) | | 218 | + }).pipe( | | 219 | + Effect.provideService(Socket.WebSocketConstructor, makeWebSocket), | | 220 | + Effect.provideService(Workspace.Service, workspace), | | 221 | + ) | 215 | 222 | }), | 216 | 223 | ) |
|