@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 540 lines 21.2 KB
Raw
1 import { encodeRpcError } from "alchemy/Rpc";
2 import { buildEventTelemetry } from "alchemy/TelemetryRuntime";
3 import * as Cause from "effect/Cause";
4 import type * as Context from "effect/Context";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import { Base64, Base64Url } from "effect/encoding";
8 import * as Exit from "effect/Exit";
9 import * as FiberSet from "effect/FiberSet";
10 import * as Layer from "effect/Layer";
11 import * as Result from "effect/Result";
12 import * as Scope from "effect/Scope";
13 import { isScopeEjected } from "../Workers/HttpServer.ts";
14 import { getWorkerExport } from "../Workers/WorkerBridge.ts";
15 import type {
16 WorkflowExport,
17 WorkflowImpl,
18 WorkflowStepConfig,
19 WorkflowStepEvent,
20 WorkflowTaskOptions,
21 } from "./Workflow.ts";
22 import {
23 WorkflowEvent as WorkflowEventService,
24 WorkflowStep,
25 WorkflowStepContext,
26 } from "./WorkflowRuntime.ts";
27
28 /**
29 * Create a WorkflowBridge class that extends `WorkflowEntrypoint` and
30 * delegates the `run(event, step)` call to the Effect-native workflow body
31 * registered via `worker.export(...)`.
32 *
33 * The bridge provides `WorkflowEvent` and `WorkflowStep` as Effect
34 * services so the user writes `yield* WorkflowEvent` and `yield* task(...)`
35 * instead of receiving callback parameters.
36 */
37 export const makeWorkflowBridge =
38 (
39 WorkflowEntrypoint: abstract new (
40 ctx: unknown,
41 env: unknown,
42 ) => { run(event: any, step: any): Promise<unknown> },
43 {
44 entrypoint,
45 stack,
46 }: { entrypoint: Effect.Effect<Record<string, any>>; stack: { name: string; stage: string } },
47 ) =>
48 (className: string) => {
49 // One isolate-lifetime layer build shared by every instantiation of this
50 // workflow class — `build` memoizes the built context.
51 const { build } = getWorkerExport<WorkflowExport>({ entrypoint, stack, exportName: className });
52
53 return class WorkflowBridge extends WorkflowEntrypoint {
54 readonly build: Promise<{
55 readonly context: Context.Context<never>;
56 readonly fn: WorkflowImpl<unknown, unknown, unknown>;
57 readonly telemetry: () => Layer.Layer<never, any, any> | undefined;
58 }>;
59
60 constructor(ctx: unknown, env: unknown) {
61 super(ctx, env);
62
63 this.build = build(() => {}).then(({ context, export: wf, telemetry }) =>
64 wf.make(env).pipe(
65 Effect.provideContext(context),
66 Effect.map((fn) => ({
67 context,
68 fn: fn as WorkflowImpl<unknown, unknown, unknown>,
69 telemetry,
70 })),
71 Effect.runPromise,
72 ),
73 );
74 }
75
76 async run(event: any, step: any): Promise<unknown> {
77 const { context, fn, telemetry } = await this.build;
78 // The run scope owns telemetry and resources outside tasks.
79 // Step attempts and rollback handlers use separate scopes.
80 const scope = Scope.makeUnsafe();
81 const exit = await Effect.runPromiseExit(
82 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- run outcome is inspected via the Exit; user fn's E/R are erased at the bridge
83 fn(event.payload).pipe(
84 Effect.provide(
85 Layer.mergeAll(
86 Layer.succeed(WorkflowEventService, wrapWorkflowEvent(event)),
87 Layer.succeed(
88 WorkflowStep,
89 wrapWorkflowStep(step, {
90 workflow: JSON.stringify([
91 stack.name,
92 stack.stage,
93 className,
94 event.workflowName ?? "",
95 ]),
96 instanceId: event.instanceId,
97 }),
98 ),
99 Layer.succeed(Scope.Scope, scope),
100 // The configured telemetry exporters, attached to the run's
101 // scope by `buildEventTelemetry` so buffered telemetry
102 // flushes when the scope closes at the end of the
103 // run-invocation.
104 Layer.effectContext(buildEventTelemetry(context, scope, telemetry())),
105 ).pipe(Layer.provideMerge(Layer.succeedContext(context))),
106 ),
107 ) as Effect.Effect<unknown, unknown>,
108 );
109 // Settle the run's resources with its real exit, unless a binding
110 // ejected the scope to outlive the invocation. The workflow runtime has
111 // no `waitUntil` to detach cleanup to, so close inline — a failing
112 // finalizer (e.g. a pg pool `end()` on a dropped connection) is logged
113 // and ignored so it can't mask the run's outcome.
114 if (!isScopeEjected(scope)) {
115 await Scope.close(scope, exit).pipe(
116 Effect.ignoreCause({ log: "Warn", message: "Workflow run scope close failed" }),
117 Effect.runPromise,
118 );
119 }
120 if (Exit.isSuccess(exit)) {
121 return exit.value;
122 }
123 throw Cause.squash(exit.cause);
124 }
125 };
126 };
127
128 const wrapWorkflowEvent = (event: any): WorkflowEventService["Service"] => ({
129 payload: event.payload,
130 timestamp: event.timestamp instanceof Date ? event.timestamp : new Date(event.timestamp),
131 instanceId: event.instanceId ?? "",
132 workflowName: event.workflowName ?? "",
133 schedule: event.schedule ?? undefined,
134 });
135
136 interface WorkflowIdentity {
137 readonly workflow: string;
138 readonly instanceId: string;
139 }
140
141 /** The raw value a native `step.do` rejected with, decoded back into a Cause below. */
142 class NativeStepRejection extends Data.TaggedError("NativeStepRejection")<{
143 readonly error: unknown;
144 }> {}
145
146 export const wrapWorkflowStep = (
147 step: any,
148 identity?: WorkflowIdentity,
149 ): WorkflowStep["Service"] => ({
150 do: <T, E>(options: WorkflowTaskOptions<T, any, any, E>): Effect.Effect<T, E> => {
151 const { name } = options;
152 // `task` provides application services; the bridge supplies attempt-local services.
153 const effect = options.effect as Effect.Effect<T, E, WorkflowStepContext | Scope.Scope>;
154 const config = definedStepConfig(options);
155 const rollbackEffect = options.rollback;
156 const rollback = rollbackEffect
157 ? {
158 // Native compensation may run after this step and the run scope have closed.
159 rollback: async (context: any) => {
160 const exit = await (
161 rollbackEffect({ error: context.error, output: context.output }) as Effect.Effect<
162 void,
163 unknown,
164 Scope.Scope
165 >
166 ).pipe(Effect.scoped, Effect.runPromiseExit);
167 if (Exit.isFailure(exit)) throw await callbackFailure(exit.cause, name, identity);
168 },
169 rollbackConfig: definedStepConfig(options.rollbackConfig),
170 }
171 : undefined;
172 return Effect.scoped(
173 Effect.gen(function* () {
174 // Join active callbacks on interruption, without waiting through native retry delays.
175 const runPromise = yield* FiberSet.makeRuntimePromise<never, Exit.Exit<T, E>>();
176 let failure: { message: string; cause: Cause.Cause<E> } | undefined;
177 const callback = async (context: any) => {
178 const exit = await runPromise(
179 effect.pipe(
180 Effect.provideService(WorkflowStepContext, {
181 step: context.step,
182 attempt: context.attempt,
183 config: context.config,
184 }),
185 Effect.scoped,
186 Effect.exit,
187 ),
188 );
189 if (Exit.isSuccess(exit)) return exit.value;
190 const error = await callbackFailure(exit.cause, name, identity);
191 // Native RPC/storage discard custom fields. Correlate only this invocation's error.
192 if (!Cause.hasDies(exit.cause) && !Cause.hasInterrupts(exit.cause)) {
193 failure = { message: error.message, cause: exit.cause };
194 }
195 throw error;
196 };
197 return yield* Effect.tryPromise<T, NativeStepRejection>({
198 try: () => {
199 if (config && rollback) return step.do(name, config, callback, rollback);
200 if (config) return step.do(name, config, callback);
201 if (rollback) return step.do(name, callback, rollback);
202 return step.do(name, callback);
203 },
204 catch: (error) => new NativeStepRejection({ error }),
205 }).pipe(
206 Effect.catch(({ error }) =>
207 failure &&
208 error instanceof Error &&
209 error.name === "Error" &&
210 error.message === failure.message
211 ? Effect.failCause(failure.cause)
212 : Effect.promise(() => decodeApplicationFailure<E>(error, name, identity)).pipe(
213 Effect.flatMap((cause) => (cause ? Effect.failCause(cause) : Effect.die(error))),
214 ),
215 ),
216 );
217 }),
218 );
219 },
220 sleep: (name: string, duration: string | number): Effect.Effect<void> =>
221 Effect.promise(() => step.sleep(name, duration)),
222 sleepUntil: (name: string, timestamp: Date | number): Effect.Effect<void> =>
223 Effect.promise(() => step.sleepUntil(name, timestamp)),
224 waitForEvent: <T>(name: string, options: any): Effect.Effect<WorkflowStepEvent<T>> =>
225 Effect.promise(() => step.waitForEvent(name, options) as Promise<WorkflowStepEvent<T>>),
226 });
227
228 const failurePrefix = "[alchemy-workflow-failure:v1]";
229 const terminalPrefix = "[alchemy-workflow-terminal:v1]";
230 const maxFailureLength = 16_384;
231
232 const terminalFailure = async (message: string): Promise<Error> => {
233 const { NonRetryableError } = await import("cloudflare:workflows");
234 return new NonRetryableError(`${terminalPrefix}${message}`);
235 };
236
237 const callbackFailure = async <E>(
238 cause: Cause.Cause<E>,
239 step: string,
240 identity: WorkflowIdentity | undefined,
241 ): Promise<Error> => {
242 if (!Cause.hasDies(cause) && !Cause.hasInterrupts(cause)) {
243 try {
244 const budget = { nodes: 0, seen: new Set<object>() };
245 const errors = cause.reasons.map((reason) => {
246 if (reason._tag !== "Fail") throw new TypeError("Expected an application failure");
247 return encodeFailureValue(reason.error, new Set<object>(), budget);
248 });
249 const error = Cause.squash(cause);
250 const summary =
251 error instanceof Error
252 ? error.message
253 : typeof error === "string"
254 ? error
255 : "Workflow application failure";
256 const body = `${Base64Url.encode(
257 JSON.stringify({
258 workflow: identity?.workflow ?? null,
259 instanceId: identity?.instanceId ?? null,
260 step,
261 errors,
262 }),
263 )}\n${summary}`;
264 const message = `${failurePrefix}${await failureDigest(body)}:${body}`;
265 if (new TextEncoder().encode(message).byteLength > maxFailureLength)
266 throw new TypeError("encoded failure exceeds 16 KiB");
267 return new Error(message);
268 } catch (error) {
269 throw await terminalFailure(
270 `Workflow application failure is not serializable: ${error instanceof Error ? error.message : "unsupported value"}`,
271 );
272 }
273 }
274 const error = Cause.squash(cause);
275 return terminalFailure(
276 error instanceof Error
277 ? error.message
278 : typeof error === "string"
279 ? error
280 : "Workflow callback defect or interruption",
281 );
282 };
283
284 const failureDigest = async (body: string): Promise<string> =>
285 Base64Url.encode(
286 new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(body))),
287 );
288
289 const decodeApplicationFailure = async <E>(
290 error: unknown,
291 step: string,
292 identity: WorkflowIdentity | undefined,
293 ): Promise<Cause.Cause<E> | undefined> => {
294 if (
295 !identity?.workflow ||
296 !identity.instanceId ||
297 !(error instanceof Error) ||
298 error.name !== "Error" ||
299 !error.message.startsWith(failurePrefix)
300 )
301 return undefined;
302 try {
303 if (new TextEncoder().encode(error.message).byteLength > maxFailureLength)
304 throw new TypeError("encoded failure exceeds 16 KiB");
305 const encoded = error.message.slice(failurePrefix.length);
306 const separator = encoded.indexOf(":");
307 const digest = encoded.slice(0, separator);
308 const body = encoded.slice(separator + 1);
309 if (separator !== 43 || digest !== (await failureDigest(body)))
310 throw new TypeError("invalid failure checksum");
311 const end = body.indexOf("\n");
312 if (end < 1) throw new TypeError("invalid failure framing");
313 const json = Base64Url.decodeString(body.slice(0, end));
314 if (Result.isFailure(json)) throw new TypeError("invalid failure encoding");
315 const envelope: unknown = JSON.parse(json.success);
316 if (
317 !envelope ||
318 typeof envelope !== "object" ||
319 !("workflow" in envelope) ||
320 envelope.workflow !== identity.workflow ||
321 !("instanceId" in envelope) ||
322 envelope.instanceId !== identity.instanceId ||
323 !("step" in envelope) ||
324 envelope.step !== step ||
325 !("errors" in envelope) ||
326 !Array.isArray(envelope.errors) ||
327 envelope.errors.length === 0 ||
328 Object.keys(envelope).length !== 4
329 )
330 throw new TypeError("invalid failure envelope");
331 // Only the validated private transport restores E's data; prototypes and identity are invocation-local.
332 const errors = envelope.errors.map((value) => decodeFailureValue(value)) as E[];
333 return Cause.fromReasons(errors.map((value) => Cause.makeFailReason(value)));
334 } catch (cause) {
335 throw new TypeError("Invalid persisted Workflow application failure", { cause });
336 }
337 };
338
339 type FailureValue = null | boolean | string | number | FailureValue[];
340
341 const encodeFailureValue = (
342 value: unknown,
343 ancestors = new Set<object>(),
344 budget = { nodes: 0, seen: new Set<object>() },
345 ): FailureValue => {
346 if (
347 ++budget.nodes > maxFailureLength ||
348 (typeof value === "string" && value.length > maxFailureLength)
349 )
350 throw new TypeError("encoded failure exceeds 16 KiB");
351 if (value === null || typeof value === "boolean" || typeof value === "string") return value;
352 if (typeof value === "number")
353 return Number.isFinite(value) && !Object.is(value, -0)
354 ? value
355 : ["number", Object.is(value, -0) ? "-0" : String(value)];
356 if (value === undefined) return ["undefined"];
357 if (typeof value === "bigint") return ["bigint", String(value)];
358 if (typeof value !== "object") throw new TypeError(`unsupported ${typeof value}`);
359 if (ancestors.has(value)) throw new TypeError("cyclic error data");
360 if (budget.seen.has(value)) throw new TypeError("shared references in error data");
361 budget.seen.add(value);
362 if (ancestors.size >= 64) throw new TypeError("error data exceeds 64 nesting levels");
363 ancestors.add(value);
364 try {
365 const encode = (item: unknown) => encodeFailureValue(item, ancestors, budget);
366 const symbols = Object.getOwnPropertySymbols(value).filter(
367 (key) => !(value instanceof Error && key === Symbol.for("effect/Data/Error/plainArgs")),
368 );
369 if (symbols.length) throw new TypeError("symbol-keyed error data");
370 if (
371 value instanceof Date ||
372 value instanceof Uint8Array ||
373 value instanceof ArrayBuffer ||
374 value instanceof Map ||
375 value instanceof Set
376 ) {
377 const own = Object.getOwnPropertyNames(value);
378 if (own.some((key) => !(value instanceof Uint8Array && /^(0|[1-9]\d*)$/.test(key))))
379 throw new TypeError("custom properties on serialized built-in values");
380 if (value instanceof Date) return ["date", encode(Date.prototype.getTime.call(value))];
381 if (value instanceof Uint8Array) return ["bytes", Base64.encode(value)];
382 if (value instanceof ArrayBuffer) return ["buffer", Base64.encode(new Uint8Array(value))];
383 if (value instanceof Map)
384 return [
385 "map",
386 [...Map.prototype.entries.call(value)].map(([key, item]) => [encode(key), encode(item)]),
387 ];
388 return ["set", [...Set.prototype.values.call(value)].map(encode)];
389 }
390 if (Array.isArray(value)) {
391 const descriptors = Object.getOwnPropertyDescriptors(value);
392 const keys = Object.keys(descriptors);
393 if (
394 keys.length !== value.length + 1 ||
395 keys.some(
396 (key) => key !== "length" && (!/^(0|[1-9]\d*)$/.test(key) || Number(key) >= value.length),
397 )
398 )
399 throw new TypeError("sparse arrays or custom array properties");
400 for (const descriptor of Object.values(descriptors)) {
401 if (!("value" in descriptor)) throw new TypeError("accessor error data");
402 }
403 return [
404 "array",
405 Array.from({ length: value.length }, (_, i) => encode(descriptors[i].value)),
406 ];
407 }
408 const descriptors = Object.getOwnPropertyDescriptors(value);
409 let tag: string | undefined;
410 for (let owner: object | null = value; owner; owner = Object.getPrototypeOf(owner)) {
411 const descriptor = Object.getOwnPropertyDescriptor(owner, "_tag");
412 if (!descriptor) continue;
413 if (!("value" in descriptor)) throw new TypeError("accessor error data");
414 if (typeof descriptor.value === "string") tag = descriptor.value;
415 break;
416 }
417 const prototype = Object.getPrototypeOf(value);
418 if (
419 !(value instanceof Error) &&
420 prototype !== null &&
421 prototype !== Object.prototype &&
422 tag === undefined
423 )
424 throw new TypeError("unsupported class instance");
425 for (const [key, descriptor] of Object.entries(descriptors)) {
426 if (!("value" in descriptor) && !(value instanceof Error && key === "stack"))
427 throw new TypeError("accessor error data");
428 }
429 const fields = new Map<string, unknown>();
430 if (tag !== undefined) fields.set("_tag", tag);
431 if (value instanceof Error) {
432 for (const key of ["name", "message", "stack"]) {
433 let owner: object | null = value;
434 while (owner) {
435 const descriptor = Object.getOwnPropertyDescriptor(owner, key);
436 if (descriptor) {
437 if (!("value" in descriptor) && !(owner === value && key === "stack"))
438 throw new TypeError("accessor error data");
439 break;
440 }
441 owner = Object.getPrototypeOf(owner);
442 }
443 }
444 for (const [key, item] of Object.entries(encodeRpcError(value) as Record<string, unknown>))
445 fields.set(key, item);
446 fields.set("name", value.name);
447 fields.set("message", value.message);
448 }
449 for (const [key, descriptor] of Object.entries(descriptors)) {
450 if (!("value" in descriptor)) {
451 if (value instanceof Error && key === "stack") fields.set(key, value.stack);
452 else throw new TypeError("accessor error data");
453 } else fields.set(key, descriptor.value);
454 }
455 return [
456 value instanceof Error ? "error" : "object",
457 [...fields].map(([key, item]) => [key, encode(item)]),
458 ];
459 } finally {
460 ancestors.delete(value);
461 }
462 };
463
464 const decodeFailureValue = (value: unknown, depth = 0): unknown => {
465 if (
466 value === null ||
467 typeof value === "boolean" ||
468 typeof value === "string" ||
469 (typeof value === "number" && Number.isFinite(value))
470 )
471 return value;
472 if (!Array.isArray(value)) throw new TypeError("invalid serialized error value");
473 const decode = (item: unknown) => decodeFailureValue(item, depth + 1);
474 const [tag, data] = value;
475 if (tag === "undefined" && value.length === 1) return undefined;
476 if (value.length !== 2) throw new TypeError("invalid serialized error tuple");
477 if (
478 tag === "number" &&
479 typeof data === "string" &&
480 ["NaN", "Infinity", "-Infinity", "-0"].includes(data)
481 )
482 return Number(data);
483 if (tag === "bigint" && typeof data === "string" && /^-?(0|[1-9]\d*)$/.test(data))
484 return BigInt(data);
485 if (depth >= 64) throw new TypeError("invalid serialized error value");
486 if (tag === "date") {
487 const timestamp = decode(data);
488 if (typeof timestamp === "number") return new Date(timestamp);
489 }
490 if ((tag === "bytes" || tag === "buffer") && typeof data === "string") {
491 const bytes = Base64.decode(data);
492 if (Result.isSuccess(bytes) && Base64.encode(bytes.success) === data)
493 return tag === "bytes" ? bytes.success : bytes.success.buffer;
494 }
495 if (Array.isArray(data)) {
496 if (tag === "array") return data.map(decode);
497 if (tag === "set") return new Set(data.map(decode));
498 if (tag === "map")
499 return new Map(
500 data.map((pair) => {
501 if (!Array.isArray(pair) || pair.length !== 2)
502 throw new TypeError("invalid serialized map");
503 return [decode(pair[0]), decode(pair[1])];
504 }),
505 );
506 if (tag === "object" || tag === "error") {
507 const result = tag === "error" ? new Error() : {};
508 const keys = new Set<string>();
509 for (const pair of data) {
510 if (
511 !Array.isArray(pair) ||
512 pair.length !== 2 ||
513 typeof pair[0] !== "string" ||
514 keys.has(pair[0])
515 )
516 throw new TypeError("invalid serialized error fields");
517 keys.add(pair[0]);
518 Object.defineProperty(result, pair[0], {
519 value: decode(pair[1]),
520 enumerable: true,
521 writable: true,
522 configurable: true,
523 });
524 }
525 return result;
526 }
527 }
528 throw new TypeError("invalid serialized error value");
529 };
530
531 // Own undefined properties overwrite the engine's defaults for steps and rollbacks.
532 const definedStepConfig = (
533 options: WorkflowStepConfig | undefined,
534 ): WorkflowStepConfig | undefined => {
535 if (options === undefined) return undefined;
536 const config: WorkflowStepConfig = {};
537 if (options.retries !== undefined) config.retries = options.retries;
538 if (options.timeout !== undefined) config.timeout = options.timeout;
539 return Object.keys(config).length > 0 ? config : undefined;
540 };