@rbxts/netweave 0.0.0-stage → 0.1.1

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,314 @@
1
+ --!strict
2
+ --!optimize 2
3
+
4
+ --[=[
5
+ @class Baseline
6
+
7
+ What each client already has, per subject, so the next packet can be a difference rather than
8
+ the whole thing.
9
+
10
+ This is the piece neither neighbour holds. Charm Sync keeps one `lastSyncedValue` per *key* and
11
+ diffs everyone against it; ReplicaService keeps no baseline at all, because the game names the
12
+ mutation. Per client per subject is what a **dynamic audience** needs (`PLAN-M4` D-4): a player
13
+ who walks into a `nearby` radius has never seen that subject, and a player who walks out has to
14
+ stop being remembered.
15
+
16
+ Internal. `nw.replicate` is the public read of it.
17
+ ]=]
18
+
19
+ --[[
20
+ Three levels of table — client, channel, subject — rather than one keyed on a composite.
21
+
22
+ Every operation that matters is "everything for this client" (a disconnect) or "everything for
23
+ this client on this channel" (a resync), and both are one `nil` assignment on a nested table and
24
+ a walk on a flat one. The one that is not — "everything for this subject, across clients" — is
25
+ rare and is paid for with the walk.
26
+
27
+ The principle `PLAN-M3` D-6 applied to the pending set applies unchanged: this is memory sized by
28
+ how many people join, so it is bounded, and the bound reports rather than grows.
29
+ ]]
30
+
31
+ local Observer = require("../api/Observer")
32
+ local Settings = require("../api/Config")
33
+
34
+ local Baseline = {}
35
+
36
+ --[[
37
+ Constant, because the condition behind it is constant: a game over the limit is over it on every
38
+ tick, and a reason built per report would be `CLAUDE.md` §9's "count it instead of weighing it"
39
+ broken on exactly the shape that taught it.
40
+
41
+ It says what it costs rather than what it is. "Sent whole instead of as a change" was the old
42
+ wording and it understates by a factor of the framerate: the subject is sent whole **every tick**,
43
+ for a world that is not moving, for as long as the game stays over the line.
44
+ ]]
45
+ local REFUSED = "this client is already holding the most replication baselines the "
46
+ .. "baselinesPerClient limit allows, so every subject past it is encoded and sent whole on "
47
+ .. "every tick, changed or not — which costs more bandwidth than replicating it would. Raise "
48
+ .. "the limit, or narrow the audience so each player sees fewer subjects"
49
+
50
+ --[[ The peer on the other end of a client's connection has no `Player` to be keyed by, and `nil` is
51
+ not a table key. This stands in for "the server" on the client's side and cannot collide with a
52
+ `Player` — the same sentinel, for the same reason, as `src/api/Protocol.luau`. ]]
53
+ local SERVER: any = table.freeze({})
54
+
55
+ local function keyOf(who: any?): any
56
+ return if who == nil then SERVER else who
57
+ end
58
+
59
+ type Subjects = { [any]: any }
60
+ type Channels = { [any]: Subjects }
61
+
62
+ --[=[
63
+ @interface Baseline
64
+ @within Baseline
65
+ .of (who: any, channel: Named, subject: any) -> any -- what they have, or nil
66
+ .keep (who: any, channel: Named, subject: any, value: any) -> boolean -- false when the limit refused it
67
+ .drop (who: any, channel: Named, subject: any) -> ()
68
+ .desync (who: any, channel: Named) -> () -- everything this client had on this channel is gone
69
+ .forget (who: any) -> () -- they left
70
+ .held (who: any) -> number
71
+ .each (who: any, channel: any, visit: (subject: any, value: any) -> ()) -> () -- every subject they hold on this channel
72
+ .reset () -> ()
73
+ ]=]
74
+ --[=[
75
+ @type Named { qualified: string }
76
+ @within Baseline
77
+
78
+ What this module needs a channel to be: a key, and a name to report against.
79
+
80
+ It was `any`, and `keep` dereferences `qualified` when the per-client limit refuses a subject —
81
+ so a channel value without one raised inside the tick, and nothing said so until it did (M4
82
+ report finding 46). Naming the two fields rather than taking the whole `Channel` record keeps the
83
+ rigs that stand one in honest without making them build one.
84
+ ]=]
85
+ export type Named = { qualified: string }
86
+
87
+ export type Baseline = {
88
+ of: (who: any, channel: Named, subject: any) -> any,
89
+ keep: (who: any, channel: Named, subject: any, value: any) -> boolean,
90
+ drop: (who: any, channel: Named, subject: any) -> (),
91
+ desync: (who: any, channel: Named) -> (),
92
+ forget: (who: any) -> (),
93
+ held: (who: any) -> number,
94
+ each: (who: any, channel: any, visit: (subject: any, value: any) -> ()) -> (),
95
+ reset: () -> (),
96
+ }
97
+
98
+ --[=[
99
+ @within Baseline
100
+ @function new
101
+ @param bounded boolean? -- whether `baselinesPerClient` applies; true unless said otherwise
102
+ @return Baseline
103
+
104
+ One store. The driver holds a single instance; a test holds its own.
105
+
106
+ :::caution
107
+ **The server's is bounded and the client's is not**, and the asymmetry is deliberate. On the
108
+ server this is memory sized by how many people join, which is what `baselinesPerClient` exists
109
+ for. On the client it is what the server chose to send — already bounded, by that limit, at the
110
+ other end — and refusing to remember one here would not save anything, it would make the next
111
+ change unreadable.
112
+
113
+ Bounded is the default because forgetting to ask for it on the server is an unbounded leak,
114
+ where forgetting to turn it off on the client is a loud desync.
115
+ :::
116
+ ]=]
117
+ function Baseline.new(bounded: boolean?): Baseline
118
+ local limited = bounded ~= false
119
+ local records: { [any]: Channels } = {}
120
+
121
+ --[[ Counted rather than walked. The limit is read on every `keep`, so recomputing the size each
122
+ time would make the bound cost more than the thing it bounds. ]]
123
+ local counts: { [any]: number } = {}
124
+
125
+ --[[ Which clients have already been told their budget is full, per channel. A boolean rather
126
+ than a count: what an author needs to know is that a line was crossed, and by how much is
127
+ `held` minus the limit, which they can read at any time. ]]
128
+ local announced: { [any]: { [any]: boolean } } = {}
129
+
130
+ --[[ Back under the line, so the next crossing is worth saying again. Called from every path that
131
+ releases one — a subject dropped, a channel given up on, a player gone. ]]
132
+ local function rearm(key: any)
133
+ if announced[key] and counts[key] and counts[key] < Settings.limitOf("baselinesPerClient") then
134
+ announced[key] = nil
135
+ end
136
+ end
137
+
138
+ local function channelsOf(key: any): Channels
139
+ local channels = records[key]
140
+
141
+ if not channels then
142
+ channels = {}
143
+ records[key] = channels
144
+ counts[key] = 0
145
+ end
146
+
147
+ return channels
148
+ end
149
+
150
+ return {
151
+ of = function(who: any, channel: any, subject: any): any
152
+ local channels = records[keyOf(who)]
153
+
154
+ if not channels then
155
+ return nil
156
+ end
157
+
158
+ local subjects = channels[channel]
159
+ return if subjects then subjects[subject] else nil
160
+ end,
161
+
162
+ keep = function(who: any, channel: any, subject: any, value: any): boolean
163
+ local key = keyOf(who)
164
+ local channels = channelsOf(key)
165
+ local subjects = channels[channel]
166
+
167
+ if not subjects then
168
+ subjects = {}
169
+ channels[channel] = subjects
170
+ end
171
+
172
+ --[[ Replacing one costs nothing and is the common case by a wide margin — a subject
173
+ whose baseline is being updated was already counted. ]]
174
+ if subjects[subject] ~= nil then
175
+ subjects[subject] = value
176
+ return true
177
+ end
178
+
179
+ if limited and counts[key] >= Settings.limitOf("baselinesPerClient") then
180
+ --[[
181
+ Refused, and what that costs is a full encode and a full packet for this subject
182
+ **on every tick** rather than a patch — correct and expensive, which is the right
183
+ way round for a bound to fail. A replication layer that answered a budget by
184
+ dropping state would be trading the one thing it exists to get right for the one
185
+ thing a bandwidth graph would have shown anyway.
186
+
187
+ ~~It degrades to what `nw.state` does for a living.~~ **It does not**, and the
188
+ difference is the whole of this finding: `nw.state` sends when the game publishes,
189
+ and this sends every tick whether anything moved or not. Measured at 300 subjects
190
+ against a limit of 256, with the store untouched: 265 bytes and 44 refusals every
191
+ idle frame, for ever (M4 report).
192
+
193
+ **And the full packet every tick stands, with the argument written out** (M4-1 asked
194
+ for a per-client "received the last snapshot" flag so an idle frame sends nothing).
195
+ A per-client flag cannot answer for a client that *enters* a subject's audience later:
196
+ the subject is settled, the client is over the line, and the flag would say "already
197
+ has it" about a subject it has never seen. The record that can answer is per
198
+ (client, subject) — and that is a baseline slot, which is exactly what this limit
199
+ bounds. So the resend is the honest cost of being over the limit, reported once below,
200
+ and the fix is the one the report names: raise the limit or narrow the audience.
201
+
202
+ **Said once per client per channel, not once per subject per tick.** The condition
203
+ is a state, not an event — 44 subjects over the line was 2,640 reports a second
204
+ describing one unchanging fact, which is a console nobody can read and an
205
+ `nw.observe` nobody can aggregate. It re-arms below the line, so a game that
206
+ crosses it again is told again.
207
+ ]]
208
+ local said = announced[key]
209
+
210
+ if not said then
211
+ said = {}
212
+ announced[key] = said
213
+ end
214
+
215
+ if not said[channel] then
216
+ said[channel] = true
217
+ Observer.emit(channel.qualified, who, "replicate", REFUSED, 0)
218
+ end
219
+
220
+ return false
221
+ end
222
+
223
+ subjects[subject] = value
224
+ counts[key] = counts[key] + 1
225
+ return true
226
+ end,
227
+
228
+ drop = function(who: any, channel: any, subject: any)
229
+ local key = keyOf(who)
230
+ local channels = records[key]
231
+
232
+ if not channels then
233
+ return
234
+ end
235
+
236
+ local subjects = channels[channel]
237
+
238
+ if not subjects or subjects[subject] == nil then
239
+ return
240
+ end
241
+
242
+ subjects[subject] = nil
243
+ counts[key] = counts[key] - 1
244
+ rearm(key)
245
+ end,
246
+
247
+ --[[ Everything this client had on this channel, gone. The next tick therefore sends
248
+ snapshots, which is the resnapshot path D-2's break detector and D-4's audience entry
249
+ both end in — one mechanism, reached three ways. ]]
250
+ desync = function(who: any, channel: any)
251
+ local key = keyOf(who)
252
+ local channels = records[key]
253
+
254
+ if not channels then
255
+ return
256
+ end
257
+
258
+ local subjects = channels[channel]
259
+
260
+ if not subjects then
261
+ return
262
+ end
263
+
264
+ local held = 0
265
+
266
+ for _ in subjects do
267
+ held += 1
268
+ end
269
+
270
+ channels[channel] = nil
271
+ counts[key] = counts[key] - held
272
+ rearm(key)
273
+ end,
274
+
275
+ --[[ They left. Tested the way `PLAN-M3` tested the queue drop, because it is the same
276
+ defect waiting in a different table: state that outlives the connection that justified
277
+ it. ]]
278
+ forget = function(who: any)
279
+ local key = keyOf(who)
280
+ records[key] = nil
281
+ counts[key] = nil
282
+ announced[key] = nil
283
+ end,
284
+
285
+ held = function(who: any): number
286
+ return counts[keyOf(who)] or 0
287
+ end,
288
+
289
+ --[[ A callback rather than the table, because the table is the record. The one caller is
290
+ `Inbound.drain`, which on a client treats these baselines as the queue for a listener
291
+ attached late (PLAN-M4-BUG D-3); it fills a list inside the visit and dispatches after,
292
+ so nothing here is mutated mid-walk. ]]
293
+ each = function(who: any, channel: any, visit: (any, any) -> ())
294
+ local channels = records[keyOf(who)]
295
+ local subjects = if channels then channels[channel] else nil
296
+
297
+ if not subjects then
298
+ return
299
+ end
300
+
301
+ for subject, value in subjects do
302
+ visit(subject, value)
303
+ end
304
+ end,
305
+
306
+ reset = function()
307
+ table.clear(records)
308
+ table.clear(counts)
309
+ table.clear(announced)
310
+ end,
311
+ }
312
+ end
313
+
314
+ return Baseline