History

refactor(api): replace withCapture proxy with per-handler capture(...)

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.

Rhys Sullivan committed 7 months ago

Commit 96b76e61 parent 50f256823 files · +294 −379Browse files

Showing the changes to packages/plugins/openapi/src/api (1 of 23 files). Show all

Unified · Split

packages/plugins/openapi/src/api/handlers.ts+16−16
@@ -3,7 +3,7 @@ import { Context, Effect } from "effect"; · 2 unmodified lines
33
44 import { runOAuthCallback } from "@executor/plugin-oauth2/http";
55
6−import { addGroup, InternalError, type Captured } from "@executor/api";
6+import { addGroup, capture, InternalError } from "@executor/api";
77 import { OpenApiOAuthError } from "../sdk/errors";
88 import type {
99 OpenApiPluginExtension,
@@ -33,7 +33,7 @@ const toPopupErrorMessage = (error: unknown): string => { · 23 unmodified lines
3333
3434 export class OpenApiExtensionService extends Context.Tag("OpenApiExtensionService")<
3535 OpenApiExtensionService,
36− Captured<OpenApiPluginExtension>
36+ OpenApiPluginExtension
3737 >() {}
3838
3939 // ---------------------------------------------------------------------------
@@ -55,13 +55,13 @@ const ExecutorApiWithOpenApi = addGroup(OpenApiGroup); · 15 unmodified lines
5555 export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "openapi", (handlers) =>
5656 handlers
5757 .handle("previewSpec", ({ payload }) =>
58− Effect.gen(function* () {
58+ capture(Effect.gen(function* () {
5959 const ext = yield* OpenApiExtensionService;
6060 return yield* ext.previewSpec(payload.spec);
61− }),
61+ })),
6262 )
6363 .handle("addSpec", ({ payload }) =>
64− Effect.gen(function* () {
64+ capture(Effect.gen(function* () {
6565 const ext = yield* OpenApiExtensionService;
6666 const result = yield* ext.addSpec({
6767 spec: payload.spec,
@@ -75,16 +75,16 @@ export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "ope · 7 unmodified lines
7575 toolCount: result.toolCount,
7676 namespace: result.sourceId,
7777 };
78− }),
78+ })),
7979 )
8080 .handle("getSource", ({ path }) =>
81− Effect.gen(function* () {
81+ capture(Effect.gen(function* () {
8282 const ext = yield* OpenApiExtensionService;
8383 return yield* ext.getSource(path.namespace);
84− }),
84+ })),
8585 )
8686 .handle("updateSource", ({ path, payload }) =>
87− Effect.gen(function* () {
87+ capture(Effect.gen(function* () {
8888 const ext = yield* OpenApiExtensionService;
8989 yield* ext.updateSource(path.namespace, {
9090 name: payload.name,
@@ -92,10 +92,10 @@ export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "ope · 1 unmodified line
9292 headers: payload.headers as Record<string, HeaderValue> | undefined,
9393 } as OpenApiUpdateSourceInput);
9494 return { updated: true };
95− }),
95+ })),
9696 )
9797 .handle("startOAuth", ({ payload }) =>
98− Effect.gen(function* () {
98+ capture(Effect.gen(function* () {
9999 const ext = yield* OpenApiExtensionService;
100100 return yield* ext.startOAuth({
101101 displayName: payload.displayName,
@@ -108,23 +108,23 @@ export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "ope · 6 unmodified lines
108108 clientSecretSecretId: payload.clientSecretSecretId ?? null,
109109 scopes: [...payload.scopes],
110110 });
111− }),
111+ })),
112112 )
113113 .handle("completeOAuth", ({ payload }) =>
114− Effect.gen(function* () {
114+ capture(Effect.gen(function* () {
115115 const ext = yield* OpenApiExtensionService;
116116 return yield* ext.completeOAuth({
117117 state: payload.state,
118118 code: payload.code,
119119 error: payload.error,
120120 });
121− }),
121+ })),
122122 )
123123 .handle("oauthCallback", ({ urlParams }) =>
124124 // OAuth popup is special: it always returns 200 HTML and renders the
125125 // failure into the popup body so the parent window's listener gets a
126126 // structured result.
127− Effect.gen(function* () {
127+ capture(Effect.gen(function* () {
128128 const ext = yield* OpenApiExtensionService;
129129 const html = yield* runOAuthCallback<OAuth2Auth, OpenApiOAuthError | InternalError, never>({
130130 complete: ({ state, code, error }) =>
@@ -138,6 +138,6 @@ export const OpenApiHandlers = HttpApiBuilder.group(ExecutorApiWithOpenApi, "ope · 7 unmodified lines
138138 channelName: OPENAPI_OAUTH_CHANNEL,
139139 });
140140 return yield* HttpServerResponse.html(html);
141− }),
141+ })),
142142 ),
143143 );

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t