Repository navigation
feat(variables): add a random operation to updateVariable - #391
Merged
Merged
Conversation
An updateVariable operation with op: random sets its variable to a randomly drawn value: a uniform integer between inclusive bounds, or one of several weighted values. Operations run in list order, so later operations of the same action see the drawn value, and one action can hold any number of rolls. A random operation does not continue to the next line. A roll that a line makes into a context variable is recorded in the randomOutcomes ledger under the path of its operation, so rollback and save/load replay it without rerolling. A roll made from an interaction is recorded as the value it set. Bumps the version to 1.53.0.
…e cases Two robustness specs, both run in CI: - variable-random-line-rollback: a line rolls a number and a weighted text value. Back and Load show the recorded roll again without drawing, and entering the line again after rolling back past it draws a new roll. - variable-random-choice-branch: a choice click rolls a number and a conditional in the same click routes on it. Back draws nothing and a second click draws again. The VT harness accepts vtRandomUint32Sequence, a list of words drawn in order, and counts the draws in __vtRandomCalls, so a spec can tell a replayed roll from a new one. More engine tests: every word of a range, weighted proportions, a conditional reading the roll in the same batch, a roll inside a weighted random outcome, same-line re-entry, templates next to a roll, saves loaded into a changed project, saves with no recorded rolls, and 150 generated cases replayed across save, load and rollback.
Every random operation of an updateVariable action is now checked before the first one is drawn, including the variable's scope, so an invalid later roll no longer consumes a draw. Weighted random outcomes again report an unsupported key before a missing weight, as they did before the sampler was shared. More tests: several rolls with one invalid, a roll and a failing operation on a line, two rolls into one variable next to device and account rolls, confirm dialog actions, form submit actions, scene replay, and template syntax in a weighted value. The docs state the template rules.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Rolling a number today needs the
randomaction in its integer form. That form has rules that are awkward for authors: a line can hold only one, it always runs after everyupdateVariableoperation whatever order the line is written in, and it continues to the next line by itself. It also cannot write text or true/false values.A roll is "set this variable to something", which is what
updateVariableis for.What changed
An
updateVariableoperation can now beop: random:integerdraws a uniform integer between inclusive bounds into a number variable, with the same bounds rules as therandomaction.weightedstores the value of one entry, chosen in proportion to the weights, with the same weight rules as weightedrandomoutcomes. A value is a string, a finite number or a boolean and must match the variable's type.variableId,opanddistribution, all fixed authored values. An operation that an action template changes is refused, so a weighted text value cannot hold template syntax such as${...}. Every random operation of an action is checked before the first one is drawn.Rollback and save/load:
randomOutcomesledger, under the path of its operation (for exampleupdateVariable.operations.0), and is replayed without rerolling. The persisted format and its version are unchanged.randomaction already has.The
randomaction is unchanged. The weighted sampler now shares its weight validation and selection with the new weighted values.The version is bumped to 1.53.0.
Testing
Engine tests:
spec/RouteEngine.updateVariableRandom.test.js(32 tests):randomoutcome.spec/RouteEngine.form.test.jscovers a roll in a form's submit actions, andspec/RouteEngine.sceneReplay.test.jscovers rolls during a scene replay (context allowed, account refused).spec/random.test.jscovers sampling and validation of the two variable distributions, andspec/projectDataSchema.test.jscovers accepted and rejected operation shapes.bun run test,bun run lint,bun run check:test-markersandbun run buildpass.Browser (VT), two new robustness specs that run in CI:
variable-random-line-rollback: a line rolls a number, adds 10 to it and rolls a weighted text value. Pressing Back shows the same roll with no new draw. Rolling back past the line restores the defaults, and entering it again draws a new roll. Loading a save made after the first roll shows the first roll again with no new draw.variable-random-choice-branch: a choice click rolls a number and a conditional in the same click routes on it. Back returns to the choice with no new draw, and a second click draws again and routes the other way.main, both fail on their first roll assertion.vtRandomUint32Sequence, a list of words drawn in order, and counts draws in__vtRandomCalls, so a spec can tell a replayed roll from a new one. The existingrandom/choice-integer-branchspec still matches its reference.