@rivetkit/effect 0.0.0-sqlite-sync-3-0-6-final.cf8679c → 0.0.0-sqlite-query-metrics-2-3-32.d7c4474

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.
@@ -0,0 +1,340 @@
1
+ import { assert, describe, it } from "@effect/vitest";
2
+ import { State } from "@rivetkit/effect";
3
+ import { Effect, Exit, PubSub, Stream } from "effect";
4
+
5
+ // Helper: build a State backed by a plain mutable cell, with
6
+ // Effect-typed read/write closures. Mirrors how Registry wires
7
+ // `decodeUnknownEffect` / `encodeUnknownEffect` over `c.state`.
8
+ const makeCellState = <A>(initial: A) => {
9
+ const cell = { value: initial };
10
+ return State.make<A, never, never>(
11
+ () => Effect.sync(() => cell.value),
12
+ (v) =>
13
+ Effect.sync(() => {
14
+ cell.value = v;
15
+ }),
16
+ ).pipe(Effect.map((s) => ({ s, cell })));
17
+ };
18
+
19
+ describe("State", () => {
20
+ it.effect("get reflects the backing store", () =>
21
+ Effect.gen(function* () {
22
+ const { s, cell } = yield* makeCellState(42);
23
+ assert.strictEqual(yield* State.get(s), 42);
24
+
25
+ cell.value = 100;
26
+ assert.strictEqual(yield* State.get(s), 100);
27
+ }),
28
+ );
29
+
30
+ it.effect("set writes through to the backing store", () =>
31
+ Effect.gen(function* () {
32
+ const { s, cell } = yield* makeCellState(0);
33
+ yield* State.set(s, 7);
34
+ assert.strictEqual(cell.value, 7);
35
+ assert.strictEqual(yield* State.get(s), 7);
36
+ }),
37
+ );
38
+
39
+ it.effect(
40
+ "getAndSet returns the previous value and commits the new value",
41
+ () =>
42
+ Effect.gen(function* () {
43
+ const { s, cell } = yield* makeCellState(10);
44
+ const previous = yield* State.getAndSet(s, 15);
45
+ assert.strictEqual(previous, 10);
46
+ assert.strictEqual(cell.value, 15);
47
+ }),
48
+ );
49
+
50
+ it.effect("setAndGet returns the committed value", () =>
51
+ Effect.gen(function* () {
52
+ const { s, cell } = yield* makeCellState(10);
53
+ const next = yield* State.setAndGet(s, 15);
54
+ assert.strictEqual(next, 15);
55
+ assert.strictEqual(cell.value, 15);
56
+ }),
57
+ );
58
+
59
+ it.effect("update applies f over read/write", () =>
60
+ Effect.gen(function* () {
61
+ const { s, cell } = yield* makeCellState(10);
62
+ yield* State.update(s, (n) => n + 5);
63
+ assert.strictEqual(cell.value, 15);
64
+ }),
65
+ );
66
+
67
+ it.effect("updateEffect applies an Effectful f over read/write", () =>
68
+ Effect.gen(function* () {
69
+ const { s, cell } = yield* makeCellState(10);
70
+ yield* State.updateEffect(s, (n) => Effect.succeed(n + 5));
71
+ assert.strictEqual(cell.value, 15);
72
+ }),
73
+ );
74
+
75
+ it.effect(
76
+ "getAndUpdate returns the previous value and commits the new value",
77
+ () =>
78
+ Effect.gen(function* () {
79
+ const { s, cell } = yield* makeCellState(10);
80
+ const previous = yield* State.getAndUpdate(s, (n) => n + 5);
81
+ assert.strictEqual(previous, 10);
82
+ assert.strictEqual(cell.value, 15);
83
+ }),
84
+ );
85
+
86
+ it.effect(
87
+ "getAndUpdateEffect returns the previous value and commits the new value",
88
+ () =>
89
+ Effect.gen(function* () {
90
+ const { s, cell } = yield* makeCellState(10);
91
+ const previous = yield* State.getAndUpdateEffect(s, (n) =>
92
+ Effect.succeed(n + 5),
93
+ );
94
+ assert.strictEqual(previous, 10);
95
+ assert.strictEqual(cell.value, 15);
96
+ }),
97
+ );
98
+
99
+ it.effect("updateAndGet returns the new value and commits it", () =>
100
+ Effect.gen(function* () {
101
+ const { s, cell } = yield* makeCellState(10);
102
+ const next = yield* State.updateAndGet(s, (n) => n + 5);
103
+ assert.strictEqual(next, 15);
104
+ assert.strictEqual(cell.value, 15);
105
+ }),
106
+ );
107
+
108
+ it.effect("updateAndGetEffect returns the new value and commits it", () =>
109
+ Effect.gen(function* () {
110
+ const { s, cell } = yield* makeCellState(10);
111
+ const next = yield* State.updateAndGetEffect(s, (n) =>
112
+ Effect.succeed(n + 5),
113
+ );
114
+ assert.strictEqual(next, 15);
115
+ assert.strictEqual(cell.value, 15);
116
+ }),
117
+ );
118
+
119
+ it.effect("modify returns B and commits the new value", () =>
120
+ Effect.gen(function* () {
121
+ const { s, cell } = yield* makeCellState("a");
122
+ const b = yield* State.modify(
123
+ s,
124
+ (str) => [str.length, `${str}b`] as const,
125
+ );
126
+ assert.strictEqual(b, 1);
127
+ assert.strictEqual(cell.value, "ab");
128
+ }),
129
+ );
130
+
131
+ it.effect("modifyEffect returns B and commits the new value", () =>
132
+ Effect.gen(function* () {
133
+ const { s, cell } = yield* makeCellState("a");
134
+ const b = yield* State.modifyEffect(s, (str) =>
135
+ Effect.succeed([str.length, `${str}b`] as const),
136
+ );
137
+ assert.strictEqual(b, 1);
138
+ assert.strictEqual(cell.value, "ab");
139
+ }),
140
+ );
141
+
142
+ it.effect(
143
+ "update is atomic across concurrent fibers (no lost updates)",
144
+ () =>
145
+ Effect.gen(function* () {
146
+ const { s, cell } = yield* makeCellState(0);
147
+ yield* Effect.all(
148
+ Array.from({ length: 100 }, () =>
149
+ State.update(s, (n) => n + 1),
150
+ ),
151
+ { concurrency: "unbounded" },
152
+ );
153
+ assert.strictEqual(cell.value, 100);
154
+ }),
155
+ );
156
+
157
+ it.effect("changes replays the most recent published value", () =>
158
+ Effect.gen(function* () {
159
+ const { s } = yield* makeCellState(0);
160
+ const initial = yield* State.changes(s).pipe(
161
+ Stream.take(1),
162
+ Stream.runCollect,
163
+ );
164
+ assert.deepStrictEqual(initial, [0]);
165
+
166
+ yield* s[State.RuntimeTypeId].publishEffect(Effect.succeed(7));
167
+ const later = yield* State.changes(s).pipe(
168
+ Stream.take(1),
169
+ Stream.runCollect,
170
+ );
171
+ assert.deepStrictEqual(later, [7]);
172
+ }),
173
+ );
174
+
175
+ it.effect("publish pushes values to live subscribers", () =>
176
+ Effect.gen(function* () {
177
+ const { s } = yield* makeCellState(0);
178
+ yield* Effect.scoped(
179
+ Effect.gen(function* () {
180
+ const sub = yield* PubSub.subscribe(
181
+ s[State.RuntimeTypeId].pubsub,
182
+ );
183
+ assert.strictEqual(yield* PubSub.take(sub), 0);
184
+
185
+ yield* s[State.RuntimeTypeId].publishEffect(
186
+ Effect.succeed(1),
187
+ );
188
+ yield* s[State.RuntimeTypeId].publishEffect(
189
+ Effect.succeed(2),
190
+ );
191
+ assert.strictEqual(yield* PubSub.take(sub), 1);
192
+ assert.strictEqual(yield* PubSub.take(sub), 2);
193
+ }),
194
+ );
195
+ }),
196
+ );
197
+
198
+ it.effect("shuts down the backing pubsub when its scope closes", () =>
199
+ Effect.gen(function* () {
200
+ const pubsub = yield* Effect.scoped(
201
+ Effect.gen(function* () {
202
+ const { s } = yield* makeCellState(0);
203
+ return s[State.RuntimeTypeId].pubsub;
204
+ }),
205
+ );
206
+
207
+ assert.isTrue(yield* PubSub.isShutdown(pubsub));
208
+ }),
209
+ );
210
+
211
+ it.effect("set does NOT auto-publish — the runtime does", () =>
212
+ Effect.gen(function* () {
213
+ const { s } = yield* makeCellState(0);
214
+ yield* State.set(s, 99);
215
+ // replay should still hold the initial 0, not 99
216
+ const latest = yield* State.changes(s).pipe(
217
+ Stream.take(1),
218
+ Stream.runCollect,
219
+ );
220
+ assert.deepStrictEqual(latest, [0]);
221
+ }),
222
+ );
223
+
224
+ it.effect("isState discriminates", () =>
225
+ Effect.gen(function* () {
226
+ const { s } = yield* makeCellState(0);
227
+ assert.isTrue(State.isState(s));
228
+ assert.isFalse(State.isState({}));
229
+ assert.isFalse(State.isState(null));
230
+ assert.isFalse(State.isState(42));
231
+ }),
232
+ );
233
+
234
+ it.effect("supports .pipe()", () =>
235
+ Effect.gen(function* () {
236
+ const { s } = yield* makeCellState(0);
237
+ yield* s.pipe(State.set(5));
238
+ assert.strictEqual(yield* State.get(s), 5);
239
+
240
+ yield* s.pipe(State.update((n) => n * 2));
241
+ assert.strictEqual(yield* State.get(s), 10);
242
+
243
+ yield* s.pipe(State.updateEffect((n) => Effect.succeed(n + 1)));
244
+ assert.strictEqual(yield* State.get(s), 11);
245
+ }),
246
+ );
247
+
248
+ it.effect("supports instance methods", () =>
249
+ Effect.gen(function* () {
250
+ const { s, cell } = yield* makeCellState(0);
251
+ assert.strictEqual(yield* s.get, 0);
252
+
253
+ yield* s.set(5);
254
+ assert.strictEqual(cell.value, 5);
255
+
256
+ const beforeSet = yield* s.getAndSet(6);
257
+ assert.strictEqual(beforeSet, 5);
258
+ assert.strictEqual(cell.value, 6);
259
+
260
+ const afterSet = yield* s.setAndGet(7);
261
+ assert.strictEqual(afterSet, 7);
262
+ assert.strictEqual(cell.value, 7);
263
+
264
+ yield* s.update((n) => n * 2);
265
+ assert.strictEqual(yield* s.get, 14);
266
+
267
+ yield* s.updateEffect((n) => Effect.succeed(n + 1));
268
+ assert.strictEqual(yield* s.get, 15);
269
+
270
+ const previousUpdate = yield* s.getAndUpdate((n) => n + 1);
271
+ assert.strictEqual(previousUpdate, 15);
272
+ assert.strictEqual(yield* s.get, 16);
273
+
274
+ const previousEffectUpdate = yield* s.getAndUpdateEffect((n) =>
275
+ Effect.succeed(n + 1),
276
+ );
277
+ assert.strictEqual(previousEffectUpdate, 16);
278
+ assert.strictEqual(yield* s.get, 17);
279
+
280
+ const next = yield* s.updateAndGet((n) => n + 1);
281
+ assert.strictEqual(next, 18);
282
+
283
+ const effectNext = yield* s.updateAndGetEffect((n) =>
284
+ Effect.succeed(n + 1),
285
+ );
286
+ assert.strictEqual(effectNext, 19);
287
+
288
+ const previous = yield* s.modify((n) => [n, n + 1] as const);
289
+ assert.strictEqual(previous, 19);
290
+ assert.strictEqual(yield* s.get, 20);
291
+
292
+ const effectPrevious = yield* s.modifyEffect((n) =>
293
+ Effect.succeed([n, n + 1] as const),
294
+ );
295
+ assert.strictEqual(effectPrevious, 20);
296
+ assert.strictEqual(yield* s.get, 21);
297
+ }),
298
+ );
299
+
300
+ it.effect("read failure propagates through get", () =>
301
+ Effect.gen(function* () {
302
+ const reads = { count: 0 };
303
+ // Construction reads once to seed the pubsub; subsequent reads
304
+ // fail. Mirrors a schema mismatch on persisted state.
305
+ const s = yield* State.make<number, "boom", never>(
306
+ () =>
307
+ Effect.suspend(() => {
308
+ reads.count++;
309
+ if (reads.count === 1) return Effect.succeed(0);
310
+ return Effect.fail("boom" as const);
311
+ }),
312
+ () => Effect.void,
313
+ );
314
+ const exit = yield* Effect.exit(State.get(s));
315
+ assert.isTrue(Exit.isFailure(exit));
316
+ }),
317
+ );
318
+
319
+ it.effect("write failure propagates through set", () =>
320
+ Effect.gen(function* () {
321
+ const s = yield* State.make<number, "boom", never>(
322
+ () => Effect.succeed(0),
323
+ () => Effect.fail("boom" as const),
324
+ );
325
+ const exit = yield* Effect.exit(State.set(s, 1));
326
+ assert.isTrue(Exit.isFailure(exit));
327
+ }),
328
+ );
329
+
330
+ it.effect("Effectful update failure does not write", () =>
331
+ Effect.gen(function* () {
332
+ const { s, cell } = yield* makeCellState(0);
333
+ const exit = yield* Effect.exit(
334
+ State.updateEffect(s, () => Effect.fail("boom" as const)),
335
+ );
336
+ assert.isTrue(Exit.isFailure(exit));
337
+ assert.strictEqual(cell.value, 0);
338
+ }),
339
+ );
340
+ });
@@ -6,17 +6,12 @@ import {
6
6
  Option,
7
7
  Record,
8
8
  Schema,
9
- type Scope,
10
9
  Tracer,
11
10
  } from "effect";
12
11
  import * as Rivetkit from "rivetkit";
13
12
  import type * as Action from "../Action.ts";
14
- import type {
15
- ActionConnection,
16
- ActionHandlersFrom,
17
- ActionRequest,
18
- Actor,
19
- } from "../Actor.ts";
13
+ import type { ActionHandlersFrom, ActionRequest, Actor } from "../Actor.ts";
14
+ import type * as Client from "../Client.ts";
20
15
  import * as ActionErrorEnvelope from "./ActionErrorEnvelope.ts";
21
16
  import { makeActorLogAnnotations } from "./logging.ts";
22
17
  import { readTraceMeta, rpcSystem } from "./tracing.ts";
@@ -28,7 +23,6 @@ export type Instance<ActionHandlers> = {
28
23
  effect: Effect.Effect<A, E, any>,
29
24
  options?: Effect.RunOptions,
30
25
  ) => Fiber.Fiber<A, E>;
31
- readonly connectionScope: (connectionId: string) => Scope.Scope;
32
26
  };
33
27
 
34
28
  export const make = <
@@ -60,14 +54,9 @@ export const make = <
60
54
  action._tag,
61
55
  async (
62
56
  c: Rivetkit.ActionContextOf<ActorDefinition>,
63
- ...args: unknown[]
57
+ payload: Action.Payload<typeof action>,
58
+ meta?: Client.ActionMeta, // TODO: Find better type
64
59
  ) => {
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;
71
60
  // Always wrap in a server-side span so the handler has a
72
61
  // live `currentSpan` even when the caller didn't ship trace
73
62
  // context (e.g., a non-Effect-SDK client). When trace context
@@ -119,7 +108,6 @@ export const make = <
119
108
  _tag: action._tag,
120
109
  action,
121
110
  payload: decodedPayload,
122
- connection: makeActionConnection(c, instance),
123
111
  } as ActionRequest<typeof action>;
124
112
 
125
113
  const resultExit = yield* Effect.exit(
@@ -195,25 +183,6 @@ export const make = <
195
183
  ];
196
184
  });
197
185
 
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
-
217
186
  const makeActorAbortedError = () =>
218
187
  new Rivetkit.RivetError("actor", "aborted", "Actor aborted", {
219
188
  public: true,
@@ -35,47 +35,6 @@ 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
- };
79
38
  };
80
39
 
81
40
  export const make = Effect.fnUntraced(function* <
@@ -137,9 +96,6 @@ export const make = Effect.fnUntraced(function* <
137
96
  runFork,
138
97
  scope,
139
98
  state,
140
- ...makeConnectionScopes(scope, (connectionId) =>
141
- c.conns.has(connectionId),
142
- ),
143
99
  };
144
100
  }).pipe(
145
101
  Effect.onError((cause) =>
@@ -165,16 +121,6 @@ export const make = Effect.fnUntraced(function* <
165
121
  stateAdapter.publishChange(instance, newState);
166
122
  }
167
123
  : 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
- },
178
124
  onTeardown: async (c: { readonly actorId: string }) => {
179
125
  return runPromise(
180
126
  Effect.gen(function* () {