replyonce 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,7 @@
1
+ replyonce
2
+ Copyright 2026 Sarosh Hussain
3
+
4
+ This product includes software developed by Sarosh Hussain
5
+ (https://github.com/shmindmaster).
6
+
7
+ Licensed under the Apache License, Version 2.0. See the LICENSE file.
package/README.md CHANGED
@@ -1,3 +1,328 @@
1
- # Temporary Holding Version
1
+ # replyonce
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **Stop your agent from replying, refunding or ticketing twice.**
4
+
5
+ [![CI](https://github.com/shmindmaster/replyonce/actions/workflows/ci.yml/badge.svg)](https://github.com/shmindmaster/replyonce/actions/workflows/ci.yml)
6
+ [![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
7
+
8
+ Agents retry tool calls. Queues redeliver jobs. Webhooks arrive twice. A deploy kills a
9
+ run halfway, and a customer's second message starts a new run while the first one is still
10
+ answering. The customer gets two replies, or two refunds, or two tickets, and the agent
11
+ reports success.
12
+
13
+ replyonce sits between your agent and its side effects:
14
+
15
+ - **Each effect runs at most once per business identity**: the order being refunded, or
16
+ the message being answered. It is never keyed on the model's wording or its tool-call id.
17
+ - **Stale runs cannot write.** Every run on a conversation holds a fencing token. Once a
18
+ newer run, or a redelivery of the same job, takes over, the old run's writes are refused.
19
+ - **A run is told when the conversation changed under it.** If another run's reply is in
20
+ flight, or landed after this run read the conversation, the write is refused with what
21
+ happened, so the agent can re-read and decide.
22
+ - **Unknown outcomes are not retried blindly.** A timeout after the provider may have acted
23
+ is recorded as `ambiguous`. A reconciler you supply checks what happened, or a human resolves it.
24
+
25
+ Zero runtime dependencies. Node 18+. Stores for memory, Postgres and Redis. Adapters for
26
+ tool-calling loops (OpenAI and Anthropic shapes), webhook handlers and BullMQ workers.
27
+ A [Python port](python/) (asyncio, 3.10+) uses the same state machine and state format,
28
+ so Node and Python workers can share one store. CI checks that both ways. Runs on Windows,
29
+ macOS and Linux: CI runs the Node and Python suites, the shared-store interop and the examples
30
+ against real Postgres and Redis on all three.
31
+
32
+ ```text
33
+ $ npx replyonce demo
34
+ replyonce demo: 100 simulated support conversations under injected faults
35
+ without: workerCrash=9, duplicateDelivery=14, customerBurst=22, timeoutAfterSend=29, timeoutBeforeSend=15
36
+ with: workerCrash=10, duplicateDelivery=15, customerBurst=22, timeoutAfterSend=18, timeoutBeforeSend=12
37
+
38
+ messages replies duplicates unanswered
39
+ without 122 174 74 0
40
+ with replyonce 122 120 0 0
41
+
42
+ No customer message was answered twice with replyonce.
43
+ ```
44
+
45
+ The demo is a simulation, so the counts vary from run to run. CI runs it on every push
46
+ and fails if a single duplicate gets through.
47
+
48
+ ## 30-second quickstart
49
+
50
+ ```sh
51
+ npm install replyonce
52
+ ```
53
+
54
+ ```ts
55
+ import { createReplyOnce, MemoryStore } from 'replyonce';
56
+
57
+ const ro = createReplyOnce({ store: new MemoryStore() }); // PostgresStore / RedisStore in production
58
+
59
+ let refundsIssued = 0;
60
+ const refund = () =>
61
+ ro.once({ scope: 'order-A-1001', intent: 'refund', payload: { order: 'A-1001', cents: 2500 } }, async (ctx) => {
62
+ // Your provider call goes here; pass ctx.idempotencyKey if it accepts one.
63
+ refundsIssued++;
64
+ return { refundId: 're_1' };
65
+ });
66
+
67
+ // Three workers race, then a late retry arrives.
68
+ const raced = await Promise.all([refund(), refund(), refund()]);
69
+ const late = await refund();
70
+
71
+ console.log(raced.map((r) => r.status)); // [ 'executed', 'in_flight', 'in_flight' ]
72
+ console.log(late.status, late.ok && late.value); // replayed { refundId: 're_1' }
73
+ console.log(refundsIssued); // 1
74
+ ```
75
+
76
+ For a conversational agent, open a **run** per unit of work (one inbound message, one queue
77
+ job) and send every customer-visible write through it:
78
+
79
+ ```ts
80
+ const run = await ro.begin(conversationId, {
81
+ runId: job.id, // the same id on redelivery resumes the same run
82
+ contextAt: job.data.readAt, // when this run's view of the conversation was captured
83
+ });
84
+ if (!run.canWrite) return; // a newer run owns the conversation, or this job already finished
85
+
86
+ const r = await run.once('reply', { inReplyTo: inboundMessageId }, () => chat.send(conversationId, text), {
87
+ summary: text, // other runs see this if they conflict with it
88
+ });
89
+
90
+ if (r.status === 'refused' && r.reason === 'conversation_changed') {
91
+ // r.conflicts says what another run did. Re-read the conversation and decide again;
92
+ // calling run.once again is allowed once you have.
93
+ }
94
+ await run.end();
95
+ ```
96
+
97
+ ## How it works
98
+
99
+ ```mermaid
100
+ sequenceDiagram
101
+ autonumber
102
+ participant A as Run A (message 1)
103
+ participant L as replyonce ledger (per conversation)
104
+ participant B as Run B (message 2)
105
+ participant P as Provider
106
+ A->>L: claim reply {inReplyTo: m1}
107
+ L-->>A: acquired (pending)
108
+ A->>P: send reply to m1
109
+ B->>L: begin (newer context: B now owns the conversation)
110
+ B->>L: claim reply {answers: m1, m2}
111
+ L-->>B: refused: conversation_changed (A's reply to m1 is pending)
112
+ P-->>A: ok
113
+ A->>L: settle committed
114
+ B->>B: re-read conversation: m1 already answered
115
+ B->>L: claim reply {answers: m2}
116
+ L-->>B: acquired
117
+ B->>P: send reply to m2
118
+ A->>L: claim add_tag
119
+ L-->>A: refused: superseded
120
+ ```
121
+
122
+ Each **scope** (usually a conversation) has one small state document holding its runs and
123
+ its effect ledger. Every decision is one atomic read-modify-write of that document: no lock
124
+ in memory, a row lock (`SELECT … FOR UPDATE`) in Postgres, and a compare-and-set Lua script
125
+ in Redis. All the logic is a pure state machine in [`src/logic.ts`](src/logic.ts), so every
126
+ store behaves the same way, and the same test suite runs against all three.
127
+
128
+ Each ledger entry moves through these states:
129
+
130
+ ```mermaid
131
+ stateDiagram-v2
132
+ [*] --> pending: claim
133
+ pending --> committed: effect returned
134
+ pending --> failed: threw NotApplied (definitely did not happen)
135
+ pending --> ambiguous: threw anything else, or never settled within pendingTtlMs
136
+ ambiguous --> committed: reconciler or operator found it happened
137
+ ambiguous --> failed: reconciler or operator found it did not happen
138
+ failed --> pending: retry (up to maxAttempts)
139
+ committed --> [*]: replayed forever after (within retention)
140
+ ```
141
+
142
+ A claim is decided in this order:
143
+
144
+ 1. **Already done?** `committed` replays the stored result. `pending` reports `in_flight`,
145
+ or waits if you pass `wait`. `ambiguous` calls your reconciler, otherwise reports `ambiguous`.
146
+ 2. **May this run write?** It is refused if it was cancelled or has ended, or if its
147
+ fencing token is stale: a newer run took the scope, or a later delivery took the run
148
+ (`superseded`). A newer run that ended without writing does not count. Two runs whose
149
+ `contextAt` is the same millisecond are peers: neither supersedes the other, and step 3
150
+ keeps them from answering the same thing twice.
151
+ 3. **Has the conversation changed?** Another run's write that is still pending, or that
152
+ landed after this run last looked, is a conflict. The refusal records an *ack*, so the
153
+ run's next attempt (after it re-reads) can go through.
154
+ 4. **Claim**: write a `pending` entry and run the effect.
155
+
156
+ "Last looked" uses a logical sequence number per scope, not wall-clock time, so two writes
157
+ in the same millisecond are still ordered. `contextAt` moves the starting point back to
158
+ when the run's input was captured, which closes the gap between "webhook payload
159
+ snapshotted" and "worker started".
160
+
161
+ ## API
162
+
163
+ ```ts
164
+ createReplyOnce(options): ReplyOnce
165
+ ```
166
+
167
+ | Option | Default | Meaning |
168
+ |---|---|---|
169
+ | `store` | required | `MemoryStore`, `PostgresStore`, `RedisStore`, or your own `Store` |
170
+ | `pendingTtlMs` | 15 min | After this, an unsettled claim is treated as abandoned (`ambiguous`). Must exceed your slowest effect. |
171
+ | `maxAttempts` | 3 | Attempts per key, counting only attempts known not to have applied |
172
+ | `leaseTtlMs` | 60 s | Run lease, renewed on every claim and `heartbeat()` |
173
+ | `retentionMs` | 7 days | How long settled effects and ended runs are remembered |
174
+ | `classifyError` | see below | `(err) => 'not_applied' \| 'ambiguous'` |
175
+ | `reconcile` | none | `async (entry) => ({ outcome: 'committed' \| 'not_applied' \| 'unknown' })` |
176
+ | `onEvent` | none | Observer for `claimed`, `committed`, `replayed`, `refused`, `ambiguous` and other events (logs, metrics, OpenTelemetry) |
177
+
178
+ By default only `NotApplied` (or any error with `replyonce: 'not_applied'`) counts as
179
+ "did not happen". Every other error is `ambiguous`, because a timeout or reset says
180
+ nothing about whether the provider acted. Throw `new NotApplied(...)` for failures you
181
+ know happened before the write, such as validation or a 4xx from a provider that is
182
+ atomic per request.
183
+
184
+ | Method | |
185
+ |---|---|
186
+ | `ro.once(key, effect, opts?)` | Run `effect` at most once. `key` is a string (its own scope) or `{ scope, intent, payload }`. |
187
+ | `ro.begin(scope, { runId?, mode?, contextAt?, leaseTtlMs? })` | Start or resume a run. `mode: 'supersede'` (default, newest context wins) or `'exclusive'` (one lease holder at a time). |
188
+ | `run.once(intent, payload, effect, opts?)` | Fenced, conversation-aware `once`. Options: `summary`, `conflict: 'any' \| 'same-intent' \| 'none'`, `ack`, `wait`. |
189
+ | `run.check(intent, payload)` / `ro.check(key)` | "Was this already done?" Returns `{ done, status, entry }` without side effects. |
190
+ | `run.heartbeat()` | Renew the lease. `false` means stop. |
191
+ | `run.end(reason?)` / `ro.cancel(scope, runId)` | Finish or cancel a run. |
192
+ | `ro.history(scope)` | The scope's ledger, oldest first. |
193
+ | `ro.resolve(key, { outcome })` | Record what an operator found for an ambiguous effect. |
194
+ | `unwrap(result)` | The value, or throw `ReplyOnceError`. |
195
+
196
+ The effect receives `ctx`: `key`, `attempt`, `idempotencyKey` (stable across attempts and
197
+ workers; pass it to providers that support one), `fencingToken`, and `signal`, which aborts
198
+ when the claim would be treated as abandoned.
199
+
200
+ Every call returns a result instead of throwing (store outages do throw):
201
+
202
+ | `status` | Meaning | What to do |
203
+ |---|---|---|
204
+ | `executed` | Ran now | Use `value` |
205
+ | `replayed` | Already done earlier | Use the stored `value`; do not tell the customer twice |
206
+ | `in_flight` | Another worker is doing it right now | Nothing, or pass `wait` |
207
+ | `ambiguous` | May or may not have happened | Reconcile or escalate; never repeat blindly |
208
+ | `failed` | Known not to have happened | Retry if `retryable` |
209
+ | `refused` | `superseded`, `run_cancelled`, `run_ended`, `busy`, `conversation_changed`, `exhausted` | See `reason` |
210
+
211
+ ## Adapters
212
+
213
+ ```ts
214
+ import { guardTool, checkTool, dispatchTool, toOpenAITool, toAnthropicTool,
215
+ webhookOnce, httpStatusFor, onceProcessor } from 'replyonce/adapters';
216
+ ```
217
+
218
+ **Tool-calling loops.** `guardTool({ name, description, parameters, identity, execute })` wraps
219
+ a side-effecting tool. `identity(args)` picks the fields that make two calls the same action.
220
+ `tool.call(run, args)` returns a model-facing result: `ok`, `message`, `terminal` (stop the
221
+ loop) and `retryable`. `checkTool(tools)` adds a read-only `check_already_done` tool the model
222
+ can call first. `toOpenAITool` (Chat Completions or Responses) and `toAnthropicTool` produce
223
+ the provider's tool schema. See [`examples/agent-loop.ts`](examples/agent-loop.ts).
224
+
225
+ **Webhooks.** `webhookOnce(ro, { scope, eventId }, handler)` returns the handler's result
226
+ plus a suggested HTTP status. A concurrent duplicate gets `503` with `Retry-After`, so the
227
+ provider keeps retrying if the first delivery dies. A handled event gets `200`, and so does
228
+ an ambiguous one, because redelivery cannot resolve it. See
229
+ [`examples/webhook-server.ts`](examples/webhook-server.ts).
230
+
231
+ **BullMQ.** `onceProcessor(ro, { scope: (job) => job.data.conversationId }, processor)` makes
232
+ each job a run keyed by queue name and job id. Retries and stalled-job redelivery resume the same run;
233
+ completed `run.once` calls replay and are not redone. `onRedeliveredAfterWrite: 'skip'`
234
+ refuses to re-run a job that already wrote, for processors whose effect identities are not
235
+ deterministic. See [`examples/bullmq-worker.ts`](examples/bullmq-worker.ts).
236
+
237
+ ## Stores
238
+
239
+ ```ts
240
+ import { PostgresStore } from 'replyonce/postgres';
241
+ const store = new PostgresStore({ pool: new pg.Pool({ connectionString }) });
242
+ await store.migrate(); // or run postgresSchemaSql() in your migration tool
243
+
244
+ import { RedisStore } from 'replyonce/redis';
245
+ const store = new RedisStore({ client: new Redis(url) }); // ioredis
246
+ ```
247
+
248
+ Both take a structural client (`pg.Pool`-like or `eval`-capable), so replyonce has no
249
+ runtime dependencies. To use node-redis, adapt `eval` as shown in the `RedisStore` docs.
250
+ To write your own store, implement `transact(scope, fn)` atomically and `read(scope)`.
251
+
252
+ ## Guarantees
253
+
254
+ With a shared store and every side effect going through replyonce:
255
+
256
+ 1. **No concurrent attempts.** For a key, a new attempt starts only when no attempt is
257
+ pending and every earlier attempt is known not to have applied.
258
+ 2. **Committed means final.** A committed effect is not run again within `retentionMs`;
259
+ later calls replay its stored result.
260
+ 3. **Fencing.** A run whose token is stale (a newer run on the scope, a later delivery of
261
+ the same job, or an exclusive lease taken over after expiry) cannot claim new writes.
262
+ 4. **No writing from a stale view.** A run's claim is refused with `conversation_changed`
263
+ while another run's write on the scope is pending, or if one landed (or turned
264
+ ambiguous) after the run's context was read.
265
+ 5. **Ambiguity is preserved.** An outcome that is not known is never turned into a retry
266
+ unless your reconciler or an operator says it did not happen.
267
+
268
+ These properties are exercised by race tests (parallel workers, redelivery storms with
269
+ crashing workers, concurrent runs on one conversation) and by the chaos demo, against all
270
+ three stores, in CI.
271
+
272
+ ## Limits
273
+
274
+ - **Not exactly-once delivery.** If the provider acts and the worker dies before
275
+ recording it, replyonce knows only that the outcome is ambiguous. Exactly-once needs a
276
+ provider that honours `ctx.idempotencyKey`, or a reconciler that can look the effect up.
277
+ - **Fencing happens at claim time.** A write already sent when its run is superseded is
278
+ not recalled. Pass `ctx.fencingToken` or `ctx.signal` downstream if the target can check them.
279
+ - **`pendingTtlMs` must exceed your slowest effect.** Past it, a claim is treated as
280
+ abandoned. If a reconciler then wrongly says "not applied", a retry can duplicate. A late
281
+ settle is flagged as `lateSettle: true` and emits a `late_settle` event.
282
+ - **Keys are only as good as their identity.** Hash model-generated text into a key and a
283
+ reworded reply is a new key. The conversation fence still catches a reworded repeat when
284
+ the earlier write landed after the run's context was read, whether by another run or by
285
+ an earlier delivery of the same job. It does not catch a run that already saw the
286
+ earlier write and repeats it anyway, or one delivery doing the same intent twice.
287
+ - **Clocks.** Leases, TTLs and `contextAt` use each caller's clock. Keep clock skew well
288
+ below those durations. Ordering within a scope uses a logical counter.
289
+ - **One document per scope.** Very long-lived scopes with thousands of effects get slower.
290
+ Use narrower scopes or a shorter `retentionMs`. Ambiguous effects are kept until resolved.
291
+ - **Redis** gives the same logic, with Redis durability: asynchronous replication can lose
292
+ acknowledged writes on failover, and an expired key (`keyTtlMs`, default 30 days) is
293
+ forgotten. Prefer Postgres when that matters. Under extreme contention on a single scope,
294
+ `RedisStore` can throw `StoreContentionError`.
295
+ - **Not a workflow engine or an outbox relay.** There is no background reconciler: ambiguous
296
+ effects are reconciled the next time the key is claimed, or by `resolve()`.
297
+ - `MemoryStore` is per process: for tests, demos and single-instance services.
298
+
299
+ ## Python
300
+
301
+ ```sh
302
+ pip install replyonce # or "replyonce[postgres]" / "replyonce[redis]"
303
+ replyonce demo
304
+ ```
305
+
306
+ ```python
307
+ from replyonce import Key, MemoryStore, ReplyOnce
308
+
309
+ ro = ReplyOnce(MemoryStore())
310
+ result = await ro.once(Key("order-A-1001", "refund", {"order": "A-1001", "cents": 2500}), issue_refund)
311
+ ```
312
+
313
+ The API mirrors the TypeScript one in snake_case (`ro.begin(scope, run_id=..., context_at=...)`,
314
+ `run.once(intent, payload, effect)`). Keys hash identically, and the Postgres table and
315
+ Redis layout are the same. See [python/README.md](python/README.md).
316
+
317
+ ## Prior art
318
+
319
+ Idempotency keys as payment APIs use them, fencing tokens for distributed locks, the
320
+ transactional outbox, and at-least-once delivery with idempotent consumers. replyonce
321
+ applies them to the conversational case those patterns do not cover on their own: a newer
322
+ message supersedes an older run, a redelivered job must not answer again, and a reworded
323
+ reply is still the same reply.
324
+
325
+ ## Contributing and security
326
+
327
+ See [CONTRIBUTING.md](CONTRIBUTING.md) and [SECURITY.md](SECURITY.md). Licensed under
328
+ [Apache-2.0](LICENSE).