Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it } from '@effect/vitest'; |
| 2 | import { Cause, Data, Effect, Option } from 'effect'; |
| 3 | import { extractSpanStackFrames } from 'src/effect-errors/logic/errors/span-annotation'; |
| 4 | |
| 5 | class CanaryError extends Data.TaggedError('CanaryError')<{ |
| 6 | readonly message: string; |
| 7 | }> {} |
| 8 | |
| 9 | const findFailReason = <E>(cause: Cause.Cause<E>): Cause.Reason<E> => |
| 10 | Option.getOrThrowWith( |
| 11 | Option.fromUndefinedOr(cause.reasons.find(Cause.isFailReason)), |
| 12 | () => new Error('expected a fail reason') |
| 13 | ); |
| 14 | |
| 15 | const findDieReason = <E>(cause: Cause.Cause<E>): Cause.Reason<E> => |
| 16 | Option.getOrThrowWith( |
| 17 | Option.fromUndefinedOr(cause.reasons.find(Cause.isDieReason)), |
| 18 | () => new Error('expected a die reason') |
| 19 | ); |
| 20 | |
| 21 | describe('extractSpanStackFrames', () => { |
| 22 | // v4 dropped the `effect/SpanAnnotation` internal that let `Effect.fail`/`Effect.die` |
| 23 | // stamp the ambient span directly onto the raised error value. The analogous v4 |
| 24 | // mechanism lives one level up, on the `Cause` `Reason`: `Effect.withSpan` pushes a |
| 25 | // span-named `StackFrame` onto the `CurrentStackFrame` fiber reference, and on |
| 26 | // failure the runtime annotates every reason with that frame chain under the |
| 27 | // `Cause.StackTrace` context key (confirmed against `ts/vendor/effect` — |
| 28 | // `internal/effect.ts`'s `provideSpanStackFrame` / `internal/core.ts`'s |
| 29 | // `exitFailCause`, public accessor `Cause.StackTrace`, readable via |
| 30 | // `Context.get(Cause.reasonAnnotations(reason), Cause.StackTrace)`). These two |
| 31 | // canaries assert that chain is recoverable for both `Effect.fail` and |
| 32 | // `Effect.die` — see `src/effect-errors/logic/errors/span-annotation.ts` for the |
| 33 | // extraction and `cause-characterization.test.ts` for the end-to-end render. |
| 34 | it.effect( |
| 35 | 'recovers the ambient span from a runtime failure (canary for the Cause.StackTrace mechanism)', |
| 36 | () => |
| 37 | Effect.gen(function* () { |
| 38 | const cause = yield* Effect.fail(new CanaryError({ message: 'boom' })).pipe( |
| 39 | Effect.withSpan('canary-span'), |
| 40 | Effect.sandbox, |
| 41 | Effect.flip |
| 42 | ); |
| 43 | |
| 44 | const frames = extractSpanStackFrames(findFailReason(cause)); |
| 45 | |
| 46 | expect(frames.map(frame => frame.name)).toEqual(['canary-span']); |
| 47 | }) |
| 48 | ); |
| 49 | |
| 50 | it.effect('recovers the ambient span from a runtime defect', () => |
| 51 | Effect.gen(function* () { |
| 52 | const cause = yield* Effect.die(new Error('defect boom')).pipe( |
| 53 | Effect.withSpan('defect-span'), |
| 54 | Effect.sandbox, |
| 55 | Effect.flip |
| 56 | ); |
| 57 | |
| 58 | const frames = extractSpanStackFrames(findDieReason(cause)); |
| 59 | |
| 60 | expect(frames.map(frame => frame.name)).toEqual(['defect-span']); |
| 61 | }) |
| 62 | ); |
| 63 | |
| 64 | it.effect('recovers a nested span chain innermost-first', () => |
| 65 | Effect.gen(function* () { |
| 66 | const cause = yield* Effect.fail(new CanaryError({ message: 'nested' })).pipe( |
| 67 | Effect.withSpan('inner-span'), |
| 68 | Effect.withSpan('outer-span'), |
| 69 | Effect.sandbox, |
| 70 | Effect.flip |
| 71 | ); |
| 72 | |
| 73 | const frames = extractSpanStackFrames(findFailReason(cause)); |
| 74 | |
| 75 | expect(frames.map(frame => frame.name)).toEqual(['inner-span', 'outer-span']); |
| 76 | }) |
| 77 | ); |
| 78 | |
| 79 | it.effect('returns an empty chain when the reason carries no span annotation', () => |
| 80 | Effect.gen(function* () { |
| 81 | const cause = yield* Effect.fail(new CanaryError({ message: 'no span' })).pipe( |
| 82 | Effect.sandbox, |
| 83 | Effect.flip |
| 84 | ); |
| 85 | |
| 86 | expect(extractSpanStackFrames(findFailReason(cause))).toEqual([]); |
| 87 | }) |
| 88 | ); |
| 89 | }); |