Skip to content

Commit abbdb65

Browse files
authored
Merge pull request #21 from typetorch/feat/live-debug
Live debug: remoteDebug.command / stream and fleet.column (plans/34, 0.7.1)
2 parents 41307de + cff6e27 commit abbdb65

6 files changed

Lines changed: 657 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,43 @@ TypeTorch.runDetached(() => store.StartSessionAsync(`Player_${userId}`)).then((p
389389
run at once per server (then it throws); one past 60 s is logged and flagged in Server > Status. Older kernels
390390
throw "needs kernel 0.3.8; use the DataHost job queue (Player data guide)": the other option, which works on every
391391
kernel. `features.runDetached` tells which one this server has.
392+
- **Live debug: `remoteDebug` and `fleet`** (framework 0.7.1, kernel 0.5.7, server only; `src/live-debug.ts`,
393+
plans/34): the game's own read-only commands and streams for the backend explorer's server page (`/servers/<JobId>`),
394+
and its own columns on the Fleet page. Import them (`import { fleet, remoteDebug } from "@typetorch/framework"`) or
395+
use `TypeTorch.remoteDebug` / `TypeTorch.fleet`:
396+
397+
```ts
398+
// A button on the server page (op `game.round_state`); fn(args, caller) runs for an owner, its return is the answer.
399+
remoteDebug.command("round_state", () => ({ kind: "kv", items: { round: this.round, alive: this.alive.size() } }), {
400+
label: "Round",
401+
description: "The current round and who is alive",
402+
});
403+
// Sampled at hz (0.2..4) only while someone watches the page; "points" values draw as markers on the live map.
404+
remoteDebug.stream("loot", () => ({ points: this.chests.map((c) => ({ x: c.X, y: c.Y, z: c.Z, label: "chest" })) }), {
405+
hz: 1,
406+
kind: "points",
407+
});
408+
// Rides every fleet heartbeat (Fleet page > Columns). Strings at most 64 characters, numbers, booleans.
409+
fleet.column("current_map", () => this.mapName, { label: "Map", type: "string" });
410+
fleet.columnsChanged(); // after the map changed: a heartbeat soon (the kernel sends at most one per 10 s)
411+
```
412+
413+
- **Commands must be read-only.** The kernel can't check what a handler does; the explorer labels them
414+
"game-defined". They get remote debug's own rules: owners only, 30 a minute, 10 s each, args at most 2 KB, the
415+
answer at most 200 KB of JSON and scrubbed of every secret the kernel knows. Answers the explorer renders:
416+
`{ kind: "table", columns: [...], rows: [[...]] }`, `{ kind: "kv", items: { k: v } }`, anything else as JSON.
417+
- **Columns must never carry player names, UserIds or other personal data**: they are game state (`current_map`,
418+
`in_round`, a round number) and the backend stores them with the server row.
419+
- Streams are posted with the kernel's built-in `positions` (every player's position, health and state) every
420+
second while the page is open; a sample at most 16 KB of JSON (bigger is dropped).
421+
- Getters and samplers run on kernel threads under pcall with a short deadline (a column that throws, yields or
422+
returns anything else is empty): keep them cheap, don't yield.
423+
- Names `^[a-z][a-z0-9_]{0,31}$`; at most 16 columns, 32 commands and 8 streams; labels at most 40 characters,
424+
descriptions 200. A bad name or option, a duplicate or one past a limit throws at once; each call returns a
425+
function that removes the entry (give it to a trove). Registrations in one frame reach the kernel as one
426+
declaration; ones made before the generation is bound (module top level) are sent when it is.
427+
- A client throws (server only). Kernels before 0.5.7, places that don't map the kernel's `LiveDebug.luau`, Studio
428+
and edit mode accept and check everything and send nothing (`remoteDebug.supported()`, `features.liveDebug`).
392429
- **`notifications()`** (plans/29): native Roblox experience notifications through the TypeTorch backend (`backend.url`
393430
/ `backend.key`, the launch-token keys in `backend.notify` from `typetorch notify key rotate`; no kernel change).
394431
The game says why a player should hear from it, the backend decides when and sends, the framework verifies the tap:
@@ -703,7 +740,8 @@ bun run build # rbxtsc --type package -> out/
703740
Attention issues.
704741
The others, each on the compiled `out/`: `test-hot-assets` (manifest parser, AssetSync decisions incl. the backup
705742
vouch, live-copy rules), `test-messaging` (messaging and the server list), `test-settings` (`liveConfig` and the
706-
settings lines), `test-live-events` (scheduled live events on a fake clock), `test-detached` (`runDetached`), `test-dev-access` (the dev-only tools rule and every gate through
743+
settings lines), `test-live-events` (scheduled live events on a fake clock), `test-detached` (`runDetached`), `test-live-debug` (`remoteDebug` / `fleet`: names, limits, the client,
744+
older kernels, one declaration per frame, the handlers, columnsChanged), `test-dev-access` (the dev-only tools rule and every gate through
707745
it), `test-remote-debug` (the remote debug ops and their limits), `test-net-inspect` (packet capture),
708746
`test-explorer-core` (Dex), `test-toolbox-sanitize` (the Creator Store sanitizer), `test-sha256` (SHA-256 / HMAC in util and the
709747
pairing-code fingerprint), `test-notifications` (the launch token with the contract's shared vector, intent checks,

‎scripts/test-live-debug.luau‎

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
--!nocheck
2+
-- Offline test of the compiled live debug API (out/live-debug.luau: remoteDebug.command / stream, fleet.column /
3+
-- columnsChanged; plans/34) under Lune, against stub kernels: names and options are checked (bad ones, duplicates and
4+
-- ones past the limits throw), a client throws, older kernels and edit mode accept and send nothing, a 0.5.7 kernel
5+
-- gets one declaration per frame (in order, with labels, kinds and types), its handlers reach the game's functions,
6+
-- removing an entry re-declares, columnsChanged reaches the kernel (declarations first), unbind stops everything.
7+
-- Run after `bun run build` (Lune is pinned in kernel/rokit.toml):
8+
-- cd ../kernel && lune run ../framework/scripts/test-live-debug.luau ../framework/
9+
local fs = require("@lune/fs")
10+
local luau = require("@lune/luau")
11+
local process = require("@lune/process")
12+
local task = require("@lune/task")
13+
local root = process.args[1] or "./"
14+
15+
local passed, failed = 0, 0
16+
local function check(label, condition, detail)
17+
if condition then
18+
passed += 1
19+
else
20+
failed += 1
21+
print("FAIL " .. label .. (if detail ~= nil then ": " .. tostring(detail) else ""))
22+
end
23+
end
24+
local function throws(fn, fragment)
25+
local ok, err = pcall(fn)
26+
return not ok and string.find(tostring(err), fragment, 1, true) ~= nil, err
27+
end
28+
local function settle()
29+
for _ = 1, 3 do
30+
task.wait()
31+
end
32+
end
33+
34+
local env = setmetatable({ task = task, script = {} }, { __index = _G })
35+
local L = luau.load(fs.readFile(`{root}out/live-debug.luau`), { debugName = "live-debug", environment = env })()
36+
local remoteDebug, fleet = L.remoteDebug, L.fleet
37+
38+
-- Names ---------------------------------------------------------------------------------------------------------------
39+
check("names: valid", L.validDebugName("a") and L.validDebugName("round_state") and L.validDebugName("a" .. string.rep("9", 31)))
40+
check("names: invalid", not L.validDebugName("") and not L.validDebugName("A") and not L.validDebugName("_x") and not L.validDebugName("1x") and not L.validDebugName("a-b") and not L.validDebugName("a.b") and not L.validDebugName(string.rep("a", 33)) and not L.validDebugName(5))
41+
42+
-- Edit mode (no kernel): accepted and checked ------------------------------------------------------------------------------
43+
local removeEdit = remoteDebug:command("edit_cmd", function() end)
44+
check("edit mode: a command is accepted (returns a remover); not supported", typeof(removeEdit) == "function" and remoteDebug:supported() == false)
45+
check("edit mode: columnsChanged does nothing", pcall(function()
46+
fleet:columnsChanged()
47+
end))
48+
removeEdit()
49+
check("bad name throws", throws(function()
50+
remoteDebug:command("Bad", function() end)
51+
end, "^[a-z][a-z0-9_]{0,31}$"))
52+
check("no function throws", throws(function()
53+
remoteDebug:command("x", nil)
54+
end, "pass a function"))
55+
check("a label over 40 characters throws", throws(function()
56+
fleet:column("x", function() end, { label = string.rep("l", 41) })
57+
end, "at most 40"))
58+
check("a description over 200 characters throws", throws(function()
59+
remoteDebug:command("x", function() end, { description = string.rep("d", 201) })
60+
end, "at most 200"))
61+
check("labels count characters, not bytes (40 x é is fine)", pcall(function()
62+
fleet:column("accents", function() end, { label = string.rep("é", 40) })()
63+
end))
64+
check("stream: no hz throws", throws(function()
65+
remoteDebug:stream("s", function() end, {})
66+
end, "hz must be"))
67+
check("stream: hz past 4 or under 0.2 throws", throws(function()
68+
remoteDebug:stream("s", function() end, { hz = 5 })
69+
end, "hz must be") and throws(function()
70+
remoteDebug:stream("s", function() end, { hz = 0.1 })
71+
end, "hz must be"))
72+
check("stream: a bad kind throws", throws(function()
73+
remoteDebug:stream("s", function() end, { hz = 1, kind = "dots" })
74+
end, "kind must be"))
75+
check("column: a bad type throws", throws(function()
76+
fleet:column("c", function() end, { type = "date" })
77+
end, "type must be"))
78+
79+
-- Limits and duplicates
80+
do
81+
local removers = {}
82+
for index = 1, 16 do
83+
table.insert(removers, fleet:column(`c{index}`, function() end))
84+
end
85+
check("limit: a 17th column throws", throws(function()
86+
fleet:column("c17", function() end)
87+
end, "at most 16 columns"))
88+
check("duplicate: throws", throws(function()
89+
fleet:column("c1", function() end)
90+
end, "already registered"))
91+
for _, remove in removers do
92+
remove()
93+
end
94+
for index = 1, 32 do
95+
table.insert(removers, remoteDebug:command(`m{index}`, function() end))
96+
end
97+
check("limit: a 33rd command throws", throws(function()
98+
remoteDebug:command("m33", function() end)
99+
end, "at most 32 commands"))
100+
for index = 1, 8 do
101+
table.insert(removers, remoteDebug:stream(`s{index}`, function() end, { hz = 1 }))
102+
end
103+
check("limit: a 9th stream throws", throws(function()
104+
remoteDebug:stream("s9", function() end, { hz = 1 })
105+
end, "at most 8 streams"))
106+
for _, remove in removers do
107+
remove()
108+
end
109+
check("removed entries free their names", pcall(function()
110+
fleet:column("c1", function() end)()
111+
end))
112+
end
113+
114+
-- A client: server only --------------------------------------------------------------------------------------------------
115+
L.bindLiveDebug("client", {})
116+
check("client: command, stream, column and columnsChanged throw server-only", throws(function()
117+
remoteDebug:command("x", function() end)
118+
end, "server-only") and throws(function()
119+
remoteDebug:stream("x", function() end, { hz = 1 })
120+
end, "server-only") and throws(function()
121+
fleet:column("x", function() end)
122+
end, "server-only") and throws(function()
123+
fleet:columnsChanged()
124+
end, "server-only"))
125+
L.unbindLiveDebug()
126+
127+
-- An older kernel (no debugDeclare): silent ---------------------------------------------------------------------------------
128+
local oldKernel = { kernelVersion = "0.5.6" }
129+
L.bindLiveDebug("server", oldKernel)
130+
local removeOld = remoteDebug:command("old_cmd", function() end)
131+
settle()
132+
check("kernel 0.5.6: accepted, not supported, nothing called", typeof(removeOld) == "function" and remoteDebug:supported() == false)
133+
check("kernel 0.5.6: columnsChanged is a no-op", pcall(function()
134+
fleet:columnsChanged()
135+
end))
136+
removeOld()
137+
L.unbindLiveDebug()
138+
139+
-- Kernel 0.5.7 -------------------------------------------------------------------------------------------------------------
140+
local kernel = { kernelVersion = "0.5.7", declared = {}, changed = 0 }
141+
function kernel:debugDeclare(declarations)
142+
table.insert(self.declared, declarations)
143+
return { ok = true, hash = "h", columns = #declarations.columns, commands = #declarations.commands, streams = #declarations.streams, dropped = 0 }
144+
end
145+
function kernel:onDebugCommand(fn)
146+
self.command = fn
147+
end
148+
function kernel:onDebugColumn(fn)
149+
self.column = fn
150+
end
151+
function kernel:onDebugStream(fn)
152+
self.stream = fn
153+
end
154+
function kernel:debugColumnsChanged()
155+
self.changed += 1
156+
return true
157+
end
158+
159+
-- Registered before the bind (module top level): sent at bind.
160+
local mapName = "Lobby"
161+
fleet:column("current_map", function()
162+
return mapName
163+
end, { label = "Map", type = "string" })
164+
L.bindLiveDebug("server", kernel)
165+
check("bind: handlers installed and the early registration declared at once", typeof(kernel.command) == "function" and typeof(kernel.column) == "function" and typeof(kernel.stream) == "function" and #kernel.declared == 1 and kernel.declared[1].columns[1].name == "current_map")
166+
check("supported on 0.5.7", remoteDebug:supported() == true)
167+
168+
local calls = {}
169+
remoteDebug:command("round_state", function(args, caller)
170+
table.insert(calls, { args = args, caller = caller })
171+
return { kind = "kv", items = { round = 3, n = args.n } }
172+
end, { label = "Round", description = "The current round" })
173+
fleet:column("in_round", function()
174+
return true
175+
end, { type = "bool" })
176+
local removeLoot = remoteDebug:stream("loot", function()
177+
return { points = { { x = 1, y = 2, z = 3 } } }
178+
end, { hz = 2, kind = "points", label = "Loot" })
179+
remoteDebug:stream("state", function()
180+
return { phase = "intermission" }
181+
end, { hz = 0.5 })
182+
check("registrations in one frame are declared once, deferred", #kernel.declared == 1)
183+
settle()
184+
check("...then one declaration", #kernel.declared == 2, #kernel.declared)
185+
local decl = kernel.declared[2]
186+
check("declaration: columns in order with label and type", #decl.columns == 2 and decl.columns[1].name == "current_map" and decl.columns[1].label == "Map" and decl.columns[1].type == "string" and decl.columns[2].name == "in_round" and decl.columns[2].type == "bool")
187+
check("declaration: commands with label and description", #decl.commands == 1 and decl.commands[1].name == "round_state" and decl.commands[1].label == "Round" and decl.commands[1].description == "The current round")
188+
check("declaration: streams with hz, kind (json by default) and label", #decl.streams == 2 and decl.streams[1].hz == 2 and decl.streams[1].kind == "points" and decl.streams[1].label == "Loot" and decl.streams[2].kind == "json")
189+
190+
-- The kernel's handlers reach the game's functions.
191+
local caller = { kind = "roblox", userId = 1, owner = true, via = "roblox" }
192+
local answer = kernel.command("round_state", { n = 7 }, caller)
193+
check("command handler: fn(args, caller), its answer returned", answer.items.round == 3 and answer.items.n == 7 and calls[1].caller == caller)
194+
check("command handler: non-table args become {}", kernel.command("round_state", "junk", caller).items.n == nil)
195+
check("command handler: an unknown name throws", throws(function()
196+
kernel.command("nope", {}, caller)
197+
end, "unknown command"))
198+
check("column handler: the getter's value", kernel.column("current_map") == "Lobby" and kernel.column("in_round") == true and kernel.column("nope") == nil)
199+
mapName = "HedgeMaze"
200+
check("column handler: read live", kernel.column("current_map") == "HedgeMaze")
201+
check("stream handler: the sampler's value", kernel.stream("loot").points[1].x == 1 and kernel.stream("state").phase == "intermission" and kernel.stream("nope") == nil)
202+
203+
-- columnsChanged
204+
fleet:columnsChanged()
205+
check("columnsChanged reaches the kernel", kernel.changed == 1)
206+
local before = #kernel.declared
207+
fleet:column("score", function()
208+
return 5
209+
end)
210+
fleet:columnsChanged()
211+
check("columnsChanged: pending declarations go first", #kernel.declared == before + 1 and kernel.changed == 2 and #kernel.declared[#kernel.declared].columns == 3)
212+
settle()
213+
check("...and not twice", #kernel.declared == before + 1)
214+
215+
-- Removing re-declares.
216+
removeLoot()
217+
settle()
218+
local last = kernel.declared[#kernel.declared]
219+
check("removing a stream re-declares without it", #last.streams == 1 and last.streams[1].name == "state" and kernel.stream("loot") == nil)
220+
removeLoot()
221+
settle()
222+
check("a remover is idempotent", #kernel.declared[#kernel.declared].streams == 1)
223+
224+
-- A throwing kernel never breaks the game.
225+
local angry = { debugDeclare = function()
226+
error("kernel down")
227+
end, debugColumnsChanged = function()
228+
error("kernel down")
229+
end }
230+
L.unbindLiveDebug()
231+
L.bindLiveDebug("server", angry)
232+
check("a kernel that throws: bind, register and columnsChanged don't", pcall(function()
233+
remoteDebug:command("extra", function() end)
234+
settle()
235+
fleet:columnsChanged()
236+
end))
237+
238+
-- Unbind: nothing reaches the kernel any more.
239+
L.unbindLiveDebug()
240+
local count = #kernel.declared
241+
remoteDebug:command("after", function() end)
242+
settle()
243+
fleet:columnsChanged()
244+
check("after unbind: nothing is sent", #kernel.declared == count and kernel.changed == 2 and remoteDebug:supported() == false)
245+
246+
print(`test-live-debug: {passed} passed, {failed} failed`)
247+
if failed > 0 then
248+
process.exit(1)
249+
end

‎src/index.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ export type { PlayerState } from "./runtime/player-state";
2929
export { TypeTorch } from "./typetorch";
3030
export type { BranchChange, TypeTorchApi, TypeTorchFeatures } from "./typetorch";
3131
export type { MessageSource, MessagingApi, MessagingSubscribeOptions } from "./messaging";
32+
// Live debug (plans/34): game-defined read-only commands and streams for the explorer, custom Fleet columns.
33+
export { fleet, remoteDebug } from "./live-debug";
34+
export type {
35+
DebugCommandArgs,
36+
DebugCommandOptions,
37+
DebugStreamOptions,
38+
FleetApi,
39+
FleetColumnOptions,
40+
FleetColumnValue,
41+
RemoteDebugApi,
42+
} from "./live-debug";
3243
export type { GameServer, ServerListOptions } from "./servers";
3344
export type { LiveConfig, LiveConfigOptions, LiveEvent, LiveEvents } from "./settings";
3445
export type { Notifications, NotifyOpen, OptInResult, WantOptions } from "./notifications/api";
@@ -72,6 +83,8 @@ export type {
7283
BranchInfo,
7384
Channel,
7485
ClientKernel,
86+
DebugDeclarations,
87+
DebugDeclareResult,
7588
DetachedStatus,
7689
DevInfo,
7790
GameMessageMeta,
@@ -88,6 +101,7 @@ export type {
88101
NewServerReport,
89102
PendingUpdate,
90103
PreviousGeneration,
104+
RemoteDebugCaller,
91105
Role,
92106
SettingsStatus,
93107
ServerKernel,

‎src/kernel.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,25 @@ export interface RemoteDebugCaller {
924924
via: "roblox" | "admin token";
925925
}
926926

927+
/**
928+
* Kernel 0.5.7 (plans/34): what a generation declares with `api:debugDeclare` (src/live-debug.ts builds it): custom fleet
929+
* columns, read-only commands (the explorer's op `game.<name>`) and streams.
930+
*/
931+
export interface DebugDeclarations {
932+
columns: { name: string; label?: string; type?: "string" | "number" | "bool" }[];
933+
commands: { name: string; label?: string; description?: string }[];
934+
streams: { name: string; hz: number; kind?: "points" | "json"; label?: string }[];
935+
}
936+
/** Kernel 0.5.7: `api:debugDeclare`'s answer (bad entries are dropped and counted, never an error). */
937+
export interface DebugDeclareResult {
938+
ok: boolean;
939+
hash?: string;
940+
columns: number;
941+
commands: number;
942+
streams: number;
943+
dropped: number;
944+
}
945+
927946
/** Kernel 0.5.0: `status().remoteDebug` (counters; never a command's contents, the URL or the key). */
928947
export interface RemoteDebugStatus {
929948
state: "off" | "polling" | "held";
@@ -1092,6 +1111,22 @@ export interface ServerKernel {
10921111
*/
10931112
onRemoteDebug?(handler: (op: string, args: unknown, caller: RemoteDebugCaller) => unknown): void;
10941113

1114+
/**
1115+
* Kernel 0.5.7 (additive; plans/34): LIVE DEBUG. Replaces this generation's declarations (names
1116+
* ^[a-z][a-z0-9_]{0,31}$; at most 16 columns, 32 commands, 8 streams). The heartbeat then carries their hash `xh`,
1117+
* `x` when the backend needs them and `cv` (the column values). `debugDeclare` doubles as the feature test; the
1118+
* framework drives it through `remoteDebug` / `fleet` (src/live-debug.ts).
1119+
*/
1120+
debugDeclare?(declarations: DebugDeclarations): DebugDeclareResult;
1121+
/** Kernel 0.5.7: fn(name, args, caller) answers a declared command (op `game.<name>`) on a kernel thread, owners only. */
1122+
onDebugCommand?(handler: ((name: string, args: unknown, caller: RemoteDebugCaller) => unknown) | undefined): void;
1123+
/** Kernel 0.5.7: fn(name) -> a declared column's value (string <= 64 characters, number, boolean; else null). */
1124+
onDebugColumn?(handler: ((name: string) => unknown) | undefined): void;
1125+
/** Kernel 0.5.7: fn(name) -> a declared stream's sample, while the explorer watches this server. */
1126+
onDebugStream?(handler: ((name: string) => unknown) | undefined): void;
1127+
/** Kernel 0.5.7: the column values changed: a coalesced heartbeat (at most one per 10 s). */
1128+
debugColumnsChanged?(): boolean;
1129+
10951130
/** `typetorch test --cloud`'s stub kernel: true (code that must not run in the gate checks it). */
10961131
readonly test?: boolean;
10971132
}

0 commit comments

Comments
 (0)