@sealant/sdk-next 0.40.0-next.956 → 0.40.0-next.958

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.js CHANGED
@@ -256,11 +256,12 @@ export class Sealant {
256
256
  */
257
257
  inference = {
258
258
  respond: async (options) => {
259
+ const trace = captureTrace(this.#ctx);
259
260
  if ("allowedLogin" in options && options.allowedLogin === "subscription") {
260
- await requireInferenceAllowedLogin(this.#ctx);
261
+ await withCallerTrace(trace, () => requireInferenceAllowedLogin(this.#ctx));
261
262
  }
262
263
  const payload = buildInferenceRespondRequest(options, this.#ctx.config.hostLocal.ownerUserId);
263
- const wire = await this.#runtime.run(inferenceRespondOp(payload));
264
+ const wire = await this.#runtime.run(inferenceRespondOp(payload), trace);
264
265
  return mapInferenceResponse(wire);
265
266
  },
266
267
  };
@@ -328,14 +329,15 @@ export class Sealant {
328
329
  };
329
330
  },
330
331
  connect: async (options) => {
332
+ const trace = captureTrace(this.#ctx);
331
333
  if (options.accountId !== undefined || options.makeDefault !== undefined) {
332
- await requireConnectedAccountDefaults(this.#ctx);
334
+ await withCallerTrace(trace, () => requireConnectedAccountDefaults(this.#ctx));
333
335
  }
334
336
  if (options.login !== undefined) {
335
- await requireConnectedAccountLoginClass(this.#ctx);
337
+ await withCallerTrace(trace, () => requireConnectedAccountLoginClass(this.#ctx));
336
338
  }
337
339
  if (options.endpoint !== undefined) {
338
- await requireConnectedAccountEndpoints(this.#ctx);
340
+ await withCallerTrace(trace, () => requireConnectedAccountEndpoints(this.#ctx));
339
341
  }
340
342
  const wire = await this.#runtime.run(createConnectedAccountOp({
341
343
  ownerUserId: this.#ctx.config.hostLocal.ownerUserId,
@@ -346,7 +348,7 @@ export class Sealant {
346
348
  ...(options.makeDefault === undefined ? {} : { makeDefault: options.makeDefault }),
347
349
  ...(options.login === undefined ? {} : { login: options.login }),
348
350
  ...(options.endpoint === undefined ? {} : { endpoint: options.endpoint }),
349
- }));
351
+ }), trace);
350
352
  return mapConnectedAccount(wire);
351
353
  },
352
354
  disconnect: async (connectedAccountId) => {
@@ -354,21 +356,23 @@ export class Sealant {
354
356
  return mapConnectedAccount(wire);
355
357
  },
356
358
  setDefault: async (options) => {
357
- await requireConnectedAccountDefaults(this.#ctx);
359
+ const trace = captureTrace(this.#ctx);
360
+ await withCallerTrace(trace, () => requireConnectedAccountDefaults(this.#ctx));
358
361
  const wire = await this.#runtime.run(setConnectedAccountDefaultOp(options.provider, {
359
362
  ownerUserId: this.#ctx.config.hostLocal.ownerUserId,
360
363
  connectedAccountId: options.accountId,
361
- }));
364
+ }), trace);
362
365
  return mapConnectedAccount(wire);
363
366
  },
364
367
  markInvalid: async (connectedAccountId, options) => {
368
+ const trace = captureTrace(this.#ctx);
365
369
  if (options?.revision !== undefined) {
366
- await requireConnectedAccountDefaults(this.#ctx);
370
+ await withCallerTrace(trace, () => requireConnectedAccountDefaults(this.#ctx));
367
371
  }
368
372
  const wire = await this.#runtime.run(markConnectedAccountInvalidOp(connectedAccountId, {
369
373
  ownerUserId: this.#ctx.config.hostLocal.ownerUserId,
370
374
  ...(options?.revision === undefined ? {} : { revision: options.revision }),
371
- }));
375
+ }), trace);
372
376
  return { outcome: wire.outcome ?? "marked", account: mapConnectedAccount(wire) };
373
377
  },
374
378
  };
@@ -1,7 +1,11 @@
1
1
  import type { SdkContext } from "../facade/context.js";
2
2
  import type { WorkspaceInit } from "../facade/workspace.js";
3
3
  import type { WorkspaceExecOptions, WorkspaceExecResult } from "../types.js";
4
+ import type { CallerTrace } from "./trace.js";
4
5
  /** The wait before the next read, given the last wait and the time since the run was registered. */
5
6
  export declare const nextExecPollMs: (lastWaitMs: number | undefined, elapsedMs: number) => number;
6
- /** The `workspace.exec()` implementation (Promise boundary over the Effect above). */
7
- export declare const execWorkspace: (ctx: SdkContext, init: WorkspaceInit, argv: readonly string[], options?: WorkspaceExecOptions) => Promise<WorkspaceExecResult>;
7
+ /**
8
+ * The `workspace.exec()` implementation (Promise boundary over the Effect above), continuing
9
+ * `trace` when given, else the caller's trace captured now.
10
+ */
11
+ export declare const execWorkspace: (ctx: SdkContext, init: WorkspaceInit, argv: readonly string[], options?: WorkspaceExecOptions, trace?: CallerTrace) => Promise<WorkspaceExecResult>;
@@ -111,5 +111,8 @@ const execWorkspaceEffect = (ctx, init, argv, options) => Effect.gen(function* (
111
111
  run: makeRun(ctx, { wire, changes }),
112
112
  };
113
113
  });
114
- /** The `workspace.exec()` implementation (Promise boundary over the Effect above). */
115
- export const execWorkspace = (ctx, init, argv, options) => ctx.runtime.run(execWorkspaceEffect(ctx, init, argv, options));
114
+ /**
115
+ * The `workspace.exec()` implementation (Promise boundary over the Effect above), continuing
116
+ * `trace` when given, else the caller's trace captured now.
117
+ */
118
+ export const execWorkspace = (ctx, init, argv, options, trace) => ctx.runtime.run(execWorkspaceEffect(ctx, init, argv, options), trace);
@@ -9,15 +9,21 @@
9
9
  * 3. the active OpenTelemetry context, read through `@opentelemetry/api`'s global registration,
10
10
  * so the SDK imports and depends on no tracing package.
11
11
  *
12
+ * A span of the calling fiber counts only when something exports it: a span made by an exporting
13
+ * tracer (anything but Effect's native one, with tracing on), or an external span the caller set as
14
+ * its parent. A span of Effect's native tracer is never exported, so a caller with telemetry off
15
+ * sends no trace context and its requests start their own traces.
16
+ *
12
17
  * Each contract call runs in a client span `sealant.sdk.<group>.<endpoint>` carrying only the IDs
13
18
  * from its path. When the caller's Effect tracer exports spans, the SDK's spans are made on that
14
- * tracer and the request carries the SDK's HTTP span. When nothing exports them, the request carries
15
- * the parent's own `traceparent`, so the server's span hangs off a span that exists. With no parent
16
- * and no exporting tracer the request carries no trace header at all.
19
+ * tracer and the request carries the SDK's HTTP span. When nothing exports them, the SDK makes no
20
+ * spans and the request carries the parent's own `traceparent` (an external one), so the server's
21
+ * span hangs off a span that exists. With no such parent the request carries no trace header.
17
22
  *
18
23
  * A caller that turned tracing off (`References.TracerEnabled`) or propagation off
19
- * (`Tracer.DisablePropagation`) is continued by nothing: its requests carry no trace header, and a
20
- * disabled span, whose IDs read `noop`, is never sent as a parent.
24
+ * (`Tracer.DisablePropagation`) is continued by nothing, not even an external parent or the
25
+ * fallbacks: its requests carry no trace header, and a disabled span, whose IDs read `noop`, is
26
+ * never sent as a parent.
21
27
  */
22
28
  import { Context, Effect, Tracer } from "effect";
23
29
  import type { SealantTraceContext } from "../types.js";
@@ -9,15 +9,21 @@
9
9
  * 3. the active OpenTelemetry context, read through `@opentelemetry/api`'s global registration,
10
10
  * so the SDK imports and depends on no tracing package.
11
11
  *
12
+ * A span of the calling fiber counts only when something exports it: a span made by an exporting
13
+ * tracer (anything but Effect's native one, with tracing on), or an external span the caller set as
14
+ * its parent. A span of Effect's native tracer is never exported, so a caller with telemetry off
15
+ * sends no trace context and its requests start their own traces.
16
+ *
12
17
  * Each contract call runs in a client span `sealant.sdk.<group>.<endpoint>` carrying only the IDs
13
18
  * from its path. When the caller's Effect tracer exports spans, the SDK's spans are made on that
14
- * tracer and the request carries the SDK's HTTP span. When nothing exports them, the request carries
15
- * the parent's own `traceparent`, so the server's span hangs off a span that exists. With no parent
16
- * and no exporting tracer the request carries no trace header at all.
19
+ * tracer and the request carries the SDK's HTTP span. When nothing exports them, the SDK makes no
20
+ * spans and the request carries the parent's own `traceparent` (an external one), so the server's
21
+ * span hangs off a span that exists. With no such parent the request carries no trace header.
17
22
  *
18
23
  * A caller that turned tracing off (`References.TracerEnabled`) or propagation off
19
- * (`Tracer.DisablePropagation`) is continued by nothing: its requests carry no trace header, and a
20
- * disabled span, whose IDs read `noop`, is never sent as a parent.
24
+ * (`Tracer.DisablePropagation`) is continued by nothing, not even an external parent or the
25
+ * fallbacks: its requests carry no trace header, and a disabled span, whose IDs read `noop`, is
26
+ * never sent as a parent.
21
27
  */
22
28
  import { Context, Effect, Fiber, Option, References, Tracer } from "effect";
23
29
  import { HttpClient } from "effect/http";
@@ -94,6 +100,28 @@ export function activeOpenTelemetryContext() {
94
100
  return undefined;
95
101
  }
96
102
  }
103
+ /**
104
+ * Whether `fiber`'s spans are exported: tracing is on and its tracer is not Effect's native one,
105
+ * which keeps spans in memory only.
106
+ */
107
+ const exportsSpans = (fiber) => fiber.getRef(References.TracerEnabled) &&
108
+ !fiber.getRef(Tracer.DisablePropagation) &&
109
+ fiber.getRef(Tracer.Tracer) !== Tracer.nativeTracer;
110
+ /**
111
+ * `span` when a request may name it as its parent: an external span the caller handed in, or a span
112
+ * an exporting tracer made. A native or disabled span never reaches Sentry, so naming it would give
113
+ * the server's span a parent that does not exist.
114
+ */
115
+ const forwardable = (span, exported) => {
116
+ // An external span (it has no name: no tracer here made it) is a remote parent the caller named.
117
+ if (!("name" in span))
118
+ return span;
119
+ if (!exported || Context.get(span.annotations, Tracer.DisablePropagation))
120
+ return undefined;
121
+ return span;
122
+ };
123
+ /** Whether the fiber turned tracing or propagation off: then nothing continues its trace. */
124
+ const propagationOff = (fiber) => !fiber.getRef(References.TracerEnabled) || fiber.getRef(Tracer.DisablePropagation);
97
125
  /** Whether `span` names a span a `traceparent` can carry: not a disabled one (`noop`). */
98
126
  const isPropagatable = (span) => parseTraceparent(formatTraceparent(span)) !== undefined;
99
127
  /**
@@ -105,14 +133,14 @@ const fromCallingFiber = () => {
105
133
  const fiber = Fiber.getCurrent();
106
134
  if (fiber === undefined)
107
135
  return undefined;
108
- if (!fiber.getRef(References.TracerEnabled) || fiber.getRef(Tracer.DisablePropagation)) {
136
+ if (propagationOff(fiber))
109
137
  return { parent: undefined, tracer: undefined, propagationOff: true };
110
- }
111
- const tracer = fiber.getRef(Tracer.Tracer);
138
+ const exported = exportsSpans(fiber);
112
139
  const parent = Option.getOrUndefined(Context.getOption(fiber.context, Tracer.ParentSpan));
140
+ const kept = parent === undefined ? undefined : forwardable(parent, exported);
113
141
  return {
114
- parent: parent !== undefined && isPropagatable(parent) ? parent : undefined,
115
- tracer: tracer === Tracer.nativeTracer ? undefined : tracer,
142
+ parent: kept !== undefined && isPropagatable(kept) ? kept : undefined,
143
+ tracer: exported ? fiber.getRef(Tracer.Tracer) : undefined,
116
144
  propagationOff: false,
117
145
  };
118
146
  }
@@ -232,29 +260,32 @@ const sdkSpanAttributes = (name) => ({
232
260
  * Run one contract call in its client span, and choose what trace header its request carries (see
233
261
  * the module comment).
234
262
  */
235
- export const traceCall = (name, attributes, effect, route) => Effect.gen(function* () {
236
- // A caller with tracing or propagation off: no span of the SDK's, and no trace header.
237
- if (!(yield* References.TracerEnabled) || (yield* Tracer.DisablePropagation)) {
238
- return yield* Effect.provideService(effect, HttpClient.TracerPropagationEnabled, false);
263
+ export const traceCall = (name, attributes, effect, route) => Effect.withFiber((fiber) => {
264
+ const exported = exportsSpans(fiber);
265
+ if (!exported) {
266
+ // Nothing would export the SDK's spans: make none, and let the request name only a parent
267
+ // that exists elsewhere, unless the caller turned propagation off.
268
+ const parent = propagationOff(fiber)
269
+ ? undefined
270
+ : Option.getOrUndefined(Context.getOption(fiber.context, Tracer.ParentSpan));
271
+ const forwarded = parent === undefined ? undefined : forwardable(parent, false);
272
+ const direct = forwarded !== undefined && isPropagatable(forwarded) ? forwarded : undefined;
273
+ const untraced = effect.pipe(Effect.provideService(HttpClient.TracerPropagationEnabled, false), Effect.provideService(Tracer.DisablePropagation, true), Effect.withTracerEnabled(false));
274
+ return direct === undefined
275
+ ? untraced
276
+ : Effect.provideService(untraced, DirectTraceparent, formatTraceparent(direct));
239
277
  }
240
- const parent = yield* Effect.serviceOption(Tracer.ParentSpan);
241
- const tracer = yield* Effect.tracer;
242
- const templated = route === undefined || tracer === Tracer.nativeTracer
278
+ const tracer = fiber.getRef(Tracer.Tracer);
279
+ const templated = route === undefined
243
280
  ? effect
244
281
  : Effect.flatMap(Effect.currentSpan.pipe(Effect.option), (span) => Option.isNone(span)
245
282
  ? effect
246
283
  : Effect.withTracer(effect, routeTemplatingTracer(tracer, span.value.spanId, route)));
247
- const spanned = Effect.withSpan(templated, name, {
284
+ return Effect.withSpan(templated, name, {
248
285
  kind: "client",
249
286
  attributes: { ...attributes, ...sdkSpanAttributes(name) },
250
287
  captureStackTrace: false,
251
288
  });
252
- if (tracer !== Tracer.nativeTracer)
253
- return yield* spanned;
254
- const unpropagated = Effect.provideService(spanned, HttpClient.TracerPropagationEnabled, false);
255
- if (Option.isNone(parent) || !isPropagatable(parent.value))
256
- return yield* unpropagated;
257
- return yield* Effect.provideService(unpropagated, DirectTraceparent, formatTraceparent(parent.value));
258
289
  });
259
290
  const snakeCase = (identifier) => identifier.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
260
291
  /** The span attributes of a call: the IDs in its path (`workspaceId` -> `sealant.workspace_id`). */
@@ -347,8 +347,11 @@ export const makeWorkspace = (ctx, init) => {
347
347
  const refused = sessionArgvIssue(argv);
348
348
  if (refused !== undefined)
349
349
  throw new SealantError(refused, { code: "invalid_argv" });
350
- if (options?.user !== undefined)
351
- await requireProcessUser(ctx, options.user);
350
+ // Captured before the feature check's await, so the open continues the caller's trace.
351
+ const trace = captureTrace(ctx);
352
+ const user = options?.user;
353
+ if (user !== undefined)
354
+ await withCallerTrace(trace, () => requireProcessUser(ctx, user));
352
355
  const request = {
353
356
  workspaceId: init.id,
354
357
  ownerUserId: ctx.config.hostLocal.ownerUserId,
@@ -366,7 +369,7 @@ export const makeWorkspace = (ctx, init) => {
366
369
  const created = await ctx.runtime
367
370
  .run(options?.user === undefined
368
371
  ? createSessionOp(request)
369
- : createSessionAsUserOp({ ...request, user: options.user }))
372
+ : createSessionAsUserOp({ ...request, user: options.user }), trace)
370
373
  .catch((error) => {
371
374
  // A control plane with the rule answers a typed 400 with its reason (`RequestRefusedError`
372
375
  // for a request it cannot decode, `SessionBadRequestError` for one it refuses); an empty
@@ -598,9 +601,13 @@ export const makeWorkspace = (ctx, init) => {
598
601
  harness,
599
602
  sessions,
600
603
  exec: async (argv, options) => {
601
- if (options?.user !== undefined)
602
- await requireProcessUser(ctx, options.user);
603
- return execWorkspace(ctx, init, argv, options);
604
+ // Captured before the feature check's await: the exec and the run reads it makes continue
605
+ // the caller's trace.
606
+ const trace = captureTrace(ctx);
607
+ const user = options?.user;
608
+ if (user !== undefined)
609
+ await withCallerTrace(trace, () => requireProcessUser(ctx, user));
610
+ return execWorkspace(ctx, init, argv, options, trace);
604
611
  },
605
612
  bind: async (options) => {
606
613
  const result = await ctx.runtime.run(bindWorkspaceOp(init.id, {
@@ -812,8 +819,10 @@ export const makeWorkspace = (ctx, init) => {
812
819
  // People's logins, one person per home; the workspace stays the client owner's.
813
820
  credentials: {
814
821
  put: async (options) => {
815
- if (options.live !== undefined)
816
- await requireCredentialPlacements(ctx);
822
+ const trace = captureTrace(ctx);
823
+ if (options.live !== undefined) {
824
+ await withCallerTrace(trace, () => requireCredentialPlacements(ctx));
825
+ }
817
826
  const choices = {
818
827
  claude: accountChoice(options.claude),
819
828
  codex: accountChoice(options.codex),
@@ -836,7 +845,7 @@ export const makeWorkspace = (ctx, init) => {
836
845
  ...(choices.opencode === undefined ? {} : { opencode: choices.opencode }),
837
846
  ...(options.partial === true ? { partial: true } : {}),
838
847
  ...(options.live === undefined ? {} : { live: options.live }),
839
- }));
848
+ }), trace);
840
849
  return {
841
850
  ...toCredentialHome(answered.home),
842
851
  skipped: (answered.skipped ?? []).map((skip) => ({
@@ -847,12 +856,13 @@ export const makeWorkspace = (ctx, init) => {
847
856
  };
848
857
  },
849
858
  prune: async (home, live) => {
850
- await requireCredentialPlacements(ctx);
859
+ const trace = captureTrace(ctx);
860
+ await withCallerTrace(trace, () => requireCredentialPlacements(ctx));
851
861
  const answered = await ctx.runtime.run(pruneWorkspaceCredentialsOp(init.id, {
852
862
  ownerUserId: ctx.config.hostLocal.ownerUserId,
853
863
  home,
854
864
  live,
855
- }));
865
+ }), trace);
856
866
  return answered.home === undefined ? undefined : toCredentialHome(answered.home);
857
867
  },
858
868
  release: async (home) => {
package/dist/types.d.ts CHANGED
@@ -52,7 +52,9 @@ export interface SealantConfig {
52
52
  readonly fetch?: typeof fetch;
53
53
  /**
54
54
  * The trace a call continues, read when each call starts. A call made inside an Effect span
55
- * continues that span without this; otherwise the SDK calls this and, when it answers nothing,
55
+ * that is exported (an exporting tracer's span, or an external span set as the parent)
56
+ * continues it without this; a span of Effect's native tracer is never exported, so it is not
57
+ * continued. Otherwise the SDK calls this and, when it answers nothing,
56
58
  * uses the active OpenTelemetry context if `@opentelemetry/api` is registered. With no parent
57
59
  * from any of them, requests carry no trace header. Read per call, so one client can serve
58
60
  * concurrent requests that belong to different traces.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealant/sdk-next",
3
- "version": "0.40.0-next.956",
3
+ "version": "0.40.0-next.958",
4
4
  "description": "The fluent public SDK for Sealant — create a workspace, run a harness, replay the record.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -26,12 +26,12 @@
26
26
  "node": ">=20"
27
27
  },
28
28
  "dependencies": {
29
- "@sealant/api-contracts": "npm:@sealant/api-contracts-next@0.40.0-next.956"
29
+ "@sealant/api-contracts": "npm:@sealant/api-contracts-next@0.40.0-next.958"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "effect": "^4.0.5"
33
33
  },
34
- "gitHead": "86d7a0b83920466f1e6489c78a3b6db522193917",
34
+ "gitHead": "718125f760629705ab168e5d57d08f717230b4a0",
35
35
  "publishConfig": {
36
36
  "access": "public"
37
37
  }