Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 38 lines 1.87 KB
Raw
1 import { type Reason, reasonAnnotations, StackTrace } from 'effect/Cause';
2 import { getOrUndefined } from 'effect/Context';
3
4 import type { ErrorSpan } from 'effect-errors/types';
5
6 // v3 internal: `Effect.fail`/`Effect.die` proxied object errors so the fiber's
7 // current span was readable under a global `effect/SpanAnnotation` registry
8 // symbol. v4 dropped that mechanism entirely (confirmed against
9 // `ts/vendor/effect` — no such symbol, and no per-reason span stamped directly
10 // onto the raised error value anywhere in the v4 source or the installed
11 // `effect` dist).
12 //
13 // The analogous v4 mechanism lives one level up, on the `Cause` `Reason`
14 // rather than the bare error value: `Effect.withSpan` pushes a span-named
15 // `StackFrame` onto the `CurrentStackFrame` fiber reference (vendored
16 // `internal/effect.ts`'s `provideSpanStackFrame`, used by
17 // `withParentSpan`/`withSpan`), and on failure the runtime annotates every
18 // reason with that frame chain under the `Cause.StackTrace` context key —
19 // `Context.get(Cause.reasonAnnotations(reason), Cause.StackTrace)`. Frames
20 // form a linked list (`{ name, stack(), parent }`), innermost span first.
21 //
22 // Only names and call-site locations survive this way: the old v3 span
23 // timeline's per-span duration/attributes lived on the `Span` object itself,
24 // which no longer exists by the time a `Cause` is inspected, so timing cannot
25 // be recovered — see the canary tests in
26 // test/src/effect-errors/span-annotation.test.ts, which confirm this chain is
27 // populated for both `Effect.fail` and `Effect.die`.
28 export const extractSpanStackFrames = (reason: Reason<unknown>): ReadonlyArray<ErrorSpan> => {
29 const frames: ErrorSpan[] = [];
30 let frame = getOrUndefined(reasonAnnotations(reason), StackTrace);
31
32 while (frame !== undefined) {
33 frames.push({ name: frame.name, location: frame.stack() });
34 frame = frame.parent;
35 }
36
37 return frames;
38 };