cratis/ai.distribution/add-reducer
Claude Code and agent skill by cratis/ai.distribution
- community
Checking that the SKILL.md file is still in the repository.
Use this skill when a read model genuinely needs an IReducerFor<T> — when model-bound projection attributes and fluent IProjectionFor<T> cannot express the state transition. Reducers are the last-resort escape hatch, not a default. Covers the admission checklist, method signatures, nullable-current handling, passivity, and pitfalls.
Install
npx vskill@latest install cratis/ai.distribution/add-reducerSKILL.md
The same SKILL.md was published earlier as cratis/ai.distribution/add-reducer.
Adding a Reducer (IReducerFor<T>)
A reducer is the last-resort way to build a read model — "current state + event → next state" with real C# control flow. Reach for it only when the projection ladder can't express the shape. A reducer that could have been a projection is a projection that was never written, and will be flagged for conversion in review.
Admission checklist — all three must be true before writing a reducer
- Model-bound attributes can't express it — not
[FromEvent<T>],[SetFrom<T>],[SetFromContext<T>],[ChildrenFrom<T>],[RemovedWith<T>],[Nested],[ClearWith<T>], counters ([Increment<T>]/[Count<T>]), or[FromAll]. - Fluent `IProjectionFor<T>` can't express it — not
UsingKey/UsingParentKey/UsingCompositeKey/UsingConstantKey,Join,.NotRewindable(), or a supported conditional setter. - The remainder needs real control flow — branching on prior state, loops, or accumulated-state calculations the declarative APIs don't have.
If all three hold, write the reducer and carry an inline justification comment on the class naming the specific limitation that ruled out a projection:
Method signatures
Each public method's first parameter is the event type it handles (dispatch is by type, not name):
| Signature | Meaning | |
|---|---|---|
T On(TEvent @event, T? current) | next state from current (null on first event) | |
T On(TEvent @event, T current) | non-nullable current when the model is always seeded first | |
+ EventContext context | add the context as a trailing parameter when you need Occurred/metadata | |
Task<T> On(...) | async when the computation awaits | |
return T?/null | treat the event as a no-op for this model |
- Return the complete new state — never mutate
currentin place (usewith { }). currentisnullon the first event for a given event source; for a standing model seeded by one event then updated by many, returnnullwhen the seed state is absent so replay andReadModelScenario<T>ordering survive.- Never store mutable state on the reducer class — it must be stateless.
- Don't throw for a no-op event — return
currentunchanged; throwing pauses the reducer's partition.
Passivity, filtering, and identity
[Passive]on the read model makes any reducer for it passive automatically (command-side reads only, not materialized);[Reducer(isActive: false)]is the per-reducer form.[Reducer(id: ..., eventSequence: ...)]overrides identity/source.- Filter which events the reducer observes with
[FilterEventsByTag],[EventSourceType],[EventStreamType](these filter;[Tag]/[Tags]only label).
Specs
Use ReadModelScenario<TReadModel> (it auto-detects reducer vs projection). Drive a sequence of events through Given.ForEventSource(id).Events(...) and assert on _scenario.Instance. Cover: first-event creation, accumulation across multiple events, and any branch in the reducer. See specs.scenarios.csharp.md and the write-specs-readmodels skill.
See also
- vertical-slices.md — the projection→fluent→reducer ladder.
add-projection— the model-bound and fluent options to exhaust first.cratis-readmodel— read model + query method basics.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/add-reducer/SKILL.md
Popularity
- 7-day trend score
- 0
Refreshed 1 Oct 2026, a month ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/cratis/ai.distribution/add-reducer)