sessionyard 0.7.0__py3-none-any.whl
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.
- agent_chat/001_core.sql +172 -0
- agent_chat/002_coordination.sql +63 -0
- agent_chat/003_harness_admission.sql +10 -0
- agent_chat/004_session_titles.sql +3 -0
- agent_chat/005_viewer.sql +9 -0
- agent_chat/006_quotas.sql +6 -0
- agent_chat/007_sender_receipts.sql +20 -0
- agent_chat/008_direct_threads.sql +49 -0
- agent_chat/009_pm_mark.sql +23 -0
- agent_chat/010_quota_backfill.sql +12 -0
- agent_chat/011_roles_and_work.sql +31 -0
- agent_chat/__init__.py +1 -0
- agent_chat/adapter.py +414 -0
- agent_chat/admin.py +524 -0
- agent_chat/api.py +641 -0
- agent_chat/auth.py +226 -0
- agent_chat/browser.py +118 -0
- agent_chat/browser_verify.py +487 -0
- agent_chat/browser_verify_artifacts.py +280 -0
- agent_chat/browser_verify_checks.py +236 -0
- agent_chat/browser_verify_run.py +523 -0
- agent_chat/cli.py +532 -0
- agent_chat/client.py +272 -0
- agent_chat/deliveries.py +555 -0
- agent_chat/events.py +48 -0
- agent_chat/feed.py +193 -0
- agent_chat/http_boundary.py +154 -0
- agent_chat/instance_client.py +349 -0
- agent_chat/instance_file.py +207 -0
- agent_chat/instance_worker.py +326 -0
- agent_chat/mcp/__init__.py +5 -0
- agent_chat/mcp/__main__.py +39 -0
- agent_chat/mcp/app.py +183 -0
- agent_chat/mcp/auth.py +67 -0
- agent_chat/mcp/config.py +55 -0
- agent_chat/mcp/tools.py +295 -0
- agent_chat/metrics.py +60 -0
- agent_chat/models.py +430 -0
- agent_chat/openapi.py +322 -0
- agent_chat/producer.py +208 -0
- agent_chat/protocol.py +71 -0
- agent_chat/publication.py +252 -0
- agent_chat/receipts.py +225 -0
- agent_chat/retention.py +154 -0
- agent_chat/roles.py +153 -0
- agent_chat/rooms.py +179 -0
- agent_chat/sessions.py +340 -0
- agent_chat/skills/agent-chat/SKILL.md +64 -0
- agent_chat/state.py +144 -0
- agent_chat/storage.py +110 -0
- agent_chat/threads.py +50 -0
- agent_chat/web/Manrope-OFL.txt +93 -0
- agent_chat/web/Manrope-Variable.ttf +0 -0
- agent_chat/web/chat.css +936 -0
- agent_chat/web/chat.js +1922 -0
- agent_chat/web/i18n.js +340 -0
- agent_chat/web/index.html +264 -0
- sessionyard-0.7.0.dist-info/METADATA +314 -0
- sessionyard-0.7.0.dist-info/RECORD +62 -0
- sessionyard-0.7.0.dist-info/WHEEL +4 -0
- sessionyard-0.7.0.dist-info/entry_points.txt +3 -0
- sessionyard-0.7.0.dist-info/licenses/LICENSE +661 -0
agent_chat/client.py
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"""One credential-bound HTTP destination; no redirect, proxy, retry or native delivery."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import http.client
|
|
6
|
+
import json
|
|
7
|
+
import socket
|
|
8
|
+
import ssl
|
|
9
|
+
import subprocess
|
|
10
|
+
import sys
|
|
11
|
+
import threading
|
|
12
|
+
import time
|
|
13
|
+
from functools import partial
|
|
14
|
+
from typing import Any
|
|
15
|
+
from urllib.parse import urlsplit
|
|
16
|
+
|
|
17
|
+
from .models import HTTP_ERROR_STATUS, ChatError
|
|
18
|
+
from .protocol import token_digest, validate_origin
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _time_left(deadline: float) -> float:
|
|
22
|
+
left = deadline - time.monotonic()
|
|
23
|
+
if left <= 0:
|
|
24
|
+
raise ChatError("CLI_INTERRUPTED")
|
|
25
|
+
return left
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _connect_before_deadline(
|
|
29
|
+
address: tuple[str, int],
|
|
30
|
+
timeout: float,
|
|
31
|
+
source_address: tuple[str, int] | None,
|
|
32
|
+
*,
|
|
33
|
+
deadline: float,
|
|
34
|
+
) -> socket.socket:
|
|
35
|
+
"""Replace HTTPConnection's socket factory, not its HTTP or TLS semantics.
|
|
36
|
+
|
|
37
|
+
getaddrinfo has no cancellation API. A private stdlib-only child can be
|
|
38
|
+
killed and reaped; abandoning a resolver thread would leave expired work
|
|
39
|
+
behind. The child receives only the destination, never the credential.
|
|
40
|
+
"""
|
|
41
|
+
_time_left(deadline)
|
|
42
|
+
with subprocess.Popen(
|
|
43
|
+
[
|
|
44
|
+
sys.executable, "-I", "-S", "-c",
|
|
45
|
+
"import json,socket,sys; print(json.dumps(socket.getaddrinfo("
|
|
46
|
+
"sys.argv[1],int(sys.argv[2]),type=socket.SOCK_STREAM)))",
|
|
47
|
+
address[0], str(address[1]),
|
|
48
|
+
],
|
|
49
|
+
stdin=subprocess.DEVNULL,
|
|
50
|
+
stdout=subprocess.PIPE,
|
|
51
|
+
stderr=subprocess.DEVNULL,
|
|
52
|
+
) as resolver:
|
|
53
|
+
try:
|
|
54
|
+
output, _ = resolver.communicate(timeout=_time_left(deadline))
|
|
55
|
+
except subprocess.TimeoutExpired:
|
|
56
|
+
raise ChatError("CLI_INTERRUPTED") from None
|
|
57
|
+
finally:
|
|
58
|
+
if resolver.poll() is None:
|
|
59
|
+
resolver.kill()
|
|
60
|
+
resolver.wait()
|
|
61
|
+
if resolver.returncode:
|
|
62
|
+
raise OSError("hostname resolution failed")
|
|
63
|
+
|
|
64
|
+
last_error = OSError("no resolved addresses")
|
|
65
|
+
for family, kind, protocol, _, sockaddr in json.loads(output):
|
|
66
|
+
left = _time_left(deadline)
|
|
67
|
+
transport: socket.socket | None = None
|
|
68
|
+
connected = False
|
|
69
|
+
try:
|
|
70
|
+
transport = socket.socket(family, kind, protocol)
|
|
71
|
+
transport.settimeout(min(timeout, left))
|
|
72
|
+
if source_address is not None:
|
|
73
|
+
transport.bind(source_address)
|
|
74
|
+
transport.connect(tuple(sockaddr))
|
|
75
|
+
# HTTPSConnection wraps this socket next. Its TLS handshake must
|
|
76
|
+
# get only the remaining budget, not the pre-connect timeout.
|
|
77
|
+
transport.settimeout(min(timeout, _time_left(deadline)))
|
|
78
|
+
connected = True
|
|
79
|
+
return transport
|
|
80
|
+
except OSError as error:
|
|
81
|
+
last_error = error
|
|
82
|
+
finally:
|
|
83
|
+
if transport is not None and not connected:
|
|
84
|
+
transport.close()
|
|
85
|
+
_time_left(deadline)
|
|
86
|
+
raise last_error
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class _DeadlineWatchdog:
|
|
90
|
+
"""Interrupt blocked transport I/O when the whole-operation deadline expires.
|
|
91
|
+
|
|
92
|
+
``http.client`` parses the status line, the headers and chunked framing with
|
|
93
|
+
blocking reads that never return to the caller, so an inactivity timeout on
|
|
94
|
+
the socket cannot bound elapsed time: a peer that drips one byte inside
|
|
95
|
+
every timeout keeps each receive short while the operation continues. This
|
|
96
|
+
timer shuts the connected transport down instead, which fails the blocked
|
|
97
|
+
read at once. That interrupts the real I/O rather than abandoning a waiting
|
|
98
|
+
caller, and the socket timeout still bounds a silent peer.
|
|
99
|
+
|
|
100
|
+
The transport is held directly, not read from the connection when the timer
|
|
101
|
+
fires: ``getresponse`` handles a ``Connection: close`` response by closing
|
|
102
|
+
the HTTPConnection and clearing its ``sock``, while the response's file
|
|
103
|
+
object keeps the same socket open.
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
def __init__(self, deadline: float, transport: socket.socket) -> None:
|
|
107
|
+
self._transport = transport
|
|
108
|
+
self._expired = threading.Event()
|
|
109
|
+
self._timer = threading.Timer(max(0.0, deadline - time.monotonic()), self._interrupt)
|
|
110
|
+
self._timer.daemon = True
|
|
111
|
+
self._timer.start()
|
|
112
|
+
|
|
113
|
+
def _interrupt(self) -> None:
|
|
114
|
+
self._expired.set()
|
|
115
|
+
try:
|
|
116
|
+
self._transport.shutdown(socket.SHUT_RDWR)
|
|
117
|
+
except OSError:
|
|
118
|
+
pass
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def expired(self) -> bool:
|
|
122
|
+
return self._expired.is_set()
|
|
123
|
+
|
|
124
|
+
def cancel(self) -> None:
|
|
125
|
+
self._timer.cancel()
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class Client:
|
|
129
|
+
def __init__(
|
|
130
|
+
self,
|
|
131
|
+
server_origin: str,
|
|
132
|
+
token: str,
|
|
133
|
+
*,
|
|
134
|
+
deadline: float | None = None,
|
|
135
|
+
allow_service_http: bool = False,
|
|
136
|
+
):
|
|
137
|
+
validate_origin(server_origin, allow_service_http=allow_service_http)
|
|
138
|
+
token_digest(token)
|
|
139
|
+
self._origin = server_origin
|
|
140
|
+
self._token = token
|
|
141
|
+
# A monotonic timestamp bounding the whole operation, or None for the CLI's signal bound.
|
|
142
|
+
self._deadline = deadline
|
|
143
|
+
# Whole-command response budget, not an unbounded allowance per page.
|
|
144
|
+
self._remaining = 16 * 1024 * 1024
|
|
145
|
+
|
|
146
|
+
def request(
|
|
147
|
+
self,
|
|
148
|
+
method: str,
|
|
149
|
+
path: str,
|
|
150
|
+
payload: dict[str, Any] | None = None,
|
|
151
|
+
key: str | None = None,
|
|
152
|
+
*,
|
|
153
|
+
invalid: str | None = None,
|
|
154
|
+
) -> Any:
|
|
155
|
+
origin = urlsplit(self._origin)
|
|
156
|
+
assert origin.hostname is not None
|
|
157
|
+
connection: http.client.HTTPConnection | None = None
|
|
158
|
+
watchdog: _DeadlineWatchdog | None = None
|
|
159
|
+
submitted = False
|
|
160
|
+
status: int | None = None
|
|
161
|
+
|
|
162
|
+
def check() -> float | None:
|
|
163
|
+
"""The time left in the whole-operation budget, or None when unbounded."""
|
|
164
|
+
if self._deadline is None:
|
|
165
|
+
return None
|
|
166
|
+
return _time_left(self._deadline)
|
|
167
|
+
|
|
168
|
+
try:
|
|
169
|
+
left = check()
|
|
170
|
+
timeout = 3.0 if left is None else max(0.001, min(3.0, left))
|
|
171
|
+
if origin.scheme == "https":
|
|
172
|
+
# Unlike create_default_context, this does not enable SSLKEYLOGFILE.
|
|
173
|
+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
|
174
|
+
context.load_default_certs()
|
|
175
|
+
connection = http.client.HTTPSConnection(
|
|
176
|
+
origin.hostname, origin.port, timeout=timeout, context=context
|
|
177
|
+
)
|
|
178
|
+
else:
|
|
179
|
+
connection = http.client.HTTPConnection(origin.hostname, origin.port, timeout=timeout)
|
|
180
|
+
if self._deadline is not None:
|
|
181
|
+
# http.client's runtime socket-factory seam is absent from typeshed.
|
|
182
|
+
setattr( # the seam is private and absent from typeshed
|
|
183
|
+
connection, "_create_connection", partial(_connect_before_deadline, deadline=self._deadline)
|
|
184
|
+
)
|
|
185
|
+
connection.connect()
|
|
186
|
+
check()
|
|
187
|
+
# From here on every transport phase is under the absolute deadline,
|
|
188
|
+
# including the blocking header and chunk-framing reads below. Hold
|
|
189
|
+
# the connected socket itself: getresponse() detaches a will-close
|
|
190
|
+
# response by clearing connection.sock, but the response keeps it.
|
|
191
|
+
transport = connection.sock
|
|
192
|
+
if self._deadline is not None and transport is not None:
|
|
193
|
+
watchdog = _DeadlineWatchdog(self._deadline, transport)
|
|
194
|
+
headers = {"Authorization": "Bearer " + self._token, "Accept": "application/json"}
|
|
195
|
+
body = None
|
|
196
|
+
if payload is not None:
|
|
197
|
+
body = json.dumps(payload, ensure_ascii=True).encode("utf-8")
|
|
198
|
+
headers["Content-Type"] = "application/json"
|
|
199
|
+
if key is not None:
|
|
200
|
+
headers["Idempotency-Key"] = key
|
|
201
|
+
submitted = method == "POST"
|
|
202
|
+
# No publication starts after the budget has already run out.
|
|
203
|
+
check()
|
|
204
|
+
connection.request(method, path, body=body, headers=headers)
|
|
205
|
+
left = check()
|
|
206
|
+
if left is not None and connection.sock is not None:
|
|
207
|
+
connection.sock.settimeout(max(0.001, min(3.0, left)))
|
|
208
|
+
response = connection.getresponse()
|
|
209
|
+
status = response.status
|
|
210
|
+
# Never inspect a redirect destination or relay a credential to it.
|
|
211
|
+
if 300 <= response.status < 400:
|
|
212
|
+
raise ChatError("REQUEST_UNCERTAIN" if submitted else "REDIRECT_REFUSED")
|
|
213
|
+
chunks: list[bytes] = []
|
|
214
|
+
total = 0
|
|
215
|
+
while True:
|
|
216
|
+
# read1 returns after one receive, so the budget is checked between chunks instead
|
|
217
|
+
# of only when a slow-dripping peer finally closes or fills the buffer.
|
|
218
|
+
left = check()
|
|
219
|
+
if left is not None and connection.sock is not None:
|
|
220
|
+
connection.sock.settimeout(max(0.001, min(3.0, left)))
|
|
221
|
+
chunk = response.read1(65536)
|
|
222
|
+
if not chunk:
|
|
223
|
+
break
|
|
224
|
+
chunks.append(chunk)
|
|
225
|
+
total += len(chunk)
|
|
226
|
+
if total > self._remaining:
|
|
227
|
+
break
|
|
228
|
+
check()
|
|
229
|
+
if watchdog is not None and watchdog.expired:
|
|
230
|
+
raise ChatError("CLI_INTERRUPTED")
|
|
231
|
+
data = b"".join(chunks)
|
|
232
|
+
self._remaining -= len(data)
|
|
233
|
+
if self._remaining < 0:
|
|
234
|
+
raise ValueError("response too large")
|
|
235
|
+
if response.length not in (None, 0):
|
|
236
|
+
# The peer closed before the declared length: a read failure, not a malformed body.
|
|
237
|
+
raise http.client.IncompleteRead(data, response.length)
|
|
238
|
+
if response.getheader("Content-Type", "").split(";", 1)[0].strip().lower() != "application/json":
|
|
239
|
+
raise ValueError("not JSON")
|
|
240
|
+
result = json.loads(data.decode("utf-8"))
|
|
241
|
+
# Decoding and parsing can exhaust the budget after the last read.
|
|
242
|
+
check()
|
|
243
|
+
if not isinstance(result, dict):
|
|
244
|
+
raise ValueError("invalid response object")
|
|
245
|
+
if response.status != 200:
|
|
246
|
+
error = result.get("error", {})
|
|
247
|
+
code = error.get("code") if isinstance(error, dict) else None
|
|
248
|
+
if isinstance(code, str) and HTTP_ERROR_STATUS.get(code) == response.status:
|
|
249
|
+
raise ChatError(code)
|
|
250
|
+
if response.status >= 500:
|
|
251
|
+
raise ChatError("REQUEST_UNCERTAIN" if submitted else "SERVICE_UNAVAILABLE")
|
|
252
|
+
raise ChatError("REQUEST_UNCERTAIN" if submitted else "HTTP_REFUSED")
|
|
253
|
+
return result
|
|
254
|
+
except ChatError:
|
|
255
|
+
raise
|
|
256
|
+
except (OSError, http.client.HTTPException, ValueError, RecursionError, KeyboardInterrupt) as error:
|
|
257
|
+
# The watchdog shut the socket down, so this failure is the deadline,
|
|
258
|
+
# not a peer or protocol fault.
|
|
259
|
+
if watchdog is not None and watchdog.expired:
|
|
260
|
+
raise ChatError("CLI_INTERRUPTED") from None
|
|
261
|
+
if self._deadline is not None and time.monotonic() >= self._deadline:
|
|
262
|
+
raise ChatError("CLI_INTERRUPTED") from None
|
|
263
|
+
# A complete 200 whose body breaks the protocol is not an outage. A caller that tolerates
|
|
264
|
+
# outages names it `invalid`; every other caller keeps the classification below.
|
|
265
|
+
if invalid is not None and status == 200 and isinstance(error, (ValueError, RecursionError)):
|
|
266
|
+
raise ChatError(invalid) from None
|
|
267
|
+
raise ChatError("REQUEST_UNCERTAIN" if submitted else "TRANSPORT_UNAVAILABLE") from None
|
|
268
|
+
finally:
|
|
269
|
+
if watchdog is not None:
|
|
270
|
+
watchdog.cancel()
|
|
271
|
+
if connection is not None:
|
|
272
|
+
connection.close()
|