Unverified Commit f7b9651d authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

docs: add turbo_tasks development guidance for agents (#100581)

### What?

Add agent instructions for developing with turbo_tasks across `crates/`
and `turbopack/`.

### Why?

Preserve fine-grained caching and invalidation by guiding cell creation,
task inputs, task boundaries, and access to build-wide maps. Discourage
unnecessary interning tasks and copying existing cell usage without
checking its assumptions.

### How?

Keep the generic turbo_tasks guidance in `turbopack/AGENTS.md` and
reference that section from `crates/AGENTS.md`. This preserves the
dependency direction from Next.js-specific crates to Turbopack, avoids
duplicating the rules, and leaves the root instructions and existing
Turbopack-specific boundaries unchanged.

### Verification

- Checked the shared-guide link resolves and the root instructions are
unchanged.
- Reviewed the guidance against existing turbo_tasks APIs.
- Formatting checked manually; Prettier is unavailable in the checkout.
No runtime tests were run for this instructions-only change.

<!-- NEXT_JS_LLM -->

<!-- fleet 0c8ecb28-308d-4b94-b67e-439012814b85 -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 7b972a88
# Rust Crates Development Guide
## turbo_tasks caching and invalidation
Read and follow the shared [turbo_tasks caching and invalidation guidance](../turbopack/AGENTS.md#turbo_tasks-caching-and-invalidation). Those turbo_tasks rules also apply throughout `crates/` and all its subdirectories when developing with turbo_tasks.
......@@ -3,3 +3,17 @@
Turbopack is a general-purpose bundler that is built and designed for Next.js, but is not necessarily Next.js-specific. Keep Next.js concepts out of it.
- **Do NOT mention Next.js config options** (e.g. anything under `next.config.js`, `experimental.*`, or other `NextConfig` fields) inside `turbopack/` code, comments, or docs. Refer to Turbopack's own options/inputs instead, and let the Next.js (in `packages/next/`) do the translation from Next.js config to Turbopack options. (This only applies to `turbopack/` — the Next.js-specific crates in `crates/` and `packages/next/` may reference Next.js config options.)
## turbo_tasks caching and invalidation
These rules apply throughout this directory tree when developing with turbo_tasks:
- Keep task cache keys narrow: pass only the inputs the task needs. Avoid build-wide state or large collections when a per-module or per-entry input would suffice.
- Avoid `.cell()` and `Vc::cell()` unless you know the current function is already called by functions with fine-grained cache keys. Inspect the callers and task boundaries before creating cells.
- Pass booleans, enums, and similar small values directly as `TaskInput` where appropriate instead of wrapping them in cells.
- When task inputs need to be cells to preserve fine-grained caching, arrange for them to already be cells returned by `#[turbo_tasks::function]` functions.
- Do not blindly introduce "interning" task functions that only wrap values, such as `Vec<T> -> Vc<Vec<T>>`. Only add such a task when you are certain it is beneficial in that particular situation.
- Existing `.cell()` calls or wrapping tasks are not automatically good precedent. Verify their caller and cache-key assumptions before copying the pattern.
- Put maps containing information for a large part of the build (for example, a map from modules to analysis results) into separate cells. Use `cell = "keyed"` on their value types, for example `#[turbo_tasks::value(transparent, cell = "keyed")]` for a map wrapper.
- Read keyed cells through keyed access, such as `map.get(&key).await?`, so invalidation depends only on the entries actually read. Awaiting the entire map and then looking up a key creates a dependency on the whole map instead.
- When changing task boundaries, check the intended invalidation behavior. An unrelated module change should not invalidate consumers of unchanged keyed entries; correct output alone does not demonstrate good cache granularity.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment