-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.py
More file actions
109 lines (93 loc) · 3.23 KB
/
client_test.py
File metadata and controls
109 lines (93 loc) · 3.23 KB
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
#!/usr/bin/env python3
import json
import os
import socket
import uuid
SOCKET_PATH = os.environ.get(
"AGENTD_SOCKET_PATH", os.path.expanduser("~/.agentd/agentd.sock")
)
def call(sock_file, request_id, method, params):
request = {"id": request_id, "method": method, "params": params}
sock_file.write(json.dumps(request).encode("utf-8") + b"\n")
sock_file.flush()
response = json.loads(sock_file.readline().decode("utf-8"))
if "error" in response:
raise RuntimeError(response["error"])
return response["result"]
def main():
first_node = str(uuid.uuid4())
second_node = str(uuid.uuid4())
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
sock.connect(SOCKET_PATH)
sock_file = sock.makefile("rwb")
registered = call(
sock_file,
1,
"RegisterTask",
{
"goal": "Demonstrate agentd node acquisition",
"context": {"source": "client_test.py"},
"initial_nodes": [
{
"id": first_node,
"dependencies": [],
"payload_schema": {"type": "object"},
},
{
"id": second_node,
"dependencies": [first_node],
"payload_schema": {"type": "object"},
},
],
},
)
task_id = registered["task_id"]
print("registered task:", task_id)
acquired = call(sock_file, 2, "AcquireNextNode", {"task_id": task_id})
print("first acquire:", json.dumps(acquired, indent=2))
node_id = acquired["node_id"]
lease_id = acquired["lease_id"]
event = call(
sock_file,
3,
"CommitEvent",
{
"task_id": task_id,
"node_id": node_id,
"lease_id": lease_id,
"payload": {"message": "node started"},
},
)
print("committed event:", event["event_id"])
completed = call(
sock_file,
4,
"CompleteNode",
{
"task_id": task_id,
"node_id": node_id,
"lease_id": lease_id,
"result_payload": {"ok": True, "value": "first node complete"},
},
)
print("completed node:", completed["event_id"])
acquired = call(sock_file, 5, "AcquireNextNode", {"task_id": task_id})
print("second acquire:", json.dumps(acquired, indent=2))
node_id = acquired["node_id"]
lease_id = acquired["lease_id"]
completed = call(
sock_file,
6,
"CompleteNode",
{
"task_id": task_id,
"node_id": node_id,
"lease_id": lease_id,
"result_payload": {"ok": True, "value": "second node complete"},
},
)
print("completed second node:", completed["event_id"])
status = call(sock_file, 7, "TaskStatus", {"task_id": task_id})
print("final counts:", json.dumps(status["counts"], sort_keys=True))
if __name__ == "__main__":
main()