replyonce 0.1.2__tar.gz → 0.1.3__tar.gz
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.
- {replyonce-0.1.2 → replyonce-0.1.3}/PKG-INFO +1 -1
- {replyonce-0.1.2 → replyonce-0.1.3}/pyproject.toml +1 -1
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/__init__.py +1 -1
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/demo.py +76 -12
- replyonce-0.1.3/tests/test_demo.py +35 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/uv.lock +1 -1
- {replyonce-0.1.2 → replyonce-0.1.3}/.gitignore +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/LICENSE +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/NOTICE +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/README.md +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/scripts/interop.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/__main__.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/_core.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/_key.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/_logic.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/adapters.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/cli.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/py.typed +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/stores/__init__.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/stores/memory.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/stores/postgres.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/src/replyonce/stores/redis.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/tests/__init__.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/tests/conftest.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/tests/test_adapters.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/tests/test_key.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/tests/test_migrate.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/tests/test_once.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/tests/test_race.py +0 -0
- {replyonce-0.1.2 → replyonce-0.1.3}/tests/test_runs.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: replyonce
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: Stop your agent from replying, refunding or ticketing twice. Idempotency keys, per-conversation write fencing and an effect ledger for tool-calling agents, webhooks and workers.
|
|
5
5
|
Project-URL: Homepage, https://github.com/shmindmaster/replyonce
|
|
6
6
|
Project-URL: Source, https://github.com/shmindmaster/replyonce/tree/main/python
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "replyonce"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.3"
|
|
8
8
|
description = "Stop your agent from replying, refunding or ticketing twice. Idempotency keys, per-conversation write fencing and an effect ledger for tool-calling agents, webhooks and workers."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import asyncio
|
|
6
|
+
import heapq
|
|
6
7
|
import random
|
|
7
8
|
import time
|
|
8
9
|
import uuid
|
|
9
10
|
from dataclasses import dataclass, field
|
|
10
|
-
from typing import Optional
|
|
11
|
+
from typing import Callable, Optional
|
|
11
12
|
|
|
12
13
|
from ._core import ReplyOnce
|
|
13
14
|
from ._key import provider_idempotency_key
|
|
@@ -28,9 +29,57 @@ class Stats:
|
|
|
28
29
|
faults: dict = field(default_factory=dict)
|
|
29
30
|
|
|
30
31
|
|
|
32
|
+
def _wall_ms() -> int:
|
|
33
|
+
return int(time.time() * 1000)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# Simulated runs report times from 2026-01-01T00:00:00Z.
|
|
37
|
+
_SIM_EPOCH_MS = 1_767_225_600_000
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _loop_ms() -> int:
|
|
41
|
+
return _SIM_EPOCH_MS + int(asyncio.get_running_loop().time() * 1000)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class _SimulatedLoop(asyncio.SelectorEventLoop):
|
|
45
|
+
"""Event loop on simulated time: whenever nothing is ready, time jumps to the next timer.
|
|
46
|
+
|
|
47
|
+
With the in-memory store nothing else can wake the loop, so a seed always replays the same
|
|
48
|
+
run, and no real time passes. Steps through CPython's ``BaseEventLoop._run_once``.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def __init__(self) -> None:
|
|
52
|
+
super().__init__()
|
|
53
|
+
# Starts at zero: at epoch-sized values a float cannot hold the loop's clock resolution,
|
|
54
|
+
# so a timer due now would never count as ready.
|
|
55
|
+
self._sim_now = 0.0
|
|
56
|
+
|
|
57
|
+
def time(self) -> float:
|
|
58
|
+
return self._sim_now
|
|
59
|
+
|
|
60
|
+
def _run_once(self) -> None:
|
|
61
|
+
while self._scheduled and self._scheduled[0]._cancelled:
|
|
62
|
+
self._timer_cancelled_count -= 1
|
|
63
|
+
heapq.heappop(self._scheduled)._scheduled = False
|
|
64
|
+
if not self._ready and not self._stopping:
|
|
65
|
+
if not self._scheduled:
|
|
66
|
+
raise RuntimeError("replyonce demo: the simulation stalled")
|
|
67
|
+
self._sim_now = max(self._sim_now, self._scheduled[0]._when)
|
|
68
|
+
super()._run_once()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _run_simulated(make_coro):
|
|
72
|
+
loop = _SimulatedLoop()
|
|
73
|
+
try:
|
|
74
|
+
return loop.run_until_complete(make_coro())
|
|
75
|
+
finally:
|
|
76
|
+
loop.close()
|
|
77
|
+
|
|
78
|
+
|
|
31
79
|
@dataclass
|
|
32
80
|
class _World:
|
|
33
81
|
rand: random.Random
|
|
82
|
+
now_ms: Callable[[], int] = _wall_ms
|
|
34
83
|
inbound: dict = field(default_factory=dict)
|
|
35
84
|
sent: list = field(default_factory=list)
|
|
36
85
|
faults: dict = field(default_factory=dict)
|
|
@@ -91,7 +140,7 @@ def _guarded(w: _World, store) -> object:
|
|
|
91
140
|
if e["type"] == "late_settle":
|
|
92
141
|
w.bump("lateSettle")
|
|
93
142
|
|
|
94
|
-
ro = ReplyOnce(store, pending_ttl_ms=2_000, max_attempts=6, lease_ttl_ms=5_000, reconcile=reconcile, on_event=on_event)
|
|
143
|
+
ro = ReplyOnce(store, pending_ttl_ms=2_000, max_attempts=6, lease_ttl_ms=5_000, reconcile=reconcile, on_event=on_event, clock=w.now_ms)
|
|
95
144
|
|
|
96
145
|
async def agent(job: dict, crash: bool) -> None:
|
|
97
146
|
await _sleep_ms(2 + w.rand.random() * 10)
|
|
@@ -99,8 +148,8 @@ def _guarded(w: _World, store) -> object:
|
|
|
99
148
|
if not run.can_write:
|
|
100
149
|
return
|
|
101
150
|
inbound, done = job["inbound"], job["answered"]
|
|
102
|
-
give_up_at =
|
|
103
|
-
while
|
|
151
|
+
give_up_at = w.now_ms() + 15_000
|
|
152
|
+
while w.now_ms() < give_up_at:
|
|
104
153
|
todo = [m for m in inbound if m not in done]
|
|
105
154
|
if not todo:
|
|
106
155
|
break
|
|
@@ -127,13 +176,14 @@ def _guarded(w: _World, store) -> object:
|
|
|
127
176
|
return agent
|
|
128
177
|
|
|
129
178
|
|
|
130
|
-
async def _simulate(conversations: int, seed: int, make_agent) -> Stats:
|
|
131
|
-
w = _World(random.Random(seed))
|
|
179
|
+
async def _simulate(conversations: int, seed: int, make_agent, real_time: bool = True) -> Stats:
|
|
180
|
+
w = _World(random.Random(seed), now_ms=_wall_ms if real_time else _loop_ms)
|
|
132
181
|
agent = make_agent(w)
|
|
133
182
|
tasks: list = []
|
|
134
183
|
crashed: list = []
|
|
135
184
|
seq = 0
|
|
136
|
-
|
|
185
|
+
# Shared stores outlive a run, so real-time runs keep their conversations apart.
|
|
186
|
+
ns = uuid.uuid4().hex[:8] if real_time else f"seed{seed}"
|
|
137
187
|
|
|
138
188
|
def deliver(job: dict) -> None:
|
|
139
189
|
r = w.rand.random()
|
|
@@ -166,7 +216,7 @@ async def _simulate(conversations: int, seed: int, make_agent) -> Stats:
|
|
|
166
216
|
"conv": conv,
|
|
167
217
|
"inbound": list(w.inbound[conv]),
|
|
168
218
|
"answered": w.answered(conv),
|
|
169
|
-
"contextAt":
|
|
219
|
+
"contextAt": w.now_ms(),
|
|
170
220
|
})
|
|
171
221
|
|
|
172
222
|
for i in range(conversations):
|
|
@@ -200,11 +250,25 @@ async def _simulate(conversations: int, seed: int, make_agent) -> Stats:
|
|
|
200
250
|
return Stats(conversations, msgs, len(w.sent), dup, unanswered, w.faults)
|
|
201
251
|
|
|
202
252
|
|
|
203
|
-
async def run_demo(conversations: int = 100, seed: int = 7, store=None) -> tuple[Stats, Stats]:
|
|
204
|
-
|
|
253
|
+
async def run_demo(conversations: int = 100, seed: int = 7, store=None, real_time: Optional[bool] = None) -> tuple[Stats, Stats]:
|
|
254
|
+
"""Run the simulation without and with replyonce.
|
|
255
|
+
|
|
256
|
+
Simulated time (the default without a ``store``) makes every count a function of the seed;
|
|
257
|
+
real time (the default with a ``store``, since shared stores do real I/O) explores timing a
|
|
258
|
+
single seed cannot.
|
|
259
|
+
"""
|
|
260
|
+
real = store is not None if real_time is None else real_time
|
|
205
261
|
st = store if store is not None else MemoryStore()
|
|
206
|
-
|
|
207
|
-
|
|
262
|
+
|
|
263
|
+
async def both() -> tuple[Stats, Stats]:
|
|
264
|
+
without = await _simulate(conversations, seed, _naive, real)
|
|
265
|
+
guarded = await _simulate(conversations, seed, lambda w: _guarded(w, st), real)
|
|
266
|
+
return without, guarded
|
|
267
|
+
|
|
268
|
+
if real:
|
|
269
|
+
return await both()
|
|
270
|
+
# The simulated loop needs a thread of its own: this coroutine already runs on a real loop.
|
|
271
|
+
return await asyncio.to_thread(_run_simulated, both)
|
|
208
272
|
|
|
209
273
|
|
|
210
274
|
def format_report(without: Stats, with_: Stats) -> str:
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import subprocess
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
from replyonce.demo import format_report, run_demo
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_same_seed_gives_the_same_counts():
|
|
9
|
+
for seed in (1, 7, 42):
|
|
10
|
+
first = asyncio.run(run_demo(conversations=150, seed=seed))
|
|
11
|
+
second = asyncio.run(run_demo(conversations=150, seed=seed))
|
|
12
|
+
assert first == second
|
|
13
|
+
without, with_ = first
|
|
14
|
+
assert without.duplicate_answers > 0
|
|
15
|
+
assert with_.duplicate_answers == 0
|
|
16
|
+
assert with_.unanswered == 0
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_different_seeds_give_different_runs():
|
|
20
|
+
a, _ = asyncio.run(run_demo(conversations=150, seed=1))
|
|
21
|
+
b, _ = asyncio.run(run_demo(conversations=150, seed=2))
|
|
22
|
+
assert a.faults != b.faults
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_cli_prints_the_same_report_in_separate_processes():
|
|
26
|
+
cmd = [sys.executable, "-m", "replyonce.cli", "demo", "--conversations", "80", "--seed", "11"]
|
|
27
|
+
first = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
|
28
|
+
second = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
|
29
|
+
assert first.stdout == second.stdout
|
|
30
|
+
assert "No customer message was answered twice" in first.stdout
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_report_shape_is_unchanged():
|
|
34
|
+
without, with_ = asyncio.run(run_demo(conversations=20, seed=3))
|
|
35
|
+
assert format_report(without, with_).splitlines()[0] == "replyonce demo: 20 simulated support conversations under injected faults"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|