Add a store
A service is a named interface for one job. Its port declares the operations that callers need. An adapter implements those operations for a storage provider.ΒΆ
Effect describes work with typed failures36 and required dependencies. Schema defines accepted values and their runtime checks.ΒΆ
A Layer builds services and supplies their construction dependencies. Alchemy declares and deploys cloud resources through Effect programs.ΒΆ
A capability is one named action with shared value definitions and a server-side implementation.ΒΆ
Read AGENTS.md first. Follow its source-first rules before changing Effect or Alchemy code. Start from packages/database and check which storage requirements it meets.ΒΆ
1. Name the facts and their owner
- Name the stored data, its system of record, and the writer.ΒΆ
- Mark copies calculated from source data. Describe how to rebuild them, how current they are, and their consistency guarantees.ΒΆ
- Read
packages/database/src/model.tsandpackages/database/src/run-log.ts.ΒΆ - Reuse
RunLogwhen its person-scoped completed-run facts and operation guarantees fit the job.ΒΆ - Name genuinely different facts before adding another store. Anonymous execution starts do not fit todayβs
RunLogschema.ΒΆ - Keep observational reporting failures from changing whether unrelated capabilities run or what they return, as request capture does in
packages/events.ΒΆ - If completeness requires gating execution, make that availability tradeoff an explicit product decision.ΒΆ
- Check composition before assuming recording exists. The deployed site does not currently provide or call
RunLog.ΒΆ - For a repeated aggregate, compare a bounded query with a keyed read model. Do not scan all history per page request.ΒΆ
- Write the authoritative fact once. Independent writes to source and aggregate cannot guarantee consistency.ΒΆ
Read skills/rat-stack-mode/principles/name-the-system-of-record.md and skills/rat-stack-mode/principles/no-dual-writes.md.ΒΆ
2. Define operations for the application job
- Copy the service shape in
packages/database/src/run-log.ts.ΒΆ - Give the port small named operations. Keep SQL, Drizzle, and vendor resources out of its callers.ΒΆ
- Put schemas and expected failures beside the port, as in
packages/database/src/model.ts.ΒΆ - Distinguish
InvalidDatabaseInputfromDatabaseError. Preserve operation and source cause.ΒΆ - Define retries and operation identity explicitly.
RunLog.recordgenerates a fresh UUIDv7 for each call; it is not replay-idempotent.ΒΆ
The existing port has no atomic multi-write operation. Do not imply that two port calls share a transaction. If a source write and cursor must commit together, design that operation before building the adapter.ΒΆ
3. Validate input and decode stored rows
- Decode input before issuing storage statements, as
RunLog.recordandRunLog.listRecentdo.ΒΆ - Reject invalid input with
InvalidDatabaseInputbefore storage work starts.ΒΆ - Decode unknown returned rows with
PersistedRunLogRowSchemainpackages/database/src/store.ts.ΒΆ - Decode the domain entry too. Reject mismatched outcome and failure fields instead of silently repairing them.ΒΆ
- Keep adapter failures typed as
DatabaseError.ΒΆ
Read skills/rat-stack-mode/principles/validate-before-durable-write.md.ΒΆ
4. Implement each vendor and bound reads
- Read
packages/database/src/vendor.tsandpackages/database/src/run-log-vendor.ts.ΒΆ - Keep each vendorβs Drizzle operations in the cartridge36, a package for one job with its implementation and required infrastructure. Choose one Layer with the exhaustive
DatabaseVendorResourceunion.ΒΆ - Name each readβs key, index, ordering, row budget, and pagination rule.ΒΆ
ListRecentRunsInputSchemabounds results to 1β100; adapters apply the person filter andlimit.ΒΆ- Inspect query plans. A bounded result can still scan many rows.ΒΆ
- Measure before choosing new operational limits. Label unmeasured values as guesses.ΒΆ
Read skills/rat-stack-mode/principles/bound-every-read.md and skills/rat-stack-mode/principles/measure-real-limits.md.ΒΆ
5. Carry schemas, migrations, and resources together
- Follow
packages/database/src/schema/d1.tsandpackages/database/src/schema/postgres.ts.ΒΆ - Keep each dialectβs migrations under
packages/database/migrations/.ΒΆ - Read
packages/database/src/migrations.tsfor paths that work outside the package directory.ΒΆ - Follow
packages/database/src/d1.tsorpackages/database/src/hyperdrive-postgres.tsfor resource-owning Layers.ΒΆ - Keep connection strings redacted. Do not expose credentials through the port or failure messages.ΒΆ
- Follow
packages/database/src/postgres-migrations.tsfor deploy-time registration.ΒΆ - Review infrastructure plans under AGENTS.mdβs approval rules. A schema change is not permission to delete a resource.ΒΆ
6. Prove the behavior on every storage implementation
Use packages/database/test/run-log.test.ts and packages/database/test/local-layers.ts as the executable references.ΒΆ
- Run the same behavior checks against D1 with local SQLite and Hyperdrive Postgres with PGlite.ΒΆ
- Check outcome variants, person isolation, ordering, limits, invalid inputs, and typed storage failures.ΒΆ
- Check replay and interrupted execution when the new operation promises idempotency or atomicity.ΒΆ
- Check migration paths with
packages/database/test/migration-paths.test.ts.ΒΆ - Prefer generated properties and command histories for new domain behavior. Plant a violation and observe failure before trusting them.ΒΆ
The current tests compare both storage implementations with examples. Deployed-provider behavior and vendor switching remain unproven. Name those gaps when reporting. Add real-infrastructure proof when the runtime differs from these substitutes.ΒΆ
7. Compose, validate, and report
Provide the chosen Layer at the appβs composition root16. Expose new actions through add-a-capability. For stateful workflows, also follow add-a-lifecycle-machine. Run pnpm turbo run check test build and fix failures without weakening the fence39. The fence is the compiler checks, lint rules, and hooks that reject prohibited code and shortcuts.ΒΆ
Report:ΒΆ
- The port, operations, system of record, and derived copies.ΒΆ
- Vendor choice, schema changes, migrations, and resource-plan actions.ΒΆ
- Read bounds, query-plan evidence, and measured limits or explicit guesses.ΒΆ
- Validation, backend test results, replay proof, and atomicity gaps.ΒΆ
- Composition wiring and what remains unproven on deployed infrastructure.ΒΆ
Last changed 2026-10-07 in f1a00f2. Source on GitHub. Change log.
Unlinked mentions (2)
- rat-stack-mode β md: copying a fact across stores or maintaining a read model.ΒΆ
- uncomplect β In domain-driven design (DDD), a read model is derived data arranged for queries.