Design Decisions and Why
Each section is a decision you’ll meet in the v2 code: what was decided, the reason the repository records for it (in commit messages, code comments, CONTRIBUTING.md or the docs), and what it costs today. Where the record gives no reason, the section says so; don’t invent one when you propose a change.
Rollback reverts statements one by one
Decided. Going back runs each statement’s action in reverse: revert() in src/engine/lifecycle.ts calls the previous statement’s willRevert(), revert() and didRevert(), and keeps going while didRevert() returns advance: true. Every action undoes itself, from what it recorded in history.
Why. The comment at the top of src/lib/Action.ts describes the reverting cycle as the counterpart of the application cycle, “used when the player goes back in the game”, with matching steps. It records no comparison with other designs.
Costs. Every action has to implement revert() correctly and record enough in history for it, and a mistake there only shows when a player goes back (stage 6 of the learning path). Nothing restores storage(): a variable changed by a plain function statement stays changed, and the engine refuses to revert a plain function at all, so games that need rollback over code use { 'Function': { 'Apply', 'Revert' } }.
The engine is one class with static members
Decided. Monogatari in src/monogatari.ts has only static members and is never instantiated. window.monogatari is the class.
Why. The record gives no reason. A comment on asEngine() explains the typing around it: the class is returned typed as VisualNovelEngine, which “enables type-safe access while allowing external users to extend the VisualNovelEngine interface via declaration merging”.
Costs. One engine per page, and its state lives as long as the page: tests can’t start a fresh engine without reloading, which is one reason v2’s tests are browser specs. In exchange, any method can be wrapped from the console and every internal call goes through it (Debugging the Engine).
The starter game is dist/, and the build output is committed
Decided. The folder people download is the repository’s dist/, and dist/engine/core/, dist/engine/debug/, dist/types/ and lib/ are committed. In 2022 commit c89fcbc4 (“Move es6 and node versions out of distribution directory”) moved the ES module and Node builds out of dist/ into lib/, so dist/ holds the game.
Why. A download must run as it is, and the README’s first instruction is to unzip it and open index.html. The Cypress specs load dist/index.html. The record doesn’t say why the starter game and the build output share a folder.
Costs. Every change to src/ comes with rebuilt bundles, and nothing in CI checks they match. Until commit 5b16c02e (“Don’t commit core”, December 2025) a workflow committed them as “Update engine core”; since then, the person making a change does. Anything left in dist/ on a maintainer’s disk can end up in the npm package, whose files list includes all of it (Building and Packaging).
The engine is TypeScript, built with Bun
Decided. The engine moved from JavaScript to TypeScript between December 2025 (bbc0c637, “Start typescript migration”) and the 2.8.0 release, and its build, scripts and CI moved from Yarn to Bun (0d2e4b12, “Keep up with bun migration”; 5b16c02e switched the workflow).
Why. CONTRIBUTING.md asks that new code be typed and that the public types under src/lib/types/ stay current. The README lists “full type definitions shipped for the engine, actions and components” among the features. The record gives no reason for Bun over the earlier tools.
Costs. The build depends on tsc for declarations, and for every release before 2.8.1 the declarations silently weren’t generated (tsconfig.json’s noEmit won over --emitDeclarationOnly): 2.8.0 shipped without them. src/browser.ts exists only to set the browser globals, which its comment calls “a hack… Once/if bun adds support for this, we can remove this file.”
Audio goes through the Web Audio API
Decided. play music, play sound and play voice load each file with fetch(), decode it with decodeAudioData() and play it through AudioPlayer (src/lib/AudioPlayer.ts) since commit b969c5a4 (“Use audio API instead of audio elements”, July 2025).
Why. The comments say what it’s for: AudioPlayer “provides a high-level API for playing audio with effects using the Web Audio API”, and the engine keeps an AudioContext “for audio playback with effects”. The commit message gives no more.
Costs. fetch() of a local file is refused by browsers for a page opened from the disk, and the effects need the decoded data. Until 2.9.0, v2 had no audio at all when a game was opened from index.html without a server. Since 2.9.0, Play plays the track through an <audio> element there (ElementAudioPlayer, a subclass of AudioPlayer, in src/lib/ElementAudioPlayer.ts), and when a served game’s fetch() is refused, without the effects (What works opened from a file).
The interface is web components on Pandora, in the light DOM
Decided. Every screen and interface element is a custom element defined by a class in src/components/, built on Pandora’s Component through src/lib/Component.ts, rendering into the light DOM. ShadowComponent exists for games but no built-in uses it.
Why. The record gives no reason for the light DOM. The effect is that a game’s style/main.css reaches every element, which is how the style docs tell game developers to restyle a game.
Costs. Global CSS reaches the engine’s elements too: the utility classes of Kayros (.left, .right, .center) apply to sprites that carry position classes with the same names, which is what the 2.8.1 positioning fix had to work around. Pandora 0.5.0 bundled Lit’s development build, so every game before 2.9.0 logs “Lit is in dev mode” at boot; 2.9.0 uses Pandora 0.5.1, built for production.
Kayros stays as a local copy
Decided. src/style/kayros.css is a copy of the Kayros CSS framework, imported by src/index.css.
Why. The comment above the import: “This library has been repurposed and no longer serves Monogatari’s purposes. We’re keeping a local copy in the meantime to avoid breaking changes.” Games may rely on its classes.
Costs. Its class names are global and generic, and collide with the engine’s (see above). Removing it would break games that use it, so it stays for v2.
random-js is vendored
Decided. src/vendor/random-js/ is a copy of the random-js library from Monogatari’s fork, vendored in 2.8.1 (fea01a2e).
Why. src/vendor/random-js/README.md: the fork “removes upstream’s unguarded dependency on Node’s crypto module so the library works in the browser bundle”, and “it is vendored instead of installed from git because recent npm versions refuse git dependencies by default, which made @monogatari/core uninstallable.”
Costs. Updates come by hand: the README asks that changes be made in the fork and copied over, and the files be kept as they are so they can be diffed.
Error dialogs only in development
Decided. FancyError.show() displays a dialog only when the debug module is loaded (it sets window.MonogatariDebug) and the debug level is above 0. The dialogs’ text lives in debug/index.js, not in the engine. Since 2.9.0 the module also gates the console’s step-by-step trace, and loading it starts the level at 1, so the dialogs work even when js/main.js never sets a level.
Why. The comment in debug/index.js: the global “signals to FancyError that we’re in debug mode and fancy errors should be displayed. Without this, FancyError.show() will not display any errors.” dist/index.html labels the module’s <script> “Remove this file for release builds”, and build-web.ts removes it, so players never see a developer’s error dialog.
Costs. A released game that hits a mistake fails quietly; only the console says why, with the errors and warnings that still print there without the module.
Each action declares when it blocks the game
Decided. The block and _executing_sub_action globals are deprecated in favor of each action’s own static blocking flag, checked in that action’s shouldProceed() (commit 14494a78, “Deprecate block and executing sub action in favor of action blocking”). Conditional and Video use it.
Why. The comment on blocking in src/lib/Action.ts: “We will use this instead of the engine block”. The record says no more.
Costs. shouldProceed() and shouldRollback() still check the old globals (the line in shouldProceed() is marked “TODO: This should be removed”), so both mechanisms exist in 2.8.x.
Old saves are migrated when they’re loaded
Decided. loadFromSlot() converts a v1.4.1 save to the v2 shape, and passes every v2 save through the functions in src/migrations/ in order.
Why. Each migration’s comment, marked @Compatibility, says what changed: v2.1.0’s character history gained the statement each show replaced, “to suppress a bug where the incorrect items were shown as it assumed they were previously visible”.
Costs. A change to what an action records needs a migration, or saves from the previous release load wrong; nothing tests old saves automatically.
Game developers need no tools
Decided. A game is a folder of static files that runs in a browser, and every docs page outside this section works without a terminal, Node, Bun or npm.
Why. The README: “your visual novel is a web page first and a game later”, and making a game needs “a text editor that can handle HTML, JavaScript and CSS”. The docs’ rule, in the user’s words: “We must never assume users are able to run anything related to node, bun or npm. Some people even develop games on their phone.”
Costs. Engine features must work in the plain files (dist/js/*.js are scripts, not modules), and anything that needs tools (serve.ts, the desktop builds, the npm package) is documented as optional. Where v2 can’t work without a server (audio, the service worker), the docs have to say so.
The docs are checked against the code
Decided. The docs live in the repository (7e7a33e3, “Migrate docs to main repo”) and every claim on them is checked against v2’s code and run where it can be (Writing and Publishing the Docs).
Why. The docs that came before were thin and, in places, wrong; the rule for the rewrite is to “match the implementation: do not guess, always check the real implementation.”
Costs. A change to the engine’s behavior must change its page in the same pull request, and pages take longer to write.