rivetkit 2.3.18 → 2.3.20

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.
Files changed (140) hide show
  1. package/dist/browser/client.d.ts +754 -608
  2. package/dist/browser/client.js +184 -28
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.js +3 -1
  5. package/dist/browser/inspector/client.js.map +1 -1
  6. package/dist/tsup/actor/errors.cjs +6 -2
  7. package/dist/tsup/actor/errors.cjs.map +1 -1
  8. package/dist/tsup/actor/errors.d.cts +1 -1
  9. package/dist/tsup/actor/errors.d.ts +1 -1
  10. package/dist/tsup/actor/errors.js +5 -1
  11. package/dist/tsup/agent-os/index.cjs +36 -1
  12. package/dist/tsup/agent-os/index.cjs.map +1 -1
  13. package/dist/tsup/agent-os/index.d.cts +149 -3
  14. package/dist/tsup/agent-os/index.d.ts +149 -3
  15. package/dist/tsup/agent-os/index.js +36 -1
  16. package/dist/tsup/agent-os/index.js.map +1 -1
  17. package/dist/tsup/{chunk-2IDK6NX3.cjs → chunk-2CE5UNWK.cjs} +13 -13
  18. package/dist/tsup/chunk-2CE5UNWK.cjs.map +1 -0
  19. package/dist/tsup/{chunk-IPXIZSE7.js → chunk-2NXLTFWI.js} +3 -3
  20. package/dist/tsup/{chunk-OKVVWOC3.cjs → chunk-3WS7Z64U.cjs} +3 -3
  21. package/dist/tsup/{chunk-OKVVWOC3.cjs.map → chunk-3WS7Z64U.cjs.map} +1 -1
  22. package/dist/tsup/{chunk-LAITPJRA.js → chunk-553CIQJY.js} +2 -2
  23. package/dist/tsup/{chunk-PXKNBNVU.js → chunk-5G2J26VM.js} +188 -31
  24. package/dist/tsup/chunk-5G2J26VM.js.map +1 -0
  25. package/dist/tsup/{chunk-IKZ6DJMP.cjs → chunk-76YTS32R.cjs} +4 -4
  26. package/dist/tsup/{chunk-IKZ6DJMP.cjs.map → chunk-76YTS32R.cjs.map} +1 -1
  27. package/dist/tsup/{chunk-NRIRV3Z2.cjs → chunk-A36FMKOV.cjs} +352 -195
  28. package/dist/tsup/chunk-A36FMKOV.cjs.map +1 -0
  29. package/dist/tsup/{chunk-6W5VGLFT.js → chunk-CSOHAAZK.js} +33 -1
  30. package/dist/tsup/chunk-CSOHAAZK.js.map +1 -0
  31. package/dist/tsup/{chunk-T6JUXRNI.cjs → chunk-CVLNRT7O.cjs} +3 -3
  32. package/dist/tsup/{chunk-T6JUXRNI.cjs.map → chunk-CVLNRT7O.cjs.map} +1 -1
  33. package/dist/tsup/{chunk-7EPLADKM.js → chunk-DHWZTUKA.js} +7 -7
  34. package/dist/tsup/chunk-DHWZTUKA.js.map +1 -0
  35. package/dist/tsup/{chunk-4XECVATQ.js → chunk-DMQUERRN.js} +2 -2
  36. package/dist/tsup/{chunk-25Z2RK7C.cjs → chunk-IHKDFKM4.cjs} +3 -3
  37. package/dist/tsup/{chunk-25Z2RK7C.cjs.map → chunk-IHKDFKM4.cjs.map} +1 -1
  38. package/dist/tsup/{chunk-2JSYYH6P.cjs → chunk-JX2ITWAV.cjs} +13 -2
  39. package/dist/tsup/chunk-JX2ITWAV.cjs.map +1 -0
  40. package/dist/tsup/{chunk-4KGXFC6M.cjs → chunk-NWCAIWHL.cjs} +34 -2
  41. package/dist/tsup/chunk-NWCAIWHL.cjs.map +1 -0
  42. package/dist/tsup/{chunk-OUQUIBVW.js → chunk-P2OFXH4G.js} +12 -1
  43. package/dist/tsup/chunk-P2OFXH4G.js.map +1 -0
  44. package/dist/tsup/{chunk-ZRTESY7V.cjs → chunk-RSIOGT2U.cjs} +7 -7
  45. package/dist/tsup/{chunk-ZRTESY7V.cjs.map → chunk-RSIOGT2U.cjs.map} +1 -1
  46. package/dist/tsup/{chunk-QIFZELUQ.js → chunk-SNNVRHLX.js} +2 -2
  47. package/dist/tsup/{chunk-2MK75JFR.js → chunk-TZVAVPGJ.js} +4 -4
  48. package/dist/tsup/{chunk-TNSLNSQH.js → chunk-W2PXGWMR.js} +2 -2
  49. package/dist/tsup/{chunk-O7RSU3UC.cjs → chunk-WAISW7KR.cjs} +10 -10
  50. package/dist/tsup/{chunk-O7RSU3UC.cjs.map → chunk-WAISW7KR.cjs.map} +1 -1
  51. package/dist/tsup/client/mod.cjs +6 -6
  52. package/dist/tsup/client/mod.d.cts +5 -5
  53. package/dist/tsup/client/mod.d.ts +5 -5
  54. package/dist/tsup/client/mod.js +5 -5
  55. package/dist/tsup/common/log.cjs +3 -3
  56. package/dist/tsup/common/log.js +2 -2
  57. package/dist/tsup/common/websocket.cjs +4 -4
  58. package/dist/tsup/common/websocket.js +3 -3
  59. package/dist/tsup/{config-Ug5M2MOr.d.ts → config-Be-VOHaX.d.ts} +155 -11
  60. package/dist/tsup/{config-gvHwMas-.d.cts → config-Ct5uXxXk.d.cts} +155 -11
  61. package/dist/tsup/{context-DererUCI.d.ts → context-CXLa0BIM.d.ts} +1 -1
  62. package/dist/tsup/{context-BSCBvbfM.d.cts → context-Cw-11Q-B.d.cts} +1 -1
  63. package/dist/tsup/db/drizzle.cjs +3 -3
  64. package/dist/tsup/db/drizzle.js +2 -2
  65. package/dist/tsup/db/mod.cjs +4 -4
  66. package/dist/tsup/db/mod.js +3 -3
  67. package/dist/tsup/dynamic/mod.cjs +2 -2
  68. package/dist/tsup/dynamic/mod.d.cts +3 -3
  69. package/dist/tsup/dynamic/mod.d.ts +3 -3
  70. package/dist/tsup/dynamic/mod.js +1 -1
  71. package/dist/tsup/inspector/mod.cjs +6 -6
  72. package/dist/tsup/inspector/mod.js +5 -5
  73. package/dist/tsup/inspector/workflow.cjs +5 -5
  74. package/dist/tsup/inspector/workflow.js +4 -4
  75. package/dist/tsup/inspector-tab/mod.cjs +2 -2
  76. package/dist/tsup/inspector-tab/mod.d.cts +4 -4
  77. package/dist/tsup/inspector-tab/mod.d.ts +4 -4
  78. package/dist/tsup/inspector-tab/mod.js +1 -1
  79. package/dist/tsup/mod.cjs +506 -319
  80. package/dist/tsup/mod.cjs.map +1 -1
  81. package/dist/tsup/mod.d.cts +5 -5
  82. package/dist/tsup/mod.d.ts +5 -5
  83. package/dist/tsup/mod.js +381 -194
  84. package/dist/tsup/mod.js.map +1 -1
  85. package/dist/tsup/test/mod.cjs +7 -7
  86. package/dist/tsup/test/mod.d.cts +3 -3
  87. package/dist/tsup/test/mod.d.ts +3 -3
  88. package/dist/tsup/test/mod.js +5 -5
  89. package/dist/tsup/{utils-Bbz-3411.d.cts → utils-CNFeS-Dk.d.cts} +1 -1
  90. package/dist/tsup/{utils-vvJQyygr.d.ts → utils-DsoDinPz.d.cts} +9 -1
  91. package/dist/tsup/{utils-vvJQyygr.d.cts → utils-DsoDinPz.d.ts} +9 -1
  92. package/dist/tsup/{utils-BqdWVlzm.d.ts → utils-OQHBjfcM.d.ts} +1 -1
  93. package/dist/tsup/utils.cjs +3 -3
  94. package/dist/tsup/utils.d.cts +2 -2
  95. package/dist/tsup/utils.d.ts +2 -2
  96. package/dist/tsup/utils.js +2 -2
  97. package/dist/tsup/workflow/mod.cjs +33 -27
  98. package/dist/tsup/workflow/mod.cjs.map +1 -1
  99. package/dist/tsup/workflow/mod.d.cts +5 -5
  100. package/dist/tsup/workflow/mod.d.ts +5 -5
  101. package/dist/tsup/workflow/mod.js +25 -19
  102. package/dist/tsup/workflow/mod.js.map +1 -1
  103. package/package.json +10 -8
  104. package/src/actor/actions.ts +36 -0
  105. package/src/actor/config.ts +65 -0
  106. package/src/actor/definition.ts +6 -1
  107. package/src/actor/errors.ts +31 -0
  108. package/src/actor/mod.ts +5 -0
  109. package/src/actor/tracing.ts +28 -0
  110. package/src/client/actor-conn.ts +7 -0
  111. package/src/client/actor-handle.ts +57 -17
  112. package/src/client/client.ts +26 -6
  113. package/src/client/outbound-telemetry.ts +66 -0
  114. package/src/client/queue.ts +3 -0
  115. package/src/client/raw-utils.ts +3 -0
  116. package/src/common/actor-router-consts.ts +3 -0
  117. package/src/common/actor-telemetry-context.ts +19 -0
  118. package/src/common/otel-context.ts +99 -0
  119. package/src/common/utils.ts +1 -19
  120. package/src/engine-client/actor-http-client.ts +15 -0
  121. package/src/registry/index.ts +6 -0
  122. package/src/registry/napi-runtime.ts +155 -35
  123. package/src/registry/native.ts +295 -191
  124. package/src/registry/runtime.ts +74 -0
  125. package/src/registry/wasm-runtime.ts +40 -0
  126. package/src/workflow/driver.ts +20 -12
  127. package/dist/tsup/chunk-2IDK6NX3.cjs.map +0 -1
  128. package/dist/tsup/chunk-2JSYYH6P.cjs.map +0 -1
  129. package/dist/tsup/chunk-4KGXFC6M.cjs.map +0 -1
  130. package/dist/tsup/chunk-6W5VGLFT.js.map +0 -1
  131. package/dist/tsup/chunk-7EPLADKM.js.map +0 -1
  132. package/dist/tsup/chunk-NRIRV3Z2.cjs.map +0 -1
  133. package/dist/tsup/chunk-OUQUIBVW.js.map +0 -1
  134. package/dist/tsup/chunk-PXKNBNVU.js.map +0 -1
  135. /package/dist/tsup/{chunk-IPXIZSE7.js.map → chunk-2NXLTFWI.js.map} +0 -0
  136. /package/dist/tsup/{chunk-LAITPJRA.js.map → chunk-553CIQJY.js.map} +0 -0
  137. /package/dist/tsup/{chunk-4XECVATQ.js.map → chunk-DMQUERRN.js.map} +0 -0
  138. /package/dist/tsup/{chunk-QIFZELUQ.js.map → chunk-SNNVRHLX.js.map} +0 -0
  139. /package/dist/tsup/{chunk-2MK75JFR.js.map → chunk-TZVAVPGJ.js.map} +0 -0
  140. /package/dist/tsup/{chunk-TNSLNSQH.js.map → chunk-W2PXGWMR.js.map} +0 -0
@@ -543,6 +543,29 @@ declare class RivetError extends Error {
543
543
  toString(): string;
544
544
  }
545
545
 
546
+ /** Sample rates keyed by action name, nested like the actor's action groups. */
547
+ interface ActionSampleRates {
548
+ readonly [name: string]: number | ActionSampleRates;
549
+ }
550
+ /**
551
+ * {@link ActionSampleRates} limited to the actions of one actor, so a rate for
552
+ * an action that does not exist is a compile error.
553
+ */
554
+ type ActionTraceSamplers<TActions> = {
555
+ readonly [K in keyof TActions]?: TActions[K] extends (...args: never[]) => unknown ? number : ActionTraceSamplers<TActions[K]>;
556
+ };
557
+ /** Trace sampling for one actor definition. */
558
+ interface ActorTracingOptions<TActions> {
559
+ /**
560
+ * Share of this actor's invocations that are recorded, from 0 to 1. A rate
561
+ * lowers what is recorded and never raises it, so an invocation inside a
562
+ * trace the caller did not record is not recorded at any rate.
563
+ */
564
+ readonly sampler?: number;
565
+ /** Rates that replace `sampler` for single actions. */
566
+ readonly actions?: ActionTraceSamplers<TActions>;
567
+ }
568
+
546
569
  type UpgradeWebSocket = (createEvents: (c: Context) => any) => Handler;
547
570
  type GetUpgradeWebSocket = () => UpgradeWebSocket;
548
571
 
@@ -624,6 +647,25 @@ interface ActorOutput {
624
647
  error?: unknown;
625
648
  }
626
649
 
650
+ /** Correlation owned by the currently executing Core actor invocation. */
651
+ interface ActorInvocationTraceContext {
652
+ /** Ray ID the invocation received, absent when nothing upstream supplied one. */
653
+ readonly rayId?: string;
654
+ /** Core-owned invocation span context, absent when tracing is disabled. */
655
+ readonly span?: ActorInvocationSpanContext;
656
+ }
657
+ /** W3C span context of the Core invocation span. */
658
+ interface ActorInvocationSpanContext {
659
+ /** W3C trace identifier. */
660
+ readonly traceId: string;
661
+ /** W3C span identifier for the Core invocation. */
662
+ readonly spanId: string;
663
+ /** OpenTelemetry trace flags encoded as an integer. */
664
+ readonly traceFlags: number;
665
+ /** Optional vendor trace state inherited by the Core invocation. */
666
+ readonly tracestate?: string;
667
+ }
668
+
627
669
  declare const handleBrand: unique symbol;
628
670
  type OpaqueHandle<Name extends string> = {
629
671
  readonly [handleBrand]: Name;
@@ -642,6 +684,27 @@ interface RuntimeActorKeySegment {
642
684
  stringValue?: string;
643
685
  numberValue?: number;
644
686
  }
687
+ /** Resolves correlation at operation time so retained clients cannot freeze stale context. */
688
+ type CurrentActorInvocation = () => ActorInvocationTraceContext | undefined;
689
+ /** One open call from an actor out to another actor. */
690
+ interface RuntimeOutboundCall {
691
+ /**
692
+ * W3C context of the call's own span, to send to the callee so it parents to
693
+ * the call. Absent when tracing is disabled.
694
+ */
695
+ readonly span?: ActorInvocationSpanContext;
696
+ /**
697
+ * Records the call's outcome. `error` is the failure encoded the way the
698
+ * bridge encodes errors, so a structured error keeps its group and code.
699
+ */
700
+ finish(error?: string): void;
701
+ }
702
+ /**
703
+ * Opens the span covering one call out to another actor, or returns `undefined`
704
+ * outside an invocation or on a runtime without invocation telemetry. Callers
705
+ * send their own context when it returns nothing.
706
+ */
707
+ type StartCallSpan = (actorName: string, actionName: string) => RuntimeOutboundCall | undefined;
645
708
  interface RuntimeHttpRequest {
646
709
  method: string;
647
710
  uri: string;
@@ -802,12 +865,19 @@ interface RuntimeSqlBatchStatement {
802
865
  interface RuntimeSqlRunResult {
803
866
  changes: number;
804
867
  }
868
+ /** Sample rates for the traces an actor starts, in the shape core takes. */
869
+ interface RuntimeActorTracingConfig {
870
+ sampler?: number;
871
+ /** Keyed by flattened, dot-separated action name. */
872
+ actions?: Record<string, number>;
873
+ }
805
874
  interface RuntimeActorConfig {
806
875
  name?: string;
807
876
  icon?: string;
808
877
  hasDatabase?: boolean;
809
878
  remoteSqlite?: boolean;
810
879
  sqliteProfiling?: SqliteProfilingOptions;
880
+ tracing?: RuntimeActorTracingConfig;
811
881
  enableActorRuntimeSocket?: boolean;
812
882
  hasState?: boolean;
813
883
  canHibernateWebsocket?: boolean;
@@ -939,6 +1009,7 @@ interface CoreRuntime {
939
1009
  serveRegistry(registry: RegistryHandle, config: RuntimeServeConfig): Promise<void>;
940
1010
  waitRegistryReady(registry: RegistryHandle): Promise<void>;
941
1011
  shutdownRegistry(registry: RegistryHandle): Promise<void>;
1012
+ shutdownTelemetry?(): Promise<void>;
942
1013
  registryActorStopThresholdMs?(registry: RegistryHandle): Promise<number | undefined>;
943
1014
  handleServerlessRequest(registry: RegistryHandle, req: RuntimeServerlessRequest, onStreamEvent: RuntimeServerlessStreamCallback, cancelToken: CancellationTokenHandle, config: RuntimeServeConfig): Promise<RuntimeServerlessResponseHead>;
944
1015
  serveListener(registry: RegistryHandle, listener: RuntimeListenerConfig, config: RuntimeServeConfig): Promise<void>;
@@ -968,6 +1039,31 @@ interface CoreRuntime {
968
1039
  actorSaveState(ctx: ActorContextHandle, payload: RuntimeStateDeltaPayload): Promise<void>;
969
1040
  actorSaveStateAndWorkflowBatch(ctx: ActorContextHandle, writes: RuntimeWorkflowKvWrite[]): Promise<void>;
970
1041
  actorId(ctx: ActorContextHandle): string;
1042
+ /**
1043
+ * Runs one actor callback with `ctx` as the current invocation: operations
1044
+ * on retained handles for the same actor resolve to it, and its Core span
1045
+ * is the active OpenTelemetry span for the duration of `run`. Only actions,
1046
+ * raw requests, and queue dispatch run this way; other callbacks have no
1047
+ * invocation span.
1048
+ */
1049
+ runWithActorInvocationContext<T>(ctx: ActorContextHandle, run: () => T): T;
1050
+ /**
1051
+ * Correlation of the invocation currently executing for this actor, or
1052
+ * `undefined` outside an invocation or after it finished. A sampled-out
1053
+ * invocation can still expose valid span context for propagation.
1054
+ */
1055
+ actorInvocationTraceContext(ctx: ActorContextHandle): ActorInvocationTraceContext | undefined;
1056
+ /**
1057
+ * Opens the span covering one call this actor makes to another actor. See
1058
+ * `StartCallSpan` for when this returns nothing.
1059
+ */
1060
+ startCallSpan(ctx: ActorContextHandle, actorName: string, actionName: string): RuntimeOutboundCall | undefined;
1061
+ /** Backs `ActorRun.startWorkflowSpan`. */
1062
+ startWorkflowSpan(ctx: ActorContextHandle): Promise<WorkflowSpan>;
1063
+ /** Backs `ActorRun.runOutsideWorkflowSpan`. */
1064
+ runOutsideActorInvocationContext<T>(run: () => T): T;
1065
+ /** Identity of the invocation the caller runs in, usable as a cache key. */
1066
+ currentInvocationScope(): object | undefined;
971
1067
  actorName(ctx: ActorContextHandle): string;
972
1068
  actorKey(ctx: ActorContextHandle): RuntimeActorKeySegment[];
973
1069
  actorRegion(ctx: ActorContextHandle): string;
@@ -1306,7 +1402,7 @@ declare class ActorConnRaw {
1306
1402
  *
1307
1403
  * @protected
1308
1404
  */
1309
- constructor(client: ClientRaw, driver: EngineControlClient, params: unknown, getParams: (() => Promise<unknown>) | undefined, encoding: Encoding, actorResolutionState: ActorResolutionState, gatewayOptions?: ActorGatewayOptions);
1405
+ constructor(client: ClientRaw, driver: EngineControlClient, params: unknown, getParams: (() => Promise<unknown>) | undefined, encoding: Encoding, actorResolutionState: ActorResolutionState, gatewayOptions?: ActorGatewayOptions, currentActorInvocation?: CurrentActorInvocation);
1310
1406
  send(name: string, body: unknown, options: QueueSendWaitOptions): Promise<QueueSendResult>;
1311
1407
  send(name: string, body: unknown, options?: QueueSendNoWaitOptions): Promise<void>;
1312
1408
  /**
@@ -1442,7 +1538,7 @@ declare class ActorHandleRaw {
1442
1538
  *
1443
1539
  * @protected
1444
1540
  */
1445
- constructor(client: any, driver: EngineControlClient, params: unknown, getParams: (() => Promise<unknown>) | undefined, encoding: Encoding, actorResolutionState: ActorResolutionState, gatewayOptions?: ActorGatewayOptions, signal?: AbortSignal);
1541
+ constructor(client: any, driver: EngineControlClient, params: unknown, getParams: (() => Promise<unknown>) | undefined, encoding: Encoding, actorResolutionState: ActorResolutionState, gatewayOptions?: ActorGatewayOptions, signal?: AbortSignal, currentActorInvocation?: CurrentActorInvocation, startCallSpan?: StartCallSpan);
1446
1542
  send(name: string, body: unknown, options: QueueSendWaitOptions): Promise<QueueSendResult>;
1447
1543
  send(name: string, body: unknown, options?: QueueSendNoWaitOptions): Promise<void>;
1448
1544
  /**
@@ -1617,6 +1713,18 @@ interface CreateOptions extends QueryOptions {
1617
1713
  }
1618
1714
  declare const ACTOR_CONNS_SYMBOL: unique symbol;
1619
1715
  declare const CREATE_ACTOR_CONN_PROXY: unique symbol;
1716
+ /** Options for constructing a raw actor client. */
1717
+ interface ClientRawOptions {
1718
+ encoding?: Encoding;
1719
+ gateway?: ActorGatewayOptions;
1720
+ }
1721
+ /** Runtime-supplied wiring for clients created inside an actor. Set only by the native runtime. */
1722
+ interface ActorClientRuntimeOptions {
1723
+ /** Supplies the calling actor's invocation so actor-to-actor clients propagate its trace and ray ID. */
1724
+ currentActorInvocation?: CurrentActorInvocation;
1725
+ /** Opens and finishes the Core span covering an actor-to-actor call. */
1726
+ startCallSpan?: StartCallSpan;
1727
+ }
1620
1728
  /**
1621
1729
  * Client for managing & connecting to actors.
1622
1730
  *
@@ -1629,7 +1737,7 @@ declare class ClientRaw {
1629
1737
  /**
1630
1738
  * Creates an instance of Client.
1631
1739
  */
1632
- constructor(driver: EngineControlClient, encoding: Encoding | undefined, gatewayOptions?: ActorGatewayOptions);
1740
+ constructor(driver: EngineControlClient, options?: ClientRawOptions & ActorClientRuntimeOptions);
1633
1741
  /**
1634
1742
  * Gets a stateless handle to a actor by its ID.
1635
1743
  *
@@ -1936,6 +2044,28 @@ interface ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase
1936
2044
  client<T = any>(): ActorClientFor<T>;
1937
2045
  [key: string]: any;
1938
2046
  }
2047
+ /** @experimental How one run of a workflow function ended. */
2048
+ type WorkflowOutcome = "completed" | "sleeping" | "evicted" | "failed" | "cancelled";
2049
+ /** @experimental How one workflow step attempt ended. `retry` will be tried again, `failed` will not. */
2050
+ type WorkflowStepOutcome = "ok" | "retry" | "failed";
2051
+ /** @experimental One open run of a workflow function, traced as its own invocation. */
2052
+ interface WorkflowSpan {
2053
+ /** Runs `body` attributed to this run. */
2054
+ run<T>(body: () => Promise<T>): Promise<T>;
2055
+ /**
2056
+ * Called by a workflow engine before each attempt at a step. Returns
2057
+ * nothing when tracing is off.
2058
+ */
2059
+ startStep(name: string, attempt: number): WorkflowStepSpan | undefined;
2060
+ finish(outcome: WorkflowOutcome): Promise<void>;
2061
+ }
2062
+ /** @experimental One open attempt at one workflow step. */
2063
+ interface WorkflowStepSpan {
2064
+ /** Runs `body` attributed to this attempt. */
2065
+ run<T>(body: () => Promise<T>): Promise<T>;
2066
+ /** `error` is what the step callback threw. */
2067
+ finish(outcome: WorkflowStepOutcome, error?: unknown): void;
2068
+ }
1939
2069
  /** @experimental */
1940
2070
  interface ActorRun {
1941
2071
  /**
@@ -1943,6 +2073,13 @@ interface ActorRun {
1943
2073
  * ensures this actor's run handler is active, starting it if inactive.
1944
2074
  */
1945
2075
  setWakeAt(timestamp: number | null): Promise<void>;
2076
+ /** @experimental Called by a workflow engine each time it runs the workflow function. */
2077
+ startWorkflowSpan(): Promise<WorkflowSpan>;
2078
+ /**
2079
+ * @experimental Runs `body` outside the current workflow run, so a workflow
2080
+ * engine's own history storage stays out of the trace.
2081
+ */
2082
+ runOutsideWorkflowSpan<T>(body: () => T): T;
1946
2083
  }
1947
2084
  type ActionContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase extends AnyDatabaseProvider, TEvents extends EventSchemaConfig = Record<never, never>, TQueues extends QueueSchemaConfig = Record<never, never>> = ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase, TEvents, TQueues> & {
1948
2085
  conn: Conn<TState, TConnParams, TConnState, TVars, TInput, TDatabase, TEvents, TQueues>;
@@ -2008,6 +2145,10 @@ declare const ActorConfigSchema: z$1.ZodObject<{
2008
2145
  onWebSocket: z$1.ZodOptional<z$1.ZodCustom<(...args: unknown[]) => unknown, (...args: unknown[]) => unknown>>;
2009
2146
  actions: z$1.ZodDefault<z$1.ZodCustom<Record<string, unknown>, Record<string, unknown>>>;
2010
2147
  actionInputSchemas: z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodAny>>;
2148
+ tracing: z$1.ZodOptional<z$1.ZodObject<{
2149
+ sampler: z$1.ZodOptional<z$1.ZodNumber>;
2150
+ actions: z$1.ZodOptional<z$1.ZodType<ActionSampleRates, unknown, z$1.core.$ZodTypeInternals<ActionSampleRates, unknown>>>;
2151
+ }, z$1.core.$strict>>;
2011
2152
  connParamsSchema: z$1.ZodOptional<z$1.ZodAny>;
2012
2153
  events: z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodAny>>;
2013
2154
  queues: z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodAny>>;
@@ -2248,6 +2389,11 @@ interface BaseActorConfig<TState, TConnParams, TConnState, TVars, TInput, TDatab
2248
2389
  * May mirror nested action groups or use dot-separated low-level action names.
2249
2390
  */
2250
2391
  actionInputSchemas?: ActionInputSchemas;
2392
+ /**
2393
+ * How much of this actor's work is recorded in traces. Defaults to the
2394
+ * process sampler from `OTEL_TRACES_SAMPLER`.
2395
+ */
2396
+ tracing?: ActorTracingOptions<TActions>;
2251
2397
  /**
2252
2398
  * Optional schema for validating connection params in native runtimes.
2253
2399
  */
@@ -543,6 +543,29 @@ declare class RivetError extends Error {
543
543
  toString(): string;
544
544
  }
545
545
 
546
+ /** Sample rates keyed by action name, nested like the actor's action groups. */
547
+ interface ActionSampleRates {
548
+ readonly [name: string]: number | ActionSampleRates;
549
+ }
550
+ /**
551
+ * {@link ActionSampleRates} limited to the actions of one actor, so a rate for
552
+ * an action that does not exist is a compile error.
553
+ */
554
+ type ActionTraceSamplers<TActions> = {
555
+ readonly [K in keyof TActions]?: TActions[K] extends (...args: never[]) => unknown ? number : ActionTraceSamplers<TActions[K]>;
556
+ };
557
+ /** Trace sampling for one actor definition. */
558
+ interface ActorTracingOptions<TActions> {
559
+ /**
560
+ * Share of this actor's invocations that are recorded, from 0 to 1. A rate
561
+ * lowers what is recorded and never raises it, so an invocation inside a
562
+ * trace the caller did not record is not recorded at any rate.
563
+ */
564
+ readonly sampler?: number;
565
+ /** Rates that replace `sampler` for single actions. */
566
+ readonly actions?: ActionTraceSamplers<TActions>;
567
+ }
568
+
546
569
  type UpgradeWebSocket = (createEvents: (c: Context) => any) => Handler;
547
570
  type GetUpgradeWebSocket = () => UpgradeWebSocket;
548
571
 
@@ -624,6 +647,25 @@ interface ActorOutput {
624
647
  error?: unknown;
625
648
  }
626
649
 
650
+ /** Correlation owned by the currently executing Core actor invocation. */
651
+ interface ActorInvocationTraceContext {
652
+ /** Ray ID the invocation received, absent when nothing upstream supplied one. */
653
+ readonly rayId?: string;
654
+ /** Core-owned invocation span context, absent when tracing is disabled. */
655
+ readonly span?: ActorInvocationSpanContext;
656
+ }
657
+ /** W3C span context of the Core invocation span. */
658
+ interface ActorInvocationSpanContext {
659
+ /** W3C trace identifier. */
660
+ readonly traceId: string;
661
+ /** W3C span identifier for the Core invocation. */
662
+ readonly spanId: string;
663
+ /** OpenTelemetry trace flags encoded as an integer. */
664
+ readonly traceFlags: number;
665
+ /** Optional vendor trace state inherited by the Core invocation. */
666
+ readonly tracestate?: string;
667
+ }
668
+
627
669
  declare const handleBrand: unique symbol;
628
670
  type OpaqueHandle<Name extends string> = {
629
671
  readonly [handleBrand]: Name;
@@ -642,6 +684,27 @@ interface RuntimeActorKeySegment {
642
684
  stringValue?: string;
643
685
  numberValue?: number;
644
686
  }
687
+ /** Resolves correlation at operation time so retained clients cannot freeze stale context. */
688
+ type CurrentActorInvocation = () => ActorInvocationTraceContext | undefined;
689
+ /** One open call from an actor out to another actor. */
690
+ interface RuntimeOutboundCall {
691
+ /**
692
+ * W3C context of the call's own span, to send to the callee so it parents to
693
+ * the call. Absent when tracing is disabled.
694
+ */
695
+ readonly span?: ActorInvocationSpanContext;
696
+ /**
697
+ * Records the call's outcome. `error` is the failure encoded the way the
698
+ * bridge encodes errors, so a structured error keeps its group and code.
699
+ */
700
+ finish(error?: string): void;
701
+ }
702
+ /**
703
+ * Opens the span covering one call out to another actor, or returns `undefined`
704
+ * outside an invocation or on a runtime without invocation telemetry. Callers
705
+ * send their own context when it returns nothing.
706
+ */
707
+ type StartCallSpan = (actorName: string, actionName: string) => RuntimeOutboundCall | undefined;
645
708
  interface RuntimeHttpRequest {
646
709
  method: string;
647
710
  uri: string;
@@ -802,12 +865,19 @@ interface RuntimeSqlBatchStatement {
802
865
  interface RuntimeSqlRunResult {
803
866
  changes: number;
804
867
  }
868
+ /** Sample rates for the traces an actor starts, in the shape core takes. */
869
+ interface RuntimeActorTracingConfig {
870
+ sampler?: number;
871
+ /** Keyed by flattened, dot-separated action name. */
872
+ actions?: Record<string, number>;
873
+ }
805
874
  interface RuntimeActorConfig {
806
875
  name?: string;
807
876
  icon?: string;
808
877
  hasDatabase?: boolean;
809
878
  remoteSqlite?: boolean;
810
879
  sqliteProfiling?: SqliteProfilingOptions;
880
+ tracing?: RuntimeActorTracingConfig;
811
881
  enableActorRuntimeSocket?: boolean;
812
882
  hasState?: boolean;
813
883
  canHibernateWebsocket?: boolean;
@@ -939,6 +1009,7 @@ interface CoreRuntime {
939
1009
  serveRegistry(registry: RegistryHandle, config: RuntimeServeConfig): Promise<void>;
940
1010
  waitRegistryReady(registry: RegistryHandle): Promise<void>;
941
1011
  shutdownRegistry(registry: RegistryHandle): Promise<void>;
1012
+ shutdownTelemetry?(): Promise<void>;
942
1013
  registryActorStopThresholdMs?(registry: RegistryHandle): Promise<number | undefined>;
943
1014
  handleServerlessRequest(registry: RegistryHandle, req: RuntimeServerlessRequest, onStreamEvent: RuntimeServerlessStreamCallback, cancelToken: CancellationTokenHandle, config: RuntimeServeConfig): Promise<RuntimeServerlessResponseHead>;
944
1015
  serveListener(registry: RegistryHandle, listener: RuntimeListenerConfig, config: RuntimeServeConfig): Promise<void>;
@@ -968,6 +1039,31 @@ interface CoreRuntime {
968
1039
  actorSaveState(ctx: ActorContextHandle, payload: RuntimeStateDeltaPayload): Promise<void>;
969
1040
  actorSaveStateAndWorkflowBatch(ctx: ActorContextHandle, writes: RuntimeWorkflowKvWrite[]): Promise<void>;
970
1041
  actorId(ctx: ActorContextHandle): string;
1042
+ /**
1043
+ * Runs one actor callback with `ctx` as the current invocation: operations
1044
+ * on retained handles for the same actor resolve to it, and its Core span
1045
+ * is the active OpenTelemetry span for the duration of `run`. Only actions,
1046
+ * raw requests, and queue dispatch run this way; other callbacks have no
1047
+ * invocation span.
1048
+ */
1049
+ runWithActorInvocationContext<T>(ctx: ActorContextHandle, run: () => T): T;
1050
+ /**
1051
+ * Correlation of the invocation currently executing for this actor, or
1052
+ * `undefined` outside an invocation or after it finished. A sampled-out
1053
+ * invocation can still expose valid span context for propagation.
1054
+ */
1055
+ actorInvocationTraceContext(ctx: ActorContextHandle): ActorInvocationTraceContext | undefined;
1056
+ /**
1057
+ * Opens the span covering one call this actor makes to another actor. See
1058
+ * `StartCallSpan` for when this returns nothing.
1059
+ */
1060
+ startCallSpan(ctx: ActorContextHandle, actorName: string, actionName: string): RuntimeOutboundCall | undefined;
1061
+ /** Backs `ActorRun.startWorkflowSpan`. */
1062
+ startWorkflowSpan(ctx: ActorContextHandle): Promise<WorkflowSpan>;
1063
+ /** Backs `ActorRun.runOutsideWorkflowSpan`. */
1064
+ runOutsideActorInvocationContext<T>(run: () => T): T;
1065
+ /** Identity of the invocation the caller runs in, usable as a cache key. */
1066
+ currentInvocationScope(): object | undefined;
971
1067
  actorName(ctx: ActorContextHandle): string;
972
1068
  actorKey(ctx: ActorContextHandle): RuntimeActorKeySegment[];
973
1069
  actorRegion(ctx: ActorContextHandle): string;
@@ -1306,7 +1402,7 @@ declare class ActorConnRaw {
1306
1402
  *
1307
1403
  * @protected
1308
1404
  */
1309
- constructor(client: ClientRaw, driver: EngineControlClient, params: unknown, getParams: (() => Promise<unknown>) | undefined, encoding: Encoding, actorResolutionState: ActorResolutionState, gatewayOptions?: ActorGatewayOptions);
1405
+ constructor(client: ClientRaw, driver: EngineControlClient, params: unknown, getParams: (() => Promise<unknown>) | undefined, encoding: Encoding, actorResolutionState: ActorResolutionState, gatewayOptions?: ActorGatewayOptions, currentActorInvocation?: CurrentActorInvocation);
1310
1406
  send(name: string, body: unknown, options: QueueSendWaitOptions): Promise<QueueSendResult>;
1311
1407
  send(name: string, body: unknown, options?: QueueSendNoWaitOptions): Promise<void>;
1312
1408
  /**
@@ -1442,7 +1538,7 @@ declare class ActorHandleRaw {
1442
1538
  *
1443
1539
  * @protected
1444
1540
  */
1445
- constructor(client: any, driver: EngineControlClient, params: unknown, getParams: (() => Promise<unknown>) | undefined, encoding: Encoding, actorResolutionState: ActorResolutionState, gatewayOptions?: ActorGatewayOptions, signal?: AbortSignal);
1541
+ constructor(client: any, driver: EngineControlClient, params: unknown, getParams: (() => Promise<unknown>) | undefined, encoding: Encoding, actorResolutionState: ActorResolutionState, gatewayOptions?: ActorGatewayOptions, signal?: AbortSignal, currentActorInvocation?: CurrentActorInvocation, startCallSpan?: StartCallSpan);
1446
1542
  send(name: string, body: unknown, options: QueueSendWaitOptions): Promise<QueueSendResult>;
1447
1543
  send(name: string, body: unknown, options?: QueueSendNoWaitOptions): Promise<void>;
1448
1544
  /**
@@ -1617,6 +1713,18 @@ interface CreateOptions extends QueryOptions {
1617
1713
  }
1618
1714
  declare const ACTOR_CONNS_SYMBOL: unique symbol;
1619
1715
  declare const CREATE_ACTOR_CONN_PROXY: unique symbol;
1716
+ /** Options for constructing a raw actor client. */
1717
+ interface ClientRawOptions {
1718
+ encoding?: Encoding;
1719
+ gateway?: ActorGatewayOptions;
1720
+ }
1721
+ /** Runtime-supplied wiring for clients created inside an actor. Set only by the native runtime. */
1722
+ interface ActorClientRuntimeOptions {
1723
+ /** Supplies the calling actor's invocation so actor-to-actor clients propagate its trace and ray ID. */
1724
+ currentActorInvocation?: CurrentActorInvocation;
1725
+ /** Opens and finishes the Core span covering an actor-to-actor call. */
1726
+ startCallSpan?: StartCallSpan;
1727
+ }
1620
1728
  /**
1621
1729
  * Client for managing & connecting to actors.
1622
1730
  *
@@ -1629,7 +1737,7 @@ declare class ClientRaw {
1629
1737
  /**
1630
1738
  * Creates an instance of Client.
1631
1739
  */
1632
- constructor(driver: EngineControlClient, encoding: Encoding | undefined, gatewayOptions?: ActorGatewayOptions);
1740
+ constructor(driver: EngineControlClient, options?: ClientRawOptions & ActorClientRuntimeOptions);
1633
1741
  /**
1634
1742
  * Gets a stateless handle to a actor by its ID.
1635
1743
  *
@@ -1936,6 +2044,28 @@ interface ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase
1936
2044
  client<T = any>(): ActorClientFor<T>;
1937
2045
  [key: string]: any;
1938
2046
  }
2047
+ /** @experimental How one run of a workflow function ended. */
2048
+ type WorkflowOutcome = "completed" | "sleeping" | "evicted" | "failed" | "cancelled";
2049
+ /** @experimental How one workflow step attempt ended. `retry` will be tried again, `failed` will not. */
2050
+ type WorkflowStepOutcome = "ok" | "retry" | "failed";
2051
+ /** @experimental One open run of a workflow function, traced as its own invocation. */
2052
+ interface WorkflowSpan {
2053
+ /** Runs `body` attributed to this run. */
2054
+ run<T>(body: () => Promise<T>): Promise<T>;
2055
+ /**
2056
+ * Called by a workflow engine before each attempt at a step. Returns
2057
+ * nothing when tracing is off.
2058
+ */
2059
+ startStep(name: string, attempt: number): WorkflowStepSpan | undefined;
2060
+ finish(outcome: WorkflowOutcome): Promise<void>;
2061
+ }
2062
+ /** @experimental One open attempt at one workflow step. */
2063
+ interface WorkflowStepSpan {
2064
+ /** Runs `body` attributed to this attempt. */
2065
+ run<T>(body: () => Promise<T>): Promise<T>;
2066
+ /** `error` is what the step callback threw. */
2067
+ finish(outcome: WorkflowStepOutcome, error?: unknown): void;
2068
+ }
1939
2069
  /** @experimental */
1940
2070
  interface ActorRun {
1941
2071
  /**
@@ -1943,6 +2073,13 @@ interface ActorRun {
1943
2073
  * ensures this actor's run handler is active, starting it if inactive.
1944
2074
  */
1945
2075
  setWakeAt(timestamp: number | null): Promise<void>;
2076
+ /** @experimental Called by a workflow engine each time it runs the workflow function. */
2077
+ startWorkflowSpan(): Promise<WorkflowSpan>;
2078
+ /**
2079
+ * @experimental Runs `body` outside the current workflow run, so a workflow
2080
+ * engine's own history storage stays out of the trace.
2081
+ */
2082
+ runOutsideWorkflowSpan<T>(body: () => T): T;
1946
2083
  }
1947
2084
  type ActionContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase extends AnyDatabaseProvider, TEvents extends EventSchemaConfig = Record<never, never>, TQueues extends QueueSchemaConfig = Record<never, never>> = ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase, TEvents, TQueues> & {
1948
2085
  conn: Conn<TState, TConnParams, TConnState, TVars, TInput, TDatabase, TEvents, TQueues>;
@@ -2008,6 +2145,10 @@ declare const ActorConfigSchema: z$1.ZodObject<{
2008
2145
  onWebSocket: z$1.ZodOptional<z$1.ZodCustom<(...args: unknown[]) => unknown, (...args: unknown[]) => unknown>>;
2009
2146
  actions: z$1.ZodDefault<z$1.ZodCustom<Record<string, unknown>, Record<string, unknown>>>;
2010
2147
  actionInputSchemas: z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodAny>>;
2148
+ tracing: z$1.ZodOptional<z$1.ZodObject<{
2149
+ sampler: z$1.ZodOptional<z$1.ZodNumber>;
2150
+ actions: z$1.ZodOptional<z$1.ZodType<ActionSampleRates, unknown, z$1.core.$ZodTypeInternals<ActionSampleRates, unknown>>>;
2151
+ }, z$1.core.$strict>>;
2011
2152
  connParamsSchema: z$1.ZodOptional<z$1.ZodAny>;
2012
2153
  events: z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodAny>>;
2013
2154
  queues: z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodAny>>;
@@ -2248,6 +2389,11 @@ interface BaseActorConfig<TState, TConnParams, TConnState, TVars, TInput, TDatab
2248
2389
  * May mirror nested action groups or use dot-separated low-level action names.
2249
2390
  */
2250
2391
  actionInputSchemas?: ActionInputSchemas;
2392
+ /**
2393
+ * How much of this actor's work is recorded in traces. Defaults to the
2394
+ * process sampler from `OTEL_TRACES_SAMPLER`.
2395
+ */
2396
+ tracing?: ActorTracingOptions<TActions>;
2251
2397
  /**
2252
2398
  * Optional schema for validating connection params in native runtimes.
2253
2399
  */
@@ -33,6 +33,26 @@ function flattenActionInputSchemas(actions, schemas) {
33
33
  }
34
34
  return flattened;
35
35
  }
36
+ function flattenActionTraceSamplers(actions, rates = {}) {
37
+ const handlers = flattenActionHandlers(actions);
38
+ const flattened = Object.fromEntries(flattenSampleRates(rates, []));
39
+ for (const name of Object.keys(flattened)) {
40
+ if (!Object.hasOwn(handlers, name)) {
41
+ throw new TypeError(
42
+ `tracing.actions.${name} does not name an action`
43
+ );
44
+ }
45
+ }
46
+ return flattened;
47
+ }
48
+ function flattenSampleRates(rates, path) {
49
+ return Object.entries(rates).flatMap(
50
+ ([segment, rate]) => {
51
+ const childPath = [...path, segment];
52
+ return typeof rate === "number" ? [[childPath.join("."), rate]] : flattenSampleRates(rate, childPath);
53
+ }
54
+ );
55
+ }
36
56
  function collectActionEntries(actions) {
37
57
  const entries = [];
38
58
  const names = /* @__PURE__ */ new Set();
@@ -228,6 +248,10 @@ var InstanceActorOptionsSchema = InstanceActorOptionsBaseSchema.prefault(() => (
228
248
  var ActorOptionsSchema = GlobalActorOptionsBaseSchema.extend(
229
249
  InstanceActorOptionsBaseSchema.shape
230
250
  ).strict().prefault(() => ({}));
251
+ var zSampleRate = z.number().min(0).max(1);
252
+ var zActionSampleRates = z.lazy(
253
+ () => z.record(z.string(), z.union([zSampleRate, zActionSampleRates]))
254
+ );
231
255
  var ActorConfigSchema = z.object({
232
256
  onCreate: zFunction().optional(),
233
257
  onDestroy: zFunction().optional(),
@@ -244,6 +268,10 @@ var ActorConfigSchema = z.object({
244
268
  onWebSocket: zFunction().optional(),
245
269
  actions: zActionTree.default(() => ({})),
246
270
  actionInputSchemas: z.record(z.string(), z.any()).optional(),
271
+ tracing: z.object({
272
+ sampler: zSampleRate.optional(),
273
+ actions: zActionSampleRates.optional()
274
+ }).strict().optional(),
247
275
  connParamsSchema: z.any().optional(),
248
276
  events: z.record(z.string(), z.any()).optional(),
249
277
  queues: z.record(z.string(), z.any()).optional(),
@@ -391,6 +419,9 @@ var DocActorConfigSchema = z.object({
391
419
  actionInputSchemas: z.record(z.string(), z.unknown()).optional().describe(
392
420
  "Optional schemas for validating action argument tuples in native runtimes. May mirror nested action groups or use dot-separated low-level action names."
393
421
  ),
422
+ tracing: z.unknown().optional().describe(
423
+ "Trace sampling for this actor: `sampler` is the share of its invocations that are recorded, from 0 to 1, with optional per-action rates under `actions`. A rate lowers what is recorded and never raises it. Defaults to the process sampler from OTEL_TRACES_SAMPLER."
424
+ ),
394
425
  connParamsSchema: z.unknown().optional().describe(
395
426
  "Optional schema for validating connection params in native runtimes."
396
427
  ),
@@ -420,7 +451,7 @@ function isRivetErrorCode(error, group, code) {
420
451
  // package.json
421
452
  var package_default = {
422
453
  name: "rivetkit",
423
- version: "2.3.18",
454
+ version: "2.3.20",
424
455
  description: "Lightweight libraries for building stateful actors on edge platforms",
425
456
  license: "Apache-2.0",
426
457
  keywords: [
@@ -629,6 +660,7 @@ var package_default = {
629
660
  },
630
661
  dependencies: {
631
662
  "@hono/zod-openapi": "^1.1.5",
663
+ "@opentelemetry/api": "^1.1.0",
632
664
  "@rivet-dev/agent-os-core": "^0.1.1",
633
665
  "@rivet-dev/services": "^0.1.5",
634
666
  "@rivetkit/bare-ts": "^0.6.2",
@@ -655,6 +687,7 @@ var package_default = {
655
687
  "@copilotkit/llmock": "^1.6.0",
656
688
  "@hono/node-server": "^1.18.2",
657
689
  "@hono/node-ws": "^1.1.1",
690
+ "@opentelemetry/sdk-trace-node": "2.11.0",
658
691
  "@rivet-dev/agent-os-common": "*",
659
692
  "@rivet-dev/agent-os-pi": "^0.1.1",
660
693
  "@standard-schema/spec": "^1.0.0",
@@ -887,12 +920,14 @@ var ActorDefinition = class {
887
920
  }
888
921
  };
889
922
  function actor(input) {
923
+ var _a;
890
924
  warnDeprecatedOptionKeys(
891
925
  input == null ? void 0 : input.options
892
926
  );
893
927
  const config = ActorConfigSchema.parse(input);
894
928
  flattenActionHandlers(config.actions);
895
929
  flattenActionInputSchemas(config.actions, config.actionInputSchemas);
930
+ flattenActionTraceSamplers(config.actions, (_a = config.tracing) == null ? void 0 : _a.actions);
896
931
  return new ActorDefinition(config);
897
932
  }
898
933