Skip to content

Commit c915e80

Browse files
phasenullclaude
andcommitted
TypeTorch.reserveServer: a reserved server on this server's branch for the game's own teleport (0.7.5, kernel 0.5.9)
Server only, never throws on a server: ok=false with "kernel too old" before kernel 0.5.9, "no kernel" in edit mode, or the kernel's own error. Types ReserveServerOptions / ReserveServerReport, features.reserveServer, Lune test scripts/test-reserve-server.luau (13). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a5e5c1b commit c915e80

8 files changed

Lines changed: 183 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,17 @@ TypeTorch.runDetached(() => store.StartSessionAsync(`Player_${userId}`)).then((p
395395
run at once per server (then it throws); one past 60 s is logged and flagged in Server > Status. Older kernels
396396
throw "needs kernel 0.3.8; use the DataHost job queue (Player data guide)": the other option, which works on every
397397
kernel. `features.runDetached` tells which one this server has.
398+
- **`reserveServer(options?)`** (framework 0.7.5, kernel 0.5.9, server only; `src/reserve-server.ts`): reserves a
399+
server for the game's OWN teleport and records it in the kernel's private branch registry on THIS server's branch,
400+
so it boots this branch's head. A plain `TeleportService:ReserveServer` server has no record and boots the
401+
universe's default branch, so players sent from a dev branch would land on the prod build. It does not teleport:
402+
teleport with your own `TeleportOptions` (`ReservedServerAccessCode = accessCode`, `SetTeleportData(...)`), e.g. a
403+
ticket bound to `privateServerId`. Returns `{ ok, accessCode?, privateServerId?, branch?, error? }` and never
404+
throws on a server (clients throw). Never another branch (prod game code must not start dev builds; `options` takes
405+
no branch). No dev check; at most 20 calls a minute per server (`"rate limited"`); yields (ReserveServer + a
406+
DataStore write). Other errors: `"studio"`, `"kernel too old"` (before kernel 0.5.9), `"no kernel"` (edit mode),
407+
`"no artifact on branch <b>"`, `"ReserveServer: ..."`, `"could not save the branch: ..."`, `"kernel error: ..."`.
408+
`features.reserveServer` tells whether this server's kernel has it. `accessCode` is server-only data.
398409
- **Live debug: `remoteDebug` and `fleet`** (framework 0.7.1, kernel 0.5.7, server only; `src/live-debug.ts`,
399410
plans/34): the game's own read-only commands and streams for the backend explorer's server page (`/servers/<JobId>`),
400411
and its own columns on the Fleet page. Import them (`import { fleet, remoteDebug } from "@typetorch/framework"`) or
@@ -807,7 +818,7 @@ bun run build # rbxtsc --type package -> out/
807818
Attention issues.
808819
The others, each on the compiled `out/`: `test-hot-assets` (manifest parser, AssetSync decisions incl. the backup
809820
vouch, live-copy rules), `test-messaging` (messaging and the server list), `test-settings` (`liveConfig` and the
810-
settings lines), `test-live-events` (scheduled live events on a fake clock), `test-detached` (`runDetached`), `test-live-debug` (`remoteDebug` / `fleet`: names, limits, the client,
821+
settings lines), `test-live-events` (scheduled live events on a fake clock), `test-detached` (`runDetached`), `test-reserve-server` (`reserveServer`: pass-through, older kernels, edit mode, odd answers), `test-live-debug` (`remoteDebug` / `fleet`: names, limits, the client,
811822
older kernels, one declaration per frame, the handlers, columnsChanged), `test-dev-access` (the dev-only tools rule and every gate through
812823
it), `test-remote-debug` (the remote debug ops and their limits), `test-net-inspect` (packet capture),
813824
`test-explorer-core` (Dex), `test-toolbox-sanitize` (the Creator Store sanitizer), `test-sha256` (SHA-256 / HMAC in util and the

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@typetorch/framework",
3-
"version": "0.7.4",
3+
"version": "0.7.5",
44
"description": "TypeTorch framework for roblox-ts: hot-swappable modules (dependency injection, lifecycle, troves), guarded networking, hot assets and the in-game dev menu. Ships inside every TypeTorch artifact.",
55
"license": "MIT",
66
"author": "TypeTorch",

‎scripts/test-reserve-server.luau‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--!nocheck
2+
-- Offline test of the compiled TypeTorch.reserveServer wrapper (out/reserve-server.luau) under Lune, against stub
3+
-- kernels: a 0.5.9 kernel's report passes through (method call with the options), "kernel too old" without the
4+
-- method, "no kernel" in edit mode, a throwing kernel, a non-table report, ok without a code, the kernel's own errors.
5+
-- Run after `bun run build` (Lune is pinned in kernel/rokit.toml):
6+
-- cd ../kernel && lune run ../framework/scripts/test-reserve-server.luau ../framework/
7+
local fs = require("@lune/fs")
8+
local luau = require("@lune/luau")
9+
local process = require("@lune/process")
10+
local root = process.args[1] or "./"
11+
12+
local passed, failed = 0, 0
13+
local function check(label, condition, detail)
14+
if condition then
15+
passed += 1
16+
else
17+
failed += 1
18+
print("FAIL " .. label .. (if detail ~= nil then ": " .. tostring(detail) else ""))
19+
end
20+
end
21+
22+
local env = setmetatable({}, { __index = _G })
23+
local module = luau.load(fs.readFile(root .. "out/reserve-server.luau"), { debugName = "reserve-server", environment = env })()
24+
local reserveServerOn, supported = module.reserveServerOn, module.reserveServerSupported
25+
26+
check("RESERVE_SERVER_KERNEL is 0.5.9", module.RESERVE_SERVER_KERNEL == "0.5.9")
27+
28+
-- A 0.5.9 kernel: called as a method, with the options; the report passes through.
29+
local calls = {}
30+
local kernel = {
31+
reserveServer = function(self, options)
32+
table.insert(calls, { self = self, options = options })
33+
return { ok = true, accessCode = "code-1", privateServerId = "ps-1", branch = "freemode", extra = "dropped" }
34+
end,
35+
}
36+
local opts = { branch = "main" }
37+
local r = reserveServerOn(kernel, opts)
38+
check("ok: the code, the id and the branch", r.ok == true and r.accessCode == "code-1" and r.privateServerId == "ps-1" and r.branch == "freemode" and r.error == nil)
39+
check("ok: only the known fields", r.extra == nil)
40+
check("ok: a method call with the options", #calls == 1 and calls[1].self == kernel and calls[1].options == opts)
41+
check("supported: a kernel with the method", supported(kernel) == true)
42+
43+
-- Older kernels and edit mode.
44+
r = reserveServerOn({ accessCode = function() end })
45+
check("an older kernel: kernel too old", r.ok == false and r.error == "kernel too old" and r.accessCode == nil)
46+
check("supported: false without the method", supported({}) == false and supported(nil) == false)
47+
r = reserveServerOn(nil)
48+
check("edit mode: no kernel", r.ok == false and r.error == "no kernel")
49+
50+
-- The kernel's refusals and odd answers never throw.
51+
r = reserveServerOn({ reserveServer = function() return { ok = false, error = "rate limited", branch = "dev" } end })
52+
check("a refusal passes through", r.ok == false and r.error == "rate limited" and r.branch == "dev" and r.accessCode == nil)
53+
r = reserveServerOn({ reserveServer = function() error("boom") end })
54+
check("a throwing kernel: kernel error", r.ok == false and string.find(r.error, "kernel error:", 1, true) == 1 and string.find(r.error, "boom", 1, true) ~= nil)
55+
r = reserveServerOn({ reserveServer = function() return "nope" end })
56+
check("a non-table report: kernel error: no report", r.ok == false and r.error == "kernel error: no report")
57+
r = reserveServerOn({ reserveServer = function() return { ok = true, privateServerId = "ps" } end })
58+
check("ok without an access code: refused, no id", r.ok == false and r.error == "kernel error: no access code" and r.privateServerId == nil)
59+
r = reserveServerOn({ reserveServer = function() return { ok = false } end })
60+
check("a refusal with no reason: kernel error: no reason", r.ok == false and r.error == "kernel error: no reason")
61+
62+
print(`reserve server: {passed} passed, {failed} failed`)
63+
if failed > 0 then
64+
process.exit(1)
65+
end

‎src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export type {
100100
MessagingStatus,
101101
NewServerReport,
102102
PendingUpdate,
103+
ReserveServerOptions,
104+
ReserveServerReport,
103105
PreviousGeneration,
104106
RemoteDebugCaller,
105107
Role,

‎src/kernel.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,35 @@ export interface NewServerReport {
356356
assetId?: number;
357357
}
358358

359+
/** Kernel 0.5.9 `reserveServer(options?)`: reserved for later; every field is ignored today. */
360+
export interface ReserveServerOptions {
361+
/**
362+
* Not supported: the reserved server always boots THIS server's branch (prod game code must not be able to start
363+
* dev builds). A value is ignored.
364+
*/
365+
branch?: never;
366+
}
367+
368+
/**
369+
* What `TypeTorch.reserveServer()` (kernel 0.5.9 `api:reserveServer`) returns. `accessCode` is server-only data:
370+
* never send it to a client.
371+
*/
372+
export interface ReserveServerReport {
373+
ok: boolean;
374+
/**
375+
* Set when `ok` is false: "studio" | "rate limited" | "no branch" | `no artifact on branch <b>` |
376+
* `ReserveServer: <error>` | `could not save the branch: <error>`; from the framework: "kernel too old" (a kernel
377+
* before 0.5.9), "no kernel" (edit mode), `kernel error: <error>`.
378+
*/
379+
error?: string;
380+
/** For TeleportOptions.ReservedServerAccessCode. Server-only: never send it to a client. */
381+
accessCode?: string;
382+
/** The reserved server's PrivateServerId (what it reads as game.PrivateServerId), e.g. to bind a ticket to it. */
383+
privateServerId?: string;
384+
/** The branch the reserved server boots: this server's. */
385+
branch?: string;
386+
}
387+
359388
/** One known deployment (kernel 0.2+ `artifacts()`), newest first. */
360389
export interface ArtifactEntry {
361390
/** Deploy sequence number (global across branches); absent for entries only known from a server's history. */
@@ -1067,6 +1096,13 @@ export interface ServerKernel {
10671096
/** A reserved server's own access code (server-only data: never send it to a client). */
10681097
accessCode?(): string | undefined;
10691098

1099+
/**
1100+
* Kernel 0.5.9 (additive): reserves a server and records it in the private branch registry on THIS server's
1101+
* branch (it boots this branch's head, like newServer), without teleporting. No dev check; 20 a minute per server.
1102+
* Yields. Game code uses `TypeTorch.reserveServer`. `reserveServer` doubles as the feature test.
1103+
*/
1104+
reserveServer?(options?: ReserveServerOptions): ReserveServerReport;
1105+
10701106
// Kernel 0.3.8 (additive): game messaging and the held roll call topic. `messagingPublish` doubles as the feature
10711107
// test; a place that doesn't map the kernel's Messaging module has none of them. Game code uses `TypeTorch.messaging`.
10721108
/**

‎src/reserve-server.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { ReserveServerOptions, ReserveServerReport, ServerKernel } from "./kernel";
2+
3+
/**
4+
* `TypeTorch.reserveServer(options?)` (server only, kernel 0.5.9): a reserved server for the GAME's own teleport,
5+
* recorded in the kernel's private branch registry on THIS server's branch, so it boots this branch's head instead of
6+
* the universe's default branch. No teleport: the game teleports with its own TeleportOptions. The kernel refuses
7+
* another branch, checks no dev permission and allows 20 calls a minute per server. Never throws: `ok` false with
8+
* `error` ("kernel too old" before 0.5.9, "no kernel" in edit mode, `kernel error: ...`, or the kernel's own).
9+
*/
10+
11+
/** The first kernel with `api:reserveServer`. */
12+
export const RESERVE_SERVER_KERNEL = "0.5.9";
13+
14+
/** The kernel has `api:reserveServer`. */
15+
export function reserveServerSupported(kernel: ServerKernel | undefined): boolean {
16+
return kernel !== undefined && typeIs((kernel as unknown as Record<string, unknown>).reserveServer, "function");
17+
}
18+
19+
/** `TypeTorch.reserveServer` on the server (the client check is the caller's). */
20+
export function reserveServerOn(kernel: ServerKernel | undefined, options?: ReserveServerOptions): ReserveServerReport {
21+
if (kernel === undefined) return { ok: false, error: "no kernel" };
22+
if (!reserveServerSupported(kernel)) return { ok: false, error: "kernel too old" };
23+
const [ok, report] = pcall(() => kernel.reserveServer!(options));
24+
if (!ok) return { ok: false, error: `kernel error: ${report}` };
25+
if (!typeIs(report, "table")) return { ok: false, error: "kernel error: no report" };
26+
const result = report as ReserveServerReport;
27+
const branch = typeIs(result.branch, "string") ? result.branch : undefined;
28+
if (result.ok === true && typeIs(result.accessCode, "string") && typeIs(result.privateServerId, "string")) {
29+
return { ok: true, accessCode: result.accessCode, privateServerId: result.privateServerId, branch };
30+
}
31+
if (result.ok === true) return { ok: false, error: "kernel error: no access code", branch };
32+
return { ok: false, error: typeIs(result.error, "string") ? result.error : "kernel error: no reason", branch };
33+
}

‎src/typetorch.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import type {
1414
LogEntry,
1515
PendingUpdate,
1616
PreviousGeneration,
17+
ReserveServerOptions,
18+
ReserveServerReport,
1719
Role,
1820
ServerKernel,
1921
ServerType,
@@ -34,6 +36,7 @@ import { bindMessaging, messaging, messagingSupported, unbindMessaging, type Mes
3436
import { bindServers, listServers, setServerInfo, unbindServers, type GameServer, type ServerListOptions } from "./servers";
3537
import { bindSettings, currentSettings, liveConfig, liveEvents, settingsSupported, unbindSettings, type LiveConfig, type LiveConfigOptions, type LiveEvents } from "./settings";
3638
import { bindDetached, detachedSupported, runDetached, unbindDetached } from "./detached";
39+
import { reserveServerOn, reserveServerSupported } from "./reserve-server";
3740
import { bindLiveDebug, fleet, remoteDebug, unbindLiveDebug, type FleetApi, type RemoteDebugApi } from "./live-debug";
3841

3942
/**
@@ -75,6 +78,8 @@ export interface TypeTorchFeatures {
7578
readonly runDetached: boolean;
7679
/** `remoteDebug.command` / `stream` and `fleet.column` reach the backend (kernel 0.5.7+, a server). */
7780
readonly liveDebug: boolean;
81+
/** `reserveServer` reserves a server on this server's branch (kernel 0.5.9+, a server). */
82+
readonly reserveServer: boolean;
7883
}
7984

8085
export interface TypeTorchApi {
@@ -274,6 +279,27 @@ export interface TypeTorchApi {
274279
*/
275280
runDetached<T>(fn: () => T): Promise<T>;
276281

282+
// Reserved servers (server only, kernel 0.5.9) -----------------------------------------------------------------------
283+
/**
284+
* Reserves a server for the GAME's own teleport and records it in the kernel's private branch registry on THIS
285+
* server's branch, so it boots this branch's head (a game-made `TeleportService:ReserveServer` server has no record
286+
* and boots the universe's default branch: a dev branch's players would land on the prod build). It does NOT
287+
* teleport: teleport with your own TeleportOptions (`ReservedServerAccessCode = accessCode`, `SetTeleportData`).
288+
* Never another branch (prod code must not start dev builds). No dev check; at most 20 calls a minute per server
289+
* ("rate limited"). Yields (ReserveServer + a DataStore write). Never throws on the server: `ok` false with
290+
* `error` ("studio", "rate limited", "kernel too old" before kernel 0.5.9, "no kernel" in edit mode, ...).
291+
* Clients: throws. `accessCode` is server-only data: never send it to a client.
292+
*
293+
* const r = TypeTorch.reserveServer();
294+
* if (r.ok) {
295+
* const options = new Instance("TeleportOptions");
296+
* options.ReservedServerAccessCode = r.accessCode!;
297+
* options.SetTeleportData({ ticket: sign(r.privateServerId!) });
298+
* TeleportService.TeleportAsync(game.PlaceId, [player], options);
299+
* }
300+
*/
301+
reserveServer(options?: ReserveServerOptions): ReserveServerReport;
302+
277303
// Live debug (server only, kernel 0.5.7; plans/34) -------------------------------------------------------------------
278304
/**
279305
* Game-defined read-only commands and streams for the backend explorer's server page: `command(name, fn, { label?,
@@ -344,6 +370,7 @@ const NO_FEATURES: TypeTorchFeatures = {
344370
settings: false,
345371
runDetached: false,
346372
liveDebug: false,
373+
reserveServer: false,
347374
};
348375

349376
function hasMethod(kernel: object, name: string): boolean {
@@ -599,6 +626,11 @@ class TypeTorchRuntime implements TypeTorchApi {
599626
return runDetached(fn);
600627
}
601628

629+
reserveServer(options?: ReserveServerOptions): ReserveServerReport {
630+
if (client) error("TypeTorch.reserveServer() is server-only (the access code must stay on the server)", 2);
631+
return reserveServerOn(server, options);
632+
}
633+
602634
notifications(): Notifications {
603635
const factory = notificationsFactory;
604636
if (factory) {
@@ -759,6 +791,7 @@ export function bindTypeTorch(
759791
settings: settingsSupported(),
760792
runDetached: detachedSupported(),
761793
liveDebug: remoteDebug.supported(),
794+
reserveServer: reserveServerSupported(server),
762795
};
763796
}
764797

‎src/version.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
* This framework's version, as package.json says (scripts/test-state-inspect.luau checks they match). Shown in
33
* uploaded log headers (Logs > Upload); bump it with package.json.
44
*/
5-
export const FRAMEWORK_VERSION = "0.7.4";
5+
export const FRAMEWORK_VERSION = "0.7.5";

0 commit comments

Comments
 (0)