cap-sdk-python 2.8.4__tar.gz → 2.9.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.
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/PKG-INFO +2 -2
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/__init__.py +3 -0
- cap_sdk_python-2.9.0/cap/handshake.py +72 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/heartbeat.py +7 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/alert_pb2_grpc.py +1 -1
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/buspacket_pb2_grpc.py +1 -1
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/handshake_pb2.py +6 -6
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/handshake_pb2_grpc.py +1 -1
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/heartbeat_pb2.py +4 -4
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/heartbeat_pb2_grpc.py +1 -1
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/job_pb2_grpc.py +1 -1
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/safety_pb2_grpc.py +1 -1
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/runtime.py +16 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/testing.py +4 -1
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap_sdk_python.egg-info/PKG-INFO +2 -2
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap_sdk_python.egg-info/SOURCES.txt +2 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap_sdk_python.egg-info/requires.txt +1 -1
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/pyproject.toml +2 -2
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_conformance.py +2 -0
- cap_sdk_python-2.9.0/tests/test_handshake.py +62 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_heartbeat.py +2 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_middleware.py +10 -7
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_runtime.py +10 -7
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/README.md +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/bus.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/client.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/constants.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/errors.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/metrics.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/middleware.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/__init__.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/__init__.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/__init__.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/__init__.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/alert_pb2.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/buspacket_pb2.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/job_pb2.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/pb/cordum/agent/v1/safety_pb2.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/progress.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/subjects.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/validate.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap/worker.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap_sdk_python.egg-info/dependency_links.txt +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/cap_sdk_python.egg-info/top_level.txt +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/setup.cfg +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_errors.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_metrics.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_progress.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_redis_tls.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_sdk.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_testing.py +0 -0
- {cap_sdk_python-2.8.4 → cap_sdk_python-2.9.0}/tests/test_validate.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cap-sdk-python
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.9.0
|
|
4
4
|
Summary: CAP (Cordum Agent Protocol) Python SDK
|
|
5
5
|
Author-email: Cordum <eng@cordum.io>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -10,7 +10,7 @@ Project-URL: Issues, https://github.com/cordum-io/cap/issues
|
|
|
10
10
|
Keywords: cap,cordum,agent,protocol,sdk,ai
|
|
11
11
|
Requires-Python: >=3.9
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
|
-
Requires-Dist: protobuf
|
|
13
|
+
Requires-Dist: protobuf<7.0.0,>=6.31.0
|
|
14
14
|
Requires-Dist: grpcio>=1.59.0
|
|
15
15
|
Requires-Dist: nats-py>=2.6.0
|
|
16
16
|
Requires-Dist: cryptography>=41.0.0
|
|
@@ -42,6 +42,7 @@ from .heartbeat import (
|
|
|
42
42
|
emit_heartbeat,
|
|
43
43
|
heartbeat_loop,
|
|
44
44
|
)
|
|
45
|
+
from .handshake import handshake_payload, publish_handshake
|
|
45
46
|
from .progress import (
|
|
46
47
|
progress_payload,
|
|
47
48
|
cancel_payload,
|
|
@@ -106,6 +107,8 @@ __all__ = [
|
|
|
106
107
|
"heartbeat_payload_with_progress",
|
|
107
108
|
"emit_heartbeat",
|
|
108
109
|
"heartbeat_loop",
|
|
110
|
+
"handshake_payload",
|
|
111
|
+
"publish_handshake",
|
|
109
112
|
"progress_payload",
|
|
110
113
|
"cancel_payload",
|
|
111
114
|
"emit_progress",
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Handshake helpers for CAP Python SDK.
|
|
2
|
+
|
|
3
|
+
These helpers build and publish worker handshake BusPacket envelopes.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Mapping, Optional, Sequence
|
|
7
|
+
|
|
8
|
+
from cryptography.hazmat.primitives import hashes
|
|
9
|
+
from cryptography.hazmat.primitives.asymmetric import ec
|
|
10
|
+
from google.protobuf import timestamp_pb2
|
|
11
|
+
|
|
12
|
+
from cap.constants import DEFAULT_PROTOCOL_VERSION
|
|
13
|
+
from cap.pb.cordum.agent.v1 import buspacket_pb2, handshake_pb2
|
|
14
|
+
from cap.subjects import SUBJECT_HANDSHAKE
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def handshake_payload(
|
|
18
|
+
component_id: str,
|
|
19
|
+
capabilities: Optional[Mapping[str, bool]] = None,
|
|
20
|
+
*,
|
|
21
|
+
sender_id: Optional[str] = None,
|
|
22
|
+
supported_versions: Optional[Sequence[int]] = None,
|
|
23
|
+
ready_topics: Optional[Sequence[str]] = None,
|
|
24
|
+
) -> bytes:
|
|
25
|
+
"""Build a worker handshake payload."""
|
|
26
|
+
ts = timestamp_pb2.Timestamp()
|
|
27
|
+
ts.GetCurrentTime()
|
|
28
|
+
|
|
29
|
+
packet = buspacket_pb2.BusPacket()
|
|
30
|
+
packet.sender_id = sender_id or component_id
|
|
31
|
+
packet.protocol_version = DEFAULT_PROTOCOL_VERSION
|
|
32
|
+
packet.created_at.CopyFrom(ts)
|
|
33
|
+
packet.handshake.CopyFrom(
|
|
34
|
+
handshake_pb2.Handshake(
|
|
35
|
+
component_id=component_id,
|
|
36
|
+
role=handshake_pb2.COMPONENT_ROLE_WORKER,
|
|
37
|
+
supported_versions=list(supported_versions or [DEFAULT_PROTOCOL_VERSION]),
|
|
38
|
+
capabilities=dict(capabilities or {}),
|
|
39
|
+
ready_topics=list(ready_topics or []),
|
|
40
|
+
)
|
|
41
|
+
)
|
|
42
|
+
return packet.SerializeToString(deterministic=True)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
async def publish_handshake(
|
|
46
|
+
nc,
|
|
47
|
+
component_id: str,
|
|
48
|
+
capabilities: Optional[Mapping[str, bool]] = None,
|
|
49
|
+
*,
|
|
50
|
+
private_key: Optional[ec.EllipticCurvePrivateKey] = None,
|
|
51
|
+
sender_id: Optional[str] = None,
|
|
52
|
+
supported_versions: Optional[Sequence[int]] = None,
|
|
53
|
+
ready_topics: Optional[Sequence[str]] = None,
|
|
54
|
+
) -> None:
|
|
55
|
+
"""Publish one worker handshake packet to the CAP handshake subject."""
|
|
56
|
+
payload = handshake_payload(
|
|
57
|
+
component_id=component_id,
|
|
58
|
+
capabilities=capabilities,
|
|
59
|
+
sender_id=sender_id,
|
|
60
|
+
supported_versions=supported_versions,
|
|
61
|
+
ready_topics=ready_topics,
|
|
62
|
+
)
|
|
63
|
+
data = payload
|
|
64
|
+
if private_key is not None:
|
|
65
|
+
packet = buspacket_pb2.BusPacket()
|
|
66
|
+
packet.ParseFromString(payload)
|
|
67
|
+
packet.ClearField("signature")
|
|
68
|
+
unsigned_data = packet.SerializeToString(deterministic=True)
|
|
69
|
+
packet.signature = private_key.sign(unsigned_data, ec.ECDSA(hashes.SHA256()))
|
|
70
|
+
data = packet.SerializeToString(deterministic=True)
|
|
71
|
+
|
|
72
|
+
await nc.publish(SUBJECT_HANDSHAKE, data)
|
|
@@ -25,6 +25,7 @@ def heartbeat_payload(
|
|
|
25
25
|
active_jobs: int,
|
|
26
26
|
max_parallel: int,
|
|
27
27
|
cpu_load: float,
|
|
28
|
+
auth_token: str = "",
|
|
28
29
|
) -> bytes:
|
|
29
30
|
"""Build a heartbeat payload with CPU utilization only."""
|
|
30
31
|
return heartbeat_payload_with_progress(
|
|
@@ -33,6 +34,7 @@ def heartbeat_payload(
|
|
|
33
34
|
active_jobs=active_jobs,
|
|
34
35
|
max_parallel=max_parallel,
|
|
35
36
|
cpu_load=cpu_load,
|
|
37
|
+
auth_token=auth_token,
|
|
36
38
|
)
|
|
37
39
|
|
|
38
40
|
|
|
@@ -43,6 +45,7 @@ def heartbeat_payload_with_memory(
|
|
|
43
45
|
max_parallel: int,
|
|
44
46
|
cpu_load: float,
|
|
45
47
|
memory_load: float,
|
|
48
|
+
auth_token: str = "",
|
|
46
49
|
) -> bytes:
|
|
47
50
|
"""Build a heartbeat payload including memory utilization."""
|
|
48
51
|
return heartbeat_payload_with_progress(
|
|
@@ -52,6 +55,7 @@ def heartbeat_payload_with_memory(
|
|
|
52
55
|
max_parallel=max_parallel,
|
|
53
56
|
cpu_load=cpu_load,
|
|
54
57
|
memory_load=memory_load,
|
|
58
|
+
auth_token=auth_token,
|
|
55
59
|
)
|
|
56
60
|
|
|
57
61
|
|
|
@@ -64,10 +68,12 @@ def heartbeat_payload_with_progress(
|
|
|
64
68
|
memory_load: float = 0.0,
|
|
65
69
|
progress_pct: int = 0,
|
|
66
70
|
last_memo: str = "",
|
|
71
|
+
auth_token: str = "",
|
|
67
72
|
) -> bytes:
|
|
68
73
|
"""Build a heartbeat payload including optional progress fields."""
|
|
69
74
|
ts = timestamp_pb2.Timestamp()
|
|
70
75
|
ts.GetCurrentTime()
|
|
76
|
+
auth_token = auth_token.strip()
|
|
71
77
|
|
|
72
78
|
packet = buspacket_pb2.BusPacket()
|
|
73
79
|
packet.sender_id = worker_id
|
|
@@ -83,6 +89,7 @@ def heartbeat_payload_with_progress(
|
|
|
83
89
|
memory_load=memory_load,
|
|
84
90
|
progress_pct=progress_pct,
|
|
85
91
|
last_memo=last_memo,
|
|
92
|
+
auth_token=auth_token,
|
|
86
93
|
)
|
|
87
94
|
)
|
|
88
95
|
return packet.SerializeToString(deterministic=True)
|
|
@@ -24,7 +24,7 @@ _sym_db = _symbol_database.Default()
|
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
|
|
27
|
-
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1f\x63ordum/agent/v1/handshake.proto\x12\x0f\x63ordum.agent.v1\"\
|
|
27
|
+
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1f\x63ordum/agent/v1/handshake.proto\x12\x0f\x63ordum.agent.v1\"\x8f\x02\n\tHandshake\x12\x14\n\x0c\x63omponent_id\x18\x01 \x01(\t\x12,\n\x04role\x18\x02 \x01(\x0e\x32\x1e.cordum.agent.v1.ComponentRole\x12\x1a\n\x12supported_versions\x18\x03 \x03(\x05\x12\x42\n\x0c\x63\x61pabilities\x18\x04 \x03(\x0b\x32,.cordum.agent.v1.Handshake.CapabilitiesEntry\x12\x13\n\x0bsdk_version\x18\x05 \x01(\t\x12\x14\n\x0cready_topics\x18\x06 \x03(\t\x1a\x33\n\x11\x43\x61pabilitiesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\x08:\x02\x38\x01*\xc4\x01\n\rComponentRole\x12\x1e\n\x1a\x43OMPONENT_ROLE_UNSPECIFIED\x10\x00\x12\x1a\n\x16\x43OMPONENT_ROLE_GATEWAY\x10\x01\x12\x1c\n\x18\x43OMPONENT_ROLE_SCHEDULER\x10\x02\x12\x19\n\x15\x43OMPONENT_ROLE_WORKER\x10\x03\x12\x1f\n\x1b\x43OMPONENT_ROLE_ORCHESTRATOR\x10\x04\x12\x1d\n\x19\x43OMPONENT_ROLE_CONTROLLER\x10\x05\x42\x7f\n\x16io.cordum.cap.agent.v1P\x01Z+github.com/cordum-io/cap/v2/cordum/agent/v1\xaa\x02\x0f\x43ordum.Agent.V1\xca\x02\x0f\x63ordum\\Agent\\V1\xea\x02\x11\x43ordum::Agent::V1b\x06proto3')
|
|
28
28
|
|
|
29
29
|
_globals = globals()
|
|
30
30
|
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
|
|
@@ -34,10 +34,10 @@ if not _descriptor._USE_C_DESCRIPTORS:
|
|
|
34
34
|
_globals['DESCRIPTOR']._serialized_options = b'\n\026io.cordum.cap.agent.v1P\001Z+github.com/cordum-io/cap/v2/cordum/agent/v1\252\002\017Cordum.Agent.V1\312\002\017cordum\\Agent\\V1\352\002\021Cordum::Agent::V1'
|
|
35
35
|
_globals['_HANDSHAKE_CAPABILITIESENTRY']._loaded_options = None
|
|
36
36
|
_globals['_HANDSHAKE_CAPABILITIESENTRY']._serialized_options = b'8\001'
|
|
37
|
-
_globals['_COMPONENTROLE']._serialized_start=
|
|
38
|
-
_globals['_COMPONENTROLE']._serialized_end=
|
|
37
|
+
_globals['_COMPONENTROLE']._serialized_start=327
|
|
38
|
+
_globals['_COMPONENTROLE']._serialized_end=523
|
|
39
39
|
_globals['_HANDSHAKE']._serialized_start=53
|
|
40
|
-
_globals['_HANDSHAKE']._serialized_end=
|
|
41
|
-
_globals['_HANDSHAKE_CAPABILITIESENTRY']._serialized_start=
|
|
42
|
-
_globals['_HANDSHAKE_CAPABILITIESENTRY']._serialized_end=
|
|
40
|
+
_globals['_HANDSHAKE']._serialized_end=324
|
|
41
|
+
_globals['_HANDSHAKE_CAPABILITIESENTRY']._serialized_start=273
|
|
42
|
+
_globals['_HANDSHAKE_CAPABILITIESENTRY']._serialized_end=324
|
|
43
43
|
# @@protoc_insertion_point(module_scope)
|
|
@@ -24,7 +24,7 @@ _sym_db = _symbol_database.Default()
|
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
|
|
27
|
-
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1f\x63ordum/agent/v1/heartbeat.proto\x12\x0f\x63ordum.agent.v1\"\
|
|
27
|
+
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1f\x63ordum/agent/v1/heartbeat.proto\x12\x0f\x63ordum.agent.v1\"\x86\x03\n\tHeartbeat\x12\x11\n\tworker_id\x18\x01 \x01(\t\x12\x0e\n\x06region\x18\x02 \x01(\t\x12\x0c\n\x04type\x18\x03 \x01(\t\x12\x10\n\x08\x63pu_load\x18\x04 \x01(\x02\x12\x17\n\x0fgpu_utilization\x18\x05 \x01(\x02\x12\x13\n\x0b\x61\x63tive_jobs\x18\x06 \x01(\x05\x12\x14\n\x0c\x63\x61pabilities\x18\x07 \x03(\t\x12\x0c\n\x04pool\x18\x0b \x01(\t\x12\x19\n\x11max_parallel_jobs\x18\x0c \x01(\x05\x12\x36\n\x06labels\x18\r \x03(\x0b\x32&.cordum.agent.v1.Heartbeat.LabelsEntry\x12\x13\n\x0bmemory_load\x18\x0e \x01(\x02\x12\x14\n\x0cprogress_pct\x18\x0f \x01(\x05\x12\x11\n\tlast_memo\x18\x10 \x01(\t\x12\x12\n\nauth_token\x18\x12 \x01(\t\x1a-\n\x0bLabelsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01J\x04\x08\x08\x10\tJ\x04\x08\t\x10\nJ\x04\x08\n\x10\x0b\x42\x7f\n\x16io.cordum.cap.agent.v1P\x01Z+github.com/cordum-io/cap/v2/cordum/agent/v1\xaa\x02\x0f\x43ordum.Agent.V1\xca\x02\x0f\x63ordum\\Agent\\V1\xea\x02\x11\x43ordum::Agent::V1b\x06proto3')
|
|
28
28
|
|
|
29
29
|
_globals = globals()
|
|
30
30
|
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
|
|
@@ -35,7 +35,7 @@ if not _descriptor._USE_C_DESCRIPTORS:
|
|
|
35
35
|
_globals['_HEARTBEAT_LABELSENTRY']._loaded_options = None
|
|
36
36
|
_globals['_HEARTBEAT_LABELSENTRY']._serialized_options = b'8\001'
|
|
37
37
|
_globals['_HEARTBEAT']._serialized_start=53
|
|
38
|
-
_globals['_HEARTBEAT']._serialized_end=
|
|
39
|
-
_globals['_HEARTBEAT_LABELSENTRY']._serialized_start=
|
|
40
|
-
_globals['_HEARTBEAT_LABELSENTRY']._serialized_end=
|
|
38
|
+
_globals['_HEARTBEAT']._serialized_end=443
|
|
39
|
+
_globals['_HEARTBEAT_LABELSENTRY']._serialized_start=380
|
|
40
|
+
_globals['_HEARTBEAT_LABELSENTRY']._serialized_end=425
|
|
41
41
|
# @@protoc_insertion_point(module_scope)
|
|
@@ -26,6 +26,7 @@ except Exception: # pragma: no cover - optional until runtime used
|
|
|
26
26
|
from cap.subjects import SUBJECT_RESULT
|
|
27
27
|
from cap.errors import InvalidInputError, MalformedPacketError, SignatureInvalidError, SignatureMissingError
|
|
28
28
|
from cap.metrics import MetricsHook, NoopMetrics
|
|
29
|
+
from cap.handshake import publish_handshake
|
|
29
30
|
from cap.heartbeat import heartbeat_loop, heartbeat_payload
|
|
30
31
|
|
|
31
32
|
from cap.constants import DEFAULT_PROTOCOL_VERSION # noqa: E402 — must be after conditional imports
|
|
@@ -361,6 +362,21 @@ class Agent:
|
|
|
361
362
|
asyncio.create_task(self._on_direct_msg(msg))
|
|
362
363
|
await self._nc.subscribe(direct_subject, cb=_direct_cb)
|
|
363
364
|
|
|
365
|
+
try:
|
|
366
|
+
await publish_handshake(
|
|
367
|
+
self._nc,
|
|
368
|
+
component_id=self._sender_id,
|
|
369
|
+
capabilities={topic: True for topic in self._handlers},
|
|
370
|
+
ready_topics=sorted(self._handlers),
|
|
371
|
+
private_key=self._private_key,
|
|
372
|
+
sender_id=self._sender_id,
|
|
373
|
+
)
|
|
374
|
+
except Exception as exc:
|
|
375
|
+
self._logger.warning(
|
|
376
|
+
"handshake publish failed",
|
|
377
|
+
extra={"sender_id": self._sender_id, "error": str(exc)},
|
|
378
|
+
)
|
|
379
|
+
|
|
364
380
|
if self._heartbeat_task is None or self._heartbeat_task.done():
|
|
365
381
|
self._heartbeat_cancel_event = asyncio.Event()
|
|
366
382
|
payload_fn = self._heartbeat_payload_fn or self._default_heartbeat_payload
|
|
@@ -114,7 +114,10 @@ async def run_handler(
|
|
|
114
114
|
msg = type("Msg", (object,), {"data": packet.SerializeToString(deterministic=True)})()
|
|
115
115
|
await cb(msg)
|
|
116
116
|
|
|
117
|
-
|
|
117
|
+
while True:
|
|
118
|
+
subject, payload = await asyncio.wait_for(mock.published.get(), timeout=5.0)
|
|
119
|
+
if subject == "sys.job.result":
|
|
120
|
+
break
|
|
118
121
|
|
|
119
122
|
result_packet = buspacket_pb2.BusPacket()
|
|
120
123
|
result_packet.ParseFromString(payload)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cap-sdk-python
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.9.0
|
|
4
4
|
Summary: CAP (Cordum Agent Protocol) Python SDK
|
|
5
5
|
Author-email: Cordum <eng@cordum.io>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -10,7 +10,7 @@ Project-URL: Issues, https://github.com/cordum-io/cap/issues
|
|
|
10
10
|
Keywords: cap,cordum,agent,protocol,sdk,ai
|
|
11
11
|
Requires-Python: >=3.9
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
|
-
Requires-Dist: protobuf
|
|
13
|
+
Requires-Dist: protobuf<7.0.0,>=6.31.0
|
|
14
14
|
Requires-Dist: grpcio>=1.59.0
|
|
15
15
|
Requires-Dist: nats-py>=2.6.0
|
|
16
16
|
Requires-Dist: cryptography>=41.0.0
|
|
@@ -5,6 +5,7 @@ cap/bus.py
|
|
|
5
5
|
cap/client.py
|
|
6
6
|
cap/constants.py
|
|
7
7
|
cap/errors.py
|
|
8
|
+
cap/handshake.py
|
|
8
9
|
cap/heartbeat.py
|
|
9
10
|
cap/metrics.py
|
|
10
11
|
cap/middleware.py
|
|
@@ -37,6 +38,7 @@ cap_sdk_python.egg-info/requires.txt
|
|
|
37
38
|
cap_sdk_python.egg-info/top_level.txt
|
|
38
39
|
tests/test_conformance.py
|
|
39
40
|
tests/test_errors.py
|
|
41
|
+
tests/test_handshake.py
|
|
40
42
|
tests/test_heartbeat.py
|
|
41
43
|
tests/test_metrics.py
|
|
42
44
|
tests/test_middleware.py
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "cap-sdk-python"
|
|
7
|
-
version = "2.
|
|
7
|
+
version = "2.9.0"
|
|
8
8
|
description = "CAP (Cordum Agent Protocol) Python SDK"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.9"
|
|
@@ -12,7 +12,7 @@ license = "Apache-2.0"
|
|
|
12
12
|
authors = [{name = "Cordum", email = "eng@cordum.io"}]
|
|
13
13
|
keywords = ["cap", "cordum", "agent", "protocol", "sdk", "ai"]
|
|
14
14
|
dependencies = [
|
|
15
|
-
"protobuf>=
|
|
15
|
+
"protobuf>=6.31.0,<7.0.0",
|
|
16
16
|
"grpcio>=1.59.0",
|
|
17
17
|
"nats-py>=2.6.0",
|
|
18
18
|
"cryptography>=41.0.0",
|
|
@@ -81,6 +81,7 @@ class TestConformanceFixtures(unittest.TestCase):
|
|
|
81
81
|
self.assertEqual(hb.pool, "job.tools")
|
|
82
82
|
self.assertEqual(hb.labels["zone"], "us-east-1a")
|
|
83
83
|
self.assertEqual(hb.progress_pct, 60)
|
|
84
|
+
self.assertEqual(hb.auth_token, "attest-worker-1")
|
|
84
85
|
|
|
85
86
|
def test_job_progress_fixture(self):
|
|
86
87
|
pkt = load_packet("buspacket_job_progress.bin")
|
|
@@ -116,6 +117,7 @@ class TestConformanceFixtures(unittest.TestCase):
|
|
|
116
117
|
self.assertTrue(hs.capabilities["cancel"])
|
|
117
118
|
self.assertFalse(hs.capabilities["compensation"])
|
|
118
119
|
self.assertEqual(hs.sdk_version, "2.0.19")
|
|
120
|
+
self.assertEqual(list(hs.ready_topics), ["job.tools", "job.tools.bulk"])
|
|
119
121
|
|
|
120
122
|
def test_alert_enhanced_fixture(self):
|
|
121
123
|
pkt = load_packet("buspacket_alert_enhanced.bin")
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from cap.runtime import Agent, InMemoryBlobStore
|
|
5
|
+
from cap.pb.cordum.agent.v1 import buspacket_pb2, handshake_pb2
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class MockNATS:
|
|
9
|
+
def __init__(self):
|
|
10
|
+
self.subscriptions = {}
|
|
11
|
+
self.published = asyncio.Queue()
|
|
12
|
+
|
|
13
|
+
async def publish(self, subject, data):
|
|
14
|
+
await self.published.put((subject, data))
|
|
15
|
+
|
|
16
|
+
async def subscribe(self, subject, queue="", cb=None):
|
|
17
|
+
self.subscriptions[subject] = cb
|
|
18
|
+
|
|
19
|
+
async def connect(self, servers, name):
|
|
20
|
+
return self
|
|
21
|
+
|
|
22
|
+
async def drain(self):
|
|
23
|
+
return None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class TestHandshake(unittest.IsolatedAsyncioTestCase):
|
|
27
|
+
async def test_agent_start_publishes_handshake(self):
|
|
28
|
+
store = InMemoryBlobStore()
|
|
29
|
+
mock = MockNATS()
|
|
30
|
+
agent = Agent(store=store, connect_fn=mock.connect, sender_id="worker-handshake")
|
|
31
|
+
|
|
32
|
+
@agent.job("job.handshake")
|
|
33
|
+
async def handler(_ctx, _data):
|
|
34
|
+
return {}
|
|
35
|
+
|
|
36
|
+
@agent.job("job.handshake.secondary")
|
|
37
|
+
async def secondary_handler(_ctx, _data):
|
|
38
|
+
return {}
|
|
39
|
+
|
|
40
|
+
await agent.start()
|
|
41
|
+
|
|
42
|
+
subject, payload = await asyncio.wait_for(mock.published.get(), timeout=1)
|
|
43
|
+
self.assertEqual(subject, "sys.handshake")
|
|
44
|
+
|
|
45
|
+
packet = buspacket_pb2.BusPacket()
|
|
46
|
+
packet.ParseFromString(payload)
|
|
47
|
+
self.assertEqual(packet.sender_id, "worker-handshake")
|
|
48
|
+
self.assertEqual(packet.handshake.component_id, "worker-handshake")
|
|
49
|
+
self.assertEqual(packet.handshake.role, handshake_pb2.COMPONENT_ROLE_WORKER)
|
|
50
|
+
self.assertEqual(list(packet.handshake.supported_versions), [1])
|
|
51
|
+
self.assertTrue(packet.handshake.capabilities["job.handshake"])
|
|
52
|
+
self.assertTrue(packet.handshake.capabilities["job.handshake.secondary"])
|
|
53
|
+
self.assertEqual(
|
|
54
|
+
list(packet.handshake.ready_topics),
|
|
55
|
+
["job.handshake", "job.handshake.secondary"],
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
await agent.close()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
if __name__ == "__main__":
|
|
62
|
+
unittest.main()
|
|
@@ -40,6 +40,7 @@ class TestHeartbeatPayload(unittest.TestCase):
|
|
|
40
40
|
active_jobs=3,
|
|
41
41
|
max_parallel=8,
|
|
42
42
|
cpu_load=42.5,
|
|
43
|
+
auth_token=" token-123 ",
|
|
43
44
|
)
|
|
44
45
|
packet = buspacket_pb2.BusPacket()
|
|
45
46
|
packet.ParseFromString(payload)
|
|
@@ -55,6 +56,7 @@ class TestHeartbeatPayload(unittest.TestCase):
|
|
|
55
56
|
self.assertEqual(packet.heartbeat.memory_load, 0.0)
|
|
56
57
|
self.assertEqual(packet.heartbeat.progress_pct, 0)
|
|
57
58
|
self.assertEqual(packet.heartbeat.last_memo, "")
|
|
59
|
+
self.assertEqual(packet.heartbeat.auth_token, "token-123")
|
|
58
60
|
|
|
59
61
|
def test_heartbeat_payload_with_memory_constructor(self):
|
|
60
62
|
payload = heartbeat_payload_with_memory(
|
|
@@ -18,7 +18,7 @@ class MockNATS:
|
|
|
18
18
|
async def publish(self, subject, data):
|
|
19
19
|
await self.published.put((subject, data))
|
|
20
20
|
|
|
21
|
-
async def subscribe(self, subject, queue, cb):
|
|
21
|
+
async def subscribe(self, subject, queue="", cb=None):
|
|
22
22
|
self.subscriptions[subject] = cb
|
|
23
23
|
|
|
24
24
|
async def connect(self, servers, name):
|
|
@@ -41,6 +41,12 @@ class TestMiddleware(unittest.IsolatedAsyncioTestCase):
|
|
|
41
41
|
self.store = InMemoryBlobStore()
|
|
42
42
|
self.mock = MockNATS()
|
|
43
43
|
|
|
44
|
+
async def _next_published(self, expected_subject: str):
|
|
45
|
+
while True:
|
|
46
|
+
subject, payload = await asyncio.wait_for(self.mock.published.get(), timeout=1)
|
|
47
|
+
if subject == expected_subject:
|
|
48
|
+
return payload
|
|
49
|
+
|
|
44
50
|
async def _send_job(self, job_id: str, topic: str, context_ptr: str):
|
|
45
51
|
req = job_pb2.JobRequest(job_id=job_id, topic=topic, context_ptr=context_ptr)
|
|
46
52
|
packet = buspacket_pb2.BusPacket()
|
|
@@ -83,8 +89,7 @@ class TestMiddleware(unittest.IsolatedAsyncioTestCase):
|
|
|
83
89
|
await agent.start()
|
|
84
90
|
await self._send_job(job_id, "job.mw", f"redis://{ctx_key}")
|
|
85
91
|
|
|
86
|
-
|
|
87
|
-
self.assertEqual(subject, "sys.job.result")
|
|
92
|
+
await self._next_published("sys.job.result")
|
|
88
93
|
self.assertEqual(order, ["A-before", "B-before", "handler", "B-after", "A-after"])
|
|
89
94
|
|
|
90
95
|
await agent.close()
|
|
@@ -111,8 +116,7 @@ class TestMiddleware(unittest.IsolatedAsyncioTestCase):
|
|
|
111
116
|
await agent.start()
|
|
112
117
|
await self._send_job(job_id, "job.short", f"redis://{ctx_key}")
|
|
113
118
|
|
|
114
|
-
|
|
115
|
-
self.assertEqual(subject, "sys.job.result")
|
|
119
|
+
await self._next_published("sys.job.result")
|
|
116
120
|
self.assertFalse(handler_called["value"])
|
|
117
121
|
|
|
118
122
|
result_data = await self.store.get(f"res:{job_id}")
|
|
@@ -148,8 +152,7 @@ class TestMiddleware(unittest.IsolatedAsyncioTestCase):
|
|
|
148
152
|
await agent.start()
|
|
149
153
|
await self._send_job(job_id, "job.log", f"redis://{ctx_key}")
|
|
150
154
|
|
|
151
|
-
|
|
152
|
-
self.assertEqual(subject, "sys.job.result")
|
|
155
|
+
await self._next_published("sys.job.result")
|
|
153
156
|
|
|
154
157
|
log_output = "\n".join(log_records)
|
|
155
158
|
self.assertIn(job_id, log_output)
|
|
@@ -16,7 +16,7 @@ class MockNATS:
|
|
|
16
16
|
async def publish(self, subject, data):
|
|
17
17
|
await self.published.put((subject, data))
|
|
18
18
|
|
|
19
|
-
async def subscribe(self, subject, queue, cb):
|
|
19
|
+
async def subscribe(self, subject, queue="", cb=None):
|
|
20
20
|
self.subscriptions[subject] = cb
|
|
21
21
|
|
|
22
22
|
async def connect(self, servers, name):
|
|
@@ -39,6 +39,12 @@ class TestRuntime(unittest.IsolatedAsyncioTestCase):
|
|
|
39
39
|
self.store = InMemoryBlobStore()
|
|
40
40
|
self.mock = MockNATS()
|
|
41
41
|
|
|
42
|
+
async def _next_published(self, expected_subject: str):
|
|
43
|
+
while True:
|
|
44
|
+
subject, payload = await asyncio.wait_for(self.mock.published.get(), timeout=1)
|
|
45
|
+
if subject == expected_subject:
|
|
46
|
+
return payload
|
|
47
|
+
|
|
42
48
|
async def _send_job(self, job_id: str, topic: str, context_ptr: str):
|
|
43
49
|
req = job_pb2.JobRequest(job_id=job_id, topic=topic, context_ptr=context_ptr)
|
|
44
50
|
packet = buspacket_pb2.BusPacket()
|
|
@@ -62,8 +68,7 @@ class TestRuntime(unittest.IsolatedAsyncioTestCase):
|
|
|
62
68
|
await agent.start()
|
|
63
69
|
await self._send_job(job_id, "job.test", f"redis://{ctx_key}")
|
|
64
70
|
|
|
65
|
-
|
|
66
|
-
self.assertEqual(subject, "sys.job.result")
|
|
71
|
+
payload = await self._next_published("sys.job.result")
|
|
67
72
|
result_packet = buspacket_pb2.BusPacket()
|
|
68
73
|
result_packet.ParseFromString(payload)
|
|
69
74
|
self.assertEqual(result_packet.job_result.status, job_pb2.JOB_STATUS_SUCCEEDED)
|
|
@@ -89,8 +94,7 @@ class TestRuntime(unittest.IsolatedAsyncioTestCase):
|
|
|
89
94
|
await agent.start()
|
|
90
95
|
await self._send_job(job_id, "job.validate", f"redis://{ctx_key}")
|
|
91
96
|
|
|
92
|
-
|
|
93
|
-
self.assertEqual(subject, "sys.job.result")
|
|
97
|
+
payload = await self._next_published("sys.job.result")
|
|
94
98
|
result_packet = buspacket_pb2.BusPacket()
|
|
95
99
|
result_packet.ParseFromString(payload)
|
|
96
100
|
self.assertEqual(result_packet.job_result.status, job_pb2.JOB_STATUS_FAILED)
|
|
@@ -116,8 +120,7 @@ class TestRuntime(unittest.IsolatedAsyncioTestCase):
|
|
|
116
120
|
await agent.start()
|
|
117
121
|
await self._send_job(job_id, "job.retry", f"redis://{ctx_key}")
|
|
118
122
|
|
|
119
|
-
|
|
120
|
-
self.assertEqual(subject, "sys.job.result")
|
|
123
|
+
payload = await self._next_published("sys.job.result")
|
|
121
124
|
result_packet = buspacket_pb2.BusPacket()
|
|
122
125
|
result_packet.ParseFromString(payload)
|
|
123
126
|
self.assertEqual(result_packet.job_result.status, job_pb2.JOB_STATUS_SUCCEEDED)
|
|
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
|