diff --git a/docs/api-reference/cli.md b/docs/api-reference/cli.md index 91eeb991231..56b09ee65e5 100644 --- a/docs/api-reference/cli.md +++ b/docs/api-reference/cli.md @@ -17,12 +17,14 @@ Options: Commands: cloud The Hosting CLI. + compile Compile the app in the current directory. db Subcommands for managing the database schema. deploy Deploy the app to the Reflex hosting service. export Export the app to a zip file. init Initialize a new Reflex app in the current directory. login Authenticate with experimental Reflex hosting service. logout Log out of access to Reflex hosting service. + minify Manage state, event and var name minification. rename Rename the app in the current directory. run Run the app in the current directory. script Subcommands for running helper scripts. @@ -112,6 +114,29 @@ The `reflex cloud` command provides access to the Reflex Cloud hosting service. For detailed documentation on Reflex Cloud and deployment, see the [Cloud Quick Start Guide](https://reflex.dev/docs/hosting/deploy-quick-start/). +## Minify + +The `reflex minify` command manages the short state, event handler and var names +used when name minification is enabled. + +```bash +$ reflex minify --help +Usage: reflex minify [OPTIONS] COMMAND [ARGS]... + + Manage state, event and var name minification. + +Commands: + init Initialize minify.json with IDs for all states, events and vars. + list Print the state tree with IDs and minified names. + lookup Lookup a state, event handler or var by its minified path (e.g.,... + sync Synchronize minify.json with the current codebase. + validate Validate minify.json against the current codebase. +``` + +See [Name Minification](/docs/api-reference/minification/) for +the `minify.json` format, the `REFLEX_MINIFY_*` environment variables, and the +deployment requirements. + ## Script The `reflex script` command provides access to helper scripts for Reflex development. diff --git a/docs/api-reference/minification.md b/docs/api-reference/minification.md new file mode 100644 index 00000000000..71039af3a35 --- /dev/null +++ b/docs/api-reference/minification.md @@ -0,0 +1,438 @@ +# State, Event and Var Name Minification + +Reflex identifies every state, event handler and state var on the wire by its +full name. A click on a button bound to `CartState.clear` in an app called +`demo` sends: + +```text +reflex___state____state.demo___demo____cart_state.clear +``` + +and the delta updating `CartState.items` looks like: + +```json +{"reflex___state____state.demo___demo____cart_state": {"items_rx_state_": []}} +``` + +Those names are also baked into the compiled frontend — as React context names, +as the keys of every state delta, as the expression reading every var, and as +the event name in every handler closure. In an app with many substates and vars +they add up in the bundle, in every websocket frame, and in the initial state +document. + +Minification replaces them with short ids drawn from a checked-in +`minify.json`, so the same event and delta become: + +```text +a.b.b +``` + +```json +{"a.b": {"a": []}} +``` + +It is **opt-in and off by default**. Turning it on requires a `minify.json` in +the app directory and one environment variable per kind of name, and the +frontend build and the backend process must agree on both. + +```md alert warning +# Minified names are not a security boundary. + +Anyone can read `minify.json` from your repository, and `reflex minify lookup` +turns a minified name back into a module and class in one command. Minification +makes names shorter, not secret. +``` + +## Quick start + +From the app directory (the one containing `rxconfig.py`): + +```bash +reflex minify init +``` + +This imports your app, walks the state tree, and writes `minify.json`. Commit +that file — it is the shared contract between the frontend bundle and the +backend, and it must be identical in both. + +```bash +git add minify.json +git commit -m "add minify.json" +``` + +Then build and run with the modes enabled: + +```bash +REFLEX_MINIFY_STATES=1 REFLEX_MINIFY_EVENTS=1 REFLEX_MINIFY_VARS=1 reflex run --env prod +``` + +The same variables must be set for whatever compiles the frontend *and* for the +backend process. + +## The `minify.json` file + +`minify.json` is read from the current working directory of the process, so +every command — `reflex minify`, `reflex run`, `reflex export`, and the backend +server — must be started from the app directory, unless `REFLEX_MINIFY_CONFIG` +names the file (see [Using a config from another path](#using-a-config-from-another-path)). + +```json +{ + "events": { + "demo.demo.State.CartState": { + "add_item": "a", + "clear": "b", + "setvar": "c" + } + }, + "states": { + "demo.demo.State.CartState": { + "id": "b", + "parent": "reflex.state.State" + }, + "reflex.state.State": { + "id": "a", + "parent": null + } + }, + "vars": { + "demo.demo.State.CartState": { + "items": "a", + "total": "b" + } + }, + "version": 1 +} +``` + +- `version` — the schema version, currently `1`. A file with any other version + is rejected. +- `states` — maps a state's **full path** to its entry. The full path is the + defining module followed by the class hierarchy from the root state down, + e.g. `demo.demo.State.CartState`. Each entry has an `id` (the minified name) + and a `parent` (the full path of the parent state, or `null` for a root + state). +- `events` — maps a state's full path to `{handler name: minified name}`. +- `vars` — maps a state's full path to `{var name: minified name}`, covering + the vars the state sends to the frontend: its own base vars and computed vars + (backend vars never leave the server, so they have no entry). A var inherited + from a parent state belongs to the parent's entry. Files written before vars + were minified have no `vars` section and load as if it were empty. + +Framework states are included too: `reflex.state.State` and its internal +substates carry entries just like your own states. + +Ids use the alphabet `a-z`, `A-Z`, `$` and `_` (the characters that are legal in +a JavaScript identifier), counting `a`, `b`, … `z`, `A`, … `_`, `ba`, `bb`, …. +A state id must be unique **among its siblings**, and must differ from its +parent's id — otherwise a relative path like `a.a` would be ambiguous. Two +states under different parents may both be `"b"`. A state id also may not start +or end with `_` or contain `__`: the compiled frontend joins a state's path with +`__`, and `a` + `_b` would read the same as `a_` + `b`. Nor can a root state's +id be a name every JavaScript object already has (`constructor`, `toString`, +`__proto__`, …): the frontend's state maps are keyed by dotted state path, and a +root's path is its id alone, so it would find the inherited member. `reflex +minify sync` replaces such an id in a file written before these rules. An event +id and a var id must each be unique **within their state**. A var id is a key of +its state's object in the frontend, so it cannot be such a name either, or end +in `_rx_state_`, the suffix of an unminified var key. + +The file is written sorted and with a stable layout, so regenerating it produces +no spurious diffs. It is hand-editable, but in practice let the CLI maintain it. +Loading it checks the schema version, that `states`, `events` and (when present) +`vars` are objects, that every state entry has a string `id` and a `parent` that +is a string or `null`, that every id uses the alphabet above and no var id is +reserved, that no state id has an underscore at an end or doubled and no root +state id is such a name, and that no two handlers or vars of a state share an +id; keys it does not know are ignored. On a file that fails these checks, +compiling or serving the app logs a warning and runs without minification. The +`reflex minify` commands stop with an error naming the problem, except for the +last two, which `validate` reports and `sync` repairs. Sibling states sharing an +id are reported by `reflex minify validate`. + +### `parent` and reserved ids + +An entry keeps its `parent` even after the class it names is deleted. That is +what keeps the deleted state's id reserved inside its sibling group, so a state +added later never inherits an id that an already-served frontend still uses for +something else. + +## Environment variables + +| Variable | Default | +|---|---| +| `REFLEX_MINIFY_STATES` | off | +| `REFLEX_MINIFY_EVENTS` | off | +| `REFLEX_MINIFY_VARS` | off | +| `REFLEX_MINIFY_CONFIG` | `minify.json` in the app directory | + +The first three are ordinary boolean env vars, so `1`, `true` and `yes` turn them on and +`0`, `false` and `no` turn them off. They are independent: you can minify any +combination of state names, event handler names and var names. None of them +has any effect without a `minify.json`. + +The variables are read at **compile time** and at **run time**, and both places +must see the same values: + +- The process that compiles the frontend (`reflex run`, `reflex compile`, + `reflex export`) bakes the resolved names, and a digest of the whole scheme, + into the generated bundle. +- The backend process resolves incoming event names through the same + configuration. + +In a split deployment — a statically hosted frontend and a separately deployed +backend — set the variables in the build environment and in the backend's +environment, and deploy the same `minify.json` to both. + +### Using a config from another path + +`REFLEX_MINIFY_CONFIG` names the config file explicitly, as an absolute path or +one relative to the working directory: + +```bash +REFLEX_MINIFY_CONFIG=/etc/myapp/minify.json reflex run --env prod +``` + +It applies to every command, the `reflex minify` ones included, and to every +app the process loads. Without it, each app is loaded with the `minify.json` in +its own directory: an app loaded after another one in the same process — such +as an `AppHarness` test app next to your repository's minified app — gets its +own config's names, or the default names when its directory has none. + +### What keeps its name + +Minification rewrites only what travels between the frontend and the backend. +Where a state lives on the server — its key in Redis or on disk — its shared +state links, and the name a `Cookie`, `LocalStorage` or `SessionStorage` var is +stored under in the browser do not depend on it, so turning minification on or +editing `minify.json` loses no session and no stored value. A storage var +without an explicit `name` keeps the storage name it has without minification. + +`State.setvar("count")` sends the var's Python name, which the backend looks up +through the state's class hierarchy; only the event name itself is minified. + +## CLI reference + +```bash +$ reflex minify --help +Usage: reflex minify [OPTIONS] COMMAND [ARGS]... + + Manage state, event and var name minification. + +Commands: + init Initialize minify.json with IDs for all states, events and vars. + list Print the state tree with IDs and minified names. + lookup Lookup a state, event handler or var by its minified path... + sync Synchronize minify.json with the current codebase. + validate Validate minify.json against the current codebase. +``` + +Every subcommand imports and compiles your app so that dynamically created +states (such as `rx.ComponentState` instances) are registered before the state +tree is walked. They read the config rather than apply it, so their output does +not depend on whether `REFLEX_MINIFY_*` is set in the shell running them. + +### `reflex minify init` + +Creates `minify.json` from the current state tree, numbering each sibling group +from `a`. It refuses to overwrite an existing file — use `sync` to update one, +or delete the file to start over. + +```bash +$ reflex minify init +Info: Created minify.json with 7 states, 15 events and 12 vars. +``` + +### `reflex minify sync` + +Adds entries for states, event handlers and vars that are in your code but not +yet in the file, leaving existing ids untouched. + +```bash +$ reflex minify sync +Info: Updated minify.json: +Info: States: 7 -> 8 +Info: Events: 15 -> 17 +Info: Vars: 12 -> 13 +``` + +Two flags change ids that clients may already be using: + +- `--prune` removes entries for states, handlers and vars that no longer exist + in the code, freeing their ids for reuse. +- `--reassign-deleted` fills the gaps left by removed entries instead of + continuing past the highest id in use. + +```md alert warning +# `--prune` and `--reassign-deleted` can hand an old id to a new state. + +A browser that is still running a frontend built against the previous file would +then resolve that id to the wrong state. Use them only when every client will +reload — for example in development, or as part of a release where the frontend +URL changes. +``` + +### `reflex minify validate` + +Checks the file against the current code. Exits non-zero when there is anything +to fix, which makes it a useful CI step. + +```bash +$ reflex minify validate +Warning: Missing entries (in code but not in config): +Warning: - state:demo.demo.State.SecondNewState +Warning: - event:demo.demo.State.SecondNewState.setvar +``` + +It reports: + +- **errors** — duplicate state ids within a sibling group, a state that reuses + its parent's id, or duplicate event or var ids within a state (exit code 1); +- **missing entries** — states, handlers or vars in the code with no entry, + which silently keep their long names (exit code 1); +- **warnings** — entries for states, handlers or vars that no longer exist, and + entries whose recorded `parent` no longer matches the code (exit code 0). + +### `reflex minify list` + +Prints the whole state tree with the id assigned to each state, handler and +var. It works without a `minify.json`, in which case it just prints the tree. + +```bash +$ reflex minify list +State Tree (minify.json loaded) +`-- State -> "a" + |-- Event Handlers: + | |-- hydrate -> "a" + | |-- hydrate_and_load -> "b" + | |-- set_is_hydrated -> "c" + | `-- setvar -> "d" + |-- Vars: + | |-- is_hydrated -> "a" + | `-- ... + |-- CartState -> "b" + | |-- Event Handlers: + | | |-- add_item -> "a" + | | |-- clear -> "b" + | | `-- setvar -> "c" + | `-- Vars: + | |-- items -> "a" + | `-- total -> "b" + `-- SettingsState -> "c" +``` + +`--json` writes the same tree as a JSON document on stdout; logs and anything +your app prints while it loads go to stderr, so the output can be piped into +another tool. + +### `reflex minify lookup` + +Turns a minified name — copied from a browser devtools network frame, or from a +backend log line — back into the module, class and handler or var it refers to. +For a var, append the key from a delta to its state's name, e.g. `a.b.a` for +key `"a"` of state `"a.b"`. + +```bash +$ reflex minify lookup a.b.b +demo.demo.CartState +demo.demo.CartState.clear +``` + +The path is resolved segment by segment from the root state, and the leading +root state segment is optional and may be given either minified (`a`) or in full +(`reflex___state____state`) — so a name copied verbatim from the frontend +resolves as-is. On its own that segment looks the root state itself up. Because +states, event handlers and vars are numbered independently, the last segment is +matched against the handlers and vars of the state resolved so far as well as +against its substates; when a segment is ambiguous, every reading is printed. +Unminified segments are accepted too, so a partially minified name still +resolves. + +`--json` prints the full resolution — `kind`, `module`, `class`, `handler` or +`var`, and `full_path` for each segment — on stdout, again with logs on stderr. + +## Frontend and backend must agree + +A compiled frontend and the backend it talks to must resolve names the same way. +The frontend therefore carries a short digest of the scheme it was built with — +computed from the contents of `minify.json` together with the mode +variables — and sends it when it opens its websocket. Uploads carry the same +digest in a `Reflex-Scheme` header, since they travel over HTTP rather than the +socket. + +If the backend's digest differs, the backend: + +1. logs a warning naming both digests; +2. sends a `scheme_mismatch` notice to that client; +3. ignores every event that client sends, and rejects its uploads with `409`. + +The page stops sending events and tells the viewer it is out of date, offering a +reload. + +This happens whenever the two sides diverge: + +- the backend was deployed with a `minify.json` the frontend was not built + against (or the other way round); +- one side has `REFLEX_MINIFY_STATES`, `REFLEX_MINIFY_EVENTS` or + `REFLEX_MINIFY_VARS` set differently from the other; +- a browser is still running a bundle from before a deploy that changed + `minify.json`. + +The digest covers the state ids and the event and var maps, so any edit that +changes an id invalidates every previously served bundle — including one that +only adds entries. Edits that change nothing a client can observe do not: a `parent` field +is not hashed, and a map whose `REFLEX_MINIFY_*` mode is off contributes nothing. +Plan deploys accordingly: ship the frontend and the backend together, and expect +open tabs to need a reload after an id changes. + +## Deploy workflow + +A typical loop when the state tree changes: + +1. Add or rename states, event handlers and vars as usual. +2. Run `reflex minify sync` and commit the updated `minify.json` alongside the + code change. +3. Optionally run `reflex minify validate` in CI so a forgotten `sync` fails the + build rather than silently shipping long names. +4. Build the frontend and run the backend with the same `REFLEX_MINIFY_*` + values and the same `minify.json`. + +States, handlers and vars with no entry in the file keep their full names; this +is not an error, it just means those names are not shortened. Compiling with +minification enabled warns about them and points at `reflex minify sync`. + +## Debugging a minified app + +Backend logs, tracebacks and OpenTelemetry spans report the resolved (minified) +names, because that is what the event carried. `reflex minify lookup` maps them +back: + +```bash +reflex minify lookup a.b.b +``` + +If you want readable names while reproducing a problem, unset the environment +variables and rebuild; `minify.json` on its own changes nothing. + +## Limitations + +- **Run from the app directory.** `minify.json` is looked up in the process's + current working directory. A backend started from anywhere else does not find + the file, resolves names the default way, and rejects every client with a + scheme mismatch. Set `REFLEX_MINIFY_CONFIG` if it cannot be started there. +- **Components built before `minify.json` is loaded keep the default names.** + Reflex loads `minify.json` as soon as it can — when `reflex.state` is first + imported from the app directory, and again before importing your app — and + renames every state's own vars when it does. A var expression a module built + from them earlier, such as a component created at the top of a module + imported before the app directory was entered, still uses the default names, + and compiling fails with a message naming each one. The same goes the other + way for state names: a component built while another app's `minify.json` was + in use cannot be compiled into an app that does not rename those states. Import the app from its + directory, or create such components inside the page function. +- **`rx.ComponentState` instances are numbered by creation order.** Each + `create()` call produces its own state class, named `Counter_n1`, `Counter_n2` + and so on, and each gets its own `minify.json` entry. Inserting or reordering + a `create()` call renames every instance after it, which moves their entries. + Pass `_state_key` to give an instance a stable name — see + [Component State](/docs/state-structure/component-state/#naming-the-state). diff --git a/docs/app/reflex_docs/pages/docs/apiref.py b/docs/app/reflex_docs/pages/docs/apiref.py index 0f55cb82e81..244f16e0103 100644 --- a/docs/app/reflex_docs/pages/docs/apiref.py +++ b/docs/app/reflex_docs/pages/docs/apiref.py @@ -47,6 +47,7 @@ "importvar", "var-system", "cli", + "minification", "browser-storage", "browser-javascript", "plugins", diff --git a/docs/app/tests/test_sidebar.py b/docs/app/tests/test_sidebar.py index 1d0bba24057..22b6c30af4a 100644 --- a/docs/app/tests/test_sidebar.py +++ b/docs/app/tests/test_sidebar.py @@ -183,6 +183,7 @@ def test_api_reference_groups_related_symbols(): "ImportVar", "Var System", "CLI", + "Minification", "Browser Storage", "Browser Javascript", "Plugins", diff --git a/docs/state_structure/component_state.md b/docs/state_structure/component_state.md index bfce270a73f..a9c1bba0b47 100644 --- a/docs/state_structure/component_state.md +++ b/docs/state_structure/component_state.md @@ -266,3 +266,42 @@ def extended_counter(): ), ) ``` + +## Naming the State + +Every `create()` call builds its own state class. By default those classes are +numbered in the order they are created — `ReusableCounter_n1`, +`ReusableCounter_n2`, and so on — so inserting or reordering a `create()` call +renames every instance after it. + +Pass `_state_key` to name an instance instead: + +```python +cart_counter = ReusableCounter.create(_state_key="cart") +wishlist_counter = ReusableCounter.create(_state_key="wishlist") +``` + +The key must be a valid Python identifier without uppercase letters, since the +state's default name is snake-cased and `cartItem` would name the same state as +`cart_item`. It must also be unique among the keyed instances of components with +that class name. Keyed instances do not advance the counter, so adding one leaves +the names of the unkeyed instances alone. + +Reusing a key raises: one key names one state. To render a keyed component on +several pages, create it once and embed that same instance in each page, rather +than calling `create()` again with the same key. + +A stable name matters when something outside the app refers to it — most +directly [state and event name minification](/docs/api-reference/minification/), +where each instance gets its own `minify.json` entry keyed by that name, under +the module that defines the component: + +```json +"my_app.counter.State.ReusableCounter__cart": { + "id": "b", + "parent": "reflex.state.State" +} +``` + +Without a key, that entry is keyed by creation order, and reordering the page +moves every instance to a different entry. diff --git a/news/6100.feature.1.md b/news/6100.feature.1.md new file mode 100644 index 00000000000..0d86d3b8af5 --- /dev/null +++ b/news/6100.feature.1.md @@ -0,0 +1 @@ +`ComponentState.create(_state_key="cart")` gives an instance's state a stable name instead of numbering it by creation order. See [Component State](https://reflex.dev/docs/state-structure/component-state/#naming-the-state). diff --git a/news/6100.feature.md b/news/6100.feature.md new file mode 100644 index 00000000000..cdfc14daa57 --- /dev/null +++ b/news/6100.feature.md @@ -0,0 +1,6 @@ +State and event handler names can be shortened to ids from a checked-in `minify.json`; the frontend and backend must be built with the same file and settings. See [minification](https://reflex.dev/docs/api-reference/minification/). + +```bash +reflex minify init +REFLEX_MINIFY_STATES=1 REFLEX_MINIFY_EVENTS=1 reflex run --env prod +``` diff --git a/news/7337.feature.1.md b/news/7337.feature.1.md new file mode 100644 index 00000000000..d81ed8b4195 --- /dev/null +++ b/news/7337.feature.1.md @@ -0,0 +1 @@ +`REFLEX_MINIFY_CONFIG` names the `minify.json` to use, for a process that cannot start in the app directory. Without it, each app is now loaded with the `minify.json` in its own directory, so an `AppHarness` test app run next to a minified app gets the default names instead of the other app's. diff --git a/news/7337.feature.md b/news/7337.feature.md new file mode 100644 index 00000000000..59591aadb2e --- /dev/null +++ b/news/7337.feature.md @@ -0,0 +1 @@ +`REFLEX_MINIFY_VARS=1` shortens state var names on the wire, using the per-state ids that `reflex minify init`/`sync` now also write to a `vars` section of `minify.json`: a delta such as `{"a.b": {"items_rx_state_": []}}` becomes `{"a.b": {"c": []}}`. Sessions in Redis or on disk and `rx.Cookie`/`rx.LocalStorage`/`rx.SessionStorage` values keep their keys when minification is turned on or `minify.json` changes, and `minify.json` may now be loaded after Reflex was imported, so in-process `AppHarness` tests can run minified. See [minification](https://reflex.dev/docs/api-reference/minification/). diff --git a/packages/reflex-base/news/6100.bugfix.md b/packages/reflex-base/news/6100.bugfix.md new file mode 100644 index 00000000000..8e016649d18 --- /dev/null +++ b/packages/reflex-base/news/6100.bugfix.md @@ -0,0 +1 @@ +An event that reaches no registered handler is now passed to the app's `backend_exception_handler` as well as logged, so it surfaces in the browser instead of leaving the page waiting for an update that never arrives. diff --git a/packages/reflex-base/news/6100.feature.md b/packages/reflex-base/news/6100.feature.md new file mode 100644 index 00000000000..54552951238 --- /dev/null +++ b/packages/reflex-base/news/6100.feature.md @@ -0,0 +1 @@ +State and event handler names now go through a pluggable `NameResolver`, installed with `RegistrationContext.set_name_resolver`. See [minification](https://reflex.dev/docs/api-reference/minification/). diff --git a/packages/reflex-base/news/7337.feature.md b/packages/reflex-base/news/7337.feature.md new file mode 100644 index 00000000000..5aecb769c66 --- /dev/null +++ b/packages/reflex-base/news/7337.feature.md @@ -0,0 +1 @@ +`NameResolver` resolves state var names too (`resolve_var_name`), and `RegistrationContext.set_name_resolver` rebuilds the Vars every registered state holds, so a resolver can be installed after the states are defined. `reflex_base.utils.format.format_var_key` and `format_state_var` give the key and expression a state var has in the frontend under the active resolver. `EnvironmentVariables.REFLEX_MINIFY_CONFIG` holds the path of an explicitly chosen `minify.json`. diff --git a/packages/reflex-base/src/reflex_base/compiler/templates.py b/packages/reflex-base/src/reflex_base/compiler/templates.py index 77f28862881..ea456199eb7 100644 --- a/packages/reflex-base/src/reflex_base/compiler/templates.py +++ b/packages/reflex-base/src/reflex_base/compiler/templates.py @@ -2,6 +2,7 @@ from __future__ import annotations +import dataclasses import json import re from collections.abc import Iterable, Mapping, Sequence @@ -17,8 +18,9 @@ from reflex_base.components.component import Component +# The local is a JavaScript identifier: a renamed state's carries a ``$rx_`` prefix. _STATE_HOOK = re.compile( - r'const (\w+) = useStateContext\(("[^"]*")(?:, (\[[^\]]*\]))?\)' + r'const ([\w$]+) = useStateContext\(("[^"]*")(?:, (\[[^\]]*\]))?\)' ) @@ -323,6 +325,30 @@ def theme_template(theme: str): return f"""export default {theme}""" +@dataclasses.dataclass(frozen=True, slots=True) +class InternalNames: + """Wire names the generated context module uses to reach framework state. + + Resolved by the compiler under the active name resolver so the frontend + keeps working when names are rewritten (e.g. minified). + + Attributes: + main_state_name: Name of the framework root ``State``. + is_hydrated_key: Delta key of the root ``State.is_hydrated`` var. + hydrate_and_load: Full event name of ``hydrate_and_load``. + on_load_internal: Full event name of ``on_load_internal``. + update_vars_internal: Full event name of ``update_vars_internal``. + handle_frontend_exception: Full event name of ``handle_frontend_exception``. + """ + + main_state_name: str + is_hydrated_key: str + hydrate_and_load: str + on_load_internal: str + update_vars_internal: str + handle_frontend_exception: str + + def context_template( *, is_dev_mode: bool, @@ -330,10 +356,11 @@ def context_template( initial_state: dict[str, Any] | None = None, initial_state_json: str | None = None, initial_state_hashes: list[str] | None = None, - state_name: str | None = None, + internal_names: InternalNames | None = None, state_schema: str | None = None, client_storage: dict[str, dict[str, dict[str, Any]]] | None = None, disable_react_owner_stacks: bool = False, + scheme_digest: str = "", ): """Template for the context file. @@ -343,7 +370,7 @@ def context_template( initial_state_hashes: Per-state hashes of ``initial_state`` in sorted state name order, sent with the first hydrate so the backend can skip vars still at their default. - state_name: The name of the state. + internal_names: Resolved framework wire names; given for a stateful app. state_schema: Hash of the state tree (names, frontend vars, event handlers) this frontend is compiled for, sent on every socket connect so the backend can detect a frontend built for another @@ -354,6 +381,8 @@ def context_template( disable_react_owner_stacks: Whether to emit the snippet that disables React's dev-build owner-stack capture (an Error() constructed per created element, whose cost grows with render depth). + scheme_digest: Digest of the wire-name scheme this bundle was built + with, sent on connect so the backend can detect a mismatch. Returns: Rendered context file content as string. @@ -363,10 +392,16 @@ def context_template( initial_state_json = json_dumps(initial_state) state_str = ( rf""" -export const state_name = "{state_name}" -export const on_load_internal_event = '{state_name}.{constants.CompileVars.ON_LOAD_INTERNAL}' +export const main_state_name = "{internal_names.main_state_name}" +export const on_load_internal_event = "{internal_names.on_load_internal}" + +export const is_hydrated_key = "{internal_names.is_hydrated_key}" -export const exception_state_name = "{constants.CompileVars.FRONTEND_EXCEPTION_STATE_FULL}" +export const hydrate_and_load = "{internal_names.hydrate_and_load}" + +export const update_vars_internal = "{internal_names.update_vars_internal}" + +export const handle_frontend_exception = "{internal_names.handle_frontend_exception}" // Tracked cookie and local storage vars set in the browser, or undefined if none. const clientStorageVars = () => {{ @@ -386,7 +421,7 @@ def context_template( if (client_storage_vars !== undefined) {{ internal_events.push( ReflexEvent( - '{state_name}.{constants.CompileVars.UPDATE_VARS_INTERNAL}', + '{internal_names.update_vars_internal}', {{{constants.CompileVars.PAYLOAD_VARS}: client_storage_vars}}, ), ); @@ -394,7 +429,7 @@ def context_template( // `on_load_internal` triggers the correct on_load event(s) for the current page. // If the page does not define any on_load event, this will just set `is_hydrated = true`. - internal_events.push(ReflexEvent('{state_name}.{constants.CompileVars.ON_LOAD_INTERNAL}')); + internal_events.push(ReflexEvent('{internal_names.on_load_internal}')); return internal_events; }} @@ -413,15 +448,21 @@ def context_template( if (first) {{ payload["{constants.CompileVars.PAYLOAD_HASHES}"] = initialStateHashes; }} - return [ReflexEvent('{state_name}.{constants.CompileVars.HYDRATE_AND_LOAD}', payload)]; + return [ReflexEvent('{internal_names.hydrate_and_load}', payload)]; }} """ - if state_name + if internal_names else """ -export const state_name = undefined +export const main_state_name = undefined export const on_load_internal_event = undefined -export const exception_state_name = undefined +export const is_hydrated_key = undefined + +export const hydrate_and_load = undefined + +export const update_vars_internal = undefined + +export const handle_frontend_exception = undefined export const onLoadInternalEvent = () => [] @@ -474,19 +515,27 @@ def context_template( export const isDevMode = {json.dumps(is_dev_mode)}; +// Identifies the wire-name scheme this bundle was built with; the backend +// rejects a connection whose scheme does not match its own. +export const schemeDigest = {json.dumps(scheme_digest)}; + // The static runtime reads these through the registry, so this module is the // only one Vite re-executes when they change. registerApp({{ initialState, clientStorage, - state_name, + main_state_name, on_load_internal_event, stateSchema, - exception_state_name, + is_hydrated_key, + hydrate_and_load, + update_vars_internal, + handle_frontend_exception, onLoadInternalEvent, initialEvents, isDevMode, defaultColorMode, + schemeDigest, }}); export function addEvents(events, args, event_actions) {{ diff --git a/packages/reflex-base/src/reflex_base/constants/compiler.py b/packages/reflex-base/src/reflex_base/constants/compiler.py index 48a21df1ed2..3a26f2d6cd3 100644 --- a/packages/reflex-base/src/reflex_base/constants/compiler.py +++ b/packages/reflex-base/src/reflex_base/constants/compiler.py @@ -78,18 +78,6 @@ class CompileVars(SimpleNamespace): CONNECT_ERROR = "connectErrors" # The name of the function for converting a dict to an event. TO_EVENT = "ReflexEvent" - # The name of the internal on_load event. - ON_LOAD_INTERNAL = "reflex___state____on_load_internal_state.on_load_internal" - # The name of the internal event to update generic state vars. - UPDATE_VARS_INTERNAL = ( - "reflex___state____update_vars_internal_state.update_vars_internal" - ) - # The name of the frontend event exception state - FRONTEND_EXCEPTION_STATE = "reflex___state____frontend_event_exception_state" - # The full name of the frontend exception state - FRONTEND_EXCEPTION_STATE_FULL = ( - f"reflex___state____state.{FRONTEND_EXCEPTION_STATE}" - ) class PageNames(SimpleNamespace): diff --git a/packages/reflex-base/src/reflex_base/constants/event.py b/packages/reflex-base/src/reflex_base/constants/event.py index 9c0cf2bd3e5..44f028c52f0 100644 --- a/packages/reflex-base/src/reflex_base/constants/event.py +++ b/packages/reflex-base/src/reflex_base/constants/event.py @@ -3,6 +3,9 @@ from enum import Enum from types import SimpleNamespace +# The name of the setvar event handler. +SETVAR = "setvar" + class Endpoint(Enum): """Endpoints for the reflex backend API.""" @@ -50,6 +53,7 @@ class SocketEvent(SimpleNamespace): PING = "ping" EVENT = "event" CLIENT_ERROR = "client_error" + SCHEME_MISMATCH = "scheme_mismatch" # Sent on connect when the frontend was compiled for another state tree # (must match STATE_MISMATCH_EVENT in packages/reflex-frontend/src/utils/state.js). STATE_MISMATCH = "state_mismatch" diff --git a/packages/reflex-base/src/reflex_base/environment.py b/packages/reflex-base/src/reflex_base/environment.py index 0d5bf09d6b5..8ffe3cf0c5a 100644 --- a/packages/reflex-base/src/reflex_base/environment.py +++ b/packages/reflex-base/src/reflex_base/environment.py @@ -954,6 +954,18 @@ class EnvironmentVariables: # Deprecated in favour of REFLEX_OPLOCK_HOLD_TIME. REFLEX_OPLOCK_HOLD_TIME_MS: EnvVar[int] = env_var(0) + # Whether to enable state ID minification (requires minify.json). + REFLEX_MINIFY_STATES: EnvVar[bool] = env_var(False) + + # Whether to enable event ID minification (requires minify.json). + REFLEX_MINIFY_EVENTS: EnvVar[bool] = env_var(False) + + # Whether to enable state var ID minification (requires minify.json). + REFLEX_MINIFY_VARS: EnvVar[bool] = env_var(False) + + # The minify.json to use instead of the one in the app's directory. + REFLEX_MINIFY_CONFIG: EnvVar[Path | None] = env_var(None) + # Extra plugins to append to the config's plugins list. REFLEX_EXTRA_PLUGINS: EnvVar[list[type[Plugin]]] = env_var([]) diff --git a/packages/reflex-base/src/reflex_base/event/__init__.py b/packages/reflex-base/src/reflex_base/event/__init__.py index 63844816912..d2fe9fab1d2 100644 --- a/packages/reflex-base/src/reflex_base/event/__init__.py +++ b/packages/reflex-base/src/reflex_base/event/__init__.py @@ -2,6 +2,7 @@ import copy import dataclasses +import functools import inspect import logging import sys @@ -576,11 +577,26 @@ class EventHandler(EventActionsMixin): default=None, repr=False, compare=False ) + # ``(resolver, wire name)``, cached by ``format_event_handler``, which only + # compares the resolver by identity. Declared so that caching it keeps the + # instance layout, and attribute reads, fast. + _formatted_name: tuple[object, str] | None = dataclasses.field( + default=None, init=False, repr=False, compare=False + ) + def __post_init__(self) -> None: """Resolve handler annotations while the state class is stable.""" if self.state is not None: self._get_type_hints() + def __getstate__(self) -> dict[str, Any]: + """Leave the cached wire name, and the resolver it holds, out of copies. + + Returns: + The instance state without the cached name. + """ + return {**vars(self), "_formatted_name": None} + def _get_type_hints(self) -> dict[str, Any]: """Get and cache the type hints for the handler function. @@ -2190,19 +2206,51 @@ def get_event(state: "BaseState", event: str): Returns: The event. """ - return f"{state.get_name()}.{event}" + from reflex_base.registry import RegistrationContext + + # A StateProxy passes itself as state; its __class__ is the wrapped state's. + state_cls = state.__class__ + # The handler name is resolver-dependent (minify.json rewrites it), so it + # cannot be interpolated raw; states without their own copy keep it as-is. + ctx = RegistrationContext.try_get() + resolved = event if ctx is None else ctx.get_handler_name(state_cls, event) + return f"{state.get_name()}.{resolved}" + + +@functools.lru_cache(maxsize=1) +def _hydrate_handler() -> EventHandler: + """Look up the one ``hydrate`` handler, defined on the root ``State``. + + Returns: + The framework's hydrate event handler. + """ + from reflex.state import State + + return State.event_handlers[constants.CompileVars.HYDRATE] + + +def get_hydrate_event_name() -> str: + """Get the wire name of the framework hydrate event. + + The compiler, the event processor and the hydrate middleware must all name + it identically, so they share this one resolution. + + Returns: + The hydrate event name under the active name resolver. + """ + return format.format_event_handler(_hydrate_handler()) def get_hydrate_event(state: "BaseState") -> str: """Get the name of the hydrate event for the state. Args: - state: The state. + state: Any state in the tree; the hydrate handler is the root's. Returns: The name of the hydrate event. """ - return get_event(state, constants.CompileVars.HYDRATE) + return get_hydrate_event_name() def _values_returned_from_event(event_spec_annotations: list[Any]) -> list[Any]: @@ -3007,12 +3055,7 @@ def no_args(): _func=FunctionStringVar("ReflexEvent"), _args=( # event handler name - ".".join( - filter( - None, - format.get_event_handler_parts(value.handler), - ) - ), + format.format_event_handler(value.handler), # event handler args {str(name): value for name, value in value.args}, # event actions @@ -3809,6 +3852,7 @@ def wrapper(func: Callable[..., Any]) -> EventCallback: get_event = staticmethod(get_event) get_hydrate_event = staticmethod(get_hydrate_event) + get_hydrate_event_name = staticmethod(get_hydrate_event_name) fix_events = staticmethod(fix_events) call_event_handler = staticmethod(call_event_handler) call_event_fn = staticmethod(call_event_fn) diff --git a/packages/reflex-base/src/reflex_base/event/processor/base_state_processor.py b/packages/reflex-base/src/reflex_base/event/processor/base_state_processor.py index 8af90e5b769..d66b9528d6a 100644 --- a/packages/reflex-base/src/reflex_base/event/processor/base_state_processor.py +++ b/packages/reflex-base/src/reflex_base/event/processor/base_state_processor.py @@ -28,6 +28,7 @@ EventHandler, EventSpec, _is_bound_event_handler, + get_hydrate_event_name, ) from reflex_base.event.context import EventContext from reflex_base.event.processor.event_processor import EventProcessor, EventQueueEntry @@ -52,27 +53,41 @@ @functools.lru_cache(maxsize=1) -def _hydrate_event_names() -> frozenset[str]: - from reflex.state import State +def _connect_handlers() -> tuple[EventHandler, EventHandler]: + """Look up the (re)connect handler and the page-load handler it supersedes. - return frozenset( - format_event_handler(State.event_handlers[name]) - for name in (CompileVars.HYDRATE, CompileVars.HYDRATE_AND_LOAD) + Cached as handlers, not names: the names follow the active name resolver, + and ``format_event_handler`` caches each one on its handler. + + Returns: + The ``hydrate_and_load`` and ``on_load_internal`` handlers. + """ + from reflex.state import OnLoadInternalState, State + + return ( + State.event_handlers[CompileVars.HYDRATE_AND_LOAD], + OnLoadInternalState.event_handlers["on_load_internal"], ) -@functools.lru_cache(maxsize=1) +def _hydrate_event_names() -> tuple[str, str]: + """The events that hydrate the state themselves. + + Returns: + The full event names of ``hydrate`` and ``hydrate_and_load``. + """ + return get_hydrate_event_name(), format_event_handler(_connect_handlers()[0]) + + def _connect_supersedes() -> tuple[str, str]: """The (re)connect event and the page-load event whose chain it cancels. Returns: The full event names of ``hydrate_and_load`` and ``on_load_internal``. """ - from reflex.state import OnLoadInternalState, State - - return ( - format_event_handler(State.event_handlers[CompileVars.HYDRATE_AND_LOAD]), - format_event_handler(OnLoadInternalState.event_handlers["on_load_internal"]), + hydrate_and_load, on_load_internal = _connect_handlers() + return format_event_handler(hydrate_and_load), format_event_handler( + on_load_internal ) diff --git a/packages/reflex-base/src/reflex_base/event/processor/event_processor.py b/packages/reflex-base/src/reflex_base/event/processor/event_processor.py index 915c0e05bcd..93b2deafb57 100644 --- a/packages/reflex-base/src/reflex_base/event/processor/event_processor.py +++ b/packages/reflex-base/src/reflex_base/event/processor/event_processor.py @@ -32,6 +32,25 @@ logger = logging.getLogger(__name__) +# Names every task running the backend exception handler, so a failure of the +# handler itself is logged instead of handled again. +_EXCEPTION_HANDLER_TASK_PREFIX = "reflex_backend_exception_handler|" + + +def _fail_future(future: asyncio.Future | None, ex: BaseException) -> None: + """Fail ``future`` with ``ex`` unless it is already done. + + Args: + future: The future, if any. + ex: The exception to fail it with. + """ + if future is not None and not future.done(): + future.set_exception(ex) + with contextlib.suppress(BaseException): + # Retrieve it so a future nobody awaits doesn't warn. + future.result() + + if TYPE_CHECKING: from reflex.app import EventNamespace from reflex.event import Event @@ -922,15 +941,60 @@ async def _process_queue(self): self._enqueue_for_token( entry=entry, registered_handler=registered_handler ) - except Exception: + except Exception as ex: # Log the error and continue processing the next events. logger.exception( f"Error processing event queue entry for {entry.event} [txid={entry.ctx.txid}]:" ) + # Fail the future before anything else can raise: a caller + # awaiting this entry would otherwise wait forever. + tb = ex.__traceback__ + _fail_future(future, ex) + # Surface it client-side too: an event that never reaches a + # handler produces no update, so the page would just stall. + # Spawned rather than awaited: the handler emits over the + # socket, and this is the only consumer draining the queue + # for every token. + self._spawn_backend_exception_handler( + ex, tb, entry.ctx, f"queue={entry.ctx.txid}" + ) queue.task_done() if self._queue_task is asyncio.current_task(): self._queue_task = None + def _spawn_backend_exception_handler( + self, + ex: Exception, + tb: TracebackType | None, + ev_ctx: EventContext, + origin: str, + ) -> bool: + """Report ``ex`` to the backend exception handler in its own task. + + Args: + ex: The exception to report. + tb: The traceback ``ex`` was raised with, captured before failing its future. + ev_ctx: The event context the exception was raised under. + origin: What raised it, for the task name. + + Returns: + Whether there is a handler, and so a task now owns the event. + """ + if self.backend_exception_handler is None: + return False + # Seeded with ev_ctx so _finish_task finds it even if cancelled unstarted. + task_context = copy_context() + task_context.run(EventContext.set, ev_ctx) + task = self._tasks[ev_ctx.txid] = task_context.run( + asyncio.create_task, + self._handle_raised_exception(ex, tb, ev_ctx), + name=f"{_EXCEPTION_HANDLER_TASK_PREFIX}{origin}|{time.time()}", + ) + if sys.version_info < (3, 12): + task._event_ctx = ev_ctx # pyright: ignore[reportAttributeAccessIssue] + task.add_done_callback(self._finish_task) + return True + async def _handle_backend_exception( self, ex: Exception, ev_ctx: EventContext | None = None ) -> None: @@ -1023,27 +1087,16 @@ def _resolve_future( tb = ex.__traceback__ if future is not None and not future.done(): - future.set_exception(ex) - with contextlib.suppress(BaseException): - # Trigger the future to avoid warnings if the caller didn't wait. - future.result() + _fail_future(future, ex) if future.awaited: # The caller awaiting the event received the exception: # reporting is its failure path's, if it does not catch it. return False telemetry.send_error(ex, context="backend") - if ( - not task.get_name().startswith("reflex_backend_exception_handler|") - and self.backend_exception_handler is not None + is_handler_task = task.get_name().startswith(_EXCEPTION_HANDLER_TASK_PREFIX) + if not is_handler_task and self._spawn_backend_exception_handler( + ex, tb, task_ctx, f"task=[{task.get_name()}]" ): - # Create a new task in the same context to invoke the exception handler. - t = self._tasks[task_ctx.txid] = asyncio.create_task( - self._handle_raised_exception(ex, tb, task_ctx), - name=f"reflex_backend_exception_handler|task=[{task.get_name()}]|{time.time()}", - ) - if sys.version_info < (3, 12): - t._event_ctx = task_ctx # pyright: ignore[reportAttributeAccessIssue] - t.add_done_callback(self._finish_task) return True logger.exception(f"Error in {task.get_name()} [txid={task_ctx.txid}]:") else: diff --git a/packages/reflex-base/src/reflex_base/registry.py b/packages/reflex-base/src/reflex_base/registry.py index 05fb5c694da..7a0f5a15c5f 100644 --- a/packages/reflex-base/src/reflex_base/registry.py +++ b/packages/reflex-base/src/reflex_base/registry.py @@ -1,15 +1,31 @@ -"""A contextual registry for state and event handlers.""" +"""Contextual registry for state classes, event handlers, and the +:class:`NameResolver` strategy that turns them into user-visible names. + +The default resolver is a no-op; ``reflex.minify.MinifyNameResolver`` +plugs in a :class:`minify.json`-driven implementation. Install a custom +resolver via :meth:`RegistrationContext.set_name_resolver`. +""" from __future__ import annotations import dataclasses -from typing import TYPE_CHECKING, Any, Self +import logging +from collections.abc import Callable, Iterable +from typing import ( + TYPE_CHECKING, + Any, + ClassVar, + Protocol, + Self, + TypeVar, + runtime_checkable, +) from reflex_base.context.base import BaseContext from reflex_base.utils.exceptions import ReflexRuntimeError, StateValueError if TYPE_CHECKING: - from collections.abc import Callable, Sequence + from collections.abc import Sequence from reflex.app import App from reflex.state import BaseState @@ -19,6 +35,150 @@ from reflex_base.utils.types import ArgsSpec from reflex_base.vars.base import Var +logger = logging.getLogger(__name__) + +_T = TypeVar("_T") + + +def _rekey( + items: Iterable[_T], key_fn: Callable[[_T], str], kind: str +) -> dict[str, _T]: + """Build a name-keyed dict, warning on collisions. + + Args: + items: Source items to re-key. + key_fn: Computes the new key for each item. + kind: Human-readable noun for the collision warning (e.g. ``"state class"``). + + Returns: + A new dict mapping resolved key to item; later items overwrite earlier. + """ + out: dict[str, _T] = {} + for item in items: + key = key_fn(item) + existing = out.get(key) + if existing is not None and existing is not item: + logger.warning( + f"Two {kind}s resolve to the same full name {key!r}: " + f"{existing!r} and {item!r}. The first one will be unreachable " + "in the registry. Check minify.json for duplicate ids." + ) + out[key] = item + return out + + +def _event_handler_key(handler: EventHandler) -> str: + """Get the full name the registry keys a handler by. + + Bypasses ``format_event_handler``, which counts each name it computes as + handed out to code: a registry key is not, and must not pass for one. + + Args: + handler: The event handler. + + Returns: + The handler's full name under the active resolver. + """ + from reflex_base.utils.format import _event_handler_full_name + + return _event_handler_full_name(handler) + + +@runtime_checkable +class NameResolver(Protocol): + """Resolves user-visible names for state classes, event handlers and vars. + + Return ``None`` to defer to the framework default. See + :class:`DefaultNameResolver` (no-op) and ``reflex.minify.MinifyNameResolver``. + """ + + def resolve_state_name(self, state_cls: type[BaseState]) -> str | None: + """Return the resolved name for ``state_cls``, or ``None`` for default.""" + ... + + def resolve_handler_name( + self, state_cls: type[BaseState], handler_name: str + ) -> str | None: + """Return the resolved name for the handler, or ``None`` for default.""" + ... + + def resolve_var_name(self, state_cls: type[BaseState], var_name: str) -> str | None: + """Return the wire key for a var of ``state_cls``, or ``None`` for default.""" + ... + + def digest(self) -> str: + """Digest the names this resolver rewrites, ``""`` when it rewrites none.""" + ... + + +@dataclasses.dataclass(frozen=True, slots=True) +class DefaultNameResolver: + """No-op resolver — every state and handler keeps its default name.""" + + # Equal digests mean equal names; see ``_same_names``. + _digest_covers_names: ClassVar[bool] = True + + def resolve_state_name(self, state_cls: type[BaseState]) -> str | None: # noqa: D102 + return None + + def resolve_handler_name( # noqa: D102 + self, + state_cls: type[BaseState], + handler_name: str, + ) -> str | None: + return None + + def resolve_var_name( # noqa: D102 + self, + state_cls: type[BaseState], + var_name: str, + ) -> str | None: + return None + + def digest(self) -> str: # noqa: D102 + return "" + + +# The resolver the names cached on the state classes, which every context +# shares, were last rebuilt under. +_names_built_under: NameResolver | None = None + + +def _same_names(a: NameResolver | None, b: NameResolver) -> bool: + """Whether two resolvers are known to give every name alike. + + Only a built-in resolver's digest covers every name it gives; a custom one + may rename while reporting ``""``. + + Args: + a: A resolver, or ``None`` if unknown. + b: Another resolver. + + Returns: + Whether both are built-in and their digests are equal. + """ + # Read off the class itself: a subclass may rename under the same digest. + return ( + a is not None + and type(a).__dict__.get("_digest_covers_names", False) + and type(b).__dict__.get("_digest_covers_names", False) + and a.digest() == b.digest() + ) + + +def scheme_digest() -> str: + """Digest the wire-name scheme the active resolver produces. + + A frontend bundle and the backend it talks to must agree on what the names + on the wire mean. Memoized on the resolver instance, so installing another + resolver produces a fresh digest without coordination. + + Returns: + A short hex digest, or ``""`` when no name is rewritten. + """ + ctx = RegistrationContext.try_get() + return ctx.name_resolver.digest() if ctx is not None else "" + def _default_bundled_libraries() -> list[str]: """Return the initial set of bundled libraries for a new context. @@ -61,6 +221,15 @@ class RegistrationContext(BaseContext): default_factory=dict, repr=False, ) + # Keyed by the resolver-independent default full name, so never re-keyed. + _states_by_default_name: dict[str, type[BaseState]] = dataclasses.field( + default_factory=dict, + repr=False, + ) + name_resolver: NameResolver = dataclasses.field( + default_factory=DefaultNameResolver, + repr=False, + ) _config: Config | None = dataclasses.field(default=None, repr=False) decorated_pages: list[tuple[Callable, dict[str, Any]]] = dataclasses.field( default_factory=list, @@ -152,6 +321,10 @@ def fork(self) -> Self: already-registered classes. The next call to `get_config()` on the fork will reload `rxconfig.py` from disk. + The state classes are shared, and their names are cached on them: a + fork installing another resolver renames them for every context until + the original one is installed again (see ``AppHarness.stop``). + Returns: A new RegistrationContext with the same registrations but no app or config. """ @@ -161,8 +334,10 @@ def fork(self) -> Self: base_state_substates={ k: set(v) for k, v in self.base_state_substates.items() }, + _states_by_default_name=dict(self._states_by_default_name), decorated_pages=list(self.decorated_pages), bundled_libraries=list(self.bundled_libraries), + name_resolver=self.name_resolver, _explicit_bundled_libraries=dict(self._explicit_bundled_libraries), ) @@ -189,6 +364,15 @@ def ensure_context(cls) -> Self: cls._context_var.set(ctx) return ctx + @classmethod + def try_get(cls) -> Self | None: + """Return the active context, or ``None`` when none is attached. + + Returns: + The registration context instance, or ``None``. + """ + return cls._context_var.get(None) + @classmethod def register_base_state(cls, state_cls: type[BaseState]) -> type[BaseState]: """Register a base state class with its full name. @@ -215,6 +399,7 @@ def _register_base_state(self, state_cls: type[BaseState]) -> type[BaseState]: The registered base state class. """ self.base_states[state_cls.get_full_name()] = state_cls + self._states_by_default_name[state_cls._get_default_full_name()] = state_cls for event_handler in state_cls.event_handlers.values(): self._register_event_handler(event_handler, states=(state_cls,)) if (parent_state := state_cls.get_parent_state()) is not None: @@ -262,10 +447,23 @@ def _register_event_handler( Returns: The registered event handler. - """ - from reflex.utils.format import format_event_handler - full_name = format_event_handler(handler) + Raises: + StateValueError: If another handler of the same state already + resolves to the same name. + """ + full_name = _event_handler_key(handler) + existing = self.event_handlers.get(full_name) + # An entry of another class is replaced, as on a module reload. + if existing is not None and existing.handler.state is handler.state: + existing_name = existing.handler.fn.__qualname__.rpartition(".")[2] + name = handler.fn.__qualname__.rpartition(".")[2] + if existing_name != name: + msg = ( + f"Event handlers '{existing_name}' and '{name}' both resolve to " + f"'{full_name}'. Run 'reflex minify sync' to give each its own id." + ) + raise StateValueError(msg) self.event_handlers[full_name] = RegisteredEventHandler( handler=handler, states=states ) @@ -287,3 +485,158 @@ def get_substates( return self.base_state_substates.setdefault( base_state_cls.get_full_name(), set() ) + + def _get_state_by_default_name(self, default_full_name: str) -> type[BaseState]: + """Look up a registered state by its resolver-independent full name. + + Args: + default_full_name: The state's ``_get_default_full_name()``. + + Returns: + The state class. + + Raises: + ValueError: If no registered state has that name. + """ + try: + return self._states_by_default_name[default_full_name] + except KeyError: + msg = f"No state is registered as {default_full_name!r}." + raise ValueError(msg) from None + + def _get_state_by_name(self, name: str) -> type[BaseState] | None: + """Look up a registered state by either of its full names. + + Args: + name: The state's ``get_full_name()`` or ``_get_default_full_name()``. + + Returns: + The state class, or ``None`` if no registered state has that name. + """ + return self._states_by_default_name.get(name) or self.base_states.get(name) + + @staticmethod + def default_state_name(state_cls: type[BaseState]) -> str: + """Compute the built-in snake-cased ``module___ClassName`` for a state. + + Args: + state_cls: The state class. + + Returns: + The default name. + """ + from reflex.utils import format + + module = state_cls.__module__.replace(".", "___") + return format.to_snake_case(f"{module}___{state_cls.__name__}") + + def get_state_name(self, state_cls: type[BaseState]) -> str: + """Resolve the user-visible name for a state class. + + Args: + state_cls: The state class. + + Returns: + The resolved name (or :meth:`default_state_name` fallback). + """ + resolved = self.name_resolver.resolve_state_name(state_cls) + if resolved is not None: + return resolved + return self.default_state_name(state_cls) + + def get_handler_name(self, state_cls: type[BaseState], handler_name: str) -> str: + """Resolve the user-visible name for an event handler. + + Args: + state_cls: The state class the handler is attached to. + handler_name: The original (Python) name of the handler. + + Returns: + The resolved name (or ``handler_name`` unchanged). + """ + resolved = self.name_resolver.resolve_handler_name(state_cls, handler_name) + if resolved is not None: + return resolved + return handler_name + + def set_name_resolver(self, resolver: NameResolver) -> None: + """Install ``resolver`` and rebuild the registry under the new names. + + Clears the shared name caches, rebuilds the Vars every registered + state holds (parents first) and calls :meth:`refresh_keys`. Uses + ``object.__setattr__`` to mutate the frozen ``name_resolver`` slot. + A built-in resolver giving the names already in use is only swapped in. + + A Var built from a state's Vars before the install -- say by a + module-level component -- keeps the names it was built with; the + compiler rejects a frontend that still refers to a renamed state. + + Args: + resolver: The resolver to install. Pass :class:`DefaultNameResolver` + to revert to built-in names. + """ + global _names_built_under + from reflex_base.utils.format import ( + _note_names_changed, + format_state_local, + format_var_key, + ) + + previous = self.name_resolver + object.__setattr__(self, "name_resolver", resolver) + # The names are cached on classes every context shares, so the last + # install anywhere must give them alike too. + if _same_names(previous, resolver) and _same_names( + _names_built_under, resolver + ): + _names_built_under = resolver + return + _note_names_changed() + # Each cache is shared by all state classes, so any one clears them all. + if (cls := next(iter(self.base_states.values()), None)) is not None: + cls.get_name.cache_clear() + cls.get_full_name.cache_clear() + cls._get_substate_path.cache_clear() + cls.get_class_substate.cache_clear() + format_state_local.cache_clear() + format_var_key.cache_clear() + for cls in sorted( + self.base_states.values(), + key=lambda state_cls: state_cls._get_default_full_name().count("."), + ): + cls._rebuild_vars() + self.refresh_keys() + _names_built_under = resolver + + def refresh_keys(self) -> None: + """Re-key the name-keyed dicts using current ``get_full_name`` values. + + Built atomically: the replacement dicts are populated before any of + ``self`` is mutated, so a partway failure (e.g. malformed minify.json + making the resolver raise) leaves the existing registry intact. A + console warning is emitted on full-name collisions — usually a sign of + duplicate ids in ``minify.json``. + """ + new_base_states = _rekey( + self.base_states.values(), lambda c: c.get_full_name(), "state class" + ) + # Walk the live tree: a module reload drops a retired state from it but + # leaves the class in base_states. + new_substates: dict[str, set[type[BaseState]]] = {} + for substates in self.base_state_substates.values(): + for cls in substates: + parent = cls.get_parent_state() + if parent is not None: + new_substates.setdefault(parent.get_full_name(), set()).add(cls) + new_handlers = _rekey( + self.event_handlers.values(), + lambda r: _event_handler_key(r.handler), + "event handler", + ) + + self.base_states.clear() + self.base_states.update(new_base_states) + self.base_state_substates.clear() + self.base_state_substates.update(new_substates) + self.event_handlers.clear() + self.event_handlers.update(new_handlers) diff --git a/packages/reflex-base/src/reflex_base/utils/format.py b/packages/reflex-base/src/reflex_base/utils/format.py index bf31c0d5ee4..e100cda5e69 100644 --- a/packages/reflex-base/src/reflex_base/utils/format.py +++ b/packages/reflex-base/src/reflex_base/utils/format.py @@ -6,19 +6,23 @@ import json import os import re -from collections.abc import Callable -from functools import lru_cache +import weakref +from collections.abc import Callable, Collection, Mapping +from functools import cache, lru_cache from typing import TYPE_CHECKING, Any from rich.markup import escape as escape_markup from reflex_base import constants +from reflex_base.constants.state import FIELD_MARKER +from reflex_base.registry import DefaultNameResolver, RegistrationContext from reflex_base.utils import console from reflex_base.utils.types import is_non_string_sequence if TYPE_CHECKING: from typing_extensions import deprecated + from reflex.state import BaseState from reflex_base.components.component import ComponentStyle from reflex_base.event import EventChain, EventHandler, EventSpec, EventType from reflex_base.utils.types import ArgsSpec @@ -436,43 +440,71 @@ def format_props(*single_props, **key_value_props) -> list[str]: def get_event_handler_parts(handler: EventHandler) -> tuple[str, str]: - """Get the state and function name of an event handler. + """Get the (state, function) name pair for an event handler. + + Both names pass through the active + :class:`~reflex_base.registry.NameResolver`, so any installed rewrite + (minification, prefixing, etc.) is applied transparently. Args: - handler: The event handler to get the parts of. + handler: The event handler. Returns: - The state and function name. + ``(state_full_name, handler_name)`` — both resolved. """ - # Get the name of the event function. name = handler.fn.__qualname__ - - # Get the state full name - state_full_name = handler.state.get_full_name() if handler.state else "" - - # If there's no enclosing state, just return the full name. if handler.state is None: return ("", name) - # Get the event name inside the state. + state_full_name = handler.state.get_full_name() func_name = name.rpartition(".")[2] + ctx = RegistrationContext.try_get() + if ctx is None or type(ctx.name_resolver) is DefaultNameResolver: + return (state_full_name, func_name) + return (state_full_name, ctx.get_handler_name(handler.state, func_name)) + + +def _event_handler_full_name(handler: EventHandler) -> str: + """Get the full name events address a handler by under the active resolver. - return (state_full_name, func_name) + Args: + handler: The event handler. + + Returns: + ``state.handler`` for a handler of a state, else its bare name. + """ + state, name = get_event_handler_parts(handler) + return name if handler.state is None else f"{state}.{name}" def format_event_handler(handler: EventHandler) -> str: """Format an event handler. + Cached on the handler instance under ``_formatted_name`` with the resolver + it was formatted under, to skip the registry/resolver dispatch on every + event while any copy of the handler still follows a resolver change. Each + name it computes counts as handed out, see :func:`issued_event_handlers`. + Args: handler: The event handler to format. Returns: The formatted function. """ - state, name = get_event_handler_parts(handler) - if state == "": - return name - return f"{state}.{name}" + ctx = RegistrationContext.try_get() + resolver = None if ctx is None else ctx.name_resolver + cached = handler._formatted_name + if cached is not None and cached[0] is resolver: + return cached[1] + full = _event_handler_full_name(handler) + if handler.state is not None: + if (issued := _issued_event_handlers.get(full)) is None: + issued = _issued_event_handlers[full] = weakref.WeakKeyDictionary() + issued.setdefault(handler.state, set()).add( + handler.fn.__qualname__.rpartition(".")[2] + ) + object.__setattr__(handler, "_formatted_name", (resolver, full)) + return full def format_event(event_spec: EventSpec) -> str: @@ -639,6 +671,146 @@ def format_state_name(state_name: str) -> str: return state_name.replace(".", "__") +# Every state local, var key and event name handed out under any resolver +# installed so far, so code built under an earlier one can be told from +# look-alike text. +_issued_state_locals: dict[str, weakref.WeakSet[type[BaseState]]] = {} +_issued_var_keys: weakref.WeakKeyDictionary[type[BaseState], dict[str, str]] = ( + weakref.WeakKeyDictionary() +) +_issued_event_handlers: dict[ + str, weakref.WeakKeyDictionary[type[BaseState], set[str]] +] = {} +# Whether an install may have changed a name after any was handed out; until +# then, compiled code cannot hold a stale one. +_renamed_after_issue = False + + +# The prefix of a renamed state's local; see ``format_state_local``. +_RENAMED_STATE_LOCAL_PREFIX = "$rx_" + +# A state's local, in either shape ``format_state_local`` gives it, and the +# member read off it; a built-in ``module___ClassName`` name has the ``___``. +STATE_MEMBER_READ = re.compile( + rf"(? Collection[type[BaseState]]: + """Get every state a local was handed out for, under any resolver. + + Args: + local: The local, as :func:`format_state_local` returns it. + + Returns: + The states, none if the local was never handed out. + """ + return _issued_state_locals.get(local, ()) + + +def issued_var_keys(state_cls: type[BaseState]) -> Mapping[str, str]: + """Get every key a state's vars were handed out under, by any resolver. + + Args: + state_cls: The state. + + Returns: + ``{key: var name}``, the name the key was last handed out for. + """ + return _issued_var_keys.get(state_cls, {}) + + +def issued_event_handlers(name: str) -> Mapping[type[BaseState], Collection[str]]: + """Get every handler an event name was handed out for, under any resolver. + + Only :func:`format_event_handler` hands names out, to compiled code and to + the wire; the registry's own keys do not count. + + Args: + name: The full event name, as :func:`format_event_handler` returns it. + + Returns: + ``{state: handler names}``, empty if the name was never handed out. + """ + return _issued_event_handlers.get(name, {}) + + +def _note_names_changed() -> None: + """Record an install that may change names; see :func:`names_may_be_stale`.""" + global _renamed_after_issue + if _issued_state_locals or _issued_var_keys or _issued_event_handlers: + _renamed_after_issue = True + + +def names_may_be_stale() -> bool: + """Whether an install may have changed a name after it was handed out. + + Returns: + ``False`` while every name handed out is still the one in use. + """ + return _renamed_after_issue + + +@cache +def format_state_local(state_cls: type[BaseState]) -> str: + """Get the local JavaScript variable a component reads a state's context into. + + A resolver's names (e.g. minified ``a``) are short enough to clash with + other identifiers in the component, so they get a prefix; the built-in + ``module___ClassName`` names cannot clash and are used as they are. + Cleared by :meth:`reflex_base.registry.RegistrationContext.set_name_resolver`. + + Args: + state_cls: The state. + + Returns: + The name of the local variable. + """ + full_name = state_cls.get_full_name() + local = format_state_name(full_name) + if full_name != state_cls._get_default_full_name(): + local = f"{_RENAMED_STATE_LOCAL_PREFIX}{local}" + _issued_state_locals.setdefault(local, weakref.WeakSet()).add(state_cls) + return local + + +@cache +def format_var_key(state_cls: type[BaseState], var_name: str) -> str: + """Get the key a state var goes by in deltas and in the compiled frontend. + + The one place a var's wire key is built. Cleared by + :meth:`reflex_base.registry.RegistrationContext.set_name_resolver`. + + Args: + state_cls: The state the var belongs to. + var_name: The var's Python name. + + Returns: + The var's resolved name, else ``var_name`` with the field marker. + """ + ctx = RegistrationContext.try_get() + resolved = ( + None if ctx is None else ctx.name_resolver.resolve_var_name(state_cls, var_name) + ) + key = var_name + FIELD_MARKER if resolved is None else resolved + _issued_var_keys.setdefault(state_cls, {})[key] = var_name + return key + + +def format_state_var(state_cls: type[BaseState], var_name: str) -> str: + """Get the JavaScript expression reading a state var in the compiled frontend. + + Args: + state_cls: The state the var belongs to. + var_name: The var's Python name. + + Returns: + The var's key on the local its state's context value is read into. + """ + return f"{format_state_local(state_cls)}.{format_var_key(state_cls, var_name)}" + + # The marker Var.__format__ puts before a Var's expression in an f-string. _VAR_MARKER = re.compile( re.escape(constants.REFLEX_VAR_OPENING_TAG) diff --git a/packages/reflex-base/src/reflex_base/vars/base.py b/packages/reflex-base/src/reflex_base/vars/base.py index 1e82c09d127..457a0bcd949 100644 --- a/packages/reflex-base/src/reflex_base/vars/base.py +++ b/packages/reflex-base/src/reflex_base/vars/base.py @@ -50,6 +50,7 @@ from reflex_base.constants.compiler import Hooks from reflex_base.constants.state import FIELD_MARKER from reflex_base.environment import _on_env_var_set, environment +from reflex_base.registry import RegistrationContext from reflex_base.utils import console, exceptions, imports, serializers, types from reflex_base.utils.compat import MISSING_TYPE, annotations_from_namespace from reflex_base.utils.decorator import once @@ -65,7 +66,7 @@ VarDependencyError, VarTypeError, ) -from reflex_base.utils.format import format_state_name, json_dumps +from reflex_base.utils.format import format_state_local, format_state_var, json_dumps from reflex_base.utils.imports import ( ImmutableImportDict, ImmutableParsedImportDict, @@ -297,6 +298,23 @@ def insert_app_wraps( target[key] = wrapper +def _registered_state(state: type[BaseState] | str) -> type[BaseState] | str: + """Resolve a registered state's full name, resolved or default, to its class. + + The two names differ once a resolver renames the state, and only the class + knows both, so a name taken as given would break on one of them. + + Args: + state: A state, or the full name of one. + + Returns: + The registered state, else ``state`` unchanged. + """ + if isinstance(state, str) and (ctx := RegistrationContext.try_get()) is not None: + return ctx._get_state_by_name(state) or state + return state + + def _normalize_field_dependencies( field_dependencies: Mapping[str, Iterable[str]] | None, state: str, @@ -619,8 +637,12 @@ def _cached_hash(self) -> int: def from_state(cls, state: type[BaseState] | str, field_name: str = "") -> VarData: """Set the state of the var. + The recorded state name is the resolver-independent default full name, + which the backend tracks dependencies by; only the hook names the state + the way the frontend does. + Args: - state: The state to set or the full name of the state. + state: The state, or the full name of one. field_name: The name of the field in the state. Optional. Returns: @@ -630,15 +652,24 @@ def from_state(cls, state: type[BaseState] | str, field_name: str = "") -> VarDa from reflex_base.components.state_context import get_event_app_wraps from reflex_base.utils import format - state_name = state if isinstance(state, str) else state.get_full_name() - state_var_name = format.format_state_name(state_name) - hook_args = json.dumps(state_name) + state = _registered_state(state) + if isinstance(state, str): + state_name = store_name = state + local = format.format_state_name(state) + field_key = field_name + FIELD_MARKER + else: + state_name = state._get_default_full_name() + store_name = state.get_full_name() + local = format.format_state_local(state) + # The key the field goes by in the store, which the resolver can rename. + field_key = format.format_var_key(state, field_name) if field_name else "" + hook_args = json.dumps(store_name) if field_name: - hook_args += f", {json.dumps([field_name + FIELD_MARKER])}" + hook_args += f", {json.dumps([field_key])}" return VarData( state=state_name, field_name=field_name, - hooks={f"const {state_var_name} = useStateContext({hook_args})": None}, + hooks={f"const {local} = useStateContext({hook_args})": None}, imports={ f"$/{constants.Dirs.UTILS}/context-registry": [ ImportVar(tag="useStateContext") @@ -1381,15 +1412,14 @@ def _var_set_state(self, state: type[BaseState] | str) -> Self: """Set the state of the var. Args: - state: The state to set. + state: The state, or the full name of one. Returns: The var with the state set. """ + state = _registered_state(state) formatted_state_name = ( - state - if isinstance(state, str) - else format_state_name(state.get_full_name()) + state if isinstance(state, str) else format_state_local(state) ) return StateOperation.create( # pyright: ignore [reportReturnType] @@ -1400,7 +1430,11 @@ def _var_set_state(self, state: type[BaseState] | str) -> Self: _var_data=VarData.merge( VarData.from_state(state), VarData( - state=state if isinstance(state, str) else state.get_full_name(), + state=( + state + if isinstance(state, str) + else state._get_default_full_name() + ), field_name=self._js_expr, ), self._var_data, @@ -3012,14 +3046,8 @@ def __get__(self, instance: Any, owner: type): if instance is None: state_where_defined = self._owner or owner - field_name = ( - format_state_name(state_where_defined.get_full_name()) - + "." - + self._js_expr - ) - return dispatch( - field_name, + format_state_var(state_where_defined, self._name), var_data=VarData.from_state(state_where_defined, self._name), result_var_type=self._var_type, existing_var=self, @@ -3124,7 +3152,7 @@ def _deps( d.update(self._static_deps) # None is a placeholder for the current state class. if None in d: - d[objclass.get_full_name()] = d.pop(None) + d[objclass._get_default_full_name()] = d.pop(None) if not self._auto_deps: return d @@ -3174,24 +3202,19 @@ def add_dependency(self, objclass: type[BaseState], dep: Var): # A composite Var names every state field it is built from, and may # span several states; register against each of them. registered = False + registration_context = RegistrationContext.get() for state_name, field_names in all_var_data.field_dependencies.items(): var_names = tuple(filter(None, field_names)) if not state_name or not var_names: continue self._static_deps.setdefault(state_name, set()).update(var_names) - target_state_class = objclass.get_root_state().get_class_substate( + target_state_class = registration_context._get_state_by_default_name( state_name ) for var_name in var_names: - target_state_class._var_dependencies.setdefault( - var_name, set() - ).add(( - objclass.get_full_name(), - self._name, - )) - target_state_class._potentially_dirty_states.add( - objclass.get_full_name() - ) + objclass._register_var_dependency( + target_state_class, var_name, self._name + ) registered = True if registered: return diff --git a/packages/reflex-base/src/reflex_base/vars/dep_tracking.py b/packages/reflex-base/src/reflex_base/vars/dep_tracking.py index 733b5274820..0bfee77519f 100644 --- a/packages/reflex-base/src/reflex_base/vars/dep_tracking.py +++ b/packages/reflex-base/src/reflex_base/vars/dep_tracking.py @@ -226,9 +226,9 @@ def load_attr_or_method(self, instruction: dis.Instruction) -> None: type(self)(func=cast(FunctionType, ref_obj), state_cls=target_state) ) elif is_dependency(target_state, instruction.argval): - self.dependencies.setdefault(target_state.get_full_name(), set()).add( - instruction.argval - ) + self.dependencies.setdefault( + target_state._get_default_full_name(), set() + ).add(instruction.argval) def _get_globals(self) -> dict[str, Any]: """Get the globals of the function. diff --git a/packages/reflex-components-core/news/6100.bugfix.md b/packages/reflex-components-core/news/6100.bugfix.md new file mode 100644 index 00000000000..ce987c5aa9c --- /dev/null +++ b/packages/reflex-components-core/news/6100.bugfix.md @@ -0,0 +1 @@ +`REFLEX_PERF_MODE` detects a state `Var` rendered into a plain string by its trailing field marker rather than by the built-in state name prefix, so it no longer flags unrelated strings that merely start with `reflex___state`, and keeps working when state names are minified. diff --git a/packages/reflex-components-core/news/7337.bugfix.md b/packages/reflex-components-core/news/7337.bugfix.md new file mode 100644 index 00000000000..7b9e48a839d --- /dev/null +++ b/packages/reflex-components-core/news/7337.bugfix.md @@ -0,0 +1 @@ +Fix `REFLEX_PERF_MODE` not flagging state Vars rendered as strings when `REFLEX_MINIFY_VARS` shortens their keys. diff --git a/packages/reflex-components-core/src/reflex_components_core/base/bare.py b/packages/reflex-components-core/src/reflex_components_core/base/bare.py index 058952c124a..309dd710477 100644 --- a/packages/reflex-components-core/src/reflex_components_core/base/bare.py +++ b/packages/reflex-components-core/src/reflex_components_core/base/bare.py @@ -11,6 +11,7 @@ from reflex_base.components.tags.tagless import Tagless from reflex_base.environment import PerformanceMode, environment from reflex_base.utils.decorator import once +from reflex_base.utils.format import STATE_MEMBER_READ, issued_states, issued_var_keys from reflex_base.utils.imports import ParsedImportDict from reflex_base.vars import BooleanVar, ObjectVar, Var from reflex_base.vars.base import GLOBAL_CACHE, VarData @@ -39,7 +40,15 @@ def validate_str(value: str): ValueError: If the value is a Var and the performance mode is set to raise. """ perf_mode = get_performance_mode() - if perf_mode != PerformanceMode.OFF and value.startswith("reflex___state"): + if ( + perf_mode != PerformanceMode.OFF + # A stringified state Var opens with its state's local and the var's + # key, both of which minify.json may rewrite, so they are told by + # having been handed out. + and (match := STATE_MEMBER_READ.match(value)) is not None + and match[2] is not None + and any(match[2] in issued_var_keys(s) for s in issued_states(match[1])) + ): if perf_mode == PerformanceMode.WARN: logger.warning( f"Output includes {value!s} which will be displayed as a string. If you are calling `str` on a Var, consider using .to_string() instead." diff --git a/packages/reflex-components-core/src/reflex_components_core/core/_upload.py b/packages/reflex-components-core/src/reflex_components_core/core/_upload.py index ddbe15ebb1e..75940b2ffe0 100644 --- a/packages/reflex-components-core/src/reflex_components_core/core/_upload.py +++ b/packages/reflex-components-core/src/reflex_components_core/core/_upload.py @@ -18,7 +18,7 @@ from typing import TYPE_CHECKING, Any, BinaryIO, Self, cast from python_multipart.multipart import MultipartParser, parse_options_header -from reflex_base.registry import RegistrationContext +from reflex_base.registry import RegistrationContext, scheme_digest from reflex_base.utils import exceptions from reflex_base.utils.format import json_dumps from reflex_base.utils.streaming_response import DisconnectAwareStreamingResponse @@ -502,7 +502,7 @@ async def parse(self) -> None: def _require_upload_headers(request: Request) -> tuple[str, str]: - """Extract the required upload headers from a request. + """Extract and validate the required upload headers from a request. Args: request: The incoming request. @@ -511,7 +511,8 @@ def _require_upload_headers(request: Request) -> tuple[str, str]: The client token and event handler name. Raises: - HTTPException: If the upload headers are missing. + HTTPException: If the upload headers are missing, or the caller was + built against a different wire-name scheme. """ token = request.headers.get("reflex-client-token") handler = request.headers.get("reflex-event-handler") @@ -522,6 +523,20 @@ def _require_upload_headers(request: Request) -> tuple[str, str]: detail="Missing reflex-client-token or reflex-event-handler header.", ) + # Uploads bypass the socket handshake, and the handler name is resolved + # against this backend's scheme, so a name minted by another one can pick + # out a real -- but wrong -- handler here. + client_scheme = request.headers.get("reflex-scheme", "") + server_scheme = scheme_digest() + if client_scheme != server_scheme: + raise HTTPException( + status_code=409, + detail=( + f"Frontend minification scheme {client_scheme!r} does not match " + f"the backend scheme {server_scheme!r}." + ), + ) + return token, handler @@ -894,7 +909,8 @@ async def upload_file(request: Request): UploadValueError: If the handler does not have a supported annotation. UploadTypeError: If a non-streaming upload is wired to a background task. HTTPException: when the request does not include token / handler headers, - or when the handler header does not name a registered event handler. + when the handler header does not name a registered event handler, + or when the caller was built against a different wire-name scheme. """ from reflex_base.event import ( resolve_upload_chunk_handler_param, diff --git a/packages/reflex-components-core/src/reflex_components_core/core/banner.py b/packages/reflex-components-core/src/reflex_components_core/core/banner.py index 6cf4e97a58b..77b5cd6d197 100644 --- a/packages/reflex-components-core/src/reflex_components_core/core/banner.py +++ b/packages/reflex-components-core/src/reflex_components_core/core/banner.py @@ -45,8 +45,18 @@ _js_expr="(connectErrors.length > 0)", _var_data=connect_error_var_data ).to(BooleanVar) +has_fatal_connection_error = Var( + # Indexed access, not Array.prototype.at: a runtime method is not + # downlevelled by the bundler, so it would break older browsers. + _js_expr="((connectErrors.length > 0) && connectErrors[connectErrors.length - 1].fatal === true)", + _var_data=connect_error_var_data, +).to(BooleanVar) + has_too_many_connection_errors = Var( - _js_expr="(connectErrors.length >= 2)", _var_data=connect_error_var_data + # A fatal mismatch never retries, so it skips the transient-error debounce: + # there will never be a second error to cross the threshold. + _js_expr=f"(connectErrors.length >= 2 || {has_fatal_connection_error!s})", + _var_data=connect_error_var_data, ).to(BooleanVar) has_hydration_payload_error = Var( @@ -83,12 +93,17 @@ def default_connection_error() -> list[str | Var | Component]: Returns: The default connection error message. """ - return [ - "Cannot connect to server: ", - connection_error, - ". Check if server is reachable at ", - WebsocketTargetURL.create(), - ] + target_url = WebsocketTargetURL.create() + unreachable = Var( + _js_expr=( + f'("Cannot connect to server: " + {connection_error!s} + ' + f'". Check if server is reachable at " + {target_url!s})' + ), + _var_data=VarData.merge(connect_error_var_data, target_url._get_all_var_data()), + ) + # A fatal mismatch is not a connectivity problem: the server is reachable, + # and the message already says what the viewer should do about it. + return [cond(has_fatal_connection_error, connection_error, unreachable)] class ConnectionToaster(Fragment): @@ -144,6 +159,15 @@ def add_hooks(self) -> list[str | Var]: f"toast?.error({loading_message!s}, {{...toast_props, onDismiss: () => setUserDismissed(true)}},)" ) + # A fatal mismatch is not a connectivity problem: the message carries + # itself and the only fix is a reload. The action must be raw JS, since + # a Reflex event would be dropped by the dead event loop. + fatal_toast_var = Var( + f"toast?.error({connection_error!s}, {{...toast_props, description: '', " + "duration: Infinity, action: {label: 'Reload', onClick: () => " + "window.location.reload()}, onDismiss: () => setUserDismissed(true)},)" + ) + individual_hooks = [ Var(f"const toast = {toast_ref};"), f"const toast_props = {LiteralVar.create(props)!s};", @@ -170,7 +194,11 @@ def add_hooks(self) -> list[str | Var]: toast?.error({connection_error!s}, {{id: "{hydration_toast_id}", closeButton: true, duration: Infinity}}); }} else if ({has_too_many_connection_errors!s}) {{ if (!userDismissed) {{ - {toast_var!s} + if ({has_fatal_connection_error!s}) {{ + {fatal_toast_var!s} + }} else {{ + {toast_var!s} + }} }} }} else {{ toast?.dismiss("{toast_id}"); diff --git a/packages/reflex-frontend/src/utils/helpers/upload.js b/packages/reflex-frontend/src/utils/helpers/upload.js index 32aa03fe3d1..95b6182960e 100644 --- a/packages/reflex-frontend/src/utils/helpers/upload.js +++ b/packages/reflex-frontend/src/utils/helpers/upload.js @@ -1,4 +1,5 @@ import env from "$/env.json"; +import { app } from "$/utils/context-registry"; import { parseJson } from "$/utils/helpers/json"; /** @@ -160,6 +161,8 @@ export const uploadFiles = async ( xhr.open("POST", getBackendURL(env.UPLOAD)); xhr.setRequestHeader("Reflex-Client-Token", getToken()); xhr.setRequestHeader("Reflex-Event-Handler", handler); + // Uploads bypass the socket handshake, so they carry the scheme themselves. + xhr.setRequestHeader("Reflex-Scheme", app.schemeDigest ?? ""); // Instrumentation hook (installed by reflex-otel): may add trace headers. /** @type {Record} */ const trace_headers = {}; diff --git a/packages/reflex-frontend/src/utils/state.js b/packages/reflex-frontend/src/utils/state.js index 7486b1dac04..b4748268d9e 100644 --- a/packages/reflex-frontend/src/utils/state.js +++ b/packages/reflex-frontend/src/utils/state.js @@ -30,6 +30,7 @@ const EVENTURL = env.EVENT; // Socket event names (must match reflex_base/constants/event.py SocketEvent) const CLIENT_ERROR_EVENT = "client_error"; const STATE_MISMATCH_EVENT = "state_mismatch"; +const SCHEME_MISMATCH_EVENT = "scheme_mismatch"; // sessionStorage key with the time of the last automatic reload (must match // LAST_RELOADED_KEY in reflex/state.py: the reload on a frontend error and the @@ -48,6 +49,11 @@ const ERROR_TYPE_DISPATCH_MISSING = "dispatch_function_missing"; const ERROR_TYPE_STATE_UPDATE = "state_update_processing_error"; const ERROR_TYPE_HYDRATION_PAYLOAD_TOO_LARGE = "hydration_payload_too_large"; +// Shown in the connection UI when the backend cannot understand this bundle: +// the only action available to the viewer is a reload. +const OUTDATED_PAGE_MESSAGE = + "This page is out of date and can no longer talk to the server. Reload the page to load the current version."; + // These hostnames indicate that the backend and frontend are reachable via the same domain. // They are compared with URL.hostname, which gives an IPv6 address in brackets // and in its shortest form: "[::]" is every spelling of the IPv6 wildcard. @@ -92,14 +98,15 @@ if (typeof document !== "undefined") { refs[CLIENT_STATE_REF] = getClientStore(); } +// Set when the backend resolves wire names with another minification scheme +// than this bundle was built against. Fatal: no further events are sent or +// applied until the page is reloaded. +let backend_scheme_mismatch = false; // Array holding pending events to be processed. const event_queue = []; -// The event that sends browser storage values to the backend. -const UPDATE_VARS_INTERNAL = - "reflex___state____update_vars_internal_state.update_vars_internal"; // A frontend event naming one synced localStorage var, turned into an -// UPDATE_VARS_INTERNAL event when it is sent: see syncLocalStorageEvent. +// update_vars_internal event when it is sent: see syncLocalStorageEvent. const SYNC_LOCAL_STORAGE = "_sync_local_storage"; // Synced localStorage values sent to the backend and not yet echoed back, by @@ -203,6 +210,20 @@ export const getBackendURL = (url_str) => { return endpoint; }; +/** + * Build the query the backend reads on connect. + * + * Used for reconnects too: a field dropped here reaches the backend as empty + * on every later connection, so the call sites must not drift apart. + * + * @param token The session token the backend links the connection to. + * @returns The socket handshake query. + */ +const handshakeQuery = (token = getToken()) => ({ + token, + scheme: app.schemeDigest ?? "", +}); + /** * Check if the backend is disabled. * @@ -229,7 +250,7 @@ const createSocket = (endpoint, transports, token) => protocols: [env.REFLEX_VERSION], autoUnref: false, autoConnect: false, - query: { token }, + query: handshakeQuery(token), reconnection: false, }); @@ -312,10 +333,12 @@ export const isStateful = () => { if (event_queue.length === 0) { return false; } + // State events are `.`; the trailing dot keeps a + // frontend-only event from matching a short (minified) root state name. return event_queue.some( (event) => typeof event?.name === "string" && - (event.name.startsWith("reflex___state") || + (event.name.startsWith(app.main_state_name + ".") || event.name === SYNC_LOCAL_STORAGE), ); }; @@ -601,7 +624,7 @@ const applyResultCallback = async ( */ const syncLocalStorageEvent = ({ key, state_keys }) => { const value = localStorage.getItem(key); - return ReflexEvent(`${app.state_name}.${UPDATE_VARS_INTERNAL}`, { + return ReflexEvent(app.update_vars_internal, { vars: Object.fromEntries( state_keys.map((state_key) => [ state_key, @@ -987,6 +1010,14 @@ export const processEvent = async (socket, navigate, params) => { return; } + // A scheme mismatch is fatal; do not send further events. Drop pending + // events too: callers drain the queue in while-loops that would otherwise + // spin forever on an early return. + if (backend_scheme_mismatch) { + event_queue.length = 0; + return; + } + // Only proceed if we're not already processing an event. if (event_queue.length === 0) { return; @@ -1197,13 +1228,17 @@ export const connect = async ( }; // Set up a reconnect helper function socket.current.reconnect = () => { + if (backend_scheme_mismatch) { + // Reconnecting cannot resolve a scheme mismatch. + return; + } if ( socket.current && !socket.current.connected && !socket.current.wait_connect ) { socket.current.wait_connect = true; - socket.current.io.opts.query = { token: getToken() }; // Update token for reconnect. + socket.current.io.opts.query = handshakeQuery(); // Refresh token/scheme. // A reconnect rehydrates in full: the reducers no longer hold the defaults. socket.current.auth = bootAuth(false); socket.current.connect(); @@ -1260,7 +1295,15 @@ export const connect = async ( failed_attempts = 0; window.clearTimeout(retry_timer); retry_timer = undefined; - setConnectErrors([]); + // A fatal mismatch is emitted from the server's connect handler, so it is + // buffered and replayed before this runs; clearing it here would discard + // the only notice the viewer gets. It also outlives a reconnect, since + // reconnecting cannot change what the names mean. + setConnectErrors((connectErrors) => + connectErrors.length > 0 && connectErrors[connectErrors.length - 1].fatal + ? connectErrors + : [], + ); window.__reflex_otel?.onSocketConnect?.(); window.addEventListener("pagehide", pagehideHandler); window.addEventListener("beforeunload", disconnectTrigger); @@ -1302,8 +1345,9 @@ export const connect = async ( socket.current.on("disconnect", (reason, details) => { // A transport closed before the namespace connected is a failed attempt, - // e.g. a CONNECT packet over the server or a proxy message size limit. - if (socket.current.wait_connect) { + // e.g. a CONNECT packet over the server or a proxy message size limit. A + // client disconnect is deliberate, like a fatal mismatch replayed early. + if (socket.current.wait_connect && reason !== "io client disconnect") { retryConnect( new Error(`connection closed before connecting (${reason})`), ); @@ -1334,6 +1378,31 @@ export const connect = async ( }); }; + // A mismatch is terminal: no event will ever be answered again. Push it onto + // connectErrors as well, or the page sits dead with the reason only in the + // console, where no end user will look. + const fatalMismatch = (developerMessage) => { + backend_scheme_mismatch = true; + event_queue.length = 0; + console.error(developerMessage); + setConnectErrors((connectErrors) => [ + ...connectErrors.slice(-9), + Object.assign(new Error(OUTDATED_PAGE_MESSAGE), { fatal: true }), + ]); + // A dead tab must not hold a live server connection; "io client + // disconnect" keeps the reconnect helpers out of it. + socket.current?.disconnect(); + }; + + // The backend resolves wire names with its own copy of the minification + // scheme. If it disagrees with the one this bundle was built against, every + // name we send is meaningless to it, so stop before the first event. + socket.current.on(SCHEME_MISMATCH_EVENT, (detail) => { + fatalMismatch( + `Cannot talk to the backend: it resolves state and event names with a different minification scheme (frontend "${detail?.frontend ?? ""}", backend "${detail?.backend ?? ""}"). If you are the developer of this app, rebuild the frontend against the same minify.json the backend is running.`, + ); + }); + // Warn about unknown substates and surface them in the backend terminal // logs, once per substate per connection. const reportUnknownSubstates = (substates) => { @@ -1380,6 +1449,10 @@ export const connect = async ( // On each received message, queue the updates and events. socket.current.on("event", (update) => { + if (backend_scheme_mismatch) { + // Its names mean something else in this bundle. + return; + } // Skip unknown substates in both reducer and client-storage updates. const delta = withoutUnknownSubstates(update.delta); try { @@ -1547,8 +1620,8 @@ const recordSentStorageValues = (event) => { const vars = event.payload?.vars; if ( vars === undefined || - (event.name !== `${app.state_name}.hydrate_and_load` && - event.name !== `${app.state_name}.${UPDATE_VARS_INTERNAL}`) + (event.name !== app.hydrate_and_load && + event.name !== app.update_vars_internal) ) { return; } @@ -1591,8 +1664,8 @@ const applyClientStorageDelta = (client_storage, delta) => { if (unqualified_states.length === 1) { const main_state = delta[unqualified_states[0]]; if ( - main_state.is_hydrated_rx_state_ !== undefined && - !main_state.is_hydrated_rx_state_ + main_state[app.is_hydrated_key] !== undefined && + !main_state[app.is_hydrated_key] ) { // skip if the state is not hydrated yet, since all client storage // values are sent in the hydrate event @@ -1756,33 +1829,39 @@ export const useEventLoop = ( return; } + // A stateless app exports no handler name, and an event without one + // reaches nothing on the backend. + const reportException = (info) => { + if (app.handle_frontend_exception) { + addEvents([ + ReflexEvent(app.handle_frontend_exception, { + info, + component_stack: "", + }), + ]); + } + }; + window.onerror = function (msg, url, lineNo, columnNo, error) { - addEvents([ - ReflexEvent(`${app.exception_state_name}.handle_frontend_exception`, { - // Some error events (e.g. a ResizeObserver loop) have no Error object. - info: error - ? error.name + ": " + error.message + "\n" + error.stack - : msg + "\n at " + url + ":" + lineNo + ":" + columnNo, - component_stack: "", - }), - ]); + // Some error events (e.g. a ResizeObserver loop) have no Error object. + reportException( + error + ? error.name + ": " + error.message + "\n" + error.stack + : msg + "\n at " + url + ":" + lineNo + ":" + columnNo, + ); return false; }; //NOTE: Only works in Chrome v49+ //https://github.com/mknichel/javascript-errors?tab=readme-ov-file#promise-rejection-events window.onunhandledrejection = function (event) { - addEvents([ - ReflexEvent(`${app.exception_state_name}.handle_frontend_exception`, { - info: - event.reason?.name + - ": " + - event.reason?.message + - "\n" + - event.reason?.stack, - component_stack: "", - }), - ]); + reportException( + event.reason?.name + + ": " + + event.reason?.message + + "\n" + + event.reason?.stack, + ); return false; }; }, []); @@ -1896,9 +1975,9 @@ export const useEventLoop = ( } // Equivalent to routeChangeStart - runs when navigation begins - const main_state_dispatch = dispatch["reflex___state____state"]; + const main_state_dispatch = dispatch[app.main_state_name]; if (main_state_dispatch !== undefined) { - main_state_dispatch({ is_hydrated_rx_state_: false }); + main_state_dispatch({ [app.is_hydrated_key]: false }); } // Equivalent to routeChangeComplete - runs after navigation completes @@ -2046,7 +2125,11 @@ export const pyStrLength = (s) => codePoints(s).length; * @param {number} i The index; negative counts from the end. * @returns {string | undefined} The code point, or undefined out of range. */ -export const pyStrAt = (s, i) => codePoints(s).at(i); +export const pyStrAt = (s, i) => { + const chars = codePoints(s); + // Indexed rather than with `.at`, which older browsers lack. + return chars[i < 0 ? chars.length + i : i]; +}; /*** * Python-semantics s[start:stop]: slice by code point. diff --git a/packages/reflex-frontend/tests/state_connection.test.js b/packages/reflex-frontend/tests/state_connection.test.js index b8d40202803..103e7acac16 100644 --- a/packages/reflex-frontend/tests/state_connection.test.js +++ b/packages/reflex-frontend/tests/state_connection.test.js @@ -104,8 +104,9 @@ beforeEach(async () => { initialEvents: () => [], onLoadInternalEvent: () => [], on_load_internal_event: "on_load_internal", - state_name: "reflex___state____state", - exception_state_name: "reflex___state____frontend_event_exception_state", + main_state_name: "reflex___state____state", + handle_frontend_exception: + "reflex___state____state.reflex___state____frontend_event_exception_state.handle_frontend_exception", stateSchema: "schema", clientStorage: {}, }); diff --git a/pyi_hashes.json b/pyi_hashes.json index 8f2b4c8aa7a..6b724a8b9a6 100644 --- a/pyi_hashes.json +++ b/pyi_hashes.json @@ -14,7 +14,7 @@ "packages/reflex-components-core/src/reflex_components_core/base/strict_mode.pyi": "135dd395a27f01be303b3879ece179c1", "packages/reflex-components-core/src/reflex_components_core/core/__init__.pyi": "931b222daf3755f67281b6c4d7155f85", "packages/reflex-components-core/src/reflex_components_core/core/auto_scroll.pyi": "2d83dc211884423496c31d372d3f3734", - "packages/reflex-components-core/src/reflex_components_core/core/banner.pyi": "85ff5f20fc7a59de5544858cfc364cf1", + "packages/reflex-components-core/src/reflex_components_core/core/banner.pyi": "8ebbd06dfe05b166221216a290d244d1", "packages/reflex-components-core/src/reflex_components_core/core/clipboard.pyi": "21d51b69ab11279e864f7aca9307462f", "packages/reflex-components-core/src/reflex_components_core/core/debounce.pyi": "86b2bea0aeec5334dbb64b3addae91df", "packages/reflex-components-core/src/reflex_components_core/core/helmet.pyi": "a43533eba05dec1ece2602bd64ca0f01", diff --git a/reflex/app.py b/reflex/app.py index 3d9229c599c..631b9018c11 100644 --- a/reflex/app.py +++ b/reflex/app.py @@ -52,7 +52,7 @@ ) from reflex_base.event.context import EventContext from reflex_base.event.processor import BaseStateEventProcessor, EventProcessor -from reflex_base.registry import RegistrationContext +from reflex_base.registry import RegistrationContext, scheme_digest from reflex_base.telemetry_context import CompileTrigger, TelemetryContext from reflex_base.utils import memo_paths from reflex_base.utils.imports import ImportVar @@ -2209,8 +2209,8 @@ def _validate_var_dependencies(self, state: type[BaseState] | None = None) -> No deps = var._deps(objclass=state) for state_name, dep_set in deps.items(): state_cls = ( - state.get_root_state().get_class_substate(state_name) - if state_name != state.get_full_name() + self._registration_context._get_state_by_default_name(state_name) + if state_name != state._get_default_full_name() else state ) for dep in dep_set: @@ -2678,6 +2678,9 @@ def __init__(self, namespace: str, app: App): # Number of client_error reports logged per SID, for rate limiting. self._client_error_counts: dict[str, int] = {} + # SIDs whose bundle resolves names with a different scheme than ours. + self._scheme_mismatch_sids: set[str] = set() + # Connection-scoped router_data entries per SID, computed once at # connect time instead of for every event on the connection. self._static_router_data: dict[str, dict[str, Any]] = {} @@ -2724,9 +2727,29 @@ async def on_connect(self, sid: str, environ: dict, auth: Any = None): puts its hydrate event here so it is processed without waiting for the connect acknowledgement round trip. """ + query_params = urllib.parse.parse_qs(environ.get("QUERY_STRING", "")) + # Per-SID state goes in before the first await: a disconnect landing + # during one would otherwise clean up first and leave it behind. A + # connect that raises keeps its socket, so on_disconnect still runs. + if otel.enabled: + # on_disconnect decrements unconditionally. + otel.record_connection(1) + # Headers, client IP, and session id cannot change for the lifetime of + # the connection; compute them once instead of on every event. + self._static_router_data[sid] = self._build_static_router_data(sid, environ) + client_scheme = next(iter(query_params.get("scheme", [])), "") + server_scheme = scheme_digest() + if scheme_mismatch := client_scheme != server_scheme: + # The client queues its initial events as soon as it sees CONNECT, + # and only learns of the mismatch a tick later, so drop whatever it + # sends meanwhile: a name from the other scheme could resolve to a + # real -- but wrong -- handler here. + self._scheme_mismatch_sids.add(sid) if isinstance(self._token_manager, RedisTokenManager): - # Make sure this instance is watching for updates from other instances. + # Make sure this instance is watching for updates from other + # instances before link_token_to_sid stores the socket record. await self._token_manager.ensure_lost_and_found_subscribed(self.emit_update) + boot_event = frontend_schema = None if isinstance(auth, dict): boot_event = auth.get(constants.CompileVars.CONNECT_AUTH_EVENT) @@ -2756,7 +2779,6 @@ async def on_connect(self, sid: str, environ: dict, auth: Any = None): }, to=sid, ) - query_params = urllib.parse.parse_qs(environ.get("QUERY_STRING", "")) token_list = query_params.get("token", []) if token_list: # The boot event is the connection's first event: processing it @@ -2772,12 +2794,19 @@ async def on_connect(self, sid: str, environ: dict, auth: Any = None): logger.warning( f"Frontend version {subprotocol} for session {sid} does not match the backend version {constants.Reflex.VERSION}." ) - if otel.enabled: - otel.record_connection(1) - # Headers, client IP, and session id cannot change for the lifetime of - # the connection; compute them once instead of on every event. - self._static_router_data[sid] = self._build_static_router_data(sid, environ) + # Unlike the version check above, a scheme mismatch is fatal: every name + # the client sends would resolve to the wrong handler, or to none. + if scheme_mismatch: + logger.warning( + f"Frontend minification scheme {client_scheme!r} for session {sid} " + f"does not match the backend scheme {server_scheme!r}." + ) + await self.emit( + str(constants.SocketEvent.SCHEME_MISMATCH), + {"frontend": client_scheme, "backend": server_scheme}, + to=sid, + ) if boot_event is not None: try: @@ -2844,6 +2873,7 @@ def on_disconnect(self, sid: str) -> asyncio.Task | None: if otel.enabled: otel.record_connection(-1) self._client_error_counts.pop(sid, None) + self._scheme_mismatch_sids.discard(sid) self._static_router_data.pop(sid, None) # Get token before cleaning up disconnect_token = self.sid_to_token.get(sid) @@ -2918,6 +2948,11 @@ async def on_event(self, sid: str, data: Any): RuntimeError: If the Socket.IO is badly initialized. EventDeserializationError: If the event data is not a dictionary. """ + if sid in self._scheme_mismatch_sids: + # Its names mean something else here; the client stops on its own + # once it processes the notice sent at connect. + return + # Determine the token for this SID if (token := self.sid_to_token.get(sid)) is None: logger.warning( diff --git a/reflex/compiler/compiler.py b/reflex/compiler/compiler.py index 83d72e5dd12..0956324a301 100644 --- a/reflex/compiler/compiler.py +++ b/reflex/compiler/compiler.py @@ -32,10 +32,14 @@ from reflex_base.constants.state import FIELD_MARKER from reflex_base.environment import environment from reflex_base.plugins import CompileContext, CompilerHooks, PageContext, Plugin -from reflex_base.registry import RegistrationContext, _default_bundled_libraries +from reflex_base.registry import ( + RegistrationContext, + _default_bundled_libraries, + scheme_digest, +) from reflex_base.utils import log, memo_paths from reflex_base.utils.exceptions import ReflexError -from reflex_base.utils.format import to_title_case +from reflex_base.utils.format import format_event_handler, format_var_key, to_title_case from reflex_base.utils.imports import ( ABSOLUTE_IMPORT_PREFIXES, ImportVar, @@ -51,7 +55,16 @@ from reflex.compiler import templates, utils from reflex.compiler.plugins import default_page_plugins from reflex.compiler.plugins.memoize import MemoizeStatefulPlugin, scope_memo_body_hooks -from reflex.state import BaseState, code_uses_state_contexts, state_snapshot_hashes +from reflex.minify import raise_for_stale_names, warn_if_config_stale +from reflex.state import ( + BaseState, + FrontendEventExceptionState, + OnLoadInternalState, + State, + UpdateVarsInternalState, + code_uses_state_contexts, + state_snapshot_hashes, +) from reflex.utils import console, frontend_skeleton, path_ops, prerequisites from reflex.utils.exec import get_compile_context, is_prod_mode from reflex.utils.prerequisites import get_web_dir @@ -240,6 +253,39 @@ def _resolve_default_color_mode(theme: Component | None) -> str: return get_config().default_color_mode +def _event_name(state_cls: type[BaseState], handler_name: str) -> str: + """Resolve the wire name of a handler defined on a state class. + + Args: + state_cls: The state class declaring the handler. + handler_name: The handler's Python name. + + Returns: + The event name under the active name resolver. + """ + return format_event_handler(state_cls.event_handlers[handler_name]) + + +def _internal_names() -> templates.InternalNames: + """Resolve the framework names the context module reads and dispatches. + + Returns: + The names under the active name resolver. + """ + return templates.InternalNames( + main_state_name=State.get_name(), + is_hydrated_key=format_var_key(State, constants.CompileVars.IS_HYDRATED), + hydrate_and_load=_event_name(State, constants.CompileVars.HYDRATE_AND_LOAD), + on_load_internal=_event_name(OnLoadInternalState, "on_load_internal"), + update_vars_internal=_event_name( + UpdateVarsInternalState, "update_vars_internal" + ), + handle_frontend_exception=_event_name( + FrontendEventExceptionState, "handle_frontend_exception" + ), + ) + + def _compile_contexts( state: type[BaseState] | None, theme: Component | None, @@ -271,12 +317,13 @@ def _compile_contexts( initial_state=initial_state, initial_state_json=initial_state_json, initial_state_hashes=state_snapshot_hashes(initial_state), - state_name=state.get_name(), + internal_names=_internal_names(), state_schema=state._to_frontend_schema(), client_storage=utils.compile_client_storage(state), is_dev_mode=not is_prod_mode(), default_color_mode=default_color_mode, disable_react_owner_stacks=disable_react_owner_stacks, + scheme_digest=scheme_digest(), ) if state and initial_state is not None else templates.context_template( @@ -777,6 +824,8 @@ def compile_contexts( Returns: The path and code of the compiled context. """ + warn_if_config_stale() + # Get the path for the output file. output_path = utils.get_context_path() @@ -1582,19 +1631,23 @@ def add_save_task( compile_results.append(result) progress.advance(task) - compile_results.extend([ - compile_contexts( - app._state, - radix_themes_plugin.get_theme(), - component_imports=all_imports, - ), - utils._compile_bundled_libraries(), - ]) + context_output = compile_contexts( + app._state, + radix_themes_plugin.get_theme(), + component_imports=all_imports, + ) + compile_results.extend([context_output, utils._compile_bundled_libraries()]) progress.advance(task) compile_results.append(compile_app_root(app_root, hydrate_fallback_export)) progress.advance(task) + # The context module is rendered from the live names and names browser + # storage by the default ones on purpose, so it is the one output exempt. + raise_for_stale_names( + output for output in compile_results if output is not context_output + ) + progress.stop() if dry_run: diff --git a/reflex/compiler/utils.py b/reflex/compiler/utils.py index e0b490938b3..7c89e3964b3 100644 --- a/reflex/compiler/utils.py +++ b/reflex/compiler/utils.py @@ -444,16 +444,23 @@ def _compile_client_storage_recursive( local_storage: dict[str, dict[str, Any]] = {} session_storage: dict[str, dict[str, Any]] = {} state_name = state.get_full_name() + default_state_name = state._get_default_full_name() for name, field in state.__fields__.items(): if field._owner is not state: # only include vars defined in this state continue - state_key = f"{state_name}.{name}" + FIELD_MARKER field_type, options = _compile_client_storage_field( field, check_factory=check_factories ) if field_type is None or options is None: continue + # Keyed like the var in deltas, but stored in the browser under the + # key it has without minification, so enabling it or editing + # minify.json never orphans a stored value. + state_key = f"{state_name}.{format.format_var_key(state, name)}" + default_key = f"{default_state_name}.{name}{FIELD_MARKER}" + if state_key != default_key: + options.setdefault("name", default_key) if field_type is Cookie: cookies[state_key] = options elif field_type is LocalStorage: diff --git a/reflex/istate/delta.py b/reflex/istate/delta.py index 7c03df6634e..4fe5fb07bd7 100644 --- a/reflex/istate/delta.py +++ b/reflex/istate/delta.py @@ -10,7 +10,6 @@ from contextvars import ContextVar from typing import TYPE_CHECKING, Any, Final, NamedTuple -from reflex_base.constants.state import FIELD_MARKER from reflex_base.vars.base import AsyncComputedVar if TYPE_CHECKING: @@ -249,9 +248,11 @@ def build_delta(state: BaseState) -> Delta: # Token of the client this delta is for, used to know which values it has. token = state.router.session.client_token if pending is not None else "" full_name = state.get_full_name() + # A StateProxy passes itself as state; its __class__ is the wrapped state's. + var_keys = state.__class__._var_keys subdelta: dict[str, Any] = {} for prop in delta_vars: - key = prop + FIELD_MARKER + key = var_keys[prop] if pending is not None and prop in always_dirty_computed_vars: # Uncached computed vars are recomputed for every delta; only # send them when the recomputed value actually changed. Nothing @@ -289,7 +290,12 @@ def build_delta(state: BaseState) -> Delta: delta[full_name] = subdelta substates = state.substates - for substate in state.dirty_substates.union(state._always_dirty_substates): + dirty_substates = state.dirty_substates + if always_dirty_substates := state._always_dirty_substates: + dirty_substates = dirty_substates.union( + substate_cls.get_name() for substate_cls in always_dirty_substates + ) + for substate in dirty_substates: delta.update(substates[substate].get_delta()) return delta diff --git a/reflex/istate/manager/token.py b/reflex/istate/manager/token.py index 8b8e626ebc2..6bfc366c020 100644 --- a/reflex/istate/manager/token.py +++ b/reflex/istate/manager/token.py @@ -152,7 +152,9 @@ def _state_key(self, cls: type[BaseState]) -> str: """Get the key a state of this token's tree is stored under. Formats the key directly, which is cheaper than building a token for the - state class with `with_cls` and converting it to a string. + state class with `with_cls` and converting it to a string. Keyed by the + resolver-independent name, so enabling minification or editing + ``minify.json`` keeps every stored session. Args: cls: The class of the state. @@ -160,7 +162,7 @@ def _state_key(self, cls: type[BaseState]) -> str: Returns: The key of the state in the underlying StateManager store. """ - return f"{self.ident}_{cls.get_full_name()}" + return f"{self.ident}_{cls._get_default_full_name()}" def __str__(self) -> str: """The key used in the underlying StateManager store. diff --git a/reflex/istate/shared.py b/reflex/istate/shared.py index a4d24de0657..1927271fe07 100644 --- a/reflex/istate/shared.py +++ b/reflex/istate/shared.py @@ -256,7 +256,7 @@ async def _link_to(self, token: str) -> Self: raise ReflexRuntimeError(msg) # Associate substate with the given link token. - state_name = self.get_full_name() + state_name = self._get_default_full_name() if self._reflex_internal_links is None: self._reflex_internal_links = {} self._reflex_internal_links[state_name] = token @@ -274,12 +274,12 @@ async def _unlink(self): msg = "Can only unlink SharedState instances." raise ReflexRuntimeError(msg) - state_name = self.get_full_name() + state_name = self._get_default_full_name() if ( not self._reflex_internal_links or state_name not in self._reflex_internal_links ): - msg = f"State {state_name} is not linked and cannot be unlinked." + msg = f"State {self.get_full_name()} is not linked and cannot be unlinked." raise ReflexRuntimeError(msg) # Break the linkage for future events. @@ -406,7 +406,8 @@ async def _modify_linked_states( Args: previous_dirty_vars: When apply linked state changes to other - tokens, provide mapping of state full_name to set of dirty vars. + tokens, provide mapping of state default full name (the key of + ``_reflex_internal_links``) to set of dirty vars. Yields: None. @@ -426,7 +427,7 @@ async def _modify_linked_states( # Go through all linked states and patch them in if they are present in the tree for linked_state_name, linked_token in self._reflex_internal_links.items(): linked_state_cls: type[SharedState] = ( - self.get_root_state().get_class_substate( # pyright: ignore[reportAssignmentType] + RegistrationContext.get()._get_state_by_default_name( # pyright: ignore[reportAssignmentType] linked_state_name ) ) @@ -449,7 +450,7 @@ async def _modify_linked_states( # Collect dirty vars and other affected clients that need to be updated. for linked_state in self._held_locks_linked_states(): if linked_state._previous_dirty_vars is not None: - current_dirty_vars[linked_state.get_full_name()] = set( + current_dirty_vars[linked_state._get_default_full_name()] = set( linked_state._previous_dirty_vars ) if ( @@ -505,14 +506,14 @@ def _collect_shared_token_updates( Args: affected_tokens: Set to update with client tokens that need notification. - current_dirty_vars: Dict to update with dirty var mappings per state. + current_dirty_vars: Dict to update with dirty vars by state default full name. """ for substate in self.substates.values(): if not isinstance(substate, SharedState): continue if substate._linked_from: if substate._previous_dirty_vars: - current_dirty_vars[substate.get_full_name()] = set( + current_dirty_vars[substate._get_default_full_name()] = set( substate._previous_dirty_vars ) if substate._was_touched or substate._previous_dirty_vars: @@ -556,4 +557,4 @@ def __init_subclass__(cls, **kwargs): # `_modify_linked_states` without having to use `.get_state()` which # pulls in all linked states and substates which may not actually be # accessed for this event. - root_state._always_dirty_substates.add(SharedStateBaseInternal.get_name()) + root_state._always_dirty_substates.add(SharedStateBaseInternal) diff --git a/reflex/minify.py b/reflex/minify.py new file mode 100644 index 00000000000..ad1cf241752 --- /dev/null +++ b/reflex/minify.py @@ -0,0 +1,1471 @@ +"""State, event and var name minification driven by ``minify.json``. + +The minification entry point is :class:`MinifyNameResolver`, a +:class:`reflex_base.registry.NameResolver` implementation. Install it via +:func:`install_minify_resolver`; :func:`ensure_minify_resolver_for_active_context` +runs it from :mod:`reflex.state` at import time and from +:func:`reflex.utils.prerequisites.get_app`. +""" + +from __future__ import annotations + +import contextlib +import dataclasses +import functools +import hashlib +import json +import logging +import re +from collections.abc import Callable, Collection, Iterable, Iterator +from pathlib import Path +from typing import TYPE_CHECKING, Any, ClassVar, Literal, NamedTuple, TypedDict + +from reflex_base.constants.state import FIELD_MARKER + +if TYPE_CHECKING: + from reflex.state import BaseState + +logger = logging.getLogger(__name__) + +# File name for the minify configuration +MINIFY_JSON = "minify.json" + +# Current schema version +SCHEMA_VERSION = 1 + +# Names listed in the stale-config warning before it summarizes the rest. +_STALE_WARNING_LIMIT = 5 + +# The frontend keys plain objects by dotted state path, and a state's object by +# var key: a root state's id, a path on its own, and a var id must not read an +# inherited Object.prototype member while the state or var it names is absent. +_OBJECT_PROTOTYPE_MEMBERS = frozenset({ + "__defineGetter__", + "__defineSetter__", + "__lookupGetter__", + "__lookupSetter__", + "__proto__", + "constructor", + "hasOwnProperty", + "isPrototypeOf", + "propertyIsEnumerable", + "toLocaleString", + "toString", + "valueOf", +}) + + +class StateEntry(TypedDict): + """A single state entry in ``minify.json``. + + ``parent`` preserves the sibling scope of an entry even after the class is + renamed or deleted, keeping orphaned ids reserved in their sibling group. + """ + + id: str # minified name, e.g. "a" + parent: str | None # parent state_path, None for root states + + +class MinifyConfig(TypedDict): + """Schema for ``minify.json`` (version :data:`SCHEMA_VERSION`).""" + + version: int + states: dict[str, StateEntry] # state_path -> {id, parent} + events: dict[str, dict[str, str]] # state_path -> {handler_name -> minified_name} + vars: dict[str, dict[str, str]] # state_path -> {var_name -> minified_name} + + +@dataclasses.dataclass(frozen=True, slots=True) +class _MemberKind: + """A kind of state member that ``minify.json`` numbers within each state.""" + + # The config section holding the ids, keyed by state path. + key: Literal["events", "vars"] + # How a member of this kind is named in labels, e.g. ``event:.``. + label: str + # The members of this kind a state class has. + names: Callable[[type[BaseState]], Iterable[str]] + + +_EVENTS = _MemberKind( + key="events", label="event", names=lambda state_cls: state_cls.event_handlers +) +# Only vars sent to the frontend have a wire name to shorten. +_VARS = _MemberKind( + key="vars", label="var", names=lambda state_cls: state_cls._frontend_var_names +) +_MEMBER_KINDS = (_EVENTS, _VARS) + + +def _get_minify_json_path() -> Path: + """Return the ``minify.json`` of the app being run. + + ``REFLEX_MINIFY_CONFIG`` names it explicitly, relative to the working + directory; otherwise it is the one in the working directory, which is the + app's own. + + Returns: + The absolute path, whether or not the file exists. + """ + from reflex.environment import environment + + return (environment.REFLEX_MINIFY_CONFIG.get() or Path(MINIFY_JSON)).absolute() + + +def _read_minify_json(path: Path) -> bytes | None: + """Read a ``minify.json``. + + Args: + path: The file. + + Returns: + Its content, or ``None`` if it does not exist. + """ + try: + return path.read_bytes() + except FileNotFoundError: + return None + + +class _ConfigSource(NamedTuple): + """What a :class:`MinifyNameResolver` is built from, read in one go.""" + + # The ``minify.json`` of the app being run. + path: Path + # Its content, ``None`` if it does not exist. + content: bytes | None + # Whether ``REFLEX_MINIFY_STATES``, ``_EVENTS`` and ``_VARS`` are on. + modes: tuple[bool, bool, bool] + + @classmethod + def read(cls) -> _ConfigSource: + """Read the file and the modes as they are now. + + Returns: + The source. + """ + from reflex.environment import environment + + path = _get_minify_json_path() + return cls( + path, + _read_minify_json(path), + ( + environment.REFLEX_MINIFY_STATES.get(), + environment.REFLEX_MINIFY_EVENTS.get(), + environment.REFLEX_MINIFY_VARS.get(), + ), + ) + + +def _validate_minified_id(label: str, value: str) -> None: + """Reject ids that can't be emitted as a JS identifier segment. + + Args: + label: Human-readable location for the error message. + value: The candidate minified id. + + Raises: + ValueError: If the id is empty or uses characters outside the alphabet. + """ + if not value or not _MINIFY_CHARS_SET.issuperset(value): + msg = f"Invalid {MINIFY_JSON}: {label} has invalid id: {value!r}" + raise ValueError(msg) + + +def _validate_var_id(label: str, value: str) -> None: + """Reject var ids that the frontend could confuse with another key. + + Args: + label: Human-readable location for the error message. + value: The candidate minified id. + + Raises: + ValueError: If the id is reserved or looks like an unminified var key. + """ + if value in _OBJECT_PROTOTYPE_MEMBERS or value.endswith(FIELD_MARKER): + msg = f"Invalid {MINIFY_JSON}: {label} has reserved id: {value!r}" + raise ValueError(msg) + + +def _validate_member_ids( + kind: _MemberKind, members: Any, *, for_repair: bool = False +) -> None: + """Validate one ``{state_path: {name: id}}`` section of ``minify.json``. + + Args: + kind: The kind of member the section numbers. + members: The loaded section. + for_repair: Accept an id two members of a state share, which + ``reflex minify sync`` reassigns. + + Raises: + ValueError: If the section is malformed. + """ + if not isinstance(members, dict): + msg = f"Invalid {MINIFY_JSON}: '{kind.key}' must be a dictionary." + raise ValueError(msg) + for state_path, ids in members.items(): + if not isinstance(ids, dict): + msg = f"Invalid {MINIFY_JSON}: {kind.key} for '{state_path}' must be a dictionary." + raise ValueError(msg) + for name, member_id in ids.items(): + label = f"{kind.label} '{state_path}.{name}'" + if not isinstance(member_id, str): + msg = f"Invalid {MINIFY_JSON}: {label} has non-string id: {member_id}" + raise ValueError(msg) + _validate_minified_id(label, member_id) + if kind is _VARS: + _validate_var_id(label, member_id) + if not for_repair and (duplicates := _find_duplicate_ids(ids.items())): + msg = f"Invalid {MINIFY_JSON}: {_duplicate_member_ids_message(kind, state_path, duplicates)}" + raise ValueError(msg) + + +def _is_state_id(value: str, *, root: bool) -> bool: + """Whether a minified id can name a state in the compiled frontend. + + The frontend joins path segments with ``__``, so ``a`` + ``_b`` and + ``a_`` + ``b`` would both read ``a___b``; an id with no underscore at + either end and none doubled can only ever split one way. It also keys + plain objects by dotted state path, which inherit the Object.prototype + members; only a root's path is its id alone. + + Args: + value: The candidate state id. + root: Whether the state has no parent. + + Returns: + Whether the id may name the state. + """ + return not ( + value.startswith("_") + or value.endswith("_") + or "__" in value + or (root and value in _OBJECT_PROTOTYPE_MEMBERS) + ) + + +def _invalid_state_id_message(state_path: str, state_id: str, *, root: bool) -> str: + """Explain why a state id is rejected and how to replace it. + + Args: + state_path: The state's config path. + state_id: Its id, which :func:`_is_state_id` rejects. + root: Whether the state has no parent. + + Returns: + The message. + """ + reason = ( + "a root state id may not name an Object.prototype member, which the " + "frontend's state maps would inherit" + if root and state_id in _OBJECT_PROTOTYPE_MEMBERS + else "a state id may not start or end with '_' or contain '__', which " + "would make two state paths read the same once dots become '__'" + ) + return ( + f"state '{state_path}' has id {state_id!r}; {reason}. " + "Run 'reflex minify sync' to reassign it." + ) + + +def _duplicate_member_ids_message( + kind: _MemberKind, state_path: str, duplicates: dict[str, list[str]] +) -> str: + """Explain which members of a state share an id and how to fix it. + + Args: + kind: The kind of member. + state_path: The state's config path. + duplicates: The shared ids and the members holding each. + + Returns: + The message. + """ + shared = "; ".join( + f"{mid!r} by {', '.join(sorted(names))}" for mid, names in duplicates.items() + ) + return ( + f"{kind.label}s of '{state_path}' share ids ({shared}), which the frontend " + "and backend cannot tell apart. Run 'reflex minify sync' to reassign them." + ) + + +def _load_minify_config_uncached(*, for_repair: bool = False) -> MinifyConfig | None: + """Load and validate ``minify.json`` from disk. + + Args: + for_repair: See :func:`_parse_minify_config`. + + Returns: + The parsed config, or ``None`` if the file is absent. + """ + return _parse_minify_config( + _read_minify_json(_get_minify_json_path()), for_repair=for_repair + ) + + +def _parse_minify_config( + content: bytes | None, *, for_repair: bool = False +) -> MinifyConfig | None: + """Parse and validate the content of a ``minify.json``. + + Args: + content: The content, ``None`` if the file is absent. + for_repair: Accept the problems ``reflex minify sync`` repairs -- state + ids :func:`_is_state_id` rejects and an id two handlers of a state + share -- so the ``reflex minify`` commands can report and fix them. + + Returns: + The parsed config, or ``None`` if the file is absent. + + Raises: + ValueError: If the content is malformed. + """ + if content is None: + return None + + try: + data = json.loads(content) + except json.JSONDecodeError as e: + msg = f"Invalid JSON in {MINIFY_JSON}: {e}" + raise ValueError(msg) from e + + if not isinstance(data, dict): + msg = ( + f"Invalid {MINIFY_JSON}: must be a JSON object, got {type(data).__name__}." + ) + raise ValueError(msg) + + # Validate schema version + version = data.get("version") + if version != SCHEMA_VERSION: + msg = ( + f"Unsupported {MINIFY_JSON} version: {version}. Expected {SCHEMA_VERSION}." + ) + raise ValueError(msg) + + # Validate required keys + if "states" not in data or not isinstance(data["states"], dict): + msg = f"Invalid {MINIFY_JSON}: 'states' must be a dictionary." + raise ValueError(msg) + if "events" not in data: + msg = f"Invalid {MINIFY_JSON}: 'events' must be a dictionary." + raise ValueError(msg) + + # Validate states: all values must be {id: str, parent: str | None} entries + for key, value in data["states"].items(): + if ( + not isinstance(value, dict) + or not isinstance(value.get("id"), str) + or "parent" not in value + ): + msg = f"Invalid {MINIFY_JSON}: state '{key}' must be an object with a string 'id' and a 'parent': {value}" + raise ValueError(msg) + _validate_minified_id(f"state '{key}'", value["id"]) + root = value["parent"] is None + if not for_repair and not _is_state_id(value["id"], root=root): + msg = f"Invalid {MINIFY_JSON}: {_invalid_state_id_message(key, value['id'], root=root)}" + raise ValueError(msg) + parent = value.get("parent") + if parent is not None and not isinstance(parent, str): + msg = ( + f"Invalid {MINIFY_JSON}: state '{key}' has non-string parent: {parent}" + ) + raise ValueError(msg) + + # Files written before vars were minified have no 'vars' section. + data.setdefault(_VARS.key, {}) + for kind in _MEMBER_KINDS: + _validate_member_ids(kind, data[kind.key], for_repair=for_repair) + + return MinifyConfig( + version=data["version"], + states=data["states"], + events=data["events"], + vars=data["vars"], + ) + + +def save_minify_config(config: MinifyConfig) -> None: + """Save minify configuration to minify.json. + + Args: + config: The configuration to save. + """ + path = _get_minify_json_path() + with path.open("w", encoding="utf-8") as f: + json.dump(config, f, indent=2, sort_keys=True) + f.write("\n") + + +@dataclasses.dataclass(slots=True) +class MinifyNameResolver: + """:class:`~reflex_base.registry.NameResolver` driven by ``minify.json``. + + Returns the minified name when the matching env-var + (``REFLEX_MINIFY_STATES`` / ``REFLEX_MINIFY_EVENTS`` / ``REFLEX_MINIFY_VARS``) + is enabled and the entry exists in the config; ``None`` otherwise. Nothing + is memoized per class: the name caches of the callers already ask once per + state, handler and var. + + Attributes: + config: Parsed ``minify.json``, or ``None``. + states_enabled: Whether ``REFLEX_MINIFY_STATES`` is on. + events_enabled: Whether ``REFLEX_MINIFY_EVENTS`` is on. + vars_enabled: Whether ``REFLEX_MINIFY_VARS`` is on. + source: What the resolver was built from, if read from disk. + """ + + config: MinifyConfig | None + states_enabled: bool + events_enabled: bool + vars_enabled: bool + source: _ConfigSource | None = None + + _digest: str | None = dataclasses.field(default=None, repr=False) + # Equal digests mean equal names; see ``reflex_base.registry._same_names``. + _digest_covers_names: ClassVar[bool] = True + + def digest(self) -> str: + """Digest the wire names this resolver rewrites. + + The config and the modes are fixed for the life of an instance, so the + result is memoized here. Installing another resolver -- which is what + editing ``minify.json`` or toggling a mode at runtime does -- yields a + fresh instance and therefore a fresh digest, with no invalidation to + coordinate. Deliberately independent of which states have registered: + keying on that made the answer depend on when it was first asked for. + + A map is skipped entirely when its ``REFLEX_MINIFY_*`` mode is off, so + toggling a mode registers as a mismatch just as editing the file does. + + Returns: + A short hex digest, or ``""`` when no name is rewritten. + """ + if self._digest is None: + self._digest = self._compute_digest() + return self._digest + + def _compute_digest(self) -> str: + """Hash the config entries that reach the wire. + + Returns: + A short hex digest, or ``""`` when no name is rewritten. + """ + if self.config is None: + return "" + payload: dict[str, Any] = { + key: section + for key, enabled, section in ( + ( + "states", + self.states_enabled, + { + path: entry["id"] + for path, entry in self.config["states"].items() + }, + ), + ("events", self.events_enabled, self.config["events"]), + ("vars", self.vars_enabled, self.config["vars"]), + ) + if enabled and section + } + if not payload: + return "" + return hashlib.sha256( + json.dumps(payload, sort_keys=True, separators=(",", ":")).encode() + ).hexdigest()[:16] + + @classmethod + def from_disk(cls, source: _ConfigSource | None = None) -> MinifyNameResolver: + """Build a resolver from ``minify.json`` (uncached) and env vars. + + Malformed configs degrade gracefully to ``config=None`` with a warning. + + Args: + source: The file and modes to build from; read now if omitted. + + Returns: + A configured resolver. + """ + if source is None: + source = _ConfigSource.read() + try: + config = _parse_minify_config(source.content) + except ValueError as e: + logger.warning( + f"{source.path} could not be loaded: {e}; minification disabled." + ) + config = None + states, events, vars_ = source.modes + return cls( + config=config, + states_enabled=states, + events_enabled=events, + vars_enabled=vars_, + source=source, + ) + + def resolve_state_name(self, state_cls: type[BaseState]) -> str | None: # noqa: D102 + if self.config is None or not self.states_enabled: + return None + entry = self.config["states"].get(get_state_full_path(state_cls)) + return None if entry is None else entry["id"] + + def resolve_handler_name( # noqa: D102 + self, state_cls: type[BaseState], handler_name: str + ) -> str | None: + if not self.events_enabled: + return None + return self._resolve_member(_EVENTS, state_cls, handler_name) + + def resolve_var_name( # noqa: D102 + self, state_cls: type[BaseState], var_name: str + ) -> str | None: + if not self.vars_enabled: + return None + return self._resolve_member(_VARS, state_cls, var_name) + + def _resolve_member( + self, kind: _MemberKind, state_cls: type[BaseState], name: str + ) -> str | None: + """Look up the id ``minify.json`` gives a member of a state. + + Args: + kind: The kind of member. + state_cls: The state the member belongs to. + name: The member's Python name. + + Returns: + The minified id, or ``None`` when the member has no entry. + """ + if self.config is None: + return None + return self.config[kind.key].get(get_state_full_path(state_cls), {}).get(name) + + +def install_minify_resolver() -> None: + """Install a fresh :class:`MinifyNameResolver` into the active context. + + Registered states get their Vars rebuilt under the new names; a Var built + from them before the install keeps the old ones, which the compiler + rejects (see :func:`raise_for_stale_names`). + """ + from reflex_base.registry import RegistrationContext + + ctx = RegistrationContext.ensure_context() + ctx.set_name_resolver(MinifyNameResolver.from_disk()) + + +# Set by :func:`force_default_names`; keeps the ensure-hook from reinstalling. +_default_names_forced = False + + +@contextlib.contextmanager +def force_default_names() -> Iterator[None]: + """Pin the block to built-in names, ignoring ``minify.json``. + + The ``reflex minify`` commands read the config rather than apply it: with + the configured names live, a duplicate id aborts the app import from + ``BaseState.__init_subclass__`` before the command can report it. Must be + entered before the app is imported. + + Yields: + ``None``, with the default resolver installed. + """ + global _default_names_forced + from reflex_base.registry import DefaultNameResolver, RegistrationContext + + ctx = RegistrationContext.ensure_context() + previous_resolver = ctx.name_resolver + previous_forced = _default_names_forced + _default_names_forced = True + ctx.set_name_resolver(DefaultNameResolver()) + try: + yield + finally: + _default_names_forced = previous_forced + ctx.set_name_resolver(previous_resolver) + + +def ensure_minify_resolver_for_active_context() -> None: + """Install the resolver for the ``minify.json`` of the app being run. + + The resolver follows that file, so an app loaded after another one in the + same process -- like an ``AppHarness`` test app next to the repository's + own -- never gets names from the other app's ``minify.json``: without a + file of its own it gets the built-in names, with a different one its own. + An edit to the file or a toggled mode is picked up on the next call, e.g. + by a hot reload. Cheap when nothing changed, so it is safe to wire into + hot paths like :func:`reflex.utils.prerequisites.get_app`. A resolver the + user installed in place of the built-in ones is left alone. + """ + from reflex_base.registry import DefaultNameResolver, RegistrationContext + + if _default_names_forced: + return + ctx = RegistrationContext.ensure_context() + resolver = ctx.name_resolver + source = _ConfigSource.read() + if isinstance(resolver, MinifyNameResolver): + if source.content is None: + ctx.set_name_resolver(DefaultNameResolver()) + return + if resolver.source == source: + return + elif type(resolver) is not DefaultNameResolver or source.content is None: + return + ctx.set_name_resolver(MinifyNameResolver.from_disk(source)) + + +def _collect_missing_entries( + states: Iterable[type[BaseState]], + config: MinifyConfig, + *, + include_states: bool = True, + kinds: Iterable[_MemberKind] = _MEMBER_KINDS, +) -> list[str]: + """Label the registered names ``config`` has no entry for. + + Args: + states: The state classes to inventory. + config: The configuration to check against. + include_states: Whether to report states with no entry. + kinds: The kinds of member to report when they have no entry. + + Returns: + Sorted ``state:`` / ``event:.`` / ``var:.`` + labels. + """ + kinds = tuple(kinds) + config_states = config["states"] + missing: list[str] = [] + for state_cls in states: + state_path = get_state_full_path(state_cls) + if include_states and state_path not in config_states: + missing.append(f"state:{state_path}") + for kind in kinds: + ids = config[kind.key].get(state_path, {}) + missing.extend( + f"{kind.label}:{state_path}.{name}" + for name in kind.names(state_cls) + if name not in ids + ) + missing.sort() + return missing + + +def _find_missing_entries() -> list[str]: + """Registered names the installed resolver has no minified id for. + + A kind of name is only reported when its ``REFLEX_MINIFY_*`` mode is on, + since a disabled mode emits full names by design. + + Returns: + Sorted ``state:`` / ``event:.`` / ``var:.`` + labels; empty when no :class:`MinifyNameResolver` with a config is + installed. + """ + from reflex_base.registry import RegistrationContext + + ctx = RegistrationContext.try_get() + if ctx is None: + return [] + resolver = ctx.name_resolver + if not isinstance(resolver, MinifyNameResolver) or resolver.config is None: + return [] + # Walks the same set as validate_minify_config, so the warning and + # 'reflex minify validate' never disagree about what is missing. + return _collect_missing_entries( + collect_all_states(), + resolver.config, + include_states=resolver.states_enabled, + kinds=[ + kind + for kind, enabled in ( + (_EVENTS, resolver.events_enabled), + (_VARS, resolver.vars_enabled), + ) + if enabled + ], + ) + + +def warn_if_config_stale() -> None: + """Warn when code has states, handlers or vars ``minify.json`` doesn't cover. + + Those names compile unminified, mixing minified and raw names on the wire. + No-op unless a resolver with a config is installed and a mode is enabled. + """ + missing = _find_missing_entries() + if not missing: + return + extra = len(missing) - _STALE_WARNING_LIMIT + shown = ", ".join(missing[:_STALE_WARNING_LIMIT]) + more = f" (+{extra} more)" if extra > 0 else "" + logger.warning( + f"{MINIFY_JSON} is out of date, so these names compile unminified: " + f"{shown}{more}. Run 'reflex minify sync' to assign ids; " + "'reflex minify validate' lists every missing entry." + ) + + +def clear_config_cache() -> None: + """Reload ``minify.json`` and reinstall the resolver.""" + install_minify_resolver() + + +# A dotted run of identifiers starting at one that holds a default state name, +# which always contains the ``___`` module separator. +_DEFAULT_NAME_RUN = re.compile(r"(? dict[str, str]: + """Map the unminified spelling of every renamed state and handler to a label. + + Returns: + ``{default spelling: what it names}``; empty when nothing is renamed. + """ + from reflex_base.registry import RegistrationContext, scheme_digest + from reflex_base.utils.format import format_state_name + + if not scheme_digest(): + return {} + ctx = RegistrationContext.get() + references: dict[str, str] = {} + for state_cls in collect_all_states(): + default_name = state_cls._get_default_full_name() + label = get_state_full_path(state_cls) + if default_name != state_cls.get_full_name(): + # Every default spelling of its handlers and vars starts with one + # of these, so they need no entries of their own; the vars of a + # state keeping its name are checked by their state's local. + references[default_name] = references[format_state_name(default_name)] = ( + f"state {label}" + ) + continue + # A renamed handler's name that is another handler's id is still live. + handlers = state_cls.event_handlers + live = {ctx.get_handler_name(state_cls, name) for name in handlers} + for name in handlers: + if name not in live: + references[f"{default_name}.{name}"] = f"event handler {label}.{name}" + return references + + +# The name of the event a compiled event call sends; ``format_event`` and +# ``LiteralEventVar`` both emit it as a JSON string literal. +_EVENT_CALL = re.compile(r'ReflexEvent\("([\w$.]+)"') + + +def _stale_event_label(name: str) -> str | None: + """Label the handlers an event name was handed out for, unless it is live. + + Code built for one handler reads the same as code built for another that + went by its name under an earlier resolver, so the name stays live while + any handler it was handed out for still goes by it. + + Args: + name: The full event name a compiled event call sends. + + Returns: + The label, or ``None`` if the name is live or was never handed out. + """ + from reflex_base.registry import RegistrationContext + from reflex_base.utils.format import issued_event_handlers + + if not (issuers := issued_event_handlers(name)): + return None + ctx = RegistrationContext.get() + labels: list[str] = [] + for state_cls, handlers in issuers.items(): + state_name = state_cls.get_full_name() + for handler in handlers: + if f"{state_name}.{ctx.get_handler_name(state_cls, handler)}" == name: + return None + labels.append(f"event handler {get_state_full_path(state_cls)}.{handler}") + return " or ".join(sorted(labels)) + + +def raise_for_stale_names(outputs: Iterable[tuple[str, str]]) -> None: + """Reject compiled code naming a state, handler or var unlike the active resolver. + + Installing a resolver rebuilds the Vars each state holds, but not a Var + built from them beforehand -- e.g. by a module-level component imported + before this app's ``minify.json`` was loaded, or while another app's was. + Such code would read a context the frontend never provides, or send an + event the backend never registered or now routes to another handler, or + read a var key it never sends. Caught by the unminified spelling of a + renamed state or handler, by each state local or var key once handed out + that no state or var goes by now, and by each event name once handed out + that no handler it was handed out for goes by now -- which text merely + looking like one never was. Skipped until an install may have changed a + name after one was handed out, as nothing can be stale before that. + + Args: + outputs: ``(path, code)`` pairs of the compiled frontend. + + Raises: + ReflexError: If any code names something unlike the active resolver. + """ + from reflex_base.registry import RegistrationContext + from reflex_base.utils.exceptions import ReflexError + from reflex_base.utils.format import ( + STATE_MEMBER_READ, + format_state_local, + format_var_key, + issued_states, + issued_var_keys, + names_may_be_stale, + ) + + if not names_may_be_stale(): + return + references = _renamed_default_references() + registered: set[type[BaseState]] | None = None + # The state each local stands for now, or None if no state goes by it. + live_owners: dict[str, type[BaseState] | None] = {} + live_keys: dict[type[BaseState], set[str]] = {} + event_labels: dict[str, str | None] = {} + found: dict[str, tuple[str, str]] = {} + for path, code in outputs: + if references: + for match in _DEFAULT_NAME_RUN.finditer(code): + prefix = "" + for segment in match.group().split("."): + prefix = f"{prefix}.{segment}" if prefix else segment + if prefix in references: + found.setdefault(prefix, (references[prefix], path)) + break + for match in STATE_MEMBER_READ.finditer(code): + local, member = match.groups() + if not (owners := issued_states(local)): + continue + if local not in live_owners: + if registered is None: + registered = set(RegistrationContext.get().base_states.values()) + # An unregistered owner, e.g. one replaced by a hot reload, keeps + # its cached name, so only settle for one if nothing else fits. + matching = [c for c in owners if format_state_local(c) == local] + live_owners[local] = next( + (c for c in matching if c in registered), + matching[0] if matching else None, + ) + if live_owners[local] is None: + labels = sorted(f"state {get_state_full_path(c)}" for c in owners) + found.setdefault(local, (" or ".join(labels), path)) + if (state_cls := live_owners[local]) is None or member is None: + continue + if state_cls not in live_keys: + live_keys[state_cls] = { + format_var_key(state_cls, n) + for n in set(issued_var_keys(state_cls).values()) + } + if member in live_keys[state_cls]: + continue + # A key of this state, or of one that had its local before. + for owner in (state_cls, *owners): + if (name := issued_var_keys(owner).get(member)) is not None: + found.setdefault( + f"{local}.{member}", + (f"var {get_state_full_path(owner)}.{name}", path), + ) + break + for name in _EVENT_CALL.findall(code): + if name not in event_labels: + event_labels[name] = _stale_event_label(name) + if (label := event_labels[name]) is not None: + found.setdefault(name, (label, path)) + if not found: + return + details = "\n".join( + f" {label}, as {spelling!r} in {path}" + for spelling, (label, path) in sorted(found.items()) + ) + msg = ( + "The compiled frontend refers to names the active name resolver does " + f"not use:\n{details}\n" + "They come from Vars built while another resolver was installed, e.g. by " + "a component created at import time of a module loaded before this app " + f"directory (and its {MINIFY_JSON}) was, or while another app's was. " + "Import the app from its directory, or create those components inside " + "the page function." + ) + raise ReflexError(msg) + + +# Base-54 encoding for minified names +# Using letters (a-z, A-Z) plus $ and _ which are valid JS identifier chars +_MINIFY_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_" +_MINIFY_BASE = len(_MINIFY_CHARS) # 54 +_MINIFY_CHARS_SET = frozenset(_MINIFY_CHARS) + + +def int_to_minified_name(id_: int) -> str: + """Encode a non-negative integer as a base-54 minified name. + + Args: + id_: The integer to encode. + + Returns: + e.g. ``0 → "a"``, ``25 → "z"``, ``54 → "ba"``. + + Raises: + ValueError: If ``id_`` is negative. + """ + if id_ < 0: + msg = f"ID must be non-negative, got {id_}" + raise ValueError(msg) + if id_ == 0: + return _MINIFY_CHARS[0] + result = [] + num = id_ + while num > 0: + result.append(_MINIFY_CHARS[num % _MINIFY_BASE]) + num //= _MINIFY_BASE + return "".join(reversed(result)) + + +def minified_name_to_int(name: str) -> int: + """Decode a base-54 minified name back to its integer id. + + Args: + name: The minified string. + + Returns: + The integer id. + + Raises: + ValueError: If ``name`` contains invalid characters. + """ + result = 0 + for char in name: + idx = _MINIFY_CHARS.find(char) + if idx == -1: + msg = f"Invalid character in minified name: '{char}'" + raise ValueError(msg) + result = result * _MINIFY_BASE + idx + return result + + +def get_state_full_path(state_cls: type[BaseState]) -> str: + """Build the unique ``module.Class.SubClass`` path for a state class. + + Starts with the module whose code defines the state, so states relocated + for pickling (e.g. by ``ComponentState.create()``) keep their source path. + + Args: + state_cls: The state class. + + Returns: + e.g. ``"myapp.state.AppState.UserState"``. + """ + module = state_cls._get_source_module() + class_hierarchy: list[str] = [] + current: type[BaseState] | None = state_cls + while current is not None: + class_hierarchy.append(current.__name__) + current = current.get_parent_state() # type: ignore[union-attr] + class_hierarchy.reverse() + return ".".join([module, *class_hierarchy]) + + +def get_parent_key(state_cls: type[BaseState]) -> str | None: + """Return the config key of ``state_cls``'s parent state. + + Args: + state_cls: The state class. + + Returns: + The parent's :func:`get_state_full_path`, or ``None`` for root states. + """ + parent = state_cls.get_parent_state() + return get_state_full_path(parent) if parent is not None else None + + +def collect_all_states( + root_state: type[BaseState] | None = None, +) -> list[type[BaseState]]: + """Collect state classes in deterministic depth-first, sibling-sorted order. + + Without ``root_state``, walks every state registered in the active + :class:`~reflex_base.registry.RegistrationContext` (one tree per + parentless root). With ``root_state``, restricts the walk to that subtree. + + Args: + root_state: Optional subtree root. + + Returns: + State classes in depth-first, sibling-sorted order. + """ + if root_state is not None: + result = [root_state] + for substate in sorted(root_state.get_substates(), key=lambda s: s.__name__): + result.extend(collect_all_states(substate)) + return result + + from reflex_base.registry import RegistrationContext + + ctx = RegistrationContext.get() + roots = sorted( + (cls for cls in ctx.base_states.values() if cls.get_parent_state() is None), + key=lambda s: s.__name__, + ) + out: list[type[BaseState]] = [] + for root in roots: + out.extend(collect_all_states(root)) + return out + + +def generate_minify_config( + root_state: type[BaseState] | None = None, +) -> MinifyConfig: + """Generate a complete minify configuration. + + A sync of an empty config, filling every gap: ids start from ``"a"`` in + each sibling group, skipping the parent's own id. Output is byte-stable. + + Args: + root_state: Optional subtree root. + + Returns: + A complete :class:`MinifyConfig`. + """ + return sync_minify_config( + MinifyConfig(version=SCHEMA_VERSION, states={}, events={}, vars={}), + root_state, + reassign_deleted=True, + ) + + +def _find_duplicate_ids(items: Iterable[tuple[str, str]]) -> dict[str, list[str]]: + """Group ``(label, minified_id)`` pairs by id, keeping only collisions. + + Args: + items: Pairs of ``(label, minified_id)``. + + Returns: + Mapping from minified id to the labels sharing it (always ``len >= 2``). + """ + by_id: dict[str, list[str]] = {} + for label, mid in items: + by_id.setdefault(mid, []).append(label) + return {mid: labels for mid, labels in by_id.items() if len(labels) > 1} + + +def _assign_next_ids( + new_keys: Iterable[str], + existing_ids: Collection[str], + reassign_deleted: bool, + is_valid: Callable[[str], bool] | None = None, + skip: Collection[str] = (), +) -> dict[str, str]: + """Assign minified ids to ``new_keys`` while skipping ``existing_ids``. + + Keys are sorted for deterministic output. Ids are compared as strings, as + on the wire: a held ``aa`` leaves ``a`` free though both decode to 0. + + Args: + new_keys: Keys needing new ids. + existing_ids: Already-used ids in the same scope. + reassign_deleted: When ``True``, scan from 0 (filling gaps); + otherwise start past the highest decoded ``existing_ids``. + is_valid: Whether an id may be taken, for ids with rules of their own. + skip: Ids to avoid that are not in the scope, so do not move its max. + + Returns: + Mapping from key to its newly-assigned minified id. + """ + pool = {*existing_ids, *skip} + next_id = ( + 0 + if reassign_deleted + else max(map(minified_name_to_int, existing_ids), default=-1) + 1 + ) + out: dict[str, str] = {} + for key in sorted(new_keys): + while (name := int_to_minified_name(next_id)) in pool or ( + is_valid is not None and not is_valid(name) + ): + next_id += 1 + out[key] = name + pool.add(name) + next_id += 1 + return out + + +def _state_id_scope( + states: dict[str, StateEntry], parent_key: str | None, exclude: Collection[str] +) -> tuple[set[str], set[str]]: + """Get the ids a state under ``parent_key`` may not take. + + Args: + states: The state entries. + parent_key: The config path of the parent, ``None`` for a root. + exclude: The paths whose own ids do not count. + + Returns: + ``(sibling ids, parent ids)``; the parent's id is avoided so the + leading segment of a relative path can only ever mean the parent. + """ + siblings = { + entry["id"] + for path, entry in states.items() + if entry["parent"] == parent_key and path not in exclude + } + parent_entry = states.get(parent_key) if parent_key is not None else None + parent = set() if parent_entry is None else {parent_entry["id"]} + return siblings, parent + + +def _assign_state_ids( + states: dict[str, StateEntry], + state_paths: Collection[str], + parent_key: str | None, + reassign_deleted: bool, +) -> dict[str, str]: + """Assign ids to states under ``parent_key``, avoiding their scope's ids. + + Args: + states: The state entries. + state_paths: The paths needing ids. + parent_key: The config path of their parent, ``None`` for roots. + reassign_deleted: Whether an id may fill a gap. + + Returns: + Mapping from path to its newly-assigned id. + """ + siblings, parent = _state_id_scope(states, parent_key, set(state_paths)) + return _assign_next_ids( + state_paths, + siblings, + reassign_deleted, + is_valid=functools.partial(_is_state_id, root=parent_key is None), + skip=parent, + ) + + +def _reassign_duplicate_ids( + ids: dict[str, str], reassign_deleted: bool, live: Collection[str] +) -> None: + """Give a new id to every name but one of those sharing an id. + + The one kept is a live name if any shares the id -- a served frontend may + be using it -- else the first by sort order. + + Args: + ids: The ``{name: id}`` map of one state, modified in place. + reassign_deleted: Whether a replacement id may fill a gap. + live: The names the code still has. + """ + kept: set[str] = set() + duplicates: list[str] = [] + for name in sorted(ids, key=lambda name: (name not in live, name)): + if ids[name] in kept: + duplicates.append(name) + else: + kept.add(ids[name]) + if duplicates: + ids.update(_assign_next_ids(duplicates, kept, reassign_deleted)) + + +def _rehome_invalid_state_ids( + states: dict[str, StateEntry], reassign_deleted: bool +) -> None: + """Replace the state ids :func:`_is_state_id` rejects, in place. + + Files written before those rules may hold them, orphaned entries included, + and the app refuses to load such a file. Parents come first, so a child is + checked against its parent's final id. + + Args: + states: The state entries to fix up, modified in place. + reassign_deleted: Whether a replacement id may fill a gap. + """ + for state_path in sorted(states): + entry = states[state_path] + if _is_state_id(entry["id"], root=entry["parent"] is None): + continue + entry["id"] = _assign_state_ids( + states, (state_path,), entry["parent"], reassign_deleted + )[state_path] + + +def _rehome_conflicting_ids( + states: dict[str, StateEntry], + all_states: Iterable[type[BaseState]], + reassign_deleted: bool, + reparented: set[str], +) -> None: + """Move an id this sync put in conflict, in place. + + Healing a stored parent keeps the entry's id, which can land it on its new + parent's id or on one a sibling already holds; a parent assigned in this + run can likewise land on a preserved child's id. Only entries this sync + moved give way, plus any child sharing its parent's id -- moving the parent + instead would cascade. An entry whose scope did not change keeps the id a + served frontend may still be using; a duplicate that was already in the + file is left for ``validate`` to report. Walks ancestors first, so a parent + is final when its children are checked. + + Args: + states: The state entries to fix up, modified in place. + all_states: State classes in depth-first order. + reassign_deleted: Whether a replacement id may fill a gap. + reparented: Paths whose stored parent this sync healed. + """ + for state_cls in all_states: + state_path = get_state_full_path(state_cls) + entry = states.get(state_path) + if entry is None or entry["parent"] is None: + continue + siblings, parent = _state_id_scope(states, entry["parent"], (state_path,)) + shares_parent_id = entry["id"] in parent + if entry["id"] not in siblings and not shares_parent_id: + continue + if state_path not in reparented and not shares_parent_id: + continue + entry["id"] = _assign_state_ids( + states, (state_path,), entry["parent"], reassign_deleted + )[state_path] + + +def validate_minify_config( + config: MinifyConfig, + root_state: type[BaseState] | None = None, +) -> tuple[list[str], list[str], list[str]]: + """Validate a minify configuration against the current state tree. + + Args: + config: The configuration to validate. + root_state: Optional subtree root. ``None`` validates against every + state in the active context. + + Returns: + A tuple ``(errors, warnings, missing_entries)``: + + * ``errors`` — critical issues (duplicate IDs, etc.) + * ``warnings`` — non-critical issues (orphaned entries) + * ``missing_entries`` — states/events in code but not in the config + """ + errors: list[str] = [] + warnings: list[str] = [] + + all_states = collect_all_states(root_state) + + # Group siblings by parent key: live entries via their actual parent class, + # orphans via the parent recorded in the config — so id reuse between a + # dead entry and a live sibling is detected. + path_to_cls = {get_state_full_path(s): s for s in all_states} + parent_to_pairs: dict[str | None, list[tuple[str, str]]] = {} + for state_path, entry in config["states"].items(): + state_cls = path_to_cls.get(state_path) + if state_cls is not None: + parent_key = get_parent_key(state_cls) + if entry["parent"] != parent_key: + warnings.append( + f"Stale parent for '{state_path}': config has " + f"{entry['parent']!r}, actual is {parent_key!r}. " + "Run 'reflex minify sync' to fix." + ) + label = state_path + else: + parent_key = entry["parent"] + label = f"{state_path} (orphaned)" + parent_to_pairs.setdefault(parent_key, []).append((label, entry["id"])) + + # Loading goes by the recorded parent and the frontend by the actual + # one, so a state either one calls a root needs a valid root id. + root = parent_key is None or entry["parent"] is None + if not _is_state_id(entry["id"], root=root): + errors.append(_invalid_state_id_message(state_path, entry["id"], root=root)) + + # A state sharing its parent's id makes a relative path ambiguous, + # which misresolves substate lookups rather than failing. Checked + # against the actual parent, which is the tree a lookup walks. An + # orphan resolves no name at runtime, and its id stays reserved at the + # value a served frontend may still hold, so it is exempt. + parent_entry = ( + config["states"].get(parent_key) + if parent_key and state_cls is not None + else None + ) + if parent_entry is not None and parent_entry["id"] == entry["id"]: + errors.append( + f"State '{state_path}' reuses the id '{entry['id']}' of its parent " + f"'{parent_key}'. Delete {MINIFY_JSON} and re-run " + "'reflex minify init'." + ) + + for parent_key, pairs in parent_to_pairs.items(): + parent_name = parent_key if parent_key is not None else "root" + errors.extend( + f"Duplicate state_id='{mid}' under '{parent_name}': {paths}" + for mid, paths in _find_duplicate_ids(pairs).items() + ) + + code_state_paths = {get_state_full_path(state_cls) for state_cls in all_states} + + missing = _collect_missing_entries(all_states, config) + + # Check for orphaned entries (in config but not in code) + warnings.extend( + f"Orphaned state in config: {state_path}" + for state_path in config["states"] + if state_path not in code_state_paths + ) + + for kind in _MEMBER_KINDS: + code_names = collect_member_names(all_states, kind) + for state_path, ids in config[kind.key].items(): + if duplicates := _find_duplicate_ids(ids.items()): + errors.append( + _duplicate_member_ids_message(kind, state_path, duplicates) + ) + if state_path not in code_names: + warnings.append(f"Orphaned {kind.key} for state: {state_path}") + else: + warnings.extend( + f"Orphaned {kind.label} in config: {state_path}.{name}" + for name in ids + if name not in code_names[state_path] + ) + + return errors, warnings, missing + + +def collect_member_names( + states: Iterable[type[BaseState]], kind: _MemberKind +) -> dict[str, set[str]]: + """Map each state's config path to its members of one kind. + + Args: + states: The state classes to inventory. + kind: The kind of member to collect. + + Returns: + ``state_path -> {name}`` for every given state. + """ + return { + get_state_full_path(state_cls): set(kind.names(state_cls)) + for state_cls in states + } + + +def _sync_member_ids( + kind: _MemberKind, + existing: dict[str, dict[str, str]], + all_states: Iterable[type[BaseState]], + reassign_deleted: bool, + prune: bool, +) -> dict[str, dict[str, str]]: + """Bring one ``{state_path: {name: id}}`` section in line with the code. + + Args: + kind: The kind of member the section numbers. + existing: The section as configured. + all_states: The state classes in the code. + reassign_deleted: Whether a new id may fill a gap. + prune: Whether to drop the members and states no longer in the code. + + Returns: + The updated section; ids a member already has are kept. + """ + code_names = collect_member_names(all_states, kind) + section = {state_path: dict(ids) for state_path, ids in existing.items()} + if prune: + section = { + state_path: kept + for state_path, ids in section.items() + if ( + kept := { + name: member_id + for name, member_id in ids.items() + if name in code_names.get(state_path, ()) + } + ) + } + # Ids are unique within each state. + for state_path, ids in section.items(): + _reassign_duplicate_ids( + ids, reassign_deleted, code_names.get(state_path, frozenset()) + ) + for state_path, names in code_names.items(): + ids = section.get(state_path, {}) + if new_names := [name for name in names if name not in ids]: + ids.update(_assign_next_ids(new_names, ids.values(), reassign_deleted)) + section[state_path] = ids + return section + + +def sync_minify_config( + existing_config: MinifyConfig, + root_state: type[BaseState] | None = None, + reassign_deleted: bool = False, + prune: bool = False, +) -> MinifyConfig: + """Synchronize minify configuration with the current state tree. + + Args: + existing_config: The existing configuration to update. + root_state: Optional subtree root. ``None`` syncs against every state + in the active context. + reassign_deleted: If True, fill id gaps left by removed entries. + Retained orphan ids stay reserved unless pruned. + prune: If True, remove entries for states, events and vars that no + longer exist. + + Returns: + The updated configuration. + """ + all_states = collect_all_states(root_state) + path_to_parent_key = {get_state_full_path(s): get_parent_key(s) for s in all_states} + + new_states: dict[str, StateEntry] = { + k: StateEntry(id=v["id"], parent=v["parent"]) + for k, v in existing_config["states"].items() + } + + # Prune orphaned entries if requested + if prune: + new_states = {k: v for k, v in new_states.items() if k in path_to_parent_key} + + # Every configured id stays reserved within its sibling group -- orphans + # under their recorded parent, so their ids are never reused. Live entries + # get their stored parent healed to the actual value. + reparented: set[str] = set() + for state_path, entry in new_states.items(): + parent_key = path_to_parent_key.get(state_path, entry["parent"]) + if parent_key != entry["parent"]: + reparented.add(state_path) + entry["parent"] = parent_key + + # Find states that need IDs assigned, grouped by parent key. + parent_key_to_new_children: dict[str | None, list[str]] = {} + for state_path, parent_key in path_to_parent_key.items(): + if state_path not in new_states: + parent_key_to_new_children.setdefault(parent_key, []).append(state_path) + + # Depth-first order assigns a parent before its children look at its id. + for parent_key, children in parent_key_to_new_children.items(): + assigned = _assign_state_ids(new_states, children, parent_key, reassign_deleted) + for state_path, minified_name in assigned.items(): + new_states[state_path] = StateEntry(id=minified_name, parent=parent_key) + + _rehome_invalid_state_ids(new_states, reassign_deleted) + _rehome_conflicting_ids(new_states, all_states, reassign_deleted, reparented) + + members = { + kind.key: _sync_member_ids( + kind, existing_config[kind.key], all_states, reassign_deleted, prune + ) + for kind in _MEMBER_KINDS + } + return MinifyConfig( + version=SCHEMA_VERSION, + states=new_states, + events=members["events"], + vars=members["vars"], + ) diff --git a/reflex/reflex.py b/reflex/reflex.py index e3a2b9aaf35..cd2c0caf5b8 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -2,6 +2,7 @@ from __future__ import annotations +import contextlib import logging import os import select @@ -14,7 +15,7 @@ from importlib.util import find_spec from pathlib import Path from types import FrameType -from typing import TYPE_CHECKING, Any, NoReturn, cast +from typing import TYPE_CHECKING, Any, Literal, NoReturn, cast, overload import click from reflex_base import constants @@ -27,16 +28,16 @@ stop_process_tree, ) -from reflex.utils.cli_options import log_options +from reflex.utils.cli_options import log_options, loglevel_option logger = logging.getLogger(__name__) if TYPE_CHECKING: - from typing import Literal - from click.shell_completion import CompletionItem from reflex_base.constants.base import LITERAL_ENV + from reflex.minify import MinifyConfig, _MemberKind + @click.group @click.version_option(constants.Reflex.VERSION, message="%(version)s") @@ -1247,6 +1248,565 @@ def rename(new_name: str): rename_app(new_name, get_config().loglevel) +# Minify command group +@cli.group(help="Manage state, event and var name minification.") +@click.pass_context +def minify(ctx: click.Context): + """Manage state, event and var name minification. + + Args: + ctx: The click context, used to scope the forced default names. + """ + from reflex.minify import force_default_names + + # These commands read minify.json rather than apply it; with the configured + # names live, a duplicate id aborts the app import before they can report + # it. Must happen here, before a subcommand body imports the app. + ctx.with_resource(force_default_names()) + + +def _load_app_for_minify() -> None: + """Compile the user's app so dynamic states (e.g. ``ComponentState``) register.""" + from reflex.utils import prerequisites + + prerequisites.get_compiled_app(dry_run=True) + + +def _count_members(config: MinifyConfig, kind: _MemberKind) -> int: + """Sum the members of one kind across every state in a minify config. + + Args: + config: The minify configuration. + kind: The kind of member to count. + + Returns: + Total member count. + """ + return sum(len(ids) for ids in config[kind.key].values()) + + +@overload +def _open_minify_session( + *, require_exists: Literal[True] = True, for_json: bool = False +) -> MinifyConfig: ... +@overload +def _open_minify_session( + *, require_exists: Literal[False], for_json: bool = False +) -> MinifyConfig | None: ... + + +def _open_minify_session( + *, require_exists: bool = True, for_json: bool = False +) -> MinifyConfig | None: + """Run the standard minify-CLI prelude. + + Compiles the user's app (so dynamic states register) and loads + ``minify.json``. Exits the CLI with an error when the file is required + but missing, or when it exists but is malformed. + + Args: + require_exists: When ``True`` (default), missing ``minify.json`` is + a fatal error. When ``False``, returns ``None`` instead. + for_json: Whether the command writes a machine-readable document, in + which case stdout is claimed before the app logs anything. + + Returns: + The parsed config, or ``None`` if ``require_exists`` is ``False`` and + the file is absent. + """ + from reflex.minify import _get_minify_json_path, _load_minify_config_uncached + + if for_json: + log.reserve_stdout() + # A real CLI process exits, but an in-process invocation (tests, an + # embedder) would leave every later log write pointed at stderr. + if (ctx := click.get_current_context(silent=True)) is not None: + ctx.call_on_close(lambda: log.reserve_stdout(False)) + + path = _get_minify_json_path() + exists = path.exists() + if require_exists and not exists: + logger.error(f"{path} does not exist. Use 'reflex minify init' to create it.") + raise SystemExit(1) + + # Loading the app runs arbitrary module-level code, which may write to + # stdout directly; reserving only covers Reflex's own logging, so send + # everything the import prints to stderr rather than into the document. + with ( + contextlib.redirect_stdout(sys.stderr) if for_json else contextlib.nullcontext() + ): + _load_app_for_minify() + + if not exists: + return None + + try: + config = _load_minify_config_uncached(for_repair=True) + except ValueError as e: + logger.error(str(e)) + raise SystemExit(1) from e + if config is None: + logger.error(f"Failed to load {path}.") + raise SystemExit(1) + return config + + +@minify.command(name="init") +@loglevel_option +def minify_init(): + """Initialize minify.json with IDs for all states, events and vars.""" + from reflex.minify import ( + _EVENTS, + _VARS, + _get_minify_json_path, + generate_minify_config, + save_minify_config, + ) + + path = _get_minify_json_path() + if path.exists(): + logger.error( + f"{path} already exists. Use 'reflex minify sync' to update " + "or delete the file to reinitialize." + ) + raise SystemExit(1) + + _load_app_for_minify() + config = generate_minify_config() + save_minify_config(config) + + logger.info( + f"Created {path} with {len(config['states'])} states, " + f"{_count_members(config, _EVENTS)} events " + f"and {_count_members(config, _VARS)} vars." + ) + + +@minify.command(name="sync") +@loglevel_option +@click.option( + "--reassign-deleted", + is_flag=True, + help="Reassign IDs that are no longer in use (potentially breaking for existing clients).", +) +@click.option( + "--prune", + is_flag=True, + help=( + "Remove entries for states/events that no longer exist in code, " + "freeing their IDs (potentially breaking for existing clients)." + ), +) +def minify_sync(reassign_deleted: bool, prune: bool): + """Synchronize minify.json with the current codebase. + + Adds new states, events and vars, optionally removes orphaned entries. + """ + from reflex.minify import ( + _MEMBER_KINDS, + _get_minify_json_path, + save_minify_config, + sync_minify_config, + ) + + existing_config = _open_minify_session() + new_config = sync_minify_config( + existing_config, reassign_deleted=reassign_deleted, prune=prune + ) + save_minify_config(new_config) + + logger.info(f"Updated {_get_minify_json_path()}:") + logger.info( + f" States: {len(existing_config['states'])} -> {len(new_config['states'])}" + ) + for kind in _MEMBER_KINDS: + logger.info( + f" {kind.key.capitalize()}: {_count_members(existing_config, kind)} " + f"-> {_count_members(new_config, kind)}" + ) + + +@minify.command(name="validate") +@loglevel_option +def minify_validate(): + """Validate minify.json against the current codebase. + + Checks for duplicate IDs, missing entries, and orphaned entries. + """ + from reflex.minify import _get_minify_json_path, validate_minify_config + + config = _open_minify_session() + errors, warnings, missing = validate_minify_config(config) + + if errors: + logger.error("Errors found:") + for error in errors: + logger.error(f" - {error}") + + if warnings: + logger.warning("Warnings:") + for warning in warnings: + logger.warning(f" - {warning}") + + if missing: + logger.warning("Missing entries (in code but not in config):") + for entry in missing: + logger.warning(f" - {entry}") + + if errors or missing: + raise SystemExit(1) + logger.info(f"{_get_minify_json_path()} is valid and up-to-date.") + + +@minify.command(name="list") +@loglevel_option +@click.option( + "--json", + "output_json", + is_flag=True, + help="Output as JSON.", +) +def minify_list(output_json: bool): + """Print the state tree with IDs and minified names.""" + from typing import TypedDict + + from reflex.minify import _EVENTS, _VARS, get_state_full_path + from reflex.state import BaseState, State + + class EventHandlerData(TypedDict): + """Type for event handler data in state tree.""" + + name: str + event_id: str | None # The minified name (e.g., "a", "ba") or None + + class StateVarData(TypedDict): + """Type for var data in state tree.""" + + name: str + var_id: str | None # The minified name (e.g., "a", "ba") or None + + class StateTreeData(TypedDict): + """Type for state tree data.""" + + name: str + full_path: str + state_id: str | None # The minified name (e.g., "a", "ba") or None + event_handlers: list[EventHandlerData] + vars: list[StateVarData] + substates: list[StateTreeData] + + # CLI inspection shows config contents regardless of env var settings. + config = _open_minify_session(require_exists=False, for_json=output_json) + states_map = config["states"] if config else {} + + def member_ids( + kind: _MemberKind, state_cls: type[BaseState], state_path: str + ) -> list[tuple[str, str | None]]: + """List a state's members of one kind with their ids. + + Args: + kind: The kind of member. + state_cls: The state. + state_path: The state's config path. + + Returns: + ``(name, id)`` pairs sorted by name. + """ + ids = config[kind.key].get(state_path, {}) if config else {} + return [(name, ids.get(name)) for name in sorted(kind.names(state_cls))] + + def build_state_tree(state_cls: type[BaseState]) -> StateTreeData: + """Recursively build state tree data. + + Args: + state_cls: The state class to build the tree for. + + Returns: + A dictionary containing the state tree data. + """ + state_path = get_state_full_path(state_cls) + state_entry = states_map.get(state_path) + + return { + "name": state_cls.__name__, + "full_path": state_path, + "state_id": state_entry["id"] if state_entry is not None else None, + "event_handlers": [ + {"name": name, "event_id": event_id} + for name, event_id in member_ids(_EVENTS, state_cls, state_path) + ], + "vars": [ + {"name": name, "var_id": var_id} + for name, var_id in member_ids(_VARS, state_cls, state_path) + ], + "substates": [ + build_state_tree(substate) + for substate in sorted( + state_cls.get_substates(), key=lambda s: s.__name__ + ) + ], + } + + def labeled(name: str, member_id: str | None) -> str: + """Show a name with the id it is minified to, if any. + + Args: + name: The Python name. + member_id: The minified id, or ``None``. + + Returns: + The label to print. + """ + return name if member_id is None else f'{name} -> "{member_id}"' + + def print_state_tree( + state_data: StateTreeData, prefix: str = "", is_last: bool = True + ): + """Print a state and its children as a tree. + + Args: + state_data: The state data dictionary. + prefix: The prefix for indentation. + is_last: Whether this is the last item in the current level. + """ + connector = "`-- " if is_last else "|-- " + click.echo( + f"{prefix}{connector}{labeled(state_data['name'], state_data['state_id'])}" + ) + + # Calculate new prefix for children + child_prefix = prefix + (" " if is_last else "| ") + groups = [ + (title, group) + for title, group in ( + ( + "Event Handlers", + [(h["name"], h["event_id"]) for h in state_data["event_handlers"]], + ), + ("Vars", [(v["name"], v["var_id"]) for v in state_data["vars"]]), + ) + if group + ] + substates = state_data["substates"] + for group_index, (title, group) in enumerate(groups): + has_more = bool(substates) or group_index < len(groups) - 1 + click.echo(f"{child_prefix}{'|' if has_more else '`'}-- {title}:") + member_prefix = child_prefix + ("| " if has_more else " ") + for i, (name, member_id) in enumerate(group): + m_connector = "`-- " if i == len(group) - 1 else "|-- " + click.echo(f"{member_prefix}{m_connector}{labeled(name, member_id)}") + + # Print substates recursively + for i, substate in enumerate(substates): + print_state_tree(substate, child_prefix, i == len(substates) - 1) + + tree_data = build_state_tree(State) + + if output_json: + import json + + click.echo(json.dumps(tree_data, indent=2)) + else: + if config is not None: + click.echo("State Tree (minify.json loaded)") + else: + click.echo("State Tree (no minify.json)") + print_state_tree(tree_data) + + +@minify.command(name="lookup") +@loglevel_option +@click.option( + "--json", + "output_json", + is_flag=True, + help="Output detailed info as JSON.", +) +@click.argument("minified_path") +def minify_lookup(output_json: bool, minified_path: str): + """Lookup a state, event handler or var by its minified path (e.g., 'a.bU'). + + Walks the state tree from the root to resolve each segment. The final + segment may also be an event handler or var id of the state resolved so + far, so an event name copied from the frontend resolves to its handler, + and a delta key appended to its state's name to its var. States, events + and vars minify independently, so every segment also matches its + unminified name. The root state's own name is optional and may be given either way, + so 'a.bU', 'a.a.bU' and 'reflex___state____state.a.bU' all resolve the + same way; given alone it resolves to the root state itself. + """ + from reflex_base.constants.state import FIELD_MARKER + from reflex_base.registry import RegistrationContext + + from reflex.minify import ( + _EVENTS, + _MEMBER_KINDS, + _VARS, + collect_all_states, + get_state_full_path, + ) + from reflex.state import State + + # How a member of each kind shows: its name's JSON key, how to read it, and + # the flag that minifies it. + presentation = { + _EVENTS: ("handler", "an event handler", "REFLEX_MINIFY_EVENTS"), + _VARS: ("var", "a var", "REFLEX_MINIFY_VARS"), + } + config = _open_minify_session(for_json=output_json) + + # Build lookup: full_path -> minified_id (None if no entry). + path_to_id: dict[str, str | None] = {} + for s in collect_all_states(State): + path = get_state_full_path(s) + entry = config["states"].get(path) + path_to_id[path] = entry["id"] if entry is not None else None + + parts = minified_path.split(".") + # Accept a path copied verbatim from the frontend, which leads with the + # root state's own name; the lookup below walks from the root's children. + # Both spellings are accepted so the result never depends on whether the + # shell running the CLI has REFLEX_MINIFY_STATES set. + root_names = { + RegistrationContext.default_state_name(State), + path_to_id[get_state_full_path(State)], + } + # A lone root segment is a lookup of the root state itself, not a prefix + # with nothing behind it; states and events minify independently, so it + # may name a root event handler as well. + leads_with_root = parts[0] in root_names + root_only = leads_with_root and len(parts) == 1 + if leads_with_root and not root_only: + parts = parts[1:] + + result_parts: list[dict[str, str | None]] = [] + current = State + last_index = len(parts) - 1 + + for index, part in enumerate(parts): + # Find the child of the previous match whose minified id or default + # name is ``part``. + found = ( + State + if root_only + else next( + ( + c + for c in current.get_substates() + if part + in ( + path_to_id.get(get_state_full_path(c)), + RegistrationContext.default_state_name(c), + ) + ), + None, + ) + ) + # An event name copied from the frontend ends in a handler id (or the + # handler's own name) of the state resolved so far, not a substate id; + # likewise a var key copied from a delta, appended to its state's name. + current_path = get_state_full_path(current) + owner = f"{current._get_source_module()}.{current.__name__}" + members: list[tuple[_MemberKind, str, str | None]] = [] + if index == last_index: + for kind in _MEMBER_KINDS: + names = kind.names(current) + ids = config[kind.key].get(current_path, {}) + # Only live members: ``sync`` keeps a deleted one's id reserved, + # and a config ``validate`` rejects can give several the same id. + holders = sorted( + name for name, member_id in ids.items() if member_id == part + ) + matches = [name for name in holders if name in names] + if len(matches) > 1 and not output_json: + logger.warning( + f"{', '.join(matches)} of {owner} share the id '{part}', so " + "all but one are unreachable. Run 'reflex minify sync' to " + "reassign them." + ) + deleted = [name for name in holders if name not in matches] + if deleted and not output_json: + logger.warning( + f"'{part}' is also the id minify.json keeps reserved for " + f"deleted {', '.join(deleted)} of {owner}, which an older " + "frontend may still send." + ) + # With this kind's minification off the wire carries names, so + # the segment may also name a member other than the id's holders. + named = next( + ( + name + for name in (part, part.removesuffix(FIELD_MARKER)) + if name in names + ), + None, + ) + if named is not None and named not in matches: + if matches and not output_json: + logger.warning( + f"Segment '{part}' is the id of {', '.join(matches)} of " + f"{owner} with {presentation[kind][2]} on, but names " + f"{named} with it off; showing both." + ) + matches.append(named) + members.extend((kind, name, ids.get(name)) for name in matches) + if found is None and not members: + wanted = "state, event handler or var" if index == last_index else "state" + logger.error( + f"No {wanted} found for minified segment '{part}' in path '{minified_path}'" + ) + raise SystemExit(1) + + if found is not None: + result_parts.append({ + "kind": "state", + "minified": part, + # ``part`` may be the unminified name, so report the id from + # the config; ``None`` when the state has no entry. + "state_id": path_to_id.get(get_state_full_path(found)), + "module": found._get_source_module(), + "class": found.__name__, + "full_path": get_state_full_path(found), + }) + # JSON readers see the ambiguity as several entries for one segment; + # several members of one kind were warned about above. + readings = (["a state"] if found is not None else []) + [ + *dict.fromkeys(presentation[kind][1] for kind, _, _ in members) + ] + if len(readings) > 1 and not output_json: + logger.warning( + f"Segment '{part}' is the id of {' and of '.join(readings)} of " + f"{owner}; showing each." + ) + for kind, member, member_id in members: + name_key = presentation[kind][0] + result_parts.append({ + "kind": kind.label, + "minified": part, + f"{kind.label}_id": member_id, + "module": current._get_source_module(), + "class": current.__name__, + name_key: member, + "full_path": f"{current_path}.{member}", + }) + if found is not None: + current = found + + if output_json: + import json + + click.echo(json.dumps(result_parts, indent=2)) + else: + # Simple output: module.ClassName, plus .handler or .var for a member. + for info in result_parts: + line = f"{info['module']}.{info['class']}" + if info["kind"] == "event": + line += f".{info['handler']}" + elif info["kind"] == "var": + line += f".{info['var']}" + click.echo(line) + + _COMPONENT_REMOVED = ( "`reflex component` was removed in Reflex 0.10. Wrap React components " "directly in your app (https://reflex.dev/docs/wrapping-react/overview/) " diff --git a/reflex/state.py b/reflex/state.py index 51934fe3e97..dfad1a96c87 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -3,7 +3,6 @@ from __future__ import annotations import builtins -import contextlib import dataclasses import functools import hashlib @@ -33,7 +32,6 @@ from reflex_base import constants from reflex_base.config import get_state_auto_setters, get_state_explicit_event_handlers -from reflex_base.constants.state import FIELD_MARKER from reflex_base.environment import PerformanceMode, auto_reload_cooldown, environment from reflex_base.event import ( _UNDECORATED_STATE_METHOD_MARKER, @@ -103,6 +101,11 @@ from reflex.istate.proxy import ImmutableMutableProxy as ImmutableMutableProxy from reflex.istate.proxy import MutableProxy from reflex.istate.storage import ClientStorageBase +from reflex.minify import ( + MINIFY_JSON, + ensure_minify_resolver_for_active_context, + get_state_full_path, +) from reflex.utils import console, format, types from reflex.utils.exec import is_testing_env @@ -141,6 +144,31 @@ def _stale_pickle_keys(cls: type[BaseState]) -> frozenset[str]: if TYPE_CHECKING: from reflex_base.components.component import Component +# As early as possible, so that Vars modules build from state Vars at import +# already use the configured names (e.g. granian prod workers import the app +# module directly, bypassing get_app); a later install only rebuilds the Vars +# each state holds. +ensure_minify_resolver_for_active_context() + + +if TYPE_CHECKING: + + def _typing_event(fn: Callable[..., Any]) -> EventHandler: + """Type a state method as the ``EventHandler`` it becomes. No-op at runtime. + + Args: + fn: The method to mark. + + Returns: + ``fn`` typed as an ``EventHandler``. + """ + ... + +else: + + def _typing_event(fn: Callable[..., Any]) -> Callable[..., Any]: + return fn + var = computed_var @@ -275,12 +303,8 @@ def get_var_for_field(cls: type[BaseState], name: str, f: Field) -> Var: Returns: The Var instance. """ - field_name = ( - format.format_state_name(cls.get_full_name()) + "." + name + FIELD_MARKER - ) - return dispatch( - field_name=field_name, + field_name=format.format_state_var(cls, name), var_data=VarData.from_state(cls, name), result_var_type=f.outer_type_, ) @@ -478,10 +502,10 @@ def _get_router_var(cls: type[BaseState]) -> RouterDataVar: # and hands back the composed RouterData, as it does on a state # with a single `router` base var. _var_data=VarData( - state=root_cls.get_full_name(), field_name=constants.ROUTER + state=root_cls._get_default_full_name(), field_name=constants.ROUTER ), ) - setattr(root_cls, "_reflex_router_var", router_var) # noqa: B010 + root_cls._reflex_router_var = router_var return router_var @@ -640,26 +664,36 @@ class BaseState(EvenMoreBasicBaseState, state_root=True): # The event handlers. event_handlers: ClassVar[builtins.dict[str, EventHandler]] = {} - # Mapping of var name to set of (state_full_name, var_name) that depend on it. - _var_dependencies: ClassVar[builtins.dict[str, set[tuple[str, str]]]] = {} + # Mapping of var name to set of (state class, computed var name) that depend on it. + _var_dependencies: ClassVar[ + builtins.dict[str, set[tuple[type[BaseState], str]]] + ] = {} # Set of vars which always need to be recomputed _always_dirty_computed_vars: ClassVar[set[str]] = set() # Set of substates which always need to be recomputed - _always_dirty_substates: ClassVar[set[str]] = set() + _always_dirty_substates: ClassVar[set[type[BaseState]]] = set() # The names the dev-mode __setattr__ has found declared, per state class. _settable_names: ClassVar[set[str]] = set() # Set of states which might need to be recomputed if vars in this state change. - _potentially_dirty_states: ClassVar[set[str]] = set() + _potentially_dirty_states: ClassVar[set[type[BaseState]]] = set() + + # Set once __init_subclass__ has registered this exact class. Checked via + # ``cls.__dict__`` so a subclass never inherits its parent's flag, and kept + # name-independent so swapping the NameResolver can't invalidate it. + _is_registered: ClassVar[bool] = False # Vars on this class sent to the client, and computed vars that expire on # an interval; recomputed with the dependency dicts, i.e. at class creation # and after any var is added. _frontend_var_names: ClassVar[frozenset[str]] = frozenset() _interval_computed_var_names: ClassVar[frozenset[str]] = frozenset() + # The key each frontend var goes by on the wire, and the vars by that key. + _var_keys: ClassVar[builtins.dict[str, str]] = {} + _var_names_by_key: ClassVar[builtins.dict[str, str]] = {} # Instance bookkeeping, kept out of `__dict__`: never a field, never pickled. __slots__ = ( @@ -722,6 +756,9 @@ class BaseState(EvenMoreBasicBaseState, state_root=True): # A special event handler for setting base vars. setvar: ClassVar[EventHandler] + # The `router` switchboard, built on first access and cached on the root. + _reflex_router_var: ClassVar[RouterDataVar | None] = None + # Held the backend vars in pickles of previous releases: reserved, so that # loading one of those cannot clash with a field. _backend_vars: ClassVar[None] = None @@ -844,38 +881,29 @@ def __init_subclass__(cls, mixin: bool = False, **kwargs): if parent_state is not None and cls.get_name() in { c.get_name() for c in parent_state.get_substates() }: - # This should not happen, since we have added module prefix to state names in #3214 - msg = ( - f"The substate class '{cls.get_name()}' has been defined multiple times. " - "Shadowing substate classes is not allowed." - ) + if ( + RegistrationContext.get().name_resolver.resolve_state_name(cls) + is not None + ): + msg = ( + f"'{get_state_full_path(cls)}' resolves to the name " + f"'{cls.get_name()}', which a sibling under " + f"'{get_state_full_path(parent_state)}' already uses. Fix the " + f"duplicate id in {MINIFY_JSON}; 'reflex minify validate' " + "reports it." + ) + else: + # Module prefixes make this unreachable for distinct classes. + msg = ( + f"The substate class '{cls.get_name()}' has been defined multiple times. " + "Shadowing substate classes is not allowed." + ) raise StateValueError(msg) explicit = cls.is_user_defined() and get_state_explicit_event_handlers() cls._bind_fields() mixin_handlers = cls._bind_mixin_members(explicit) - - # Set the base and computed vars. - cls.base_vars = { - name: get_var_for_field(cls, name, f) - for name, f in cls.__fields__.items() - if f._owner is cls and not f._backend - } - cls.computed_vars = { - name: v._replace(merge_var_data=VarData.from_state(cls)) - for name, v in cls.__dict__.items() - if is_computed_var(v) - } - cls.vars = { - **(parent_state.vars if parent_state is not None else {}), - **cls.base_vars, - **cls.computed_vars, - # `router` is a switchboard over the per-field router vars rather - # than a field of its own, but it is usable as a Var everywhere one - # is accepted, so it is listed here (and thus inherited by - # substates). It has no backing field, so it never reaches a delta. - constants.ROUTER: _get_router_var(cls), - } + cls._init_vars() cls.event_handlers = {} # Setup the base vars at the class level. @@ -912,7 +940,51 @@ def __init_subclass__(cls, mixin: bool = False, **kwargs): cls._var_dependencies = {} cls._init_var_dependency_dicts() - all_base_state_classes[cls.get_full_name()] = None + all_base_state_classes[cls._get_default_full_name()] = None + cls._is_registered = True + + @classmethod + def _init_vars(cls) -> None: + """Build the Vars standing for the fields and computed vars of this class. + + They name the state the way the active name resolver does, so + installing another resolver builds them again, parents first. + """ + parent_state = cls.get_parent_state() + cls.base_vars = { + name: get_var_for_field(cls, name, f) + for name, f in cls.__fields__.items() + if f._owner is cls and not f._backend + } + cls.computed_vars = { + name: v._replace(_var_data=VarData.from_state(cls, name)) + for name, v in cls.__dict__.items() + if is_computed_var(v) + } + cls.vars = { + **(parent_state.vars if parent_state is not None else {}), + **cls.base_vars, + **cls.computed_vars, + # `router` is a switchboard over the per-field router vars rather + # than a field of its own, but it is usable as a Var everywhere one + # is accepted, so it is listed here (and thus inherited by + # substates). It has no backing field, so it never reaches a delta. + constants.ROUTER: _get_router_var(cls), + } + for name, prop in cls.base_vars.items(): + cls._bind_var(name, prop) + + @classmethod + def _rebuild_vars(cls) -> None: + """Rename this state's Vars after a name resolver was installed. + + Called for every registered state, parents first. Dependency tracking + keys on classes and default names, so only the Vars need rebuilding. + """ + if "_reflex_router_var" in cls.__dict__: + cls._reflex_router_var = None + cls._init_vars() + cls._init_frontend_var_names() @classmethod def _bind_fields(cls) -> None: @@ -1003,17 +1075,21 @@ def _add_event_handler( cls, name: str, fn: Callable, - ): + ) -> EventHandler: """Add an event handler dynamically to the state. Args: name: The name of the event handler. fn: The function to call when the event is triggered. + + Returns: + The created EventHandler instance. """ _validate_state_name(cls._reflex_state_root, name) handler = cls._create_event_handler(fn) cls.event_handlers[name] = handler setattr(cls, name, handler) + return handler @staticmethod def _copy_fn(fn: Callable) -> Callable: @@ -1192,9 +1268,7 @@ def _init_var_dependency_dicts(cls): Additional updates tracking dicts for vars and substates that always need to be recomputed. """ - cls._frontend_var_names = frozenset(cls.base_vars).union( - name for name, cvar in cls.computed_vars.items() if not cvar._backend - ) + cls._init_frontend_var_names() cls._interval_computed_var_names = frozenset( name for name, cvar in cls.computed_vars.items() @@ -1222,23 +1296,11 @@ def _init_var_dependency_dicts(cls): dvar_set = (dvar_set - {constants.ROUTER}) | set( constants.ROUTER_VARS ) - state_cls = cls.get_root_state().get_class_substate(state_name) + state_cls = RegistrationContext.get()._get_state_by_default_name( + state_name + ) for dvar in dvar_set: - # The var is tracked on the state its descriptor is bound to. - desc = _inherited_value(state_cls.__mro__, dvar) - owner = ( - desc._owner - if isinstance(desc, Field) or is_computed_var(desc) - else None - ) - defining_state_cls = owner or state_cls - defining_state_cls._var_dependencies.setdefault(dvar, set()).add(( - cls.get_full_name(), - cvar_name, - )) - defining_state_cls._potentially_dirty_states.add( - cls.get_full_name() - ) + cls._register_var_dependency(state_cls, dvar, cvar_name) # ComputedVar with cache=False always need to be recomputed cls._always_dirty_computed_vars = { @@ -1250,14 +1312,10 @@ def _init_var_dependency_dicts(cls): # Any substate containing a ComputedVar with cache=False always needs to be recomputed if cls._always_dirty_computed_vars: # Tell parent classes that this substate has always dirty computed vars - state_name = cls.get_name() - parent_state = cls.get_parent_state() + state_cls, parent_state = cls, cls.get_parent_state() while parent_state is not None: - parent_state._always_dirty_substates.add(state_name) - state_name, parent_state = ( - parent_state.get_name(), - parent_state.get_parent_state(), - ) + parent_state._always_dirty_substates.add(state_cls) + state_cls, parent_state = parent_state, parent_state.get_parent_state() # Reset cached schema values; the frontend schema spans the tree, so a # change to any state invalidates every cached root. @@ -1265,6 +1323,40 @@ def _init_var_dependency_dicts(cls): _frontend_schema.cache_clear() _shared_storage_groups.cache_clear() + @classmethod + def _init_frontend_var_names(cls) -> None: + """Collect the vars of this class sent to the client, and their wire keys.""" + cls._frontend_var_names = frozenset(cls.base_vars).union( + name for name, cvar in cls.computed_vars.items() if not cvar._backend + ) + cls._var_keys = { + name: format.format_var_key(cls, name) for name in cls._frontend_var_names + } + cls._var_names_by_key = {key: name for name, key in cls._var_keys.items()} + + @classmethod + def _register_var_dependency( + cls, state_cls: type[BaseState], var_name: str, cvar_name: str + ) -> None: + """Mark a computed var of this class dirty whenever a var of a state changes. + + Args: + state_cls: The state the var is read from. + var_name: The name of the var the computed var depends on. + cvar_name: The name of the computed var on this class. + """ + # The var is tracked on the state its descriptor is bound to. + desc = _inherited_value(state_cls.__mro__, var_name) + owner = ( + desc._owner if isinstance(desc, Field) or is_computed_var(desc) else None + ) + defining_state_cls = owner or state_cls + defining_state_cls._var_dependencies.setdefault(var_name, set()).add(( + cls, + cvar_name, + )) + defining_state_cls._potentially_dirty_states.add(cls) + @classmethod def _iter_functions(cls) -> Iterator[tuple[str, FunctionType]]: """Iterate over the functions defined on the class and its bases. @@ -1337,19 +1429,31 @@ def get_substates(cls) -> set[type[BaseState]]: """ return RegistrationContext.get().get_substates(cls) + # Unbounded on purpose: the resolved name depends on the active resolver, + # which lives in a ContextVar, so an evicted entry can be recomputed from a + # scope that has no context and pin the default name process-wide. Not per + # class: installing a resolver must clear every class, registered or not. @classmethod - @_cache_per_class + @functools.cache def get_name(cls) -> str: - """Get the name of the state. + """Get the user-visible name of the state. + + Defers to the active :class:`~reflex_base.registry.NameResolver` + (e.g. ``minify.json`` rewrites), falling back to the built-in + snake-cased ``module___ClassName`` when no context is attached. Returns: - The name of the state. + The resolved name of the state. """ - module = cls.__module__.replace(".", "___") - return format.to_snake_case(f"{module}___{cls.__name__}") + from reflex_base.registry import RegistrationContext + + ctx = RegistrationContext.try_get() + if ctx is None: + return RegistrationContext.default_state_name(cls) + return ctx.get_state_name(cls) @classmethod - @_cache_per_class + @functools.cache def get_full_name(cls) -> str: """Get the full name of the state. @@ -1362,6 +1466,44 @@ def get_full_name(cls) -> str: name = parent_state.get_full_name() + "." + name return name + @classmethod + @functools.cache + def _get_default_full_name(cls) -> str: + """Get the full name of the state under the built-in names. + + Unlike :meth:`get_full_name`, it never depends on the name resolver, so + it keys what must survive a resolver change: persisted state, shared + state links, browser storage and the backend's dependency tracking. + + Returns: + The dotted path of built-in ``module___ClassName`` names. + """ + name = RegistrationContext.default_state_name(cls) + parent_state = cls.get_parent_state() + if parent_state is not None: + name = parent_state._get_default_full_name() + "." + name + return name + + @classmethod + def _get_source_module(cls) -> str: + """Get the module whose code defines the state, wherever its class was put. + + States defined inside a function and the ones ``ComponentState.create()`` + builds live in ``reflex.istate.dynamic``, for pickling. + + Returns: + The name of the defining module. + """ + # Its own attribute: a subclass would inherit a locally defined parent's. + if (original_module := cls.__dict__.get("__original_module__")) is not None: + return original_module + if cls.__module__ == reflex.istate.dynamic.__name__ and issubclass( + cls, ComponentState + ): + # Built from the component class, its first base. + return cls.__bases__[0].__module__ + return cls.__module__ + @classmethod @functools.lru_cache def _get_substate_path(cls) -> tuple[str, ...]: @@ -1374,11 +1516,16 @@ def _get_substate_path(cls) -> tuple[str, ...]: @classmethod @functools.lru_cache - def get_class_substate(cls, path: Sequence[str] | str) -> type[BaseState]: + def get_class_substate( + cls, path: Sequence[str] | str, _skip_self: bool = True + ) -> type[BaseState]: """Get the class substate. Args: path: The path to the substate. + _skip_self: Internal recursion flag; leave at the default. Allows + ``"a.b.b"`` to resolve to a child that shares its parent's + minified name. Returns: The class substate. @@ -1391,13 +1538,13 @@ def get_class_substate(cls, path: Sequence[str] | str) -> type[BaseState]: if len(path) == 0: return cls - if path[0] == cls.get_name(): + if _skip_self and path[0] == cls.get_name(): if len(path) == 1: return cls path = path[1:] for substate in cls.get_substates(): if path[0] == substate.get_name(): - return substate.get_class_substate(path[1:]) + return substate.get_class_substate(path[1:], _skip_self=False) msg = f"Invalid path: {path}" raise ValueError(msg) @@ -1435,7 +1582,7 @@ def is_user_defined(cls) -> bool: @classmethod def _init_var(cls, name: str, prop: Var): - """Initialize a variable. + """Validate the type of a base var and give it a setter. Args: name: The name of the variable @@ -1455,7 +1602,6 @@ def _init_var(cls, name: str, prop: Var): ) raise VarTypeError(msg) field = cls.__fields__[name] - field._var = prop if types.safe_issubclass(field.type_, ClientStorageBase) or any( issubclass(member, ClientStorageBase) for member in field._plain_types ): @@ -1463,6 +1609,16 @@ def _init_var(cls, name: str, prop: Var): field._plain_types |= {str} if cls.is_user_defined() and get_state_auto_setters() is True: cls._create_setter(name, prop) + + @classmethod + def _bind_var(cls, name: str, prop: Var): + """Make a Var the one its field returns on class access. + + Args: + name: The name of the field. + prop: The Var standing for the field. + """ + cls.__fields__[name]._var = prop cls._set_default_value(name, prop) @classmethod @@ -1498,10 +1654,7 @@ def add_var(cls, name: str, type_: Any, default_value: Any = None): # create the variable based on name and type var = Var( - _js_expr=format.format_state_name(cls.get_full_name()) - + "." - + name - + FIELD_MARKER, + _js_expr=format.format_state_var(cls, name), _var_type=type_, _var_data=VarData.from_state(cls, name), ).guess_type() @@ -1509,6 +1662,7 @@ def add_var(cls, name: str, type_: Any, default_value: Any = None): # add the field dynamically (must be done before _init_var) cls.add_field(name, var, default_value) + cls._bind_var(name, var) cls._init_var(name, var) cls.base_vars[name] = var @@ -1532,7 +1686,7 @@ def _create_event_handler( event_actions = getattr(fn, EVENT_ACTIONS_MARKER, {}) handler = event_handler_cls(fn=fn, state=cls, event_actions=event_actions) - if cls.get_full_name() in all_base_state_classes: + if cls.__dict__.get("_is_registered", False): # Register handlers created after the class was registered. reg_ctx = RegistrationContext.get() reg_ctx.register_event_handler(handler, states=(cls,)) @@ -1541,7 +1695,9 @@ def _create_event_handler( @classmethod def _create_setvar(cls): """Create the setvar method for the state.""" - cls.setvar = cls.event_handlers["setvar"] = EventHandlerSetVar(state_cls=cls) + cls.setvar = cls.event_handlers[constants.event.SETVAR] = EventHandlerSetVar( + state_cls=cls + ) @classmethod def _create_setter(cls, name: str, prop: Var): @@ -1835,6 +1991,9 @@ def _reset_client_storage(self): def get_substate(self, path: Sequence[str]) -> BaseState: """Get the substate. + Only the first segment may name this state itself, so ``"a.b.b"`` + resolves to a child that shares its parent's (minified) name. + Args: path: The path to the substate. @@ -1845,12 +2004,8 @@ def get_substate(self, path: Sequence[str]) -> BaseState: ValueError: If the substate is not found. """ state = self - index = 0 + index = 1 if path and path[0] == self.get_name() else 0 while index < len(path): - if path[index] == state.get_name(): - index += 1 - if index == len(path): - return state if path[index] not in state.substates: msg = f"Invalid path: {path[index:]}" raise ValueError(msg) @@ -1865,13 +2020,7 @@ def _get_potentially_dirty_states(cls) -> set[type[BaseState]]: Returns: The set of potentially dirty substate classes. """ - return { - cls.get_class_substate(substate_name) - for substate_name in cls._always_dirty_substates - }.union({ - cls.get_root_state().get_class_substate(substate_name) - for substate_name in cls._potentially_dirty_states - }) + return cls._always_dirty_substates | cls._potentially_dirty_states def _get_root_state(self) -> BaseState: """Get the root state of the state tree. @@ -2004,7 +2153,7 @@ async def get_var_value(self, var: Var[VAR_TYPE]) -> VAR_TYPE: msg = f"Unable to retrieve value for {var._js_expr}: not associated with any state." raise UnretrievableVarValueError(msg) # Fastish case: this var belongs to this state - if var_data.state == self.get_full_name(): + if var_data.state == self._get_default_full_name(): value = getattr(self, var_data.field_name) if inspect.isawaitable(value): return await value @@ -2012,7 +2161,7 @@ async def get_var_value(self, var: Var[VAR_TYPE]) -> VAR_TYPE: # Slow case: this var belongs to another state other_state = await self.get_state( - self._get_root_state().get_class_substate(var_data.state) + RegistrationContext.get()._get_state_by_default_name(var_data.state) ) value = getattr(other_state, var_data.field_name) if inspect.isawaitable(value): @@ -2053,18 +2202,21 @@ def _mark_dirty_computed_vars( ] if not pending: return - seen: set[tuple[str, str]] = set() + seen: set[tuple[type[BaseState], str]] = set() while pending: state, name = pending.pop() for dependent in state._var_dependencies[name]: if dependent in seen: continue seen.add(dependent) - state_name, cvar_name = dependent - if state_name == state.get_full_name(): + state_cls, cvar_name = dependent + # The class of the state, also when `state` is a StateProxy. + if state_cls is state.__class__: target = state else: - target = state._get_root_state().get_substate(state_name.split(".")) + target = state._get_root_state().get_substate( + state_cls._get_substate_path() + ) target._mark_ancestors_dirty() if invalidate: target.computed_vars[cvar_name].mark_dirty(instance=target) @@ -2211,9 +2363,11 @@ def dict( else: computed_vars = {} variables = {**base_vars, **computed_vars} + # A StateProxy runs this as self; its __class__ is the wrapped state's. + var_keys = self.__class__._var_keys d = { self.get_full_name(): { - k + FIELD_MARKER: variables[k] for k in sorted(variables) + var_keys[k]: variables[k] for k in sorted(variables) }, } for substate_d in [ @@ -2344,7 +2498,9 @@ def _to_frontend_schema(cls) -> str: event handler names. Defaults and values are left out, so a frontend compiled from one tree matches a backend running another exactly when the two trees are compatible. The compiler writes this hash into the - frontend and the backend compares it on each socket connect. + frontend and the backend compares it on each socket connect. All names + are the resolver-independent ones: the scheme digest covers how a + name resolver rewrites them. Returns: The hash of the frontend schema. @@ -2468,12 +2624,11 @@ def _shared_storage_groups( local_storage = _compile_client_storage_recursive(root, check_factories=False)[1] for state_key, options in local_storage.items(): if name := options.get("name"): - state_name, _, var_name = state_key.removesuffix(FIELD_MARKER).rpartition( - "." - ) + state_name, _, var_key = state_key.rpartition(".") + state_cls = root.get_class_substate(state_name) by_name.setdefault(name, []).append(( - root.get_class_substate(state_name), - var_name, + state_cls, + state_cls._var_names_by_key[var_key], bool(options.get("sync")), )) # Sorted, so conflicting writes to one name set the other vars the same way @@ -2553,7 +2708,7 @@ def _frontend_schema(root: type[BaseState], context: RegistrationContext) -> str def _state_entry(state: type[BaseState]) -> list[Any]: fields = state.get_fields() return [ - state.get_full_name(), + state._get_default_full_name(), sorted( (name, _serialize_type(fields[name].type_)) for name in state.base_vars ), @@ -2655,7 +2810,7 @@ async def _get_state_from_redis(self, state_cls: type[T_STATE]) -> T_STATE: self._reflex_internal_links and ( linked_token := self._reflex_internal_links.get( - state_cls.get_full_name() + state_cls._get_default_full_name() ) ) is not None @@ -2944,17 +3099,18 @@ async def _apply_client_storage_vars( Args: state: Any state in the tree; used to reach the owning substates. - vars: Fully qualified var names mapped to their browser values. + vars: Values keyed by ``{state.get_full_name()}.{var_key}``, where + ``var_key`` is the key the var goes by on the wire. Returns: Each var that was set, with the state that owns it. """ applied: list[tuple[BaseState, str]] = [] for var, value in vars.items(): - state_name, _, var_name = var.rpartition(".") - var_name = var_name.removesuffix(FIELD_MARKER) + state_name, _, var_key = var.rpartition(".") var_state_cls = State.get_class_substate(state_name) - if var_state_cls._is_client_storage(var_name): + var_name = var_state_cls._var_names_by_key.get(var_key) + if var_name is not None and var_state_cls._is_client_storage(var_name): var_state = await state.get_state(var_state_cls) setattr(var_state, var_name, value) applied.append((var_state, var_name)) @@ -3051,7 +3207,7 @@ class FrontendEventExceptionState(State): ), ] - @event + @_typing_event def handle_frontend_exception( self, info: str, component_stack: str ) -> Iterator[EventSpec]: @@ -3092,11 +3248,13 @@ def handle_frontend_exception( class UpdateVarsInternalState(State): """Substate for handling internal state var updates.""" + @_typing_event async def update_vars_internal(self, vars: dict[str, Any]) -> None: """Apply updates to fully qualified state vars. - The keys in `vars` should be in the form of `{state.get_full_name()}.{var_name}`, - and each value will be set on the appropriate substate instance. + The keys in `vars` should be in the form of `{state.get_full_name()}.{var_key}`, + where `var_key` is the key the var goes by on the wire, and each value + will be set on the appropriate substate instance. This function is primarily used to apply cookie and local storage updates from the frontend to the appropriate substate. @@ -3223,20 +3381,65 @@ def get_component(cls, *children, **props) -> Component: raise NotImplementedError(msg) @classmethod - def create(cls, *children, **props) -> Component: + def create(cls, *children, _state_key: str | None = None, **props) -> Component: """Create a new instance of the Component. Args: children: The children of the component. + _state_key: Names this instance's state. Must be a valid Python + identifier without uppercase letters and unique among the keyed + instances of components with this class name. Unkeyed instances + are named by creation order instead. props: The props of the component. Returns: A new instance of the Component with an independent copy of the State. + + Raises: + ValueError: If ``_state_key`` is not a string usable as a name segment. + StateValueError: If ``_state_key`` is already used by another instance. """ + from reflex_base.utils.exceptions import StateValueError + from reflex.compiler.compiler import into_component - cls._per_component_state_instance_count += 1 - state_cls_name = f"{cls.__name__}_n{cls._per_component_state_instance_count}" + if _state_key is not None: + # It becomes part of a class name, so anything that is not an + # identifier string is rejected here rather than further in. The + # default state name is snake-cased, which reads ``cartItem`` as + # ``cart_item``, so capitals would let two keys name one state. + if ( + not isinstance(_state_key, str) + or not _state_key.isidentifier() + or _state_key != _state_key.lower() + ): + msg = ( + "_state_key must be a string that is a valid Python identifier " + f"without uppercase letters, got {_state_key!r}." + ) + raise ValueError(msg) + # Doubled separator: an unkeyed name is always ``_n`` followed by + # digits, so no key can produce one. + state_cls_name = f"{cls.__name__}__{_state_key}" + # Report the key rather than letting the generated class name reach + # the sibling-shadowing guard, whose message names neither. + existing = getattr(reflex.istate.dynamic, state_cls_name, None) + if existing is not None: + owner = existing.__mro__[1] + msg = ( + f"_state_key={_state_key!r} is already used by a " + f"{owner.__module__}.{owner.__qualname__} instance. A key names " + "one state per component: create the component once and reuse " + "that instance on every page that renders it, or pick another key." + ) + raise StateValueError(msg) + else: + # Keyed instances do not advance the counter, so adding one leaves + # the unkeyed names alone. + cls._per_component_state_instance_count += 1 + state_cls_name = ( + f"{cls.__name__}_n{cls._per_component_state_instance_count}" + ) component_state = type( state_cls_name, (cls, State), @@ -3324,21 +3527,24 @@ def reload_state_module( """ # Clean out all potentially dirty states of reloaded modules. - for pd_state in tuple(state._potentially_dirty_states): - with contextlib.suppress(ValueError): - if ( - state.get_root_state().get_class_substate(pd_state).__module__ == module - and module is not None - ): - state._potentially_dirty_states.remove(pd_state) + if module is not None: + state._potentially_dirty_states = { + pd_state + for pd_state in state._potentially_dirty_states + if pd_state._get_source_module() != module + } reg_ctx = RegistrationContext.get() substates = reg_ctx.get_substates(state) for subclass in tuple(substates): reload_state_module(module=module, state=subclass) - if subclass.__module__ == module and module is not None: - all_base_state_classes.pop(subclass.get_full_name(), None) + if subclass._get_source_module() == module and module is not None: + all_base_state_classes.pop(subclass._get_default_full_name(), None) + subclass._is_registered = False substates.remove(subclass) - state._always_dirty_substates.discard(subclass.get_name()) + # Free its name there, or the reloaded module cannot build it again. + if vars(reflex.istate.dynamic).get(subclass.__name__) is subclass: + delattr(reflex.istate.dynamic, subclass.__name__) + state._always_dirty_substates.discard(subclass) state._var_dependencies = {} state._init_var_dependency_dicts() state.get_class_substate.cache_clear() diff --git a/reflex/testing.py b/reflex/testing.py index 1a68919ab63..271e92c9863 100644 --- a/reflex/testing.py +++ b/reflex/testing.py @@ -39,7 +39,7 @@ import reflex.utils.prerequisites import reflex.utils.processes from reflex.istate.shared import SharedState as SharedState # To register it. -from reflex.state import reload_state_module +from reflex.state import State, reload_state_module from reflex.utils.exec import _frontend_dev_command, _with_development_condition from reflex.utils.export import export from reflex.utils.token_manager import TokenManager @@ -213,9 +213,10 @@ def get_full_state_name(self, path: list[str]) -> str: Returns: The full state name """ - # NOTE: using State.get_name() somehow causes trouble here - # path = [State.get_name()] + [self.get_state_name(p) for p in path] # noqa: ERA001 - path = ["reflex___state____state"] + [self.get_state_name(p) for p in path] + # Default scheme like get_state_name; get_name() would apply the resolver. + path = [RegistrationContext.default_state_name(State)] + [ + self.get_state_name(p) for p in path + ] return ".".join(path) def _get_globals_from_signature(self, func: Any) -> dict[str, Any]: @@ -507,7 +508,13 @@ def stop(self) -> None: self._reload_state_module() if self._registry_token is not None: + app_resolver = RegistrationContext.get().name_resolver RegistrationContext.reset(self._registry_token) + restored = RegistrationContext.ensure_context() + # Names are cached on the state classes the contexts share, so + # an app that ran under another resolver leaves them renamed. + if restored.name_resolver is not app_resolver: + restored.set_name_resolver(restored.name_resolver) if self.backend is not None: self.backend.should_exit = True diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index c7e32ac36ab..7d1de487c0d 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -247,6 +247,7 @@ def get_app(reload: bool = False) -> ModuleType: Raises: Exception: If an error occurs while getting the app module. """ + from reflex.minify import ensure_minify_resolver_for_active_context from reflex.utils import telemetry try: @@ -258,6 +259,9 @@ def get_app(reload: bool = False) -> ModuleType: module = config.module sys.path.insert(0, getcwd()) # noqa: PTH109 + # Before the user module imports, so the Vars its module-level code + # builds from state Vars use the configured names. + ensure_minify_resolver_for_active_context() app = ( __import__(module, fromlist=(constants.CompileVars.APP,)) if not config.app_module diff --git a/tests/benchmarks/test_shared_state.py b/tests/benchmarks/test_shared_state.py index cb8cfa36a0c..b199ab971c0 100644 --- a/tests/benchmarks/test_shared_state.py +++ b/tests/benchmarks/test_shared_state.py @@ -337,7 +337,7 @@ async def emit_impl( # noqa: RUF029 mock.patch.object( rx.State, "_always_dirty_substates", - rx.State._always_dirty_substates | {SharedStateBaseInternal.get_name()}, + rx.State._always_dirty_substates | {SharedStateBaseInternal}, ), ): state_manager = StateManagerMemory() diff --git a/tests/integration/test_minification.py b/tests/integration/test_minification.py new file mode 100644 index 00000000000..2f1bb4908b5 --- /dev/null +++ b/tests/integration/test_minification.py @@ -0,0 +1,313 @@ +"""Integration tests for state and event handler minification.""" + +from __future__ import annotations + +import json +from collections.abc import Generator +from typing import TYPE_CHECKING + +import pytest +from reflex_base.registry import RegistrationContext +from selenium.webdriver.common.by import By + +from reflex.environment import environment +from reflex.minify import ( + MINIFY_JSON, + SCHEMA_VERSION, + clear_config_cache, + int_to_minified_name, +) +from reflex.state import State +from reflex.testing import AppHarness + +if TYPE_CHECKING: + from selenium.webdriver.remote.webdriver import WebDriver + + +def MinificationApp(): + """Test app with one root + one substate, each with one event handler.""" + import reflex as rx + from reflex.utils import format + + class RootState(rx.State): + count: int = 0 + token_cookie: str = rx.Cookie("") + label: str = "count" + + @rx.var + def doubled(self) -> int: + return self.count * 2 + + @rx.event + def increment(self): + self.count += 1 + + @rx.event + def save_cookie(self): + self.token_cookie = f"tok-{self.count}" + + class SubState(RootState): + message: str = "hello" + + @rx.event + def update_message(self): + parent = self.parent_state + assert parent is not None + assert isinstance(parent, RootState) + self.message = f"count is {parent.count}" + + increment_handler_name = format.format_event_handler( + RootState.event_handlers["increment"] + ) + update_handler_name = format.format_event_handler( + SubState.event_handlers["update_message"] + ) + + def index() -> rx.Component: + return rx.vstack( + rx.input( + value=RootState.router.session.client_token, + is_read_only=True, + id="token", + ), + rx.text(f"Root state name: {RootState.get_name()}", id="root_state_name"), + rx.text(f"Sub state name: {SubState.get_name()}", id="sub_state_name"), + rx.text( + f"Increment handler: {increment_handler_name}", + id="increment_handler_name", + ), + rx.text(f"Update handler: {update_handler_name}", id="update_handler_name"), + rx.text(f"Count key: {RootState.count!s}", id="count_key"), + rx.text(RootState.count, id="count_value"), + rx.text(RootState.doubled, id="doubled_value"), + rx.text(RootState.token_cookie, id="cookie_value"), + # Reads two fields of one state, which subscribe through one hook. + rx.text(f"{RootState.label}: {RootState.count}", id="label_and_count"), + rx.text(SubState.message, id="message_value"), + rx.button("Increment", on_click=RootState.increment, id="increment_btn"), + rx.button("Save Cookie", on_click=RootState.save_cookie, id="cookie_btn"), + rx.button( + "Update Message", on_click=SubState.update_message, id="update_msg_btn" + ), + ) + + app = rx.App() + app.add_page(index) + + +# The framework states already exist when AppHarness loads this config, well +# after pytest imported ``reflex.state``: installing it renames their Vars too. +_FRAMEWORK_STATES = { + "reflex.state.State": {"id": "a", "parent": None}, + **{ + f"reflex.state.State.{name}": { + "id": minified_id, + "parent": "reflex.state.State", + } + for name, minified_id in ( + ("FrontendEventExceptionState", "b"), + ("OnLoadInternalState", "c"), + ("UpdateVarsInternalState", "d"), + ) + }, + "reflex.istate.shared.State.SharedStateBaseInternal": { + "id": "e", + "parent": "reflex.state.State", + }, +} +_MINIFY_CONFIG = { + "version": SCHEMA_VERSION, + "states": { + **_FRAMEWORK_STATES, + "minify_enabled.minify_enabled.State.RootState": { + "id": "k", # 10 + "parent": "reflex.state.State", + }, + "minify_enabled.minify_enabled.State.RootState.SubState": { + "id": "l", # 11 + "parent": "minify_enabled.minify_enabled.State.RootState", + }, + }, + "events": { + "reflex.state.State": {"hydrate": "a", "set_is_hydrated": "b"}, + "reflex.state.State.OnLoadInternalState": {"on_load_internal": "a"}, + "reflex.state.State.UpdateVarsInternalState": {"update_vars_internal": "a"}, + "reflex.state.State.FrontendEventExceptionState": { + "handle_frontend_exception": "a" + }, + "minify_enabled.minify_enabled.State.RootState": { + "increment": "f", # 5 + "save_cookie": "g", # 6 + }, + "minify_enabled.minify_enabled.State.RootState.SubState": { + "update_message": "h" # 7 + }, + }, + "vars": { + "reflex.state.State": {"is_hydrated": "h", "rx_router_session": "s"}, + "minify_enabled.minify_enabled.State.RootState": { + "count": "c", + "doubled": "d", + "token_cookie": "t", + }, + "minify_enabled.minify_enabled.State.RootState.SubState": {"message": "m"}, + }, +} + + +@pytest.fixture(params=["disabled", "enabled", "foreign"]) +def minify_app( + request: pytest.FixtureRequest, + app_harness_env: type[AppHarness], + tmp_path_factory: pytest.TempPathFactory, + monkeypatch: pytest.MonkeyPatch, +) -> Generator[tuple[bool, AppHarness], None, None]: + """Run :func:`MinificationApp` in each way a ``minify.json`` can reach it. + + ``foreign`` runs it from a process that already loaded another app's + ``minify.json``, as an embedded test app next to a minified repository. + + Yields: + ``(minified, harness)``. + """ + mode: str = request.param + minified = mode == "enabled" + if mode != "disabled": + monkeypatch.setenv(environment.REFLEX_MINIFY_STATES.name, "1") + monkeypatch.setenv(environment.REFLEX_MINIFY_EVENTS.name, "1") + monkeypatch.setenv(environment.REFLEX_MINIFY_VARS.name, "1") + if mode == "foreign": + outside = tmp_path_factory.mktemp("foreign_config") + (outside / MINIFY_JSON).write_text(json.dumps(_MINIFY_CONFIG)) + monkeypatch.chdir(outside) + clear_config_cache() + + # The foreign config names this very app, so only the directory stops it. + app_name = "minify_disabled" if mode == "disabled" else "minify_enabled" + app_root = tmp_path_factory.mktemp(app_name) + harness = app_harness_env.create( + root=app_root, app_name=app_name, app_source=MinificationApp + ) + if mode == "enabled": + (app_root / MINIFY_JSON).write_text(json.dumps(_MINIFY_CONFIG)) + + try: + with harness: + yield minified, harness + if mode == "foreign": + # The embedded app's names do not outlive it. + resolver = RegistrationContext.get().name_resolver + assert State.get_name() == ( + resolver.resolve_state_name(State) + or RegistrationContext.default_state_name(State) + ) + finally: + # Put the default names back for the tests that share this process. + monkeypatch.undo() + clear_config_cache() + + +@pytest.fixture +def driver( + minify_app: tuple[bool, AppHarness], +) -> Generator[WebDriver, None, None]: + """WebDriver scoped to the parametrized :func:`minify_app`. + + Yields: + A WebDriver pointed at the running app. + """ + _enabled, harness = minify_app + assert harness.app_instance is not None, "app is not running" + drv = harness.frontend() + try: + yield drv + finally: + drv.quit() + + +def _text_after_colon(text: str) -> str: + """Strip the ``"label: "`` prefix from a UI element's text. + + Args: + text: The element text. + + Returns: + The substring after the first ``": "``, or ``text`` unchanged. + """ + return text.split(": ", 1)[-1] if ": " in text else text + + +def test_minification( + minify_app: tuple[bool, AppHarness], + driver: WebDriver, +) -> None: + """State and event handler names follow the minify config when enabled.""" + enabled, harness = minify_app + assert harness.app_instance is not None + + token_input = AppHarness.poll_for_or_raise_timeout( + lambda: driver.find_element(By.ID, "token") + ) + assert harness.poll_for_value(token_input) + + root_name = driver.find_element(By.ID, "root_state_name").text + sub_name = driver.find_element(By.ID, "sub_state_name").text + increment_name = _text_after_colon( + driver.find_element(By.ID, "increment_handler_name").text + ) + update_name = _text_after_colon( + driver.find_element(By.ID, "update_handler_name").text + ) + + count_key = _text_after_colon(driver.find_element(By.ID, "count_key").text) + + if enabled: + assert root_name.endswith(int_to_minified_name(10)) + assert sub_name.endswith(int_to_minified_name(11)) + assert ( + increment_name == f"a.{int_to_minified_name(10)}.{int_to_minified_name(5)}" + ) + assert update_name.endswith(f".{int_to_minified_name(7)}") + assert "increment" not in increment_name.lower() + assert "update_message" not in update_name.lower() + assert count_key == "$rx_a__k.c" + else: + assert count_key.endswith(".count_rx_state_") + assert increment_name.startswith("reflex___state____state.") + assert "root_state" in root_name.lower() + assert "sub_state" in sub_name.lower() + assert "increment" in increment_name.lower() + assert "update_message" in update_name.lower() + assert "." in increment_name + assert "." in update_name + + # Event dispatch sanity check (must work regardless of minification). + count = driver.find_element(By.ID, "count_value") + doubled = driver.find_element(By.ID, "doubled_value") + label_and_count = driver.find_element(By.ID, "label_and_count") + assert count.text == "0" + assert label_and_count.text == "count: 0" + driver.find_element(By.ID, "increment_btn").click() + AppHarness.poll_for_or_raise_timeout(lambda: count.text == "1") + AppHarness.poll_for_or_raise_timeout(lambda: doubled.text == "2") + AppHarness.poll_for_or_raise_timeout(lambda: label_and_count.text == "count: 1") + + # The cookie is stored under the name it has without minification, so + # turning minification on or off keeps what browsers already hold. + cookie = driver.find_element(By.ID, "cookie_value") + driver.find_element(By.ID, "cookie_btn").click() + AppHarness.poll_for_or_raise_timeout(lambda: cookie.text == "tok-1") + app_name = harness.app_name + stored = driver.get_cookie( + f"reflex___state____state.{app_name}___{app_name}____root_state" + ".token_cookie_rx_state_" + ) + assert stored is not None + assert stored["value"] == "tok-1" + + if enabled: + # Substate handler dispatch through minified names. + message = driver.find_element(By.ID, "message_value") + driver.find_element(By.ID, "update_msg_btn").click() + AppHarness.poll_for_or_raise_timeout(lambda: "count is 1" in message.text) + assert message.text == "count is 1" diff --git a/tests/integration/tests_playwright/test_client_error.py b/tests/integration/tests_playwright/test_client_error.py index 4bdce672821..febf7a1d111 100644 --- a/tests/integration/tests_playwright/test_client_error.py +++ b/tests/integration/tests_playwright/test_client_error.py @@ -19,7 +19,7 @@ from collections.abc import Generator import pytest -from playwright.sync_api import ConsoleMessage, Page, expect +from playwright.sync_api import ConsoleMessage, Page, WebSocket, expect from reflex_base.registry import RegistrationContext from reflex.state import State, StateUpdate, _frontend_schema, all_base_state_classes @@ -273,10 +273,12 @@ class ClientErrorExtraState(State): context = RegistrationContext.get() context.get_substates(State).discard(ClientErrorExtraState) context.base_states.pop(ClientErrorExtraState.get_full_name(), None) + default_name = ClientErrorExtraState._get_default_full_name() + context._states_by_default_name.pop(default_name, None) for name, registered in list(context.event_handlers.items()): if ClientErrorExtraState in registered.states: del context.event_handlers[name] - all_base_state_classes.pop(ClientErrorExtraState.get_full_name(), None) + all_base_state_classes.pop(default_name, None) State.get_class_substate.cache_clear() _frontend_schema.cache_clear() @@ -364,6 +366,78 @@ def test_state_mismatch_prompts_reload( assert "no dispatch function" in mismatch_reports[0] +def test_scheme_mismatch_is_visible_to_the_viewer( + client_error_app: AppHarness, page: Page, monkeypatch: pytest.MonkeyPatch +): + """A scheme mismatch reaches the connection UI, not just the console. + + The backend emits the notice from its connect handler, so socket.io + buffers it and replays it before the client's own ``connect`` handler + runs. A ``connect`` handler that cleared every pending error would discard + the only signal the viewer gets, leaving a silently dead page. + + Args: + client_error_app: Running AppHarness instance. + page: Playwright page fixture. + monkeypatch: pytest fixture for patching the backend's digest. + """ + assert client_error_app.frontend_url is not None + # The bundle was compiled with the real digest; disagree only on the + # backend so the handshake fails the way a stale bundle would. + monkeypatch.setattr("reflex.app.scheme_digest", lambda: "other-scheme") + + page.goto(client_error_app.frontend_url) + + expect(page.get_by_text("This page is out of date")).to_be_visible(timeout=30000) + + +def test_scheme_mismatch_closes_the_socket( + client_error_app: AppHarness, page: Page, monkeypatch: pytest.MonkeyPatch +): + """A scheme mismatch drops the connection instead of idling on it. + + Nothing can be exchanged over the socket once the mismatch is known, so + keeping it open would only pin a server connection to a dead tab. The + client must also not reconnect: a reconnect cannot change what the names + mean, and would loop forever. + + Args: + client_error_app: Running AppHarness instance. + page: Playwright page fixture. + monkeypatch: pytest fixture for patching the backend's digest. + """ + assert client_error_app.frontend_url is not None + assert client_error_app.app_instance is not None + event_namespace = client_error_app.app_instance.event_namespace + assert event_namespace is not None + monkeypatch.setattr("reflex.app.scheme_digest", lambda: "other-scheme") + + # The dev server keeps its own HMR socket open; only the backend's matters. + backend_sockets: list[WebSocket] = [] + page.on( + "websocket", + lambda ws: backend_sockets.append(ws) if "/_event" in ws.url else None, + ) + + page.goto(client_error_app.frontend_url) + expect(page.get_by_text("This page is out of date")).to_be_visible(timeout=30000) + + assert AppHarness._poll_for( + lambda: backend_sockets and all(ws.is_closed() for ws in backend_sockets) + ), "the frontend kept its socket open after a fatal scheme mismatch" + + # The backend saw the disconnect and forgot the stale tab. + assert AppHarness._poll_for(lambda: not event_namespace._scheme_mismatch_sids), ( + "the backend still tracks the mismatched session" + ) + + # No reconnect loop: nothing reopens a socket afterwards. + opened = len(backend_sockets) + page.wait_for_timeout(2000) + assert len(backend_sockets) == opened + assert all(ws.is_closed() for ws in backend_sockets) + + def test_state_mismatch_handled_by_app( client_error_app_env: AppHarness, page: Page, mismatch_reports: list[str] ): diff --git a/tests/integration/tests_playwright/test_linked_state.py b/tests/integration/tests_playwright/test_linked_state.py index f93d53ca3c8..867dbbfcc20 100644 --- a/tests/integration/tests_playwright/test_linked_state.py +++ b/tests/integration/tests_playwright/test_linked_state.py @@ -2,6 +2,7 @@ from __future__ import annotations +import json import time import uuid from collections.abc import Callable, Generator @@ -11,6 +12,8 @@ from playwright.sync_api import Browser, Page, expect from reflex_base.config import get_config +from reflex.environment import environment +from reflex.minify import MINIFY_JSON, SCHEMA_VERSION, clear_config_cache from reflex.testing import AppHarness @@ -264,6 +267,50 @@ def linked_state(tmp_path_factory) -> Generator[AppHarness, None, None]: yield harness +_MINIFIED_APP = "linked_minified" + + +@pytest.fixture +def minified_linked_state( + tmp_path_factory, monkeypatch: pytest.MonkeyPatch +) -> Generator[AppHarness, None, None]: + """Start LinkedStateApp with its shared state names minified. + + Args: + tmp_path_factory: pytest tmp_path_factory fixture. + monkeypatch: pytest monkeypatch fixture. + + Yields: + The running app harness. + """ + monkeypatch.setenv(environment.REFLEX_MINIFY_STATES.name, "1") + app_root = tmp_path_factory.mktemp(_MINIFIED_APP) + harness = AppHarness.create( + root=app_root, app_name=_MINIFIED_APP, app_source=LinkedStateApp + ) + # Shared states hang off the framework's SharedStateBaseInternal. + parent = "reflex.istate.shared.State.SharedStateBaseInternal" + state_path = f"{_MINIFIED_APP}.{_MINIFIED_APP}.State.SharedStateBaseInternal" + (app_root / MINIFY_JSON).write_text( + json.dumps({ + "version": SCHEMA_VERSION, + "states": { + f"{state_path}.{name}": {"id": state_id, "parent": parent} + for name, state_id in (("SharedState", "s"), ("SharedNotes", "t")) + }, + "events": {}, + }) + ) + clear_config_cache() + try: + with harness: + yield harness + finally: + # Put the default names back for the tests that share this process. + monkeypatch.undo() + clear_config_cache() + + @pytest.fixture def tab_factory( linked_state: AppHarness, browser: Browser @@ -547,3 +594,38 @@ def test_concurrent_async_vars_do_not_deadlock_linked_token( f"Async vars took {elapsed:.2f}s; expected under {max_acceptable_s:.2f}s " f"(lock_expiration={manager.lock_expiration / 1000:.2f}s)." ) + + +def test_linked_state_propagates_with_minified_state_names( + minified_linked_state: AppHarness, browser: Browser +): + """A change one linked client makes reaches the others under minified names. + + Links are keyed by the name a state has without minification, so the vars + a change dirtied must be handed to the other clients under that name too. + + Args: + minified_linked_state: harness for LinkedStateApp with minified names. + browser: Playwright browser. + """ + assert minified_linked_state.app_instance is not None + assert minified_linked_state.app_module is not None + shared_state = minified_linked_state.app_module.SharedState + assert shared_state.get_name() == "s" + assert shared_state.get_full_name() != shared_state._get_default_full_name() + context = browser.new_context() + try: + tabs = [ + _linked_page(minified_linked_state, context.new_page, "") for _ in range(2) + ] + shared_token = f"shared-min-{uuid.uuid4()}" + for tab in tabs: + _submit(tab, "#token-input", shared_token) + expect(tab.locator("#linked-to")).to_have_text(shared_token) + + counter_1, counter_2 = (tab.locator("#counter-button") for tab in tabs) + counter_2.click() + expect(counter_2).to_have_text("1") + expect(counter_1).to_have_text("1") + finally: + context.close() diff --git a/tests/units/compiler/state_js.test.mjs b/tests/units/compiler/state_js.test.mjs index b5eba0f1dee..24bf07fba97 100644 --- a/tests/units/compiler/state_js.test.mjs +++ b/tests/units/compiler/state_js.test.mjs @@ -25,6 +25,7 @@ async function setup({ initialState = { root: {}, child: {} }, dropConnects = 0, dropReason = "transport close", + replayOnConnect = [], maxPayload = 1_000_000, eventUrl = "ws://localhost:8000/_event", firstPayload = {}, @@ -155,7 +156,15 @@ async function setup({ } this.finishConnectAck = () => { this.connected = true; + // Like Socket.onconnect: events received before the CONNECT ack are + // replayed first, and "connect" fires even if one of them disconnected. + for (const [name, ...args] of replayOnConnect) { + handlers.get(name)(...args); + } const connected = handlers.get("connect")(); + if (!this.connected) { + return connected; + } handlers.get("new_token")("assigned-token"); handlers.get("event")({ delta: { child: { count: 7 } } }); return connected; @@ -169,7 +178,11 @@ async function setup({ }, disconnect() { this.disconnects++; - this.connected = false; + // Like socket.io-client, only an open namespace reports the close. + if (this.connected) { + this.connected = false; + handlers.get("disconnect")("io client disconnect"); + } }, }; sockets.push(socket); @@ -209,10 +222,13 @@ async function setup({ initialState: stateful ? initialState : {}, onLoadInternalEvent() {}, on_load_internal_event: onLoadInternalEvent, - state_name: stateName, + main_state_name: stateName, + hydrate_and_load: "root.hydrate_and_load", + update_vars_internal: + "root.reflex___state____update_vars_internal_state.update_vars_internal", + handle_frontend_exception: "exception.handle_frontend_exception", stateSchema: { version: "test" }, clientStorage, - exception_state_name: "exception", }, eventLoop: { addEvents() {}, connectErrors: [] }, }, @@ -662,6 +678,19 @@ test("a dropped connection reconnects at once", async () => { assert.equal(app.timers.size, 0); }); +test("a fatal mismatch replayed before connect is not retried and stays visible", async () => { + const app = await setup({ + replayOnConnect: [["scheme_mismatch", { frontend: "a", backend: "b" }]], + }); + app.flush(); + await app.connect(); + assert.equal(app.socket.current.disconnects, 1); + assert.equal(app.timers.size, 0); + const errors = app.connectErrors; + assert.equal(errors.length, 1); + assert.equal(errors[0].fatal, true); +}); + for (const reason of ["error", "timeout", "pagehide", "hot reload"]) { test(`discard an unclaimed warm transport on ${reason}`, async () => { const app = await setup(); diff --git a/tests/units/compiler/test_compiler.py b/tests/units/compiler/test_compiler.py index 0f42e0ea870..6d853c83de5 100644 --- a/tests/units/compiler/test_compiler.py +++ b/tests/units/compiler/test_compiler.py @@ -831,10 +831,8 @@ class SchemaContextChild(SchemaContextRoot): schema = SchemaContextRoot._to_frontend_schema() assert f'export const stateSchema = "{schema}"' in code - on_load_internal_event = ( - f"{SchemaContextRoot.get_full_name()}.{constants.CompileVars.ON_LOAD_INTERNAL}" - ) - assert f"export const on_load_internal_event = '{on_load_internal_event}'" in code + on_load_internal_event = compiler._internal_names().on_load_internal + assert f'export const on_load_internal_event = "{on_load_internal_event}"' in code start = code.index("registerApp({") assert "stateSchema," in code[start : code.index("});", start)] assert "on_load_internal_event," in code[start : code.index("});", start)] @@ -2151,6 +2149,62 @@ def test_context_template_owner_stack_pin(disable_owner_stacks: bool): assert "captureOwnerStack" in rendered +def test_context_template_renders_internal_names(): + """The generated module carries the resolved framework names.""" + from reflex_base.compiler.templates import InternalNames, context_template + + rendered = context_template( + is_dev_mode=True, + default_color_mode='"light"', + initial_state={"reflex___state____state": {}}, + internal_names=InternalNames( + main_state_name="reflex___state____state", + is_hydrated_key="h", + hydrate_and_load="reflex___state____state.g", + on_load_internal="reflex___state____state.a.b", + update_vars_internal="reflex___state____state.c.d", + handle_frontend_exception="reflex___state____state.e.f", + ), + ) + assert "ReflexEvent('reflex___state____state.g', payload)" in rendered + assert "ReflexEvent('reflex___state____state.a.b')" in rendered + assert "'reflex___state____state.c.d'" in rendered + assert 'handle_frontend_exception = "reflex___state____state.e.f"' in rendered + assert 'is_hydrated_key = "h"' in rendered + assert 'hydrate_and_load = "reflex___state____state.g"' in rendered + assert 'on_load_internal_event = "reflex___state____state.a.b"' in rendered + # The static runtime reads the names through the registry. + registered = rendered[rendered.index("registerApp({") :] + for name in ( + "main_state_name", + "is_hydrated_key", + "on_load_internal_event", + "hydrate_and_load", + "update_vars_internal", + "handle_frontend_exception", + ): + assert f" {name},\n" in registered + # One name for the framework root; a second export would drift from it. + assert "export const state_name" not in rendered + assert " state_name,\n" not in registered + + +def test_context_template_carries_the_scheme_digest(): + """The bundle advertises the wire-name scheme it was built against.""" + from reflex_base.compiler.templates import context_template + + rendered = context_template( + is_dev_mode=True, + default_color_mode='"light"', + scheme_digest="abc123", + ) + + assert 'export const schemeDigest = "abc123"' in rendered + # The static runtime reads it through the registry to send it on connect. + registered = rendered[rendered.index("registerApp({") :] + assert " schemeDigest,\n" in registered + + def test_context_template_client_side_component_is_named(): """``ClientSide`` returns a named component, not an anonymous arrow.""" from reflex_base.compiler.templates import context_template diff --git a/tests/units/compiler/test_compiler_utils.py b/tests/units/compiler/test_compiler_utils.py index 5e2b83b4d17..7771c7545df 100644 --- a/tests/units/compiler/test_compiler_utils.py +++ b/tests/units/compiler/test_compiler_utils.py @@ -808,3 +808,63 @@ def test_document_root_controls_preserve_no_id_and_page_refs(): assert not document_root._get_all_hooks() assert page_script._get_all_hooks() + + +def test_client_storage_keys_follow_the_wire_but_storage_names_do_not( + temp_minify_json, monkeypatch: pytest.MonkeyPatch +) -> None: + """Browser storage keeps the name a var has without minification. + + The key must match the var's delta key, which minification rewrites, but + the name a value is stored under in the browser must not move, or enabling + minification or editing minify.json would orphan every stored value. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + import reflex as rx + from reflex.minify import StateEntry, get_state_full_path + from reflex.state import BaseState + from tests.units.minify_helpers import install_config, set_minify_modes + + class StorageRoot(BaseState): + token: str = rx.Cookie("") + theme: str = rx.Cookie("light", name="theme") + note: str = rx.LocalStorage("") + draft: str = rx.SessionStorage("") + + default_prefix = StorageRoot._get_default_full_name() + unminified = utils.compile_client_storage(StorageRoot) + assert unminified["cookies"] == { + f"{default_prefix}.token{FIELD_MARKER}": {"path": "/", "sameSite": "lax"}, + f"{default_prefix}.theme{FIELD_MARKER}": { + "name": "theme", + "path": "/", + "sameSite": "lax", + }, + } + + path = get_state_full_path(StorageRoot) + set_minify_modes(monkeypatch, states=True, vars=True) + install_config( + states={path: StateEntry(id="s", parent=None)}, + vars={path: {"token": "t", "theme": "h", "note": "n"}}, + ) + minified = utils.compile_client_storage(StorageRoot) + assert minified["cookies"] == { + "s.t": { + "path": "/", + "sameSite": "lax", + "name": f"{default_prefix}.token{FIELD_MARKER}", + }, + # An explicit name is the user's to keep. + "s.h": {"name": "theme", "path": "/", "sameSite": "lax"}, + } + assert minified["local_storage"] == { + "s.n": {"sync": False, "name": f"{default_prefix}.note{FIELD_MARKER}"} + } + # A var without an id keeps its key but is still under a renamed state. + assert minified["session_storage"] == { + f"s.draft{FIELD_MARKER}": {"name": f"{default_prefix}.draft{FIELD_MARKER}"} + } diff --git a/tests/units/compiler/test_context_js_template.py b/tests/units/compiler/test_context_js_template.py index 2e1b6b82468..14a89a830b9 100644 --- a/tests/units/compiler/test_context_js_template.py +++ b/tests/units/compiler/test_context_js_template.py @@ -7,7 +7,7 @@ from pathlib import Path import pytest -from reflex_base.compiler.templates import context_template +from reflex_base.compiler.templates import InternalNames, context_template from reflex_base.constants import Templates requires_node = pytest.mark.skipif(shutil.which("node") is None, reason="node missing") @@ -104,7 +104,6 @@ def _event_loop_provider_body() -> str: is_dev_mode=False, default_color_mode="light", initial_state={"state": {}}, - state_name="state", ) start = rendered.index("export function EventLoopProvider") end = rendered.index("function createStateStore", start) @@ -157,7 +156,6 @@ def test_server_state_provider_depth_is_constant_for_many_substates(tmp_path: Pa is_dev_mode=True, default_color_mode='"light"', initial_state={name: {"n": i} for i, name in enumerate(names)}, - state_name=names[0], ) for specifier, source in _STUB_MODULES.items(): stub = tmp_path / (specifier.replace("/", "_").replace("$", "") + ".mjs") @@ -198,7 +196,14 @@ def test_client_state_store_notifies_only_substate_subscribers(tmp_path: Path): "reflex___state____state__sub": {"value": 2}, "reflex___state____state__list": {"items": ["a"]}, }, - state_name="reflex___state____state", + internal_names=InternalNames( + main_state_name="reflex___state____state", + is_hydrated_key="is_hydrated_rx_state_", + hydrate_and_load="reflex___state____state.hydrate_and_load", + on_load_internal="reflex___state____state.on_load.on_load_internal", + update_vars_internal="reflex___state____state.update.update_vars_internal", + handle_frontend_exception="reflex___state____state.exc.handle_frontend_exception", + ), ).replace("function ClientStateProvider", "export function ClientStateProvider") react_stub = """ let hookIndex = 0; diff --git a/tests/units/compiler/test_state_js_template.py b/tests/units/compiler/test_state_js_template.py index 7a27a17b030..22d771a990c 100644 --- a/tests/units/compiler/test_state_js_template.py +++ b/tests/units/compiler/test_state_js_template.py @@ -92,6 +92,73 @@ def test_state_js_still_handles_page_lifecycle_disconnect() -> None: ) +def test_state_js_avoids_unpolyfilled_runtime_apis() -> None: + """The template must not call `Array.prototype.at`. + + `Array.prototype.at` is ES2022 and, being a runtime method rather than + syntax, is not downlevelled by the bundler's build target, so it breaks + older browsers outright. Use length-guarded indexed access instead. + """ + content = STATE_JS_TEMPLATE.read_text() + + assert ".at(" not in content, ( + "state.js calls Array.prototype.at; use `arr[arr.length - 1]` instead." + ) + + +def test_state_js_reads_the_hydrated_flag_by_its_wire_key() -> None: + """The hydrated flag's key comes from the compiled context, not a literal. + + Minification renames the var, so a hardcoded key would never match a delta. + """ + content = STATE_JS_TEMPLATE.read_text() + + assert "_rx_state_" not in content, "state.js hardcodes a state var key." + assert content.count("app.is_hydrated_key") == 3 + + +def test_state_js_disconnects_on_fatal_mismatch() -> None: + """A fatal frontend/backend mismatch must close the socket. + + No event can be exchanged after a mismatch, so a socket left open only + pins a server connection to a dead tab. + """ + content = STATE_JS_TEMPLATE.read_text() + + fatal_mismatch = content.split("const fatalMismatch = ", 1)[-1].split("\n };", 1)[ + 0 + ] + assert "socket.current?.disconnect();" in fatal_mismatch, ( + "fatalMismatch should disconnect the socket after surfacing the error." + ) + + +def test_state_js_reconnect_helper_respects_fatal_mismatch() -> None: + """Every reconnect path must funnel through the mismatch-aware helper. + + `socket.current.reconnect()` short-circuits on `backend_scheme_mismatch`, so + disconnecting on a fatal mismatch cannot start a reconnect loop. + """ + content = STATE_JS_TEMPLATE.read_text() + + reconnect_helper = content.split("socket.current.reconnect = () => {", 1)[-1].split( + "\n };", 1 + )[0] + assert "if (backend_scheme_mismatch)" in reconnect_helper, ( + "the reconnect helper must not reconnect after a fatal mismatch." + ) + # Counting every spelling, `socket.current?.connect()` included, so a + # direct call cannot slip past the guard by writing itself differently. + # The only other call is the first connect of a new socket, made at the end + # of `connect` once its handlers are installed. + first_connect = "\n socket.current.connect();\n};" + assert reconnect_helper.count(".connect(") == 1, ( + "socket reconnection should only happen inside the reconnect helper." + ) + assert content.count(".connect(") == 2 + assert first_connect in content + + def test_every_user_code_eval_can_reach_add_events() -> None: """Every scope that runs user code must declare the `addEvents` alias. diff --git a/tests/units/components/base/test_bare.py b/tests/units/components/base/test_bare.py index 30f3d9b233c..913d8db0e62 100644 --- a/tests/units/components/base/test_bare.py +++ b/tests/units/components/base/test_bare.py @@ -2,6 +2,10 @@ from reflex_base.vars.base import Var from reflex_components_core.base.bare import Bare +from reflex.minify import StateEntry, get_state_full_path +from reflex.state import State +from tests.units.minify_helpers import install_config, set_minify_modes + STATE_VAR = Var(_js_expr="default_state.name") @@ -23,3 +27,42 @@ def test_fstrings(contents, expected): """ comp = Bare.create(contents).render() assert comp["contents"] == expected + + +@pytest.mark.parametrize( + ("minify_states", "minify_vars"), + [(True, False), (True, True), (False, True)], + ids=["states", "states-and-vars", "vars"], +) +def test_stringified_var_flagged_when_minified( + temp_minify_json, monkeypatch, mocker, minify_states, minify_vars +): + """The perf-mode check flags state Vars whatever minification renames.""" + from reflex_base.environment import PerformanceMode + + import reflex as rx + + set_minify_modes(monkeypatch, states=minify_states, vars=minify_vars) + + class StrVarProbe(State): + field: int = 1 + + path = get_state_full_path(StrVarProbe) + install_config( + states={path: StateEntry(id="b", parent="reflex.state.State")}, + vars={path: {"field": "c"}}, + include_state_root=True, + ) + stringified = str(StrVarProbe.field) + assert (State.get_name() == "a") is minify_states + assert stringified.endswith(".c") is minify_vars + + mocker.patch( + "reflex_components_core.base.bare.get_performance_mode", + return_value=PerformanceMode.RAISE, + ) + + with pytest.raises(ValueError, match="displayed as a string"): + rx.vstack(stringified) + # The issued state local followed by a key it never handed out. + rx.vstack(f"{stringified.split('.')[0]}.not_a_key") diff --git a/tests/units/components/core/test_banner.py b/tests/units/components/core/test_banner.py index 8ea73a93775..9cdff588250 100644 --- a/tests/units/components/core/test_banner.py +++ b/tests/units/components/core/test_banner.py @@ -4,6 +4,10 @@ ConnectionPulser, ConnectionToaster, WebsocketTargetURL, + connection_error, + has_connection_errors, + has_fatal_connection_error, + has_too_many_connection_errors, ) from reflex_components_radix.themes.typography.text import Text @@ -54,12 +58,41 @@ def test_connection_pulser(): _imports = pulser._get_all_imports(collapse=True) +def test_connection_error_vars_avoid_unpolyfilled_runtime_apis(): + """The generated JS must not depend on `Array.prototype.at` (ES2022). + + It is a runtime method, so the bundler's build target does not downlevel + it and older browsers throw while rendering the connection UI. + """ + for var in ( + connection_error, + has_connection_errors, + has_fatal_connection_error, + has_too_many_connection_errors, + ): + assert ".at(" not in str(var) + + +def test_has_fatal_connection_error_is_length_guarded(): + assert str(has_fatal_connection_error) == ( + "((connectErrors.length > 0)" + " && connectErrors[connectErrors.length - 1].fatal === true)" + ) + + +def test_has_too_many_connection_errors_composes_the_fatal_check(): + assert str(has_too_many_connection_errors) == ( + "(connectErrors.length >= 2 || ((connectErrors.length > 0)" + " && connectErrors[connectErrors.length - 1].fatal === true))" + ) + + def test_connection_toaster_shows_hydration_errors_at_once(): hooks = "\n".join(map(str, ConnectionToaster.create()._get_all_hooks())) hydration_check = hooks.index("error.code === 'hydration_payload_too_large'") # Checked before the two-error threshold, with an id no connection toast # dismisses, and kept open until the user closes it. - assert hydration_check < hooks.index("(connectErrors.length >= 2)") + assert hydration_check < hooks.index(str(has_too_many_connection_errors)) assert 'id: "hydration-payload-error"' in hooks assert "duration: Infinity" in hooks diff --git a/tests/units/conftest.py b/tests/units/conftest.py index cfc99db8f27..76e50b00c1a 100644 --- a/tests/units/conftest.py +++ b/tests/units/conftest.py @@ -573,6 +573,41 @@ def clean_registration_context() -> Generator[RegistrationContext, None, None]: yield ctx +@pytest.fixture +def temp_minify_json( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> Generator[Path, None, None]: + """Run the test against a fresh ``minify.json`` location. + + Runs inside a forked registration context so State subclasses defined in + the test never reach the shared substate tree. Teardown undoes monkeypatch + *before* clearing the cache so the registry is rebuilt under unmodified + env/cwd — otherwise other tests would inherit minified names. + + Args: + tmp_path: The pytest temporary directory. + monkeypatch: The pytest monkeypatch fixture. + + Yields: + The temporary directory path. + """ + from reflex.minify import clear_config_cache + from reflex.state import all_base_state_classes + + monkeypatch.chdir(tmp_path) + # Forking isolates the registry but not this module-level map. + registered_states = dict(all_base_state_classes) + try: + with RegistrationContext.get().fork(): + clear_config_cache() + yield tmp_path + finally: + monkeypatch.undo() + clear_config_cache() + all_base_state_classes.clear() + all_base_state_classes.update(registered_states) + + @pytest.fixture def preserve_memo_registries(): """Save and restore the global memo registry around a test. diff --git a/tests/units/istate/manager/test_token.py b/tests/units/istate/manager/test_token.py index edbb5a3478a..4deb2f56561 100644 --- a/tests/units/istate/manager/test_token.py +++ b/tests/units/istate/manager/test_token.py @@ -80,6 +80,25 @@ class TokState(BaseState): assert TokState.get_full_name() in result +def test_base_state_token_str_survives_renaming(clean_registration_context): + """A resolver renaming the state does not move where its session is stored. + + Args: + clean_registration_context: A fresh, empty registration context. + """ + from reflex.state import BaseState + from tests.units.name_resolvers import stub_resolver, temporary_resolver + + class RenamedTokState(BaseState): + pass + + token = BaseStateToken(ident="client-abc", cls=RenamedTokState) + stored_under = str(token) + with temporary_resolver(stub_resolver(state_name="z", target=RenamedTokState)): + assert RenamedTokState.get_full_name() == "z" + assert str(token) == stored_under + + def test_base_state_token_with_cls(clean_registration_context): """with_cls returns a BaseStateToken (not a plain StateToken). diff --git a/tests/units/istate/test_shared.py b/tests/units/istate/test_shared.py index e7cb2e73010..4bb8d8242d2 100644 --- a/tests/units/istate/test_shared.py +++ b/tests/units/istate/test_shared.py @@ -173,3 +173,56 @@ async def test_patch_state_recomputes_readers_after_restoring(): async with _patch_state(original_state=original, linked_state=linked): assert await reader.greeting == "linked" # pyright: ignore[reportAttributeAccessIssue] assert await reader.greeting == "private" # pyright: ignore[reportAttributeAccessIssue] + + +async def test_links_survive_renaming_the_linked_state( + temp_minify_json, monkeypatch: pytest.MonkeyPatch +): + """A link recorded before a state is renamed still resolves afterwards. + + Links are stored with the root state, which outlives a deploy that turns + minification on or edits minify.json. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + from reflex.istate.shared import SharedStateBaseInternal + from reflex.minify import StateEntry, get_state_full_path + from tests.units.minify_helpers import install_config, set_minify_modes + + # Defining a shared state changes the root state for good; undo it after. + links = State.get_fields()["_reflex_internal_links"] + monkeypatch.setattr(links, "default", links.default) + monkeypatch.setattr(links, "default_factory", links.default_factory) + monkeypatch.setattr( + State, "_always_dirty_substates", set(State._always_dirty_substates) + ) + + class RenamedLinkState(rx.SharedState): + count: int = 0 + + async def _patched(self, token, full_delta=False): # noqa: RUF029 + return self + + monkeypatch.setattr(RenamedLinkState, "_internal_patch_linked_state", _patched) + resolved = AsyncMock(return_value=None) + monkeypatch.setattr(SharedStateBaseInternal, "_resolve_linked_state", resolved) + + root = State(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] + linked = root.get_substate(RenamedLinkState.get_full_name().split(".")) + assert isinstance(linked, RenamedLinkState) + await linked._link_to("shared-token") + + set_minify_modes(monkeypatch, states=True) + install_config( + states={ + get_state_full_path(RenamedLinkState): StateEntry( + id="z", parent="reflex.state.State" + ) + } + ) + assert RenamedLinkState.get_name() == "z" + + await root._get_state_from_redis(RenamedLinkState) + resolved.assert_awaited_once_with(RenamedLinkState, "shared-token") diff --git a/tests/units/minify_helpers.py b/tests/units/minify_helpers.py new file mode 100644 index 00000000000..85628945985 --- /dev/null +++ b/tests/units/minify_helpers.py @@ -0,0 +1,194 @@ +"""Helpers for installing a ``minify.json`` in unit tests.""" + +from __future__ import annotations + +import json +import os +import subprocess +import sys +import textwrap +from collections.abc import Mapping +from pathlib import Path +from typing import Any + +import pytest +from reflex_base.registry import RegistrationContext + +from reflex.environment import environment +from reflex.minify import ( + MINIFY_JSON, + SCHEMA_VERSION, + MinifyConfig, + MinifyNameResolver, + StateEntry, + clear_config_cache, + save_minify_config, +) +from reflex.state import BaseState + + +def resolved_event_id(state_cls: type[BaseState], handler_name: str) -> str | None: + """Resolve a handler's minified id through the active resolver. + + Args: + state_cls: The state owning the handler. + handler_name: The handler's Python name. + + Returns: + The minified id, or ``None`` when the handler is not minified. + """ + return RegistrationContext.get().name_resolver.resolve_handler_name( + state_cls, handler_name + ) + + +def set_minify_modes( + monkeypatch: pytest.MonkeyPatch, + *, + states: bool | None = None, + events: bool | None = None, + vars: bool | None = None, +) -> None: + """Set ``REFLEX_MINIFY_*`` env vars; ``None`` leaves the var unchanged. + + The resolver reads the modes when installed, so they only take effect + through a following ``install_config()`` or ``clear_config_cache()``. + + Args: + monkeypatch: The pytest monkeypatch fixture. + states: Whether ``REFLEX_MINIFY_STATES`` is on. + events: Whether ``REFLEX_MINIFY_EVENTS`` is on. + vars: Whether ``REFLEX_MINIFY_VARS`` is on. + """ + for env_var, enabled in ( + (environment.REFLEX_MINIFY_STATES, states), + (environment.REFLEX_MINIFY_EVENTS, events), + (environment.REFLEX_MINIFY_VARS, vars), + ): + if enabled is not None: + monkeypatch.setenv(env_var.name, str(int(enabled))) + + +def make_config( + states: Mapping[str, str | StateEntry] | None = None, + events: dict[str, dict[str, str]] | None = None, + vars: dict[str, dict[str, str]] | None = None, + *, + include_state_root: bool = False, +) -> MinifyConfig: + """Build a ``minify.json`` config. + + Args: + states: ``state_path -> minified_id`` map. Plain string values are + wrapped into :class:`StateEntry` with ``parent=None``. + events: ``state_path -> {handler -> minified_id}`` map. + vars: ``state_path -> {var -> minified_id}`` map. + include_state_root: Add ``"reflex.state.State": "a"`` so subclasses + of ``State`` resolve through the root entry. + + Returns: + The config. + """ + states_map: dict[str, StateEntry] = { + path: value if isinstance(value, dict) else StateEntry(id=value, parent=None) + for path, value in (states or {}).items() + } + if include_state_root: + states_map.setdefault("reflex.state.State", StateEntry(id="a", parent=None)) + return MinifyConfig( + version=SCHEMA_VERSION, + states=states_map, + events=events or {}, + vars=vars or {}, + ) + + +def write_config(directory: Path, config: Mapping[str, Any]) -> None: + """Write ``config`` as the ``minify.json`` of ``directory``, as it is. + + Args: + directory: The directory to write it to, created if missing. + config: The contents, which may be malformed on purpose. + """ + directory.mkdir(parents=True, exist_ok=True) + (directory / MINIFY_JSON).write_text(json.dumps(config)) + + +def install_config( + states: Mapping[str, str | StateEntry] | None = None, + events: dict[str, dict[str, str]] | None = None, + vars: dict[str, dict[str, str]] | None = None, + *, + include_state_root: bool = False, +) -> MinifyConfig: + """Build, save, and activate a ``minify.json`` in one call. + + Calls ``clear_config_cache()`` afterward — that re-installs the resolver + and rebuilds whatever names it changes, so tests don't need to call + ``State.get_name.cache_clear()`` etc. by hand. + + Args: + states: See :func:`make_config`. + events: See :func:`make_config`. + vars: See :func:`make_config`. + include_state_root: See :func:`make_config`. + + Returns: + The saved config. + """ + config = make_config(states, events, vars, include_state_root=include_state_root) + save_minify_config(config) + clear_config_cache() + return config + + +def minify_resolver( + config: MinifyConfig | None = None, + *, + states: bool = False, + events: bool = False, + vars: bool = False, +) -> MinifyNameResolver: + """Build a resolver over ``config`` with the given modes on. + + Args: + config: The config, if any. + states: Whether state names are minified. + events: Whether event names are minified. + vars: Whether var names are minified. + + Returns: + The resolver. + """ + return MinifyNameResolver( + config=config, states_enabled=states, events_enabled=events, vars_enabled=vars + ) + + +def run_in_fresh_interpreter( + tmp_path: Path, config: MinifyConfig, script: str, **env: str +) -> None: + """Run ``script`` in a new interpreter rooted at a directory holding ``config``. + + The framework states bake their names when ``reflex.state`` is first + imported, so anything that depends on their minified names needs an + interpreter that starts up with ``minify.json`` already in place. + + Args: + tmp_path: Directory to use as the app root. + config: The ``minify.json`` contents to write there. + script: Python source to execute; a non-zero exit fails the test. + env: Extra environment variables for the child process. + """ + write_config(tmp_path, config) + (tmp_path / "check.py").write_text(textwrap.dedent(script)) + + result = subprocess.run( + [sys.executable, "-c", "import check"], + cwd=tmp_path, + env={**os.environ, **env}, + capture_output=True, + text=True, + check=False, + ) + assert result.returncode == 0, result.stderr diff --git a/tests/units/name_resolvers.py b/tests/units/name_resolvers.py new file mode 100644 index 00000000000..5d5754413a3 --- /dev/null +++ b/tests/units/name_resolvers.py @@ -0,0 +1,69 @@ +"""Stub ``NameResolver``s for unit testing.""" + +from __future__ import annotations + +import contextlib +from collections.abc import Iterator + +from reflex_base.registry import NameResolver, RegistrationContext + +from reflex.state import BaseState, State + + +def stub_resolver( + *, + state_name: str | None = None, + target: type[BaseState] = State, + handler_prefix: str | None = None, + var_prefix: str | None = None, + digest: str = "", +) -> NameResolver: + """Build a tiny one-off :class:`NameResolver`. + + Args: + state_name: Override returned for ``target`` (else ``None``). + target: Which state class the override scopes to. + handler_prefix: When set, prefixes every handler name. + var_prefix: When set, prefixes every var name. + digest: The scheme digest this resolver reports. + + Returns: + A resolver with the requested behavior. + """ + + class _Stub: + def resolve_state_name(self, state_cls): + return state_name if state_cls is target else None + + def resolve_handler_name(self, state_cls, handler_name): + return f"{handler_prefix}{handler_name}" if handler_prefix else None + + def resolve_var_name(self, state_cls, var_name): + return f"{var_prefix}{var_name}" if var_prefix else None + + def digest(self): + return digest + + return _Stub() + + +@contextlib.contextmanager +def temporary_resolver(resolver: NameResolver) -> Iterator[RegistrationContext]: + """Install ``resolver`` for the duration of the ``with`` block. + + Restoring matters: ``set_name_resolver`` clears the per-class name caches, + so a resolver left installed leaks its names into later tests. + + Args: + resolver: The resolver to install temporarily. + + Yields: + The active registration context. + """ + ctx = RegistrationContext.get() + original = ctx.name_resolver + try: + ctx.set_name_resolver(resolver) + yield ctx + finally: + ctx.set_name_resolver(original) diff --git a/tests/units/reflex_base/client_event_queue_runtime.mjs b/tests/units/reflex_base/client_event_queue_runtime.mjs index f13cb974094..57e019e042b 100644 --- a/tests/units/reflex_base/client_event_queue_runtime.mjs +++ b/tests/units/reflex_base/client_event_queue_runtime.mjs @@ -12,8 +12,7 @@ export async function createQueueRuntime( initialEvents: () => [], initialState: {}, onLoadInternalEvent: unused, - state_name: "test_state", - exception_state_name: "test_exception_state", + main_state_name: "reflex___state", }; const dependencies = { "test:browser": { diff --git a/tests/units/reflex_base/client_upload.mjs b/tests/units/reflex_base/client_upload.mjs index 93dec17b810..da2d04815a8 100644 --- a/tests/units/reflex_base/client_upload.mjs +++ b/tests/units/reflex_base/client_upload.mjs @@ -51,6 +51,7 @@ globalThis.XMLHttpRequest = StreamedRequest; const dependencies = { "$/env.json": { default: { UPLOAD: "http://backend/_upload" } }, + "$/utils/context-registry": { app: { schemeDigest: "" } }, "$/utils/helpers/json": { parseJson: JSON.parse }, }; const module = new SourceTextModule(source); diff --git a/tests/units/reflex_base/event/processor/test_base_state_processor.py b/tests/units/reflex_base/event/processor/test_base_state_processor.py index 0f6735439b6..1d86218dcf9 100644 --- a/tests/units/reflex_base/event/processor/test_base_state_processor.py +++ b/tests/units/reflex_base/event/processor/test_base_state_processor.py @@ -32,6 +32,8 @@ from reflex_base.event.context import EventContext from reflex_base.event.processor import BaseStateEventProcessor from reflex_base.event.processor.base_state_processor import ( + _connect_supersedes, + _hydrate_event_names, _prepare_event_payload, _transform_event_arg, _transform_event_payload, @@ -63,6 +65,7 @@ from reflex.utils import types as reflex_types from tests.units.conftest import metric_points from tests.units.mock_redis import mock_redis +from tests.units.name_resolvers import stub_resolver, temporary_resolver # Class-level registries on State that registering a substate, or installing a # dynamic route arg, writes into with no removal path. @@ -491,7 +494,7 @@ async def fg(self): # The uncached computed var registered BgRaceState as an always-dirty # substate on the shared State class; later tests' state trees don't # contain it and would KeyError in get_delta (see reload_state_module). - State._always_dirty_substates.discard(BgRaceState.get_name()) + State._always_dirty_substates.discard(BgRaceState) state_name = BgRaceState.get_full_name() victim_key = "victim" + FIELD_MARKER @@ -799,7 +802,7 @@ async def bg(self): ) await processor.join(5) finally: - State._always_dirty_substates.discard(NoContextBgState.get_name()) + State._always_dirty_substates.discard(NoContextBgState) state_name = NoContextBgState.get_full_name() beat_key = "beat" + FIELD_MARKER @@ -957,7 +960,7 @@ async def bg_raises(self): ) await processor.join(5) finally: - State._always_dirty_substates.discard(RaisingBgState.get_name()) + State._always_dirty_substates.discard(RaisingBgState) state_name = RaisingBgState.get_full_name() beat_key = "beat" + FIELD_MARKER @@ -1026,7 +1029,7 @@ async def bg_raises(self): ): await processor.join(5) finally: - State._always_dirty_substates.discard(MaskingBgState.get_name()) + State._always_dirty_substates.discard(MaskingBgState) assert [type(ex) for ex in handled] == [RuntimeError], ( f"the flush failure masked the handler's exception: {handled}" @@ -3188,6 +3191,17 @@ async def slow_load(self): assert (await root.get_state(State)).is_hydrated is True +def test_connect_event_names_follow_a_resolver_installed_later(): + """The (re)connect events are recognized under the names the frontend sends.""" + default = (_connect_supersedes(), _hydrate_event_names()) + with temporary_resolver(stub_resolver(handler_prefix="h_")): + boot, on_load = _connect_supersedes() + assert boot.endswith(".h_hydrate_and_load") + assert on_load.endswith(".h_on_load_internal") + assert _hydrate_event_names() == (f"{State.get_name()}.h_hydrate", boot) + assert (_connect_supersedes(), _hydrate_event_names()) == default + + @pytest.mark.parametrize( "processor_state_manager", ["in_process", "redis"], indirect=True ) diff --git a/tests/units/reflex_base/event/processor/test_event_processor.py b/tests/units/reflex_base/event/processor/test_event_processor.py index 7302bbeb6d2..9c44bb9220f 100644 --- a/tests/units/reflex_base/event/processor/test_event_processor.py +++ b/tests/units/reflex_base/event/processor/test_event_processor.py @@ -2070,6 +2070,84 @@ async def test_event_spans_chain_parent_child(token: str, otel_exporter): assert child.attributes[otel.ATTR_SESSION_ID] == otel._session_id(token) +async def test_unregistered_event_reaches_the_exception_handler(token: str): + """An event with no handler is reported, not just logged. + + Such an event produces no update, so without this the page would stall + with nothing but a server-side traceback to explain it. + + Args: + token: The client token. + """ + seen: list[Exception] = [] + processor = EventProcessor( + backend_exception_handler=seen.append, graceful_shutdown_timeout=2 + ) + processor.configure() + + async with processor as ep: + await ep.enqueue(token, Event(name="no.such.handler")) + + assert len(seen) == 1 + assert isinstance(seen[0], KeyError) + assert "no.such.handler" in str(seen[0]) + + +async def test_raising_exception_handler_does_not_stop_the_queue(token: str): + """A handler that raises must not take the remaining events down with it. + + ``_process_queue`` is the sole consumer, so an escaping exception would + leave later events queued forever. + + Args: + token: The client token. + """ + _CALL_LOG.clear() + + def explode(ex: Exception) -> None: + msg = "handler is broken" + raise RuntimeError(msg) + + processor = EventProcessor( + backend_exception_handler=explode, graceful_shutdown_timeout=2 + ) + processor.configure() + + async with processor as ep: + await ep.enqueue(token, Event(name="no.such.handler")) + current = await ep.enqueue( + token, Event.from_event_type(logging_event("after_failure"))[0] + ) + await asyncio.wait_for(current.wait_all(), timeout=2) + + assert _CALL_LOG == [{"value": "after_failure"}], ( + "the event queued after the handler failure never ran" + ) + + +async def test_cancelled_spawned_exception_handler_task_is_untracked(token: str): + """An exception handler task cancelled before it starts leaves no ``_tasks`` entry. + + Args: + token: The client token. + """ + seen: list[Exception] = [] + processor = EventProcessor( + backend_exception_handler=seen.append, graceful_shutdown_timeout=2 + ) + processor.configure() + async with processor as ep: + assert ep._root_context is not None + ev_ctx = ep._root_context.fork(token=token) + ep._spawn_backend_exception_handler(RuntimeError("boom"), None, ev_ctx, "test") + task = ep._tasks[ev_ctx.txid] + task.cancel() + with contextlib.suppress(asyncio.CancelledError): + await task + assert ev_ctx.txid not in ep._tasks + assert seen == [] + + async def test_top_level_event_under_local_span_names_no_parent_event( token: str, otel_exporter ): diff --git a/tests/units/reflex_base/test_registry.py b/tests/units/reflex_base/test_registry.py index 82490effd6c..9a53b6cb1ed 100644 --- a/tests/units/reflex_base/test_registry.py +++ b/tests/units/reflex_base/test_registry.py @@ -6,10 +6,20 @@ import pytest from reflex_base.config import Config, get_config, reload_config -from reflex_base.registry import RegisteredEventHandler, RegistrationContext +from reflex_base.constants.state import FIELD_MARKER +from reflex_base.registry import ( + DefaultNameResolver, + NameResolver, + RegisteredEventHandler, + RegistrationContext, +) +from reflex_base.utils import format from reflex_base.utils.exceptions import ReflexRuntimeError, StateValueError +from reflex.state import BaseState, State from reflex.testing import chdir +from tests.units.minify_helpers import make_config, minify_resolver +from tests.units.name_resolvers import stub_resolver, temporary_resolver def test_ensure_context_creates_if_missing(): @@ -59,7 +69,6 @@ def test_register_base_state(clean_registration_context: RegistrationContext): Args: clean_registration_context: A fresh, empty registration context. """ - from reflex.state import BaseState class AutoRegistered(BaseState): x: int = 0 @@ -73,7 +82,6 @@ def test_duplicate_substate_raises(clean_registration_context: RegistrationConte Args: clean_registration_context: A fresh, empty registration context. """ - from reflex.state import BaseState class DupParent(BaseState): pass @@ -91,7 +99,6 @@ def test_get_substates(clean_registration_context: RegistrationContext): Args: clean_registration_context: A fresh, empty registration context. """ - from reflex.state import BaseState class GetSubRoot(BaseState): pass @@ -113,7 +120,6 @@ def test_get_substates_by_name(clean_registration_context: RegistrationContext): Args: clean_registration_context: A fresh, empty registration context. """ - from reflex.state import BaseState class NamedState(BaseState): pass @@ -369,7 +375,6 @@ def test_fork_clears_app_and_preserves_registrations( """ import reflex as rx from reflex.event import EventHandler - from reflex.state import BaseState class ForkState(BaseState): x: int = 0 @@ -424,6 +429,297 @@ def test_bundled_libraries_isolated_between_contexts(): assert "some-extra-lib" not in ctx_b.bundled_libraries +def test_resolver_installed_later_renames_existing_vars( + clean_registration_context: RegistrationContext, +): + """A resolver installed after a state exists rebuilds the Vars it holds.""" + import reflex as rx + + class LateRenamedState(rx.State): + value: str = "" + + @rx.var + def doubled(self) -> str: + return self.value * 2 + + default_expr = str(LateRenamedState.value) + with temporary_resolver( + stub_resolver(state_name="zzz", target=LateRenamedState, var_prefix="v_") + ): + context = format.format_state_name(LateRenamedState.get_full_name()) + assert context.endswith("__zzz") + prefix = format.format_state_local(LateRenamedState) + assert prefix == f"$rx_{context}" + assert str(LateRenamedState.value) == f"{prefix}.v_value" + for var in ( + LateRenamedState.base_vars["value"], + LateRenamedState.vars["value"], + ): + assert str(var) == f"{prefix}.v_value" + var_data = var._get_all_var_data() + assert var_data is not None + # The field subscription names the field by its renamed key. + assert var_data.hooks == ( + f'const {prefix} = useStateContext("{LateRenamedState.get_full_name()}", ["v_value"])', + ) + # Dependency tracking keys on the name that never changes. + assert var_data.state == LateRenamedState._get_default_full_name() + assert str(LateRenamedState.doubled) == f"{prefix}.v_doubled" + assert LateRenamedState._var_names_by_key["v_value"] == "value" + # Deltas and full state dicts key the vars the way the frontend reads them. + state = LateRenamedState(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] + full_name = LateRenamedState.get_full_name() + assert state.dict() == {full_name: {"v_value": "", "v_doubled": ""}} + state.value = "a" + assert state.get_delta() == {full_name: {"v_value": "a", "v_doubled": "aa"}} + assert str(LateRenamedState.value) == default_expr + state = LateRenamedState(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] + assert state.dict() == { + LateRenamedState.get_full_name(): { + f"value{FIELD_MARKER}": "", + f"doubled{FIELD_MARKER}": "", + } + } + + +def test_resolver_installed_later_renames_inherited_vars( + clean_registration_context: RegistrationContext, +): + """A substate's map of inherited vars follows its parent's rebuilt Vars.""" + import reflex as rx + + class RenamedParent(rx.State): + value: int = 0 + + class KeptChild(RenamedParent): + pass + + with temporary_resolver(stub_resolver(state_name="p", target=RenamedParent)): + assert KeptChild.vars["value"] is RenamedParent.base_vars["value"] + assert str(KeptChild.vars["value"]).endswith("__p.value_rx_state_") + + +def test_resolver_installed_later_renames_the_router_var(): + """The cached router switchboard is rebuilt with the root's new name.""" + with temporary_resolver(stub_resolver(state_name="r")): + assert str(State.router.url).startswith("$rx_r.") + assert str(State.router.url).startswith(f"{State.get_name()}.") + + +def test_default_resolver_returns_none(): + """The default resolver yields no overrides.""" + resolver = DefaultNameResolver() + assert resolver.resolve_state_name(State) is None + assert resolver.resolve_handler_name(State, "any_handler") is None + assert resolver.resolve_var_name(State, "any_var") is None + + +def test_default_resolver_satisfies_protocol(): + """``DefaultNameResolver`` is a structural :class:`NameResolver`.""" + assert isinstance(DefaultNameResolver(), NameResolver) + + +def test_minify_resolver_satisfies_protocol(): + """``MinifyNameResolver`` is a structural :class:`NameResolver`.""" + resolver = minify_resolver() + assert isinstance(resolver, NameResolver) + + +def test_get_state_name_falls_back_to_default(): + """``RegistrationContext.get_state_name`` returns the built-in name when + the resolver returns None (the default). + """ + ctx = RegistrationContext.get() + assert ctx.get_state_name(State) == RegistrationContext.default_state_name(State) + + +def test_get_handler_name_falls_back_to_default(): + """``RegistrationContext.get_handler_name`` returns the input name when + the resolver returns None (the default). + """ + ctx = RegistrationContext.get() + assert ctx.get_handler_name(State, "some_handler") == "some_handler" + + +def test_set_name_resolver_propagates_through_get_name(): + """A custom resolver swaps ``BaseState.get_name`` for the targeted class.""" + with temporary_resolver(stub_resolver(state_name="fixed_name")): + assert State.get_name() == "fixed_name" + + +def test_set_name_resolver_propagates_through_format_event_handler(): + """A custom resolver swaps the formatted handler name.""" + from reflex.state import OnLoadInternalState + from reflex.utils.format import format_event_handler + + with temporary_resolver(stub_resolver(handler_prefix="px_")): + formatted = format_event_handler(OnLoadInternalState.on_load_internal) # pyright: ignore[reportArgumentType] + assert formatted.endswith(".px_on_load_internal") + + +def test_resolver_swap_clears_lru_caches(): + """``set_name_resolver`` invalidates the name caches immediately.""" + with temporary_resolver(stub_resolver(state_name="first")) as ctx: + assert State.get_full_name() == "first" + ctx.set_name_resolver(stub_resolver(state_name="second")) + assert State.get_full_name() == "second" + + +def test_builtin_resolver_giving_the_names_in_use_is_only_swapped_in(): + """A built-in resolver with the digest in use keeps the Vars as they are.""" + with temporary_resolver(DefaultNameResolver()) as ctx: + var = State.base_vars["is_hydrated"] + unminified = minify_resolver() + ctx.set_name_resolver(unminified) + assert ctx.name_resolver is unminified + assert State.base_vars["is_hydrated"] is var + # A custom resolver may rename under any digest, so it always rebuilds. + ctx.set_name_resolver(stub_resolver()) + assert State.base_vars["is_hydrated"] is not var + + +def test_reinstalling_a_resolver_undoes_the_names_a_fork_installed(): + """The names are cached on classes the fork shares, so they are rebuilt.""" + with temporary_resolver(DefaultNameResolver()) as ctx: + with ctx.fork() as fork: + fork.set_name_resolver( + minify_resolver(make_config(include_state_root=True), states=True) + ) + assert State.get_name() == "a" + ctx.set_name_resolver(ctx.name_resolver) + assert State.get_name() == RegistrationContext.default_state_name(State) + + +def test_chain_of_resolvers(): + """Resolvers compose with a tiny user-written chain wrapper.""" + + class Chain: + """Returns the first non-None override from the wrapped resolvers.""" + + def __init__(self, *resolvers): + self.resolvers = resolvers + + def resolve_state_name(self, state_cls): + for r in self.resolvers: + v = r.resolve_state_name(state_cls) + if v is not None: + return v + return None + + def resolve_handler_name(self, state_cls, handler_name): + for r in self.resolvers: + v = r.resolve_handler_name(state_cls, handler_name) + if v is not None: + return v + return None + + def resolve_var_name(self, state_cls, var_name): + for r in self.resolvers: + v = r.resolve_var_name(state_cls, var_name) + if v is not None: + return v + return None + + def digest(self): + return "".join(r.digest() for r in self.resolvers) + + chain = Chain(stub_resolver(state_name="from_first"), DefaultNameResolver()) + with temporary_resolver(chain): + assert State.get_name() == "from_first" + + +def test_handlers_resolving_to_one_name_raise( + clean_registration_context: RegistrationContext, +): + """A handler resolved onto another handler's name in its state is rejected. + + Registering both under one name would let an event for one run the other. + + Args: + clean_registration_context: A fresh, empty registration context. + """ + + class FooToA: + def resolve_state_name(self, state_cls): + return None + + def resolve_handler_name(self, state_cls, handler_name): + return "a" if handler_name == "foo" else None + + def resolve_var_name(self, state_cls, var_name): + return None + + def digest(self): + return "" + + with ( + temporary_resolver(FooToA()), + pytest.raises(StateValueError, match="'foo' and 'a'"), + ): + + class CollidingHandlers(BaseState): + def foo(self): + pass + + def a(self): + pass + + +def test_handler_is_registered_under_the_name_its_events_carry( + clean_registration_context: RegistrationContext, +): + """The registry key is the formatted name, even for an empty resolved name. + + Args: + clean_registration_context: A fresh, empty registration context. + """ + + class EmptyFoo: + def resolve_state_name(self, state_cls): + return None + + def resolve_handler_name(self, state_cls, handler_name): + return "" if handler_name == "foo" else None + + def resolve_var_name(self, state_cls, var_name): + return None + + def digest(self): + return "" + + with temporary_resolver(EmptyFoo()): + + class EmptyNamedHandlerState(BaseState): + def foo(self): + pass + + handler = EmptyNamedHandlerState.event_handlers["foo"] + name = format.format_event_handler(handler) + assert name == f"{EmptyNamedHandlerState.get_full_name()}." + assert clean_registration_context.event_handlers[name].handler is handler + + +def test_refresh_keys_keeps_reloaded_substates_out( + clean_registration_context: RegistrationContext, +): + """Re-keying keeps a substate that a module reload removed out of the tree. + + Args: + clean_registration_context: A fresh, empty registration context. + """ + from reflex.state import reload_state_module + + class ReloadedParent(BaseState): + pass + + class ReloadedChild(ReloadedParent): + pass + + reload_state_module(ReloadedChild._get_source_module(), ReloadedParent) + clean_registration_context.refresh_keys() + assert ReloadedChild not in clean_registration_context.get_substates(ReloadedParent) + + def test_reset_compile_caches_empties_per_compile_maps( clean_registration_context: RegistrationContext, ): diff --git a/tests/units/reflex_base/utils/test_format.py b/tests/units/reflex_base/utils/test_format.py index c97d38cb25a..a49c6e14b69 100644 --- a/tests/units/reflex_base/utils/test_format.py +++ b/tests/units/reflex_base/utils/test_format.py @@ -45,6 +45,31 @@ def test_format_queue_events_args_spec(): assert str(var).startswith("(_result) => {addEvents([") +def test_format_var_key_marks_names_the_resolver_keeps(clean_registration_context): + """An unrenamed var keeps the field marker; a renamed one is just its id. + + Args: + clean_registration_context: A fresh, empty registration context. + """ + from tests.units.name_resolvers import stub_resolver, temporary_resolver + + class FormatKeyState(rx.State): + count: int = 0 + + assert format.format_var_key(FormatKeyState, "count") == ( + "count" + constants.state.FIELD_MARKER + ) + with temporary_resolver(stub_resolver(var_prefix="v_")): + # Installing the resolver clears the cache. + assert format.format_var_key(FormatKeyState, "count") == "v_count" + assert format.format_state_var(FormatKeyState, "count") == ( + f"{format.format_state_name(FormatKeyState.get_full_name())}.v_count" + ) + assert format.format_var_key(FormatKeyState, "count") == ( + "count" + constants.state.FIELD_MARKER + ) + + def test_format_data_editor_column_deprecated(mocker: MockerFixture): """The column formatter still works, but warns that rx.data_editor owns it. diff --git a/tests/units/reflex_base/vars/test_base.py b/tests/units/reflex_base/vars/test_base.py index 2be7bd6e83d..11ee14bf827 100644 --- a/tests/units/reflex_base/vars/test_base.py +++ b/tests/units/reflex_base/vars/test_base.py @@ -89,7 +89,9 @@ import reflex as rx from reflex.istate.data import ReflexURLCastedVar, RouterDataVar from reflex.istate.proxy import MutableProxy +from reflex.minify import StateEntry, get_state_full_path from reflex.state import BaseState, State, _override_base_method +from tests.units.minify_helpers import install_config, set_minify_modes _MARKER_ATTR = "_marker" @@ -1976,6 +1978,91 @@ class Child(Parent): assert "count" not in Child.base_vars +@pytest.mark.parametrize("spelling", ["resolved", "default"]) +def test_state_named_by_string_reads_like_the_class( + temp_minify_json, monkeypatch, spelling +): + """A registered state named by either full name resolves to the class. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + spelling: Which of the state's full names the caller passes. + """ + + class NamedByStringState(State): + count: int = 0 + + set_minify_modes(monkeypatch, states=True) + install_config( + states={ + get_state_full_path(NamedByStringState): StateEntry( + id="z", parent="reflex.state.State" + ) + }, + include_state_root=True, + ) + resolved = NamedByStringState.get_full_name() + default = NamedByStringState._get_default_full_name() + assert resolved != default + name = resolved if spelling == "resolved" else default + + by_class = VarData.from_state(NamedByStringState, "count") + by_name = VarData.from_state(name, "count") + assert by_name.hooks == by_class.hooks + assert by_name.field_dependencies == by_class.field_dependencies + + var = Var(_js_expr="count") + by_class_var = var._var_set_state(NamedByStringState) + by_name_var = var._var_set_state(name) + assert str(by_name_var) == str(by_class_var) + assert by_name_var._get_all_var_data() == by_class_var._get_all_var_data() + + +def test_dependency_on_a_var_read_off_a_resolved_state_name( + temp_minify_json, monkeypatch +): + """A computed var can depend on a var whose state is named by its wire name. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class WireNamedState(State): + data: int = 0 + + class DependentState(State): + @computed_var + def total(self) -> int: + return 0 + + set_minify_modes(monkeypatch, states=True) + install_config( + states={ + get_state_full_path(WireNamedState): StateEntry( + id="z", parent="reflex.state.State" + ) + }, + include_state_root=True, + ) + dep = Var(_js_expr="data")._var_set_state(WireNamedState.get_full_name()) + DependentState.computed_vars["total"].add_dependency(DependentState, dep) + assert (DependentState, "total") in WireNamedState._var_dependencies["data"] + + +def test_unregistered_state_name_is_taken_as_given(): + """A string naming no registered state keeps meaning what it says.""" + var_data = VarData.from_state("not_a_state", "field") + assert var_data.state == "not_a_state" + assert var_data.hooks == ( + f'const not_a_state = useStateContext("not_a_state", ["field{FIELD_MARKER}"])', + ) + assert str(Var(_js_expr="field")._var_set_state("not_a_state")) == ( + "not_a_state.field" + ) + + @pytest.mark.parametrize("name", ["value", "_value"]) def test_class_assignment_over_a_var_raises(name: str): """Assigning a state var on its class or a subclass raises and keeps the var. diff --git a/tests/units/test_app.py b/tests/units/test_app.py index b348f5f4eac..26518887d7d 100644 --- a/tests/units/test_app.py +++ b/tests/units/test_app.py @@ -113,6 +113,7 @@ from .conftest import active_tracer, chdir, metric_points from .mock_redis import mock_redis +from .name_resolvers import stub_resolver, temporary_resolver from .states import GenState from .states.upload import ( ChildFileUploadState, @@ -1909,6 +1910,39 @@ async def test_upload_error_the_event_processor_did_not_report_reaches_the_serve ) +@pytest.mark.asyncio +async def test_upload_file_scheme_mismatch_returns_409( + token: str, +): + """An upload from a bundle built against another scheme is rejected. + + Uploads bypass the socket handshake, and the handler header is resolved + against this backend's scheme, so a name from another scheme can pick out + a real -- but wrong -- upload handler. + + Args: + token: a Token. + """ + app = App(_state=State) + + request_mock = unittest.mock.Mock() + request_mock.headers = { + "reflex-client-token": token, + "reflex-event-handler": f"{FileUploadState.get_full_name()}.multi_handle_upload", + "reflex-scheme": "stale-bundle-digest", + } + + with ( + temporary_resolver(stub_resolver(digest="backend-digest")), + pytest.raises(HTTPException) as err, + ): + await upload(app)(request_mock) + assert err.value.status_code == 409 + # Rejected before the body is touched. + request_mock.form.assert_not_called() + await app.state_manager.close() + + @pytest.mark.asyncio @pytest.mark.parametrize( "state", @@ -2363,7 +2397,9 @@ def _dynamic_state_event(name, val, **kwargs): prev_exp_val = "" for exp_index, exp_val in enumerate(exp_vals): on_load_internal = _event( - name=f"{OnLoadInternalState.get_full_name()}.{constants.CompileVars.ON_LOAD_INTERNAL.rpartition('.')[2]}", + name=format.format_event_handler( + OnLoadInternalState.event_handlers["on_load_internal"] + ), val=exp_val, ) exp_router = RouterData.from_router_data(on_load_internal.router_data) @@ -6340,6 +6376,57 @@ async def test_connect_disconnect_counts_connections(otel_metrics): await ns._token_manager.disconnect_all() +@pytest.mark.asyncio +async def test_disconnect_during_scheme_mismatch_notice_leaves_no_sid_state(): + """A disconnect landing while the mismatch notice is emitted leaves nothing behind.""" + mock_app = unittest.mock.Mock() + mock_app._state = None + ns = EventNamespace(namespace="/", app=mock_app) + + async def emit(*args, **kwargs): + # socketio's emit awaits its send tasks, letting a disconnect run here. + if task := ns.on_disconnect("sid1"): + await task + + ns.emit = unittest.mock.AsyncMock(side_effect=emit) + await ns.on_connect("sid1", {"QUERY_STRING": "token=t1&scheme=stale"}) + ns.emit.assert_awaited_once() + assert "sid1" not in ns._scheme_mismatch_sids + assert "sid1" not in ns._static_router_data + await ns._token_manager.disconnect_all() + + +@pytest.mark.asyncio +@pytest.mark.parametrize("fail_at", ["link", "notice"]) +async def test_failed_connect_is_undone_by_disconnect(otel_metrics, fail_at): + """A connect that raises is undone by the disconnect socketio still sends. + + Args: + otel_metrics: The in-memory OpenTelemetry metric reader. + fail_at: The await that raises. + """ + mock_app = unittest.mock.Mock() + mock_app._state = None + ns = EventNamespace(namespace="/", app=mock_app) + ns.emit = unittest.mock.AsyncMock( + side_effect=RuntimeError("boom") if fail_at == "notice" else None + ) + if fail_at == "link": + ns.link_token_to_sid = unittest.mock.AsyncMock(side_effect=RuntimeError("boom")) + + with pytest.raises(RuntimeError, match="boom"): + await ns.on_connect("sid1", {"QUERY_STRING": "token=t1&scheme=stale"}) + # The sid stays connected in socketio's manager until its socket closes. + if task := ns.on_disconnect("sid1"): + await task + + assert "sid1" not in ns._scheme_mismatch_sids + assert "sid1" not in ns._static_router_data + (point,) = metric_points(otel_metrics, otel.METRIC_WEBSOCKET_CONNECTIONS) + assert point.value == 0 + await ns._token_manager.disconnect_all() + + def test_compile_installs_browser_plugin( compilable_app: tuple[App, Path], mocker: MockerFixture, diff --git a/tests/units/test_environment.py b/tests/units/test_environment.py index c48d27d85c7..0ed9f88498a 100644 --- a/tests/units/test_environment.py +++ b/tests/units/test_environment.py @@ -797,6 +797,57 @@ def test_timedelta_env_var_round_trips_through_set( assert env_var_instance.get() == value +@pytest.mark.parametrize( + "var", + [ + EnvironmentVariables.REFLEX_MINIFY_STATES, + EnvironmentVariables.REFLEX_MINIFY_EVENTS, + EnvironmentVariables.REFLEX_MINIFY_VARS, + ], +) +@pytest.mark.parametrize( + ("value", "expected"), + [ + ("true", True), + ("TRUE", True), + ("1", True), + ("yes", True), + ("false", False), + ("0", False), + ("no", False), + ("", False), + ], +) +def test_minify_env_vars_accept_boolean_literals(monkeypatch, var, value, expected): + """The minify toggles read like every other boolean env var. + + Args: + monkeypatch: The pytest monkeypatch fixture. + var: The env var under test. + value: The literal written to the environment. + expected: The value the env var should report. + """ + monkeypatch.setenv(var.name, value) + assert var.get() is expected + + +def test_minify_env_vars_default_to_off(monkeypatch): + """Minification stays off until it is asked for. + + Args: + monkeypatch: The pytest monkeypatch fixture. + """ + toggles = ( + EnvironmentVariables.REFLEX_MINIFY_STATES, + EnvironmentVariables.REFLEX_MINIFY_EVENTS, + EnvironmentVariables.REFLEX_MINIFY_VARS, + ) + for toggle in toggles: + monkeypatch.delenv(toggle.name, raising=False) + for toggle in toggles: + assert toggle.get() is False + + @pytest.fixture(autouse=True) def _forget_superseded_warnings(monkeypatch: pytest.MonkeyPatch) -> None: """Each test sees the deprecation warning as if the process had just started. diff --git a/tests/units/test_event.py b/tests/units/test_event.py index 538daadae01..06cb43912b8 100644 --- a/tests/units/test_event.py +++ b/tests/units/test_event.py @@ -1,10 +1,12 @@ +import copy import json import logging import shutil import subprocess from collections import UserList from collections.abc import Callable, Mapping, Sequence -from typing import Any, TypeVar, cast +from pathlib import Path +from typing import Any, TypeVar, cast, get_type_hints from unittest.mock import patch import pytest @@ -24,9 +26,11 @@ event, event_sequence, fix_events, + get_event, on_submit_event, on_submit_string_event, ) +from reflex_base.event.context import EventContext from reflex_base.registry import RegistrationContext from reflex_base.utils import console, format, log from reflex_base.utils.exceptions import ( @@ -41,7 +45,13 @@ from typing_extensions import TypeAliasType import reflex as rx +from reflex.istate.manager import StateManager +from reflex.istate.manager.token import BaseStateToken +from reflex.istate.proxy import StateProxy +from reflex.minify import StateEntry, get_state_full_path from reflex.state import BaseState +from tests.units.minify_helpers import install_config, set_minify_modes +from tests.units.name_resolvers import stub_resolver, temporary_resolver def make_var(value) -> Var: @@ -1597,6 +1607,18 @@ def handle_submit(form_data: dict[str, str]): assert "\\" not in out +def test_typing_event_helper_is_not_public(): + """The pyright-only handler marker is not part of the ``rx.event`` namespace. + + It lives in ``reflex.state`` as ``_typing_event``; both the name it used to + be attached under and the current one are checked. + """ + import reflex as rx + + assert not hasattr(rx.event, "typing_event") + assert not hasattr(rx.event, "_typing_event") + + def test_event_chain_cache_lives_on_the_registration_context( forked_registration_context: RegistrationContext, ): @@ -1684,6 +1706,99 @@ def args_spec(): ) +class CopiedHandlerState(BaseState): + """A state whose handler is copied before a resolver switch.""" + + def handle(self): + """A handler.""" + + +@pytest.mark.parametrize( + "derive", + [ + copy.copy, + copy.deepcopy, + lambda handler: handler.stop_propagation, + ], + ids=["copy", "deepcopy", "stop_propagation"], +) +def test_derived_handler_is_not_named_by_a_previous_resolver( + derive: Callable[[Any], Any], +): + """A handler copied or derived before a resolver switch gets the new name. + + Args: + derive: Builds the handler to format from the registered one. + """ + handler = CopiedHandlerState.event_handlers["handle"] + derived = derive(handler) + format.format_event_handler(derived) + with temporary_resolver(stub_resolver(handler_prefix="h_")): + assert format.format_event_handler(derived).endswith(".h_handle") + assert format.format_event_handler(derived).endswith(".handle") + + +def test_copied_handler_leaves_the_cached_resolver_behind(): + """Copies drop the cached name, so deepcopy never copies the resolver.""" + handler = CopiedHandlerState.event_handlers["handle"] + format.format_event_handler(handler) + assert handler._formatted_name is not None + for clone in (copy.copy, copy.deepcopy): + copied = clone(handler) + assert copied._formatted_name is None + assert copied == handler + + +class ProxiedEventState(BaseState): + """A root state whose background handler names itself through its proxy.""" + + outside: str = "" + inside: str = "" + + @rx.event(background=True) + async def handle(self): + """Record this handler's event name, outside and inside the lock.""" + outside = get_event(self, "handle") + async with self: + self.outside = outside + self.inside = get_event(self, "handle") + + +async def test_get_event_names_a_state_proxy_by_the_wrapped_state( + temp_minify_json: Path, + monkeypatch: pytest.MonkeyPatch, + token: str, + state_manager: StateManager, + attached_mock_event_context: EventContext, +): + """A background task's StateProxy resolves its handler through the wrapped state. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + token: The client token. + state_manager: The state manager to exercise. + attached_mock_event_context: The attached event context. + """ + set_minify_modes(monkeypatch, states=True, events=True) + path = get_state_full_path(ProxiedEventState) + install_config( + states={path: StateEntry(id="p", parent=None)}, + events={path: {"handle": "h"}}, + ) + state_token = BaseStateToken(ident=token, cls=ProxiedEventState) + async with state_manager.modify_state(state_token) as root: + proxy = StateProxy(root) + + await ProxiedEventState.event_handlers["handle"].fn(proxy) + assert (proxy.outside, proxy.inside) == ("p.h", "p.h") + + +def test_event_handler_type_hints_resolve(): + """Every ``EventHandler`` field annotation resolves at runtime, as docgen needs.""" + assert "_formatted_name" in get_type_hints(EventHandler, include_extras=True) + + _BOUNDED_SPEC_ARG = TypeVar("_BOUNDED_SPEC_ARG", bound=Mapping[str, Any]) _CONSTRAINED_SPEC_ARG = TypeVar("_CONSTRAINED_SPEC_ARG", int, str) diff --git a/tests/units/test_minify.py b/tests/units/test_minify.py new file mode 100644 index 00000000000..85103bad104 --- /dev/null +++ b/tests/units/test_minify.py @@ -0,0 +1,2588 @@ +"""Unit tests for reflex/minify.py.""" + +from __future__ import annotations + +import logging +import os +import weakref +from collections.abc import Callable +from pathlib import Path + +import pytest +from reflex_base.event import EventHandler +from reflex_base.registry import DefaultNameResolver, RegistrationContext, scheme_digest +from reflex_base.utils import format +from reflex_base.utils.exceptions import ReflexError +from reflex_base.utils.format import format_event_handler, format_queue_events + +import reflex as rx +from reflex.environment import environment +from reflex.minify import ( + MINIFY_JSON, + SCHEMA_VERSION, + MinifyConfig, + MinifyNameResolver, + StateEntry, + _find_missing_entries, + _get_minify_json_path, + _load_minify_config_uncached, + clear_config_cache, + ensure_minify_resolver_for_active_context, + generate_minify_config, + get_parent_key, + get_state_full_path, + int_to_minified_name, + minified_name_to_int, + raise_for_stale_names, + save_minify_config, + sync_minify_config, + validate_minify_config, + warn_if_config_stale, +) +from reflex.state import BaseState, State +from tests.units.minify_helpers import ( + install_config, + make_config, + minify_resolver, + resolved_event_id, + run_in_fresh_interpreter, + set_minify_modes, + write_config, +) +from tests.units.name_resolvers import stub_resolver, temporary_resolver + + +def test_zero(): + """Test that 0 maps to 'a'.""" + assert int_to_minified_name(0) == "a" + + +def test_int_to_minified_name_single_char(): + """Test single character mappings.""" + assert int_to_minified_name(1) == "b" + assert int_to_minified_name(25) == "z" + assert int_to_minified_name(26) == "A" + assert int_to_minified_name(51) == "Z" + assert int_to_minified_name(52) == "$" + assert int_to_minified_name(53) == "_" + + +def test_two_chars(): + """Test two character mappings (base 54).""" + # 54 = 1*54 + 0 -> 'ba' + assert int_to_minified_name(54) == "ba" + # 55 = 1*54 + 1 -> 'bb' + assert int_to_minified_name(55) == "bb" + + +def test_unique_names(): + """Test that a large range of IDs produce unique names.""" + names = set() + for i in range(10000): + name = int_to_minified_name(i) + assert name not in names, f"Duplicate name {name} for id {i}" + names.add(name) + + +def test_negative_raises(): + """Test that negative IDs raise ValueError.""" + with pytest.raises(ValueError) as error: + int_to_minified_name(-1) + assert str(error.value) == "ID must be non-negative, got -1" + + +def test_minified_name_to_int_single_char(): + """Test single character conversion.""" + assert minified_name_to_int("a") == 0 + assert minified_name_to_int("b") == 1 + assert minified_name_to_int("z") == 25 + assert minified_name_to_int("A") == 26 + assert minified_name_to_int("Z") == 51 + + +def test_roundtrip(): + """Test that int -> minified -> int roundtrip works.""" + for i in range(1000): + minified = int_to_minified_name(i) + result = minified_name_to_int(minified) + assert result == i, f"Roundtrip failed for {i}: {minified} -> {result}" + + +def test_invalid_char_raises(): + """Test that invalid characters raise ValueError.""" + with pytest.raises(ValueError) as error: + minified_name_to_int("!") + assert str(error.value) == "Invalid character in minified name: '!'" + + +def test_root_state_path(): + """Test that root State has correct full path.""" + path = get_state_full_path(State) + assert path == "reflex.state.State" + + +def test_substate_path(): + """Test that substates have correct full paths.""" + + class TestState(BaseState): + pass + + path = get_state_full_path(TestState) + assert "TestState" in path + assert path.startswith(f"{__name__}.") + + +def test_path_names_the_module_that_defines_a_state(temp_minify_json): + """A module-level subclass of a local state is keyed under its own module. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + def make_local_parent() -> type[State]: + class LocalPathParent(State): + pass + + return LocalPathParent + + parent = make_local_parent() + child = type("ModulePathChild", (parent,), {"__module__": "path_module_b"}) + parent_path = get_state_full_path(parent) + assert parent_path.startswith(f"{__name__}.") + assert get_state_full_path(child) == ( + f"path_module_b{parent_path.removeprefix(__name__)}.ModulePathChild" + ) + + +def test_component_state_is_keyed_under_its_component_module(temp_minify_json): + """A state ``ComponentState.create()`` builds keeps its component's module. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + import reflex as rx + + class PathCounter(rx.ComponentState): + count: int = 0 + + @classmethod + def get_component(cls, *children, **props) -> rx.Component: + return rx.text(cls.count) + + state = PathCounter.create(_state_key="path_counter").State + assert state is not None + assert get_state_full_path(state).startswith(f"{__name__}.State.") + + +def test_no_config_returns_none(temp_minify_json): + """Test that missing minify.json returns None.""" + assert _load_minify_config_uncached() is None + assert scheme_digest() == "" + + +def test_save_and_load_config(temp_minify_json, monkeypatch): + """Test saving and loading a config.""" + set_minify_modes(monkeypatch, states=True, events=True) + install_config( + states={"test.module.MyState": "a"}, + events={"test.module.MyState": {"handler": "a"}}, + ) + + assert scheme_digest() + loaded = _load_minify_config_uncached() + assert loaded is not None + assert loaded["states"]["test.module.MyState"] == {"id": "a", "parent": None} + assert loaded["events"]["test.module.MyState"]["handler"] == "a" + + +_WELL_FORMED = make_config() + +_BLURRING_REASON = ( + "a state id may not start or end with '_' or contain '__', which would make " + "two state paths read the same once dots become '__'. Run 'reflex minify " + "sync' to reassign it." +) + + +@pytest.mark.parametrize( + ("config", "message"), + [ + pytest.param( + {**_WELL_FORMED, "version": 999}, + f"Unsupported {MINIFY_JSON} version: 999. Expected {SCHEMA_VERSION}.", + id="version", + ), + pytest.param( + {"version": SCHEMA_VERSION, "events": {}}, + f"Invalid {MINIFY_JSON}: 'states' must be a dictionary.", + id="no-states", + ), + pytest.param( + {**_WELL_FORMED, "states": {"test.module.MyState": "a"}}, + f"Invalid {MINIFY_JSON}: state 'test.module.MyState' must be an object " + "with a string 'id' and a 'parent': a", + id="flat-state", + ), + pytest.param( + {**_WELL_FORMED, "states": {"test.module.MyState": {"id": "a"}}}, + f"Invalid {MINIFY_JSON}: state 'test.module.MyState' must be an object " + "with a string 'id' and a 'parent': {'id': 'a'}", + id="no-parent", + ), + *( + pytest.param( + { + **_WELL_FORMED, + "states": {"test.module.MyState": {"id": bad_id, "parent": None}}, + }, + f"Invalid {MINIFY_JSON}: state 'test.module.MyState' has invalid " + f"id: {bad_id!r}", + id=f"state-id-{bad_id!r}", + ) + for bad_id in ("", "1bad", "a-b", "a.b", "a b") + ), + *( + pytest.param( + { + **_WELL_FORMED, + "events": {"test.module.MyState": {"handler": bad_id}}, + }, + f"Invalid {MINIFY_JSON}: event 'test.module.MyState.handler' has " + f"invalid id: {bad_id!r}", + id=f"event-id-{bad_id!r}", + ) + for bad_id in ("", "1bad", "a-b") + ), + # ``a`` + ``_b`` and ``a_`` + ``b`` would both read ``a___b`` once a + # path's dots become ``__``, colliding two states' context keys. + *( + pytest.param( + { + **_WELL_FORMED, + "states": {"test.module.MyState": {"id": state_id, "parent": None}}, + }, + f"Invalid {MINIFY_JSON}: state 'test.module.MyState' has id " + f"{state_id!r}; {_BLURRING_REASON}", + id=f"blurring-state-id-{state_id!r}", + ) + for state_id in ("_", "_b", "a_", "a__b") + ), + *( + pytest.param( + {**_WELL_FORMED, "vars": section}, + f"Invalid {MINIFY_JSON}: {message}", + id=f"vars-{name}", + ) + for name, section, message in ( + ("not-a-dict", [], "'vars' must be a dictionary."), + ( + "state-not-a-dict", + {"test.module.MyState": []}, + "vars for 'test.module.MyState' must be a dictionary.", + ), + ( + "non-string", + {"test.module.MyState": {"count": 1}}, + "var 'test.module.MyState.count' has non-string id: 1", + ), + ( + "invalid", + {"test.module.MyState": {"count": "a-b"}}, + "var 'test.module.MyState.count' has invalid id: 'a-b'", + ), + # A var id is a key of its state's frontend object. + *( + ( + f"reserved-{reserved}", + {"test.module.MyState": {"count": reserved}}, + f"var 'test.module.MyState.count' has reserved id: {reserved!r}", + ) + for reserved in ("constructor", "__proto__", "a_rx_state_") + ), + ) + ), + ], +) +def test_config_rejects_malformed(temp_minify_json, config, message): + """A malformed ``minify.json`` is refused, naming what is wrong. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + config: The malformed contents. + message: The expected error. + """ + write_config(temp_minify_json, config) + with pytest.raises(ValueError) as error: + _load_minify_config_uncached() + assert str(error.value) == message + + +@pytest.mark.parametrize( + ("payload", "type_name"), + [("[1, 2, 3]", "list"), ('"a string"', "str"), ("42", "int"), ("null", "NoneType")], +) +def test_non_object_json_raises( + temp_minify_json: Path, payload: str, type_name: str +) -> None: + """Valid JSON that isn't an object is rejected as a ValueError. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + payload: The file's contents. + type_name: The name of the type it parses to. + """ + (temp_minify_json / MINIFY_JSON).write_text(payload, encoding="utf-8") + + with pytest.raises(ValueError) as error: + _load_minify_config_uncached() + assert str(error.value) == ( + f"Invalid {MINIFY_JSON}: must be a JSON object, got {type_name}." + ) + + +def test_generate_for_root_state(): + """Test generating config for the root State.""" + config = generate_minify_config(State) + + assert config["version"] == SCHEMA_VERSION + assert "reflex.state.State" in config["states"] + assert config["states"]["reflex.state.State"]["parent"] is None + # State should have event handlers like set_is_hydrated + state_path = "reflex.state.State" + assert state_path in config["events"] + assert "set_is_hydrated" in config["events"][state_path] + + +def test_generates_unique_sibling_ids(): + """Test that sibling states get unique IDs.""" + + class ParentState(BaseState): + pass + + class ChildA(ParentState): + pass + + class ChildB(ParentState): + pass + + config = generate_minify_config(ParentState) + + # Find the IDs for ChildA and ChildB + child_a_path = get_state_full_path(ChildA) + child_b_path = get_state_full_path(ChildB) + + child_a_entry = config["states"].get(child_a_path) + child_b_entry = config["states"].get(child_b_path) + + assert child_a_entry is not None + assert child_b_entry is not None + assert child_a_entry["id"] != child_b_entry["id"] + parent_path = get_state_full_path(ParentState) + assert child_a_entry["parent"] == parent_path + assert child_b_entry["parent"] == parent_path + + +def test_valid_config_no_errors(): + """Test that a valid config produces no errors.""" + config = generate_minify_config(State) + errors, _warnings, missing = validate_minify_config(config, State) + + assert len(errors) == 0 + assert len(missing) == 0 + + +def test_duplicate_state_ids_detected(): + """Test that duplicate state IDs are detected.""" + config = make_config( + states={ + "test.Parent": StateEntry(id="a", parent=None), + "test.Parent.ChildA": StateEntry(id="b", parent="test.Parent"), + "test.Parent.ChildB": StateEntry( # Duplicate! + id="b", parent="test.Parent" + ), + } + ) + + # Create a mock state tree + class Parent(BaseState): + pass + + errors, _warnings, _missing = validate_minify_config(config, Parent) + + assert errors == [ + ( + "Duplicate state_id='b' under 'test.Parent': " + "['test.Parent.ChildA (orphaned)', 'test.Parent.ChildB (orphaned)']" + ) + ] + + +def test_validate_detects_orphan_sibling_collision(): + """An orphaned entry sharing an id with a live sibling is an error.""" + + class OrphanCollisionParent(BaseState): + pass + + class OrphanCollisionLiveChild(OrphanCollisionParent): + pass + + parent_path = get_state_full_path(OrphanCollisionParent) + live_path = get_state_full_path(OrphanCollisionLiveChild) + orphan_path = f"{parent_path}.DeadChild" + + config = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + live_path: StateEntry(id="b", parent=parent_path), + orphan_path: StateEntry(id="b", parent=parent_path), # collision! + } + ) + + errors, _warnings, _missing = validate_minify_config(config, OrphanCollisionParent) + + assert errors == [ + ( + f"Duplicate state_id='b' under '{parent_path}': " + f"['{live_path}', '{orphan_path} (orphaned)']" + ) + ] + + +def test_validate_no_cross_group_orphan_false_positive(): + """An orphan of a different parent may share an id with a root state.""" + + class OrphanNoFalsePositiveRoot(BaseState): + pass + + root_path = get_state_full_path(OrphanNoFalsePositiveRoot) + config = make_config( + states={ + root_path: StateEntry(id="a", parent=None), + # Orphan from an unrelated (deleted) parent, same id "a". + "gone.module.Gone.Child": StateEntry(id="a", parent="gone.module.Gone"), + } + ) + + errors, warnings, _missing = validate_minify_config( + config, OrphanNoFalsePositiveRoot + ) + + assert errors == [] + assert warnings == ["Orphaned state in config: gone.module.Gone.Child"] + + +def test_validate_reports_missing_framework_states(): + """Framework states are ordinary states: omitting them is a gap to sync.""" + + class UserOnlyState(State): + def do_thing(self): + pass + + user_path = get_state_full_path(UserOnlyState) + # Config omits the framework reflex.state.State entries. + config = make_config( + states={user_path: StateEntry(id="a", parent="reflex.state.State")}, + events={user_path: {"do_thing": "a"}}, + ) + + _errors, _warnings, missing = validate_minify_config(config, State) + + assert "state:reflex.state.State" in missing + assert "event:reflex.state.State.hydrate" in missing + assert f"state:{user_path}" not in missing + assert f"event:{user_path}.do_thing" not in missing + + +def test_validate_warns_stale_parent(): + """A live entry whose stored parent disagrees with the code is flagged.""" + + class StaleParentParent(BaseState): + pass + + class StaleParentChild(StaleParentParent): + pass + + parent_path = get_state_full_path(StaleParentParent) + child_path = get_state_full_path(StaleParentChild) + config = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + child_path: StateEntry(id="b", parent="wrong.Path"), + } + ) + + errors, warnings, _missing = validate_minify_config(config, StaleParentParent) + + assert errors == [] + assert warnings == [ + ( + f"Stale parent for '{child_path}': config has 'wrong.Path', actual is " + f"'{parent_path}'. Run 'reflex minify sync' to fix." + ) + ] + + +def test_sync_adds_new_states(): + """Test that sync adds new states.""" + + class TestState(BaseState): + def handler(self): + pass + + # Start with empty config + existing_config = make_config() + + new_config = sync_minify_config(existing_config, TestState) + + # Should have added the state with its parent key recorded + state_path = get_state_full_path(TestState) + assert state_path in new_config["states"] + assert new_config["states"][state_path]["parent"] == get_parent_key(TestState) + assert state_path in new_config["events"] + assert "handler" in new_config["events"][state_path] + + +def test_sync_preserves_existing_ids(): + """Test that sync preserves existing IDs.""" + + class TestState(BaseState): + def handler_a(self): + pass + + def handler_b(self): + pass + + state_path = get_state_full_path(TestState) + + # Start with partial config + existing_config = make_config( + states={ + state_path: StateEntry(id="bU", parent=None) # codespell:ignore + }, + events={state_path: {"handler_a": "k"}}, + ) + + new_config = sync_minify_config(existing_config, TestState) + + # Existing IDs should be preserved + assert new_config["states"][state_path]["id"] == "bU" # codespell:ignore + assert new_config["events"][state_path]["handler_a"] == "k" + # New handler should be added with next ID (k=10, so next is l=11) + assert "handler_b" in new_config["events"][state_path] + assert new_config["events"][state_path]["handler_b"] == "l" # 10 + 1 = 11 -> 'l' + + +def test_sync_no_sibling_collision_across_modules(temp_minify_json): + """Test that sync assigns unique IDs to siblings of the same parent. + + When children of the same parent state class are defined in different + Python modules, their get_state_full_path() produces different string + prefixes. The sync function must group siblings by the actual parent + class object, not by string-splitting the path, to avoid ID collisions. + Runs in a forked context, so the relocated class stays out of later tests. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class ParentState(BaseState): + pass + + class ChildA(ParentState): + pass + + class ChildB(ParentState): + pass + + # Simulate ChildB living in another module, the way get_state_full_path + # sees a state relocated by ``ComponentState.create()``. Its path now + # diverges from ChildA's in the prefix, not just the trailing class name, + # so grouping siblings by string-splitting the path would put the two in + # different buckets and hand both the same id. + ChildB.__original_module__ = "some.other.module" + + parent_path = get_state_full_path(ParentState) + child_a_path = get_state_full_path(ChildA) + assert not get_state_full_path(ChildB).startswith(parent_path) + + # Config already has ParentState and ChildA assigned + existing_config = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + child_a_path: StateEntry(id="a", parent=parent_path), + } + ) + + # Sync should assign ChildB a DIFFERENT ID than ChildA + new_config = sync_minify_config(existing_config, ParentState) + + child_b_path = get_state_full_path(ChildB) + assert child_b_path in new_config["states"] + assert ( + new_config["states"][child_a_path]["id"] + != new_config["states"][child_b_path]["id"] + ), ( + f"Sibling collision: ChildA and ChildB both got " + f"'{new_config['states'][child_a_path]['id']}'" + ) + + +def test_validate_detects_sibling_collision(): + """Test that validate catches duplicate IDs among siblings of same parent.""" + + class ParentState(BaseState): + pass + + class ChildA(ParentState): + pass + + class ChildB(ParentState): + pass + + parent_path = get_state_full_path(ParentState) + child_a_path = get_state_full_path(ChildA) + child_b_path = get_state_full_path(ChildB) + + # Manually create a config with colliding sibling IDs + bad_config = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + child_a_path: StateEntry(id="b", parent=parent_path), + child_b_path: StateEntry(id="b", parent=parent_path), # collision! + } + ) + + errors, _warnings, _missing = validate_minify_config(bad_config, ParentState) + assert errors == [ + ( + f"Duplicate state_id='b' under '{parent_path}': " + f"['{child_a_path}', '{child_b_path}']" + ) + ] + + +def test_sync_reserves_orphan_ids(): + """A new sibling must not reuse the id of an orphaned entry.""" + + class OrphanReserveParent(BaseState): + pass + + class OrphanReserveRenamedChild(OrphanReserveParent): + pass + + parent_path = get_state_full_path(OrphanReserveParent) + orphan_path = f"{parent_path}.OldChild" + + existing_config = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + orphan_path: StateEntry(id="a", parent=parent_path), + } + ) + + new_config = sync_minify_config(existing_config, OrphanReserveParent) + + renamed_path = get_state_full_path(OrphanReserveRenamedChild) + assert new_config["states"][orphan_path] == StateEntry(id="a", parent=parent_path) + assert new_config["states"][renamed_path]["id"] != "a" + + +def test_sync_reassign_deleted_keeps_orphan_ids_reserved(): + """``reassign_deleted`` fills gaps but never reuses retained orphan ids.""" + + class OrphanReassignParent(BaseState): + pass + + class OrphanReassignChild(OrphanReassignParent): + pass + + parent_path = get_state_full_path(OrphanReassignParent) + orphan_path = f"{parent_path}.OldChild" + + existing_config = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + orphan_path: StateEntry(id="a", parent=parent_path), + } + ) + + new_config = sync_minify_config( + existing_config, OrphanReassignParent, reassign_deleted=True + ) + + child_path = get_state_full_path(OrphanReassignChild) + assert new_config["states"][child_path]["id"] != "a" + + +def test_sync_prune_frees_orphan_ids(): + """``prune`` removes orphans, freeing their ids for reassignment.""" + + class OrphanPruneParent(BaseState): + pass + + class OrphanPruneChild(OrphanPruneParent): + pass + + parent_path = get_state_full_path(OrphanPruneParent) + orphan_path = f"{parent_path}.OldChild" + + existing_config = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + orphan_path: StateEntry(id="b", parent=parent_path), + } + ) + + new_config = sync_minify_config( + existing_config, OrphanPruneParent, reassign_deleted=True, prune=True + ) + + child_path = get_state_full_path(OrphanPruneChild) + assert orphan_path not in new_config["states"] + assert new_config["states"][child_path]["id"] == "b" + + +def test_sync_heals_stale_parent(): + """Sync rewrites a live entry's stored parent to the actual value.""" + + class HealParentParent(BaseState): + pass + + class HealParentChild(HealParentParent): + pass + + parent_path = get_state_full_path(HealParentParent) + child_path = get_state_full_path(HealParentChild) + + existing_config = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + child_path: StateEntry(id="b", parent="wrong.Path"), + } + ) + + new_config = sync_minify_config(existing_config, HealParentParent) + + assert new_config["states"][child_path] == StateEntry(id="b", parent=parent_path) + + +@pytest.mark.parametrize( + "var", ["REFLEX_MINIFY_STATES", "REFLEX_MINIFY_EVENTS", "REFLEX_MINIFY_VARS"] +) +def test_enabled_requires_env_and_config(temp_minify_json, monkeypatch, var): + """Each mode renames only when its env var is on AND a config exists.""" + monkeypatch.setenv(getattr(environment, var).name, "1") + clear_config_cache() + assert scheme_digest() == "" # env on, no config + install_config(states={"x": "a"}, events={"x": {"h": "a"}}, vars={"x": {"v": "a"}}) + assert scheme_digest() + + +def test_modes_toggle_independently(temp_minify_json, monkeypatch): + """States can be on while events stay off (or vice versa).""" + + class ToggledState(State): + def handle(self): + pass + + path = get_state_full_path(ToggledState) + set_minify_modes(monkeypatch, states=True, events=False) + install_config( + states={path: StateEntry(id="z", parent="reflex.state.State")}, + events={path: {"handle": "a"}}, + ) + assert ToggledState.get_name() == "z" + assert resolved_event_id(ToggledState, "handle") is None + + +def test_disabled_returns_none(temp_minify_json): + """When neither flag is enabled, the resolver returns None for all.""" + resolver = minify_resolver(make_config()) + assert resolver.resolve_state_name(State) is None + assert resolver.resolve_handler_name(State, "any") is None + + +def test_resolver_no_config_returns_none(): + """No config means no overrides even when flags are enabled.""" + resolver = minify_resolver(states=True, events=True) + assert resolver.resolve_state_name(State) is None + assert resolver.resolve_handler_name(State, "any") is None + + +def test_state_lookup(): + """A state with an entry resolves to its id.""" + + # Non-framework state — see :func:`_is_framework_state`. + class UserStateResolverCacheTest(State): + pass + + config = make_config( + states={ + get_state_full_path(UserStateResolverCacheTest): StateEntry( + id="rs", parent=None + ) + } + ) + resolver = minify_resolver(config, states=True) + assert resolver.resolve_state_name(UserStateResolverCacheTest) == "rs" + + +def test_event_lookup(): + """Handlers with an entry resolve to their ids, others to ``None``.""" + + class UserStateEventCacheTest(State): + pass + + config = make_config( + events={get_state_full_path(UserStateEventCacheTest): {"foo": "f", "bar": "b"}} + ) + resolver = minify_resolver(config, events=True) + assert resolver.resolve_handler_name(UserStateEventCacheTest, "foo") == "f" + assert resolver.resolve_handler_name(UserStateEventCacheTest, "bar") == "b" + assert resolver.resolve_handler_name(UserStateEventCacheTest, "missing") is None + + +def test_from_disk_handles_malformed_config(temp_minify_json): + """``from_disk`` returns a usable resolver even when minify.json is bad.""" + path = temp_minify_json / MINIFY_JSON + with path.open("w") as f: + f.write("{not valid json") + resolver = MinifyNameResolver.from_disk() + # config falls back to None — every lookup returns None. + assert resolver.config is None + assert resolver.resolve_state_name(State) is None + assert resolver.resolve_handler_name(State, "any") is None + + +def test_root_state_name_follows_config(temp_minify_json, monkeypatch): + """``reflex.state.State`` is renamed like any other configured state.""" + set_minify_modes(monkeypatch, states=True) + install_config(states={"reflex.state.State": "a"}) + + assert State.get_name() == "a" + + +def test_hydrate_event_name_resolves_its_handler(temp_minify_json, monkeypatch): + """The hydrate name resolves its handler, however it is reached. + + The frontend sends whatever this returns, so a literal ``.hydrate`` + suffix here would never reach the minified registry key. + """ + from reflex_base.event import get_event, get_hydrate_event, get_hydrate_event_name + + set_minify_modes(monkeypatch, states=True, events=True) + install_config( + states={"reflex.state.State": "a"}, + events={"reflex.state.State": {"hydrate": "q"}}, + ) + + state = State(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] + + assert get_hydrate_event_name() == "a.q" + assert get_hydrate_event(state) == "a.q" + assert get_event(state, "hydrate") == "a.q" + assert "a.q" in RegistrationContext.get().event_handlers + + +@pytest.mark.parametrize("modes", ["unset", "off"]) +def test_empty_when_config_present_but_modes_off(temp_minify_json, monkeypatch, modes): + """A config nobody applies, by default or explicitly, leaves the wire names. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + modes: Whether the mode env vars are left unset or set off. + """ + if modes == "unset": + for env_var in ( + environment.REFLEX_MINIFY_STATES, + environment.REFLEX_MINIFY_EVENTS, + environment.REFLEX_MINIFY_VARS, + ): + monkeypatch.delenv(env_var.name, raising=False) + else: + set_minify_modes(monkeypatch, states=False, events=False, vars=False) + install_config( + states={"reflex.state.State": "a"}, + events={"reflex.state.State": {"hydrate": "a"}}, + vars={"reflex.state.State": {"is_hydrated": "h"}}, + ) + + assert scheme_digest() == "" + + +def test_differs_when_a_mode_is_toggled(temp_minify_json, monkeypatch): + """Turning events on renames handlers, so the schemes must not match.""" + config_states: dict[str, str | StateEntry] = {"reflex.state.State": "a"} + config_events = {"reflex.state.State": {"hydrate": "q"}} + + set_minify_modes(monkeypatch, states=True, events=False) + install_config(states=config_states, events=config_events) + states_only = scheme_digest() + + set_minify_modes(monkeypatch, events=True) + install_config(states=config_states, events=config_events) + both = scheme_digest() + + assert states_only + assert both + assert states_only != both + + +def test_same_scheme_digests_identically_across_processes(tmp_path): + """Both sides compute the digest independently; they must agree. + + Same-process stability is not enough: the frontend digest is baked at + compile time and the backend recomputes it in another interpreter. + """ + config = make_config( + states={"reflex.state.State": StateEntry(id="a", parent=None)}, + events={"reflex.state.State": {"hydrate": "q"}}, + ) + run_in_fresh_interpreter( + tmp_path, + config, + """ + from reflex_base.registry import scheme_digest + import reflex.state + + first = scheme_digest() + assert first, "expected a digest with minification enabled" + assert scheme_digest() == first, "digest is not stable" + print(first) + """, + REFLEX_MINIFY_STATES="1", + REFLEX_MINIFY_EVENTS="1", + ) + + +def test_independent_of_which_states_have_registered(temp_minify_json, monkeypatch): + """The digest hashes the config, not the state tree. + + Both sides read the same ``minify.json`` but register states at + different moments, so keying on the tree would make agreement depend + on timing. + """ + set_minify_modes(monkeypatch, states=True) + install_config(states={"reflex.state.State": "a"}) + + before = scheme_digest() + + class RegisteredAfterwards(State): + value: str = "" + + assert RegisteredAfterwards.get_full_name() + assert scheme_digest() == before + + +@pytest.mark.parametrize( + ("section", "before", "after"), + [ + ("states", {"reflex.state.State": "a"}, {"reflex.state.State": "b"}), + ( + "events", + {"reflex.state.State": {"hydrate": "a"}}, + {"reflex.state.State": {"hydrate": "b"}}, + ), + ( + "vars", + {"reflex.state.State": {"is_hydrated": "h"}}, + {"reflex.state.State": {"is_hydrated": "i"}}, + ), + ], +) +def test_differs_when_an_id_changes( + temp_minify_json, monkeypatch, section, before, after +): + """Editing minify.json renames names on the wire, so the schemes must differ. + + ``install_config`` reinstalls the resolver, which is the only thing that + invalidates the digest: a value cached anywhere that outlives the resolver + would fail here rather than reject every later connection. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + section: The config section, named like its mode, whose id changes. + before: The section before the edit. + after: The section after the edit. + """ + set_minify_modes(monkeypatch, **{section: True}) + + install_config(**{section: before}) + first = scheme_digest() + + install_config(**{section: after}) + + assert first + assert scheme_digest() not in ("", first) + + +def test_resolver_active_before_any_state_registers(tmp_path): + """A fresh interpreter registers user states under their minified name.""" + run_in_fresh_interpreter( + tmp_path, + make_config( + states={"check.State.Foo": StateEntry(id="f", parent="reflex.state.State")} + ), + """ + from reflex_base.registry import RegistrationContext + import reflex.state + resolver = RegistrationContext.ensure_context().name_resolver + assert type(resolver).__name__ == "MinifyNameResolver", resolver + class Foo(reflex.state.State): + pass + assert Foo.get_name() == "f", Foo.get_name() + """, + REFLEX_MINIFY_STATES="1", + ) + + +def test_no_resolver_installed_without_config(temp_minify_json: Path) -> None: + """Without a ``minify.json`` the zero-cost default resolver stays in place.""" + ctx = RegistrationContext.ensure_context() + ctx.set_name_resolver(DefaultNameResolver()) + + ensure_minify_resolver_for_active_context() + + assert type(ctx.name_resolver) is DefaultNameResolver + + +def test_resolver_installed_when_config_appears(temp_minify_json: Path) -> None: + """A ``minify.json`` showing up later still swaps the resolver in.""" + ctx = RegistrationContext.ensure_context() + ctx.set_name_resolver(DefaultNameResolver()) + + install_config(states={"test.module.MyState": "a"}) + ensure_minify_resolver_for_active_context() + + assert isinstance(ctx.name_resolver, MinifyNameResolver) + assert ctx.name_resolver.config is not None + + +def test_minify_json_path_defaults_to_the_working_directory( + temp_minify_json: Path, +) -> None: + """Without ``REFLEX_MINIFY_CONFIG``, the app's own ``minify.json`` is used.""" + assert _get_minify_json_path() == temp_minify_json / MINIFY_JSON + + +@pytest.mark.parametrize("absolute", [True, False]) +def test_minify_json_path_from_env_var( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch, absolute: bool +) -> None: + """``REFLEX_MINIFY_CONFIG`` names the file, relative to the working directory. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + absolute: Whether the env var holds an absolute path. + """ + target = temp_minify_json / "conf" / "names.json" + monkeypatch.setenv( + environment.REFLEX_MINIFY_CONFIG.name, + str(target if absolute else target.relative_to(temp_minify_json)), + ) + assert _get_minify_json_path() == target + + +def test_resolver_follows_the_app_directory( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Each app is loaded with the names of its own ``minify.json``, or none. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class FollowState(State): + pass + + path = get_state_full_path(FollowState) + set_minify_modes(monkeypatch, states=True) + write_config(temp_minify_json, make_config(states={path: "f"})) + write_config(temp_minify_json / "other", make_config(states={path: "g"})) + (temp_minify_json / "plain").mkdir() + ctx = RegistrationContext.ensure_context() + + ensure_minify_resolver_for_active_context() + assert FollowState.get_name() == "f" + resolver = ctx.name_resolver + ensure_minify_resolver_for_active_context() + assert ctx.name_resolver is resolver + + monkeypatch.chdir(temp_minify_json / "plain") + ensure_minify_resolver_for_active_context() + assert type(ctx.name_resolver) is DefaultNameResolver + assert FollowState.get_name() == RegistrationContext.default_state_name(FollowState) + + monkeypatch.chdir(temp_minify_json / "other") + ensure_minify_resolver_for_active_context() + assert FollowState.get_name() == "g" + + monkeypatch.chdir(temp_minify_json) + ensure_minify_resolver_for_active_context() + assert FollowState.get_name() == "f" + + +def test_resolver_reloads_an_edited_config( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """An edit to ``minify.json`` or a toggled mode applies on the next app load. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class EditedState(State): + pass + + path = get_state_full_path(EditedState) + set_minify_modes(monkeypatch, states=True) + write_config(temp_minify_json, make_config(states={path: "e"})) + ensure_minify_resolver_for_active_context() + assert EditedState.get_name() == "e" + + # Same size, modification time put back: only the content tells. + config_file = temp_minify_json / MINIFY_JSON + stat = config_file.stat() + write_config(temp_minify_json, make_config(states={path: "f"})) + os.utime(config_file, ns=(stat.st_atime_ns, stat.st_mtime_ns)) + assert config_file.stat().st_size == stat.st_size + ensure_minify_resolver_for_active_context() + assert EditedState.get_name() == "f" + + set_minify_modes(monkeypatch, states=False) + ensure_minify_resolver_for_active_context() + assert EditedState.get_name() == RegistrationContext.default_state_name(EditedState) + + +def test_malformed_config_warns_once_until_edited( + temp_minify_json: Path, caplog: pytest.LogCaptureFixture +) -> None: + """A broken ``minify.json`` is not re-read on every app load. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + caplog: The pytest log capture fixture. + """ + (temp_minify_json / MINIFY_JSON).write_text("{not json") + ensure_minify_resolver_for_active_context() + ensure_minify_resolver_for_active_context() + assert caplog.text.count("could not be loaded") == 1 + + (temp_minify_json / MINIFY_JSON).write_text("{still not json") + ensure_minify_resolver_for_active_context() + assert caplog.text.count("could not be loaded") == 2 + + +def test_env_var_pins_the_config_for_every_directory( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """``REFLEX_MINIFY_CONFIG`` applies whichever directory the app is in. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class PinnedState(State): + pass + + path = get_state_full_path(PinnedState) + set_minify_modes(monkeypatch, states=True) + write_config(temp_minify_json / "deploy", make_config(states={path: "p"})) + write_config(temp_minify_json / "app", make_config(states={path: "q"})) + monkeypatch.setenv( + environment.REFLEX_MINIFY_CONFIG.name, + str(temp_minify_json / "deploy" / MINIFY_JSON), + ) + + for directory in (temp_minify_json, temp_minify_json / "app"): + monkeypatch.chdir(directory) + ensure_minify_resolver_for_active_context() + assert PinnedState.get_name() == "p" + + +def test_custom_resolver_is_left_in_place(temp_minify_json: Path) -> None: + """A resolver the user installed is not replaced by the app's config. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + install_config(states={"test.module.MyState": "a"}) + custom = stub_resolver(state_name="custom") + with temporary_resolver(custom) as ctx: + ensure_minify_resolver_for_active_context() + assert ctx.name_resolver is custom + (temp_minify_json / MINIFY_JSON).unlink() + ensure_minify_resolver_for_active_context() + assert ctx.name_resolver is custom + + +def test_framework_names_reach_registered_handlers(temp_minify_json, monkeypatch): + """The names the context module emits are the keys the backend dispatches on. + + The config is installed after the framework states were created, so this + also covers renaming states whose Vars already exist. + """ + from reflex_base.event import get_hydrate_event + + from reflex.compiler.compiler import _internal_names + + set_minify_modes(monkeypatch, states=True, events=True, vars=True) + install_config( + states={ + "reflex.state.State": StateEntry(id="a", parent=None), + "reflex.state.State.FrontendEventExceptionState": StateEntry( + id="b", parent="reflex.state.State" + ), + "reflex.state.State.OnLoadInternalState": StateEntry( + id="c", parent="reflex.state.State" + ), + "reflex.state.State.UpdateVarsInternalState": StateEntry( + id="d", parent="reflex.state.State" + ), + }, + events={ + "reflex.state.State": {"hydrate": "a", "hydrate_and_load": "b"}, + "reflex.state.State.FrontendEventExceptionState": { + "handle_frontend_exception": "a" + }, + "reflex.state.State.OnLoadInternalState": {"on_load_internal": "a"}, + "reflex.state.State.UpdateVarsInternalState": {"update_vars_internal": "a"}, + }, + vars={"reflex.state.State": {"is_hydrated": "h"}}, + ) + + assert State.get_name() == "a" + names = _internal_names() + assert names.main_state_name == "a" + assert names.is_hydrated_key == "h" + assert names.hydrate_and_load == "a.b" + + handlers = RegistrationContext.get().event_handlers + for wire_name in ( + names.hydrate_and_load, + names.on_load_internal, + names.update_vars_internal, + names.handle_frontend_exception, + ): + assert wire_name in handlers, (wire_name, sorted(handlers)) + + # The legacy hydrate event, which the middleware compares against, is + # renamed the same way. + root = State(_reflex_internal_init=True, init_substates=False) # pyright: ignore [reportCallIssue] + assert get_hydrate_event(root) == "a.a" + assert "a.a" in handlers + + # The Var the frontend reads names the context and key the compiler emits. + assert str(State.is_hydrated) == "$rx_a.h" + assert root.dict()["a"]["h"] is False + + +def _parent_id_collisions(config: MinifyConfig) -> list[str]: + """Find entries that share their parent's id. + + Args: + config: The config to inspect. + + Returns: + The state paths that reuse their parent's id. + """ + states = config["states"] + return [ + path + for path, entry in states.items() + if entry["parent"] is not None + and (parent := states.get(entry["parent"])) is not None + and parent["id"] == entry["id"] + ] + + +def test_generate_reserves_the_parent_id(): + """No generated state shares an id with its parent. + + A shared id makes a leading path segment ambiguous, so ``_skip_self`` + resolves a relative substate path to the parent instead of the child. + """ + assert _parent_id_collisions(generate_minify_config()) == [] + + +def test_sync_reserves_the_parent_id(): + """Ids assigned by ``sync`` skip the parent's own id too.""" + + class SyncReserveParent(BaseState): + pass + + class SyncReserveChild(SyncReserveParent): + pass + + parent_path = get_state_full_path(SyncReserveParent) + existing = make_config(states={parent_path: StateEntry(id="a", parent=None)}) + + new_config = sync_minify_config(existing, SyncReserveParent) + + child = new_config["states"][get_state_full_path(SyncReserveChild)] + assert child["id"] != "a" + assert _parent_id_collisions(new_config) == [] + + +def test_sync_numbers_new_children_like_init(): + """The parent's id is skipped but does not move where numbering starts.""" + + class NumberedParent(BaseState): + pass + + class NumberedFirst(NumberedParent): + pass + + class NumberedSecond(NumberedParent): + pass + + existing = make_config( + states={get_state_full_path(NumberedParent): StateEntry(id="k", parent=None)} + ) + + new_config = sync_minify_config(existing, NumberedParent) + + assert [ + new_config["states"][get_state_full_path(child)]["id"] + for child in (NumberedFirst, NumberedSecond) + ] == ["a", "b"] + + +def test_validate_detects_a_child_reusing_its_parent_id(): + """``validate`` reports a hand-edited config that reintroduces the collision.""" + + class ValidateReuseParent(BaseState): + pass + + class ValidateReuseChild(ValidateReuseParent): + pass + + parent_path = get_state_full_path(ValidateReuseParent) + child_path = get_state_full_path(ValidateReuseChild) + config = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + child_path: StateEntry(id="a", parent=parent_path), + } + ) + + errors, _warnings, _missing = validate_minify_config(config, ValidateReuseParent) + + assert errors == [ + ( + f"State '{child_path}' reuses the id 'a' of its parent '{parent_path}'. " + f"Delete {MINIFY_JSON} and re-run 'reflex minify init'." + ) + ] + + +def test_sync_reserves_the_parent_id_through_a_new_subtree(): + """A whole new branch keeps the invariant at every level. + + Each level's id is reserved from its children's pool only once the parent + has been assigned, so this pins the order ``sync`` walks the buckets in. + """ + + class DeepSyncRoot(BaseState): + pass + + class DeepSyncA(DeepSyncRoot): + pass + + class DeepSyncB(DeepSyncA): + pass + + existing = make_config( + states={get_state_full_path(DeepSyncRoot): StateEntry(id="a", parent=None)} + ) + + new_config = sync_minify_config(existing, DeepSyncRoot) + + assert _parent_id_collisions(new_config) == [] + + +def test_sync_moves_a_reparented_id_off_its_new_parent(): + """A preserved id that survives a move must not land on the new parent's. + + `sync` heals the stored parent but keeps the id, so without a fix-up it + writes a config its own `validate` rejects. + """ + + class MoveParent(BaseState): + pass + + class MovedChild(MoveParent): + pass + + parent_path = get_state_full_path(MoveParent) + existing = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + get_state_full_path(MovedChild): StateEntry( + id="a", parent="some.old.Parent" + ), + } + ) + + new_config = sync_minify_config(existing, MoveParent) + + assert _parent_id_collisions(new_config) == [] + errors, _warnings, _missing = validate_minify_config(new_config, MoveParent) + assert not errors, errors + + +def test_sync_moves_a_preserved_id_off_a_newly_inserted_parent(): + """Inserting a state above a preserved one must not collide either. + + The new parent draws its id from a different sibling pool, so it can land + on the id the child below it already holds. + """ + + class MidRoot(BaseState): + pass + + class NewMid(MidRoot): + pass + + class OldLeaf(NewMid): + pass + + root_path = get_state_full_path(MidRoot) + existing = make_config( + states={ + root_path: StateEntry(id="a", parent=None), + get_state_full_path(OldLeaf): StateEntry(id="b", parent=root_path), + } + ) + + new_config = sync_minify_config(existing, MidRoot) + + assert _parent_id_collisions(new_config) == [] + errors, _warnings, _missing = validate_minify_config(new_config, MidRoot) + assert not errors, errors + + +def test_sync_moves_a_reparented_id_off_an_occupied_sibling_id(): + """Healing a parent must not drop an entry onto a sibling's id. + + The entry keeps its id across the move, so it can collide with a sibling + already holding it. The entry that moved gives way; the one whose scope + did not change keeps the id a served frontend may still be using. + """ + + class SiblingRoot(BaseState): + pass + + class SiblingParent(SiblingRoot): + pass + + class SiblingMoved(SiblingParent): + pass + + class SiblingIncumbent(SiblingParent): + pass + + root_path = get_state_full_path(SiblingRoot) + parent_path = get_state_full_path(SiblingParent) + moved_path = get_state_full_path(SiblingMoved) + incumbent_path = get_state_full_path(SiblingIncumbent) + existing = make_config( + states={ + root_path: StateEntry(id="a", parent=None), + parent_path: StateEntry(id="b", parent=root_path), + # Recorded under the root, but the code says SiblingParent. + moved_path: StateEntry(id="c", parent=root_path), + incumbent_path: StateEntry(id="c", parent=parent_path), + } + ) + + new_config = sync_minify_config(existing, SiblingRoot) + + assert new_config["states"][incumbent_path]["id"] == "c" + assert new_config["states"][moved_path]["id"] != "c" + errors, _warnings, _missing = validate_minify_config(new_config, SiblingRoot) + assert not errors, errors + + +def test_sync_keeps_ids_that_only_decode_alike(): + """``sync`` compares ids as the strings they are on the wire. + + A hand-edited ``aa`` decodes to the same number as ``a`` and ``ab`` to the + same as ``b``, yet each is a distinct name, so none of them has to move. + """ + + class LeadingRoot(BaseState): + pass + + class LeadingKept(LeadingRoot): + pass + + class LeadingMoved(LeadingRoot): + pass + + class LeadingSibling(LeadingRoot): + pass + + root_path = get_state_full_path(LeadingRoot) + kept_path = get_state_full_path(LeadingKept) + moved_path = get_state_full_path(LeadingMoved) + existing = make_config( + states={ + root_path: StateEntry(id="a", parent=None), + kept_path: StateEntry(id="aa", parent=root_path), + moved_path: StateEntry(id="ab", parent="some.old.Parent"), + get_state_full_path(LeadingSibling): StateEntry(id="b", parent=root_path), + } + ) + + new_config = sync_minify_config(existing, LeadingRoot) + + assert new_config["states"][kept_path]["id"] == "aa" + assert new_config["states"][moved_path] == StateEntry(id="ab", parent=root_path) + errors, _warnings, _missing = validate_minify_config(new_config, LeadingRoot) + assert not errors, errors + + +def test_sync_assigns_ids_that_only_decode_alike(): + """A held ``aa`` reserves only itself, so filling gaps still hands out ``a``.""" + + class AliasRoot(BaseState): + pass + + class AliasHeld(AliasRoot): + def held(self): + pass + + def added(self): + pass + + class AliasNew(AliasRoot): + pass + + root_path = get_state_full_path(AliasRoot) + held_path = get_state_full_path(AliasHeld) + existing = make_config( + states={ + root_path: StateEntry(id="b", parent=None), + held_path: StateEntry(id="aa", parent=root_path), + }, + events={held_path: {"held": "aa"}, "gone.State": {"x": "aa", "y": "aa"}}, + ) + + new_config = sync_minify_config(existing, AliasRoot, reassign_deleted=True) + + assert new_config["states"][get_state_full_path(AliasNew)]["id"] == "a" + assert new_config["events"][held_path]["added"] == "a" + assert new_config["events"]["gone.State"] == {"x": "aa", "y": "a"} + + +def test_validate_exempts_an_orphan_holding_its_parent_id(): + """An orphan may keep an id its live parent also holds. + + Its id stays reserved at the value a served frontend may still use, and it + resolves no name at runtime, so moving it would cost more than it buys. + ``sync`` must not produce a config ``validate`` then rejects. + """ + + class OrphanHolder(BaseState): + pass + + parent_path = get_state_full_path(OrphanHolder) + orphan_path = f"{parent_path}.DeletedChild" + existing = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + orphan_path: StateEntry(id="a", parent=parent_path), + } + ) + + new_config = sync_minify_config(existing, OrphanHolder) + + assert new_config["states"][orphan_path]["id"] == "a" + errors, _warnings, _missing = validate_minify_config(new_config, OrphanHolder) + assert not errors, errors + + +def test_validate_checks_the_actual_parent_not_the_recorded_one(): + """A stale ``parent`` must not hide a real collision. + + Lookups walk the live tree, so the collision that matters is with the + actual parent even when the config still records an older one. + """ + + class ActualParentState(BaseState): + pass + + class ActualChildState(ActualParentState): + pass + + parent_path = get_state_full_path(ActualParentState) + child_path = get_state_full_path(ActualChildState) + config = make_config( + states={ + parent_path: StateEntry(id="a", parent=None), + child_path: StateEntry(id="a", parent="some.stale.Path"), + } + ) + + errors, _warnings, _missing = validate_minify_config(config, ActualParentState) + + assert errors == [ + ( + f"State '{child_path}' reuses the id 'a' of its parent '{parent_path}'. " + f"Delete {MINIFY_JSON} and re-run 'reflex minify init'." + ) + ] + + +def test_find_missing_entries_flags_a_state_added_after_the_config(): + """A state the config predates compiles unminified and is reported.""" + config = generate_minify_config() + + class LateAddedState(State): + @staticmethod + def ping(): + """A handler the config cannot know about.""" + + resolver = minify_resolver(config, states=True, events=True) + with temporary_resolver(resolver): + missing = _find_missing_entries() + + assert f"state:{get_state_full_path(LateAddedState)}" in missing + + +def test_find_missing_entries_respects_disabled_modes(): + """A disabled mode emits full names by design, so it reports nothing.""" + config = generate_minify_config() + + class ModeGatedState(State): + pass + + path = get_state_full_path(ModeGatedState) + with temporary_resolver(minify_resolver(config, events=True)): + assert f"state:{path}" not in _find_missing_entries() + with temporary_resolver(minify_resolver(config)): + assert _find_missing_entries() == [] + + +def test_find_missing_entries_without_a_minify_resolver(): + """The default resolver rewrites nothing, so nothing is missing.""" + with temporary_resolver(DefaultNameResolver()): + assert _find_missing_entries() == [] + + +def test_warn_if_config_stale_points_at_sync(caplog): + """The warning names the command that fixes it. + + Args: + caplog: The pytest log capture fixture. + """ + config = generate_minify_config() + + class StaleWarningState(State): + pass + + path = get_state_full_path(StaleWarningState) + resolver = minify_resolver(config, states=True, events=True) + with temporary_resolver(resolver), caplog.at_level("WARNING"): + warn_if_config_stale() + + assert caplog.record_tuples == [ + ( + "reflex.minify", + logging.WARNING, + ( + f"{MINIFY_JSON} is out of date, so these names compile unminified: " + f"event:{path}.setvar, state:{path}. Run 'reflex minify sync' to " + "assign ids; 'reflex minify validate' lists every missing entry." + ), + ) + ] + + +def test_warn_if_config_stale_is_silent_when_current(caplog): + """A config that covers every registered name produces no warning. + + Args: + caplog: The pytest log capture fixture. + """ + resolver = minify_resolver(generate_minify_config(), states=True, events=True) + with temporary_resolver(resolver), caplog.at_level("WARNING"): + warn_if_config_stale() + + assert caplog.text == "" + + +def test_config_without_vars_loads_with_empty_vars(temp_minify_json): + """Files written before vars were minified still load.""" + write_config( + temp_minify_json, {"version": SCHEMA_VERSION, "states": {}, "events": {}} + ) + loaded = _load_minify_config_uncached() + assert loaded is not None + assert loaded["vars"] == {} + + +def test_reserved_ids_are_fine_for_events(temp_minify_json): + """Only var ids name object keys; an event id may be any identifier.""" + write_config( + temp_minify_json, + make_config(events={"test.module.MyState": {"handler": "constructor"}}), + ) + loaded = _load_minify_config_uncached() + assert loaded is not None + assert loaded["events"]["test.module.MyState"]["handler"] == "constructor" + + +def test_resolve_var_name_follows_its_mode(temp_minify_json): + """Var ids apply only while ``REFLEX_MINIFY_VARS`` is on. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class VarModeState(State): + count: int = 0 + + config = make_config(vars={get_state_full_path(VarModeState): {"count": "c"}}) + on = minify_resolver(config, vars=True) + off = minify_resolver(config, states=True, events=True) + assert on.resolve_var_name(VarModeState, "count") == "c" + assert on.resolve_var_name(VarModeState, "missing") is None + assert off.resolve_var_name(VarModeState, "count") is None + + +def test_generate_numbers_the_frontend_vars_of_each_state(temp_minify_json): + """Each state numbers the vars it sends to the client, not inherited ones. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class GenVarParent(State): + count: int = 0 + _secret: str = "" + + @rx.var + def doubled(self) -> int: + return self.count * 2 + + @rx.var(backend=True) + def hidden(self) -> int: + return 0 + + class GenVarChild(GenVarParent): + label: str = "" + + config = generate_minify_config(GenVarParent) + assert config["vars"] == { + get_state_full_path(GenVarParent): {"count": "a", "doubled": "b"}, + get_state_full_path(GenVarChild): {"label": "a"}, + } + + +def test_validate_reports_var_problems(temp_minify_json): + """Duplicate var ids fail validation; missing and orphaned vars are reported. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class ValidateVarState(State): + first: int = 0 + second: int = 0 + + path = get_state_full_path(ValidateVarState) + config = generate_minify_config(ValidateVarState) + config["vars"][path] = {"first": "a", "second": "a", "gone": "b"} + + errors, warnings, missing = validate_minify_config(config, ValidateVarState) + assert any( + f"vars of '{path}' share ids ('a' by first, second)" in error + for error in errors + ) + assert f"Orphaned var in config: {path}.gone" in warnings + assert missing == [] + + del config["vars"][path] + _errors, _warnings, missing = validate_minify_config(config, ValidateVarState) + assert missing == [f"var:{path}.first", f"var:{path}.second"] + + +def test_sync_assigns_new_var_ids_and_prunes_old_ones(temp_minify_json): + """New vars get fresh ids; existing ones keep theirs until pruned. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class SyncVarState(State): + kept: int = 0 + added: int = 0 + + path = get_state_full_path(SyncVarState) + existing = generate_minify_config(SyncVarState) + existing["vars"][path] = {"kept": "b", "removed": "a"} + + synced = sync_minify_config(existing, SyncVarState) + assert synced["vars"][path] == {"kept": "b", "removed": "a", "added": "c"} + + pruned = sync_minify_config(existing, SyncVarState, prune=True) + assert pruned["vars"][path] == {"kept": "b", "added": "c"} + + +def test_missing_vars_reported_only_when_minified(temp_minify_json, monkeypatch): + """The stale-config warning covers vars once their mode is on. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class MissingVarState(State): + count: int = 0 + + label = f"var:{get_state_full_path(MissingVarState)}.count" + + set_minify_modes(monkeypatch, states=False, events=False, vars=False) + install_config() + assert label not in _find_missing_entries() + + set_minify_modes(monkeypatch, vars=True) + install_config() + assert label in _find_missing_entries() + + +def _stale_names_error(code: str) -> str: + """Scan compiled code for stale names and return the error raised, if any. + + Args: + code: The compiled code. + + Returns: + The error message, or an empty string when the code is clean. + """ + try: + raise_for_stale_names([("page.jsx", code)]) + except ReflexError as e: + return str(e) + return "" + + +def test_stale_names_ignored_without_minification(): + """Nothing is renamed, so the default spellings are the right ones.""" + assert _stale_names_error(f"{State.get_full_name()}.hydrate") == "" + + +def test_stale_state_names_are_rejected(temp_minify_json, monkeypatch): + """Code still spelling a renamed state the default way cannot reach it. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class StaleNameState(State): + count: int = 0 + + default_expr = str(StaleNameState.count) + default_event = f"{StaleNameState.get_full_name()}.setvar" + path = get_state_full_path(StaleNameState) + + set_minify_modes(monkeypatch, states=True) + install_config(states={path: StateEntry(id="z", parent="reflex.state.State")}) + + fresh = str(StaleNameState.count) + assert fresh != default_expr + assert _stale_names_error(f"const x = {fresh};") == "" + for stale in (default_expr, f'addEvents([ReflexEvent("{default_event}")])'): + error = _stale_names_error(stale) + assert f"state {path}" in error + assert "page.jsx" in error + + +def test_stale_names_are_scanned_only_once_an_install_renames( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Compiled code holds no stale name until an install renames a handed-out one. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class UnscannedState(State): + count: int = 0 + + path = get_state_full_path(UnscannedState) + stale_expr = str(UnscannedState.count) + local = format.format_state_name(UnscannedState._get_default_full_name()) + monkeypatch.setattr(format, "_renamed_after_issue", False) + set_minify_modes(monkeypatch, states=True) + + # Another resolver that renames nothing leaves the scan off. + clear_config_cache() + assert not format.names_may_be_stale() + + install_config(states={path: StateEntry(id="z", parent="reflex.state.State")}) + assert format.names_may_be_stale() + assert _stale_names_error(stale_expr) == ( + "The compiled frontend refers to names the active name resolver does " + f"not use:\n state {path}, as '{local}' in page.jsx\n" + "They come from Vars built while another resolver was installed, e.g. by " + "a component created at import time of a module loaded before this app " + f"directory (and its {MINIFY_JSON}) was, or while another app's was. " + "Import the app from its directory, or create those components inside " + "the page function." + ) + + monkeypatch.setattr(format, "_renamed_after_issue", False) + assert _stale_names_error(stale_expr) == "" + + # Nothing handed out before a rename cannot have gone stale. + monkeypatch.setattr(format, "_issued_state_locals", {}) + monkeypatch.setattr(format, "_issued_var_keys", weakref.WeakKeyDictionary()) + monkeypatch.setattr(format, "_issued_event_handlers", {}) + format._note_names_changed() + assert not format.names_may_be_stale() + + +def test_stale_event_names_are_rejected_when_only_events_minify( + temp_minify_json, monkeypatch +): + """With states unminified, only the handler part of a name goes stale. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class StaleEventState(State): + @rx.event + def ping(self): + pass + + path = get_state_full_path(StaleEventState) + full_name = StaleEventState.get_full_name() + + set_minify_modes(monkeypatch, states=False, events=True) + install_config(events={path: {"ping": "p"}}) + + assert _stale_names_error(f'"{full_name}.p"') == "" + assert f"event handler {path}.ping" in _stale_names_error(f'"{full_name}.ping"') + + +def test_handler_id_matching_a_renamed_handler_name_is_not_rejected( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A handler's id may spell another handler's Python name. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class AliasedEventState(State): + @rx.event + def foo(self): + pass + + @rx.event + def bar(self): + pass + + path = get_state_full_path(AliasedEventState) + # Handed out for ``foo`` while it still goes by its own name. + rx.button(on_click=AliasedEventState.foo) + set_minify_modes(monkeypatch, states=False, events=True) + install_config(events={path: {"foo": "a", "bar": "foo"}}) + + event = f'"{format_event_handler(AliasedEventState.event_handlers["bar"])}"' + assert _stale_names_error(event) == "" + assert _stale_names_error(str(rx.button(on_click=AliasedEventState.bar))) == "" + + +def test_stale_var_keys_are_rejected_when_only_vars_minify( + temp_minify_json, monkeypatch +): + """With states unminified, only the var part of an expression goes stale. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class StaleVarState(State): + count: int = 0 + + path = get_state_full_path(StaleVarState) + default_expr = str(StaleVarState.count) + + set_minify_modes(monkeypatch, states=False, vars=True) + install_config(vars={path: {"count": "c"}}) + + assert _stale_names_error(str(StaleVarState.count)) == "" + assert f"var {path}.count" in _stale_names_error(f"{default_expr}.length") + + +def test_stale_resolved_state_names_are_rejected( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Code built under another app's ``minify.json`` cannot reach its states. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class ResolvedState(State): + count: int = 0 + + set_minify_modes(monkeypatch, states=True) + install_config( + states={ + get_state_full_path(ResolvedState): StateEntry( + id="z", parent="reflex.state.State" + ) + } + ) + minified_expr = str(ResolvedState.count) + assert "$rx_" in minified_expr + assert _stale_names_error(minified_expr) == "" + + (temp_minify_json / "plain").mkdir() + monkeypatch.chdir(temp_minify_json / "plain") + ensure_minify_resolver_for_active_context() + + assert _stale_names_error(str(ResolvedState.count)) == "" + error = _stale_names_error(f"const x = {minified_expr};") + assert f"state {get_state_full_path(ResolvedState)}" in error + assert "page.jsx" in error + + +@pytest.mark.parametrize("new_vars", [{"count": "d"}, {}], ids=["other", "none"]) +def test_stale_var_keys_of_a_state_keeping_its_name_are_rejected( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch, new_vars: dict[str, str] +) -> None: + """A var key from another ``minify.json`` is caught though its state's id matches. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + new_vars: The var ids of the config the code is compiled under. + """ + + class KeptNameState(State): + count: int = 0 + + path = get_state_full_path(KeptNameState) + entry = StateEntry(id="z", parent="reflex.state.State") + set_minify_modes(monkeypatch, states=True, vars=True) + install_config(states={path: entry}, vars={path: {"count": "c"}}) + old_expr = str(KeptNameState.count) + + install_config(states={path: entry}, vars={path: new_vars}) + + assert _stale_names_error(str(KeptNameState.count)) == "" + error = _stale_names_error(f"{old_expr}.length") + assert f"var {path}.count, as '{old_expr}'" in error + + +def test_stale_var_read_is_rejected_when_another_state_takes_its_local( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A var read off a local now handed to another state is caught by its key. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class FormerOwnerState(State): + count: int = 0 + + class NewOwnerState(State): + total: int = 0 + + former, new = ( + get_state_full_path(FormerOwnerState), + get_state_full_path(NewOwnerState), + ) + set_minify_modes(monkeypatch, states=True, vars=True) + root = "reflex.state.State" + install_config( + states={ + former: StateEntry(id="k", parent=root), + new: StateEntry(id="m", parent=root), + }, + vars={former: {"count": "c"}, new: {"total": "t"}}, + ) + old_expr = str(FormerOwnerState.count) + + install_config( + states={ + former: StateEntry(id="m", parent=root), + new: StateEntry(id="k", parent=root), + }, + vars={former: {"count": "c"}, new: {"total": "t"}}, + ) + + assert str(NewOwnerState.total).split(".")[0] == old_expr.split(".")[0] + assert _stale_names_error(str(NewOwnerState.total)) == "" + assert f"var {former}.count" in _stale_names_error(old_expr) + + +def test_stale_minified_var_keys_are_rejected_after_unminifying( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A minified var key is caught once its state's vars are unminified. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class UnminifiedVarState(State): + count: int = 0 + + path = get_state_full_path(UnminifiedVarState) + set_minify_modes(monkeypatch, states=False, vars=True) + install_config(vars={path: {"count": "c"}}) + old_expr = str(UnminifiedVarState.count) + + set_minify_modes(monkeypatch, vars=False) + install_config(vars={path: {"count": "c"}}) + + assert _stale_names_error(str(UnminifiedVarState.count)) == "" + assert f"var {path}.count" in _stale_names_error(old_expr) + + +@pytest.mark.parametrize( + "emit", + [ + lambda handler: str(rx.button(on_click=handler)), + lambda handler: str(format_queue_events(handler)), + ], + ids=["component", "callback"], +) +def test_stale_minified_event_names_are_rejected( + temp_minify_json: Path, + monkeypatch: pytest.MonkeyPatch, + emit: Callable[[EventHandler], str], +) -> None: + """An event built under another ``minify.json`` is caught by its handler's id. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + emit: Builds compiled code calling a handler. + """ + + class MovedEventState(State): + @rx.event + def ping(self): + pass + + path = get_state_full_path(MovedEventState) + handler = MovedEventState.event_handlers["ping"] + set_minify_modes(monkeypatch, states=False, events=True) + install_config(events={path: {"ping": "c"}}) + old_code = emit(handler) + old_name = format_event_handler(handler) + + install_config(events={path: {"ping": "d"}}) + + assert _stale_names_error(emit(handler)) == "" + assert _stale_names_error(f'"{old_name}"') == "" + error = _stale_names_error(old_code) + assert f"event handler {path}.ping, as '{old_name}' in page.jsx" in error + + +def test_stale_event_names_are_rejected_when_only_the_state_id_changes( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """An event built under another ``minify.json`` is caught by its state's id. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class MovedStateEventState(State): + @rx.event + def ping(self): + pass + + path = get_state_full_path(MovedStateEventState) + root = "reflex.state.State" + events = {path: {"ping": "c"}} + set_minify_modes(monkeypatch, states=True, events=True) + install_config(states={path: StateEntry(id="z", parent=root)}, events=events) + old_code = str(rx.button(on_click=MovedStateEventState.ping)) + + install_config(states={path: StateEntry(id="y", parent=root)}, events=events) + + assert _stale_names_error(str(rx.button(on_click=MovedStateEventState.ping))) == "" + assert f"event handler {path}.ping" in _stale_names_error(old_code) + + +def test_event_id_reassigned_to_another_handler_is_rejected( + temp_minify_json: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """An event whose id now names another handler is caught unless built for it. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class ReassignedEventState(State): + @rx.event + def ping(self): + pass + + @rx.event + def pong(self): + pass + + path = get_state_full_path(ReassignedEventState) + set_minify_modes(monkeypatch, states=False, events=True) + install_config(events={path: {"ping": "c", "pong": "e"}}) + old_code = str(rx.button(on_click=ReassignedEventState.ping)) + + install_config(events={path: {"ping": "d", "pong": "c"}}) + + error = _stale_names_error(old_code) + assert f"event handler {path}.ping" in error + assert f"{path}.pong" not in error + # Code built for the handler now holding the id reads the same. + new_code = str(rx.button(on_click=ReassignedEventState.pong)) + assert new_code == old_code + assert _stale_names_error(new_code) == "" + + +def test_text_resembling_state_reads_is_not_rejected(temp_minify_json: Path) -> None: + """Only locals and var keys once handed out count, not look-alike page text. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class LookAlikeState(State): + pass + + root = State.get_full_name() + for code in ( + '"Total: $rx_total"', + "$rx_never__issued.c", + f'"{root}.hydrate"', + f'"{root}.{LookAlikeState.get_name()}.anything"', + f"{root}.not_a_var", + ): + assert _stale_names_error(code) == "", code + + +def test_generated_state_ids_keep_paths_distinct(temp_minify_json): + """Numbering past ``$`` skips the ids an underscore would blur. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + parent = type("ManySiblingsParent", (State,), {"__module__": __name__}) + siblings = [ + type(f"ManySiblings{index:03d}", (parent,), {"__module__": __name__}) + for index in range(120) + ] + config = generate_minify_config(parent) + ids = [config["states"][get_state_full_path(s)]["id"] for s in siblings] + assert len(set(ids)) == len(ids) + assert not [i for i in ids if i.startswith("_") or i.endswith("_") or "__" in i] + + # Sync assigns the ids a later sibling needs the same way. + del config["states"][get_state_full_path(siblings[-1])] + synced = sync_minify_config(config, parent) + new_id = synced["states"][get_state_full_path(siblings[-1])]["id"] + assert not (new_id.startswith("_")) + assert not (new_id.endswith("_")) + assert "__" not in new_id + + +def _config_with_ambiguous_ids(parent: type[BaseState]) -> MinifyConfig: + """A config as generated before ambiguous state ids were skipped. + + Args: + parent: A state with at least one substate. + + Returns: + The config, with the first substate on ``_`` and an orphan on ``a_``. + """ + config = generate_minify_config(parent) + parent_path = get_state_full_path(parent) + child_path = next( + path for path, entry in config["states"].items() if entry["parent"] + ) + config["states"][child_path]["id"] = "_" + config["states"][f"{parent_path}.Gone"] = StateEntry(id="a_", parent=parent_path) + return config + + +def _load_for_repair( + directory: Path, config: MinifyConfig, message: str +) -> MinifyConfig: + """Write a config the app refuses, and load it the way the CLI repairs it. + + Args: + directory: Where to write it. + config: The config needing ``reflex minify sync``. + message: The error the app refuses it with. + + Returns: + The config as loaded for repair. + """ + write_config(directory, config) + with pytest.raises(ValueError) as error: + _load_minify_config_uncached() + assert str(error.value) == message + loaded = _load_minify_config_uncached(for_repair=True) + assert loaded is not None + return loaded + + +def _assert_repaired(config: MinifyConfig) -> None: + """Save a repaired config and check the app loads it again. + + Args: + config: The repaired config. + """ + save_minify_config(config) + assert _load_minify_config_uncached() is not None + + +def test_ambiguous_state_ids_are_reported_and_reassigned(temp_minify_json): + """A config written before the rule loads for the CLI, which repairs it. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + parent = type("AmbiguousParent", (State,), {"__module__": __name__}) + type("AmbiguousChild", (parent,), {"__module__": __name__}) + parent_path = get_state_full_path(parent) + config = _config_with_ambiguous_ids(parent) + message = f"state '{parent_path}' has id '_'; {_BLURRING_REASON}" + loaded = _load_for_repair( + temp_minify_json, config, f"Invalid {MINIFY_JSON}: {message}" + ) + + errors, _warnings, _missing = validate_minify_config(loaded, parent) + assert errors == [ + message, + f"state '{parent_path}.Gone' has id 'a_'; {_BLURRING_REASON}", + ] + + synced = sync_minify_config(loaded, parent) + ids = {path: entry["id"] for path, entry in synced["states"].items()} + assert "_" not in ids.values() + assert "a_" not in ids.values() + assert len(ids) == len(config["states"]) + _assert_repaired(synced) + + +def test_state_id_naming_a_prototype_member_is_rejected_and_reassigned( + temp_minify_json, +): + """A root state on ``constructor`` would find ``Object`` as its dispatcher. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + root = type("PrototypeIdRoot", (BaseState,), {"__module__": __name__}) + path = get_state_full_path(root) + config = generate_minify_config(root) + config["states"][path]["id"] = "constructor" + write_config(temp_minify_json, config) + message = ( + f"state '{path}' has id 'constructor'; a root state id may not name an " + "Object.prototype member, which the frontend's state maps would " + "inherit. Run 'reflex minify sync' to reassign it." + ) + + with pytest.raises(ValueError) as error: + _load_minify_config_uncached() + assert str(error.value) == f"Invalid {MINIFY_JSON}: {message}" + loaded = _load_minify_config_uncached(for_repair=True) + assert loaded is not None + errors, _warnings, _missing = validate_minify_config(loaded, root) + assert errors == [message] + + synced = sync_minify_config(loaded, root) + assert synced["states"] == {path: StateEntry(id="a", parent=None)} + save_minify_config(synced) + assert _load_minify_config_uncached() == synced + + +def test_nested_state_id_may_name_a_prototype_member(temp_minify_json): + """A nested state's frontend key is its dotted path, never a bare id. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + root = type("PrototypeIdParent", (BaseState,), {"__module__": __name__}) + child = type("PrototypeIdChild", (root,), {"__module__": __name__}) + config = generate_minify_config(root) + config["states"][get_state_full_path(child)]["id"] = "constructor" + write_config(temp_minify_json, config) + + assert _load_minify_config_uncached() == config + assert validate_minify_config(config, root) == ([], [], []) + assert sync_minify_config(config, root) == config + + +def test_validate_judges_a_root_id_by_both_parents(): + """A stale ``parent`` must not hide a root id naming a prototype member. + + The app loads the file by the recorded parent, while the frontend keys a + state by its actual one, so the id has to suit both. + """ + root = type("StaleRootParent", (BaseState,), {"__module__": __name__}) + child = type("StaleRootChild", (root,), {"__module__": __name__}) + root_path = get_state_full_path(root) + child_path = get_state_full_path(child) + config = generate_minify_config(root) + config["states"][root_path] = StateEntry(id="constructor", parent="stale.Parent") + config["states"][child_path] = StateEntry(id="toString", parent=None) + reason = ( + "a root state id may not name an Object.prototype member, which the " + "frontend's state maps would inherit. Run 'reflex minify sync' to " + "reassign it." + ) + + assert validate_minify_config(config, root) == ( + [ + f"state '{root_path}' has id 'constructor'; {reason}", + f"state '{child_path}' has id 'toString'; {reason}", + ], + [ + ( + f"Stale parent for '{root_path}': config has 'stale.Parent', " + "actual is None. Run 'reflex minify sync' to fix." + ), + ( + f"Stale parent for '{child_path}': config has None, actual is " + f"'{root_path}'. Run 'reflex minify sync' to fix." + ), + ], + [], + ) + + synced = sync_minify_config(config, root) + assert synced["states"] == { + root_path: StateEntry(id="a", parent=None), + child_path: StateEntry(id="toString", parent=root_path), + } + assert validate_minify_config(synced, root) == ([], [], []) + + +def test_duplicate_event_ids_are_rejected_and_reassigned(temp_minify_json): + """Two handlers of a state on one id would leave one unreachable. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class DuplicateEventState(State): + def first(self): + pass + + def second(self): + pass + + path = get_state_full_path(DuplicateEventState) + config = generate_minify_config(DuplicateEventState) + config["events"][path] = {"first": "a", "second": "a", "setvar": "c"} + config["events"]["gone.module.State"] = {"x": "b", "y": "b"} + loaded = _load_for_repair( + temp_minify_json, + config, + f"Invalid {MINIFY_JSON}: events of '{path}' share ids ('a' by first, " + "second), which the frontend and backend cannot tell apart. Run 'reflex " + "minify sync' to reassign them.", + ) + + synced = sync_minify_config(loaded, DuplicateEventState) + assert synced["events"][path]["first"] == "a" + assert synced["events"][path]["second"] not in ("a", "c") + assert len(set(synced["events"]["gone.module.State"].values())) == 2 + _assert_repaired(synced) + + +def test_duplicate_var_ids_are_rejected_and_reassigned(temp_minify_json): + """Two vars of a state on one id would overwrite each other on the wire. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class DuplicateVarState(State): + first: int = 0 + second: int = 0 + + path = get_state_full_path(DuplicateVarState) + config = generate_minify_config(DuplicateVarState) + config["vars"][path] = {"first": "a", "second": "a"} + loaded = _load_for_repair( + temp_minify_json, + config, + f"Invalid {MINIFY_JSON}: vars of '{path}' share ids ('a' by first, " + "second), which the frontend and backend cannot tell apart. Run 'reflex " + "minify sync' to reassign them.", + ) + + synced = sync_minify_config(loaded, DuplicateVarState) + assert synced["vars"][path]["first"] == "a" + assert synced["vars"][path]["second"] != "a" + _assert_repaired(synced) + + +def test_duplicate_repair_keeps_the_live_handler_id(temp_minify_json): + """A deleted handler sharing a live one's id gives way, whatever the order. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class LiveHandlerState(State): + def zed(self): + pass + + path = get_state_full_path(LiveHandlerState) + config = generate_minify_config(LiveHandlerState) + config["events"][path] = {"alpha_gone": "a", "zed": "a", "setvar": "b"} + + synced = sync_minify_config(config, LiveHandlerState) + assert synced["events"][path]["zed"] == "a" + assert synced["events"][path]["alpha_gone"] not in ("a", "b") + + +def test_duplicate_repair_keeps_the_live_var_id(temp_minify_json): + """A deleted var sharing a live one's id gives way, whatever the order. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class LiveVarState(State): + zed: int = 0 + + path = get_state_full_path(LiveVarState) + config = generate_minify_config(LiveVarState) + config["vars"][path] = {"alpha_gone": "a", "zed": "a"} + + synced = sync_minify_config(config, LiveVarState) + assert synced["vars"][path]["zed"] == "a" + assert synced["vars"][path]["alpha_gone"] != "a" diff --git a/tests/units/test_reflex.py b/tests/units/test_reflex.py index fba31b1da4f..bdd8b25a51a 100644 --- a/tests/units/test_reflex.py +++ b/tests/units/test_reflex.py @@ -11,19 +11,27 @@ import subprocess import sys import time +from pathlib import Path +from textwrap import dedent from types import SimpleNamespace +from unittest import mock import click import click.testing import psutil import pytest +from click.testing import CliRunner from pytest_mock import MockerFixture from reflex_base import constants from reflex_base.environment import environment +from reflex_base.registry import RegistrationContext from reflex_base.utils import log from reflex import reflex +from reflex.minify import _get_minify_json_path, clear_config_cache, get_state_full_path +from reflex.state import State from reflex.testing import DEFAULT_TIMEOUT +from tests.units.minify_helpers import install_config _CLI_STARTUP_DENIED_MODULES = frozenset({ "PIL", @@ -50,6 +58,34 @@ }) +@pytest.fixture +def cli_runner(monkeypatch: pytest.MonkeyPatch) -> CliRunner: + """Click runner with ``prerequisites.get_compiled_app`` stubbed out. + + Args: + monkeypatch: The pytest monkeypatch fixture. + + Returns: + A ``CliRunner`` ready to invoke ``reflex.reflex.cli`` commands. + """ + from reflex.utils import prerequisites + + monkeypatch.setattr(prerequisites, "get_compiled_app", lambda *a, **kw: mock.Mock()) + return CliRunner() + + +def _unwrapped(output: str) -> str: + """Undo the wrapping log messages get at the terminal width. + + Args: + output: The captured CLI output. + + Returns: + Its words, separated by single spaces. + """ + return " ".join(output.split()) + + def _run_cli_probe(probe: str) -> dict[str, object]: """Run a CLI import probe in a fresh interpreter. @@ -113,6 +149,7 @@ def test_cli_startup_does_not_import_runtime_modules( import sys from click.testing import CliRunner +from pytest_mock import MockerFixture from reflex.reflex import cli result = CliRunner().invoke(cli, {argv!r}) @@ -140,6 +177,7 @@ def test_backend_launcher_does_not_import_compiler_or_state() -> None: """ import sys from reflex import reflex +from reflex.environment import environment from reflex.istate.manager import reset_disk_state_manager from reflex.utils import build, exec, telemetry @@ -263,7 +301,9 @@ def test_cloud_commands_registered(): import click from click.testing import CliRunner +from pytest_mock import MockerFixture from reflex import reflex +from reflex.environment import environment deploy_command = reflex.cli.commands["deploy"] cloud_command = reflex.cli.commands["cloud"] @@ -604,6 +644,823 @@ def test_init_records_version_check_after_frontend_setup( assert events == ["frontend", "version"] +def test_minify_sync_and_validate_name_the_configured_file( + temp_minify_json: Path, + cli_runner: CliRunner, + monkeypatch: pytest.MonkeyPatch, + mocker: MockerFixture, +) -> None: + """With ``REFLEX_MINIFY_CONFIG`` set, the commands name the file they use. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + cli_runner: The click CLI runner. + monkeypatch: The pytest monkeypatch fixture. + mocker: The pytest-mock fixture. + """ + from reflex.reflex import cli + + target = temp_minify_json / "deploy" / "names.json" + target.parent.mkdir() + monkeypatch.setenv(environment.REFLEX_MINIFY_CONFIG.name, str(target)) + install_config(include_state_root=True) + info = mocker.spy(reflex.logger, "info") + + synced = cli_runner.invoke(cli, ["minify", "sync"]) + assert synced.exit_code == 0, synced.output + validated = cli_runner.invoke(cli, ["minify", "validate"]) + assert validated.exit_code == 0, validated.output + + messages = [call.args[0] for call in info.call_args_list] + assert f"Updated {target}:" in messages + assert f"{target} is valid and up-to-date." in messages + + +def test_lookup_resolves_minified_path(temp_minify_json, cli_runner): + """Test that lookup resolves a minified path to full state info.""" + from reflex.reflex import cli + + class AppState(State): + pass + + class ChildState(AppState): + pass + + install_config( + states={ + get_state_full_path(AppState): "b", + get_state_full_path(ChildState): "c", + }, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "b.c"]) + + assert result.exit_code == 0, result.output + # A locally defined state takes a suffix when its name is already taken. + assert result.output == ( + f"{__name__}.{AppState.__name__}\n{__name__}.{ChildState.__name__}\n" + ) + + +def test_lookup_accepts_full_wire_path(temp_minify_json, cli_runner): + """A path copied verbatim from the frontend keeps the root state prefix.""" + from reflex.reflex import cli + + class WirePathState(State): + pass + + install_config( + states={get_state_full_path(WirePathState): "b"}, + include_state_root=True, + ) + + result = cli_runner.invoke( + cli, ["minify", "lookup", "--json", f"{State.get_name()}.b"] + ) + + assert result.exit_code == 0, result.output + output_data = json.loads(result.output) + assert [info["class"] for info in output_data] == ["WirePathState"] + + +def test_lookup_accepts_both_root_prefix_spellings(temp_minify_json, cli_runner): + """The root prefix resolves whether given minified or in full. + + Args: + temp_minify_json: The temporary config fixture. + cli_runner: The click CLI runner. + """ + from reflex.reflex import cli + + class RootIdState(State): + pass + + # "a" is the root's config id; the only real segment is RootIdState's "b". + install_config( + states={get_state_full_path(RootIdState): "b"}, + include_state_root=True, + ) + + for prefix in ("a", RegistrationContext.default_state_name(State)): + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", f"{prefix}.b"]) + assert result.exit_code == 0, f"{prefix}: {result.output}" + assert [info["class"] for info in json.loads(result.output)] == [ + RootIdState.__name__ + ], f"{prefix}: {result.output}" + + +def test_lookup_fails_without_minify_json(temp_minify_json, cli_runner): + """Test that lookup fails gracefully when minify.json is missing.""" + from reflex.reflex import cli + + clear_config_cache() + # Wide enough that the path, whose length varies, is not wrapped. + result = cli_runner.invoke( + cli, ["minify", "lookup", "a.b"], env={"COLUMNS": "1000"} + ) + + assert result.exit_code == 1 + assert result.output == ( + f"{_get_minify_json_path()} does not exist. Use 'reflex minify init' to " + "create it.\n" + ) + + +def test_lookup_reads_the_configured_minify_json( + temp_minify_json: Path, cli_runner: CliRunner, monkeypatch: pytest.MonkeyPatch +) -> None: + """``REFLEX_MINIFY_CONFIG`` points the minify commands at another file.""" + from reflex.reflex import cli + + class ConfiguredPathState(State): + pass + + install_config( + states={get_state_full_path(ConfiguredPathState): "b"}, + include_state_root=True, + ) + target = temp_minify_json / "deploy" / "names.json" + target.parent.mkdir() + (temp_minify_json / "minify.json").rename(target) + monkeypatch.setenv(environment.REFLEX_MINIFY_CONFIG.name, str(target)) + clear_config_cache() + + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", "b"]) + + assert result.exit_code == 0, result.output + assert [info["class"] for info in json.loads(result.output)] == [ + ConfiguredPathState.__name__ + ] + + +def test_lookup_fails_for_malformed_config( + temp_minify_json: Path, cli_runner: CliRunner +) -> None: + """Test that a malformed minify.json exits cleanly, not with a traceback.""" + from reflex.reflex import cli + + (temp_minify_json / "minify.json").write_text("{not json", encoding="utf-8") + clear_config_cache() + + result = cli_runner.invoke(cli, ["minify", "lookup", "a.b"]) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert _unwrapped(result.output) == ( + "Invalid JSON in minify.json: Expecting property name enclosed in double " + "quotes: line 1 column 2 (char 1)" + ) + + +def test_lookup_fails_for_invalid_path(temp_minify_json, cli_runner): + """Test that lookup fails for non-existent minified path.""" + from reflex.reflex import cli + + class InvalidPathState(State): + pass + + install_config( + states={get_state_full_path(InvalidPathState): "b"}, + include_state_root=True, + ) + result = cli_runner.invoke(cli, ["minify", "lookup", "b.xyz"]) + + assert result.exit_code == 1 + assert _unwrapped(result.output) == ( + "No state, event handler or var found for minified segment 'xyz' in path " + "'b.xyz'" + ) + + +def test_lookup_resolves_event_handler(temp_minify_json, cli_runner): + """The final segment of a copied event name is a handler id, not a state id.""" + from reflex.reflex import cli + + class HandlerLookupState(State): + def increment(self): + pass + + state_path = get_state_full_path(HandlerLookupState) + install_config( + states={state_path: "b"}, + events={state_path: {"increment": "cX"}}, + include_state_root=True, + ) + + result = cli_runner.invoke( + cli, ["minify", "lookup", "--json", f"{State.get_name()}.b.cX"] + ) + + assert result.exit_code == 0, result.output + output_data = json.loads(result.output) + assert [entry["kind"] for entry in output_data] == ["state", "event"] + assert {entry["module"] for entry in output_data} == {__name__} + assert output_data[1]["class"] == "HandlerLookupState" + assert output_data[1]["handler"] == "increment" + assert output_data[1]["event_id"] == "cX" + assert output_data[1]["full_path"] == f"{state_path}.increment" + + +def test_lookup_event_handler_text_output(temp_minify_json, cli_runner): + """Text output names the handler after its owning state class.""" + from reflex.reflex import cli + + class HandlerTextState(State): + def increment(self): + pass + + state_path = get_state_full_path(HandlerTextState) + install_config( + states={state_path: "b"}, + events={state_path: {"increment": "a"}}, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "b.a"]) + + assert result.exit_code == 0, result.output + assert result.output == ( + f"{__name__}.HandlerTextState\n{__name__}.HandlerTextState.increment\n" + ) + + +def test_lookup_reports_ambiguous_final_segment(temp_minify_json, cli_runner): + """A final segment that is both a substate id and a handler id yields both.""" + from reflex.reflex import cli + + class AmbiguousParentState(State): + def increment(self): + pass + + class AmbiguousChildState(AmbiguousParentState): + pass + + parent_path = get_state_full_path(AmbiguousParentState) + install_config( + states={ + parent_path: "b", + get_state_full_path(AmbiguousChildState): "a", + }, + events={parent_path: {"increment": "a"}}, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", "b.a"]) + + assert result.exit_code == 0, result.output + output_data = json.loads(result.output) + assert [(entry["kind"], entry["class"]) for entry in output_data] == [ + ("state", "AmbiguousParentState"), + ("state", "AmbiguousChildState"), + ("event", "AmbiguousParentState"), + ] + + text_result = cli_runner.invoke(cli, ["minify", "lookup", "b.a"]) + + assert text_result.exit_code == 0, text_result.output + assert _unwrapped(text_result.output) == ( + "Warning: Segment 'a' is the id of a state and of an event handler of " + f"{__name__}.AmbiguousParentState; showing each. " + f"{__name__}.AmbiguousParentState {__name__}.AmbiguousChildState " + f"{__name__}.AmbiguousParentState.increment" + ) + + +@pytest.mark.parametrize("minified", [True, False]) +def test_lookup_bare_root_segment(temp_minify_json, cli_runner, minified): + """A lone root segment is the root state, not a prefix with nothing behind it. + + Args: + temp_minify_json: The temporary config fixture. + cli_runner: The click CLI runner. + minified: Whether to look the root state up by its minified id. + """ + from reflex.reflex import cli + + install_config(include_state_root=True) + + segment = "a" if minified else RegistrationContext.default_state_name(State) + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", segment]) + + assert result.exit_code == 0, result.output + assert [(entry["kind"], entry["class"]) for entry in json.loads(result.output)] == [ + ("state", "State") + ] + + +def test_lookup_bare_root_segment_also_matches_root_handler( + temp_minify_json, cli_runner +): + """The root state id and a root handler id collide; both are reported.""" + from reflex.reflex import cli + + install_config( + events={get_state_full_path(State): {"hydrate": "a"}}, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", "a"]) + + assert result.exit_code == 0, result.output + assert [ + (entry["kind"], entry.get("handler")) for entry in json.loads(result.output) + ] == [("state", None), ("event", "hydrate")] + + +def test_lookup_accepts_unminified_segments(temp_minify_json, cli_runner): + """States and events minify independently, so either half may be unminified.""" + from reflex.reflex import cli + + class MixedModeState(State): + def increment(self): + pass + + state_path = get_state_full_path(MixedModeState) + install_config( + states={state_path: "b"}, + events={state_path: {"increment": "c"}}, + include_state_root=True, + ) + default_name = RegistrationContext.default_state_name(MixedModeState) + + # Only events minified: the state keeps its default name on the wire. + result = cli_runner.invoke( + cli, ["minify", "lookup", "--json", f"{State.get_name()}.{default_name}.c"] + ) + assert result.exit_code == 0, result.output + assert [(e["kind"], e["class"]) for e in json.loads(result.output)] == [ + ("state", "MixedModeState"), + ("event", "MixedModeState"), + ] + + # Only states minified: the handler keeps its Python name. + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", "b.increment"]) + assert result.exit_code == 0, result.output + assert json.loads(result.output)[1]["handler"] == "increment" + + +def test_lookup_reports_configured_ids_for_unminified_segments( + temp_minify_json, cli_runner +): + """``state_id``/``event_id`` are the configured ids, not the segment typed.""" + from reflex.reflex import cli + + class ConfiguredIdState(State): + def increment(self): + pass + + class UnconfiguredIdState(ConfiguredIdState): + def decrement(self): + pass + + state_path = get_state_full_path(ConfiguredIdState) + install_config( + states={state_path: "b"}, + events={state_path: {"increment": "c"}}, + include_state_root=True, + ) + default_name = RegistrationContext.default_state_name(ConfiguredIdState) + + result = cli_runner.invoke( + cli, ["minify", "lookup", "--json", f"{default_name}.increment"] + ) + + assert result.exit_code == 0, result.output + output_data = json.loads(result.output) + assert [(e["kind"], e["minified"]) for e in output_data] == [ + ("state", default_name), + ("event", "increment"), + ] + assert output_data[0]["state_id"] == "b" + assert output_data[1]["event_id"] == "c" + + # A state and handler with no configured id report ``None``, not the name. + child_name = RegistrationContext.default_state_name(UnconfiguredIdState) + result = cli_runner.invoke( + cli, ["minify", "lookup", "--json", f"b.{child_name}.decrement"] + ) + + assert result.exit_code == 0, result.output + output_data = json.loads(result.output) + assert output_data[1]["state_id"] is None + assert output_data[2]["event_id"] is None + + +def test_lookup_handler_id_only_matches_final_segment(temp_minify_json, cli_runner): + """A handler id in the middle of a path is an error, not a state.""" + from reflex.reflex import cli + + class MiddleHandlerState(State): + def increment(self): + pass + + state_path = get_state_full_path(MiddleHandlerState) + install_config( + states={state_path: "b"}, + events={state_path: {"increment": "c"}}, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "b.c.d"]) + + assert result.exit_code == 1 + assert _unwrapped(result.output) == ( + "No state found for minified segment 'c' in path 'b.c.d'" + ) + + +def test_lookup_with_json_output(temp_minify_json, cli_runner): + """Test that lookup with --json flag outputs valid JSON.""" + from reflex.reflex import cli + + class JsonTestState(State): + pass + + install_config( + states={get_state_full_path(JsonTestState): "b"}, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", "b"]) + + assert result.exit_code == 0, result.output + assert json.loads(result.output) == [ + { + "kind": "state", + "minified": "b", + "state_id": "b", + "module": __name__, + "class": "JsonTestState", + "full_path": get_state_full_path(JsonTestState), + } + ] + + +@pytest.mark.parametrize("command", ["list", "lookup"]) +def test_stdout_is_reserved_before_the_app_loads( + command, temp_minify_json, monkeypatch, cli_runner +): + """Reserving after the app loaded would be too late to help. + + Loading the app dry-runs a compile, which logs warnings and + deprecations; those go to stdout unless it is claimed first. + + Args: + command: The ``reflex minify`` subcommand under test. + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + cli_runner: Click runner with the app loader stubbed. + """ + from reflex_base.utils import log + + from reflex.reflex import cli + from reflex.utils import prerequisites + + monkeypatch.setattr(log, "_stdout_reserved", False) + + class JsonOutputState(State): + pass + + install_config( + states={get_state_full_path(JsonOutputState): "b"}, + include_state_root=True, + ) + args = ["minify", command, "--json"] + if command == "lookup": + args.append("b") + + reserved_when_loading: list[bool] = [] + + def _noisy_load(*a, **kw): + reserved_when_loading.append(log.is_stdout_reserved()) + # Loading an app runs arbitrary module-level code; reserving only + # covers Reflex's own logging, not a raw write like this one. + print("noise from the app import") + return mock.Mock() + + monkeypatch.setattr(prerequisites, "get_compiled_app", _noisy_load) + + result = cli_runner.invoke(cli, args) + + assert reserved_when_loading == [True] + assert "noise from the app import" not in result.stdout + json.loads(result.stdout) + + +@pytest.mark.parametrize("command", ["list", "lookup"]) +def test_json_output_does_not_leak_the_stdout_reservation( + command, temp_minify_json, cli_runner +): + """The reservation is scoped to the command, not the process. + + A real CLI process exits, but in-process callers would otherwise leave + every later log write pointed at stderr. + + Args: + command: The ``reflex minify`` subcommand under test. + temp_minify_json: Temporary ``minify.json`` location. + cli_runner: Click runner with the app loader stubbed. + """ + from reflex_base.utils import log + + from reflex.reflex import cli + + class ReservationState(State): + pass + + install_config( + states={get_state_full_path(ReservationState): "b"}, + include_state_root=True, + ) + args = ["minify", command, "--json"] + if command == "lookup": + args.append("b") + + assert cli_runner.invoke(cli, args).exit_code == 0 + assert log.is_stdout_reserved() is False + + +def test_lookup_resolves_var_key(temp_minify_json, cli_runner): + """A delta key appended to its state's name resolves to the var. + + The key may be copied minified, unminified with its field marker, or be + the var's own name. + """ + from reflex.reflex import cli + + class VarLookupState(State): + count: int = 0 + + state_path = get_state_full_path(VarLookupState) + install_config( + states={state_path: "b"}, + vars={state_path: {"count": "cN"}}, + include_state_root=True, + ) + + for segment in ("cN", "count_rx_state_", "count"): + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", f"b.{segment}"]) + assert result.exit_code == 0, result.output + output_data = json.loads(result.output) + assert [entry["kind"] for entry in output_data] == ["state", "var"] + assert output_data[1]["class"] == "VarLookupState" + assert output_data[1]["var"] == "count" + assert output_data[1]["var_id"] == "cN" + assert output_data[1]["full_path"] == f"{state_path}.count" + + text = cli_runner.invoke(cli, ["minify", "lookup", "b.cN"]) + assert text.exit_code == 0, text.output + assert text.output == ( + f"{__name__}.VarLookupState\n{__name__}.VarLookupState.count\n" + ) + + +def test_list_reports_var_ids(temp_minify_json, cli_runner): + """The state tree shows each var with the id it is minified to.""" + from reflex.reflex import cli + + class VarListState(State): + count: int = 0 + label: str = "" + + state_path = get_state_full_path(VarListState) + install_config( + states={state_path: "b"}, + vars={state_path: {"count": "c"}}, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "list", "--json"]) + assert result.exit_code == 0, result.output + tree = json.loads(result.output) + node = next(s for s in tree["substates"] if s["full_path"] == state_path) + assert node["vars"] == [ + {"name": "count", "var_id": "c"}, + {"name": "label", "var_id": None}, + ] + + text = cli_runner.invoke(cli, ["minify", "list"]) + assert text.exit_code == 0, text.output + # The states of other test modules share the tree, so compare this state's + # rows past the two columns its siblings decide. + rows = [line[8:] for line in text.output.splitlines()] + start = rows.index('VarListState -> "b"') + assert rows[start : start + 6] == [ + 'VarListState -> "b"', + "|-- Event Handlers:", + "| `-- setvar", + "`-- Vars:", + ' |-- count -> "c"', + " `-- label", + ] + + +def test_minify_help_shows_no_docstring_sections(cli_runner): + """The group's help is its summary, not its Google-style docstring.""" + from reflex.reflex import cli + + result = cli_runner.invoke(cli, ["minify", "--help"]) + + assert result.exit_code == 0, result.output + assert result.output == dedent("""\ + Usage: cli minify [OPTIONS] COMMAND [ARGS]... + + Manage state, event and var name minification. + + Options: + --help Show this message and exit. + + Commands: + init Initialize minify.json with IDs for all states, events and vars. + list Print the state tree with IDs and minified names. + lookup Lookup a state, event handler or var by its minified path... + sync Synchronize minify.json with the current codebase. + validate Validate minify.json against the current codebase. + """) + + +def test_minify_sync_repairs_ambiguous_state_ids(temp_minify_json, cli_runner): + """``validate`` flags an id the loader now rejects; ``sync`` reassigns it.""" + from reflex.minify import ( + _load_minify_config_uncached, + generate_minify_config, + save_minify_config, + ) + from reflex.reflex import cli + + class RepairedState(State): + pass + + path = get_state_full_path(RepairedState) + # Every other state has an entry, so validate reports nothing else. + config = generate_minify_config() + config["states"][path]["id"] = "_" + save_minify_config(config) + clear_config_cache() + + validated = cli_runner.invoke(cli, ["minify", "validate"]) + assert validated.exit_code == 1 + assert _unwrapped(validated.output) == ( + f"Errors found: - state '{path}' has id '_'; a state id may not start or " + "end with '_' or contain '__', which would make two state paths read the " + "same once dots become '__'. Run 'reflex minify sync' to reassign it." + ) + + synced = cli_runner.invoke(cli, ["minify", "sync"]) + assert synced.exit_code == 0, synced.output + config = _load_minify_config_uncached() + assert config is not None + assert config["states"][path]["id"] != "_" + + +def test_lookup_shows_every_handler_sharing_an_id(temp_minify_json, cli_runner): + """An id two handlers share resolves to both, with a warning, not the first.""" + from reflex.reflex import cli + + class SharedIdState(State): + def first(self): + pass + + def second(self): + pass + + state_path = get_state_full_path(SharedIdState) + install_config( + states={state_path: "b"}, + events={state_path: {"first": "x", "second": "x"}}, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", "b.x"]) + assert result.exit_code == 0, result.output + handlers = [e["handler"] for e in json.loads(result.output) if e["kind"] == "event"] + assert handlers == ["first", "second"] + + text = cli_runner.invoke(cli, ["minify", "lookup", "b.x"]) + assert text.exit_code == 0, text.output + assert _unwrapped(text.output) == ( + f"Warning: first, second of {__name__}.SharedIdState share the id 'x', so " + "all but one are unreachable. Run 'reflex minify sync' to reassign them. " + f"{__name__}.SharedIdState {__name__}.SharedIdState.first " + f"{__name__}.SharedIdState.second" + ) + + +def test_lookup_ignores_deleted_handlers_holding_an_id(temp_minify_json, cli_runner): + """``sync`` keeps a deleted handler's id reserved; lookup must not report it.""" + from reflex.reflex import cli + + class DeletedIdState(State): + def live(self): + pass + + state_path = get_state_full_path(DeletedIdState) + install_config( + states={state_path: "b"}, + events={state_path: {"live": "x", "gone": "x", "also_gone": "y"}}, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", "b.x"]) + assert result.exit_code == 0, result.output + handlers = [e["handler"] for e in json.loads(result.output) if e["kind"] == "event"] + assert handlers == ["live"] + + assert cli_runner.invoke(cli, ["minify", "lookup", "b.y"]).exit_code == 1 + + +def test_lookup_warns_about_an_id_reserved_for_a_deleted_handler( + temp_minify_json, cli_runner +): + """A segment matching a live handler by name notes the deleted id holder.""" + from reflex.reflex import cli + + class ReservedIdState(State): + def a(self): + pass + + state_path = get_state_full_path(ReservedIdState) + install_config( + states={state_path: "b"}, + events={state_path: {"old": "a", "a": "c"}}, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "b.a"]) + assert result.exit_code == 0, result.output + assert _unwrapped(result.output) == ( + "Warning: 'a' is also the id minify.json keeps reserved for deleted old " + f"of {__name__}.ReservedIdState, which an older frontend may still send. " + f"{__name__}.ReservedIdState {__name__}.ReservedIdState.a" + ) + + +def test_lookup_shows_both_readings_of_a_name_another_handler_holds_as_id( + temp_minify_json, cli_runner +): + """A segment naming one member and holding another's id resolves to both.""" + from reflex.reflex import cli + + class NameIdState(State): + count: int = 0 + total: int = 0 + + def foo(self): + pass + + def bar(self): + pass + + def own(self): + pass + + state_path = get_state_full_path(NameIdState) + install_config( + states={state_path: "b"}, + events={state_path: {"foo": "a", "bar": "foo", "own": "own"}}, + vars={state_path: {"count": "a", "total": "count"}}, + include_state_root=True, + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", "b.foo"]) + assert result.exit_code == 0, result.output + assert [ + (e["handler"], e["event_id"]) + for e in json.loads(result.output) + if e["kind"] == "event" + ] == [("bar", "foo"), ("foo", "a")] + + text = cli_runner.invoke(cli, ["minify", "lookup", "b.foo"]) + assert text.exit_code == 0, text.output + assert _unwrapped(text.output) == ( + f"Warning: Segment 'foo' is the id of bar of {__name__}.NameIdState with " + "REFLEX_MINIFY_EVENTS on, but names foo with it off; showing both. " + f"{__name__}.NameIdState {__name__}.NameIdState.bar " + f"{__name__}.NameIdState.foo" + ) + + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", "b.count"]) + assert result.exit_code == 0, result.output + assert [ + (e["var"], e["var_id"]) for e in json.loads(result.output) if e["kind"] == "var" + ] == [("total", "count"), ("count", "a")] + + text = cli_runner.invoke(cli, ["minify", "lookup", "b.count"]) + assert text.exit_code == 0, text.output + assert _unwrapped(text.output) == ( + f"Warning: Segment 'count' is the id of total of {__name__}.NameIdState " + "with REFLEX_MINIFY_VARS on, but names count with it off; showing both. " + f"{__name__}.NameIdState {__name__}.NameIdState.total " + f"{__name__}.NameIdState.count" + ) + + # A handler holding its own name as id is a single reading. + result = cli_runner.invoke(cli, ["minify", "lookup", "--json", "b.own"]) + assert result.exit_code == 0, result.output + assert [e.get("handler") for e in json.loads(result.output)] == [None, "own"] + + @pytest.fixture def init_in_reflex_dir(tmp_path, monkeypatch: pytest.MonkeyPatch) -> list[str]: """Run `_init` from a directory named `reflex` with the setup steps stubbed. diff --git a/tests/units/test_state.py b/tests/units/test_state.py index 4a516554069..420b9ec59dc 100644 --- a/tests/units/test_state.py +++ b/tests/units/test_state.py @@ -2,6 +2,7 @@ import asyncio import contextlib +import contextvars import copy import dataclasses import datetime @@ -33,6 +34,7 @@ from plotly.graph_objects import Figure from pytest_mock import MockerFixture from reflex_base import constants +from reflex_base.compiler.templates import _render_hooks from reflex_base.constants import CompileVars, RouteVar from reflex_base.constants.state import FIELD_MARKER from reflex_base.event import Event, EventHandler, EventSpec @@ -57,6 +59,7 @@ from typing_extensions import TypeAliasType import reflex as rx +import reflex.istate.dynamic from reflex.app import App from reflex.environment import environment from reflex.istate.data import ( @@ -74,6 +77,7 @@ from reflex.istate.manager.redis import StateManagerRedis from reflex.istate.manager.token import BaseStateToken from reflex.istate.proxy import MutableProxy, StateProxy +from reflex.minify import StateEntry, get_state_full_path from reflex.state import ( BaseState, ImmutableStateError, @@ -92,6 +96,11 @@ ) from reflex.testing import chdir from reflex.utils import prerequisites +from tests.units.minify_helpers import ( + install_config, + resolved_event_id, + set_minify_modes, +) from tests.units.mock_redis import mock_redis from .states import GenState @@ -1498,9 +1507,7 @@ def rendered_var(self) -> str: return self.t2 for name in ("flag", "t1", "t2"): - assert MainState._var_dependencies[name] == { - (MainState.get_full_name(), "rendered_var") - } + assert MainState._var_dependencies[name] == {(MainState, "rendered_var")} assert MainState.computed_vars["rendered_var"]._deps(objclass=MainState) == { MainState.get_full_name(): {"flag", "t1", "t2"} } @@ -2192,10 +2199,7 @@ def __get__(self, instance: Any, owner: Any = None) -> Any: assert isinstance(HandlerState.handler, EventHandler) s = HandlerState() - assert ( - HandlerState.get_full_name(), - "cached_x_side_effect", - ) in s._var_dependencies["x"] + assert (HandlerState, "cached_x_side_effect") in s._var_dependencies["x"] assert s.cached_x_side_effect == 1 assert s.x == 43 s.handler() @@ -2286,14 +2290,14 @@ def comp_z(self) -> list[bool]: cs = ComputedState() assert cs._var_dependencies["v"] == { - (ComputedState.get_full_name(), "comp_v"), - (ComputedState.get_full_name(), "comp_v_backend"), - (ComputedState.get_full_name(), "comp_v_via_property"), + (ComputedState, "comp_v"), + (ComputedState, "comp_v_backend"), + (ComputedState, "comp_v_via_property"), } - assert cs._var_dependencies["w"] == {(ComputedState.get_full_name(), "comp_w")} - assert cs._var_dependencies["x"] == {(ComputedState.get_full_name(), "comp_x")} - assert cs._var_dependencies["y"] == {(ComputedState.get_full_name(), "comp_y")} - assert cs._var_dependencies["_z"] == {(ComputedState.get_full_name(), "comp_z")} + assert cs._var_dependencies["w"] == {(ComputedState, "comp_w")} + assert cs._var_dependencies["x"] == {(ComputedState, "comp_x")} + assert cs._var_dependencies["y"] == {(ComputedState, "comp_y")} + assert cs._var_dependencies["_z"] == {(ComputedState, "comp_z")} def test_backend_method(): @@ -4410,9 +4414,7 @@ def foo(self) -> str: RouterVarDepState.get_full_name(): set(constants.ROUTER_VARS) } for router_var in constants.ROUTER_VARS: - assert (RouterVarDepState.get_full_name(), "foo") in State._var_dependencies[ - router_var - ] + assert (RouterVarDepState, "foo") in State._var_dependencies[router_var] # Get state from state manager. rx_state = await state_manager.get_state(BaseStateToken(ident=token, cls=State)) @@ -4435,9 +4437,9 @@ def foo(self) -> str: # entry and raises on the missing substate. Drop them. for dep_set in State._var_dependencies.values(): dep_set.difference_update({ - (RouterVarDepState.get_full_name(), "foo"), + (RouterVarDepState, "foo"), }) - State._potentially_dirty_states.discard(RouterVarDepState.get_full_name()) + State._potentially_dirty_states.discard(RouterVarDepState) @pytest.mark.parametrize("name", constants.ROUTER_VARS) @@ -4470,17 +4472,14 @@ def foo(self) -> str: return self.router.url.path for router_var in constants.ROUTER_VARS: - assert ( - LegacyRouterDepState.get_full_name(), - "foo", - ) in State._var_dependencies[router_var] + assert (LegacyRouterDepState, "foo") in State._var_dependencies[router_var] assert "router" not in State._var_dependencies # Drop the class-level registrations this locally-defined state made; see # the note in test_router_var_dep. for dep_set in State._var_dependencies.values(): - dep_set.discard((LegacyRouterDepState.get_full_name(), "foo")) - State._potentially_dirty_states.discard(LegacyRouterDepState.get_full_name()) + dep_set.discard((LegacyRouterDepState, "foo")) + State._potentially_dirty_states.discard(LegacyRouterDepState) def test_router_var_dep_legacy_string_still_compiles() -> None: @@ -4503,8 +4502,8 @@ def foo(self) -> str: App()._validate_var_dependencies() for dep_set in State._var_dependencies.values(): - dep_set.discard((LegacyRouterCompileState.get_full_name(), "foo")) - State._potentially_dirty_states.discard(LegacyRouterCompileState.get_full_name()) + dep_set.discard((LegacyRouterCompileState, "foo")) + State._potentially_dirty_states.discard(LegacyRouterCompileState) @pytest.mark.asyncio @@ -4567,10 +4566,10 @@ def from_string(self) -> str: assert "StringFormRouterDepState.from_string" in deprecations[0] for dep_set in State._var_dependencies.values(): - dep_set.discard((VarFormRouterDepState.get_full_name(), "from_var")) - dep_set.discard((StringFormRouterDepState.get_full_name(), "from_string")) - State._potentially_dirty_states.discard(VarFormRouterDepState.get_full_name()) - State._potentially_dirty_states.discard(StringFormRouterDepState.get_full_name()) + dep_set.discard((VarFormRouterDepState, "from_var")) + dep_set.discard((StringFormRouterDepState, "from_string")) + State._potentially_dirty_states.discard(VarFormRouterDepState) + State._potentially_dirty_states.discard(StringFormRouterDepState) def test_router_var_dep_whole_router() -> None: @@ -4596,21 +4595,17 @@ def summary(self) -> str: State.get_full_name(): {constants.ROUTER, *constants.ROUTER_VARS} } for router_var in constants.ROUTER_VARS: - assert ( - WholeRouterDepState.get_full_name(), - "summary", - ) in State._var_dependencies[router_var] + assert (WholeRouterDepState, "summary") in State._var_dependencies[router_var] # `router` has no backing field, so nothing may be registered against it -- # it would never be dirtied and the dependent var would go stale. - assert ( - WholeRouterDepState.get_full_name(), - "summary", - ) not in State._var_dependencies.get(constants.ROUTER, set()) + assert (WholeRouterDepState, "summary") not in State._var_dependencies.get( + constants.ROUTER, set() + ) # Drop the class-level registrations; see the note in test_router_var_dep. for dep_set in State._var_dependencies.values(): - dep_set.discard((WholeRouterDepState.get_full_name(), "summary")) - State._potentially_dirty_states.discard(WholeRouterDepState.get_full_name()) + dep_set.discard((WholeRouterDepState, "summary")) + State._potentially_dirty_states.discard(WholeRouterDepState) def test_router_is_listed_as_a_var_and_inherited_by_substates() -> None: @@ -6037,6 +6032,29 @@ def _frontend_schema_with(*patches: contextlib.AbstractContextManager) -> str: _frontend_schema.cache_clear() +def test_frontend_schema_ignores_the_name_resolver(temp_minify_json, monkeypatch): + """Renamed states, vars and handlers keep the hash; the scheme digest covers them. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + schema = _frontend_schema_with() + root_path = get_state_full_path(FrontendSchemaRoot) + child_path = get_state_full_path(FrontendSchemaChild) + set_minify_modes(monkeypatch, states=True, events=True, vars=True) + install_config( + states={ + root_path: StateEntry(id="r", parent=None), + child_path: StateEntry(id="c", parent=root_path), + }, + events={child_path: {"bump": "b"}}, + vars={child_path: {"name": "n", "shout": "s"}}, + ) + assert FrontendSchemaChild.get_full_name() == "r.c" + assert _frontend_schema_with() == schema + + def test_frontend_schema_ignores_defaults(): """A changed default keeps the compiled frontend compatible.""" schema = FrontendSchemaRoot._to_frontend_schema() @@ -6100,10 +6118,14 @@ def renamed(mapping: Mapping[str, Any], old: str, new: str): patches: list[contextlib.AbstractContextManager] = { "state_renamed": [ - patch.object(child, "get_full_name", classmethod(lambda cls: "renamed")) + patch.object( + child, "_get_default_full_name", classmethod(lambda cls: "renamed") + ) ], "state_moved": [ - patch.object(child, "get_full_name", classmethod(lambda cls: "other.child")) + patch.object( + child, "_get_default_full_name", classmethod(lambda cls: "other.child") + ) ], "var_added": [ patch.dict(child.base_vars, {"extra": child.base_vars["name"]}), @@ -6154,7 +6176,9 @@ def _forget_state(state: type[BaseState]) -> None: assert parent is not None context.get_substates(parent).discard(state) context.base_states.pop(state.get_full_name(), None) - all_base_state_classes.pop(state.get_full_name(), None) + default_name = state._get_default_full_name() + context._states_by_default_name.pop(default_name, None) + all_base_state_classes.pop(default_name, None) _frontend_schema.cache_clear() @@ -6965,7 +6989,7 @@ def doubled(self) -> int: # A computed var depending on the descriptor must register the dependency. deps = DescriptorState._var_dependencies.get("_desc_value", set()) - assert (DescriptorState.get_full_name(), "doubled") in deps + assert (DescriptorState, "doubled") in deps def test_descriptor_overrides_inherited_descriptor(): @@ -7006,8 +7030,8 @@ def child_view(self) -> int: # Child's computed var depends on child's _shared, parent's stays at parent. child_deps = ChildDescState._var_dependencies.get("_shared", set()) parent_deps = ParentDescState._var_dependencies.get("_shared", set()) - assert (ChildDescState.get_full_name(), "child_view") in child_deps - assert (ParentDescState.get_full_name(), "parent_view") in parent_deps + assert (ChildDescState, "child_view") in child_deps + assert (ParentDescState, "parent_view") in parent_deps class OnLoadCancelState(State): @@ -7155,6 +7179,497 @@ def test_setattr_alias_annotated_var(mocker: MockerFixture): error_mock.assert_called_once() +class KeyedCounter(rx.ComponentState): + """A component state used to check how its instances are named.""" + + count: int = 0 + + @classmethod + def get_component(cls, **props) -> rx.Component: + """Render the counter. + + Args: + props: The component props. + + Returns: + The component. + """ + return rx.text(cls.count, **props) + + +def test_component_state_key_names_the_state(): + """``_state_key`` names the instance; an unkeyed one is numbered.""" + keyed = KeyedCounter.create(_state_key="cart").State + unkeyed = KeyedCounter.create().State + + assert keyed is not None + assert unkeyed is not None + assert keyed.__name__ == "KeyedCounter__cart" + assert unkeyed.__name__.startswith("KeyedCounter_n") + + +def test_component_state_key_is_independent_of_creation_order(): + """A keyed name is fixed; unkeyed names follow the order they are made in.""" + keyed = KeyedCounter.create(_state_key="stable").State + unkeyed_before = KeyedCounter.create().State + KeyedCounter.create(_state_key="stable2") + unkeyed_after = KeyedCounter.create().State + + assert keyed is not None + assert unkeyed_before is not None + assert unkeyed_after is not None + assert keyed.__name__ == "KeyedCounter__stable" + assert unkeyed_before.__name__ != unkeyed_after.__name__ + + +class KeyedFirstCounter(rx.ComponentState): + """Names a keyed instance before an unkeyed one.""" + + count: int = 0 + + @classmethod + def get_component(cls, **props) -> rx.Component: + """Render the counter. + + Args: + props: The component props. + + Returns: + The component. + """ + return rx.text(cls.count, **props) + + +class UnkeyedFirstCounter(rx.ComponentState): + """Names an unkeyed instance before a keyed one.""" + + count: int = 0 + + @classmethod + def get_component(cls, **props) -> rx.Component: + """Render the counter. + + Args: + props: The component props. + + Returns: + The component. + """ + return rx.text(cls.count, **props) + + +def test_keyed_name_does_not_collide_when_keyed_is_created_first(): + """A key shaped like a generated name stays in its own namespace.""" + keyed = KeyedFirstCounter.create(_state_key="n1").State + unkeyed = KeyedFirstCounter.create().State + + assert keyed is not None + assert unkeyed is not None + assert keyed.__name__ == "KeyedFirstCounter__n1" + assert unkeyed.__name__ == "KeyedFirstCounter_n1" + + +def test_keyed_name_does_not_collide_when_unkeyed_is_created_first(): + """The namespaces stay separate whichever instance is created first.""" + unkeyed = UnkeyedFirstCounter.create().State + keyed = UnkeyedFirstCounter.create(_state_key="n1").State + + assert keyed is not None + assert unkeyed is not None + assert unkeyed.__name__ == "UnkeyedFirstCounter_n1" + assert keyed.__name__ == "UnkeyedFirstCounter__n1" + + +@pytest.mark.parametrize( + "bad_key", + ["not an identifier", "1leading_digit", "", 1, ("a",), "cartItem", "Cart"], + ids=["spaces", "digit", "empty", "int", "tuple", "camel", "capitalized"], +) +def test_component_state_rejects_an_unusable_key(bad_key): + """A key becomes part of a class name, so it has to be an identifier string. + + The default state name is snake-cased, so ``cartItem`` would name the same + state as ``cart_item``. + + Args: + bad_key: A key that cannot name a class. + """ + with pytest.raises(ValueError) as error: + KeyedCounter.create(_state_key=bad_key) + assert str(error.value) == ( + "_state_key must be a string that is a valid Python identifier without " + f"uppercase letters, got {bad_key!r}." + ) + + +def test_component_state_rejects_a_duplicate_key(): + """A key is unique among the instances of a component.""" + KeyedCounter.create(_state_key="only_once") + + with pytest.raises(StateValueError) as error: + KeyedCounter.create(_state_key="only_once") + assert str(error.value) == ( + f"_state_key='only_once' is already used by a {__name__}.KeyedCounter " + "instance. A key names one state per component: create the component " + "once and reuse that instance on every page that renders it, or pick " + "another key." + ) + + +@pytest.mark.parametrize( + ("mode", "expect_minified"), + [(None, False), (True, True), (False, False)], +) +def test_state_name_resolution(temp_minify_json, monkeypatch, mode, expect_minified): + """Minified name only when env is ENABLED and config has the entry.""" + if mode is not None: + set_minify_modes(monkeypatch, states=mode) + + class TestState(BaseState): + pass + + if mode is not None: + install_config(states={get_state_full_path(TestState): "f"}) + + name = TestState.get_name() + assert (name == "f") is expect_minified + if not expect_minified: + assert "test_state" in name.lower() + + +@pytest.mark.parametrize( + ("mode", "expected"), + [(None, "my_handler"), (True, "d"), (False, "my_handler")], +) +def test_event_name_resolution(temp_minify_json, monkeypatch, mode, expected): + """Minified handler name only when env is ENABLED and config has the entry.""" + import reflex as rx + from reflex.utils.format import get_event_handler_parts + + class EventNameState(State): + @rx.event + def my_handler(self): + pass + + if mode is not None: + set_minify_modes(monkeypatch, events=mode) + install_config( + events={get_state_full_path(EventNameState): {"my_handler": "d"}} + ) + + _, name = get_event_handler_parts(EventNameState.event_handlers["my_handler"]) + assert name == expected + + +def test_setvar_registered_with_config(temp_minify_json, monkeypatch): + """Test that ``setvar`` is resolvable to its minified name.""" + set_minify_modes(monkeypatch, events=True) + state_path = f"{__name__}.State.TestStateWithSetvar" + install_config( + states={state_path: "b"}, + events={state_path: {"setvar": "s"}}, + include_state_root=True, + ) + + class TestStateWithSetvar(State): + pass + + assert resolved_event_id(TestStateWithSetvar, "setvar") == "s" + + +def test_auto_setter_registered_with_config(temp_minify_json, monkeypatch): + """Test that auto-setters (set_*) are resolvable to their minified name.""" + set_minify_modes(monkeypatch, events=True) + # `_init_var` reads the flag when the class is created; it defaults to off. + monkeypatch.setattr("reflex.state.get_state_auto_setters", lambda: True) + state_path = f"{__name__}.State.TestStateWithAutoSetter" + install_config( + states={state_path: "b"}, + events={state_path: {"set_count": "c", "setvar": "v"}}, + include_state_root=True, + ) + + class TestStateWithAutoSetter(State): + count: int = 0 + + assert "set_count" in TestStateWithAutoSetter.event_handlers + assert resolved_event_id(TestStateWithAutoSetter, "set_count") == "c" + + +def test_dynamic_handlers_not_registered_without_config(temp_minify_json): + """Test that dynamic handlers have no resolved minified name without config.""" + + class TestStateNoConfig(State): + count: int = 0 + + for handler_name in TestStateNoConfig.event_handlers: + assert resolved_event_id(TestStateNoConfig, handler_name) is None + + +def test_add_event_handler_registered_with_config(temp_minify_json, monkeypatch): + """Test that dynamically added event handlers via _add_event_handler are registered.""" + import reflex as rx + + set_minify_modes(monkeypatch, events=True) + state_path = f"{__name__}.State.TestStateWithDynamicHandler" + install_config( + states={state_path: "b"}, + events={state_path: {"dynamic_handler": "d", "setvar": "v"}}, + include_state_root=True, + ) + + class TestStateWithDynamicHandler(State): + pass + + @rx.event + def dynamic_handler(self): + pass + + TestStateWithDynamicHandler._add_event_handler("dynamic_handler", dynamic_handler) + + assert resolved_event_id(TestStateWithDynamicHandler, "dynamic_handler") == "d" + + +def test_component_state_picks_up_minified_name(temp_minify_json, monkeypatch): + """``ComponentState.create()`` instances are real state classes too. + + They register via ``__init_subclass__`` like any other state, so as + long as the minify resolver is installed before ``create()`` runs, + the resulting class gets the minified name from ``minify.json``. + """ + import reflex as rx + + set_minify_modes(monkeypatch, states=True, events=True) + # ComponentState.create() names the class it builds with a ``_n`` + # suffix and keys it under the component's module, so the path under + # which the resolver will look it up is fully determined ahead of time. + instance_count = rx.ComponentState._per_component_state_instance_count + 1 + instance_path = f"{__name__}.State.ComponentStateMinifyExample_n{instance_count}" + install_config( + states={instance_path: "z"}, + events={instance_path: {"increment": "i", "setvar": "s"}}, + include_state_root=True, + ) + + class ComponentStateMinifyExample(rx.ComponentState): + count: int = 0 + + @rx.event + def increment(self): + self.count += 1 + + @classmethod + def get_component(cls, **props): + return rx.fragment() + + ComponentStateMinifyExample.create() + instance_cls = next( + cls + for cls in RegistrationContext.get().base_states.values() + if cls.__name__ == f"ComponentStateMinifyExample_n{instance_count}" + ) + + assert instance_cls.get_name() == "z" + assert resolved_event_id(instance_cls, "increment") == "i" + + +def test_state_created_after_resolver_install_uses_minified_name( + temp_minify_json, monkeypatch +): + """A state class created after the resolver is installed gets its + minified name at registration time — no later refresh needed. + + This is the path exercised by ``ComponentState.create()`` and any + locally-defined state inside a page function: the class doesn't + exist when ``minify.json`` is loaded, so the resolver must be + consulted lazily on first lookup. + """ + set_minify_modes(monkeypatch, states=True) + late_path = f"{__name__}.State.LateBornState" + install_config(states={late_path: "lb"}) + + class LateBornState(State): + pass + + ctx = RegistrationContext.get() + assert LateBornState.get_name() == "lb" + # State stays un-minified, so the parent prefix is its default snake form. + assert ctx.base_states.get(f"{State.get_full_name()}.lb") is LateBornState + + +def test_add_event_handler_registered_after_resolver_swap( + temp_minify_json, monkeypatch +): + """Handlers added to a state that predates the resolver swap still register. + + Swapping the resolver renames already-registered states, so the + "is this class registered?" check must not depend on the resolved name. + """ + import reflex as rx + + set_minify_modes(monkeypatch, states=True, events=True) + + # The class exists *before* the config is installed. + class PreSwapState(State): + pass + + state_path = get_state_full_path(PreSwapState) + install_config( + states={state_path: "b"}, + events={state_path: {"post_swap_handler": "d"}}, + include_state_root=True, + ) + assert PreSwapState.get_name() == "b" + + @rx.event + def post_swap_handler(self): + pass + + PreSwapState._add_event_handler("post_swap_handler", post_swap_handler) + + handlers = RegistrationContext.get().event_handlers + assert f"{PreSwapState.get_full_name()}.d" in handlers + + +def test_get_class_substate_with_parent_child_name_collision( + temp_minify_json, monkeypatch +): + """Test that get_class_substate resolves correctly when parent and child + share the same minified name (IDs are only sibling-unique). + """ + set_minify_modes(monkeypatch, states=True) + + # Build State -> ParentClassSubstateCollision -> ChildClassSubstateCollision + # where both children minify to "b". Class names are deliberately unique + # so ``_handle_local_def`` doesn't append a numeric suffix. + class ParentClassSubstateCollision(State): + pass + + class ChildClassSubstateCollision(ParentClassSubstateCollision): + pass + + install_config( + states={ + get_state_full_path(ParentClassSubstateCollision): "b", + get_state_full_path(ChildClassSubstateCollision): "b", + } + ) + + assert ParentClassSubstateCollision.get_name() == "b" + assert ChildClassSubstateCollision.get_name() == "b" + + state_prefix = State.get_full_name() + assert ChildClassSubstateCollision.get_full_name() == f"{state_prefix}.b.b" + + resolved = State.get_class_substate(f"{state_prefix}.b.b") + assert resolved is ChildClassSubstateCollision + + +def test_get_substate_with_parent_child_name_collision(temp_minify_json, monkeypatch): + """Test that get_substate (instance method) resolves correctly when parent + and child share the same minified name. + """ + import reflex as rx + + set_minify_modes(monkeypatch, states=True) + + class ParentInstanceSubstateCollision(State): + pass + + class ChildInstanceSubstateCollision(ParentInstanceSubstateCollision): + @rx.event + def my_handler(self): + pass + + install_config( + states={ + get_state_full_path(ParentInstanceSubstateCollision): "b", + get_state_full_path(ChildInstanceSubstateCollision): "b", + } + ) + + root = State(_reflex_internal_init=True) # type: ignore[call-arg] + + resolved = root.get_substate([State.get_name(), "b", "b"]) + assert type(resolved) is ChildInstanceSubstateCollision + + +def test_first_substate_is_not_mistaken_for_its_parent(temp_minify_json, monkeypatch): + """A minified substate resolves to itself, not to the state it hangs off. + + The parent resolves the always-dirty child by class, so its minified name + never has to match one recorded when the child was created. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + set_minify_modes(monkeypatch, states=True) + parent_path = f"{__name__}.State.DirtyParent" + install_config( + states={parent_path: "b", f"{parent_path}.DirtyChild": "c"}, + ) + + class DirtyParent(State): + pass + + class DirtyChild(DirtyParent): + @rx.var(cache=False) + def always_dirty(self) -> int: + return 1 + + try: + assert DirtyParent.get_name() == "b" + assert DirtyChild.get_name() == "c" + assert DirtyParent.get_class_substate(DirtyChild.get_name()) is DirtyChild + assert DirtyChild in DirtyParent._get_potentially_dirty_states() + finally: + # The forked registry forgets these states; State's class-level set + # outlives the test and would send later deltas looking for them. + State._always_dirty_substates.discard(DirtyParent) + + +def test_resolved_name_survives_cache_pressure(temp_minify_json, monkeypatch): + """A resolved name is never recomputed from a scope with no context. + + The name caches are keyed only by the class but the resolver lives in a + ContextVar, so a bounded cache would evict a name and let any later caller + without a registration context pin the default name process-wide. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + set_minify_modes(monkeypatch, states=True) + parent_path = f"{__name__}.State.PressureState" + install_config(states={parent_path: "b"}) + + class PressureState(State): + pass + + assert PressureState.get_name() == "b" + + # More distinct classes than a default lru_cache would hold. + fillers = [ + type(f"Filler{index}", (BaseState,), {"__module__": __name__}) + for index in range(150) + ] + try: + for filler in fillers: + filler.get_name() + + def resolve_without_context() -> tuple[RegistrationContext | None, str]: + return RegistrationContext.try_get(), PressureState.get_name() + + ctx, name = contextvars.Context().run(resolve_without_context) + assert ctx is None + assert name == "b", "name was re-resolved without a registration context" + assert PressureState.get_name() == "b" + finally: + State.get_name.cache_clear() + State.get_full_name.cache_clear() + + def test_setattr_unannotated_empty_containers(mocker: MockerFixture) -> None: """Empty unannotated containers accept values when assigned later.""" @@ -7397,11 +7912,10 @@ def combined(self) -> str: # dependency maps, which outlive this test. Left behind, a later test that # dirties a_field or b_field resolves the stale entry and raises on the # missing substate. Drop them. - consumer_name = _CompositeDepConsumer.get_full_name() for state_cls in (_CompositeDepStateA, _CompositeDepStateB): for dep_set in state_cls._var_dependencies.values(): - dep_set.difference_update({(consumer_name, "combined")}) - state_cls._potentially_dirty_states.discard(consumer_name) + dep_set.difference_update({(_CompositeDepConsumer, "combined")}) + state_cls._potentially_dirty_states.discard(_CompositeDepConsumer) def test_getstate_holds_only_the_fields_of_the_state(): @@ -7555,6 +8069,382 @@ class ClashingState(BaseState): _backend_vars: dict = {} # pyright: ignore[reportIncompatibleVariableOverride] +def test_default_full_name_ignores_the_resolver(temp_minify_json, monkeypatch): + """The name persisted keys use stays put when the state is renamed. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class DefaultNamedRoot(BaseState): + pass + + class DefaultNamedChild(DefaultNamedRoot): + pass + + default_name = DefaultNamedChild._get_default_full_name() + assert default_name == DefaultNamedChild.get_full_name() + + root_path = get_state_full_path(DefaultNamedRoot) + set_minify_modes(monkeypatch, states=True) + install_config( + states={ + root_path: StateEntry(id="r", parent=None), + get_state_full_path(DefaultNamedChild): StateEntry( + id="c", parent=root_path + ), + } + ) + assert DefaultNamedChild.get_full_name() == "r.c" + assert DefaultNamedChild._get_default_full_name() == default_name + + +def test_minified_vars_reach_the_wire(temp_minify_json, monkeypatch): + """Vars with an id go by it in Var expressions, deltas and full state dicts. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + set_minify_modes(monkeypatch, states=True, vars=True) + root_path = f"{__name__}.WireVarRoot" + path = f"{root_path}.WireVarState" + install_config( + states={ + root_path: StateEntry(id="r", parent=None), + path: StateEntry(id="w", parent=root_path), + }, + vars={path: {"count": "c", "doubled": "d"}}, + ) + + class WireVarRoot(BaseState): + pass + + class WireVarState(WireVarRoot): + count: int = 0 + label: str = "" + + @rx.var + def doubled(self) -> int: + return self.count * 2 + + assert str(WireVarState.count) == "$rx_r__w.c" + assert str(WireVarState.doubled) == "$rx_r__w.d" + # A var without an id keeps its key, marker and all. + assert str(WireVarState.label) == f"$rx_r__w.label{FIELD_MARKER}" + assert WireVarState._var_names_by_key == { + "c": "count", + "d": "doubled", + f"label{FIELD_MARKER}": "label", + } + + state = WireVarState(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] + assert state.dict() == {"r.w": {"c": 0, "d": 0, f"label{FIELD_MARKER}": ""}} + state.count = 2 + assert state.get_delta() == {"r.w": {"c": 2, "d": 4}} + + +class ProxiedWireVarState(BaseState): + """A root state whose var minifies, read through a background task's proxy.""" + + count: int = 0 + + +@pytest.mark.asyncio +async def test_minified_vars_reach_the_wire_through_a_state_proxy( + temp_minify_json: Path, + monkeypatch: pytest.MonkeyPatch, + token: str, + state_manager: StateManager, + attached_mock_event_context: EventContext, +): + """A background task's StateProxy keys its dict and delta by the wrapped state. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + token: The client token. + state_manager: The state manager to exercise. + attached_mock_event_context: The attached event context. + """ + set_minify_modes(monkeypatch, states=True, vars=True) + path = get_state_full_path(ProxiedWireVarState) + install_config( + states={path: StateEntry(id="p", parent=None)}, + vars={path: {"count": "c"}}, + ) + state_token = BaseStateToken(ident=token, cls=ProxiedWireVarState) + async with state_manager.modify_state(state_token) as root: + proxy = StateProxy(root) + + async with proxy: + proxy.count = 2 + assert proxy.get_delta() == {"p": {"c": 2}} + expected = proxy.__wrapped__.dict() + assert expected["p"]["c"] == 2 + assert proxy.dict() == expected + + +def test_minified_names_rebuilt_for_existing_states(temp_minify_json, monkeypatch): + """Installing a config after the states exist renames what they send. + + Dependency tracking and always-dirty substates are keyed by class, so they + keep working under the new names. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class LateRoot(BaseState): + count: int = 0 + + class LateChild(LateRoot): + @rx.var + def doubled(self) -> int: + return self.count * 2 + + @rx.var(cache=False) + def mirrored(self) -> int: + return self.count + + root_path = get_state_full_path(LateRoot) + child_path = get_state_full_path(LateChild) + set_minify_modes(monkeypatch, states=True, vars=True) + install_config( + states={ + root_path: StateEntry(id="r", parent=None), + child_path: StateEntry(id="k", parent=root_path), + }, + vars={root_path: {"count": "c"}, child_path: {"doubled": "d", "mirrored": "m"}}, + ) + + assert str(LateRoot.count) == "$rx_r.c" + assert str(LateChild.doubled) == "$rx_r__k.d" + assert LateChild.vars["count"] is LateRoot.base_vars["count"] + + root = LateRoot(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] + root.count = 3 + assert root.get_delta() == {"r": {"c": 3}, "r.k": {"d": 6, "m": 3}} + + +def test_inherited_var_goes_by_its_owners_key(temp_minify_json, monkeypatch): + """A var is keyed where it is declared, however it is reached. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + class KeyOwner(BaseState): + count: int = 0 + + class KeyHeir(KeyOwner): + pass + + set_minify_modes(monkeypatch, vars=True) + install_config(vars={get_state_full_path(KeyOwner): {"count": "c"}}) + + assert str(KeyHeir.count) == str(KeyOwner.count) + assert str(KeyOwner.count).endswith(".c") + assert "c" not in KeyHeir._var_names_by_key + + +@pytest.mark.asyncio +async def test_update_vars_internal_resolves_wire_keys(temp_minify_json, monkeypatch): + """Client storage values sent under minified keys land on their vars. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + from reflex.state import UpdateVarsInternalState + + set_minify_modes(monkeypatch, states=True, vars=True) + path = f"{__name__}.State.StorageKeyState" + install_config( + states={path: StateEntry(id="s", parent="reflex.state.State")}, + vars={path: {"token": "t", "plain": "p"}}, + include_state_root=True, + ) + + class StorageKeyState(State): + token: str = rx.Cookie("") + plain: str = "" + + storage = StorageKeyState(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] + + class _Caller: + async def get_state(self, state_cls: type[BaseState]) -> BaseState: + assert state_cls is StorageKeyState + return storage + + await UpdateVarsInternalState.update_vars_internal.fn( + _Caller(), {"a.s.t": "tok", "a.s.p": "not storage", "a.s.gone": "x"} + ) + assert storage.token == "tok" + # Only client storage vars are writable this way. + assert storage.plain == "" + + +def test_minified_state_reads_its_context_into_a_prefixed_local( + temp_minify_json, monkeypatch +): + """A short minified name must not become a bare local JS variable. + + Components read a state's context into a local named after the state, so + ``const a = ...`` would clash with any ``a`` a user hook declares or reads. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + set_minify_modes(monkeypatch, states=True) + path = f"{__name__}.State.LocalNameState" + install_config(states={path: "b"}, include_state_root=True) + + class LocalNameState(State): + count: int = 0 + + @rx.var + def doubled(self) -> int: + return self.count * 2 + + for var, key in ( + (LocalNameState.base_vars["count"], "count"), + (LocalNameState.doubled, "doubled"), + ): + assert str(var) == f"$rx_a__b.{key}{FIELD_MARKER}" + var_data = var._get_all_var_data() + assert var_data is not None + assert var_data.hooks == ( + f'const $rx_a__b = useStateContext("a.b", ["{key}{FIELD_MARKER}"])', + ) + + # A component reading both declares the prefixed local once. + hooks = rx.text(LocalNameState.count, LocalNameState.doubled)._get_all_hooks() + fields = f'["count{FIELD_MARKER}", "doubled{FIELD_MARKER}"]' + assert _render_hooks(hooks) == f'const $rx_a__b = useStateContext("a.b", {fields})' + + +def test_unminified_state_keeps_its_context_local(temp_minify_json): + """Built-in names cannot clash, so they are left as they are. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + """ + + class PlainLocalState(State): + count: int = 0 + + local = format.format_state_name(PlainLocalState.get_full_name()) + assert str(PlainLocalState.count) == f"{local}.count{FIELD_MARKER}" + + +def test_state_count_keys_on_names_minification_cannot_share( + temp_minify_json, monkeypatch +): + """Stateful page detection counts states by a name no resolver shortens. + + Minified full names like ``a.b`` repeat across states that never meet in one + tree, so a new state keyed by one would not grow the count. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + set_minify_modes(monkeypatch, states=True) + path = f"{__name__}.State.CountedState" + install_config(states={path: "b"}, include_state_root=True) + + class CountedState(State): + pass + + assert CountedState.get_full_name() == "a.b" + assert CountedState._get_default_full_name() in all_base_state_classes + assert "a.b" not in all_base_state_classes + + +def test_reload_retires_component_states_of_the_reloaded_module( + temp_minify_json, monkeypatch +): + """Re-running a reloaded module's ``create()`` calls builds fresh states. + + Their states live in ``reflex.istate.dynamic``, not the module, so the + reload has to recognize them by the component class they come from, or + the re-run collides with them: a keyed one by its key, an unkeyed one by + its restarted counter. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + from reflex.state import reload_state_module + + # A reload rebuilds the dependency tracking of the states it prunes. + monkeypatch.setattr(State, "_var_dependencies", dict(State._var_dependencies)) + monkeypatch.setattr( + State, "_potentially_dirty_states", set(State._potentially_dirty_states) + ) + module = "reloaded_component_module" + + def get_component(cls: Any) -> rx.Component: + return rx.text(cls.value) + + def define_and_create() -> tuple[type[State], type[State]]: + counter = type( + "ReloadCounter", + (rx.ComponentState,), + { + "__module__": module, + "__annotations__": {"value": int}, + "value": 0, + "get_component": classmethod(get_component), + }, + ) + keyed = counter.create(_state_key="cart").State + unkeyed = counter.create().State + assert keyed is not None + assert unkeyed is not None + return keyed, unkeyed + + old_keyed, old_unkeyed = define_and_create() + try: + reload_state_module(module=module) + assert old_keyed not in State.get_substates() + assert old_unkeyed not in State.get_substates() + + new_keyed, new_unkeyed = define_and_create() + assert new_keyed.get_name() == old_keyed.get_name() + assert new_unkeyed.get_name() == old_unkeyed.get_name() + assert getattr(reflex.istate.dynamic, new_keyed.__name__) is new_keyed + finally: + # Frees the generated classes' names in reflex.istate.dynamic. + reload_state_module(module=module) + assert not hasattr(reflex.istate.dynamic, old_keyed.__name__) + + +def test_reload_attributes_a_state_to_its_own_module(temp_minify_json, monkeypatch): + """A state is not claimed by the module of a locally defined parent. + + Args: + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + + def make_local_parent() -> type[State]: + class LocalReloadParent(State): + pass + + return LocalReloadParent + + parent = make_local_parent() + child = type("ModuleReloadChild", (parent,), {"__module__": "reload_module_b"}) + assert parent._get_source_module() == __name__ + assert child._get_source_module() == "reload_module_b" + + def test_setstate_drops_inherited_fields(): """The value of an inherited field lives on the parent state, not on the substate.""" @@ -8240,6 +9130,34 @@ class SharedSecond(State): assert groups == (((SharedFirst, "value", True), (SharedSecond, "value", False)),) +def test_shared_storage_groups_name_minified_vars( + clean_registration_context: RegistrationContext, temp_minify_json, monkeypatch +): + """Vars sharing a storage name are grouped by their names, not their wire keys. + + Args: + clean_registration_context: An isolated state registry. + temp_minify_json: Temporary ``minify.json`` location. + monkeypatch: The pytest monkeypatch fixture. + """ + set_minify_modes(monkeypatch, vars=True) + first_path = f"{__name__}.State.MinifiedSharedFirst" + second_path = f"{__name__}.State.MinifiedSharedSecond" + install_config(vars={first_path: {"value": "v"}, second_path: {"value": "w"}}) + + class MinifiedSharedFirst(State): + value: str = rx.LocalStorage("", name="minified_shared", sync=True) + + class MinifiedSharedSecond(State): + value: str = rx.LocalStorage("", name="minified_shared") + + assert format.format_var_key(MinifiedSharedFirst, "value") == "v" + groups = _shared_storage_groups(State, RegistrationContext.get()) + assert groups == ( + ((MinifiedSharedFirst, "value", True), (MinifiedSharedSecond, "value", False)), + ) + + @pytest.mark.parametrize("second_sync", [True, False]) async def test_shared_storage_name_keeps_event_writes( clean_registration_context: RegistrationContext, diff --git a/tests/units/test_testing.py b/tests/units/test_testing.py index 82daa3541d1..213abf93b40 100644 --- a/tests/units/test_testing.py +++ b/tests/units/test_testing.py @@ -250,12 +250,11 @@ def test_app_harness_reload_forgets_states_of_every_app_module(tmp_path, monkeyp {"__module__": "harnessapp.states"}, ) widget_state._evaluate(lambda state: rx.text("widget")) - name = widget_state.get_name() - assert name in rx.State._always_dirty_substates + assert widget_state in rx.State._always_dirty_substates AppHarness.create(root=tmp_path, app_name="harnessapp")._reload_state_module() - assert name not in rx.State._always_dirty_substates + assert widget_state not in rx.State._always_dirty_substates assert widget_state not in rx.State.get_substates()