Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

43.6 KB
Raw Blame
"""
Vite integration helper for FastAPI
Handles development and production asset loading
"""

import hashlib
import json
import threading
import time
from dataclasses import dataclass, field
from pathlib import Path, PureWindowsPath
from typing import Any, Optional
from urllib.parse import quote
from markupsafe import Markup
from loguru import logger

# Upper bound on the manifest.json bytes we are willing to read. The real
# manifest for this project is a few tens of KiB; anything vastly bigger is a
# corrupted or substituted file, and reading it whole would hand an unbounded
# allocation (and an unbounded `json.loads`) to whatever wrote it. We read one
# byte past the cap so "too large" is detected without materialising the file.
_MAX_MANIFEST_BYTES = 4 * 1024 * 1024

# Causes we have already reported, so a persistent failure logs once instead
# of once per request. Keyed by cause rather than by a single boolean: a
# second, *different* failure still gets reported instead of being swallowed
# because some earlier transient already burned the only warning. The key is
# deliberately free of the manifest fingerprint: a manifest rewritten with
# fresh garbage on every request produces new bytes but the *same* operator
# problem, and keying on the bytes would re-warn once per render. The
# fingerprint goes in the message body instead, so the one line that is
# emitted still identifies the snapshot it describes. Module-level so the
# de-duplication is process-wide, matching the "warn once per process" intent
# of the original flag.
_warned_causes = set()
_warned_causes_lock = threading.Lock()
_WARNED_CAUSES_LIMIT = 64

# Bound on remembered per-entry validation results (see `_entry_problem`).
# Concurrent renders each test this bound and then insert, so the memo can
# overshoot it by up to one entry per racing thread; it is a sanity cap on an
# otherwise unbounded key space, not an exact quota.
_VERIFIED_ENTRIES_LIMIT = 64

# How long a positive per-entry verification stays good before that entry's
# reference closure is walked again. The memo exists so the healthy path costs
# one dict lookup instead of a `resolve()` plus an `is_file()` per referenced
# path; but a memo is only *discarded* when the manifest bytes change, so
# without an expiry a verification outlives the files it verified: deleting a
# chunk from `dist/` while leaving manifest.json alone left the entry serving
# a URL for a file that is no longer there, with no banner and no log line,
# until the next rebuild or a restart. Re-walking at most once per entry per
# window bounds how long that can last; a few seconds is far below the time
# any operator needs to notice, and it keeps the steady-state cost at
# essentially the memo's.
_VERIFICATION_TTL_SECONDS = 5.0

# Bound to a module-level name so a test can drive the window with a fake
# clock instead of sleeping. Monotonic, so a wall-clock adjustment cannot make
# a memo look arbitrarily fresh (or expire it early).
_monotonic = time.monotonic


def _warn_once(cause_key, message):
    """Log ``message`` the first time this ``cause_key`` is seen."""
    with _warned_causes_lock:
        if cause_key in _warned_causes:
            return
        if len(_warned_causes) >= _WARNED_CAUSES_LIMIT:
            # Pathological churn (e.g. a manifest whose *causes* keep
            # changing) must not grow this set without bound; dropping the
            # history costs at most a repeated warning.
            _warned_causes.clear()
        _warned_causes.add(cause_key)
    logger.warning(message)


def _for_log(value):
    """Render a manifest-supplied string safely for a single log line.

    Manifest keys and file paths come from a JSON file on disk that this
    process does not write. A key containing a newline (or a CR, or an ANSI
    escape) would otherwise forge additional log lines - a fabricated
    "ERROR ..." record indistinguishable from a real one in any log
    aggregator. Non-printable characters are escaped and the result is
    truncated, so one bad manifest key cannot rewrite the log.
    """
    text = value if isinstance(value, str) else str(value)
    if len(text) > 200:
        text = text[:200] + "..."
    return "".join(
        character if character.isprintable() else f"\\u{ord(character):04x}"
        for character in text
    )


# Visible, self-contained warning banner rendered when the Vite build output
# is missing. Inline styles are intentional: the real stylesheet is exactly
# what failed to load, so we can't rely on it, and the banner carries no JS
# dependency because the JS bundle is the very thing that's broken.
#
# Templates render this via `vite_missing_assets_banner()` at the top of
# <body> rather than from the `vite_asset()` call in <head> — flow content in
# <head> only renders because the HTML parser's error recovery force-opens
# <body>, which no strict parser or template test would reproduce.
_FALLBACK_BANNER = """
<div id="ldr-vite-missing-assets-banner" style="position:relative;z-index:99999;display:block;width:100%;box-sizing:border-box;background:#b91c1c;color:#ffffff;padding:12px 20px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;font-size:14px;line-height:1.5;text-align:center;border-bottom:2px solid #7f1d1d;">
    <strong>Frontend assets have not been built.</strong>
    The page below is unstyled and has no JavaScript.
    Run <code style="background:rgba(255,255,255,0.25);padding:2px 6px;border-radius:3px;font-family:monospace;">npm run build</code>
    from the project root, then reload this page — the rebuild is picked up
    automatically, without a restart. If the banner is still here after a
    successful build and reload, the server log names the cause; restarting
    the server is the last resort.
</div>
"""


@dataclass(frozen=True, eq=False)
class _ManifestState:
    """Everything derived from one read of manifest.json, as one value.

    The point of grouping these is that they must be read together or not at
    all. `vite_asset()` renders from a manifest, decides whether that
    manifest's entry is servable, and reports why it isn't - three questions
    that must all be answered about the *same* snapshot. Holding them in
    separate attributes let a positive verification of manifest A be stored
    under manifest B's fingerprint (a rebuild landing between the two reads),
    after which B was served with validation skipped, durably, until its
    bytes changed again.

    So readers take `self._state` once and use only that object, and writers
    publish a whole new state in a single attribute store. The dataclass is
    frozen to make "mutate one field" impossible to write by accident.
    `verified_entries` is a mutable dict owned by *this* state: it memoises
    results for these bytes only, and a new state starts with a fresh one, so
    a memo can never outlive the manifest it describes. It cannot outlive the
    *files* it describes either by more than `_VERIFICATION_TTL_SECONDS`,
    because each result is stamped with the time it was taken and expires
    after that window.
    """

    manifest: Optional[dict] = None
    # SHA-256 of the exact manifest.json bytes that produced `manifest`, or
    # None when `manifest` did not come from a disk read (a direct
    # assignment, or a failure). Deriving it from the parsed bytes keeps the
    # pair coherent even if a rebuild replaces the file during startup, and
    # also detects a same-size rewrite whose mtime is restored or coarse.
    fingerprint: Optional[str] = None
    # SHA-256 of the last manifest.json bytes that were read and refused.
    # Acceptance is a pure function of those bytes (no filesystem lookups
    # happen during adoption), so identical bytes always get the identical
    # verdict: remembering the fingerprint lets the degraded path skip the
    # JSON parse until the file actually changes.
    rejected_fingerprint: Optional[str] = None
    # (cause, location) describing why this state has no usable manifest, or
    # None when no read has failed. Lets the fallback markup and its log
    # distinguish "no manifest file at all" from "manifest present but
    # refused", which call for different operator actions.
    failure: Optional[tuple] = None
    # Entry point -> monotonic timestamp of the check that found its whole
    # blocking reference closure on disk, for *this* manifest. Positive
    # results only: a rejected entry is re-checked on the next call so a
    # chunk that lands late is picked up without waiting for the manifest
    # bytes to change. A positive result is re-checked too, once its
    # timestamp is older than `_VERIFICATION_TTL_SECONDS`, so a file deleted
    # after the check is noticed within that window of the next render
    # rather than never - expiry is only evaluated when the entry is next
    # requested, not on a timer, so an idle server never notices on its own.
    verified_entries: dict = field(default_factory=dict)


class ViteHelper:
    """Helper class for Vite integration with FastAPI.

    The `init_app()` / `_load_manifest()` pair is the legacy Flask entry
    point; the shipped application reaches this class through
    `init_for_fastapi()`.
    """

    def __init__(self, app=None):
        self.app = app
        self.is_dev = False
        # Set by `_load_manifest()` / `init_for_fastapi()` so
        # `_refresh_manifest_if_stale()` knows where to re-check for a
        # rebuilt/just-appeared manifest. Stays None for helpers built
        # directly in tests, which is the signal to skip any filesystem
        # access.
        self._manifest_path = None
        # The single coherent snapshot every reader takes a reference to.
        self._state = _ManifestState()

        if app:
            self.init_app(app)

    @property
    def manifest(self):
        """The manifest dict of the current snapshot (None before any load)."""
        return self._state.manifest

    @manifest.setter
    def manifest(self, value):
        # Assigning a manifest directly starts a brand-new snapshot: no
        # fingerprint describes these bytes (they never came from a read),
        # and no earlier verification result may carry over to them.
        self._state = _ManifestState(manifest=value)

    @property
    def _manifest_fingerprint(self):
        return self._state.fingerprint

    @property
    def _rejected_fingerprint(self):
        return self._state.rejected_fingerprint

    @property
    def _manifest_failure(self):
        return self._state.failure

    @property
    def _verified_entries(self):
        return self._state.verified_entries

    def init_app(self, app):
        """Initialize the helper with Flask app"""
        self.app = app
        self.is_dev = app.debug or app.config.get("VITE_DEV_MODE", False)

        if not self.is_dev:
            # Load manifest in production
            self._load_manifest()

        # Register template functions
        app.jinja_env.globals["vite_asset"] = self.vite_asset
        app.jinja_env.globals["vite_hmr"] = self.vite_hmr
        app.jinja_env.globals["vite_missing_assets_banner"] = (
            self.missing_assets_banner
        )

    def _load_manifest(self):
        """Load Vite manifest file"""
        static_dir = self.app.config.get("STATIC_DIR", "static")
        manifest_path = Path(static_dir) / "dist" / ".vite" / "manifest.json"
        self._manifest_path = manifest_path
        self._state = _ManifestState(manifest={})
        self._adopt_manifest_from_disk()

    def vite_hmr(self):
        """Return HMR client script for development"""
        if self.is_dev:
            return Markup(
                '<script type="module" src="http://localhost:5173/@vite/client"></script>'
            )
        return ""

    def vite_asset(self, entry_point="js/app.js"):
        """
        Return appropriate script tags for the entry point

        In development: Points to Vite dev server
        In production: Uses manifest to get hashed filenames
        """
        if self.is_dev:
            # Development mode - use Vite dev server
            # Development entry names come from trusted template calls; this is a development-only asset tag.
            return Markup(  # nosec B704
                f'<script type="module" src="http://localhost:5173/{entry_point}"></script>'
            )

        self._refresh_manifest_if_stale()
        # Read the snapshot once into a local: a concurrent refresh (which is
        # not serialised, and really is concurrent on a free-threaded build)
        # can publish a new state at any point, and the manifest, the
        # verification verdict and the failure reason below must all describe
        # the one snapshot this call renders from.
        state = self._state
        manifest = state.manifest
        if not manifest:
            # No usable manifest at all - either the file is absent or it was
            # read and refused. The entry name is irrelevant because nothing
            # resolves against an unusable manifest; `_fallback_assets`
            # distinguishes the two causes from `state.failure`.
            return self._fallback_assets(state)

        problem = self._entry_problem(state, entry_point)
        if problem is not None:
            return self._fallback_assets(state, entry_point, problem)

        # Get the built file from manifest
        file_info = manifest.get(entry_point)
        if not isinstance(file_info, dict):
            # Defence in depth. `_entry_problem` already established that this
            # key holds an object with a string 'file' *in this snapshot*, and
            # the snapshot cannot be swapped underneath us, but the dict it
            # points at is still shared mutable state (tests assign
            # `helper.manifest` directly, and nothing stops a future caller
            # editing it in place). Fail closed to the banner rather than
            # raise out of template rendering and 500 the page. The cause is
            # passed explicitly: the entry is present, so the default
            # "absent from the manifest" would send an operator looking for
            # a key that is sitting right there.
            return self._fallback_assets(
                state,
                entry_point,
                "its manifest entry stopped being an object after it was "
                "verified",
            )

        file_name = file_info.get("file")
        if not isinstance(file_name, str):
            # Same reasoning: a bare `file_info['file']` here turns a manifest
            # whose entry lost its 'file' key into a KeyError escaping into
            # Jinja, i.e. a 500 on every page instead of a degraded one. The
            # entry itself is present, so the cause is named rather than
            # left to the "absent from the manifest" default.
            return self._fallback_assets(
                state,
                entry_point,
                "its manifest entry has no 'file' string",
            )

        # Encode filename characters before putting them into trusted markup.
        # A literal ?, # or % must still address the file we verified, and
        # quotes must remain URL data rather than HTML attribute delimiters.
        file_path = f"/static/dist/{quote(file_name, safe='/')}"

        # Include CSS if present
        css_tags = ""
        for css_file in file_info.get("css") or ():
            css_tags += f'<link rel="stylesheet" href="/static/dist/{quote(css_file, safe="/")}">\n'

        # Include the main JS file
        js_tag = f'<script type="module" src="{file_path}"></script>'

        # Manifest asset paths are verified and URL-quoted before these fixed tags are assembled.
        return Markup(css_tags + js_tag)  # nosec B704

    def _fallback_assets(self, state=None, entry_point=None, problem=None):
        """Fallback markup used when the Vite manifest or a requested entry
        isn't usable.

        This only fires in production mode (`is_dev` is False) - dev-server
        mode returns from `vite_asset()` before ever reaching here, so it is
        never mistaken for a legitimate "no manifest yet" state. It means
        `npm run build` has never been run (or the built entry point is
        missing/stale), so we log it once per distinct cause and return HTML
        comments in place of the script and stylesheet tags. The comments
        are for whoever views source; the operator-visible signal is the
        red banner from `missing_assets_banner()`, which templates render at
        the top of `<body>`, and the log line emitted here.

        ``state`` is the snapshot the caller rendered from, so the reason
        reported here describes the same bytes the caller refused to serve
        rather than whatever the current attribute happens to hold; it
        defaults to the live snapshot for callers with nothing to render.

        ``entry_point`` is None when no manifest is in memory at all, which
        covers both "the file is absent" and "the file was read and refused";
        the snapshot's ``failure`` tells those apart, and a refusal was
        already reported by `_fail_closed()` naming the file and the cause, so
        it is not re-reported here under a misleading "manifest not found"
        headline. ``entry_point`` is the requested entry name (e.g.
        ``"js/app.js"``) when a manifest is loaded but that entry cannot be
        served, with ``problem`` naming the offending manifest key or file.
        Every caller that has an entry name also knows why that entry was
        refused and says so; the default below is only a last resort for a
        future caller that does not, so it is deliberately non-committal
        rather than asserting a specific cause it cannot know. Guessing
        "absent from the manifest" here sent operators looking for a key
        that was sitting in the file.
        The cases are logged differently because they call for different
        operator actions (rebuild everything vs. investigate one entry).
        """
        if state is None:
            state = self._state
        if entry_point is not None:
            problem = problem or "it is not servable from the current build"
            safe_entry = _for_log(entry_point)
            _warn_once(
                f"entry:{safe_entry}:{problem}",
                f"Vite manifest entry '{safe_entry}' cannot be served: "
                f"{problem}. This asset came from a stale or incomplete "
                "build (unstyled page or missing JavaScript). Run "
                "'npm run build' from the project root to regenerate "
                "static/dist, then reload.",
            )
            # Deliberately free of the entry name, the manifest path and the
            # hashed filename: those go to the log, which is not served to
            # every visitor, and nothing interpolated into this markup is
            # then attacker- or manifest-controlled.
            detail = (
                "the requested entry is not servable from the current "
                "build - see the server log"
            )
        elif state.failure is not None:
            cause, _location = state.failure
            # `_fail_closed()` already logged this, naming the file. `cause`
            # is one of this module's own literals.
            detail = (
                f"the manifest was read and refused ({cause}) - see the "
                "server log"
            )
        else:
            _warn_once(
                f"manifest-absent:{self._manifest_path}",
                "Vite manifest not found - the frontend was served "
                "without built assets (unstyled page, no JavaScript). "
                "Run 'npm run build' from the project root to generate "
                "static/dist, then reload.",
            )
            detail = "using existing static files as fallback"

        # Diagnostic detail is selected from fixed module messages; paths and entry names stay in logs.
        return Markup(  # nosec B704
            "\n<!-- Vite build not found - run 'npm run build' to generate production assets -->\n"
            f"<!-- {detail} -->\n"
        )

    def _refresh_manifest_if_stale(self):
        """Refresh production state from a coherent manifest snapshot.

        Detecting a same-size rewrite whose mtime is restored or coarse
        requires looking at content, not metadata, so every production call
        reads and hashes the (size-capped) manifest.json. `main` did no
        filesystem work at all on the healthy path - an accurate claim there,
        and exactly why `main` can never notice a rebuild without a restart.
        This trades that for staleness detection. JSON parsing happens only
        when the bytes differ from the one fingerprint the current state
        carries - a state records either the manifest it adopted or the
        bytes it refused, never both - so a steady state, healthy or
        degraded, costs a bounded read plus a hash and no parse.

        Templates currently invoke both the asset and the banner helper, so
        each helper invocation performs this check independently, and the
        two can legitimately disagree: `vite_asset()` in `<head>` and
        `missing_assets_banner()`/`assets_are_missing()` at the top of
        `<body>` each take their own snapshot. Each is internally coherent,
        but a rebuild landing between them can put valid script tags and the
        red banner on the same page. That is a cosmetic one-render artefact,
        not a stale-URL bug, and the next render agrees with itself again.
        The corollary is the steady-state cost: a page that calls both
        helpers pays this read-plus-hash twice, once per helper, not once.

        One steady state costs more than that, and deliberately so: a
        manifest that parses fine while a file it references is missing from
        `dist/`. Adoption succeeds, so the read and hash are the small ones,
        but the *entry* is unservable and negative verification results are
        not memoised (that is what lets a chunk landing a second later be
        picked up on the very next render). Every render therefore re-walks
        that entry's blocking closure and performs a `resolve()` plus an
        `is_file()` for each path in it - so the cost of that state is
        linear in the size of the closure, not of the manifest. On the real
        67-entry manifest, `js/app.js`'s own blocking closure is just two
        files - itself and its one stylesheet (65 of the 67 entries are
        fonts nothing asks for, and the on-demand diagram-export chunk is
        excluded from this closure by design, see `_entry_problem`) - but
        `resolve()` re-derives the real path from scratch for every
        referenced file rather than reusing the already-resolved `dist/`
        prefix, so most of the cost is stat-ing each directory component
        down to `dist/`, not the handful of files themselves. Measured
        against this repository's own checkout depth, a single degraded
        closure walk costs on the order of thirty `stat`/`lstat` syscalls,
        and a page renders both helpers (see above), so on the order of
        fifty of those syscalls are paid per page for as long as the build
        stays broken - a state an operator is expected to fix rather than
        run in. The healthy state pays the same walk once per entry per
        `_VERIFICATION_TTL_SECONDS`, which is what bounds how long a file
        deleted from `dist/` after verification can go unnoticed.

        The content hash comes from the exact bytes being parsed. A
        replacement between a read and a separate stat cannot pair manifest A
        in memory with manifest B's fingerprint: the next call hashes B and
        adopts it. Missing, oversized, malformed, or non-object manifests fail
        closed by clearing cached URLs, which activates fallback markup and
        the visible banner while a later valid build can still be adopted
        automatically.
        """
        if self.is_dev or not self._manifest_path:
            return
        self._adopt_manifest_from_disk()

    def _adopt_manifest_from_disk(self):
        """Read, parse, and adopt a coherent disk snapshot.

        The fingerprint is calculated from the exact bytes parsed below. This
        avoids a read-then-stat race at startup and ensures a same-size,
        same-mtime content rewrite is observed. It is taken before the size
        check so that an oversized manifest is remembered by the bytes that
        were actually read, exactly like every other refusal - an oversize
        refusal that stored no fingerprint left the state permanently
        "unknown", so nothing downstream could tell a repeat of the same bad
        file from a fresh one.

        Adoption itself touches only the bytes: unreadable, oversized,
        unparseable, and non-object manifests are refused, everything else is
        adopted. Whether the files a *requested* entry points at are actually
        on disk is checked per entry in `_entry_problem()`, so one dangling
        reference in an entry nothing asks for cannot blank the whole UI.
        Because adoption is a pure function of the bytes, identical bytes get
        an identical verdict - which is what makes the `rejected_fingerprint`
        fast path below safe: re-parsing refused bytes could not change the
        outcome, so the degraded path parses once, not once per request.

        A failure deliberately drops any old cached manifest rather than
        preserving URLs that a rebuild may have deleted. Adoption and refusal
        both publish one whole new `_ManifestState` in a single attribute
        store, so a concurrent reader sees either the old snapshot or the new
        one and never a mixture of the two.

        Two concurrent refreshes are not serialised, so the state that ends
        up published may be the one built from the *older* bytes. That is
        still a coherent snapshot rather than a mixture, and it self-heals:
        the next call hashes what is on disk, finds it differs from the
        fingerprint the published state carries, and adopts it.
        """
        try:
            with self._manifest_path.open("rb") as handle:
                raw_manifest = handle.read(_MAX_MANIFEST_BYTES + 1)
        except OSError:
            # Absent, unreadable (EACCES), or a directory. There is nothing to
            # fingerprint, so this is re-checked on every call: that is what
            # lets a manifest that appears later be picked up.
            self._fail_closed("no readable manifest file")
            return

        fingerprint = hashlib.sha256(raw_manifest).hexdigest()
        state = self._state
        if state.manifest and fingerprint == state.fingerprint:
            return
        if not state.manifest and fingerprint == state.rejected_fingerprint:
            return

        if len(raw_manifest) > _MAX_MANIFEST_BYTES:
            self._fail_closed(
                f"larger than the {_MAX_MANIFEST_BYTES}-byte read cap",
                fingerprint,
            )
            return

        try:
            manifest = json.loads(raw_manifest.decode("utf-8-sig"))
        except ValueError:
            self._fail_closed("not valid JSON", fingerprint)
            return
        except RecursionError:
            # Deeply nested JSON exhausts the interpreter's stack rather than
            # raising ValueError; without this the exception would escape into
            # template rendering and 500 every page instead of degrading.
            self._fail_closed("nested too deeply to parse", fingerprint)
            return

        if not isinstance(manifest, dict) or not manifest:
            self._fail_closed("not a non-empty JSON object", fingerprint)
            return

        self._state = _ManifestState(manifest=manifest, fingerprint=fingerprint)

    def _fail_closed(self, cause, fingerprint=None):
        """Drop the cached manifest and report why, once per distinct cause.

        "Distinct cause" means the (location, cause) pair, not the bytes: a
        manifest being rewritten with fresh garbage produces a new
        fingerprint on every render while remaining the *same* problem for
        the operator, and keying the de-duplication on the bytes turned "warn
        once" into one warning per render. The fingerprint is named in the
        message instead, so the single line that is emitted still identifies
        which bytes it looked at.
        """
        location = self._manifest_path
        self._state = _ManifestState(
            manifest={},
            rejected_fingerprint=fingerprint,
            failure=(
                cause,
                str(location) if location is not None else "<unknown path>",
            ),
        )
        if location is None or self.is_dev:
            # Nothing to name, or dev mode - where the manifest is expected to
            # be absent because Vite serves from memory.
            return
        seen = (
            f" (sha256 {fingerprint[:12]})" if fingerprint is not None else ""
        )
        _warn_once(
            f"manifest:{location}:{cause}",
            f"Vite manifest at {location} is unusable ({cause}){seen}; the "
            "frontend is being served without built assets (unstyled page, "
            "no JavaScript). Run 'npm run build' from the project root, "
            "then reload.",
        )

    @staticmethod
    def _is_contained_file(dist_root, relative_path):
        """True when ``relative_path`` names a real file inside ``dist_root``.

        The path has to be *relative*, has to name a file, and has to be
        written in already-normalised form, because the very same string is
        percent-encoded into a URL as `/static/dist/<path>`. Anything pathlib folds
        away silently is therefore a trap: this function verifies one path
        and the browser then requests another.

        `dist_root / relative_path` lets an absolute operand win, so an
        absolute path pointing at a file *inside* dist/ passed containment
        and `is_file()`, and then rendered a `src` that concatenated the
        static prefix with the server's own directory layout - a 404 that
        publishes that layout to every visitor. A trailing slash, a doubled
        separator, a `.` segment and a `..` segment are the same shape of
        bug: `js/app.js/`, `js//app.js` and `js/app.js/.` all resolve to the
        same file here and all render URLs that no static handler maps back
        to the file that was checked. Reject the lot rather than serve a URL
        that only looks verified.
        """
        if not isinstance(relative_path, str) or not relative_path:
            return False
        # Segment-level check on the raw string, before pathlib sees it. An
        # empty segment covers a leading separator, a trailing separator and
        # `a//b`; `.` and `..` cover the segments pathlib would fold away.
        # Both separators are inspected because nothing guarantees the
        # manifest was written on this platform.
        for separator in ("/", "\\"):
            if any(
                segment in ("", ".", "..")
                for segment in relative_path.split(separator)
            ):
                return False
        if PureWindowsPath(relative_path).drive:
            return False
        from ...security.path_validator import resolve_allow_missing

        try:
            # URL paths use UTF-8; a filesystem surrogate cannot be served
            # through that encoding even if this host can store its bytes.
            relative_path.encode("utf-8")
            # resolve_allow_missing(), not a bare resolve(): an unresolvable
            # path (a loop, a symlink chain whose resolved form outgrows
            # PATH_MAX) raises instead of coming back half resolved.
            target = resolve_allow_missing(dist_root / relative_path)
            return target.is_relative_to(dist_root) and target.is_file()
        except (OSError, ValueError):
            return False

    def _entry_problem(self, state, entry_point):
        """Return None if ``entry_point`` is servable, else why it isn't.

        ``state`` is the snapshot to answer the question about, passed in
        rather than re-read from `self`: the verdict, the manifest it
        describes and the memo it is recorded in have to be the same
        snapshot, or a verification of manifest A gets stored against
        manifest B and B is then served with validation skipped.

        Validation is scoped to what serving this entry actually needs: the
        entry itself, its `css`, and the transitive closure of its `imports`.
        Entries outside that closure are irrelevant - the manifest's 60-odd
        font entries are never `vite_asset()` targets (every template asks for
        `js/app.js`), and one font missing from `dist/` must not take the
        whole UI down when on `main` it cost a single 404.

        `dynamicImports` are deliberately *outside* the blocking closure.
        They are the chunks the browser fetches later, on demand: the real
        `js/app.js` entry lazily imports a canvg chunk that only the diagram
        export path ever needs, and letting it gate the closure means one
        unflushed lazy chunk blanks every page including the login form. They
        are still walked, and a problem in them is warned about once, because
        an operator does want to know that a feature will fail when it is
        reached - but it degrades one feature rather than the whole app.

        Every path that *is* reached must resolve to a real file inside
        `dist/`, so a symlink pointing out of the tree, or a chunk that has
        not been flushed yet, is refused. Cycles terminate; dangling manifest
        references are refused. Positive results are remembered in this
        state's own memo, so the healthy path costs one dict lookup for
        `_VERIFICATION_TTL_SECONDS` and then one closure walk; negative ones
        are not remembered at all, so a late-arriving chunk recovers on the
        very next call. The expiry is what keeps the memo from outliving the
        files it describes: manifest.json is not rewritten when a chunk is
        deleted from `dist/`, so nothing else would ever invalidate it.
        """
        verified = state.verified_entries
        verified_at = verified.get(entry_point)
        if verified_at is not None:
            # Clamped so a clock that runs backwards (a fake clock in a test,
            # or a real one stepped back by NTP) cannot produce a negative
            # delta and make this memo look permanently fresh.
            age = max(0.0, _monotonic() - verified_at)
            if age < _VERIFICATION_TTL_SECONDS:
                return None

        manifest = state.manifest
        dist_root = None
        if self._manifest_path is not None:
            from ...security.path_validator import resolve_allow_missing

            try:
                dist_root = resolve_allow_missing(
                    self._manifest_path.parent.parent
                )
            except OSError:
                return "the dist/ directory could not be resolved"

        deferred: list[tuple[str, Any]] = []
        problem = self._closure_problem(
            manifest, dist_root, [entry_point], deferred
        )
        if problem is not None:
            return problem

        if deferred:
            lazy_problem = self._lazy_closure_problem(
                manifest, dist_root, deferred
            )
            if lazy_problem is not None:
                safe_entry = _for_log(entry_point)
                _warn_once(
                    f"lazy:{safe_entry}:{lazy_problem}",
                    f"Vite entry '{safe_entry}' is servable, but a chunk it "
                    f"loads on demand is not: {lazy_problem}. The page will "
                    "render; the feature that pulls that chunk in will fail "
                    "at runtime. Run 'npm run build' from the project root "
                    "to regenerate static/dist, then reload.",
                )

        # Remembering more than the bound is refused rather than handled by
        # clearing: entry points come from templates, so a real deployment
        # has one or two, and this dict is discarded wholesale the moment the
        # manifest bytes change. Never clearing it also means a concurrent
        # reader's lookup cannot race a wipe. Re-stamping an entry that is
        # already remembered is always allowed, so a full memo cannot pin an
        # entry to a timestamp that never advances.
        if entry_point in verified or len(verified) < _VERIFIED_ENTRIES_LIMIT:
            verified[entry_point] = _monotonic()
        return None

    @classmethod
    def _closure_problem(cls, manifest, dist_root, roots, deferred):
        """Walk the reference closure from ``roots``; return the first problem.

        ``deferred`` is a list that collects ``(key, dynamicImports value)``
        pairs instead of following them - the blocking pass, which must not
        let a lazily-loaded chunk decide whether the page renders at all.
        Pass None to follow `dynamicImports` inline, which is what the
        advisory pass does.
        """
        pending = list(roots)
        visited = set()
        while pending:
            key = pending.pop()
            if key in visited:
                continue
            visited.add(key)

            entry = manifest.get(key)
            if not isinstance(entry, dict):
                return (
                    f"manifest key '{_for_log(key)}' is absent or not an object"
                )

            file_path = entry.get("file")
            if not isinstance(file_path, str):
                return f"manifest key '{_for_log(key)}' has no 'file' string"
            if dist_root is not None and not cls._is_contained_file(
                dist_root, file_path
            ):
                return (
                    f"'{_for_log(file_path)}' (manifest key "
                    f"'{_for_log(key)}') is not a file inside dist/"
                )

            css_paths = entry.get("css")
            if css_paths is not None:
                if not isinstance(css_paths, list):
                    return (
                        f"manifest key '{_for_log(key)}' has a non-list 'css'"
                    )
                for css_path in css_paths:
                    if not isinstance(css_path, str):
                        return (
                            f"manifest key '{_for_log(key)}' has a "
                            "non-string CSS entry"
                        )
                    if dist_root is not None and not cls._is_contained_file(
                        dist_root, css_path
                    ):
                        return (
                            f"'{_for_log(css_path)}' (manifest key "
                            f"'{_for_log(key)}') is not a file inside dist/"
                        )

            fields = ["imports"]
            if deferred is None:
                fields.append("dynamicImports")
            elif entry.get("dynamicImports") is not None:
                deferred.append((key, entry.get("dynamicImports")))

            for field_name in fields:
                references = entry.get(field_name)
                if references is None:
                    continue
                if not isinstance(references, list):
                    return (
                        f"manifest key '{_for_log(key)}' has a non-list "
                        f"'{field_name}'"
                    )
                for reference in references:
                    if not isinstance(reference, str):
                        return (
                            f"manifest key '{_for_log(key)}' has a "
                            f"non-string '{field_name}' reference"
                        )
                    if reference not in manifest:
                        return (
                            f"manifest key '{_for_log(key)}' imports "
                            f"'{_for_log(reference)}', which the manifest "
                            "does not define"
                        )
                    pending.append(reference)

        return None

    @classmethod
    def _lazy_closure_problem(cls, manifest, dist_root, deferred):
        """Validate the on-demand closure; a problem here is advisory only."""
        roots = []
        for key, references in deferred:
            if not isinstance(references, list):
                return (
                    f"manifest key '{_for_log(key)}' has a non-list "
                    "'dynamicImports'"
                )
            for reference in references:
                if not isinstance(reference, str):
                    return (
                        f"manifest key '{_for_log(key)}' has a non-string "
                        "'dynamicImports' reference"
                    )
                if reference not in manifest:
                    return (
                        f"manifest key '{_for_log(key)}' lazily imports "
                        f"'{_for_log(reference)}', which the manifest does "
                        "not define"
                    )
                roots.append(reference)
        return cls._closure_problem(manifest, dist_root, roots, None)

    def assets_are_missing(self) -> bool:
        """True when a production page would render without built assets."""
        if self.is_dev:
            return False
        self._refresh_manifest_if_stale()
        state = self._state
        if not state.manifest:
            return True
        return self._entry_problem(state, "js/app.js") is not None

    def missing_assets_banner(self):
        """Body-level banner shown when the frontend was never built."""
        if not self.assets_are_missing():
            return Markup("")
        # This banner is a fixed module literal and contains no request or manifest data.
        return Markup(_FALLBACK_BANNER)  # nosec B704

    def init_for_fastapi(self, static_dir, jinja2_templates):
        """Initialize the helper for FastAPI (no Flask app dependency).

        Args:
            static_dir: Path to the static directory.
            jinja2_templates: FastAPI Jinja2Templates instance.
        """
        # Dev mode via env setting — in dev, templates link to the Vite
        # dev server (port 5173) for HMR. In prod (default), we read
        # the hashed manifest.
        from ...settings.env_registry import get_env_setting

        self.is_dev = bool(get_env_setting("vite.dev_mode", False))

        manifest_path = Path(static_dir) / "dist" / ".vite" / "manifest.json"
        self._manifest_path = manifest_path
        self._state = _ManifestState(manifest={})
        self._adopt_manifest_from_disk()

        if not self.manifest and not self.is_dev:
            # Prod mode: no usable manifest is a real problem — warn loudly
            # instead of silently serving a blank page. This covers a
            # manifest that is present but was refused, not only an absent
            # one: both render the same assetless page, and gating on
            # `manifest_path.exists()` would stay silent for the former.
            #
            # A manifest that *parses* while a chunk it references is missing
            # is not covered here and cannot be: adoption succeeds, so this
            # branch is not taken, and the problem only surfaces when an
            # entry is actually requested. Its first log line therefore
            # arrives at the first render, not at startup.
            logger.warning(
                f"No usable Vite manifest at {manifest_path}. "
                "Run `npm run build` to generate production assets, or "
                "set LDR_VITE_DEV_MODE=true and run `npm run dev`. "
                "The app will render without JS/CSS until this is fixed."
            )

        # Register template functions on the Jinja2 environment
        jinja2_templates.env.globals["vite_asset"] = self.vite_asset
        jinja2_templates.env.globals["vite_hmr"] = self.vite_hmr
        jinja2_templates.env.globals["vite_missing_assets_banner"] = (
            self.missing_assets_banner
        )


# Create global instance
vite = ViteHelper()