Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/luau-remo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-baseplate": minor
---

Luau games declare their remotes with Remo instead of the template's own `Remote`, `Serve` and `RateLimit` modules: each feature's remotes sit in a Remo namespace, with a `t` validator for what clients send and `throttleMiddleware` on client-to-server remotes. The check still fails on `OnServerEvent` or `OnServerInvoke` in the game's own code.
6 changes: 3 additions & 3 deletions cli/template/luau/base/.agents/skills/add-feature/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ description: Add a feature to the game: a service, its remotes, a controller, UI

Run `mise run feature <name>` with the name in lowercase letters and digits (`dailyreward`: it is part of a Roblox path, so no hyphens). In a multi-place game the feature goes in the shared code and every place lists it; `mise run feature <name> <place>` keeps it to one place. It writes, under `features/<name>/`:

- `shared/<Name>Network.luau`: the remotes between client and server, each with its argument types and, for what clients send, a `t` check.
- `server/<Name>Service.luau`: the service, with every client-to-server event behind `RateLimit`, and its spec beside it.
- `shared/<Name>Network.luau`: the remotes between client and server, made with Remo, each with its argument types and, for what clients send, a `t` validator.
- `server/<Name>Service.luau`: the service, which connects to the remotes, and its spec beside it.
- `client/<Name>Controller.luau`: the controller.

and adds the service and the controller to the lists in `app/server/Main.server.luau` and `app/client/Start.luau`, or they never start. Then make it do something:

1. **Rules first.** Put the rules in the service, as functions a spec can call: ownership, cost, cooldowns, limits. The clients ask; nothing the client sends is true until the service checks it. Write the spec for each rule before the code that uses it (`write-tests` skill).
2. **Remotes.** Add them to `shared/<Name>Network.luau`. Each client-to-server event goes through `Serve.event` with a limit that fits the action (a purchase is one a second, not twenty).
2. **Remotes.** Add them to `shared/<Name>Network.luau`. Give each argument a client sends a `t` validator, and each client-to-server remote a `throttleMiddleware` that fits the action (a purchase is one a second, not twenty).
3. **Controller.** Keep what the UI shows, and let `App.luau` subscribe to it, as `ClickerController` does.
4. **UI.** A component in `client/` with a story, and look at its picture (`mise run ui --filter <name>`): text cut off, overlap, the empty state.
5. **End to end.** A request from the client through the real server and remotes in `tests/e2e/<name>.luau`, copied from `tests/e2e/clicker.luau`.
Expand Down
8 changes: 4 additions & 4 deletions cli/template/luau/base/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ Each feature is a folder, `src/features/<feature>/`; `src/features/clicker` is t

A file outside these three folders isn't placed in the game, and the check fails on it: there's no default side, so server code can't reach clients by mistake.

Require by instance path, the way `rogen where <file>` prints it: `require(ReplicatedStorage.Shared.features.clicker.ClickerNetwork)`, `require(ServerScriptService.network.Serve)`. Client code is copied into each player at runtime, so it reaches other client code from `script`, as `src/app/client/Start.luau` does: `script.Parent.Parent.features.clicker.ClickerController`.
Require by instance path, the way `rogen where <file>` prints it: `require(ReplicatedStorage.Shared.features.clicker.ClickerNetwork)`, `require(ServerScriptService.features.clicker.ClickerService)`. Client code is copied into each player at runtime, so it reaches other client code from `script`, as `src/app/client/Start.luau` does: `script.Parent.Parent.features.clicker.ClickerController`.

`src/app/` holds the entry scripts: `app/server/Main.server.luau` lists every service and starts each, `app/client/Start.luau` (which `Main.client.luau` runs) does the same for controllers and mounts the UI, and `app/client/App.luau` is the root of the UI. `src/network/` holds the remotes (`shared/Remote.luau`) and how the server takes them (`server/Serve.luau`, `server/RateLimit.luau`). Files are PascalCase, named after what they hold: `InventoryService.luau`.
`src/app/` holds the entry scripts: `app/server/Main.server.luau` lists every service and starts each, `app/client/Start.luau` (which `Main.client.luau` runs) does the same for controllers and mounts the UI, and `app/client/App.luau` is the root of the UI. Files are PascalCase, named after what they hold: `InventoryService.luau`.

## Adding a feature

Run `mise run feature <name>` to start one, and read `.agents/skills/add-feature/SKILL.md`: it writes steps 1 and 2 below and the service's spec. By hand:

1. Remotes in `shared/<Feature>Network.luau`, declared with `Remote.clientEvent`, `Remote.clientRequest` or `Remote.serverEvent` and their argument types, as `ClickerNetwork.luau` does. What a client sends gets a `t` check (`t.tuple(t.integer, t.string)`).
1. Remotes in `shared/<Feature>Network.luau`, made with [Remo](https://github.com/littensy/remo) (`ReplicatedStorage.Packages.Remo`) in a namespace named after the feature, with a `Remotes` type that gives their arguments (`Remo.ClientToServer<number>`, `Remo.ServerToClient<number>`, `Remo.ServerAsync<(number), (string)>`), as `ClickerNetwork.luau` does. Each argument a client sends gets a `t` validator (`Remo.remote(t.integer, t.string)`), and each client-to-server remote `.middleware(Remo.throttleMiddleware(<seconds>))`.
2. A service in `server/<Feature>Service.luau` and, if the client takes part, a controller in `client/<Feature>Controller.luau`: a module with a `start()` that connects its remotes and events. Add each to the list in `src/app/server/Main.server.luau` or `src/app/client/Start.luau`, or it never starts; the check fails on one that isn't.
3. UI as a component in `client/` with a story, rendered from `App.luau`.
4. Tests: a spec for the service, and an end-to-end test for each request a client sends.

The rules the code follows:

- **Never trust the client.** Clients ask; services decide. Check every request on the server: rate limits, ownership, cost, distance, cooldowns. The server takes what clients send only through `Serve.event` and `Serve.request`, which drop calls whose arguments fail the remote's `t` check and need a `limit`: `RateLimit.perSecond(20)`, or `RateLimit.trustsClient("<why>")` for a remote that must take every call. The check fails on `OnServerEvent` or `OnServerInvoke` anywhere else.
- **Never trust the client.** Clients ask; services decide. Check every request on the server: rate limits, ownership, cost, distance, cooldowns. The server takes what clients send only through Remo remotes, whose validators drop calls with the wrong arguments and whose throttle drops floods; the throttle fits the action (a purchase is one a second, not twenty). The check fails on `OnServerEvent` or `OnServerInvoke` in the game's code.
- **Services own server state.** A service keeps its state in its module and only it changes it; other code calls its functions. Depend on another service by requiring it.
- **Controllers run the client side.** A controller handles input and the server's events and keeps what the UI shows; `App.luau` subscribes to it, as with `ClickerController`.

Expand Down
7 changes: 3 additions & 4 deletions cli/template/luau/base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A Roblox game in Luau, set up for coding agents: one command checks everything,
- **Strict types for the whole game.** Every file is `--!strict`, and [luau-lsp](https://github.com/JohnnyMorganz/luau-lsp) type-checks it against every instance in the game, world included, and the packages' own types: `require(ReplicatedStorage.Shared.features.clicker.ClickerNetwork)` and `workspace.SpawnLocation` are typed, and a wrong name fails the check.
- **Tests run without Studio.** Jest specs and end-to-end tests, with the real server and a client talking through remotes, run in [ree](https://github.com/Anime-Vanguards/roblox-engine-emulator), an emulated Roblox engine, in a few seconds; a failure points at the file and line. The same specs run in real Studio or Open Cloud through [jest-roblox](https://github.com/christopher-buss/jest-roblox-cli) (`mise run test:engine`).
- **It can see the UI.** `mise run ui` renders every story offline with [rur](https://github.com/Anime-Vanguards/roblox-ui-renderer) and prints where each picture is.
- **Remotes can't be taken unguarded.** Each remote declares its arguments' types and a [`t`](https://github.com/osyrisrblx/t) check; the server takes them only through `Serve`, which rate-limits each player and drops calls that fail the check. The check fails on `OnServerEvent` anywhere else.
- **Remotes can't be taken unguarded.** Remotes are made with [Remo](https://github.com/littensy/remo): each declares its arguments' types and a [`t`](https://github.com/osyrisrblx/t) validator, and client-to-server remotes are throttled per player. The check fails on `OnServerEvent` in the game's own code.
- **The world is files, apart from the code.** Studio edits the map and [rbx-forge](https://github.com/christopher-buss/rbx-forge) syncs it back into `world/` as readable JSON, while Rojo serves only code.
- **Each feature is one folder.** [Rogen](https://github.com/LDGerrits/rogen) routes its `server/`, `client/` and `shared/` folders to the right place in the game.
- **Instructions where agents look.** `AGENTS.md` covers the layout, adding a feature and the rules; `.agents/skills/` (linked as `.claude/skills/`) covers tests, the world and Studio.
Expand All @@ -24,7 +24,7 @@ A Roblox game in Luau, set up for coding agents: one command checks everything,
| Language | [Luau](https://luau.org), strict, type-checked by [luau-lsp](https://github.com/JohnnyMorganz/luau-lsp) |
| Sync | [Rogen](https://github.com/LDGerrits/rogen) over [Rojo](https://rojo.space); [rbx-forge](https://github.com/christopher-buss/rbx-forge) runs Studio and syncback |
| Packages | [Wally](https://wally.run), typed by [wally-package-types](https://github.com/JohnnyMorganz/wally-package-types) |
| Structure | Services and controllers as plain modules; remotes declared once, checked with [`t`](https://github.com/osyrisrblx/t) and rate-limited |
| Structure | Services and controllers as plain modules; remotes declared once with [Remo](https://github.com/littensy/remo), checked with [`t`](https://github.com/osyrisrblx/t) and throttled |
| UI | [React](https://github.com/jsdotlua/react-lua) (react-lua), [Ripple](https://github.com/littensy/ripple) for animation, stories in [UI Labs](https://github.com/PepeElToro41/ui-labs) format |
| Checks | [StyLua](https://github.com/JohnnyMorganz/StyLua) for formatting, [Selene](https://github.com/Kampfkarren/selene) for lint, luau-lsp for types; Lune for place checks; Biome for JSON and the tooling scripts |
| Tests | [Jest](https://github.com/jsdotlua/jest-lua) run in [ree](https://github.com/Anime-Vanguards/roblox-engine-emulator), and in Studio or Open Cloud through [jest-roblox](https://github.com/christopher-buss/jest-roblox-cli) |
Expand Down Expand Up @@ -70,8 +70,7 @@ src/
clicker/ an example feature
server/ ClickerService.luau: owns the counts, checks the cooldown; its spec
client/ ClickerController.luau, ClickerButton.luau and its story
shared/ ClickerNetwork.luau: the remotes
network/ remotes, and how the server takes them: Serve, RateLimit
shared/ ClickerNetwork.luau: the remotes (Remo)
tests/e2e/ end-to-end tests: clicker.luau
world/ the map and lighting, edited in Studio and synced back
wally.toml the Roblox packages
Expand Down
4 changes: 2 additions & 2 deletions cli/template/luau/base/mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ depends = ["generate"]
run = [
"stylua .",
"biome check --write --no-errors-on-unmatched .",
# Tests named so they never run, services nothing starts, and remotes taken without a rate limit.
# Tests named so they never run, services nothing starts, and remotes taken without Remo.
"node scripts/check-sources.ts",
# The agent files at the create-baseplate version package.json pins, and no clash left unmerged.
"node scripts/agent-files.ts --check",
Expand All @@ -96,7 +96,7 @@ depends = ["generate"]
run = [
"stylua --check .",
"biome ci .",
# Tests named so they never run, services nothing starts, and remotes taken without a rate limit.
# Tests named so they never run, services nothing starts, and remotes taken without Remo.
"node scripts/check-sources.ts",
# The agent files at the create-baseplate version package.json pins, and no clash left unmerged.
"node scripts/agent-files.ts --check",
Expand Down
4 changes: 2 additions & 2 deletions cli/template/luau/base/scripts/check-sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Services (*Service.luau that Rogen routes to the server: under a server/ folder, at any depth,
// or named *Service@server) start from src/app/server/Main.server.luau, controllers
// (*Controller.luau routed to the client) from src/app/client/Start.luau;
// - a remote the server takes outside network/server/Serve.luau, with no rate limit or argument
// check (see unguarded-remotes.ts).
// - a remote the server takes directly instead of through Remo, with no validators or throttle
// (see unguarded-remotes.ts).
// `mise run check` runs it. In a multi-place game, a place's entry scripts start its own code and
// the shared code; shared code passes when one place lists it.
import fs from "node:fs";
Expand Down
2 changes: 1 addition & 1 deletion cli/template/luau/base/scripts/feature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test("starts a feature in a single-place game and lists it in the entry scripts"
]) {
assert.ok(fs.existsSync(path.join(dir, "src/features/shop", file)), file);
}
assert.match(read(dir, "src/features/shop/server/ShopService.luau"), /Serve\.event\(ShopNetwork\.request/);
assert.match(read(dir, "src/features/shop/server/ShopService.luau"), /ShopNetwork\.request:connect\(/);
assert.match(
read(dir, "src/app/server/Main.server.luau"),
/require\(ServerScriptService\.features\.shop\.ShopService\),/,
Expand Down
38 changes: 24 additions & 14 deletions cli/template/luau/base/scripts/feature.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// `mise run feature <name>`: starts a feature the way AGENTS.md's "Adding a feature" describes, so
// its folders, its rate-limited event and its entry-script entries are right from the first line.
// its folders, its throttled remote and its entry-script entries are right from the first line.
//
// node scripts/feature.ts <name> [<place>]
//
Expand All @@ -19,27 +19,37 @@ const pascal = (name: string) => name.replace(/^./, (letter) => letter.toUpperCa
export function featureFiles(name: string): Record<string, string> {
const Name = pascal(name);
return {
[`shared/${Name}Network.luau`]: `-- The ${name} feature's remotes. Each declares the arguments it carries: the type for the type checker,
-- and for what clients send, a \`t\` check the server runs before any handler sees the call.
[`shared/${Name}Network.luau`]: `-- The ${name} feature's remotes, made with Remo. The type gives both sides each remote's arguments;
-- Remo runs the \`t\` validators on what arrives before any handler sees it, so every argument a
-- client sends gets one, and throttleMiddleware drops a player's floods.
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Remote = require(ReplicatedStorage.Shared.network.Remote)
local Remo = require(ReplicatedStorage.Packages.Remo)
local t = require(ReplicatedStorage.Packages.t)

return {
-- Client to server: the player asks for their value. Carries nothing.
request = Remote.clientEvent("request", t.tuple()) :: Remote.ClientEvent<()>,
-- Server to client: the player's value.
changed = Remote.serverEvent("changed") :: Remote.ServerEvent<number>,
type Remotes = {
${name}: {
-- Client to server: the player asks for their value. Carries nothing.
request: Remo.ClientToServer<()>,
-- Server to client: the player's value.
changed: Remo.ServerToClient<number>,
},
}

-- The namespace keeps these names apart from other features': "${name}.request", "${name}.changed".
local remotes: Remotes = Remo.createRemotes({
${name} = Remo.namespace({
request = Remo.remote().middleware(Remo.throttleMiddleware(0.1)),
changed = Remo.remote(t.number),
}),
})

return remotes.${name}
`,
[`server/${Name}Service.luau`]: `-- Owns every player's ${name} state. Clients ask; this service decides.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local ${Name}Network = require(ReplicatedStorage.Shared.features.${name}.${Name}Network)
local RateLimit = require(ServerScriptService.network.RateLimit)
local Serve = require(ServerScriptService.network.Serve)

local ${Name}Service = {}

Expand All @@ -51,8 +61,8 @@ end

-- Main.server.luau calls this once, after every service has loaded.
function ${Name}Service.start()
-- Every client-to-server event needs a rate limit, or the check fails.
Serve.event(${Name}Network.request, { limit = RateLimit.perSecond(10) }, function(player)
-- The remote's throttle drops floods; the service still checks each request.
${Name}Network.request:connect(function(player)
${Name}Network.changed:fire(player, ${Name}Service.getValue(player.UserId))
end)
end
Expand Down
12 changes: 2 additions & 10 deletions cli/template/luau/base/scripts/unguarded-remotes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import { test } from "node:test";
import { unguardedRemotes } from "./unguarded-remotes.ts";

test("flags OnServerEvent and OnServerInvoke outside Serve.luau", () => {
test("flags OnServerEvent and OnServerInvoke", () => {
const problems = unguardedRemotes(
new Map([
[
Expand All @@ -17,7 +17,7 @@ test("flags OnServerEvent and OnServerInvoke outside Serve.luau", () => {
assert.match(problems[1] ?? "", /^src\/features\/shop\/server\/Prices\.luau:1: OnServerInvoke/);
});

test("isn't fooled by indexing, spacing, strings holding -- or a folder named like Serve's", () => {
test("isn't fooled by indexing, spacing or strings holding --", () => {
const evasions = [
'remote["OnServerEvent"]:Connect(handler)',
"remote. OnServerEvent:Connect(handler)",
Expand All @@ -28,14 +28,6 @@ test("isn't fooled by indexing, spacing, strings holding -- or a folder named li
for (const code of evasions) {
assert.equal(unguardedRemotes(new Map([["src/features/shop/server/ShopService.luau", code]])).length, 1, code);
}
const lookalike = "src/features/network/server/NetworkService.luau";
assert.equal(unguardedRemotes(new Map([[lookalike, "remote.OnServerEvent:Connect(handler)"]])).length, 1);
});

test("lets Serve.luau take remotes", () => {
const serve = "remote.instance.OnServerEvent:Connect(handler)\nremote.instance.OnServerInvoke = handler";
assert.deepEqual(unguardedRemotes(new Map([["src/network/server/Serve.luau", serve]])), []);
assert.deepEqual(unguardedRemotes(new Map([["projects/common/src/network/server/Serve.luau", serve]])), []);
});

test("ignores remotes named in comments", () => {
Expand Down
Loading