/autumn:autumn-patternsfrom autumn
Provides idiomatic patterns for autumn-web Rust projects: state machine fields, TestApp/TestClient testing, query budget assertions, and framework-first design decisions beyond the API reference.
Ships in autumn. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Reference skills/autumn-web/SKILL.md for the API quick guide. This file
covers patterns and design decisions that apply across an Autumn app.
First rule: prefer the framework idiom. Before hand-rolling validation,
CRUD queries, pagination, forms, auth checks, background threads, or caching,
check the "Prefer framework idioms over raw Diesel/Axum" table in
skills/autumn-web/SKILL.md. Raw Axum/Diesel is allowed but is a last resort.
#[state_machine], not hand-rolled checksWhen a model has a status/phase column with legal transitions, declare them
with #[state_machine(transitions(...))] on the field and enforce with the
generated transition_{field}_to in before_update — never write your own
match (old, new) validation in hooks or handlers. To reuse one transition
graph across fields/models, define a #[lifecycle] enum and reference it with
#[state_machine(lifecycle = Enum)] (trunk-dev, #1916). The macro proves the
graph sound at compile time — an unreachable state or a non-terminal dead-end
does not compile (#1675); autumn lifecycle check re-proves it project-wide
and autumn lifecycle diagram emits a DOT/Mermaid state diagram. See docs/guide/state-machines.md and the worked
example in skills/autumn-web/references/examples.md.
The test-support feature ships an in-process test client. No running
server is needed. Add the feature for tests only:
[dev-dependencies]
autumn-web = { version = "0.11", features = ["test-support"] }
use autumn_web::test::TestApp; // not in prelude; requires test-support feature
#[tokio::test]
async fn create_post_returns_redirect() {
let client = TestApp::new()
.routes(routes![posts::create])
.build(); // synchronous; returns TestClient
let res = client
.post("/posts")
.form("title=Hello&body=World") // form() takes a URL-encoded &str
.send()
.await;
res.assert_status(302);
// header() returns Option<&str>
assert!(res.header("location").is_some_and(|loc| loc.contains("/posts/")));
}
Always use TestApp in tests — never spin up a real server or hit a live
database in unit tests.
Pin the SQL a route is allowed to run so an accidental N+1 fails the test:
client.get("/posts").send().await
.assert_status(200)
.assert_max_queries(3); // panics naming the route if the count exceeds 3
TestResponse::query_count() returns the observed count (from the
Server-Timing query counter); assert_max_queries(n) chains and returns
&Self (issue #1262).
assert_max_queries catches an N+1 only on the path a test exercises.
#[query_budget(N)] fails the build when any statically reachable path
can exceed N — every branch, tested or not:
#[get("/posts")]
#[query_budget(2)]
pub async fn index(repo: PgPostRepository) -> AutumnResult<Markup> {
let posts = repo.find_all().await?; // 1
let posts = repo.preload(posts, Post::preload().author()).await?; // 2
Ok(render(&posts))
}
Written with a per-row repo.find_author(...) inside a for loop instead, the
build fails with a diagnostic naming the loop and the call. Straight-line
statements sum; if/match arms take the worst arm; a handle-rooted chain is
one query; preload costs one query per association. Anything the analysis
cannot read — a helper handed the Db handle, a macro body naming it, a
closure that may run per element — is reported, not assumed query-free.
Escape hatches: #[query_budget(unbounded, reason = "…")] on the handler,
#[query_cost(N)] / #[query_exempt(reason = "…")] on a statement. Use the
two together: the compile-time gate for the upper bound, assert_max_queries
for the SQL actually issued (issue #1667). See docs/guide/query-budgets.md.
An endpoint tagged #[api_doc(mcp)] is an action an autonomous agent can take.
Declare what it is allowed to do, and let the build prove nothing in the body
exceeds it (#1691):
use autumn_web::prelude::*;
authority_grant! {
pub RefundDrafter {
writes: [Refund],
tenant_scope: scoped,
outbound: ["https://api.stripe.com/v1/refunds"],
jobs: [NotifyFinanceJob],
rate: "10/min",
spend: "500.00 USD",
reversibility: compensable,
}
}
#[post("/api/refunds")]
#[api_doc(mcp, summary = "Draft a refund")]
#[agent_operable(grant = RefundDrafter)]
pub async fn draft_refund(repo: PgRefundRepository, client: Client, Json(body): Json<NewRefund>)
-> AutumnResult<Json<Refund>> { /* … */ }
The analysis derives the handler's effect set — bounded and unbounded writes
(delete_all, truncate, an unfiltered diesel::update), cross-tenant reach
(across_tenants(), for_tenant(..), a raw diesel query on the request's
connection), literal outbound URLs and named("alias") clients, webhook
topics, and job enqueues — and emits one const-eval assertion per effect, so
payouts.delete_all() in that body fails cargo build at the call. Effects
are a set over every branch; a job, webhook, outbound call or unbounded write
cannot be declared reversible. Anything opaque (a helper handed a handle, a
format!-built URL, a non-literal job name, tokio::spawn, a dyn/impl
handle) is refused, never assumed harmless; discharge it with
#[agent_effect(writes(Refund), reason = "…")] or
#[agent_effect(none, reason = "…")] on the statement — declared effects are
checked against the grant exactly like proved ones.
autumn agents manifest --check agent-authority.json writes the diffable
record and fails on drift or on an ungoverned mutating MCP tool. Every MCP
tools/call writes attempt and outcome audit events carrying a correlation
id, the grant, the compile-known reversibility and the proved effects, with no
per-handler wiring. The MCP dispatcher enforces rate per process. spend is declared and published only.
See docs/guide/agent-authority.md.
Don't hand-build model fixtures. #[model] generates a {Model}Factory; fill
the rest with realistic fake data inferred from each field's name + type:
let post = Post::factory().title("Fixed").fake().build(); // NewPost, unset fields faked
let posts = Post::factory().fake().build_many(10); // Vec<NewPost>, each row re-drawn
let saved = Post::factory().fake().create(&pool).await; // persist (panics on failure)
Generators live in autumn_web::fake (name, email, sentence,
int_range, uuid, …); seed deterministically with AUTUMN_FAKE_SEED or
autumn_web::fake::reseed(seed). For bulk data outside tests,
autumn seed --count N --model <Name> (both flags together) inserts N faked rows
via the model's factory (issue #1343). See docs/guide/seeding.md.
Prefer free functions over a repository struct unless #[repository] is
generating a REST API. Repository structs are heavyweight; functions compose
better in tests.
// Good: free functions
pub async fn find_post(conn: &mut AsyncPgConnection, post_id: i64) -> AutumnResult<Post> {
use crate::schema::posts::dsl::*;
posts.find(post_id) // generated PK column is `id`; use .find() for primary key lookups
.first(conn)
.await
.map_err(|_| AutumnError::not_found_msg("post not found"))
}
// Use #[autumn_web::repository] when you need a generated REST API
#[autumn_web::repository(Post, api = "/api/posts", policy = PostPolicy, scope = PostScope)]
pub trait PostRepository {}
Repository-generated REST APIs must declare a policy in production or set
security.allow_unauthorized_repository_api = true explicitly — the default
will fail autumn doctor --strict.
Only add a service layer when logic is shared across multiple handlers or jobs. For single-handler logic, keep it in the handler.
pub struct PostService<'a> {
conn: &'a mut AsyncPgConnection,
}
impl<'a> PostService<'a> {
pub fn new(conn: &'a mut AsyncPgConnection) -> Self { Self { conn } }
pub async fn publish(&mut self, post_id: i64) -> AutumnResult<Post> {
// business logic here
}
}
// In the handler:
#[post("/posts/{id}/publish")]
#[secured]
async fn publish_post(Path(id): Path<i64>, mut db: Db) -> AutumnResult<Redirect> {
let mut svc = PostService::new(&mut *db);
svc.publish(id).await?;
Ok(Redirect::to(&format!("/posts/{}", id)))
}
Use #[job] for request-triggered work with retries. Keep jobs idempotent —
they may run more than once.
The #[job] macro takes AppState and a typed args struct (serializable).
The macro generates a PascalCaseJob struct with a static enqueue method.
Since 0.6.0 a job may also accept an
optional third JobContext argument for progress reporting on tracked jobs,
and a queue = "name" attribute for named priority queues.
Published 0.5.0 also supports idempotency/backpressure attributes:
#[job(unique)], unique_by = "field", unique_for_ms = N,
concurrency = N, concurrency_key = "field" — prefer these over
hand-rolled dedupe tables or semaphores.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SendWelcomeEmailArgs {
pub user_id: i64,
}
#[job(name = "send_welcome_email", max_attempts = 3, backoff_ms = 500)]
pub async fn send_welcome_email(state: AppState, args: SendWelcomeEmailArgs) -> AutumnResult<()> {
let pool = state.pool().ok_or_else(|| AutumnError::service_unavailable_msg("no db"))?;
let mut conn = pool.get().await.map_err(AutumnError::from)?;
let user = find_user(&mut conn, args.user_id).await?;
// state.extension::<Mailer>() returns Option<Arc<Mailer>>
let mailer = state.extension::<Mailer>()
.ok_or_else(|| AutumnError::service_unavailable_msg("mailer not configured"))?;
// generated helpers take &Mailer — deref the Arc:
UserMailer.send_welcome(&*mailer, user.email.clone(), user.username.clone()).await?;
Ok(())
}
// Enqueue from a handler (generated struct name = PascalCase + "Job"):
SendWelcomeEmailJob::enqueue(SendWelcomeEmailArgs { user_id: user.id }).await?;
Use #[scheduled] for recurring work. Use #[task] for operator-invoked
CLI work (autumn task <name>).
For durable multi-step workflows or jobs that need activity retries, timers,
or human approval steps, reach for Autumn Harvest (autumn-harvest-plugin).
Do not hand-roll step checkpoints or sagas on top of #[job]; see
docs/adr/0016-durable-workflows-live-in-harvest.md.
Before shipping any route:
#[secured] on any route that requires authentication#[secured("admin")] for admin-only routes#[authorize("update", resource = Post)] for record-level auth<form method="POST">Valid<Form<T>> or Valid<Json<T>> on every mutationunwrap() — use ? with AutumnResult<T>autumn.toml// Every POST form must include the CSRF token.
// `_csrf` is the default field name; if you set `security.csrf.form_field`
// in autumn.toml, use that value instead (or read it via CsrfConfig).
#[get("/posts/new")]
async fn new_post(csrf: CsrfToken) -> Markup {
html! {
form method="POST" action="/posts" {
input type="hidden" name="_csrf" value=(csrf.token());
// ...
}
}
}
In production, generate a stable signing secret before first deploy:
export AUTUMN_SECURITY__SIGNING_SECRET="$(openssl rand -hex 32)"
Always return AutumnResult<T>. Use typed constructors — avoid rolling
custom status codes.
// Not found
.map_err(|_| AutumnError::not_found_msg("post not found"))?
// Validation failure (re-render form)
return Err(AutumnError::unprocessable_msg("title is too short"));
// Authorization failure
return Err(AutumnError::forbidden_msg("not your post"));
JSON clients receive RFC 7807 Problem Details automatically. HTML clients get the configured error page renderer.
Keep layout in a shared function. Avoid duplicating DOCTYPE, <head>, and
<nav> across templates.
pub fn layout(title: &str, content: Markup) -> Markup {
html! {
(maud::DOCTYPE)
html lang="en" {
head {
meta charset="utf-8";
title { (title) " — MyApp" }
script src=(HTMX_JS_PATH) defer {}
script src=(HTMX_CSRF_JS_PATH) defer {}
link rel="stylesheet" href="/static/css/autumn.css";
}
body {
header { nav { /* ... */ } }
main class="container mx-auto px-4 py-8" { (content) }
}
}
}
}
pub fn post_show(post: &Post) -> Markup {
layout(&post.title, html! {
article {
h1 { (post.title) }
div class="prose" { (post.body) }
}
})
}
Config resolves lowest-to-highest: framework defaults → profile smart
defaults → autumn.toml → [profile.<name>] → autumn-{profile}.toml →
AUTUMN_* env vars.
Env var format: AUTUMN_SECTION__FIELD, double underscore as separator.
Examples: AUTUMN_DATABASE__PRIMARY_URL, AUTUMN_JOBS__BACKEND,
AUTUMN_SECURITY__SIGNING_SECRET.
Profile selection: AUTUMN_ENV > AUTUMN_PROFILE > --profile flag >
debug/release auto-detection.
| Concept | Name |
|---|---|
| Library crate (crates.io) | autumn-web |
| Rust import path | autumn_web:: |
| Entry macro | #[autumn_web::main] |
| CLI binary | autumn |
| Proc macros | autumn-macros |
| Admin plugin | autumn-admin-plugin |
| S3 storage | autumn-storage-s3 |
| Redis cache | autumn-cache-redis |
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
autumn-web, rust-patterns, state-machines, test-client, n-plus-one
Generated from the plugin's indexed repository (autumn-foundation/autumn) — no native marketplace selected
npx claudepluginhub p/madmax983-autumn7 skills in this plugin
/autumn-webUse when building, debugging, documenting, or upgrading Rust web applications with autumn-web, autumn-cli, or first-party Autumn crates; also use for Autumn route/model/repository/job/webhook/admin macros, AppBuilder setup, Maud + htmx server-rendered UI, Diesel async Postgres, and Autumn 0.11.x migration or release work.
/devUse when the user runs /autumn:dev, asks to start the Autumn development server, enable hot reload, or check what's running locally.
/doctorUse when the user runs /autumn:doctor, asks to audit their Autumn app's configuration, check for deployment readiness, or diagnose config problems, missing secrets, or stale migrations.
Other plugins' skills for the same job