33import asyncio
44import contextlib
55import time
6- from typing import TYPE_CHECKING , Generic , Literal , cast
6+ from typing import TYPE_CHECKING , Final , Generic , Literal , cast
77from typing_extensions import (
88 Any ,
99 ParamSpec ,
2828from duron ._core .stream import ObserverStream , Stream , StreamWriter
2929from duron ._loop import EventLoop , create_loop
3030from duron .codec import Codec , JSONValue
31- from duron .log import decode_id , derive_id , encode_id , is_entry , random_id
31+ from duron .log import derive_id , is_entry , random_id
3232from duron .typing import Unspecified , inspect_function
3333
3434if TYPE_CHECKING :
6060_T = TypeVar ("_T" )
6161_P = ParamSpec ("_P" )
6262
63- _CURRENT_VERSION = 0
63+ _CURRENT_VERSION : Final = 0
6464
6565
6666@final
@@ -95,7 +95,7 @@ def get_init() -> InitParams:
9595 "version" : _CURRENT_VERSION ,
9696 "args" : [codec .encode_json (arg ) for arg in args ],
9797 "kwargs" : {k : codec .encode_json (v ) for k , v in kwargs .items ()},
98- "seed " : _generate_id (),
98+ "nonce " : random_id (),
9999 }
100100
101101 codec = self ._fn .codec
@@ -238,7 +238,7 @@ class InitParams(TypedDict):
238238 version : int
239239 args : list [JSONValue ]
240240 kwargs : dict [str , JSONValue ]
241- seed : str
241+ nonce : str
242242
243243
244244async def _invoke_prelude (
@@ -254,7 +254,7 @@ async def _invoke_prelude(
254254 if init_params ["version" ] != _CURRENT_VERSION :
255255 msg = "version mismatch"
256256 raise RuntimeError (msg )
257- loop .set_key (decode_id ( init_params ["seed" ] ))
257+ loop .set_key (init_params ["nonce" ]. encode ( ))
258258 extra_kwargs : dict [str , object ] = {}
259259 for name , type_ , dtype in job_fn .inject :
260260 with ctx .metadata ({"param.name" : name }):
@@ -324,7 +324,7 @@ def __init__(
324324 str ,
325325 tuple [Callable [[], Coroutine [Any , Any , None ]], TypeHint [Any ]],
326326 ] = {}
327- self ._pending_ops : set [bytes ] = set ()
327+ self ._pending_ops : set [str ] = set ()
328328 self ._now = 0
329329 self ._tasks : dict [str , tuple [asyncio .Future [None ], TypeHint [Any ]]] = {}
330330 self ._streams : dict [
@@ -337,7 +337,7 @@ def __init__(
337337 ] = {}
338338 self ._watchers = watchers or []
339339 self ._debug : dict [str , JSONValue ] | None = (
340- {"run.id" : _generate_id ()} if debug else None
340+ {"run.id" : random_id ()} if debug else None
341341 )
342342
343343 async def close (self ) -> None :
@@ -420,7 +420,7 @@ async def handle_message(
420420 pending_info = self ._pending_task .pop (e ["promise_id" ], None )
421421 task_info = self ._tasks .get (e ["promise_id" ], None )
422422
423- id_ = decode_id ( e ["promise_id" ])
423+ id_ = e ["promise_id" ]
424424
425425 return_type : TypeHint [Any ] = Unspecified
426426 if pending_info is not None :
@@ -451,7 +451,7 @@ async def handle_message(
451451 msg = f"Invalid promise/complete entry: { e !r} "
452452 raise ValueError (msg )
453453 elif e ["type" ] == "stream/create" :
454- id_ = decode_id ( e ["id" ])
454+ id_ = e ["id" ]
455455 if e ["id" ] not in self ._streams :
456456 self ._loop .post_completion (
457457 id_ , exception = ValueError ("Stream not found" )
@@ -460,7 +460,7 @@ async def handle_message(
460460 self ._loop .post_completion (id_ , result = e ["id" ])
461461 self ._pending_ops .discard (id_ )
462462 elif e ["type" ] == "stream/emit" :
463- id_ = decode_id ( e ["id" ])
463+ id_ = e ["id" ]
464464 if e ["stream_id" ] not in self ._streams :
465465 self ._loop .post_completion (
466466 id_ , exception = ValueError ("Stream not found" )
@@ -475,7 +475,7 @@ async def handle_message(
475475 self ._loop .post_completion (id_ , result = None )
476476 self ._pending_ops .discard (id_ )
477477 elif e ["type" ] == "stream/complete" :
478- id_ = decode_id ( e ["id" ])
478+ id_ = e ["id" ]
479479 if e ["stream_id" ] not in self ._streams :
480480 self ._loop .post_completion (
481481 id_ , exception = ValueError ("Stream not found" )
@@ -492,7 +492,7 @@ async def handle_message(
492492 _ = self ._streams .pop (e ["stream_id" ], None )
493493 self ._pending_ops .discard (id_ )
494494 elif e ["type" ] == "barrier" :
495- id_ = decode_id ( e ["id" ])
495+ id_ = e ["id" ]
496496 self ._loop .post_completion (id_ , result = offset )
497497 self ._pending_ops .discard (id_ )
498498
@@ -515,13 +515,13 @@ async def enqueue_log(self, entry: Entry, *, flush: bool = False) -> None:
515515 await self ._log .flush (self ._running )
516516 await self .handle_message (offset , entry )
517517
518- async def enqueue_op (self , id_ : bytes , fut : OpFuture [object ]) -> None :
518+ async def enqueue_op (self , id_ : str , fut : OpFuture [object ]) -> None :
519519 op = cast ("Op" , fut .params )
520520 match op :
521521 case FnCall ():
522522 promise_create_entry : PromiseCreateEntry = {
523523 "ts" : self .now (),
524- "id" : encode_id ( id_ ) ,
524+ "id" : id_ ,
525525 "type" : "promise/create" ,
526526 }
527527 if op .metadata :
@@ -538,9 +538,9 @@ async def cb() -> None:
538538 now_us = self .now ()
539539 entry : PromiseCompleteEntry = {
540540 "ts" : now_us ,
541- "id" : encode_id ( derive_id (id_ ) ),
541+ "id" : derive_id (id_ ),
542542 "type" : "promise/complete" ,
543- "promise_id" : encode_id ( id_ ) ,
543+ "promise_id" : id_ ,
544544 }
545545 try :
546546 result = op .callable (* op .args , ** op .kwargs )
@@ -556,7 +556,7 @@ async def cb() -> None:
556556
557557 def done (f : OpFuture [object ]) -> None :
558558 if f .cancelled ():
559- sid = encode_id ( f .id )
559+ sid = f .id
560560 if self ._pending_task .get (sid , None ):
561561 # pending task cancelled
562562 pass
@@ -566,14 +566,14 @@ def done(f: OpFuture[object]) -> None:
566566 _ = task .get_loop ().call_soon (task .cancel )
567567
568568 fut .add_done_callback (done )
569- sid = encode_id ( id_ )
569+ sid = id_
570570 if self ._running :
571571 self ._tasks [sid ] = (asyncio .create_task (cb ()), op .return_type )
572572 else :
573573 self ._pending_task [sid ] = (cb , op .return_type )
574574
575575 case StreamCreate ():
576- stream_id = encode_id ( id_ )
576+ stream_id = id_
577577
578578 # Determine which observer to use
579579 ob = [op .observer ] if op .observer else []
@@ -597,7 +597,7 @@ def done(f: OpFuture[object]) -> None:
597597 case StreamEmit ():
598598 stream_emit_entry : StreamEmitEntry = {
599599 "ts" : self .now (),
600- "id" : encode_id ( id_ ) ,
600+ "id" : id_ ,
601601 "stream_id" : op .stream_id ,
602602 "type" : "stream/emit" ,
603603 "value" : self ._codec .encode_json (op .value ),
@@ -607,7 +607,7 @@ def done(f: OpFuture[object]) -> None:
607607 if op .exception :
608608 stream_close_entry_err : StreamCompleteEntry = {
609609 "ts" : self .now (),
610- "id" : encode_id ( id_ ) ,
610+ "id" : id_ ,
611611 "stream_id" : op .stream_id ,
612612 "type" : "stream/complete" ,
613613 "error" : _encode_error (op .exception ),
@@ -616,27 +616,27 @@ def done(f: OpFuture[object]) -> None:
616616 else :
617617 stream_close_entry : StreamCompleteEntry = {
618618 "ts" : self .now (),
619- "id" : encode_id ( id_ ) ,
619+ "id" : id_ ,
620620 "stream_id" : op .stream_id ,
621621 "type" : "stream/complete" ,
622622 }
623623 await self .enqueue_log (stream_close_entry )
624624 case Barrier ():
625625 barrier_entry : BarrierEntry = {
626626 "ts" : self .now (),
627- "id" : encode_id ( id_ ) ,
627+ "id" : id_ ,
628628 "type" : "barrier" ,
629629 }
630630 await self .enqueue_log (barrier_entry , flush = True )
631631 case ExternalPromiseCreate ():
632632 promise_create_entry = {
633633 "ts" : self .now (),
634- "id" : encode_id ( id_ ) ,
634+ "id" : id_ ,
635635 "type" : "promise/create" ,
636636 }
637637 if op .metadata :
638638 promise_create_entry ["metadata" ] = op .metadata
639- self ._tasks [encode_id ( id_ ) ] = (asyncio .Future (), op .return_type )
639+ self ._tasks [id_ ] = (asyncio .Future (), op .return_type )
640640 await self .enqueue_log (promise_create_entry )
641641 case _:
642642 assert_never (op )
@@ -654,7 +654,7 @@ async def complete_external_promise(
654654 now_us = self .now ()
655655 entry : PromiseCompleteEntry = {
656656 "ts" : now_us ,
657- "id" : encode_id ( derive_id (decode_id ( id_ )) ),
657+ "id" : derive_id (id_ ),
658658 "type" : "promise/complete" ,
659659 "promise_id" : id_ ,
660660 }
@@ -678,7 +678,7 @@ async def send_stream(
678678 if predicate (md or {}):
679679 entry : StreamEmitEntry = {
680680 "ts" : ts ,
681- "id" : _generate_id (),
681+ "id" : random_id (),
682682 "type" : "stream/emit" ,
683683 "stream_id" : stream_id ,
684684 "value" : self ._codec .encode_json (value ),
@@ -705,15 +705,15 @@ async def close_stream(
705705 if error :
706706 entry : StreamCompleteEntry = {
707707 "ts" : ts ,
708- "id" : _generate_id (),
708+ "id" : random_id (),
709709 "type" : "stream/complete" ,
710710 "stream_id" : stream_id ,
711711 "error" : _encode_error (error ),
712712 }
713713 else :
714714 entry = {
715715 "ts" : ts ,
716- "id" : _generate_id (),
716+ "id" : random_id (),
717717 "type" : "stream/complete" ,
718718 "stream_id" : stream_id ,
719719 }
@@ -722,10 +722,6 @@ async def close_stream(
722722 return cnt
723723
724724
725- def _generate_id () -> str :
726- return encode_id (random_id ())
727-
728-
729725def _encode_error (error : BaseException ) -> ErrorInfo :
730726 if type (error ) is asyncio .CancelledError :
731727 return {
0 commit comments