From 72cefb773b66687ce62425d90b691f16d88aed6d Mon Sep 17 00:00:00 2001 From: acamilo Date: Wed, 23 Sep 2026 14:20:02 +0000 Subject: [PATCH] session-types: legacy-rate-role-id-v1, the identity on the Id grammar, pinned in both languages --- packages/session-types/src/gameboy.ts | 46 ++- packages/session-types/tests/gameboy.test.ts | 21 ++ .../fixtures/gameboy-rate-roles.json | 332 ++++++++++++++++++ .../crates/fly-session-types/src/gameboy.rs | 54 +++ .../fly-session-types/tests/gameboy_legacy.rs | 41 +++ 5 files changed, 491 insertions(+), 3 deletions(-) create mode 100644 services/flysim/crates/fly-session-types/fixtures/gameboy-rate-roles.json diff --git a/packages/session-types/src/gameboy.ts b/packages/session-types/src/gameboy.ts index 261e708..ec44ba5 100644 --- a/packages/session-types/src/gameboy.ts +++ b/packages/session-types/src/gameboy.ts @@ -14,8 +14,8 @@ import { digestOf, fail } from './canonical'; import { readRational, readSchemaRef } from './common'; import { Reader, readArtifactRef, requireUnique, u64 } from './reader'; import type { ArtifactRef, Digest, Id, RationalNs, SchemaRef, TypedValue } from './scalar'; -import { isDigest } from './scalar'; -import { type AssetRef, readAssetRef } from './workers'; +import { isDigest, isId } from './scalar'; +import { type AssetRef, MAX_RATE_ROLES, readAssetRef } from './workers'; import { MAX_SLOTS, ROLLBACK_POLICY, SLOTS_CAPABILITY } from './extensions'; export const PROFILE_ID = 'gameboy-legacy-fafb-v783-v1'; @@ -93,7 +93,10 @@ export const PAYLOAD_SCHEMAS: readonly SchemaRef[] = [ /** The legacy profile document's AssetRef digest: SHA-256 of its canonical JSON. */ export const PROFILE_DIGEST = '41e5d1ac62ab23f1b2d7252d52faac08b269c85e6b4c9ed7a370c74032c60878'; -/** `ChannelName`: a decoder channel or rate-role name. Not an Id: legacy names carry '_'. */ +/** + * `ChannelName`: a decoder channel or rate-role name, `^[a-z][a-z0-9_]{0,63}$`. A subset of the + * `Id` grammar (which admits '_'); see `rateRoleId` below (amendment 2026-09-23). + */ export function isChannelName(value: unknown): value is string { return typeof value === 'string' && /^[a-z][a-z0-9_]{0,63}$/.test(value); } @@ -558,3 +561,40 @@ export function decoderConfigForm(config: DecoderConfigLike): unknown { export function decoderConfigDigest(config: DecoderConfigLike): Digest { return digestOf(decoderConfigForm(config)); } + +// Rate roles: legacy-rate-role-id-v1 (amendment 2026-09-23, AGENT-01) ---------------------- + +/** + * The mapping from a legacy rate-role name to `AgentGraph.rateRoles` / `rates[].roleId`. + * + * The contract left it open on the premise that `command_0`, `macro_go_item` and the rest are + * not `Id`s; they are, because the `Id` grammar `^[a-z0-9][a-z0-9._-]{0,63}$` admits '_'. So the + * mapping is the identity on the `Id` grammar and a name outside it has no roleId: the agent + * refuses such a dataset. `fly-session-types/src/gameboy.rs` is the twin and + * `fixtures/gameboy-rate-roles.json` holds both to it. + */ +export const RATE_ROLE_MAPPING = 'legacy-rate-role-id-v1'; + +/** The roleId of a legacy rate-role name: the name itself, when it is an Id. */ +export function rateRoleId(name: string): Id { + if (!isId(name)) { + fail(`${RATE_ROLE_MAPPING}: rate role ${JSON.stringify(name)} is not an Id and has no roleId`); + } + return name; +} + +/** The legacy rate-role name a roleId stands for: the inverse of `rateRoleId`. */ +export function rateRoleName(roleId: string): string { + if (!isId(roleId)) fail(`${RATE_ROLE_MAPPING}: ${JSON.stringify(roleId)} is not a roleId`); + return roleId; +} + +/** Maps a tracked-role list in order, holding it to `AgentGraph.rateRoles`' bounds. */ +export function rateRoleIds(names: readonly string[]): Id[] { + const ids = names.map(rateRoleId); + if (ids.length > MAX_RATE_ROLES) { + fail(`${RATE_ROLE_MAPPING}: ${ids.length} tracked rate roles; AgentGraph.rateRoles holds at most ${MAX_RATE_ROLES}`); + } + requireUnique(ids, 'rateRoles'); + return ids; +} diff --git a/packages/session-types/tests/gameboy.test.ts b/packages/session-types/tests/gameboy.test.ts index b92ebd9..a5cadea 100644 --- a/packages/session-types/tests/gameboy.test.ts +++ b/packages/session-types/tests/gameboy.test.ts @@ -174,3 +174,24 @@ test('the decoderConfigDigest vectors are what the TypeScript oracle preset comp assert.equal(example.decoderConfigDigest, cases[1]!.digest); assert.deepEqual(example.executor.macroChannels, cases[1]!.macroChannels); }); + +test('legacy-rate-role-id-v1 is the identity on the Id grammar and refuses the rest', () => { + const file = fixtures.load('gameboy-rate-roles.json') as Record; + assert.equal(file.mapping, gameboy.RATE_ROLE_MAPPING); + assert.ok(file.accepted.length > 0); + for (const { name, roleId } of file.accepted as { name: string; roleId: string }[]) { + assert.equal(gameboy.rateRoleId(name), roleId, name); + assert.equal(gameboy.rateRoleName(roleId), name, `${roleId} maps back`); + } + for (const { name } of file.refused as { name: string }[]) { + assert.throws(() => gameboy.rateRoleId(name), `${JSON.stringify(name)} has no roleId`); + assert.throws(() => gameboy.rateRoleName(name)); + } + const tracked = file.fafbTrackedRoles as string[]; + assert.deepEqual(gameboy.rateRoleIds(tracked), tracked); + for (const role of tracked.filter((r) => r.startsWith('macro_'))) { + assert.ok(gameboy.isChannelName(role)); + } + assert.throws(() => gameboy.rateRoleIds(Array.from({ length: 65 }, (_, i) => `role_${i}`))); + assert.throws(() => gameboy.rateRoleIds(['command_0', 'command_0'])); +}); diff --git a/services/flysim/crates/fly-session-types/fixtures/gameboy-rate-roles.json b/services/flysim/crates/fly-session-types/fixtures/gameboy-rate-roles.json new file mode 100644 index 0000000..b562ecb --- /dev/null +++ b/services/flysim/crates/fly-session-types/fixtures/gameboy-rate-roles.json @@ -0,0 +1,332 @@ +{ + "description": "legacy-rate-role-id-v1 (legacy-gameboy-v1 section 13, amendment 2026-09-23, AGENT-01): the roleId of a legacy rate-role name is the name itself when it matches the session Id grammar, and a name outside it has no roleId. fafbTrackedRoles is the rate-role list the kernel tracks on data/fafb-v783 in network order (AgentGraph.rateRoles order); fly-session's gated test recomputes it from the committed dataset. Both languages must map every accepted name to itself and back, and refuse every refused name.", + "mapping": "legacy-rate-role-id-v1", + "idPattern": "^[a-z0-9][a-z0-9._-]{0,63}$", + "fafbTrackedRoles": [ + "backward", + "command_0", + "command_1", + "command_2", + "command_3", + "command_4", + "command_5", + "command_6", + "command_7", + "forward", + "proboscis", + "reward_pam", + "steer_left", + "steer_right", + "macro_go_objective", + "macro_go_out", + "macro_go_warp", + "macro_go_route", + "macro_go_item", + "macro_go_npc", + "macro_go_frontier", + "macro_go_shop", + "macro_go_heal", + "macro_talk", + "macro_menu", + "macro_next", + "macro_yes", + "macro_no", + "macro_close", + "macro_confirm", + "macro_back", + "macro_move_1", + "macro_move_2", + "macro_move_3", + "macro_move_4", + "macro_switch", + "macro_item", + "macro_throw_ball", + "macro_run", + "macro_buy_potion", + "macro_buy_ball", + "macro_buy_antidote", + "macro_buy_repel", + "macro_heal", + "macro_leave" + ], + "accepted": [ + { + "name": "backward", + "roleId": "backward" + }, + { + "name": "command_0", + "roleId": "command_0" + }, + { + "name": "command_1", + "roleId": "command_1" + }, + { + "name": "command_2", + "roleId": "command_2" + }, + { + "name": "command_3", + "roleId": "command_3" + }, + { + "name": "command_4", + "roleId": "command_4" + }, + { + "name": "command_5", + "roleId": "command_5" + }, + { + "name": "command_6", + "roleId": "command_6" + }, + { + "name": "command_7", + "roleId": "command_7" + }, + { + "name": "forward", + "roleId": "forward" + }, + { + "name": "proboscis", + "roleId": "proboscis" + }, + { + "name": "reward_pam", + "roleId": "reward_pam" + }, + { + "name": "steer_left", + "roleId": "steer_left" + }, + { + "name": "steer_right", + "roleId": "steer_right" + }, + { + "name": "macro_go_objective", + "roleId": "macro_go_objective" + }, + { + "name": "macro_go_out", + "roleId": "macro_go_out" + }, + { + "name": "macro_go_warp", + "roleId": "macro_go_warp" + }, + { + "name": "macro_go_route", + "roleId": "macro_go_route" + }, + { + "name": "macro_go_item", + "roleId": "macro_go_item" + }, + { + "name": "macro_go_npc", + "roleId": "macro_go_npc" + }, + { + "name": "macro_go_frontier", + "roleId": "macro_go_frontier" + }, + { + "name": "macro_go_shop", + "roleId": "macro_go_shop" + }, + { + "name": "macro_go_heal", + "roleId": "macro_go_heal" + }, + { + "name": "macro_talk", + "roleId": "macro_talk" + }, + { + "name": "macro_menu", + "roleId": "macro_menu" + }, + { + "name": "macro_next", + "roleId": "macro_next" + }, + { + "name": "macro_yes", + "roleId": "macro_yes" + }, + { + "name": "macro_no", + "roleId": "macro_no" + }, + { + "name": "macro_close", + "roleId": "macro_close" + }, + { + "name": "macro_confirm", + "roleId": "macro_confirm" + }, + { + "name": "macro_back", + "roleId": "macro_back" + }, + { + "name": "macro_move_1", + "roleId": "macro_move_1" + }, + { + "name": "macro_move_2", + "roleId": "macro_move_2" + }, + { + "name": "macro_move_3", + "roleId": "macro_move_3" + }, + { + "name": "macro_move_4", + "roleId": "macro_move_4" + }, + { + "name": "macro_switch", + "roleId": "macro_switch" + }, + { + "name": "macro_item", + "roleId": "macro_item" + }, + { + "name": "macro_throw_ball", + "roleId": "macro_throw_ball" + }, + { + "name": "macro_run", + "roleId": "macro_run" + }, + { + "name": "macro_buy_potion", + "roleId": "macro_buy_potion" + }, + { + "name": "macro_buy_ball", + "roleId": "macro_buy_ball" + }, + { + "name": "macro_buy_antidote", + "roleId": "macro_buy_antidote" + }, + { + "name": "macro_buy_repel", + "roleId": "macro_buy_repel" + }, + { + "name": "macro_heal", + "roleId": "macro_heal" + }, + { + "name": "macro_leave", + "roleId": "macro_leave" + }, + { + "name": "up", + "roleId": "up" + }, + { + "name": "down", + "roleId": "down" + }, + { + "name": "left", + "roleId": "left" + }, + { + "name": "right", + "roleId": "right" + }, + { + "name": "a", + "roleId": "a" + }, + { + "name": "b", + "roleId": "b" + }, + { + "name": "start", + "roleId": "start" + }, + { + "name": "select", + "roleId": "select" + }, + { + "name": "kenyon", + "roleId": "kenyon" + }, + { + "name": "mbon", + "roleId": "mbon" + }, + { + "name": "visual_l1", + "roleId": "visual_l1" + }, + { + "name": "descending", + "roleId": "descending" + }, + { + "name": "motor", + "roleId": "motor" + }, + { + "name": "sensory", + "roleId": "sensory" + }, + { + "name": "7th.role-x", + "roleId": "7th.role-x" + }, + { + "name": "0", + "roleId": "0" + }, + { + "name": "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", + "roleId": "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr" + } + ], + "refused": [ + { + "name": "Command_0", + "why": "uppercase" + }, + { + "name": "_command", + "why": "leading underscore" + }, + { + "name": "-x", + "why": "leading hyphen" + }, + { + "name": "", + "why": "empty" + }, + { + "name": "steer left", + "why": "space" + }, + { + "name": "macro_é", + "why": "non-ASCII" + }, + { + "name": "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", + "why": "65 characters" + } + ] +} diff --git a/services/flysim/crates/fly-session-types/src/gameboy.rs b/services/flysim/crates/fly-session-types/src/gameboy.rs index 29e88f8..c644612 100644 --- a/services/flysim/crates/fly-session-types/src/gameboy.rs +++ b/services/flysim/crates/fly-session-types/src/gameboy.rs @@ -413,6 +413,60 @@ pub fn extension_set_digest() -> String { canonical::digest_of(&extension_set()).expect("canonicalizable") } +// --------------------------------------------------------------------------------------------- +// Rate roles: `legacy-rate-role-id-v1` (amendment 2026-09-23, AGENT-01) + +/// The name of the mapping from a legacy rate-role name to `AgentGraph.rateRoles` / +/// `AgentTelemetry.rates[].roleId`. +/// +/// `legacy-gameboy-v1` section 13 left this to AGENT-01 on the premise that the legacy names +/// (`command_0`, `macro_go_item`, `steer_left`, ...) are not `Id`s. They are: the session `Id` +/// grammar is `^[a-z0-9][a-z0-9._-]{0,63}$` (`ipc-v1` section 1), which admits `_`, and every +/// role `data/fafb-v783` declares or the kernel tracks matches it. So the mapping is the identity +/// on the `Id` grammar, trivially reversible, and a name outside the grammar is not mapped at +/// all: the agent refuses to initialize on such a dataset rather than invent an encoding that +/// no consumer could read back. `fixtures/gameboy-rate-roles.json` pins it in both languages. +pub const RATE_ROLE_MAPPING: &str = "legacy-rate-role-id-v1"; + +/// The `roleId` a legacy rate-role name is published under: the name itself, when it is an +/// `Id`. Anything else is refused. +pub fn rate_role_id(name: &str) -> Result { + if is_id(name) { + Ok(name.to_owned()) + } else { + err(format!( + "{RATE_ROLE_MAPPING}: rate role {name:?} is not an Id (^[a-z0-9][a-z0-9._-]{{0,63}}$) and has no roleId" + )) + } +} + +/// The legacy rate-role name a `roleId` stands for: the inverse of [`rate_role_id`]. +pub fn rate_role_name(role_id: &str) -> Result { + if is_id(role_id) { + Ok(role_id.to_owned()) + } else { + err(format!("{RATE_ROLE_MAPPING}: {role_id:?} is not a roleId")) + } +} + +/// Maps a tracked-role list, in order, and holds it to `AgentGraph.rateRoles`' own bounds: +/// at most 64 (`MAX_RATE_ROLES`, the kernel's role bitmask) and unique. +pub fn rate_role_ids<'a>(names: impl IntoIterator) -> Result> { + let ids = names + .into_iter() + .map(rate_role_id) + .collect::>>()?; + if ids.len() > crate::workers::MAX_RATE_ROLES { + return err(format!( + "{RATE_ROLE_MAPPING}: {} tracked rate roles; AgentGraph.rateRoles holds at most {}", + ids.len(), + crate::workers::MAX_RATE_ROLES + )); + } + require_unique(ids.iter().map(String::as_str), "rateRoles")?; + Ok(ids) +} + // --------------------------------------------------------------------------------------------- // Scalars and helpers diff --git a/services/flysim/crates/fly-session-types/tests/gameboy_legacy.rs b/services/flysim/crates/fly-session-types/tests/gameboy_legacy.rs index c305f29..659fee7 100644 --- a/services/flysim/crates/fly-session-types/tests/gameboy_legacy.rs +++ b/services/flysim/crates/fly-session-types/tests/gameboy_legacy.rs @@ -348,3 +348,44 @@ fn the_extension_methods_check_their_scope() { assert_eq!(SLOTS_CAPABILITY, "gameboy-slots-v1"); assert_eq!(ROLLBACK_CAPABILITY, ROLLBACK_POLICY); } + +/// `legacy-rate-role-id-v1`: every accepted name is its own roleId and maps back; every refused +/// name has none. The TypeScript twin reads the same file. +#[test] +fn the_rate_role_mapping_is_the_identity_on_the_id_grammar_and_refuses_the_rest() { + let file = fixtures::load("gameboy-rate-roles.json").expect("gameboy-rate-roles.json"); + assert_eq!(file["mapping"].as_str(), Some(gameboy::RATE_ROLE_MAPPING)); + let accepted = file["accepted"].as_array().expect("accepted"); + assert!(!accepted.is_empty()); + for case in accepted { + let name = case["name"].as_str().expect("name"); + let role_id = case["roleId"].as_str().expect("roleId"); + assert_eq!(gameboy::rate_role_id(name).expect(name), role_id, "{name}"); + assert_eq!(gameboy::rate_role_name(role_id).expect(role_id), name, "{role_id} maps back"); + } + for case in file["refused"].as_array().expect("refused") { + let name = case["name"].as_str().expect("name"); + assert!(gameboy::rate_role_id(name).is_err(), "{name:?} must have no roleId"); + assert!(gameboy::rate_role_name(name).is_err(), "{name:?} is no roleId either"); + } + // The FAFB tracked list fits AgentGraph.rateRoles as it stands: <= 64, unique, in order. + let tracked: Vec<&str> = file["fafbTrackedRoles"] + .as_array() + .expect("fafbTrackedRoles") + .iter() + .map(|v| v.as_str().expect("a role name")) + .collect(); + let ids = gameboy::rate_role_ids(tracked.iter().copied()).expect("the FAFB roles map"); + assert_eq!(ids, tracked); + // Every decoder channel is a ChannelName, and every ChannelName is an Id, so a macro + // channel's role is its own roleId too. + for role in &tracked { + if role.starts_with("macro_") { + assert!(gameboy::is_channel_name(role)); + } + } + // The list bound and uniqueness are the contract's, not the mapping's convenience. + let many: Vec = (0..65).map(|i| format!("role_{i}")).collect(); + assert!(gameboy::rate_role_ids(many.iter().map(String::as_str)).is_err()); + assert!(gameboy::rate_role_ids(["command_0", "command_0"]).is_err()); +}