statewire 0.23.4__tar.gz → 0.23.5__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.
- {statewire-0.23.4 → statewire-0.23.5}/PKG-INFO +1 -1
- {statewire-0.23.4 → statewire-0.23.5}/pyproject.toml +1 -1
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/api.py +101 -16
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_durable_admission.py +58 -0
- {statewire-0.23.4 → statewire-0.23.5}/.gitignore +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/README.md +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/examples/__init__.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/examples/demo_app.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/__init__.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/client.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/document.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/documents.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/filter.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/langgraph.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/limits.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/metrics.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/ops.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/protocol.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/state.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/src/statewire/testing.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/client_helpers.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/statewire_helpers.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/storage_probe.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_attach_fence.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_attach_guardrail.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_attached_hook.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_authorize.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_client.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_client_binding.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_client_ws.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_context.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_date_registry.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_departure.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_document.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_eviction.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_filter.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_frames.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_gap_fill.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_idle.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_lanes.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_langgraph.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_lifespan.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_meta.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_observer.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_ops.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_protocol_negotiation.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_read_filters.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_route_auth.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_start_session.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_state_proxy.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_statewire_hostable.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_storage_bridge.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_stream.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_testing.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_writer_lease.py +0 -0
- {statewire-0.23.4 → statewire-0.23.5}/tests/test_ws.py +0 -0
|
@@ -222,12 +222,14 @@ ATTACHER_HEADER = ops.ATTACHER_HEADER
|
|
|
222
222
|
RESULT_CACHE_TTL = 30.0
|
|
223
223
|
TRAY_RETIRING_RETRY_MS = 1000
|
|
224
224
|
RESULT_CACHE_LIMIT = 256
|
|
225
|
+
_PARKED_ANSWERS = 64
|
|
225
226
|
MAX_DRAIN_DEPTH = 50
|
|
226
227
|
MAX_JSON_DEPTH = 256
|
|
227
228
|
Handler = Callable[..., Awaitable[Any]]
|
|
228
229
|
|
|
229
230
|
_UNSET: Any = object()
|
|
230
231
|
_UNBOUND: Any = object()
|
|
232
|
+
_ABSENT: Any = object()
|
|
231
233
|
_WS_FIN_ATTEMPTED = "statewire.ws_fin_attempted"
|
|
232
234
|
|
|
233
235
|
|
|
@@ -666,7 +668,10 @@ class _Group:
|
|
|
666
668
|
class ClientCommands(Tray):
|
|
667
669
|
"""Per-client durable lanes: seq maps each protocol to its admitted-seq
|
|
668
670
|
watermark, the pool holds admitted frames (each one frame's durable
|
|
669
|
-
statements) until they execute.
|
|
671
|
+
statements) until they execute. parked holds ``{protocol, seq, response}``
|
|
672
|
+
of commands answered through ``_settle_parked``: None until the answer
|
|
673
|
+
lands, then kept for the latest ``_PARKED_ANSWERS`` so a redelivered frame
|
|
674
|
+
or a gap-fill reads it. Admission enlists kick (the running
|
|
670
675
|
claim) before the pool append before seq, so no crash point strands or
|
|
671
676
|
loses a pooled frame; the ack heals seq over the popped frame, so a
|
|
672
677
|
crash-cut seq never durably understates an executed frame.
|
|
@@ -679,6 +684,7 @@ class ClientCommands(Tray):
|
|
|
679
684
|
drain_on_eviction = True
|
|
680
685
|
seq = persistent({})
|
|
681
686
|
pool = persistent([])
|
|
687
|
+
parked = persistent([])
|
|
682
688
|
quarantine = persistent([])
|
|
683
689
|
identity = persistent(None)
|
|
684
690
|
identity_through = persistent({})
|
|
@@ -816,6 +822,39 @@ class ClientCommands(Tray):
|
|
|
816
822
|
self._check_above(statements)
|
|
817
823
|
self._advance(statements)
|
|
818
824
|
|
|
825
|
+
def parked_answer(self, protocol: str, seq: int) -> Any:
|
|
826
|
+
"""A parked command's stored answer: None while pending, _ABSENT when not parked."""
|
|
827
|
+
return next(
|
|
828
|
+
(
|
|
829
|
+
p["response"]
|
|
830
|
+
for p in self.parked.value
|
|
831
|
+
if p["protocol"] == protocol and p["seq"] == seq
|
|
832
|
+
),
|
|
833
|
+
_ABSENT,
|
|
834
|
+
)
|
|
835
|
+
|
|
836
|
+
def pooled(self, protocol: str, seq: int) -> bool:
|
|
837
|
+
return any(
|
|
838
|
+
s["protocol"] == protocol and s["seq"] == seq
|
|
839
|
+
for frame in self.pool.value
|
|
840
|
+
for s in _pool_statements(frame)
|
|
841
|
+
)
|
|
842
|
+
|
|
843
|
+
@inbox
|
|
844
|
+
def answer(self, protocol: str, seq: int, response: dict[str, Any]) -> None:
|
|
845
|
+
"""Store a parked or pooled command's answer."""
|
|
846
|
+
kept = [
|
|
847
|
+
p
|
|
848
|
+
for p in self.parked.value
|
|
849
|
+
if not (p["protocol"] == protocol and p["seq"] == seq)
|
|
850
|
+
]
|
|
851
|
+
answered = [p for p in kept if p["response"] is not None]
|
|
852
|
+
stale = answered[: max(0, len(answered) + 1 - _PARKED_ANSWERS)]
|
|
853
|
+
self.parked.value = [
|
|
854
|
+
*(p for p in kept if not any(p is q for q in stale)),
|
|
855
|
+
{"protocol": protocol, "seq": seq, "response": response},
|
|
856
|
+
]
|
|
857
|
+
|
|
819
858
|
@inbox
|
|
820
859
|
def discard(self, frames: list[tuple[str, list[dict[str, Any]]]]) -> None:
|
|
821
860
|
"""Remove held frames; their seqs stay consumed."""
|
|
@@ -910,12 +949,11 @@ class _Lane:
|
|
|
910
949
|
"type": "rejected",
|
|
911
950
|
**_IDENTITY_UNAVAILABLE,
|
|
912
951
|
}
|
|
913
|
-
if tray is
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
return None # pooled, not yet executed: answers ride the stream
|
|
952
|
+
answer = _ABSENT if tray is None else tray.parked_answer(self.protocol, seq)
|
|
953
|
+
if answer is not None and answer is not _ABSENT:
|
|
954
|
+
return {"protocol": self.protocol, "seq": seq, **answer}
|
|
955
|
+
if answer is None or (tray is not None and tray.pooled(self.protocol, seq)):
|
|
956
|
+
return None # pooled or parked: answers ride the stream
|
|
919
957
|
return {"protocol": self.protocol, "seq": seq, "type": "result-unavailable"}
|
|
920
958
|
|
|
921
959
|
def _retain(self, response: dict[str, Any]) -> None:
|
|
@@ -1242,7 +1280,8 @@ def _release_admission(applied: Callable[[], None]) -> None:
|
|
|
1242
1280
|
the caller bounds its parked commands. ``applied`` is the command's
|
|
1243
1281
|
``ctx.applied``; any other callable is ignored. On a durable host the
|
|
1244
1282
|
caller must have durably recorded the command's intent first: a crash
|
|
1245
|
-
after the release no longer redelivers the frame
|
|
1283
|
+
after the release no longer redelivers the frame, and the seq stays
|
|
1284
|
+
pending until ``_settle_parked`` answers it. No-op after applied or
|
|
1246
1285
|
settle."""
|
|
1247
1286
|
ctx = getattr(applied, "__self__", None)
|
|
1248
1287
|
if not isinstance(ctx, StatewireCommandContext):
|
|
@@ -1251,6 +1290,12 @@ def _release_admission(applied: Callable[[], None]) -> None:
|
|
|
1251
1290
|
if inv.applied or inv.settled:
|
|
1252
1291
|
return
|
|
1253
1292
|
inv.parked = True
|
|
1293
|
+
if inv.tray_ctx is not None:
|
|
1294
|
+
commands = inv.tray_ctx.tray
|
|
1295
|
+
commands.parked.value = [
|
|
1296
|
+
*commands.parked.value,
|
|
1297
|
+
{"protocol": inv.lane.protocol, "seq": inv.seq, "response": None},
|
|
1298
|
+
]
|
|
1254
1299
|
inv.finish()
|
|
1255
1300
|
|
|
1256
1301
|
|
|
@@ -1265,6 +1310,48 @@ def _admission_key(applied: Callable[[], None]) -> str | None:
|
|
|
1265
1310
|
return json.dumps([inv.client.id, inv.lane.protocol, inv.seq])
|
|
1266
1311
|
|
|
1267
1312
|
|
|
1313
|
+
async def _settle_parked(host: "Statewire", key: str, response: dict[str, Any]) -> None:
|
|
1314
|
+
"""Internal (harness_sdk's run manager): answer a durable command by its
|
|
1315
|
+
``_admission_key`` and store the answer in its client's ClientCommands
|
|
1316
|
+
row; returns once that write is durable. ``response`` is a
|
|
1317
|
+
``_response_of`` verdict; a command already answered stays as it was."""
|
|
1318
|
+
client_id, protocol, seq = json.loads(key)
|
|
1319
|
+
while True:
|
|
1320
|
+
try:
|
|
1321
|
+
tray = await host.client_commands.get(client_id)
|
|
1322
|
+
except KeyError:
|
|
1323
|
+
return
|
|
1324
|
+
if tray._retirement is None:
|
|
1325
|
+
break
|
|
1326
|
+
await tray._retirement.task
|
|
1327
|
+
stored = tray.parked_answer(protocol, seq)
|
|
1328
|
+
if stored is not _ABSENT and stored is not None:
|
|
1329
|
+
return
|
|
1330
|
+
if stored is _ABSENT and not tray.pooled(protocol, seq):
|
|
1331
|
+
return
|
|
1332
|
+
lane = host._pooled_client(tray).lane(protocol)
|
|
1333
|
+
inv = lane.inflight.get(seq)
|
|
1334
|
+
if inv is not None:
|
|
1335
|
+
host._settle(inv, response)
|
|
1336
|
+
else:
|
|
1337
|
+
lane.settle_late(seq, response)
|
|
1338
|
+
await tray.answer(protocol, seq, response)
|
|
1339
|
+
|
|
1340
|
+
|
|
1341
|
+
def _response_of(outcome: Any) -> dict[str, Any]:
|
|
1342
|
+
"""Internal: a handler's return value, or the exception it raised, as its verdict."""
|
|
1343
|
+
if isinstance(outcome, StatewireReject):
|
|
1344
|
+
response: dict[str, Any] = {"type": "rejected", "message": outcome.message}
|
|
1345
|
+
if outcome.code is not None:
|
|
1346
|
+
response["code"] = outcome.code
|
|
1347
|
+
if outcome.payload is not None:
|
|
1348
|
+
response.update(_payload_fields(outcome.payload))
|
|
1349
|
+
return response
|
|
1350
|
+
if isinstance(outcome, BaseException):
|
|
1351
|
+
return {"type": "failed", "message": "the command failed"}
|
|
1352
|
+
return {"type": "accepted", **({} if outcome is None else _payload_fields(outcome))}
|
|
1353
|
+
|
|
1354
|
+
|
|
1268
1355
|
class StatewireReject(Exception):
|
|
1269
1356
|
"""Definitive handler rejection: a ``rejected`` verdict rides ``cmd`` on
|
|
1270
1357
|
the stream, the seq is consumed (no re-run on retry), and message/payload
|
|
@@ -3458,7 +3545,10 @@ class Statewire(PinnedAPI):
|
|
|
3458
3545
|
}
|
|
3459
3546
|
slots = []
|
|
3460
3547
|
for statement in statements:
|
|
3461
|
-
|
|
3548
|
+
answer = commands_tray.parked_answer(statement["protocol"], statement["seq"])
|
|
3549
|
+
if answer is not None and answer is not _ABSENT:
|
|
3550
|
+
slot = answer
|
|
3551
|
+
elif denied is not None:
|
|
3462
3552
|
slot = dict(denied)
|
|
3463
3553
|
else:
|
|
3464
3554
|
prepared, rejection = self._prepare_member(statement)
|
|
@@ -3602,7 +3692,7 @@ class Statewire(PinnedAPI):
|
|
|
3602
3692
|
seq = statement["seq"]
|
|
3603
3693
|
lane = self._lane(client, lease, statement["protocol"])
|
|
3604
3694
|
if isinstance(slot, dict):
|
|
3605
|
-
#
|
|
3695
|
+
# A static rejection, or a stored answer, consumes its seq.
|
|
3606
3696
|
lane.settle(seq, {"type": "rejected", **slot})
|
|
3607
3697
|
continue
|
|
3608
3698
|
prepared = slot
|
|
@@ -3680,12 +3770,7 @@ class Statewire(PinnedAPI):
|
|
|
3680
3770
|
try:
|
|
3681
3771
|
value = await prepared.handler(*prepared.params, **kwargs)
|
|
3682
3772
|
except StatewireReject as exc:
|
|
3683
|
-
|
|
3684
|
-
if exc.code is not None:
|
|
3685
|
-
response["code"] = exc.code
|
|
3686
|
-
if exc.payload is not None:
|
|
3687
|
-
response.update(_payload_fields(exc.payload))
|
|
3688
|
-
self._settle(inv, response)
|
|
3773
|
+
self._settle(inv, _response_of(exc))
|
|
3689
3774
|
except HTTPException as exc:
|
|
3690
3775
|
detail = (
|
|
3691
3776
|
exc.detail if isinstance(exc.detail, str) else f"HTTP {exc.status_code}"
|
|
@@ -1182,3 +1182,61 @@ async def test_a_released_admission_frees_the_pool_slot_before_the_handler_settl
|
|
|
1182
1182
|
assert seen == [False]
|
|
1183
1183
|
assert keys == [('["old-client", "default", 1]', None)]
|
|
1184
1184
|
release.set()
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
async def test_a_parked_seq_stays_pending_across_a_restart_until_settled():
|
|
1188
|
+
"""``_settle_parked`` (the run manager's seam): a parked command a restart
|
|
1189
|
+
left without an invocation reads pending, then its stored answer, once."""
|
|
1190
|
+
from statewire.api import _response_of, _settle_parked
|
|
1191
|
+
|
|
1192
|
+
store = MemoryStateStore()
|
|
1193
|
+
row = ("/test", "test", "client_commands", "c1")
|
|
1194
|
+
parked = {"protocol": "default", "seq": 3, "response": None}
|
|
1195
|
+
store.trays[row] = (False, {"seq": {"default": 3}, "parked": [parked]})
|
|
1196
|
+
key = '["c1", "default", 3]'
|
|
1197
|
+
answer = {"protocol": "default", "seq": 3, "type": "accepted", "payload": {"echo": 1}}
|
|
1198
|
+
async with instance(Admitting, state_store=store) as host:
|
|
1199
|
+
commands = await host.client_commands.get("c1")
|
|
1200
|
+
lane = host._pooled_client(commands).lane("default")
|
|
1201
|
+
assert lane.duplicate_answer(2)["type"] == "result-unavailable"
|
|
1202
|
+
assert lane.duplicate_answer(3) is None
|
|
1203
|
+
await _settle_parked(host, key, _response_of({"echo": 1}))
|
|
1204
|
+
await _settle_parked(host, key, _response_of(None))
|
|
1205
|
+
assert lane.duplicate_answer(3) == answer
|
|
1206
|
+
lane.results.clear()
|
|
1207
|
+
assert lane.duplicate_answer(3) == answer
|
|
1208
|
+
await host.frontier.wait()
|
|
1209
|
+
assert store.trays[row][1]["parked"] == [
|
|
1210
|
+
{**parked, "response": {"type": "accepted", "payload": {"echo": 1}}}
|
|
1211
|
+
]
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
async def test_a_pooled_statement_with_a_stored_answer_answers_without_running():
|
|
1215
|
+
from statewire.protocol import VERSION
|
|
1216
|
+
|
|
1217
|
+
calls = []
|
|
1218
|
+
|
|
1219
|
+
class Restored(Admitting):
|
|
1220
|
+
@command
|
|
1221
|
+
async def ask(self, by: int):
|
|
1222
|
+
calls.append(by)
|
|
1223
|
+
return {"echo": by}
|
|
1224
|
+
|
|
1225
|
+
store = MemoryStateStore()
|
|
1226
|
+
row = ("/test", "test", "client_commands", "c1")
|
|
1227
|
+
frame = [{**statement(seq, params=[seq]), "_statewire_version": VERSION} for seq in (1, 2)]
|
|
1228
|
+
stored = {"type": "rejected", "message": "no", "code": "nope"}
|
|
1229
|
+
store.trays[row] = (True, {
|
|
1230
|
+
"seq": {"default": 2},
|
|
1231
|
+
"pool": [{"statewire_admission": 1, "auth": {"value": None}, "statements": frame}],
|
|
1232
|
+
"parked": [{"protocol": "default", "seq": 1, "response": stored}],
|
|
1233
|
+
})
|
|
1234
|
+
async with instance(Restored, state_store=store) as host:
|
|
1235
|
+
async with asyncio.timeout(2):
|
|
1236
|
+
while pooled_statements(store):
|
|
1237
|
+
await host.frontier.wait()
|
|
1238
|
+
await asyncio.sleep(0.01)
|
|
1239
|
+
results = host._clients["c1"].lane("default").results
|
|
1240
|
+
assert results[1][1] == {"protocol": "default", "seq": 1, **stored}
|
|
1241
|
+
assert results[2][1]["type"] == "accepted"
|
|
1242
|
+
assert calls == [2]
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|