cu-python-task 1.0.0-rc1

Python-backed CuTask implementations for Copper
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
"""Bootstrap helpers for ``cu_python_task``.

This module is used by both Python backends:

- embedded mode imports these helpers directly through PyO3
- process mode executes this file in a child interpreter and speaks a small
  length-prefixed CBOR protocol over stdin/stdout

The bootstrap layer exposes Copper messages, task state, and outputs as mutable
attribute-style Python objects. That makes the user-facing API convenient, but
it does not make it cheap: values are still copied and allocated around every
call.

This is a prototyping bridge, not a realtime integration mechanism.
"""

import importlib.util
import inspect
import mmap
import struct
import sys
import traceback
from collections.abc import Mapping
from dataclasses import dataclass


_SHM_FILE_CACHE = {}
_SHM_MMAP_CACHE = {}
_SHM_ELEMENT_SIZES = {
    "u8": 1,
    "u16": 2,
    "u32": 4,
    "u64": 8,
    "i8": 1,
    "i16": 2,
    "i32": 4,
    "i64": 8,
    "f32": 4,
    "f64": 8,
}
_SHM_NUMPY_DTYPES = {
    "u8": "u1",
    "u16": "u2",
    "u32": "u4",
    "u64": "u8",
    "i8": "i1",
    "i16": "i2",
    "i32": "i4",
    "i64": "i8",
    "f32": "f4",
    "f64": "f8",
}


def _is_shm_handle_descriptor(value):
    return isinstance(value, Mapping) and value.get("__cu_shm_handle__") is True


class SharedMemoryHandle:
    __slots__ = ("_descriptor",)

    def __init__(self, descriptor):
        self._descriptor = dict(descriptor)

    @property
    def path(self):
        return self._descriptor["path"]

    @property
    def offset_bytes(self):
        return self._descriptor["offset_bytes"]

    @property
    def len_elements(self):
        return self._descriptor["len_elements"]

    @property
    def element_type(self):
        return self._descriptor["element_type"]

    @property
    def byte_len(self):
        return self.len_elements * _SHM_ELEMENT_SIZES[self.element_type]

    def descriptor(self):
        return dict(self._descriptor)

    def _mmap(self):
        cached = _SHM_MMAP_CACHE.get(self.path)
        if cached is not None:
            return cached

        file_obj = open(self.path, "r+b", buffering=0)
        mapped = mmap.mmap(file_obj.fileno(), 0)
        _SHM_FILE_CACHE[self.path] = file_obj
        _SHM_MMAP_CACHE[self.path] = mapped
        return mapped

    def memoryview(self):
        start = self.offset_bytes
        end = start + self.byte_len
        return memoryview(self._mmap())[start:end]

    def numpy(self, shape=None):
        try:
            import numpy as np
        except ImportError as exc:
            raise RuntimeError("numpy is required to expose shared-memory handles as ndarrays") from exc

        dtype = np.dtype(_SHM_NUMPY_DTYPES[self.element_type])
        if shape is None:
            shape = (self.len_elements,)
        expected = 1
        for dim in shape:
            expected *= dim
        if expected != self.len_elements:
            raise ValueError(f"shape {shape!r} does not match len_elements={self.len_elements}")
        return np.ndarray(shape=shape, dtype=dtype, buffer=self._mmap(), offset=self.offset_bytes)

    def __len__(self):
        return self.len_elements


class AttrDict(dict):
    def __init__(self, *args, on_mutate=None, **kwargs):
        object.__setattr__(self, "_cu_on_mutate", on_mutate)
        super().__init__()
        if args or kwargs:
            items = dict(*args, **kwargs)
            for key, value in items.items():
                super().__setitem__(key, _wrap(value, on_mutate=on_mutate))

    def _cu_mark_mutated(self):
        callback = object.__getattribute__(self, "_cu_on_mutate")
        if callback is not None:
            callback()

    def __getattr__(self, name):
        try:
            return self[name]
        except KeyError as exc:
            raise AttributeError(name) from exc

    def __setattr__(self, name, value):
        if name.startswith("_cu_"):
            object.__setattr__(self, name, value)
            return
        self[name] = value

    def __delattr__(self, name):
        if name.startswith("_cu_"):
            object.__delattr__(self, name)
            return
        try:
            del self[name]
        except KeyError as exc:
            raise AttributeError(name) from exc

    def __setitem__(self, key, value):
        self._cu_mark_mutated()
        super().__setitem__(
            key, _wrap(value, on_mutate=object.__getattribute__(self, "_cu_on_mutate"))
        )

    def __delitem__(self, key):
        self._cu_mark_mutated()
        super().__delitem__(key)

    def clear(self):
        if self:
            self._cu_mark_mutated()
        super().clear()

    def pop(self, key, *args):
        if key in self:
            self._cu_mark_mutated()
        return super().pop(key, *args)

    def popitem(self):
        self._cu_mark_mutated()
        return super().popitem()

    def setdefault(self, key, default=None):
        if key in self:
            return super().setdefault(key)
        self._cu_mark_mutated()
        return super().setdefault(
            key, _wrap(default, on_mutate=object.__getattribute__(self, "_cu_on_mutate"))
        )

    def update(self, *args, **kwargs):
        items = dict(*args, **kwargs)
        for key, value in items.items():
            self[key] = value


class AttrList(list):
    def __init__(self, iterable=(), on_mutate=None):
        object.__setattr__(self, "_cu_on_mutate", on_mutate)
        super().__init__(_wrap(item, on_mutate=on_mutate) for item in iterable)

    def _cu_mark_mutated(self):
        callback = object.__getattribute__(self, "_cu_on_mutate")
        if callback is not None:
            callback()

    def __setitem__(self, index, value):
        self._cu_mark_mutated()
        if isinstance(index, slice):
            super().__setitem__(
                index,
                [_wrap(item, on_mutate=object.__getattribute__(self, "_cu_on_mutate")) for item in value],
            )
        else:
            super().__setitem__(
                index, _wrap(value, on_mutate=object.__getattribute__(self, "_cu_on_mutate"))
            )

    def __delitem__(self, index):
        self._cu_mark_mutated()
        super().__delitem__(index)

    def append(self, value):
        self._cu_mark_mutated()
        super().append(_wrap(value, on_mutate=object.__getattribute__(self, "_cu_on_mutate")))

    def extend(self, values):
        self._cu_mark_mutated()
        super().extend(
            _wrap(value, on_mutate=object.__getattribute__(self, "_cu_on_mutate"))
            for value in values
        )

    def insert(self, index, value):
        self._cu_mark_mutated()
        super().insert(index, _wrap(value, on_mutate=object.__getattribute__(self, "_cu_on_mutate")))

    def pop(self, index=-1):
        self._cu_mark_mutated()
        return super().pop(index)

    def remove(self, value):
        self._cu_mark_mutated()
        super().remove(value)

    def clear(self):
        if self:
            self._cu_mark_mutated()
        super().clear()


class MessageDict(AttrDict):
    def __init__(self, mapping):
        payload_present = mapping.get("__cu_payload_present__", mapping.get("payload") is not None)
        payload_template = mapping.get("__cu_payload_template__")
        clean = {
            key: value
            for key, value in mapping.items()
            if key not in {"__cu_payload_present__", "__cu_payload_template__"}
        }
        super().__init__()
        object.__setattr__(self, "_cu_payload_present", payload_present)
        object.__setattr__(self, "_cu_payload_touched", False)
        object.__setattr__(self, "_cu_payload_template", payload_template)
        for key, value in clean.items():
            if key == "payload" and value is None:
                dict.__setitem__(self, key, None)
            else:
                callback = self._cu_mark_payload_touched if key == "payload" else None
                dict.__setitem__(self, key, _wrap(value, on_mutate=callback))

    def _cu_mark_payload_touched(self):
        object.__setattr__(self, "_cu_payload_touched", True)

    def _cu_materialize_payload(self):
        payload = dict.get(self, "payload")
        if payload is None:
            template = object.__getattribute__(self, "_cu_payload_template")
            if template is None:
                return None
            payload = _wrap(template, on_mutate=self._cu_mark_payload_touched)
            dict.__setitem__(self, "payload", payload)
        return payload

    def __getattr__(self, name):
        if name == "payload":
            return self._cu_materialize_payload()
        return super().__getattr__(name)

    def __getitem__(self, key):
        if key == "payload":
            return self._cu_materialize_payload()
        return super().__getitem__(key)

    def get(self, key, default=None):
        if key == "payload":
            payload = self._cu_materialize_payload()
            return default if payload is None else payload
        return super().get(key, default)

    def __setattr__(self, name, value):
        if name.startswith("_cu_"):
            object.__setattr__(self, name, value)
            return
        if name == "payload":
            self._cu_mark_payload_touched()
            dict.__setitem__(self, "payload", _wrap(value, on_mutate=self._cu_mark_payload_touched))
            return
        super().__setattr__(name, value)

    def __setitem__(self, key, value):
        if key == "payload":
            self._cu_mark_payload_touched()
            dict.__setitem__(self, "payload", _wrap(value, on_mutate=self._cu_mark_payload_touched))
            return
        super().__setitem__(key, value)


class ContextSnapshot:
    __slots__ = ("_cu_now_ns", "_cu_recent_ns", "cl_id", "task_id", "task_index")

    def __init__(self, now_ns, recent_ns, cl_id, task_id=None, task_index=None):
        self._cu_now_ns = now_ns
        self._cu_recent_ns = recent_ns
        self.cl_id = cl_id
        self.task_id = task_id
        self.task_index = task_index

    @classmethod
    def from_mapping(cls, mapping):
        return cls(
            now_ns=mapping["now_ns"],
            recent_ns=mapping["recent_ns"],
            cl_id=mapping["cl_id"],
            task_id=mapping.get("task_id"),
            task_index=mapping.get("task_index"),
        )

    @property
    def now_ns(self):
        return self._cu_now_ns

    @property
    def recent_ns(self):
        return self._cu_recent_ns

    def now(self):
        return self._cu_now_ns

    def recent(self):
        return self._cu_recent_ns


def _wrap(value, on_mutate=None, output_mode=False):
    if isinstance(value, SharedMemoryHandle):
        return value
    if isinstance(value, (MessageDict, AttrDict, AttrList)):
        return value
    if _is_shm_handle_descriptor(value):
        return SharedMemoryHandle(value)
    if isinstance(value, Mapping):
        if output_mode and (
            "__cu_payload_present__" in value or "__cu_payload_template__" in value
        ):
            return MessageDict(value)
        return AttrDict(
            {
                key: _wrap(item, on_mutate=on_mutate, output_mode=output_mode)
                for key, item in value.items()
            },
            on_mutate=on_mutate,
        )
    if isinstance(value, list):
        return AttrList(
            [_wrap(item, on_mutate=on_mutate, output_mode=output_mode) for item in value],
            on_mutate=on_mutate,
        )
    return value


def _unwrap(value):
    if isinstance(value, SharedMemoryHandle):
        return value.descriptor()
    if isinstance(value, MessageDict):
        result = {key: _unwrap(item) for key, item in value.items() if key != "payload"}
        if object.__getattribute__(value, "_cu_payload_present") or object.__getattribute__(
            value, "_cu_payload_touched"
        ):
            result["payload"] = _unwrap(dict.get(value, "payload"))
        else:
            result["payload"] = None
        return result
    if isinstance(value, AttrDict):
        return {key: _unwrap(item) for key, item in value.items()}
    if isinstance(value, AttrList):
        return [_unwrap(item) for item in value]
    if isinstance(value, dict):
        return {key: _unwrap(item) for key, item in value.items()}
    if isinstance(value, (list, tuple)):
        return [_unwrap(item) for item in value]
    return value


def _wrap_context(value):
    if isinstance(value, ContextSnapshot):
        return value
    if isinstance(value, Mapping):
        return ContextSnapshot.from_mapping(value)
    return value


@dataclass(slots=True, frozen=True)
class ProcessRequest:
    ctx: object
    input: object
    state: object
    output: object


@dataclass(slots=True, frozen=True)
class StartRequest:
    ctx: object
    state: object


@dataclass(slots=True, frozen=True)
class StopRequest:
    ctx: object
    state: object


@dataclass(slots=True, frozen=True)
class ShutdownRequest:
    pass


@dataclass(slots=True, frozen=True)
class ReadyResponse:
    cbor2_accelerated: bool


@dataclass(slots=True, frozen=True)
class StateResultResponse:
    state: object

    @classmethod
    def from_state_result(cls, value):
        if not isinstance(value, Mapping):
            raise TypeError(f"Unsupported state result type: {type(value).__name__}")
        try:
            return cls(state=value["state"])
        except KeyError as exc:
            raise RuntimeError(f"State result is missing field {exc.args[0]!r}") from exc


@dataclass(slots=True, frozen=True)
class ProcessResultResponse:
    state: object
    output: object

    @classmethod
    def from_process_result(cls, value):
        if not isinstance(value, Mapping):
            raise TypeError(f"Unsupported process result type: {type(value).__name__}")
        try:
            return cls(state=value["state"], output=value["output"])
        except KeyError as exc:
            raise RuntimeError(f"Process result is missing field {exc.args[0]!r}") from exc


@dataclass(slots=True, frozen=True)
class ErrorResponse:
    message: str


def _decode_request(value):
    if not isinstance(value, Mapping):
        raise RuntimeError(f"Expected request mapping, got {type(value).__name__}")

    kind = value.get("kind")
    if kind == "start":
        return _coerce_state_request(value, StartRequest)
    if kind == "stop":
        return _coerce_state_request(value, StopRequest)
    if kind == "process":
        return _coerce_process_request(value)
    if kind == "shutdown":
        return ShutdownRequest()

    raise RuntimeError(f"Unsupported request kind: {kind!r}")


def _encode_response(value):
    if isinstance(value, ReadyResponse):
        return {"kind": "ready", "cbor2_accelerated": value.cbor2_accelerated}
    if isinstance(value, StateResultResponse):
        return {"kind": "state", "state": value.state}
    if isinstance(value, ProcessResultResponse):
        return {"kind": "result", "state": value.state, "output": value.output}
    if isinstance(value, ErrorResponse):
        return {"kind": "error", "message": value.message}
    raise TypeError(f"Unsupported response type: {type(value).__name__}")


def _coerce_state_request(value, request_type):
    if isinstance(value, request_type):
        return value
    if isinstance(value, Mapping):
        try:
            return request_type(
                ctx=value["ctx"],
                state=value["state"],
            )
        except KeyError as exc:
            raise RuntimeError(f"State request is missing field {exc.args[0]!r}") from exc
    raise TypeError(f"Unsupported state request type: {type(value).__name__}")


def _coerce_process_request(value):
    if isinstance(value, ProcessRequest):
        return value
    if isinstance(value, Mapping):
        try:
            return ProcessRequest(
                ctx=value["ctx"],
                input=value["input"],
                state=value["state"],
                output=value["output"],
            )
        except KeyError as exc:
            raise RuntimeError(f"Process request is missing field {exc.args[0]!r}") from exc
    raise TypeError(f"Unsupported process request type: {type(value).__name__}")


def _validate_callable_signature(callable_obj, min_positional_args, script_path, signature):
    positional = _inspect_callable_signature(callable_obj)
    if positional is not None and len(positional) < min_positional_args:
        raise RuntimeError(f"Python task script {script_path!r} must define {signature}")
    return callable_obj


def load_task_functions(script_path):
    """Load the user script and return its process/start/stop callables."""
    spec = importlib.util.spec_from_file_location("cu_python_task_user", script_path)
    if spec is None or spec.loader is None:
        raise RuntimeError(f"Could not load Python task script from {script_path!r}")

    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)

    process_fn = getattr(module, "process", None)
    if process_fn is None or not callable(process_fn):
        raise RuntimeError(
            f"Python task script {script_path!r} must define a callable process(ctx, input, state, output)"
        )
    process_fn = _validate_callable_signature(
        process_fn,
        4,
        script_path,
        "process(ctx, input, state, output)",
    )

    start_fn = getattr(module, "start", None)
    if start_fn is not None:
        if not callable(start_fn):
            raise RuntimeError(
                f"Python task script {script_path!r} defines non-callable start; expected start(ctx, state)"
            )
        start_fn = _validate_callable_signature(
            start_fn,
            2,
            script_path,
            "optional start(ctx, state)",
        )

    stop_fn = getattr(module, "stop", None)
    if stop_fn is not None:
        if not callable(stop_fn):
            raise RuntimeError(
                f"Python task script {script_path!r} defines non-callable stop; expected stop(ctx, state)"
            )
        stop_fn = _validate_callable_signature(
            stop_fn,
            2,
            script_path,
            "optional stop(ctx, state)",
        )

    return process_fn, start_fn, stop_fn


def _inspect_callable_signature(callable_obj):
    try:
        signature = inspect.signature(callable_obj)
    except (TypeError, ValueError):
        return None

    positional = [
        param.name
        for param in signature.parameters.values()
        if param.kind in (inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD)
    ]
    return positional


def _call_and_capture_final_locals(callable_obj, *args):
    captured = {}
    active_frame = None

    def profiler(frame, event, arg):
        nonlocal active_frame
        if active_frame is None and event == "call":
            active_frame = frame
            return profiler
        if active_frame is frame and event == "return":
            captured.update(frame.f_locals)
        return profiler

    previous_profiler = sys.getprofile()
    sys.setprofile(profiler)
    try:
        callable_obj(*args)
    finally:
        sys.setprofile(previous_profiler)

    return captured


def _call_and_capture_final_bindings(process_fn, positional, *args):
    """Call ``process_fn`` and capture the final locals of its top-level frame."""
    captured = _call_and_capture_final_locals(process_fn, *args)
    return {
        "state": captured.get(positional[2], args[2]),
        "output": captured.get(positional[3], args[3]),
    }


def _call_and_capture_final_state(hook_fn, positional, *args):
    captured = _call_and_capture_final_locals(hook_fn, *args)
    return captured.get(positional[1], args[1])


def call_process(process_fn, request, live_ctx=None):
    """Invoke the user ``process`` function and unwrap the mutated state/output values."""
    request = _coerce_process_request(request)
    positional = _inspect_callable_signature(process_fn)
    ctx_value = live_ctx if live_ctx is not None else _wrap_context(request.ctx)
    input_value = _wrap(request.input)
    state_value = _wrap(request.state)
    output_value = _wrap(request.output, output_mode=True)
    if positional is not None:
        final_bindings = _call_and_capture_final_bindings(
            process_fn, positional, ctx_value, input_value, state_value, output_value
        )
        state_value = final_bindings["state"]
        output_value = final_bindings["output"]
    else:
        process_fn(ctx_value, input_value, state_value, output_value)
    return {
        "state": _unwrap(state_value),
        "output": _unwrap(output_value),
    }


def call_optional_state_hook(hook_fn, request, live_ctx=None):
    """Invoke an optional ``start``/``stop`` hook and unwrap the resulting state."""
    if not isinstance(request, (StartRequest, StopRequest)):
        request = _coerce_state_request(request, StartRequest)
    ctx_value = live_ctx if live_ctx is not None else _wrap_context(request.ctx)
    state_value = _wrap(request.state)

    if hook_fn is not None:
        positional = _inspect_callable_signature(hook_fn)
        if positional is not None:
            state_value = _call_and_capture_final_state(hook_fn, positional, ctx_value, state_value)
        else:
            hook_fn(ctx_value, state_value)

    return {"state": _unwrap(state_value)}


def _load_cbor2():
    try:
        import cbor2
    except ImportError as exc:
        raise RuntimeError(
            "cu_python_task process mode requires the Python package `cbor2`"
        ) from exc

    accelerated = (
        getattr(cbor2.loads, "__module__", "") == "_cbor2"
        and getattr(cbor2.dumps, "__module__", "") == "_cbor2"
    )
    return cbor2, accelerated


def _read_exact(reader, length):
    data = reader.read(length)
    if len(data) != length:
        raise EOFError("Incomplete frame payload")
    return data


def _read_frame(reader):
    header = reader.read(4)
    if not header:
        return None
    if len(header) != 4:
        raise EOFError("Incomplete frame header")

    (length,) = struct.unpack("<I", header)
    return _read_exact(reader, length)


def _write_frame(writer, payload):
    writer.write(struct.pack("<I", len(payload)))
    writer.write(payload)
    writer.flush()


def _write_cbor(writer, cbor2, value):
    _write_frame(writer, cbor2.dumps(_encode_response(value)))


def main():
    """Run the process-mode request loop."""
    if len(sys.argv) != 2:
        raise RuntimeError("Expected script path argument")

    script_path = sys.argv[1]
    reader = sys.stdin.buffer
    writer = sys.stdout.buffer

    cbor2 = None
    try:
        cbor2, accelerated = _load_cbor2()
        process_fn, start_fn, stop_fn = load_task_functions(script_path)
        _write_cbor(writer, cbor2, ReadyResponse(cbor2_accelerated=accelerated))
    except Exception:
        if cbor2 is not None:
            _write_cbor(writer, cbor2, ErrorResponse(message=traceback.format_exc()))
        else:
            traceback.print_exc()
        return 1

    while True:
        try:
            frame = _read_frame(reader)
            if frame is None:
                return 0

            request = _decode_request(cbor2.loads(frame))

            if isinstance(request, ShutdownRequest):
                return 0

            if isinstance(request, StartRequest):
                response = StateResultResponse.from_state_result(
                    call_optional_state_hook(start_fn, request)
                )
            elif isinstance(request, StopRequest):
                response = StateResultResponse.from_state_result(
                    call_optional_state_hook(stop_fn, request)
                )
            else:
                response = ProcessResultResponse.from_process_result(
                    call_process(process_fn, request)
                )
            _write_cbor(writer, cbor2, response)
        except Exception:
            _write_cbor(writer, cbor2, ErrorResponse(message=traceback.format_exc()))


if __name__ == "__main__":
    raise SystemExit(main())