codecraftai-python-rpc 1.3.0__tar.gz → 1.4.0__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.
Files changed (54) hide show
  1. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/PKG-INFO +2 -2
  2. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/pyproject.toml +2 -18
  3. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/session/base.py +15 -1
  4. codecraftai_python_rpc-1.4.0/source/packages/ccai/rpc/core/session/server.py +129 -0
  5. codecraftai_python_rpc-1.3.0/source/packages/ccai/rpc/core/session/server.py +0 -68
  6. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/LICENSE.md +0 -0
  7. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/README.rst +0 -0
  8. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/__init__.py +0 -0
  9. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/aspects.py +0 -0
  10. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/__init__.py +0 -0
  11. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/dispatch/__init__.py +0 -0
  12. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/dispatch/context.py +0 -0
  13. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/dispatch/lanes.py +0 -0
  14. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/flattenable.py +0 -0
  15. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/futures.py +0 -0
  16. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/leases.py +0 -0
  17. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/looper.py +0 -0
  18. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/looperpool.py +0 -0
  19. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/looperqueue.py +0 -0
  20. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/observability/__init__.py +0 -0
  21. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/observability/metrics.py +0 -0
  22. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/observability/state.py +0 -0
  23. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/protocol/__init__.py +0 -0
  24. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/protocol/constants.py +0 -0
  25. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/protocol/header.py +0 -0
  26. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/protocol/messages.py +0 -0
  27. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/proxy/__init__.py +0 -0
  28. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/proxy/cache.py +0 -0
  29. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/proxy/handlers.py +0 -0
  30. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/proxy/remoteproxy.py +0 -0
  31. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/proxy/table.py +0 -0
  32. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/proxy/wrappers.py +0 -0
  33. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/serialization/__init__.py +0 -0
  34. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/serialization/codec.py +0 -0
  35. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/serialization/placeholder.py +0 -0
  36. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/serialization/registry.py +0 -0
  37. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/serialization/tags.py +0 -0
  38. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/session/__init__.py +0 -0
  39. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/session/client.py +0 -0
  40. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/session/dispatch.py +0 -0
  41. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/session/heartbeat.py +0 -0
  42. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/transport/__init__.py +0 -0
  43. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/transport/base.py +0 -0
  44. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/transport/faults.py +0 -0
  45. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/transport/inprocess.py +0 -0
  46. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/core/transport/socketio.py +0 -0
  47. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/endpoints.py +0 -0
  48. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/errors.py +0 -0
  49. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/py.typed +0 -0
  50. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/rpcsocketclient.py +0 -0
  51. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/rpcsocketserver.py +0 -0
  52. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/security/__init__.py +0 -0
  53. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/security/auth.py +0 -0
  54. {codecraftai_python_rpc-1.3.0 → codecraftai_python_rpc-1.4.0}/source/packages/ccai/rpc/security/tls.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: codecraftai-python-rpc
3
- Version: 1.3.0
3
+ Version: 1.4.0
4
4
  Summary: Code Craft AI - Python RPC Package
5
5
  License-Expression: LicenseRef-CodeCraftAI-Source-Available
6
6
  License-File: LICENSE.md
@@ -10,7 +10,7 @@ Author-email: myron.walker@gmail.com
10
10
  Requires-Python: >=3.10,<4.0
11
11
  Classifier: Programming Language :: Python :: 3
12
12
  Classifier: Operating System :: POSIX
13
- Requires-Dist: codecraftai-foundations (<2.0.0)
13
+ Requires-Dist: codecraftai-foundations (>=1.0.0,<2.0.0)
14
14
  Project-URL: Issues, https://github.com/code-craft-ai-company/codecraftai-python-rpc/issues
15
15
  Project-URL: Repository, https://github.com/code-craft-ai-company/codecraftai-python-rpc
16
16
  Description-Content-Type: text/x-rst
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "codecraftai-python-rpc"
3
- version = "1.3.0"
3
+ version = "1.4.0"
4
4
  description = "Code Craft AI - Python RPC Package"
5
5
  readme = "README.rst"
6
6
  license = "LicenseRef-CodeCraftAI-Source-Available"
@@ -14,7 +14,7 @@ classifiers = [
14
14
  ]
15
15
  requires-python = ">=3.10,<4.0"
16
16
  dependencies = [
17
- "codecraftai-foundations<2.0.0",
17
+ "codecraftai-foundations>=1.0.0,<2.0.0",
18
18
  ]
19
19
 
20
20
  [project.urls]
@@ -27,22 +27,6 @@ dev = []
27
27
  [tool.poetry]
28
28
  packages = [{include="ccai", from="source/packages"}]
29
29
 
30
- [tool.poetry.dependencies]
31
- codecraftai-foundations = {source = "codecraftai"}
32
-
33
- [[tool.poetry.source]]
34
- name = "codecraftai"
35
- url = "https://code-craft.ai/licensed-repository/simple/"
36
- priority = "supplemental"
37
-
38
- [[tool.uv.index]]
39
- # Same licensed index as [[tool.poetry.source]]. Listed before PyPI so that
40
- # codecraftai-* packages (direct and transitive) resolve from it; everything
41
- # else falls through to PyPI. Credentials: UV_INDEX_CODECRAFTAI_USERNAME /
42
- # UV_INDEX_CODECRAFTAI_PASSWORD, or `uv auth login`.
43
- name = "codecraftai"
44
- url = "https://code-craft.ai/licensed-repository/simple/"
45
-
46
30
  [tool.ruff.lint]
47
31
  # Deliberately narrow. This gate exists for one reason: a bare `except:`
48
32
  # swallows ConfigurationError and SemanticError, which derive from
@@ -150,6 +150,7 @@ class DuplexSessionBase:
150
150
  self._running = False
151
151
  self._closing = False
152
152
  self._closed = False
153
+ self._receive_stopped = False # set when no further inbound frame may be read
153
154
  self._closed_event = threading.Event()
154
155
  self._close_reason: Optional[BaseException] = None
155
156
  self._waiters: Dict[uuid.UUID, Waiter] = {}
@@ -594,7 +595,7 @@ class DuplexSessionBase:
594
595
  def _receive_loop(self, gate: threading.Event) -> None:
595
596
  gate.set()
596
597
  reason = None
597
- while self._running and not self._closing:
598
+ while self._running and not self._closing and not self._receive_stopped:
598
599
  try:
599
600
  frame = self._transport.receive_frame()
600
601
  except TimeoutError:
@@ -642,9 +643,22 @@ class DuplexSessionBase:
642
643
  if header.mcode == RequestCode.HELLO:
643
644
  self._handle_hello_frame(frame, header)
644
645
  return
646
+ if not self._admit_request(header):
647
+ return
645
648
  self._pool.push_work(frame)
646
649
  return
647
650
 
651
+ def _admit_request(self, header: MessageHeader) -> bool:
652
+ """
653
+ Decides, on the receive thread, whether a request other than HELLO or PING may be dispatched. The
654
+ base session admits every request; the server session admits requests only after a completed HELLO.
655
+
656
+ :param header: The header of the inbound request frame.
657
+
658
+ :returns: True when the frame may be handed to the dispatch pool, False when it was refused.
659
+ """
660
+ return True
661
+
648
662
  def _handle_hello_frame(self, frame: bytes, header: MessageHeader) -> None:
649
663
  raise ProtocolError("unexpected HELLO on a {} session".format(self.role))
650
664
 
@@ -0,0 +1,129 @@
1
+ """
2
+ .. module:: server
3
+ :platform: Darwin, Linux, Unix, Windows
4
+ :synopsis: DuplexServerSession: the session that answers HELLO, authenticating the peer first.
5
+
6
+ .. moduleauthor:: Myron Walker <myron.walker@gmail.com>
7
+ """
8
+
9
+ __author__ = "Myron Walker"
10
+ __copyright__ = "Copyright 2025, Code Craft AI LLC"
11
+ __credits__ = []
12
+
13
+ import threading
14
+ import time
15
+
16
+ from ccai.rpc.errors import ProtocolError, UnauthorizedError
17
+ from ccai.rpc.core.protocol.constants import MIN_SUPPORTED_VERSION, PROTOCOL_VERSION, ExceptionCode, RequestCode
18
+ from ccai.rpc.core.protocol.header import MessageHeader
19
+ from ccai.rpc.core.protocol.messages import ExceptionMessage, Message, ReplyHello
20
+ from ccai.rpc.core.session.base import DuplexSessionBase
21
+
22
+ # How long a refused session waits for its exception reply to leave the outbound queue before the transport closes.
23
+ REFUSAL_FLUSH_SECONDS = 0.5
24
+
25
+
26
+ class DuplexServerSession(DuplexSessionBase):
27
+ """
28
+ The server end of a connection. No request other than HELLO or PING is dispatched until the peer has
29
+ completed HELLO (and passed the authenticator, when one is configured). A request before HELLO, a rejected
30
+ credential, a failed version negotiation or a repeated HELLO is answered with an exception reply, and the
31
+ session stops reading frames and tears itself down.
32
+ """
33
+
34
+ role = "server"
35
+
36
+ def __init__(self, *args, on_hello=None, **kwargs) -> None:
37
+ super().__init__(*args, **kwargs)
38
+ self._on_hello = on_hello
39
+ self.hello_done = threading.Event()
40
+ return
41
+
42
+ def _admit_request(self, header: MessageHeader) -> bool:
43
+ """
44
+ Admits a request only after a completed HELLO; otherwise answers it with UNAUTHORIZED and closes the
45
+ session.
46
+
47
+ :param header: The header of the inbound request frame.
48
+
49
+ :returns: True when the frame may be handed to the dispatch pool, False when it was refused.
50
+ """
51
+ if self.hello_done.is_set():
52
+ return True
53
+ code_name = RequestCode(header.mcode).name if header.mcode in RequestCode._value2member_map_ else str(header.mcode)
54
+ self.logger.warning("refusing %s from %s: the peer has not completed HELLO", code_name, self.remote_address)
55
+ self._refuse_and_close(header, ExceptionCode.UNAUTHORIZED, "UnauthorizedError",
56
+ "HELLO must complete before {}".format(code_name),
57
+ UnauthorizedError("peer {} sent {} before completing HELLO".format(self.remote_address, code_name)))
58
+ return False
59
+
60
+ def _refuse_and_close(self, header: MessageHeader, code: ExceptionCode, type_name: str, text: str, reason: BaseException) -> None:
61
+ """
62
+ Stops the receive loop before it reads another frame, answers the offending request with an exception
63
+ reply and tears the session down once that reply has been handed to the transport.
64
+
65
+ :param header: The header of the refused request, whose correlation id the exception reply carries.
66
+ :param code: The exception code of the reply.
67
+ :param type_name: The exception type name carried in the reply.
68
+ :param text: The exception message carried in the reply.
69
+ :param reason: The close reason recorded by the teardown.
70
+ """
71
+ self._receive_stopped = True
72
+ self.send_exception(ExceptionMessage(code=int(code), type_name=type_name, message=text, correlation_id=header.correlation_id))
73
+ threading.Thread(target=self._teardown_after_flush, args=(reason,), name="rpc-{}-refuse-{}".format(self.role, self._short),
74
+ daemon=True).start()
75
+ return
76
+
77
+ def _teardown_after_flush(self, reason: BaseException) -> None:
78
+ """
79
+ Waits, off the receive thread, for the outbound queue to drain (bounded by REFUSAL_FLUSH_SECONDS) so the
80
+ exception reply reaches the peer, then tears the session down.
81
+
82
+ :param reason: The close reason recorded by the teardown.
83
+ """
84
+ deadline = time.monotonic() + REFUSAL_FLUSH_SECONDS
85
+ while self._outbound.depth > 0 and time.monotonic() < deadline:
86
+ time.sleep(0.01)
87
+ time.sleep(0.02) # the transmit thread has popped the last frame; give it time to write it
88
+ self._teardown(reason)
89
+ return
90
+
91
+ def _handle_hello_frame(self, frame: bytes, header) -> None:
92
+ if self.hello_done.is_set():
93
+ self.logger.warning("refusing a repeated HELLO from %s", self.remote_address)
94
+ self._refuse_and_close(header, ExceptionCode.PROTOCOL_ERROR, "ProtocolError", "HELLO was already completed on this session",
95
+ ProtocolError("peer {} sent a second HELLO".format(self.remote_address)))
96
+ return
97
+ message = Message.unpack(frame)
98
+ chosen = min(message.version_max, PROTOCOL_VERSION)
99
+ if chosen < max(message.version_min, MIN_SUPPORTED_VERSION):
100
+ self._refuse_and_close(header, ExceptionCode.PROTOCOL_ERROR, "ProtocolError", "no common protocol version",
101
+ ProtocolError("no common protocol version with peer"))
102
+ return
103
+ if self._authenticator is not None:
104
+ identity = None
105
+ try:
106
+ identity = self._authenticator(message.credential or b"", self.remote_address)
107
+ except Exception as err: # noqa: BLE001
108
+ self.logger.warning("authentication raised for %s: %s", self.remote_address, err)
109
+ if identity is None:
110
+ self.logger.warning("authentication failed for %s", self.remote_address)
111
+ self._refuse_and_close(header, ExceptionCode.UNAUTHORIZED, "UnauthorizedError", "credential rejected",
112
+ UnauthorizedError("peer {} failed authentication".format(self.remote_address)))
113
+ return
114
+ self.peer_identity = identity
115
+ self.logger.info("authenticated %s as %s", self.remote_address, identity)
116
+ elif getattr(self._transport, "peer_identity", None):
117
+ self.peer_identity = getattr(self._transport, "peer_identity")
118
+ self.logger.info("authenticated %s by client certificate as %s", self.remote_address, self.peer_identity)
119
+ self.protocol_version = chosen
120
+ self.peer_session_id = message.session_id
121
+ self.peer_context = dict(message.context or {})
122
+ self.logger.extra["peer_session_id"] = str(self.peer_session_id)
123
+ self.send_reply(ReplyHello(chosen_version=chosen, session_id=self.session_id, lease_duration=self._aspects.lease_duration,
124
+ heartbeat_interval=self._aspects.heartbeat_interval, correlation_id=header.correlation_id))
125
+ self.hello_done.set()
126
+ self.logger.info("HELLO complete: protocol %s, client session %s", chosen, self.peer_session_id)
127
+ if self._on_hello is not None:
128
+ self._pool.push_work(lambda: self._on_hello(self))
129
+ return
@@ -1,68 +0,0 @@
1
- """
2
- .. module:: server
3
- :platform: Darwin, Linux, Unix, Windows
4
- :synopsis: DuplexServerSession: the session that answers HELLO, authenticating the peer first.
5
-
6
- .. moduleauthor:: Myron Walker <myron.walker@gmail.com>
7
- """
8
-
9
- __author__ = "Myron Walker"
10
- __copyright__ = "Copyright 2025, Code Craft AI LLC"
11
- __credits__ = []
12
-
13
- import threading
14
-
15
- from ccai.rpc.errors import ProtocolError, UnauthorizedError
16
- from ccai.rpc.core.protocol.constants import MIN_SUPPORTED_VERSION, PROTOCOL_VERSION, ExceptionCode
17
- from ccai.rpc.core.protocol.messages import ExceptionMessage, Message, ReplyHello
18
- from ccai.rpc.core.session.base import DuplexSessionBase
19
-
20
-
21
- class DuplexServerSession(DuplexSessionBase):
22
-
23
- role = "server"
24
-
25
- def __init__(self, *args, on_hello=None, **kwargs) -> None:
26
- super().__init__(*args, **kwargs)
27
- self._on_hello = on_hello
28
- self.hello_done = threading.Event()
29
- return
30
-
31
- def _handle_hello_frame(self, frame: bytes, header) -> None:
32
- message = Message.unpack(frame)
33
- chosen = min(message.version_max, PROTOCOL_VERSION)
34
- if chosen < max(message.version_min, MIN_SUPPORTED_VERSION):
35
- self.send_exception(ExceptionMessage(code=int(ExceptionCode.PROTOCOL_ERROR), type_name="ProtocolError",
36
- message="no common protocol version", correlation_id=header.correlation_id))
37
- threading.Thread(target=self._teardown, args=(ProtocolError("no common protocol version with peer"),), daemon=True).start()
38
- return
39
- if self._authenticator is not None:
40
- identity = None
41
- try:
42
- identity = self._authenticator(message.credential or b"", self.remote_address)
43
- except Exception as err: # noqa: BLE001
44
- self.logger.warning("authentication raised for %s: %s", self.remote_address, err)
45
- if identity is None:
46
- self.logger.warning("authentication failed for %s", self.remote_address)
47
- self.send_exception(ExceptionMessage(code=int(ExceptionCode.UNAUTHORIZED), type_name="UnauthorizedError",
48
- message="credential rejected", correlation_id=header.correlation_id))
49
- import time
50
- time.sleep(0.05)
51
- threading.Thread(target=self._teardown, args=(UnauthorizedError("peer {} failed authentication".format(self.remote_address)),), daemon=True).start()
52
- return
53
- self.peer_identity = identity
54
- self.logger.info("authenticated %s as %s", self.remote_address, identity)
55
- elif getattr(self._transport, "peer_identity", None):
56
- self.peer_identity = getattr(self._transport, "peer_identity")
57
- self.logger.info("authenticated %s by client certificate as %s", self.remote_address, self.peer_identity)
58
- self.protocol_version = chosen
59
- self.peer_session_id = message.session_id
60
- self.peer_context = dict(message.context or {})
61
- self.logger.extra["peer_session_id"] = str(self.peer_session_id)
62
- self.send_reply(ReplyHello(chosen_version=chosen, session_id=self.session_id, lease_duration=self._aspects.lease_duration,
63
- heartbeat_interval=self._aspects.heartbeat_interval, correlation_id=header.correlation_id))
64
- self.hello_done.set()
65
- self.logger.info("HELLO complete: protocol %s, client session %s", chosen, self.peer_session_id)
66
- if self._on_hello is not None:
67
- self._pool.push_work(lambda: self._on_hello(self))
68
- return