A cartridge owns one job and its resources
A cartridge is a package that owns one job and the resources it needs. A service is the named interface callers use for that job. Its service tag identifies the service. A Layer describes how to build services and supply their dependencies.¶
An Alchemy Layer can also declare the cloud resources behind a service. Layer.provide supplies the chosen implementation to the application.
Replacing that Layer changes the implementation and its dependencies without changing callers.¶
The database cartridge makes this concrete. DatabaseVendor represents either D1 or Hyperdrive Postgres. packages/database/src/d1.ts declares the D1 schema and database10.¶
packages/database/src/hyperdrive-postgres.ts declares the Postgres schema, Hyperdrive connection, and migration registration.
Both provide the same RunLog service. The auth cartridge consumes DatabaseVendor.¶
packages/auth/src/auth.ts picks Better Auth18’s D1 or Hyperdrive adapter.¶
A cartridge must:¶
- Have a label.¶
- Add with one package and one Layer or Alchemy Stack line.¶
- Remove without breaking unrelated packages.¶
- Declare the bindings26 that give it access to its resources.¶
- Be easy to throw away.¶
Removal must leave no broken imports and no resource declarations in the Stack. Delete the package and its provision line, then run pnpm turbo run check test build. Retained production state stays in the cloud account until you delete it, as the worked example shows.¶
Worked example: remove request capture at one seam
The events cartridge documents its removal through apps/site/src/events.ts.
That file exports both names the site uses:¶
export { events, memoryEvents } from "@rat-stack/events/site";Core provides the same names with capture off:¶
export const events = (_options: EventsOptions) => Effect.undefined;
export const memoryEvents = (_salt: string) =>
Effect.succeed({
capture:
<RB>(_settings: RequestCaptureSettings<RB>) =>
<A, E, R>(app: Effect.Effect<A, E, R>) =>
app,
recorded: Effect.succeed<readonly RawEvent[]>([]),
});events returns undefined inside Effect37. memoryEvents leaves the application unchanged. Its recorded Effect yields an empty list.¶
The documented removal procedure has four steps:¶
- Change the re-export’s module to
@rat-stack/core/events-off.¶ - Remove
packages/events.¶ - Remove
@rat-stack/eventsfromapps/site/package.json.¶ - Run
pnpm install,pnpm git-snapshot, thenpnpm turbo run check test build.¶
This procedure removes the package from the application. It leaves the retained bucket, stream, and salt in the cloud account until explicitly deleted.
Turning capture off with enabled: false still declares those resources through basinFoundation.
See Analytics19 for the capture switch and its resource behavior.¶
Analytics19 is a second running example: packages/events owns capture and its Basin resources. README lists the steps needed to remove it. packages/subscriber-delivery is a provider adapter behind core’s ports. One-line infrastructure removal remains unproven for that package.¶
Where the pattern comes from
This project calls the pattern a “cartridge”. Sam describes Layers as a way to carry infrastructure and permissions with a service, and calls the idea “SaaS as NPM.” devtools.fm, 21:15. He also describes swapping implementations by providing different Layers. Effect Office Hours 27, 34:35.¶
Interchangeability between provider implementations remains unproven. Sam describes test or development Layers for infrastructure and a type error when code still requires a removed binding. Happy Path 116, 15:25 Effect Office Hours 27, 07:05.¶
See one capability, every surface21 for the shared contract and a clean playground9 for the house rule9 behind these boundaries.¶
Sources
- Events cartridge removal procedure. GitHub joelhooks/rat-stack. The import seam, replacement export, removal checks, and retained cloud resources. Accessed 2026-11-09.
- The site's events import seam. GitHub joelhooks/rat-stack. The single re-export quoted in the worked example. Accessed 2026-11-09.
- Core's capture-off replacement. GitHub joelhooks/rat-stack. The replacement keeps both export names, returns the application unchanged, and records no events. Accessed 2026-11-09.
- Disabled capture retains its resource declarations. GitHub joelhooks/rat-stack. The disabled branch calls basinFoundation before returning undefined. Accessed 2026-11-09.
- The events resource foundation. GitHub joelhooks/rat-stack. basinFoundation declares the bucket, stream, and visitor salt. Accessed 2026-11-09.
- rat-stack cartridge criteria. GitHub joelhooks/rat-stack. Cartridge criteria; used for labeling, composition, removal, and self-containment. Accessed 2026-11-09.
- rat-stack/packages/database/src/vendor.ts at a39e00b. GitHub joelhooks/rat-stack. Database vendor boundary; used for the D1 and Hyperdrive example. Accessed 2026-11-09.
- rat-stack/packages/auth/src/auth.ts at a39e00b. GitHub joelhooks/rat-stack. Auth composition; used for the cartridge that consumes the database vendor. Accessed 2026-11-09.
Last changed 2026-11-09 in 41dd0fe. Source on GitHub. Change log.
Linked from
- A clean playground. rat-stack does real work with packages that stay easy to add and remove.
A cartridge is a package for one job, with the resources that job needs.
- A Layer as constructor. Sam Goodwin compares an Effect Layer that builds a service to a constructor.
See Sam Goodwin on Alchemy and cartridges.¶
- A service captures its construction dependencies. Build a service with its dependencies, then expose methods that callers can use without rebuilding the graph.
rat-stack also uses capability projections, cartridge packages, and a no-comments rule.¶
- add-a-store. Add persistence through a service named for an application job, with validation, vendor Layers, migrations, bounded reads, and shared backend tests.
Keep each vendor’s Drizzle operations in the cartridge, a package for one job with its implementation and required infrastructure.
- Agent guide. An Effect stack so pure (aspirational) Kit Langton will blush.
A cartridge owns one job and its resources — A cartridge owns one job and brings the infrastructure its service needs.
- Agent rules for rat-stack (AGENTS.md). What you may change, which commands to run, and which changes need approval.
Role: Database cartridge: the RunLog service and the DatabaseVendor choice (D1 or Hyperdrive Postgres), with each vendor’s schema, migrations, and resources
- An MCP your users want. Rhys Sullivan's practical guide gives rat-stack a checklist for useful MCP tools.
: rat-stack today: The auth cartridge supports Better Auth sessions for an app, but ratstack.sh says it has no OAuth or access tokens.¶
- Analytics. One event per HTTP request, stored unchanged for queries to select and interpret later.
The packages/events cartridge is a package for one job, with its implementation and required infrastructure.
- Auth. Email and password sign-in through Better Auth, with the signed-in person provided to each RPC call.
The auth cartridge, packages/auth, is a package for one job with its implementation and required infrastructure.
- Bindings. Sam Goodwin describes bindings as the place where runtime permissions and configuration meet infrastructure.
The events cartridge sends one row per request to a Pipelines stream.
- Capture first, shape later. Store events without rejecting them for an evolving event shape, then decide their shape when you query.
In rat-stack, the packages/events cartridge owns event recording and its resources.
- Change log. What changed in the files served here, newest first.
A cartridge owns one job and its resources — edited: content: cartridge removal rule matches the retained-resource example; pin the remaining cartridge sources
- Database. One interface records action runs and their outcomes in either Cloudflare D1 or Postgres through Hyperdrive.
The database cartridge, packages/database, is a package for one job, with its implementation and required infrastructure.
- Durability. Seven durable Effect patterns, each with its seam, its fence, and the incident that taught it.
Seam: the flags cartridge separates the declared → rollingOut → on → removed lifecycle from scoped evidence rungs.
- Effect + Alchemy peer patterns. Source-grounded patterns from repos on nearby Effect and Alchemy pins.
Adapt this for the database cartridge.
- Effect + Alchemy peers. Public repositories that share rat-stack's prerelease lines.
Runtime provider switching remains a rejected pattern for our cartridge boundary.
- Feature flags. Typed feature flags with owners, removal dates, and stable subject rollouts.
The packages/flags cartridge evaluates typed flags.
- Full agent guide. An Effect stack so pure (aspirational) Kit Langton will blush.
- Hexagonal architecture. Ports name the application's jobs; adapters connect them to the outside world.
Cartridges adds rat-stack’s own packaging and removal test, including infrastructure supplied through Layer.provide.
- Hosted agent interfaces. Discovery documents, HTTP, MCP, A2A, and bounded code mode expose the public reference to agents.
A reusable cartridge for agent interfaces remains planned.
- Infrastructure as effects. Sam Goodwin relates application operations to infrastructure requirements in one typed program.
In the database cartridge, makeD1Vendor declares SQLite migrations and a D1 database.
- Layer composition makes dependencies explicit. Supply a service's dependencies, choose which services remain visible, and reuse Layer identity within a build.
A cartridge packages one job and its infrastructure.
- Learn mode presents concepts at useful depths. An agent presents source-derived concepts during real work and keeps learning events in a private local log.
The learning cartridge provides the adapter through @rat-stack/learn/local.
- Learn the stack | rat-stack. Hands-on guides to Effect actions, XState lifecycles, and the seams between stack pieces.
Link context
uncomplect — Find responsibilities that a rat-stack design combines, separate them into capabilities, cartridges, machines, features, and clients, and enforce that separation through types and checks.
- One capability, every surface. A schema-typed contract and one handler feed every interface rat-stack keeps.
Analytics shows a separate infrastructure cartridge; interest signup shows core ports with a delivery adapter outside core.
- One program can replace several tool calls. Code mode combines typed content calls inside one bounded execute request.
The cartridges page is now titled “A cartridge owns one job and its resources”, so a fresh run returns that title.¶
- Property-based testing. State a rule that must hold for every input, then let a generator hunt for the smallest input that breaks it.
The capture-first events cartridge is tested this way.
- Purpose and boundaries of rat-stack (VISION.md). What this starter is for and what a useful copy should keep.
Types, the fence, and cartridges that pull out cleanly raise the floor for every agent working here.
- query-design. Design or review SQL reads, writes, scans, backfills, and slow database paths. Use before changing a store method or any loop that queries. Covers round-trip arithmetic, batching, keyset paging, index evidence, row budgets, PlanetScale Vitess limits, Hyperdrive connections, statement-count tests, and statement-template fences.
A cartridge is a package for one job, with its implementation and required infrastructure.
- Rat Stack lore | rat-stack. Short, source-grounded notes on the ideas and decisions behind rat-stack.
A cartridge owns one job and its resources — A cartridge owns one job and brings the infrastructure its service needs.
- Rat Stack systems | rat-stack. The systems rat-stack ships, the standard each keeps, and how to check it.
The hosted agent front door records discovery, MCP versions, API, A2A, and sandbox limits; extracting it into a generic cartridge remains future work.
- rat-stack. An Effect stack so pure (aspirational) Kit Langton will blush.
Some packages share the hosted content build; they are not independent cartridges.
- rat-stack-mode. Route non-trivial rat-stack code, data, design, investigation, and documentation tasks to every matching playbook and principle.
A cartridge is a package for one job with its implementation and required infrastructure.¶
- Rat's Nest keeps the code public and the inventory private. Joel's purpose-built on-prem cloud uses rat-stack and Alchemy, with private inventory outside the public repo.
Cartridges explain rat-stack’s service and infrastructure pattern.¶
- Source change history (log.md). The generated change log as Markdown.
A cartridge owns one job and its resources — edited: content: cartridge removal rule matches the retained-resource example; pin the remaining cartridge sources
- Structure an Effect codebase by domain. Start with folders for application work, services and Layers in the same file, and startup code that supplies implementations.
Replaceable implementations: Cartridges with an add/remove test¶
- Tests prove behavior through supplied services. Supply services through Layers, choose properties or models for domain rules, and reject tests that cannot detect broken behavior.
Live-provider behavior needs separate proof for each cartridge.¶
- Turbo remote cache. A separate Worker stores streamed Turbo artifacts in R2, with one credential per team and lifecycle expiry.
The Stack includes the cartridge with yield* turboCache.
- uncomplect. Find responsibilities that a rat-stack design combines, separate them into capabilities, cartridges, machines, features, and clients, and enforce that separation through types and checks. Use when asked to simplify, review, or replace a design, "what would Rich Hickey do", "optimize for deletion", or "define this error out of existence".
Cartridge: A package that owns one job and its resources.
- Worker init runs twice. Alchemy Worker initialization runs in deploy and runtime contexts, so resource declarations need a boundary.
See cartridges for the rule that each resource belongs to its Layer and the fence for the required tests that check this separation.¶
Unlinked mentions (12)
- A clean playground → md describes the cartridge test.
- Agent rules for rat-stack (AGENTS.md) → Role: Better Auth cartridge over the chosen DatabaseVendor: Auth, CurrentPerson, and the RPC middleware that provides it; only Unauthenticated crosses the wire.
- Analytics → The cartridge does not prove that a delete physically erases a row by a known time.
- Auth → It uses the implementation chosen by the database cartridge.
- Effect + Alchemy peer patterns → Use one BetterAuth service with the database cartridge’s selected Layer.
- Effect + Alchemy peers → No closer lifecycle or cartridge pattern identified.
- Hosted agent interfaces → A cartridge is a package for one job, with its implementation and required infrastructure.¶
- Learn mode presents concepts at useful depths → A cartridge is a package for one job, with its implementation and required infrastructure.
- Purpose and boundaries of rat-stack (VISION.md) → Anything good can go in if it passes the cartridge test:
- query-design → rat-stack’s current database cartridge supplies D1 and Hyperdrive Postgres adapters.
- rat-stack → The hosted agent interfaces run in Mischief; extracting them into a separate cartridge is still work to do.
- uncomplect → Look for Drizzle queries, SQL dialects, or provider SDK calls outside their cartridge.¶