# @demlik/tea for coding agents

Version: 0.37.0
Use the docs and types shipped in this package: `node_modules/@demlik/tea/docs/` and `node_modules/@demlik/tea/dist/**/*.d.ts`.
The API changed between versions. Prefer these files over memory, external documentation and other copies of tea.
IMPORTANT: Prefer retrieval-led reasoning over pre-training-led reasoning. Read the relevant page before writing code.

<!-- Generated by pnpm --filter @demlik/tea run docs:agents. Do not edit. -->

## Before you hand-write it

| When | Use | From | Page |
| --- | --- | --- | --- |
| Replies arrive out of order and an older answer overwrites newer state; ignore late answers | `defineMachine`, `Identity`. Replaces a request id kept in State and compared in every update cell; give defineMachine an identity: { ofState, ofMsg } | `@demlik/tea` | [Read](docs/how-to/ignore-a-late-answer.md) |
| You step the machine yourself and must ignore late answers | `matchesIdentity`, `applyCell`. Replaces a custom filter; call matchesIdentity before applyCell | `@demlik/tea` | [Read](docs/how-to/ignore-a-late-answer.md#3-step-the-machine-in-your-own-host) |
| Save to your own database or ledger that can append atomically and read in order | `Journal`, `eventSourcedStore`. Replaces a Store that keeps only the latest State and no log; eventSourcedStore retains owed work (one writer per stream) | `@demlik/tea/persistence` | [Read](docs/how-to/make-durable.md) |
| Save the latest State in one SQL row with compare-and-set | `rowStore`. Replaces hand-written revision fencing; use a RowPort and { fenced: true } | `@demlik/tea/persistence` | [Read](docs/reference/persistence.md#rowStore) |
| Two processes may write the same machine's log | `FencedJournal`, `eventSourcedStore`. Replaces unchecked appends; supply a FencedJournal and { fenced: true } | `@demlik/tea/persistence` | [Read](docs/reference/persistence.md#eventSourcedStore) |
| Check whether your storage driver preserves order and atomic batches | `journalConformance`. Replaces a home-grown driver test suite; inspect failed and skipped cases | `@demlik/tea/pbt` | [Read](docs/reference/persistence.md#check-a-host-written-journal) |
| Work takes longer than one call or the platform's request limit | `owed`. Replaces awaiting the whole job; durably hand off, return owed(), then dispatch(msg, { answerTo }) on a later call (logged store required) | `@demlik/tea` | [Read](docs/how-to/make-durable.md#6-hand-long-work-to-an-external-owner) |
| List what is still waiting for an answer | `logLedger`. Replaces a second pending-work list; read logLedger(await store.readLog()).surviving() | `@demlik/tea` | [Read](docs/how-to/make-durable.md#6-hand-long-work-to-an-external-owner) |
| The host must wake the machine at its next deadline after the call ends | `deadlinesSub`, `deadlineExceeded`. Replaces an in-process timer after stop; share the State deadline selector and set the platform alarm in the host | `@demlik/tea/resilience` | [Read](docs/how-to/make-durable.md#5-end-each-call-and-return-the-next-alarm) |
| An effect needs typed success and error results | `Cmd.define`. Replaces hand-written outcome Msgs; declare input and ok schemas and err tags | `@demlik/tea` | [Read](docs/how-to/run-on-the-promise-engine.md) |
| The saved State shape changed and old saves must still load | `schemaMigrate`. Replaces repeated boundary parsing; give Store.migrate a schemaMigrate(schema, upcast) | `@demlik/tea` | [Read](docs/reference/tea.md#schemaMigrate) |
| The same request or job can arrive twice (a retry, a redelivery after a crash); do its work once per id | `createIntake`, `seen`, `remember`. Replaces a hand-kept Set of processed ids in State; keep an IdempotencyStore in the Model and check seen before the work | `@demlik/tea/idempotency` | [Read](docs/reference/idempotency.md) |

## Docs index

Paths are relative to this package. Reference pages list exports and signatures.

```text
|docs:{glossary.md "Glossary",README.md "@demlik/tea documentation"}
|docs/explanation:{agents-sdk-coexistence.md "Coexisting with Cloudflare's `agents` SDK",cmd-or-sub.md "Cmd or Sub: do this once, or tell me whenever",durability-model.md "What durability actually promises",errors-as-data.md "Why failures are values and bugs are throws",export-tiers.md "What each import is allowed to do to you",index.md "Explanation",pick-an-update-form.md "Which update form, and what a missing cell means",retained-case-history.md "Why the case source outlives its active context",what-bounds-a-run.md "What each run guard bounds"}
|docs/how-to:{add-resilience.md "Add retry and backoff to a call",ask-jev-a-typed-question.md "Ask Jev a typed question",bound-a-run.md "Bound a `defineAgent` run",compose-two-battery-slices.md "Compose two battery slices in one Model",delegate-to-a-sub-agent.md "Delegate to a sub-agent",deploy-an-agent-to-a-durable-object.md "Deploy an agent to a Durable Object",drive-from-react.md "Drive a machine from React",find-a-wrong-machine-with-a-law.md "Find a wrong machine with a law",gate-a-refactor-on-parity.md "Gate a refactor on a parity check",hand-wire-a-resilient-call.md "Hand-wire a resilient call",handle-a-tool-failure.md "Handle a tool failure",ignore-a-late-answer.md "Ignore an answer that arrives too late",index.md "How-to guides",make-durable.md "Make a machine durable and crash-recoverable",migrate-a-saved-state.md "Change a saved state's shape without losing old saves",migrate-from-0-15.md "Migrate from 0.15 to 0.19",reopen-a-retained-case.md "Reopen an investigation with retained history",replay-in-a-test.md "Replay a recorded run in a test",restore-or-refuse.md "Show a \"couldn't restore\" view when saved state can't be read",run-many-machines.md "Run many machines under one parent",run-on-the-effect-engine.md "Run a machine on the Effect engine",run-on-the-promise-engine.md "Run a machine on the Promise engine",show-a-run-in-progress.md "Show a run's progress while it runs",skip-saving-transient-states.md "Skip saving short-lived states",supply-the-agents-model.md "Supply the agent's model",test-a-machine-with-drive.md "Test a machine with `drive`",trace-an-agent-run-in-langfuse.md "Trace an agent run in Langfuse",wrap-one-tool-cell.md "Wrap one tool's interpret cell"}
|docs/reference:{agent.md,all-modules.md,devtools.md,do.md,effect.md,extension.md,flow.md,idempotency.md,index.md,jev.md,machine-viz.md,mem.md,node.md,otel.md,paginate.md,parity.md,pbt.md,persistence.md,promise.md,react.md,resilience.md,retry-backoff.md,tea.md,testing-effect.md,testing-promise.md,testing.md,timing.md,work-queue.md}
|docs/tutorial:{add-your-first-effect.md "Add your first effect",build-a-durable-agent.md "Build a durable agent",build-your-first-machine.md "Build and replay your first machine",index.md "Tutorials"}
```
