Skip to content

Commit 41307de

Browse files
phasenullclaude
andauthored
Experiments: pick your own variant in the dev menu, vi on experiment rows, 0.7.1 (#19)
- Server > Experiments (new sub-tab) and palette commands "Experiment <name>: use <variant>" / "clear my variant": an owner or dev picks a variant of an analytics experiment for themselves, sees which one they're in and why (random, forced by the settings, their pick), and clears it. Same rule as Modules > State (devs where the dev-only tools are open, owners everywhere), re-checked by the server ops (exp.list, exp.set: one change a second) and by the analytics engine through a gate the dev menu registers (analytics/dev-control.ts); the gate also requires a dev. - The pick is stored in the analytics DataStore (dexp/<UserId>, read at join only for players the gate allows), wins over the signed settings, is stamped forced on a new experiment row (also when it equals the random variant), and re-sends the player's hello so the client side agrees. A variant the game didn't pass is refused; a stored pick is ignored while the gate refuses the player. - Experiment rows carry vi (the variant's 1-based position in the game's list) so the backend can check the split. - Lune: test-analytics-server (stored pick at join, set / clear, forced rows, vi, gate closed), test-dev-access (exp.list / exp.set and the engine gate per server kind and role). Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5e3b8e6 commit 41307de

12 files changed

Lines changed: 561 additions & 10 deletions

‎README.md‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,20 @@ return the generation's stop function, which the kernel calls on a swap.
192192
close it, dock all windows; Reload, Roll back, switch branch, load a build here, join a server on a branch or build
193193
(the Branch tab's rules and words, `devtools/build-actions.ts`), New server and Join a listed server (owners); find a
194194
player by name, display name or UserId and open them in Manage > Players or their Kick / Ban card (owners); copy the
195-
job id, artifact id, commit, branch or place version (the copy popup); "Ask Claude: ..." puts the query in the Claude
196-
draft. Anything that swaps what runs on the server takes a second Enter or tap to confirm. The kernel and the server
195+
job id, artifact id, commit, branch or place version (the copy popup); "Experiment <name>: use <variant>" / "clear my
196+
variant" (Server > Experiments, below); "Ask Claude: ..." puts the query in the Claude draft. Anything that swaps what runs on the server takes a second Enter or tap to confirm. The kernel and the server
197197
re-check every request. Core: `devtools/commands.ts` (pure, Lune-tested by `scripts/test-commands.luau`).
198+
- **Server > Experiments (framework 0.7.1): your own variant.** Every analytics experiment the game asked about for
199+
you, the variant you're in and why (random, forced by the settings, or your pick), a button per variant to pick it
200+
for yourself, and Clear; the palette has the same as "Experiment <name>: use <variant>". Devs where the dev-only
201+
tools are open to them (dev rules, or owners on a server an owner switched), owners everywhere: the same rule as
202+
Modules > State, re-checked by the server and by the analytics engine (`analytics/dev-control.ts`). It only ever
203+
changes your own variant, at most one change a second. The pick is stored in the analytics DataStore
204+
(`dexp/<UserId>`, read at join only for players the rule allows), so it follows you to every server; it wins over the
205+
signed settings (stop, settle, weights, `players`), is stamped `forced` on the experiment row (the backend leaves
206+
forced players out of the results), and the game sees it the next time it asks for your variant, usually your next
207+
join. A pick the game's variants don't have is refused. While the rule doesn't allow you (a dev on a prod server,
208+
a revoked role) a stored pick is ignored.
198209
- **Server TPS (framework 0.4.2, kernel 0.4.2):** Server > Status has a *TPS* line next to Memory and Lua heap: the
199210
last minute's average, its slowest second and the physics FPS (`59.9 avg, 52.0 min, physics 60`; the warning colour
200211
under 50 on average). Older kernels show `needs kernel 0.4.2`. The same numbers ride on fleet heartbeats (the
@@ -574,7 +585,9 @@ const variant = analytics.experiment("onboarding", ["short", "long"]); // same a
574585
The settings turn one off (`active: false`: everyone gets the first variant), sets `weights` or forces a
575586
`variant`, live. `players` (framework 0.5.3+) forces a variant for single players, keyed by UserId or pid, over
576587
everything else (QA, support); a client only ever gets its own entry. The CLI writes these with `typetorch
577-
experiment weights | settle | stop | resume | clear | force | unforce`.
588+
experiment weights | settle | stop | resume | clear | force | unforce` (the explorer's Experiments page prepares the
589+
command for owners). A dev picks their own variant in the dev menu (Server > Experiments). The experiment row carries
590+
`vi` (framework 0.7.1+), the variant's position in the game's list, so the backend can check the split.
578591
- **First-ever session:** for new players (a share of them, `recordShare`), the client records character and camera
579592
about 10 times a second, every input (never while a TextBox or the chat has focus; text boxes only say which box was
580593
used), buttons pressed and hovered, screens, prompts and deaths, from the join until `recordSeconds` (the settings,

‎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.0",
3+
"version": "0.7.1",
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-analytics-server.luau‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@ for page = 1, 12 do
482482
friendLists[2][page] = if page == 11 then { 1 } else { 200 + page }
483483
end
484484

485+
-- Dev overrides (dev-control.ts): the dev menu's gate allows the regular (UserId 2) only; they stored a pick before.
486+
local DC = loadAnalytics("dev-control")
487+
local devAllowed = { [regular] = true }
488+
local removeGate = DC.registerDevGate(function(player)
489+
return devAllowed[player] == true
490+
end)
491+
records["dexp/2"] = { stored = "b", ["bad name!"] = "x", toolong = string.rep("v", 40) }
492+
485493
local options = {
486494
settings = {
487495
backend = "duckdb",
@@ -676,6 +684,63 @@ local qaRow = rowsNamed(store().events.rows, "experiment", "qa")[1]
676684
check("per-player: row forced", qaRow and serde.decode("json", qaRow.props).forced == true, qaRow and qaRow.props)
677685
check("per-player: others keep the override", core1:experiment(newbie, "qa", { "a", "b" }) == "a" and #rowsNamed(store().events.rows, "experiment", "qa") == 1)
678686

687+
-- Dev overrides: the stored pick applies at join (bad entries dropped), forced, with the variant's position (vi).
688+
check("dev: stored pick read at join", hiReg.exps.stored ~= nil and hiReg.exps.stored.player == "b" and hiReg.exps["bad name!"] == nil and hiReg.exps.toolong == nil)
689+
check("dev: not for others", hiNew.exps.stored == nil)
690+
check("dev: stored pick wins", core1:experiment(regular, "stored", { "a", "b" }) == "b")
691+
local storedRow = rowsNamed(store().events.rows, "experiment", "stored")[1]
692+
local storedProps = storedRow and serde.decode("json", storedRow.props) or {}
693+
check("dev: row forced with vi", storedProps.forced == true and storedProps.vi == 2 and storedProps.variants == 2, storedRow and storedRow.props)
694+
local onboardingRow = rowsNamed(store().events.rows, "experiment", "onboarding")[1]
695+
check("experiment row: vi of the random variant", serde.decode("json", onboardingRow.props).vi == (if variant == "short" then 1 else 2), onboardingRow.props)
696+
local control = DC.devExperiments()
697+
check("dev: the engine registered", control ~= nil)
698+
local refused = pcall(control.set, newbie, "onboarding", "long")
699+
check("dev: a player the gate refuses can't pick", not refused)
700+
-- The regular picks the other onboarding variant than the hash gives them.
701+
local regularRandom = core1:experiment(regular, "onboarding", { "short", "long" })
702+
local other = if regularRandom == "short" then "long" else "short"
703+
local rowsBefore = #rowsNamed(store().events.rows, "experiment", "onboarding")
704+
local sentBefore = #sent
705+
local reply = control.set(regular, "onboarding", other)
706+
local entry
707+
for _, item in reply.experiments do
708+
if item.name == "onboarding" then
709+
entry = item
710+
end
711+
end
712+
check("dev: set answers the list", entry ~= nil and entry.variant == other and entry.how == "dev" and entry.override == other and reply.saved == true, entry and entry.how)
713+
check("dev: saved to the store", records["dexp/2"].onboarding == other and records["dexp/2"].stored == "b" and records["dexp/2"]["bad name!"] == nil)
714+
local pickRows = rowsNamed(store().events.rows, "experiment", "onboarding")
715+
local pickProps = serde.decode("json", pickRows[#pickRows].props)
716+
check("dev: a new forced row", #pickRows == rowsBefore + 1 and pickProps.variant == other and pickProps.forced == true)
717+
check("dev: experiment() gives the pick", core1:experiment(regular, "onboarding", { "short", "long" }) == other)
718+
local resent = false
719+
for index = sentBefore + 1, #sent do
720+
if sent[index].player == regular and sent[index].args[1] == "hi" and sent[index].args[2].exps.onboarding.player == other then
721+
resent = true
722+
end
723+
end
724+
check("dev: hello sent again with the pick", resent)
725+
check("dev: unknown variant refused", not pcall(control.set, regular, "onboarding", "nope"))
726+
check("dev: undeclared experiment refused", not pcall(control.set, regular, "never_asked", "a"))
727+
-- Picking the variant you already have still marks you forced (left out of the results).
728+
local colorRandom = core1:experiment(regular, "color", { "red", "blue" })
729+
control.set(regular, "color", colorRandom)
730+
local colorRows = rowsNamed(store().events.rows, "experiment", "color")
731+
check("dev: same variant, now forced", #colorRows == 2 and serde.decode("json", colorRows[2].props).forced == true)
732+
-- Clear: back to the hash, a row without forced.
733+
control.set(regular, "onboarding", nil)
734+
check("dev: cleared in the store", records["dexp/2"].onboarding == nil and records["dexp/2"].color == colorRandom)
735+
check("dev: cleared gives the random variant", core1:experiment(regular, "onboarding", { "short", "long" }) == regularRandom)
736+
local clearRows = rowsNamed(store().events.rows, "experiment", "onboarding")
737+
check("dev: cleared row not forced", serde.decode("json", clearRows[#clearRows].props).forced == nil)
738+
-- The gate closes (role revoked): the stored pick is ignored.
739+
devAllowed[regular] = nil
740+
check("dev: gate closed, pick ignored", core1:experiment(regular, "stored", { "a", "b" }) == E.assignVariant(records["p/2"].pid, "stored", { "a", "b" }).variant)
741+
devAllowed[regular] = true
742+
check("dev: gate open again", core1:experiment(regular, "stored", { "a", "b" }) == "b")
743+
679744
-- Client events: valid, server-only kinds (session, purchase, currency), bad JSON, a screen change.
680745
raw["__tt/an"](newbie, "ev", {
681746
{ 100, "custom", "opened_map", "{}" },
@@ -937,6 +1002,9 @@ do
9371002
check("settings: the engine's listener goes with its trove", #settingsMock.listeners == 0)
9381003
end
9391004

1005+
removeGate()
1006+
check("dev: no gate, nobody may", not DC.devOverrideAllowed(regular))
1007+
9401008
print(`analytics server: {passed} passed, {failed} failed`)
9411009
if failed > 0 then
9421010
process.exit(1)

‎scripts/test-dev-access.luau‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,30 @@ stubs.ab = {
637637
NEEDS_KERNEL_AB = "kernel 0.2.3 needed",
638638
}
639639
stubs.status = { analyticsStatus = function() end } -- analytics/status
640+
-- analytics/dev-control: the gate server.luau registers, and a stub engine that answers who asked.
641+
local devGate
642+
local devSets = {}
643+
stubs["dev-control"] = {
644+
registerDevGate = function(fn)
645+
devGate = fn
646+
return function()
647+
if devGate == fn then
648+
devGate = nil
649+
end
650+
end
651+
end,
652+
devExperiments = function()
653+
return {
654+
list = function()
655+
return { { name = "onboarding", variants = { "a", "b" }, variant = "a", active = true, how = "random" } }
656+
end,
657+
set = function(target, name, variant)
658+
table.insert(devSets, { target = target, name = name, variant = variant })
659+
return { experiments = {}, saved = true }
660+
end,
661+
}
662+
end,
663+
}
640664
stubs.sync = { -- assets/sync
641665
assetFacts = function() end,
642666
assetReport = function()
@@ -772,7 +796,20 @@ for _, case in SERVER_CASES do
772796
ok, reply = request(target, "logs.upload", { kind = "server" })
773797
local code = if ok then reply.error else reply
774798
check(`{label}: logs.upload, {name}`, if expect ~= nil then code == expect else code == "not_connected", code)
799+
-- Server > Experiments (your own variant): like Modules > State, owners everywhere, other devs where the tools are open.
800+
ok, reply = request(target, "exp.list")
801+
check(`{label}: exp.list, {name}`, if stateOpen then (ok and reply.supported == true and reply.experiments[1].name == "onboarding") else (not ok and reply == expect), reply)
802+
check(`{label}: the engine's gate agrees, {name}`, devGate ~= nil and devGate(target) == stateOpen)
803+
local setsBefore = #devSets
804+
ok, reply = request(target, "exp.set", { name = "onboarding", variant = "b" })
805+
check(`{label}: exp.set, {name}`, if stateOpen then (ok and reply.saved == true and #devSets == setsBefore + 1 and devSets[#devSets].target == target) else (not ok and #devSets == setsBefore), reply)
806+
if stateOpen then
807+
ok, reply = request(target, "exp.set", { name = "onboarding" })
808+
check(`{label}: exp.set twice in a second is refused, {name}`, not ok and reply == "rate_limited", reply)
809+
end
775810
end
811+
check(`{label}: a stranger may not hold an override`, not devGate(stranger))
812+
check(`{label}: exp.set checks its payload`, not (request(owner, "exp.set", { name = 5 })))
776813
local ok, reply = request(stranger, "access")
777814
check(`{label}: not a dev`, ok == false and reply == "not a dev", reply)
778815
stop()

‎src/analytics/SCHEMA.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ First-ever-session detail, one row per packed chunk.
7676
| zone | `leave` | `zone`, `to?`, `secs` |
7777
| state | `activity` | `to`, `from` (server or client src) |
7878
| state | `screen` | `to`, `from` (`""` = none). Client: ScreenGuis in PlayerGui (Enabled), GuiObjects tagged `TTScreen` (Visible), `screen()` |
79-
| experiment | `<experiment>` | `variant`, `variants` (count), `forced?`. Once per session when assigned or changed. At most 32 experiments per session: a 33rd `experiment()` returns the control (the first variant) and warns. A client's experiment calls count against its event budget (below) |
79+
| experiment | `<experiment>` | `variant`, `variants` (count), `vi` (the variant's 1-based position in the game's list; 0.7.1+), `forced?` (the settings or the player's own dev pick chose it). Once per session when assigned or changed, and again when the same variant becomes or stops being forced. At most 32 experiments per session: a 33rd `experiment()` returns the control (the first variant) and warns. A client's experiment calls count against its event budget (below) |
8080
| recording_meta | `start` | `share` (the recorded share then), `secs?` (the window then: `recordSeconds`; frameworks before it leave it out and used 60) |
8181
| recording_meta | `end` | `chunks`, `bytes`, `why`: `window` (`recordSeconds`, default 60 s, after the first input), `cap` (10 min, or the recording's 256 KB / 80 chunks), `left`, `end` |
8282
| fleet | `heartbeat` | The kernel's `fleetStatus()` (kernel 0.3.2+), every 60 ± 10 s and once at each generation start. pid `""` |

‎src/analytics/dev-control.ts‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Per-player dev overrides of analytics experiments (the dev menu's Server > Experiments page and its palette commands):
3+
* an owner or dev picks a variant of an experiment for themselves, sees which one they're in, and clears it.
4+
*
5+
* Who may: the dev menu decides (devtools/server.ts registers the gate: devs where the dev-only tools are open to them,
6+
* owners everywhere, like Modules > State). Without a registered gate (no dev menu in this generation) nobody may, and a
7+
* stored override is not applied. It only ever changes the player's own variant.
8+
*
9+
* Where: the analytics DataStore, key `dexp/<UserId>` = { [experiment]: variant }, read once per join and only for
10+
* players the gate allows (so other players cost nothing), and written on each change, so the override follows the
11+
* dev to every server and session. It wins over the signed settings (stop, settle, weights, `players`), is stamped
12+
* `forced` on the experiment row (the backend leaves forced players out of the results), and the game sees it the next
13+
* time it asks for the variant (usually the next join).
14+
*
15+
* The running engine registers its half here (like analytics/status.ts), so devtools never imports the engine.
16+
*/
17+
18+
/** Overrides one player may hold (one per experiment). */
19+
export const DEV_OVERRIDES_MAX = 32;
20+
/** The DataStore key prefix (in the analytics store, next to `p/<UserId>`). */
21+
export const DEV_OVERRIDE_PREFIX = "dexp/";
22+
23+
/** One experiment as the dev menu shows it to the player. */
24+
export interface DevExperiment {
25+
name: string;
26+
/** The game's variants, once the game asked for this player's variant (the first is the control). */
27+
variants?: string[];
28+
/** The variant the player gets now (undefined while the game hasn't asked yet). */
29+
variant?: string;
30+
/** false: the experiment is stopped (everyone gets the control, not stamped). */
31+
active: boolean;
32+
/** "dev": the player's own override; "settings": the signed settings forced it; "random": the hash and the weights. */
33+
how?: "dev" | "settings" | "random";
34+
/** The player's own override, if any (may name a variant the game doesn't have: then it is ignored). */
35+
override?: string;
36+
}
37+
38+
export interface DevExperimentsReply {
39+
experiments: DevExperiment[];
40+
/** The override was written to the DataStore (false: it holds for this server only until it saves). */
41+
saved?: boolean;
42+
}
43+
44+
/** What the running engine offers the dev menu. */
45+
export interface DevExperimentControl {
46+
readonly list: (player: Player) => DevExperiment[];
47+
/** Sets (variant) or clears (undefined) the player's override; throws a short message when it can't. */
48+
readonly set: (player: Player, name: string, variant: string | undefined) => DevExperimentsReply;
49+
}
50+
51+
let control: DevExperimentControl | undefined;
52+
let gate: ((player: Player) => boolean) | undefined;
53+
54+
/** The engine registers itself; the returned function removes it again (only if it is still the registered one). */
55+
export function registerDevExperiments(value: DevExperimentControl): () => void {
56+
control = value;
57+
return () => {
58+
if (control === value) control = undefined;
59+
};
60+
}
61+
62+
/** The running engine's control, or undefined when none runs in this generation. */
63+
export function devExperiments(): DevExperimentControl | undefined {
64+
return control;
65+
}
66+
67+
/** The dev menu registers who may hold an override; the returned function removes it. */
68+
export function registerDevGate(value: (player: Player) => boolean): () => void {
69+
gate = value;
70+
return () => {
71+
if (gate === value) gate = undefined;
72+
};
73+
}
74+
75+
/** Whether `player` may hold a dev override here (false without a gate; a failing gate counts as no). */
76+
export function devOverrideAllowed(player: Player): boolean {
77+
const check = gate;
78+
if (check === undefined) return false;
79+
const [ok, allowed] = pcall(() => check(player));
80+
return ok && allowed === true;
81+
}
82+
83+
/** A variant name as the framework takes it (isVariantList: 1-32 characters). */
84+
export function isVariantName(value: unknown): value is string {
85+
return typeIs(value, "string") && value.size() >= 1 && value.size() <= 32;
86+
}
87+
88+
/** A stored record cleaned: experiment name -> variant, at most DEV_OVERRIDES_MAX, bad entries dropped. */
89+
export function cleanDevOverrides(raw: unknown, isName: (value: unknown) => boolean): Map<string, string> {
90+
const result = new Map<string, string>();
91+
if (!typeIs(raw, "table")) return result;
92+
for (const [name, variant] of pairs(raw as object)) {
93+
if (result.size() >= DEV_OVERRIDES_MAX) break;
94+
if (isName(name) && isVariantName(variant)) result.set(name as string, variant);
95+
}
96+
return result;
97+
}

0 commit comments

Comments
 (0)