# Lynx Stack This repository contains the **core JavaScript stack** of Lynx, including ReactLynx, Rspeedy, Lynx for Web, and testing utilities. This document provides essential information for coding agents to work efficiently with this codebase. ## Repository Overview **What it does**: Lynx is a cross-platform application framework that enables developers to build applications with familiar React syntax that can run natively on mobile devices and web browsers. This repository contains the core runtime, build toolchain, web platform implementation, and testing infrastructure. **Size & Structure**: Large monorepo with 49 packages, ~15MB of source code **Languages**: TypeScript (primary), JavaScript, Rust (native bindings) **Build System**: Turbo monorepo + pnpm workspaces **Target Runtimes**: Node.js 22+, browsers, mobile devices ## Essential Build Commands **ALWAYS follow this exact order for reliable builds:** ### 1. Initial Setup ```bash # Install dependencies (required first) npm install -g corepack corepack enable pnpm install --frozen-lockfile # Verify toolchain node --version # Required pnpm --version # Required rustc --version # Required for native bindings ``` ### 2. Building ```bash # Full build (REQUIRED before running tests) pnpm turbo build # Development build with watching pnpm turbo watch build ``` **⚠️ Critical**: Always run `pnpm install --frozen-lockfile` when dependencies or generated toolchain state may be stale, then run the full repository build with `pnpm turbo build` before tests. Watch mode only compiles TypeScript, not Rust components. Do not run ad hoc package-local builds such as `cd packages/... && pnpm build`, `rsbuild build`, or `rslib build` as a substitute for validation. Lynx Stack build artifacts are cross-package and can become stale or internally inconsistent when built outside Turbo. If you need a narrower build for diagnosis, still invoke it from the repository root through Turbo filtering, and run `pnpm turbo build` before relying on generated binary/e2e artifacts or updating snapshots. ### 3. Code Quality ```bash # Format code (ALWAYS run before committing) pnpm dprint fmt # Additional formatting check pnpm biome check # Linting (SLOW: 2+ minutes, run with adequate timeout) pnpm eslint . # Fix auto-fixable lint issues pnpm eslint --fix . ``` When editing `Cargo.toml`, especially workspace `members` or shared dependency lists, run `pnpm dprint fmt Cargo.toml`; dprint enforces TOML ordering and will fail CI if entries are left unsorted. ### 4. Testing ```bash # Run all tests (requires prior full build) pnpm test # Run tests for specific package pnpm run test --project websocket # Run tests for multiple packages pnpm run test --project 'webpack/*' # Test with coverage pnpm run test --coverage # Update test snapshots pnpm run test --update ``` ### 5. API Documentation The API reference in `docs/` (the pages synced to lynxjs.org) is generated from the exports and TSDoc comments of the public packages when the site builds; the generated pages are not committed. When you change a public API — an export, a type, an option, a default value or its TSDoc — check the documentation in the same change: ```bash # Generate the pages and build the site (fails on any TypeDoc warning or dead link; ALWAYS commit docs/i18n/zh.json) pnpm turbo build --filter @lynx-js/lynx-stack-docs # Update the API reports (ALWAYS commit changes) pnpm turbo api-extractor -- --local ``` - Write TSDoc for every public export and option: a one-sentence summary, `@defaultValue` when there is a default, and `@example` for options that need one. The pages render it as written. - A package is documented from `src/index.ts`; add a `typedoc.json` to the package for other entry points. Describe the package with `@packageDocumentation`. - See `docs/README.md` for how the pages are produced. ### 6. Changesets (for contributions) ```bash # Generate changeset for your changes pnpm changeset # Check changeset status (requires git remote origin/main) pnpm changeset status --since=origin/main ``` ## Common Build Issues & Solutions **Issue**: Build fails with Rust compilation errors **Solution**: Install/update Rust toolchain: ```bash # Install Rust with wasm target curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh rustup target add wasm32-unknown-unknown ``` **Issue**: "API Report Changes Detected" error **Solution**: Run API extractor and commit changes: ```bash pnpm turbo api-extractor -- --local git add packages/**/*.api.md ``` **Issue**: "API Docs Translations Changed" error **Solution**: Build the API reference, translate the new strings and commit them: ```bash pnpm turbo build --filter @lynx-js/lynx-stack-docs git add docs/i18n ``` **Issue**: Tests fail with missing build artifacts **Solution**: Always run full build first: ```bash pnpm turbo build pnpm test ``` **Issue**: Out of memory during build/test **Solution**: Increase Node.js memory: ```bash export NODE_OPTIONS="--max-old-space-size=32768" ``` ## Project Architecture ### Core Packages - `packages/react/` - **ReactLynx**: React-like framework for Lynx applications - `runtime/` - Core runtime implementation - `transform/` - Rust-based code transformations - `testing-library/` - Testing utilities for React components - `components/` - Built-in component library - `packages/rspeedy/` - **Rspeedy**: Webpack/rspack-based build toolchain - `core/` - Main build tool implementation - `create-rspeedy/` - Project scaffolding tool - `plugin-*/` - Various build plugins - `packages/web-platform/` - **Lynx for Web**: Web platform implementation - `web-core/` - Core web runtime - `web-elements/` - DOM element implementations - `web-core-e2e` - e2e tests - `packages/testing-library/` - Testing infrastructure - `packages/tools/` - Build and development utilities - `packages/webpack/` - Webpack plugins and tools ### Configuration Files - `turbo.json` - Monorepo build orchestration - `pnpm-workspace.yaml` - Workspace package definitions - `tsconfig.json` - TypeScript configuration (strictest mode) - `vitest.config.ts` - Test runner configuration - `eslint.config.js` - Linting rules (complex, includes React/TS rules) - `.dprint.jsonc` - Code formatting configuration - `biome.jsonc` - Additional code quality rules - `Cargo.toml` - Rust workspace configuration ## Validation Pipeline The CI runs these checks (replicate locally for confidence): 1. **Code style**: `pnpm dprint check && pnpm biome check` 2. **API consistency**: `pnpm turbo api-extractor`, and `pnpm turbo build --filter @lynx-js/lynx-stack-docs` leaves no changes in `docs/i18n` 3. **Changeset validation**: `pnpm changeset status --since=origin/main` 4. **Linting**: `pnpm eslint .` (allow 5+ minutes) 5. **TypeScript compilation**: Part of `pnpm turbo build` 6. **Unit tests**: `pnpm test` (vitest-based, requires build) 7. **E2E tests**: Web platform tests with Playwright 8. **Rust tests**: `cargo test --all-targets --all-features` in Rust packages 9. **Type checking**: `pnpm -r run test:type` ### GitHub Workflows - `.github/workflows/test.yml` - Main test orchestration - `.github/workflows/workflow-build.yml` - Cross-platform builds - `.github/workflows/workflow-test.yml` - Reusable test template - Multiple specialized workflows for different test suites ## Development Environment ### Required Tools - **Node.js** (specified in .nvmrc) - **pnpm** (specified in package.json, see `packageManager` field) - **Rust** (for native bindings compilation) ### VS Code Setup - Use "JavaScript Debug Terminal" for debugging tests - Extensions configured in `.vscode/extensions.json` - Settings in `.vscode/settings.json` ### Environment Variables ```bash CI=1 # Enables CI mode TURBO_TELEMETRY_DISABLED=1 # Disables telemetry NODE_OPTIONS="--max-old-space-size=32768" # For large builds DEBUG=lynx # Enable debug logging (DEBUG=rspeedy still works) ``` ## Trust These Instructions These instructions were generated through comprehensive analysis and testing of the repository. **Trust this information and only search for additional details if:** - Commands fail with unexpected errors - Instructions appear outdated (check git blame on this file) - Working with packages not covered in the architecture section **Time-saving tips:** - Use `pnpm turbo build --summarize` to see build performance - Use `--project` flag to focus tests on specific packages - Always check `pnpm-workspace.yaml` to understand package structure - Monitor `.turbo/` directory for cached build artifacts - Check `etc/*.api.md` files when API changes are needed ## Package-Specific Notes ### ReactLynx (`packages/react/`) - Core React-like implementation for cross-platform development - Requires both TypeScript and Rust compilation - Has extensive test suite in `__test__/` directories - JSX transforms handled by custom Rust-based compiler ### Rspeedy (`packages/rspeedy/`) - Webpack/rspack-based build tool optimized for Lynx apps - Includes CLI tool: `packages/rspeedy/core/bin/rspeedy.js` - Plugin architecture for extensibility - Template system in `create-rspeedy/` ### Web Platform (`packages/web-platform/`) - Large collection of 20+ packages for web implementation - Includes E2E test suite requiring Playwright - Many packages have complex interdependencies - Contains performance-critical rendering code - See `packages/web-platform/web-core/AGENTS.md` for specific instructions on `web-core`. ### GenUI UI Judge (`packages/genui/ui-judge/`) - UI Judge is a Rust library crate by default. The `server` feature adds the `ui-judge-server` HTTP binary; keep server code feature-gated and do not add an npm or TypeScript facade back to this directory. - The HTTP server must honor `LYNX_USE_HOST` when set and otherwise listen on IPv4 and IPv6 unspecified addresses, keep native Lynx capture on its dedicated current-thread worker, bound queued captures, compare images concurrently, and drain accepted work during SIGINT or SIGTERM shutdown. Keep CPU-heavy image processing on the bounded Rayon pool so it does not block Tokio workers. - Keep UI Judge limited to screenshots and deterministic image comparison. Export `capture_page`, `CapturePageRequest`, `CapturePageError`, `compare_images`, `ReferenceImageComparison`, `VisualEvaluationError`, and `VisualEvaluationErrorCode`; gate its HTTP adapter behind `server`. Use the unchanged headless runner and reject bare paths before runtime initialization. - GenUI Server owns model evaluation and agent behavior, using its existing provider and selected model configuration. Do not add model clients, model environment variables, or interaction planning to UI Judge. Its screenshot routes accept only capture inputs, upload the raw BMP to TOS using the same `TOS_*` environment variables as GenUI Server (plus the optional `TOS_SCREENSHOT_STORAGE_PREFIX`), and return `{"url": ...}`; consumers fetch the BMP from that URL and GenUI converts it to PNG before scoring. - Keep Rust tests deterministic and model-free. Its runtime-backed `headless_e2e` test captures and compares the real React fixture on Linux and macOS without credentials. Do not add browsers, emulators, devices, or PR-comment publishing jobs. Test evaluation and cancellation in GenUI Server with deterministic model mocks. Remember: This is a complex, multi-language monorepo. Always allow extra time for builds and tests, and follow the exact command sequences provided. ## Commit conventions - Do **not** add `Co-Authored-By:` (or other AI-attribution) footers to commit messages. Authorship is tracked via the git author field; trailers like these add noise to the history. ## Submit knowledge updates When you learn new patterns or best practices while working on the `lynx-stack` project, you should update or create one or more ".github/*.instructions.md" files, adding natural language instructions to the file(s). Whitespace between instructions is ignored, so the instructions can be written as a single paragraph, each on a new line, or separated by blank lines for legibility. Specify what files or directories the instructions apply to by adding applyTo frontmatter to the Markdown files, using glob syntax. Specify what files or directories the instructions apply to by adding applyTo frontmatter to the Markdown files, using glob syntax. For example: ```markdown --- applyTo: "packages/**" --- Add custom instructions here ```