Repository navigation
Expand file tree
/
Copy pathcode_workbench_client.py
More file actions
52 lines (46 loc) · 1.86 KB
/
Copy pathcode_workbench_client.py
File metadata and controls
52 lines (46 loc) · 1.86 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
"""Small shell client; no package imports, model client, or host-path API."""
import json
import os
import socket
import sys
import time
SOCKET = "/tmp/task-scratch/.ic-code.sock"
MAX_REQUEST = 16384
MAX_RESPONSE = 1024 * 1024
def main():
try:
lease = os.environ["IC_CODE_LEASE"]
remaining = float(os.environ["IC_CODE_DEADLINE"]) - time.monotonic()
if remaining <= 0:
raise TimeoutError("original action deadline expired")
payload = json.dumps(dict(lease=lease, argv=sys.argv[1:])).encode() + b"\n"
if len(payload) > MAX_REQUEST:
raise ValueError("arguments exceed16KiB;write payload to task scratch")
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
client.settimeout(remaining)
client.connect(SOCKET)
client.sendall(payload)
with client.makefile("rb") as stream:
raw = stream.readline(MAX_RESPONSE + 1)
if len(raw) > MAX_RESPONSE or not raw.endswith(b"\n"):
raise ValueError("incomplete or oversized tool response;outcome unknown")
response = json.loads(raw)
if type(response.get("returncode")) is not int or not isinstance(
response.get("output"), str
):
raise ValueError("invalid tool response;outcome unknown")
print(response["output"])
return response["returncode"]
except (KeyError, OSError, ValueError, TimeoutError) as error:
print(
json.dumps(
dict(
error=type(error).__name__ + ": " + str(error),
tool_outcome="not_confirmed;do not automatically retry writes",
fallback="Ordinary shell reading and editing remain available.",
)
)
)
return 2
if __name__ == "__main__":
sys.exit(main())