Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 1c03328

Browse files
committed
chore(core): migrate to BARE
1 parent 60532e4 commit 1c03328

File tree

47 files changed

+1526
-587
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1526
-587
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,4 @@ Cargo.lock
184184
**/.wrangler
185185
**/.DS_Store
186186
.aider*
187+
/packages/core/dist/schemas/

packages/core/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,19 @@
150150
"scripts": {
151151
"dev": "pnpm build --watch",
152152
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/common/websocket.ts src/actor/errors.ts src/topologies/coordinate/mod.ts src/topologies/partition/mod.ts src/utils.ts src/driver-helpers/mod.ts src/driver-test-suite/mod.ts src/test/mod.ts src/inspector/mod.ts",
153+
"build:schema": "node ../../packages/misc/bare-compiler/dist/cli.js compile schemas/client-protocol/v1.bare -o dist/schemas/client-protocol/v1.ts && node ../../packages/misc/bare-compiler/dist/cli.js compile schemas/file-system-driver/v1.bare -o dist/schemas/file-system-driver/v1.ts ",
153154
"check-types": "tsc --noEmit",
154155
"boop": "tsc --outDir dist/test -d",
155156
"test": "vitest run",
156157
"test:watch": "vitest",
157158
"dump-openapi": "tsx scripts/dump-openapi.ts"
158159
},
159160
"dependencies": {
161+
"@bare-ts/lib": "~0.3.0",
160162
"@hono/standard-validator": "^0.1.3",
161163
"@hono/zod-openapi": "^0.19.10",
162164
"@rivetkit/fast-json-patch": "^3.1.2",
165+
"@rivetkit/versioned-data-util": "workspace:*",
163166
"cbor-x": "^1.6.0",
164167
"hono": "^4.7.0",
165168
"invariant": "^2.2.4",
@@ -173,6 +176,7 @@
173176
"@hono/node-server": "^1.18.2",
174177
"@hono/node-ws": "^1.1.1",
175178
"@rivet-gg/actor-core": "^25.1.0",
179+
"@rivetkit/bare-compiler": "workspace:*",
176180
"@types/invariant": "^2",
177181
"@types/node": "^22.13.1",
178182
"@types/ws": "^8",
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# MARK: Message To Client
2+
type Init struct {
3+
actorId: str
4+
connectionId: str
5+
connectionToken: str
6+
}
7+
8+
type Error struct {
9+
code: str
10+
message: str
11+
metadata: optional<data>
12+
actionId: optional<uint>
13+
}
14+
15+
type ActionResponse struct {
16+
id: uint
17+
output: data
18+
}
19+
20+
type Event struct {
21+
name: str
22+
# CBOR array
23+
args: data
24+
}
25+
26+
type ToClientBody union {
27+
Init |
28+
Error |
29+
ActionResponse |
30+
Event
31+
}
32+
33+
type ToClient struct {
34+
body: ToClientBody
35+
}
36+
37+
# MARK: Message To Server
38+
type ActionRequest struct {
39+
id: uint
40+
name: str
41+
# CBOR array
42+
args: data
43+
}
44+
45+
type SubscriptionRequest struct {
46+
eventName: str
47+
subscribe: bool
48+
}
49+
50+
type ToServerBody union {
51+
ActionRequest |
52+
SubscriptionRequest
53+
}
54+
55+
type ToServer struct {
56+
body: ToServerBody
57+
}
58+
59+
# MARK: HTTP Action
60+
type HttpActionRequest struct {
61+
# CBOR array
62+
args: data
63+
}
64+
65+
type HttpActionResponse struct {
66+
output: data
67+
}
68+
69+
# MARK: HTTP Error
70+
type HttpResponseError struct {
71+
code: str
72+
message: str
73+
metadata: optional<data>
74+
}
75+
76+
# MARK: HTTP Resolve
77+
type HttpResolveRequest void
78+
79+
type HttpResolveResponse struct {
80+
actorId: str
81+
}
82+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# File System Driver Schema (v1)
2+
3+
# MARK: Actor State
4+
# Represents the persisted state for an actor on disk.
5+
# Note: createdAt is not persisted; it is derived from the file's birthtime.
6+
type ActorState struct {
7+
id: str
8+
name: str
9+
key: list<str>
10+
persistedData: data
11+
}
12+
13+
# MARK: Actor Alarm
14+
# Represents a scheduled alarm for an actor.
15+
# Stored per-actor; the actor id is implied by the filename.
16+
# The timestamp is milliseconds since epoch.
17+
type ActorAlarm struct {
18+
timestamp: uint
19+
}
20+

packages/core/src/actor/connection.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type * as messageToClient from "@/actor/protocol/message/to-client";
2-
import type * as wsToClient from "@/actor/protocol/message/to-client";
1+
import * as cbor from "cbor-x";
2+
import type * as protocol from "@/schemas/client-protocol/mod";
3+
import { TO_CLIENT_VERSIONED } from "@/schemas/client-protocol/versioned";
34
import type { AnyDatabaseProvider } from "./database";
45
import { type ConnDriver, ConnectionReadyState } from "./driver";
56
import * as errors from "./errors";
@@ -162,7 +163,7 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
162163
*
163164
* @protected
164165
*/
165-
public _sendMessage(message: CachedSerializer<messageToClient.ToClient>) {
166+
public _sendMessage(message: CachedSerializer<protocol.ToClient>) {
166167
this.#driver.sendMessage?.(this.#actor, this, this.__persist.ds, message);
167168
}
168169

@@ -181,14 +182,18 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
181182
connId: this.id,
182183
});
183184
this._sendMessage(
184-
new CachedSerializer<wsToClient.ToClient>({
185-
b: {
186-
ev: {
187-
n: eventName,
188-
a: args,
185+
new CachedSerializer<protocol.ToClient>(
186+
{
187+
body: {
188+
tag: "Event",
189+
val: {
190+
name: eventName,
191+
args: cbor.encode(args).buffer.slice() as ArrayBuffer,
192+
},
189193
},
190194
},
191-
}),
195+
TO_CLIENT_VERSIONED,
196+
),
192197
);
193198
}
194199

packages/core/src/actor/driver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type * as messageToClient from "@/actor/protocol/message/to-client";
21
import type { CachedSerializer } from "@/actor/protocol/serde";
32
import type { AnyClient } from "@/client/client";
43
import type { ManagerDriver } from "@/manager/driver";
54
import type { RegistryConfig } from "@/registry/config";
65
import type { RunConfig } from "@/registry/run-config";
6+
import type * as protocol from "@/schemas/client-protocol/mod";
77
import type { AnyConn, ConnectionDriver } from "./connection";
88
import type { GenericConnGlobalState } from "./generic-conn-driver";
99
import type { AnyActorInstance } from "./instance";
@@ -60,7 +60,7 @@ export interface ConnDriver<ConnDriverState = unknown> {
6060
actor: AnyActorInstance,
6161
conn: AnyConn,
6262
state: ConnDriverState,
63-
message: CachedSerializer<messageToClient.ToClient>,
63+
message: CachedSerializer<protocol.ToClient>,
6464
): void;
6565

6666
/**

packages/core/src/actor/generic-conn-driver.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
ConnectionReadyState,
1414
} from "@/actor/driver";
1515
import type { AnyActorInstance } from "@/actor/instance";
16-
import type * as messageToClient from "@/actor/protocol/message/to-client";
1716
import type { CachedSerializer, Encoding } from "@/actor/protocol/serde";
1817
import { encodeDataToString } from "@/actor/protocol/serde";
18+
import type * as protocol from "@/schemas/client-protocol/mod";
1919
import { logger } from "./log";
2020

2121
// This state is different than `PersistedConn` state since the connection-specific state is persisted & must be serializable. This is also part of the connection driver, not part of the core actor.
@@ -54,7 +54,7 @@ export function createGenericWebSocketDriver(
5454
actor: AnyActorInstance,
5555
conn: AnyConn,
5656
state: GenericWebSocketDriverState,
57-
message: CachedSerializer<messageToClient.ToClient>,
57+
message: CachedSerializer<protocol.ToClient>,
5858
) => {
5959
const ws = globalState.websockets.get(conn.id);
6060
if (!ws) {
@@ -168,7 +168,7 @@ export function createGenericSseDriver(
168168
_actor: AnyActorInstance,
169169
conn: AnyConn,
170170
state: GenericSseDriverState,
171-
message: CachedSerializer<messageToClient.ToClient>,
171+
message: CachedSerializer<protocol.ToClient>,
172172
) => {
173173
const stream = globalState.sseStreams.get(conn.id);
174174
if (!stream) {
@@ -223,7 +223,7 @@ export type GenericHttpDriverState = Record<never, never>;
223223

224224
export function createGenericHttpDriver(): ConnDriver<GenericHttpDriverState> {
225225
return {
226-
getConnectionReadyState(_actor, conn) {
226+
getConnectionReadyState(_actor, _conn) {
227227
// TODO: This might not be the correct logic
228228
return ConnectionReadyState.OPEN;
229229
},

packages/core/src/actor/instance.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import * as cbor from "cbor-x";
22
import invariant from "invariant";
33
import onChange from "on-change";
44
import type { ActorKey } from "@/actor/mod";
5-
import type * as wsToClient from "@/actor/protocol/message/to-client";
6-
import type * as wsToServer from "@/actor/protocol/message/to-server";
75
import type { Client } from "@/client/client";
86
import type { Logger } from "@/common/log";
97
import { isCborSerializable, stringifyError } from "@/common/utils";
108
import type { UniversalWebSocket } from "@/common/websocket-interface";
119
import { ActorInspector } from "@/inspector/actor";
1210
import type { Registry } from "@/mod";
11+
import type * as protocol from "@/schemas/client-protocol/mod";
12+
import { TO_CLIENT_VERSIONED } from "@/schemas/client-protocol/versioned";
1313
import { SinglePromiseQueue } from "@/utils";
1414
import type { ActionContext } from "./action";
1515
import type { ActorConfig, OnConnectOptions } from "./config";
@@ -28,9 +28,8 @@ import type {
2828
PersistedActor,
2929
PersistedConn,
3030
PersistedScheduleEvent,
31-
PersistedScheduleEventKind,
3231
} from "./persisted";
33-
import { processMessage } from "./protocol/message/mod";
32+
import { processMessage } from "./protocol/old";
3433
import { CachedSerializer } from "./protocol/serde";
3534
import { Schedule } from "./schedule";
3635
import { DeadlineError, deadline } from "./utils";
@@ -945,23 +944,27 @@ export class ActorInstance<
945944

946945
// Send init message
947946
conn._sendMessage(
948-
new CachedSerializer<wsToClient.ToClient>({
949-
b: {
950-
i: {
951-
ai: this.id,
952-
ci: conn.id,
953-
ct: conn._token,
947+
new CachedSerializer<protocol.ToClient>(
948+
{
949+
body: {
950+
tag: "Init",
951+
val: {
952+
actorId: this.id,
953+
connectionId: conn.id,
954+
connectionToken: conn._token,
955+
},
954956
},
955957
},
956-
}),
958+
TO_CLIENT_VERSIONED,
959+
),
957960
);
958961

959962
return conn;
960963
}
961964

962965
// MARK: Messages
963966
async processMessage(
964-
message: wsToServer.ToServer,
967+
message: protocol.ToServer,
965968
conn: Conn<S, CP, CS, V, I, AD, DB>,
966969
) {
967970
await processMessage(message, this, conn, {
@@ -1428,14 +1431,18 @@ export class ActorInstance<
14281431
const subscriptions = this.#subscriptionIndex.get(name);
14291432
if (!subscriptions) return;
14301433

1431-
const toClientSerializer = new CachedSerializer<wsToClient.ToClient>({
1432-
b: {
1433-
ev: {
1434-
n: name,
1435-
a: args,
1434+
const toClientSerializer = new CachedSerializer<protocol.ToClient>(
1435+
{
1436+
body: {
1437+
tag: "Event",
1438+
val: {
1439+
name,
1440+
args: cbor.encode(args).buffer.slice() as ArrayBuffer,
1441+
},
14361442
},
14371443
},
1438-
});
1444+
TO_CLIENT_VERSIONED,
1445+
);
14391446

14401447
// Send message to clients
14411448
for (const connection of subscriptions) {

packages/core/src/actor/protocol/http/action.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/core/src/actor/protocol/http/error.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)