The proxy version buys ~one character per handler at the cost of: - a recursive `Captured<T>` type with a hand-curated `CapturedOpaque` leaf list (branded IDs already bit us once); - a runtime Proxy that walks arbitrary object shapes and has to distinguish plain nested objects from class instances, with new stdlib types as potential edge cases; - a type-level lie where the declared method signature says `Effect<_, InternalError>` but the underlying method actually returns `Effect<_, StorageError>` and the proxy rewrites it. Replace with: every handler wraps its `Effect.gen(function*(){...})` body with `capture(...)`. One line at the top, explicit, honest, and self-enforcing — TypeScript rejects handlers that forget because `StorageError` isn't in the group's `.addError` list. `scripts/wrap-handlers-with-capture.ts` handled the mechanical part (45 handler bodies across core + every plugin). The rest: - Deleted `withCapture`, `Captured<T>`, `CapturedOpaque`, `isPlainObject` from `packages/core/api/src/observability.ts` and its exports. `capture` stays. - Service tags reverted from `Captured<XxxPluginExtension>` to `XxxPluginExtension` / raw `Executor`. - Host wiring (`apps/cloud/src/api/protected.ts`, `apps/local/src/server/main.ts`, cloud test harness) dropped the `const wrapped = withCapture(executor)` intro line; plain `executor.X` everywhere. - `.addError(InternalError)` added to the `executions` + `scope` API groups (they didn't previously declare it). - Handler tests (mcp + google-discovery) dropped `withCapture` import + wrap. - `notes/error-handling.md` rewritten around the per-handler `capture(...)` model. 34/34 typecheck. Unit tests + handler tests + cloud workers tests all green on clean runs. The remaining cloud node-test flakiness (sometimes-failing `sources.remove` / `openapi.updateSource`) is pre-existing PGlite test-harness setup racing, unrelated to this refactor.
Showing the changes to packages/plugins/openapi/src/api (1 of 23 files). Show all
Unified · Split
| @@ -3,7 +3,7 @@ import { Context, Effect } from "effect"; · 2 unmodified lines | |||
| 3 | 3 | ||
| 4 | import { runOAuthCallback } from "@executor/plugin-oauth2/http"; | 4 | import { runOAuthCallback } from "@executor/plugin-oauth2/http"; |
| 5 | 5 | ||
| 6 | import { addGroup, InternalError, type Captured } from "@executor/api"; | 6 | import { addGroup, capture, InternalError } from "@executor/api"; |
| 7 | import { OpenApiOAuthError } from "../sdk/errors"; | 7 | import { OpenApiOAuthError } from "../sdk/errors"; |
| 8 | import type { | 8 | import type { |
| 9 | OpenApiPluginExtension, | 9 | OpenApiPluginExtension, |
| @@ -33,7 +33,7 @@ const toPopupErrorMessage = (error: unknown): string => { · 23 unmodified lines | |||
| 33 | 33 | ||
| 34 | export class OpenApiExtensionService extends Context.Tag("OpenApiExtensionService")< | 34 | export class OpenApiExtensionService extends Context.Tag("OpenApiExtensionService")< |
| 35 | OpenApiExtensionService, | 35 | OpenApiExtensionService, |
| 36 | Captured<OpenApiPluginExtension> | 36 | OpenApiPluginExtension |
| 37 | >() {} | 37 | >() {} |
| 38 | 38 | ||
| 39 | // --------------------------------------------------------------------------- | 39 | // --------------------------------------------------------------------------- |
| @@ -55,13 +55,13 @@ const ExecutorApiWithOpenApi = addGroup(OpenApiGroup); · 15 unmodified lines | |||
| 55 | export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "openapi", (handlers) => | 55 | export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "openapi", (handlers) => |
| 56 | handlers | 56 | handlers |
| 57 | .handle("previewSpec", ({ payload }) => | 57 | .handle("previewSpec", ({ payload }) => |
| 58 | Effect.gen(function* () { | 58 | capture(Effect.gen(function* () { |
| 59 | const ext = yield* OpenApiExtensionService; | 59 | const ext = yield* OpenApiExtensionService; |
| 60 | return yield* ext.previewSpec(payload.spec); | 60 | return yield* ext.previewSpec(payload.spec); |
| 61 | }), | 61 | })), |
| 62 | ) | 62 | ) |
| 63 | .handle("addSpec", ({ payload }) => | 63 | .handle("addSpec", ({ payload }) => |
| 64 | Effect.gen(function* () { | 64 | capture(Effect.gen(function* () { |
| 65 | const ext = yield* OpenApiExtensionService; | 65 | const ext = yield* OpenApiExtensionService; |
| 66 | const result = yield* ext.addSpec({ | 66 | const result = yield* ext.addSpec({ |
| 67 | spec: payload.spec, | 67 | spec: payload.spec, |
| @@ -75,16 +75,16 @@ export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "ope · 7 unmodified lines | |||
| 75 | toolCount: result.toolCount, | 75 | toolCount: result.toolCount, |
| 76 | namespace: result.sourceId, | 76 | namespace: result.sourceId, |
| 77 | }; | 77 | }; |
| 78 | }), | 78 | })), |
| 79 | ) | 79 | ) |
| 80 | .handle("getSource", ({ path }) => | 80 | .handle("getSource", ({ path }) => |
| 81 | Effect.gen(function* () { | 81 | capture(Effect.gen(function* () { |
| 82 | const ext = yield* OpenApiExtensionService; | 82 | const ext = yield* OpenApiExtensionService; |
| 83 | return yield* ext.getSource(path.namespace); | 83 | return yield* ext.getSource(path.namespace); |
| 84 | }), | 84 | })), |
| 85 | ) | 85 | ) |
| 86 | .handle("updateSource", ({ path, payload }) => | 86 | .handle("updateSource", ({ path, payload }) => |
| 87 | Effect.gen(function* () { | 87 | capture(Effect.gen(function* () { |
| 88 | const ext = yield* OpenApiExtensionService; | 88 | const ext = yield* OpenApiExtensionService; |
| 89 | yield* ext.updateSource(path.namespace, { | 89 | yield* ext.updateSource(path.namespace, { |
| 90 | name: payload.name, | 90 | name: payload.name, |
| @@ -92,10 +92,10 @@ export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "ope · 1 unmodified line | |||
| 92 | headers: payload.headers as Record<string, HeaderValue> | undefined, | 92 | headers: payload.headers as Record<string, HeaderValue> | undefined, |
| 93 | } as OpenApiUpdateSourceInput); | 93 | } as OpenApiUpdateSourceInput); |
| 94 | return { updated: true }; | 94 | return { updated: true }; |
| 95 | }), | 95 | })), |
| 96 | ) | 96 | ) |
| 97 | .handle("startOAuth", ({ payload }) => | 97 | .handle("startOAuth", ({ payload }) => |
| 98 | Effect.gen(function* () { | 98 | capture(Effect.gen(function* () { |
| 99 | const ext = yield* OpenApiExtensionService; | 99 | const ext = yield* OpenApiExtensionService; |
| 100 | return yield* ext.startOAuth({ | 100 | return yield* ext.startOAuth({ |
| 101 | displayName: payload.displayName, | 101 | displayName: payload.displayName, |
| @@ -108,23 +108,23 @@ export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "ope · 6 unmodified lines | |||
| 108 | clientSecretSecretId: payload.clientSecretSecretId ?? null, | 108 | clientSecretSecretId: payload.clientSecretSecretId ?? null, |
| 109 | scopes: [...payload.scopes], | 109 | scopes: [...payload.scopes], |
| 110 | }); | 110 | }); |
| 111 | }), | 111 | })), |
| 112 | ) | 112 | ) |
| 113 | .handle("completeOAuth", ({ payload }) => | 113 | .handle("completeOAuth", ({ payload }) => |
| 114 | Effect.gen(function* () { | 114 | capture(Effect.gen(function* () { |
| 115 | const ext = yield* OpenApiExtensionService; | 115 | const ext = yield* OpenApiExtensionService; |
| 116 | return yield* ext.completeOAuth({ | 116 | return yield* ext.completeOAuth({ |
| 117 | state: payload.state, | 117 | state: payload.state, |
| 118 | code: payload.code, | 118 | code: payload.code, |
| 119 | error: payload.error, | 119 | error: payload.error, |
| 120 | }); | 120 | }); |
| 121 | }), | 121 | })), |
| 122 | ) | 122 | ) |
| 123 | .handle("oauthCallback", ({ urlParams }) => | 123 | .handle("oauthCallback", ({ urlParams }) => |
| 124 | // OAuth popup is special: it always returns 200 HTML and renders the | 124 | // OAuth popup is special: it always returns 200 HTML and renders the |
| 125 | // failure into the popup body so the parent window's listener gets a | 125 | // failure into the popup body so the parent window's listener gets a |
| 126 | // structured result. | 126 | // structured result. |
| 127 | Effect.gen(function* () { | 127 | capture(Effect.gen(function* () { |
| 128 | const ext = yield* OpenApiExtensionService; | 128 | const ext = yield* OpenApiExtensionService; |
| 129 | const html = yield* runOAuthCallback<OAuth2Auth, OpenApiOAuthError | InternalError, never>({ | 129 | const html = yield* runOAuthCallback<OAuth2Auth, OpenApiOAuthError | InternalError, never>({ |
| 130 | complete: ({ state, code, error }) => | 130 | complete: ({ state, code, error }) => |
| @@ -138,6 +138,6 @@ export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "ope · 7 unmodified lines | |||
| 138 | channelName: OPENAPI_OAUTH_CHANNEL, | 138 | channelName: OPENAPI_OAUTH_CHANNEL, |
| 139 | }); | 139 | }); |
| 140 | return yield* HttpServerResponse.html(html); | 140 | return yield* HttpServerResponse.html(html); |
| 141 | }), | 141 | })), |
| 142 | ), | 142 | ), |
| 143 | ); | 143 | ); |