---
name: rust-architecture-baseline
description: >-
  Use when designing, scaffolding, planning, or coding any Rust product backend, CLI, or Tauri v2 desktop app: defines the layered L0-L5 Cargo workspace, strict deny lints, Engine::execute command pipeline, xtask verification gates, and Backend Service vs. Desktop-CLI profiles.
title: rust-architecture-baseline
canonical_url: https://skilld.dev/gh/kj-aiml/rust-product-skills/rust-architecture-baseline
last_updated: 2026-11-21T19:15:25.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [references/backend-service-profile.md](https://skilld.dev/api/skills-raw/kj-aiml/rust-product-skills/rust-architecture-baseline/references/backend-service-profile.md), [references/desktop-cli-profile.md](https://skilld.dev/api/skills-raw/kj-aiml/rust-product-skills/rust-architecture-baseline/references/desktop-cli-profile.md), [references/workspace-scaffold.md](https://skilld.dev/api/skills-raw/kj-aiml/rust-product-skills/rust-architecture-baseline/references/workspace-scaffold.md).
>
> If the user asked to install this Skill, run `npx skilld install kj-aiml/rust-product-skills/rust-architecture-baseline`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/rust-architecture-baseline`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/kj-aiml/rust-product-skills/rust-architecture-baseline) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/rust-architecture-baseline`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/rust-architecture-baseline --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/rust-architecture-baseline --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Rust Architecture Baseline

## Overview

Every Rust product built with this baseline shares one strictly layered workspace, one set of crash-prevention lints, and one automated verification gate (`cargo run -p xtask -- verify`). **Baseline invariants carry across every product; domain features and storage engines are chosen per product.**

Break a baseline rule only with an explicit ADR documenting what the rule protects and how the product preserves that guarantee.

## Default Stack

| Area | Baseline Choice |
| :--- | :--- |
| Language | Rust **Edition 2024**, pinned in `rust-toolchain.toml` (`clippy` + `rustfmt` included) |
| Workspace | Strict layered DAG: `L0 primitives` → `L1 spec` → `L2 domain` → `L3 store` → `L4 engine` → `L5 integrations` → `apps/*` + `xtask` |
| API / Dispatch | Single `CommandRegistry` + `Engine::execute` running in a fixed pipeline (policy check → panic boundary → handler → store/audit) |
| Persistence | Profile-dependent, isolated solely in `L3 store`: **Postgres + forced RLS** (multi-tenant services) OR **SQLite / atomic JSON** (single-user desktop/local tools) |
| External I/O | Isolated solely in `L5 integrations` implementing `L4 engine` port traits (`reqwest`, subprocesses, OS APIs) |
| UI Contract | React + Vite + Tailwind v4 consuming TypeScript bindings generated directly from `L0`/`L1` via `ts-rs` |
| Quality Gates | Workspace lints deny `unwrap`, `expect`, `panic`, `todo`, `unimplemented`, `unreachable`, `dbg`, `as`; forbid `unsafe`. Enforced by `cargo run -p xtask -- verify`. |

## Layered Crate Rules (The DAG)

| Layer | Crate Role | Allowed Internal Deps | Strictly Forbidden |
| :--- | :--- | :--- | :--- |
| **L0** | `<prefix>-primitives` | None | Any async runtime, `std::fs`, `std::process`, `std::net`, `sqlx`, `reqwest`, `tauri` |
| **L1** | `<prefix>-spec` | L0 | Same as L0 (pure contracts, DTOs, `CommandId`, `AccessPolicy`, `ts-rs` exports) |
| **L2** | `<prefix>-domain` | L0, L1 | Same as L0 (pure parsers, validators, state transitions, policy checks) |
| **L3** | `<prefix>-store` | L0, L1, L2 | `L4`, `L5`, `reqwest`, `std::process`, `tauri` (persistence only) |
| **L4** | `<prefix>-engine` | L0, L1, L2, L3 | `L5`, `reqwest`, `std::fs`, `std::process`, `tauri` (defines port traits; never imports adapters) |
| **L5** | `<prefix>-integrations` | L0, L4 | `L3`, `tauri` (implements L4 port traits using HTTP, OS APIs, or subprocesses) |
| **App** | `apps/server`, `apps/cli`, `desktop/src-tauri` | L0–L5 | Putting business logic or raw SQL directly in route/IPC handlers |

## Core Baseline Invariants

| Rule | What It Protects |
| :--- | :--- |
| **Pure `L0`–`L2` core** | Parsers, business rules, and domain policies stay 100% deterministic and testable in milliseconds with zero mocks or I/O |
| **Persistence only in `L3 store`** | Queries, migrations, and atomic file writes are audited in one crate; no stray connection pool or ad-hoc `fs::write` bypasses invariants |
| **External I/O only in `L5 integrations`** | Network calls, subprocesses, and OS APIs sit behind L4 traits so the entire engine runs under deterministic fake rigs in tests |
| **Fixed `Engine::execute` pipeline** | Every command declares an `AccessPolicy` in `CommandRegistry`; access is default-deny and checked before execution |
| **Engine owns the audit trail** | Individual commands never write their own audit rows; `Engine::execute` records redacted outcomes centrally |
| **Never crash** | Workspace deny lints (`unwrap_used`, `expect_used`, `panic`, `as_conversions`, `unsafe_code = "forbid"`) + `catch_unwind` future boundary inside `Engine::execute` |
| **No I/O inside an open DB transaction** | In database-backed services, commands emit outbox events or split into `begin` (tx 1) → L5 port call (no tx) → `finish` (tx 2) |
| **Automated `xtask verify` gates** | Layer violations, purity leaks, stale TypeScript types, and lint regressions fail fast locally and in CI |

## Choosing a Runtime Profile

Read the reference guide matching your product before scaffolding:
- **Workspace & `xtask verify` setup (required for all projects):** [references/workspace-scaffold.md](https://skilld.dev/api/skills-raw/kj-aiml/rust-product-skills/rust-architecture-baseline/references/workspace-scaffold.md)
- **Profile A — Multi-tenant / API Backend Service (Axum + MCP + Postgres RLS / SQLite):** [references/backend-service-profile.md](https://skilld.dev/api/skills-raw/kj-aiml/rust-product-skills/rust-architecture-baseline/references/backend-service-profile.md)
- **Profile B — Local Desktop & CLI App (Tauri v2 + Atomic Local Store / SQLite + Tray):** [references/desktop-cli-profile.md](https://skilld.dev/api/skills-raw/kj-aiml/rust-product-skills/rust-architecture-baseline/references/desktop-cli-profile.md)

## Breaking a Baseline Rule (ADR Pattern)

When a product genuinely cannot satisfy a rule verbatim (for example, an AI proxy whose core operation *is* a live upstream HTTP request rather than an async outbox event), write a short ADR that preserves the underlying safety property:

1. **Begin** (short tx 1): authenticate, validate quota/policy, insert a `pending` record.
2. **Call external port** (L5, no transaction open): invoke upstream provider with strict timeout and byte cap.
3. **Finish** (short tx 2): record usage/outcome on success, failure, and timeout alike.
4. **Reconcile**: run a periodic sweep for rows stuck in `pending`.
