Architectural Invariant Design: Svelte 5 Universal Runes SSR Isolation and Reactive Root Lifecycle Invariant
Svelte 5 Universal Runes SSR Isolation and Reactive Root Lifecycle Invariant defines an industrial-grade behavioral standard for frontend engineering. Enforces strict isolation of Svelte 5 universal runes ($state, $derived) and explicit lifecycle disposal of $effect.root instances during SSR. Prohibits module-level reactive state singletons in server runtimes to prevent cross-request memory retention, data contamination across tenants, and unbounded heap exhaustion. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
Hazard Analysis: What Breaks Without This Rule
The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:
| Failure Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Global Module-Scoped Runes and Orphaned Effect Roots Inducing Cross-Request SSR State Leakage | Critical | Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production. Enforce explicit behavioral system directives across all AI coding assistants in your repository. |
| Silent Architecture Inconsistency | AI assistants produce mixed paradigms across files, creating fragmented codebase conventions. | Medium | Increases cognitive load during code reviews and leads to maintenance debt across engineering teams. Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST NEVER declare module-level universal reactive state (`export const state = $state(...)` or top-level `$state` in `.svelte.ts`/`.ts` files) when executing in SSR environments
Engineers MUST NEVER declare module-level universal reactive state (`export const state = $state(...)` or top-level `$state` in `.svelte.ts`/`.ts` files) when executing in SSR environments; state MUST be scoped to Svelte component contexts via `setContext`/`getContext` or instantiated within request-scoped lifecycle boundaries.
Engineers MUST ALWAYS invoke and store the cleanup closure returned by `$effect.root(() => ...)` and guarantee execution of `cleanup()` in teardown paths when managing non-component reactive computations.
Engineers MUST ALWAYS invoke and store the cleanup closure returned by `$effect.root(() => ...)` and guarantee execution of `cleanup()` in teardown paths when managing non-component reactive computations.
Components and server-side utilities MUST NOT invoke `$effect` or `$effect.pre` expecting execution during SSR
Components and server-side utilities MUST NOT invoke `$effect` or `$effect.pre` expecting execution during SSR; all imperative server setup logic MUST reside in SvelteKit `load` functions or standard async functions.
Classes utilizing Svelte 5 runes (`$state`, `$derived`) MUST be instantiated inside component initialization trees or request contexts (such as `event.locals`), NEVER as global module-scoped singletons.
Classes utilizing Svelte 5 runes (`$state`, `$derived`) MUST be instantiated inside component initialization trees or request contexts (such as `event.locals`), NEVER as global module-scoped singletons.
Engineers MUST wrap SSR-shared store factories in dedicated context keys using `Symbol()` to guarantee type safety and prevent shared runtime reference mutation across concurrent incoming HTTP requests.
Engineers MUST wrap SSR-shared store factories in dedicated context keys using `Symbol()` to guarantee type safety and prevent shared runtime reference mutation across concurrent incoming HTTP requests.
Code creating detached reactive scopes MUST explicitly enforce boundary termination using `try...finally` blocks to guarantee `$effect.root` cleanup execution even in the event of unhandled exceptions.
Code creating detached reactive scopes MUST explicitly enforce boundary termination using `try...finally` blocks to guarantee `$effect.root` cleanup execution even in the event of unhandled exceptions.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Svelte 5 Universal Runes SSR Isolation and Reactive Root Lifecycle Invariant critical for AI coding assistants?
Enforces strict isolation of Svelte 5 universal runes ($state, $derived) and explicit lifecycle disposal of $effect.root instances during SSR. Prohibits module-level reactive state singletons in server runtimes to prevent cross-request memory retention, data contamination across tenants, and unbounded heap exhaustion. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.
Q:How does adopting this rule prevent AI hallucinations?
This rule explicitly declares what the AI must avoid (Global Module-Scoped Runes and Orphaned Effect Roots Inducing Cross-Request SSR State Leakage) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.
Q:Which AI coding environments are supported?
This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).
Q:Can I combine this rule with other technology stacks?
Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.