@rivetkit/effect 0.0.0-fix-get-or-create-w.7070ab4 → 0.0.0-fix-envoy-late-frames.e9d435c

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 (46) hide show
  1. package/dist/Action.d.ts +1 -1
  2. package/dist/Action.js +1 -1
  3. package/dist/Actor.d.ts +26 -1
  4. package/dist/Actor.d.ts.map +1 -1
  5. package/dist/Actor.js +1 -0
  6. package/dist/Actor.js.map +1 -1
  7. package/dist/Registry.d.ts +2 -2
  8. package/dist/Registry.d.ts.map +1 -1
  9. package/dist/Registry.js +1 -2
  10. package/dist/Registry.js.map +1 -1
  11. package/dist/RivetError.d.ts +19 -3
  12. package/dist/RivetError.d.ts.map +1 -1
  13. package/dist/RivetError.js +60 -26
  14. package/dist/RivetError.js.map +1 -1
  15. package/dist/internal/ActionDispatcher.d.ts +3 -3
  16. package/dist/internal/ActionDispatcher.d.ts.map +1 -1
  17. package/dist/internal/ActionDispatcher.js +23 -1
  18. package/dist/internal/ActionDispatcher.js.map +1 -1
  19. package/dist/internal/ActorInstanceManager.d.ts +7 -0
  20. package/dist/internal/ActorInstanceManager.d.ts.map +1 -1
  21. package/dist/internal/ActorInstanceManager.js +40 -0
  22. package/dist/internal/ActorInstanceManager.js.map +1 -1
  23. package/dist/internal/logging.d.ts.map +1 -1
  24. package/dist/internal/logging.js +2 -2
  25. package/dist/internal/logging.js.map +1 -1
  26. package/dist/mod.d.ts +1 -0
  27. package/dist/mod.d.ts.map +1 -1
  28. package/dist/mod.js +1 -0
  29. package/dist/mod.js.map +1 -1
  30. package/package.json +10 -9
  31. package/src/Action.ts +1 -1
  32. package/src/Actor.ts +28 -0
  33. package/src/Registry.ts +2 -2
  34. package/src/RivetError.ts +66 -26
  35. package/src/internal/ActionDispatcher.ts +35 -4
  36. package/src/internal/ActorInstanceManager.ts +54 -0
  37. package/src/internal/logging.ts +3 -3
  38. package/src/mod.ts +1 -0
  39. package/src/Actor.test-d.ts +0 -641
  40. package/src/Actor.test.ts +0 -209
  41. package/src/Client.test.ts +0 -206
  42. package/src/Registry.test-d.ts +0 -126
  43. package/src/Registry.test.ts +0 -400
  44. package/src/RivetError.test.ts +0 -199
  45. package/src/State.test.ts +0 -340
  46. package/src/internal/logging.test.ts +0 -274
package/src/RivetError.ts CHANGED
@@ -4,7 +4,7 @@ import * as RivetkitErrors from "rivetkit/errors";
4
4
  const ReasonTypeId = "~@rivetkit/effect/RivetError/Reason" as const;
5
5
  const TypeId = "~@rivetkit/effect/RivetError" as const;
6
6
 
7
- export class Forbidden extends Schema.TaggedErrorClass<Forbidden>(
7
+ export class Forbidden extends Schema.TaggedError<Forbidden>(
8
8
  `${ReasonTypeId}/Forbidden`,
9
9
  )("Forbidden", {
10
10
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -39,7 +39,7 @@ export class Forbidden extends Schema.TaggedErrorClass<Forbidden>(
39
39
  }
40
40
  }
41
41
 
42
- export class ActorNotFound extends Schema.TaggedErrorClass<ActorNotFound>(
42
+ export class ActorNotFound extends Schema.TaggedError<ActorNotFound>(
43
43
  `${ReasonTypeId}/ActorNotFound`,
44
44
  )("ActorNotFound", {
45
45
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -74,7 +74,7 @@ export class ActorNotFound extends Schema.TaggedErrorClass<ActorNotFound>(
74
74
  }
75
75
  }
76
76
 
77
- export class ActorStopping extends Schema.TaggedErrorClass<ActorStopping>(
77
+ export class ActorStopping extends Schema.TaggedError<ActorStopping>(
78
78
  `${ReasonTypeId}/ActorStopping`,
79
79
  )("ActorStopping", {
80
80
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -109,7 +109,7 @@ export class ActorStopping extends Schema.TaggedErrorClass<ActorStopping>(
109
109
  }
110
110
  }
111
111
 
112
- export class ActorRestarting extends Schema.TaggedErrorClass<ActorRestarting>(
112
+ export class ActorRestarting extends Schema.TaggedError<ActorRestarting>(
113
113
  `${ReasonTypeId}/ActorRestarting`,
114
114
  )("ActorRestarting", {
115
115
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -152,7 +152,7 @@ export class ActorRestarting extends Schema.TaggedErrorClass<ActorRestarting>(
152
152
  }
153
153
  }
154
154
 
155
- export class ActionNotFound extends Schema.TaggedErrorClass<ActionNotFound>(
155
+ export class ActionNotFound extends Schema.TaggedError<ActionNotFound>(
156
156
  `${ReasonTypeId}/ActionNotFound`,
157
157
  )("ActionNotFound", {
158
158
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -187,7 +187,7 @@ export class ActionNotFound extends Schema.TaggedErrorClass<ActionNotFound>(
187
187
  }
188
188
  }
189
189
 
190
- export class ActionTimedOut extends Schema.TaggedErrorClass<ActionTimedOut>(
190
+ export class ActionTimedOut extends Schema.TaggedError<ActionTimedOut>(
191
191
  `${ReasonTypeId}/ActionTimedOut`,
192
192
  )("ActionTimedOut", { cause: Schema.instanceOf(RivetkitErrors.RivetError) }) {
193
193
  readonly [ReasonTypeId] = ReasonTypeId;
@@ -220,7 +220,7 @@ export class ActionTimedOut extends Schema.TaggedErrorClass<ActionTimedOut>(
220
220
  }
221
221
  }
222
222
 
223
- export class ActionAborted extends Schema.TaggedErrorClass<ActionAborted>(
223
+ export class ActionAborted extends Schema.TaggedError<ActionAborted>(
224
224
  `${ReasonTypeId}/ActionAborted`,
225
225
  )("ActionAborted", {
226
226
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -255,7 +255,7 @@ export class ActionAborted extends Schema.TaggedErrorClass<ActionAborted>(
255
255
  }
256
256
  }
257
257
 
258
- export class ActorOverloaded extends Schema.TaggedErrorClass<ActorOverloaded>(
258
+ export class ActorOverloaded extends Schema.TaggedError<ActorOverloaded>(
259
259
  `${ReasonTypeId}/ActorOverloaded`,
260
260
  )("ActorOverloaded", {
261
261
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -290,7 +290,7 @@ export class ActorOverloaded extends Schema.TaggedErrorClass<ActorOverloaded>(
290
290
  }
291
291
  }
292
292
 
293
- export class IncomingMessageTooLong extends Schema.TaggedErrorClass<IncomingMessageTooLong>(
293
+ export class IncomingMessageTooLong extends Schema.TaggedError<IncomingMessageTooLong>(
294
294
  `${ReasonTypeId}/IncomingMessageTooLong`,
295
295
  )("IncomingMessageTooLong", {
296
296
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -325,7 +325,7 @@ export class IncomingMessageTooLong extends Schema.TaggedErrorClass<IncomingMess
325
325
  }
326
326
  }
327
327
 
328
- export class OutgoingMessageTooLong extends Schema.TaggedErrorClass<OutgoingMessageTooLong>(
328
+ export class OutgoingMessageTooLong extends Schema.TaggedError<OutgoingMessageTooLong>(
329
329
  `${ReasonTypeId}/OutgoingMessageTooLong`,
330
330
  )("OutgoingMessageTooLong", {
331
331
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -360,7 +360,7 @@ export class OutgoingMessageTooLong extends Schema.TaggedErrorClass<OutgoingMess
360
360
  }
361
361
  }
362
362
 
363
- export class InvalidEncoding extends Schema.TaggedErrorClass<InvalidEncoding>(
363
+ export class InvalidEncoding extends Schema.TaggedError<InvalidEncoding>(
364
364
  `${ReasonTypeId}/InvalidEncoding`,
365
365
  )("InvalidEncoding", {
366
366
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -395,7 +395,7 @@ export class InvalidEncoding extends Schema.TaggedErrorClass<InvalidEncoding>(
395
395
  }
396
396
  }
397
397
 
398
- export class InvalidRequest extends Schema.TaggedErrorClass<InvalidRequest>(
398
+ export class InvalidRequest extends Schema.TaggedError<InvalidRequest>(
399
399
  `${ReasonTypeId}/InvalidRequest`,
400
400
  )("InvalidRequest", {
401
401
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -430,7 +430,42 @@ export class InvalidRequest extends Schema.TaggedErrorClass<InvalidRequest>(
430
430
  }
431
431
  }
432
432
 
433
- export class GuardActorReadyTimeout extends Schema.TaggedErrorClass<GuardActorReadyTimeout>(
433
+ export class GuardRequestTimeout extends Schema.TaggedError<GuardRequestTimeout>(
434
+ `${ReasonTypeId}/GuardRequestTimeout`,
435
+ )("GuardRequestTimeout", {
436
+ cause: Schema.instanceOf(RivetkitErrors.RivetError),
437
+ }) {
438
+ readonly [ReasonTypeId] = ReasonTypeId;
439
+ override get message() {
440
+ return this.cause.message;
441
+ }
442
+ get group() {
443
+ return this.cause.group;
444
+ }
445
+ get code() {
446
+ return this.cause.code;
447
+ }
448
+ get metadata() {
449
+ return this.cause.metadata;
450
+ }
451
+ get actor() {
452
+ return this.cause.actor;
453
+ }
454
+ get statusCode() {
455
+ return this.cause.statusCode;
456
+ }
457
+ get public() {
458
+ return this.cause.public;
459
+ }
460
+ get rayId() {
461
+ return this.cause.rayId;
462
+ }
463
+ get isRetryable(): boolean {
464
+ return false;
465
+ }
466
+ }
467
+
468
+ export class GuardActorReadyTimeout extends Schema.TaggedError<GuardActorReadyTimeout>(
434
469
  `${ReasonTypeId}/GuardActorReadyTimeout`,
435
470
  )("GuardActorReadyTimeout", {
436
471
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -465,7 +500,7 @@ export class GuardActorReadyTimeout extends Schema.TaggedErrorClass<GuardActorRe
465
500
  }
466
501
  }
467
502
 
468
- export class GuardActorWakeRetriesExceeded extends Schema.TaggedErrorClass<GuardActorWakeRetriesExceeded>(
503
+ export class GuardActorWakeRetriesExceeded extends Schema.TaggedError<GuardActorWakeRetriesExceeded>(
469
504
  `${ReasonTypeId}/GuardActorWakeRetriesExceeded`,
470
505
  )("GuardActorWakeRetriesExceeded", {
471
506
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -500,7 +535,7 @@ export class GuardActorWakeRetriesExceeded extends Schema.TaggedErrorClass<Guard
500
535
  }
501
536
  }
502
537
 
503
- export class GuardActorRunnerFailed extends Schema.TaggedErrorClass<GuardActorRunnerFailed>(
538
+ export class GuardActorRunnerFailed extends Schema.TaggedError<GuardActorRunnerFailed>(
504
539
  `${ReasonTypeId}/GuardActorRunnerFailed`,
505
540
  )("GuardActorRunnerFailed", {
506
541
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -535,7 +570,7 @@ export class GuardActorRunnerFailed extends Schema.TaggedErrorClass<GuardActorRu
535
570
  }
536
571
  }
537
572
 
538
- export class GuardServiceUnavailable extends Schema.TaggedErrorClass<GuardServiceUnavailable>(
573
+ export class GuardServiceUnavailable extends Schema.TaggedError<GuardServiceUnavailable>(
539
574
  `${ReasonTypeId}/GuardServiceUnavailable`,
540
575
  )("GuardServiceUnavailable", {
541
576
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -570,7 +605,7 @@ export class GuardServiceUnavailable extends Schema.TaggedErrorClass<GuardServic
570
605
  }
571
606
  }
572
607
 
573
- export class GuardActorStoppedWhileWaiting extends Schema.TaggedErrorClass<GuardActorStoppedWhileWaiting>(
608
+ export class GuardActorStoppedWhileWaiting extends Schema.TaggedError<GuardActorStoppedWhileWaiting>(
574
609
  `${ReasonTypeId}/GuardActorStoppedWhileWaiting`,
575
610
  )("GuardActorStoppedWhileWaiting", {
576
611
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -605,7 +640,7 @@ export class GuardActorStoppedWhileWaiting extends Schema.TaggedErrorClass<Guard
605
640
  }
606
641
  }
607
642
 
608
- export class GuardTunnelRequestAborted extends Schema.TaggedErrorClass<GuardTunnelRequestAborted>(
643
+ export class GuardTunnelRequestAborted extends Schema.TaggedError<GuardTunnelRequestAborted>(
609
644
  `${ReasonTypeId}/GuardTunnelRequestAborted`,
610
645
  )("GuardTunnelRequestAborted", {
611
646
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -640,7 +675,7 @@ export class GuardTunnelRequestAborted extends Schema.TaggedErrorClass<GuardTunn
640
675
  }
641
676
  }
642
677
 
643
- export class GuardTunnelMessageTimeout extends Schema.TaggedErrorClass<GuardTunnelMessageTimeout>(
678
+ export class GuardTunnelMessageTimeout extends Schema.TaggedError<GuardTunnelMessageTimeout>(
644
679
  `${ReasonTypeId}/GuardTunnelMessageTimeout`,
645
680
  )("GuardTunnelMessageTimeout", {
646
681
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -675,7 +710,7 @@ export class GuardTunnelMessageTimeout extends Schema.TaggedErrorClass<GuardTunn
675
710
  }
676
711
  }
677
712
 
678
- export class GuardTunnelResponseClosed extends Schema.TaggedErrorClass<GuardTunnelResponseClosed>(
713
+ export class GuardTunnelResponseClosed extends Schema.TaggedError<GuardTunnelResponseClosed>(
679
714
  `${ReasonTypeId}/GuardTunnelResponseClosed`,
680
715
  )("GuardTunnelResponseClosed", {
681
716
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -710,7 +745,7 @@ export class GuardTunnelResponseClosed extends Schema.TaggedErrorClass<GuardTunn
710
745
  }
711
746
  }
712
747
 
713
- export class GuardGatewayResponseStartTimeout extends Schema.TaggedErrorClass<GuardGatewayResponseStartTimeout>(
748
+ export class GuardGatewayResponseStartTimeout extends Schema.TaggedError<GuardGatewayResponseStartTimeout>(
714
749
  `${ReasonTypeId}/GuardGatewayResponseStartTimeout`,
715
750
  )("GuardGatewayResponseStartTimeout", {
716
751
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -745,7 +780,7 @@ export class GuardGatewayResponseStartTimeout extends Schema.TaggedErrorClass<Gu
745
780
  }
746
781
  }
747
782
 
748
- export class InternalError extends Schema.TaggedErrorClass<InternalError>(
783
+ export class InternalError extends Schema.TaggedError<InternalError>(
749
784
  `${ReasonTypeId}/InternalError`,
750
785
  )("InternalError", {
751
786
  cause: Schema.instanceOf(RivetkitErrors.RivetError),
@@ -780,7 +815,7 @@ export class InternalError extends Schema.TaggedErrorClass<InternalError>(
780
815
  }
781
816
  }
782
817
 
783
- export class ActionErrorDecodeFailed extends Schema.TaggedErrorClass<ActionErrorDecodeFailed>(
818
+ export class ActionErrorDecodeFailed extends Schema.TaggedError<ActionErrorDecodeFailed>(
784
819
  `${ReasonTypeId}/ActionErrorDecodeFailed`,
785
820
  )("ActionErrorDecodeFailed", {
786
821
  cause: Schema.instanceOf(Schema.SchemaError),
@@ -826,7 +861,7 @@ export class ActionErrorDecodeFailed extends Schema.TaggedErrorClass<ActionError
826
861
  * receive those errors **typed** in the error channel; this reason is
827
862
  * the catch-all for everything else.
828
863
  */
829
- export class UnknownUserError extends Schema.TaggedErrorClass<UnknownUserError>(
864
+ export class UnknownUserError extends Schema.TaggedError<UnknownUserError>(
830
865
  `${ReasonTypeId}/UnknownUserError`,
831
866
  )("UnknownUserError", { cause: Schema.instanceOf(RivetkitErrors.RivetError) }) {
832
867
  readonly [ReasonTypeId] = ReasonTypeId;
@@ -864,7 +899,7 @@ export class UnknownUserError extends Schema.TaggedErrorClass<UnknownUserError>(
864
899
  * not recognize yet, and for malformed non-Rivet failures. Known wire
865
900
  * fields are mirrored when present, while `cause` preserves the raw input.
866
901
  */
867
- export class UnknownError extends Schema.TaggedErrorClass<UnknownError>(
902
+ export class UnknownError extends Schema.TaggedError<UnknownError>(
868
903
  `${ReasonTypeId}/UnknownError`,
869
904
  )("UnknownError", {
870
905
  message: Schema.String,
@@ -924,6 +959,7 @@ export type RivetErrorReason =
924
959
  | OutgoingMessageTooLong
925
960
  | InvalidEncoding
926
961
  | InvalidRequest
962
+ | GuardRequestTimeout
927
963
  | GuardActorReadyTimeout
928
964
  | GuardActorWakeRetriesExceeded
929
965
  | GuardActorRunnerFailed
@@ -952,6 +988,7 @@ export const RivetErrorReason: Schema.Union<
952
988
  typeof OutgoingMessageTooLong,
953
989
  typeof InvalidEncoding,
954
990
  typeof InvalidRequest,
991
+ typeof GuardRequestTimeout,
955
992
  typeof GuardActorReadyTimeout,
956
993
  typeof GuardActorWakeRetriesExceeded,
957
994
  typeof GuardActorRunnerFailed,
@@ -979,6 +1016,7 @@ export const RivetErrorReason: Schema.Union<
979
1016
  OutgoingMessageTooLong,
980
1017
  InvalidEncoding,
981
1018
  InvalidRequest,
1019
+ GuardRequestTimeout,
982
1020
  GuardActorReadyTimeout,
983
1021
  GuardActorWakeRetriesExceeded,
984
1022
  GuardActorRunnerFailed,
@@ -1018,7 +1056,7 @@ export const isRivetErrorReason = (u: unknown): u is RivetErrorReason =>
1018
1056
  * arrive in the typed error channel separately and do NOT flow through
1019
1057
  * `RivetError`.
1020
1058
  */
1021
- export class RivetError extends Schema.TaggedErrorClass<RivetError>(
1059
+ export class RivetError extends Schema.TaggedError<RivetError>(
1022
1060
  "@rivetkit/effect/RivetError",
1023
1061
  )("RivetError", {
1024
1062
  reason: RivetErrorReason,
@@ -1108,6 +1146,8 @@ const reasonByCode: { [key: string]: MakeRivetErrorReason | undefined } = {
1108
1146
  new OutgoingMessageTooLong({ cause: error }),
1109
1147
  "encoding.invalid": (error) => new InvalidEncoding({ cause: error }),
1110
1148
  "request.invalid": (error) => new InvalidRequest({ cause: error }),
1149
+ "guard.request_timeout": (error) =>
1150
+ new GuardRequestTimeout({ cause: error }),
1111
1151
  "guard.actor_ready_timeout": (error) =>
1112
1152
  new GuardActorReadyTimeout({ cause: error }),
1113
1153
  "guard.actor_wake_retries_exceeded": (error) =>
@@ -6,12 +6,17 @@ import {
6
6
  Option,
7
7
  Record,
8
8
  Schema,
9
+ type Scope,
9
10
  Tracer,
10
11
  } from "effect";
11
12
  import * as Rivetkit from "rivetkit";
12
13
  import type * as Action from "../Action.ts";
13
- import type { ActionHandlersFrom, ActionRequest, Actor } from "../Actor.ts";
14
- import type * as Client from "../Client.ts";
14
+ import type {
15
+ ActionConnection,
16
+ ActionHandlersFrom,
17
+ ActionRequest,
18
+ Actor,
19
+ } from "../Actor.ts";
15
20
  import * as ActionErrorEnvelope from "./ActionErrorEnvelope.ts";
16
21
  import { makeActorLogAnnotations } from "./logging.ts";
17
22
  import { readTraceMeta, rpcSystem } from "./tracing.ts";
@@ -23,6 +28,7 @@ export type Instance<ActionHandlers> = {
23
28
  effect: Effect.Effect<A, E, any>,
24
29
  options?: Effect.RunOptions,
25
30
  ) => Fiber.Fiber<A, E>;
31
+ readonly connectionScope: (connectionId: string) => Scope.Scope;
26
32
  };
27
33
 
28
34
  export const make = <
@@ -54,9 +60,14 @@ export const make = <
54
60
  action._tag,
55
61
  async (
56
62
  c: Rivetkit.ActionContextOf<ActorDefinition>,
57
- payload: Action.Payload<typeof action>,
58
- meta?: Client.ActionMeta, // TODO: Find better type
63
+ ...args: unknown[]
59
64
  ) => {
65
+ // Scheduler metadata is appended after stored arguments. Only strip
66
+ // the trusted invocation object, never a caller-supplied lookalike.
67
+ if (c.scheduledFire && args.at(-1) === c.scheduledFire) {
68
+ args.pop();
69
+ }
70
+ const [payload, meta] = args;
60
71
  // Always wrap in a server-side span so the handler has a
61
72
  // live `currentSpan` even when the caller didn't ship trace
62
73
  // context (e.g., a non-Effect-SDK client). When trace context
@@ -108,6 +119,7 @@ export const make = <
108
119
  _tag: action._tag,
109
120
  action,
110
121
  payload: decodedPayload,
122
+ connection: makeActionConnection(c, instance),
111
123
  } as ActionRequest<typeof action>;
112
124
 
113
125
  const resultExit = yield* Effect.exit(
@@ -183,6 +195,25 @@ export const make = <
183
195
  ];
184
196
  });
185
197
 
198
+ const makeActionConnection = (
199
+ c: { readonly conn?: { readonly id: string } },
200
+ instance: Pick<Instance<unknown>, "connectionScope">,
201
+ ): Option.Option<ActionConnection> => {
202
+ // Scheduled actions run without a connection, although the RivetKit
203
+ // context type declares `conn` as always present.
204
+ if (!c.conn) return Option.none();
205
+
206
+ const { id } = c.conn;
207
+ return Option.some({
208
+ id,
209
+ // Most handlers never read this, and the first read checks the
210
+ // connection list, which costs O(connections).
211
+ get scope() {
212
+ return instance.connectionScope(id);
213
+ },
214
+ });
215
+ };
216
+
186
217
  const makeActorAbortedError = () =>
187
218
  new Rivetkit.RivetError("actor", "aborted", "Actor aborted", {
188
219
  public: true,
@@ -35,6 +35,47 @@ export type Instance<
35
35
  ) => Fiber.Fiber<A, E>;
36
36
  readonly scope: Scope.Closeable;
37
37
  readonly state?: ActorStateAdapter.ActorState<StateDefinition>;
38
+ readonly connectionScope: (connectionId: string) => Scope.Scope;
39
+ readonly closeConnectionScope: (
40
+ connectionId: string,
41
+ ) => Effect.Effect<void>;
42
+ };
43
+
44
+ /**
45
+ * Tracks one scope per connection, created when a handler first reads it.
46
+ * Each scope is a child of the wake scope, so sleep and destroy also close it.
47
+ */
48
+ const makeConnectionScopes = (
49
+ wakeScope: Scope.Scope,
50
+ isConnected: (connectionId: string) => boolean,
51
+ ) => {
52
+ const scopes = new Map<string, Scope.Closeable>();
53
+
54
+ return {
55
+ connectionScope: (connectionId: string): Scope.Scope => {
56
+ const existing = scopes.get(connectionId);
57
+ if (existing) return existing;
58
+
59
+ const scope = Scope.forkUnsafe(wakeScope);
60
+ // Core removes a connection before it reports the disconnect. So a
61
+ // connection that is still present will get a later close, and one
62
+ // that is gone gets a closed scope, which runs finalizers immediately.
63
+ if (isConnected(connectionId)) {
64
+ scopes.set(connectionId, scope);
65
+ } else {
66
+ Scope.closeUnsafe(scope, Exit.void);
67
+ }
68
+ return scope;
69
+ },
70
+ closeConnectionScope: (connectionId: string) =>
71
+ Effect.suspend(() => {
72
+ const scope = scopes.get(connectionId);
73
+ if (!scope) return Effect.void;
74
+
75
+ scopes.delete(connectionId);
76
+ return Scope.close(scope, Exit.void);
77
+ }),
78
+ };
38
79
  };
39
80
 
40
81
  export const make = Effect.fnUntraced(function* <
@@ -96,6 +137,9 @@ export const make = Effect.fnUntraced(function* <
96
137
  runFork,
97
138
  scope,
98
139
  state,
140
+ ...makeConnectionScopes(scope, (connectionId) =>
141
+ c.conns.has(connectionId),
142
+ ),
99
143
  };
100
144
  }).pipe(
101
145
  Effect.onError((cause) =>
@@ -121,6 +165,16 @@ export const make = Effect.fnUntraced(function* <
121
165
  stateAdapter.publishChange(instance, newState);
122
166
  }
123
167
  : undefined,
168
+ onDisconnect: async (
169
+ c: { readonly actorId: string },
170
+ conn: { readonly id: string },
171
+ ) => {
172
+ const instance = instances.get(c.actorId);
173
+ // The disconnect can arrive after teardown closed the wake scope.
174
+ if (!instance) return;
175
+
176
+ await runPromise(instance.closeConnectionScope(conn.id));
177
+ },
124
178
  onTeardown: async (c: { readonly actorId: string }) => {
125
179
  return runPromise(
126
180
  Effect.gen(function* () {
@@ -2,11 +2,11 @@ import {
2
2
  Config,
3
3
  Context,
4
4
  Effect,
5
+ Record as EffectRecord,
5
6
  Logger,
7
+ type LogLevel,
6
8
  Option,
7
9
  Predicate,
8
- Record as EffectRecord,
9
- type LogLevel,
10
10
  References,
11
11
  } from "effect";
12
12
  import type * as Rivetkit from "rivetkit";
@@ -51,7 +51,7 @@ const RivetkitLevelByEffectLevel = {
51
51
  All: "trace",
52
52
  } as Record<LogLevel.LogLevel, RivetkitLog.LogLevel>;
53
53
 
54
- const rivetLogLevelFromEnv = Config.string("RIVET_LOG_LEVEL").pipe(
54
+ const rivetLogLevelFromEnv = Config.String("RIVET_LOG_LEVEL").pipe(
55
55
  Effect.option,
56
56
  Effect.map((maybeRivetLogLevel) => {
57
57
  if (Option.isNone(maybeRivetLogLevel)) return Option.none();
package/src/mod.ts CHANGED
@@ -4,4 +4,5 @@ export * as Client from "./Client.ts";
4
4
  export * as Registry from "./Registry.ts";
5
5
  export * as RivetLogger from "./RivetLogger.ts";
6
6
  export * as RivetError from "./RivetError.ts";
7
+ /** @deprecated Actor state was deprecated in 3.0 and will be removed in a future version. Store durable data in SQLite (`c.db`) and in-memory data in `c.vars`. See https://rivet.dev/actors/docs/deprecated/state */
7
8
  export * as State from "./State.ts";