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/auth.py
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"""Token purposes and transaction-time fences shared by session and message operations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Literal
|
|
6
|
+
from uuid import UUID
|
|
7
|
+
|
|
8
|
+
from psycopg import Connection
|
|
9
|
+
|
|
10
|
+
from .models import LEASE_SECONDS, Actor, ChatError, ProducerActor
|
|
11
|
+
from .protocol import token_digest
|
|
12
|
+
|
|
13
|
+
Access = Literal["operator", "instance", "viewer"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def authenticate(db: Connection[dict[str, Any]], token: str, *, registration: bool = False) -> Actor:
|
|
17
|
+
digest = token_digest(token)
|
|
18
|
+
principal = db.execute(
|
|
19
|
+
"""
|
|
20
|
+
SELECT id FROM agent_chat.principals
|
|
21
|
+
WHERE credential_hash = %s AND NOT revoked AND kind = %s
|
|
22
|
+
""",
|
|
23
|
+
(digest, "agent" if registration else "operator"),
|
|
24
|
+
).fetchone()
|
|
25
|
+
if principal is not None:
|
|
26
|
+
return Actor(principal["id"], None, None)
|
|
27
|
+
if not registration:
|
|
28
|
+
instance = db.execute(
|
|
29
|
+
"""
|
|
30
|
+
SELECT p.id AS principal_id, s.id AS session_id, i.id AS instance_id
|
|
31
|
+
FROM agent_chat.instances i JOIN agent_chat.sessions s ON s.id = i.session_id
|
|
32
|
+
JOIN agent_chat.principals p ON p.id = s.principal_id
|
|
33
|
+
WHERE i.token_hash = %s AND NOT p.revoked
|
|
34
|
+
AND NOT EXISTS (SELECT 1 FROM agent_chat.principals WHERE credential_hash = %s)
|
|
35
|
+
""",
|
|
36
|
+
(digest, digest),
|
|
37
|
+
).fetchone()
|
|
38
|
+
if instance is not None:
|
|
39
|
+
return Actor(instance["principal_id"], instance["session_id"], instance["instance_id"])
|
|
40
|
+
raise ChatError("UNAUTHENTICATED")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def authenticate_producer(db: Connection[dict[str, Any]], token: str) -> ProducerActor:
|
|
44
|
+
"""Match only a live producer credential. `authenticate` never accepts one, so a producer token reaches
|
|
45
|
+
no route except the one that calls this."""
|
|
46
|
+
principal = db.execute(
|
|
47
|
+
"""
|
|
48
|
+
SELECT id, host, producer FROM agent_chat.principals
|
|
49
|
+
WHERE credential_hash = %s AND kind = 'producer' AND NOT revoked
|
|
50
|
+
""",
|
|
51
|
+
(token_digest(token),),
|
|
52
|
+
).fetchone()
|
|
53
|
+
if principal is None:
|
|
54
|
+
raise ChatError("UNAUTHENTICATED")
|
|
55
|
+
return ProducerActor(principal["id"], principal["host"], principal["producer"])
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def lock_producer(db: Connection[dict[str, Any]], actor: ProducerActor, room: UUID) -> ProducerActor:
|
|
59
|
+
"""Caller holds the event clock. Lock the live principal, then its grant to the room, and return the
|
|
60
|
+
identity those locked rows carry."""
|
|
61
|
+
principal = db.execute(
|
|
62
|
+
"""
|
|
63
|
+
SELECT host, producer FROM agent_chat.principals
|
|
64
|
+
WHERE id = %s AND kind = 'producer' AND NOT revoked FOR SHARE
|
|
65
|
+
""",
|
|
66
|
+
(actor.principal_id,),
|
|
67
|
+
).fetchone()
|
|
68
|
+
if principal is None:
|
|
69
|
+
raise ChatError("UNAUTHENTICATED")
|
|
70
|
+
allowed = db.execute(
|
|
71
|
+
"SELECT 1 FROM agent_chat.principal_rooms WHERE principal_id = %s AND room_id = %s FOR SHARE",
|
|
72
|
+
(actor.principal_id, room),
|
|
73
|
+
).fetchone()
|
|
74
|
+
if allowed is None:
|
|
75
|
+
raise ChatError("ROOM_FORBIDDEN")
|
|
76
|
+
return ProducerActor(actor.principal_id, principal["host"], principal["producer"])
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def lock_actor(
|
|
80
|
+
db: Connection[dict[str, Any]], actor: Actor, *, allow_closed: bool = False,
|
|
81
|
+
exclusive_session: bool = False, read: bool = False,
|
|
82
|
+
) -> Access:
|
|
83
|
+
"""Caller holds a transaction; lock principal, session, then instance in that order."""
|
|
84
|
+
principal = db.execute(
|
|
85
|
+
"SELECT kind FROM agent_chat.principals WHERE id = %s AND NOT revoked FOR SHARE",
|
|
86
|
+
(actor.principal_id,),
|
|
87
|
+
).fetchone()
|
|
88
|
+
if principal is None:
|
|
89
|
+
raise ChatError("UNAUTHENTICATED")
|
|
90
|
+
kind = principal["kind"]
|
|
91
|
+
if kind == "viewer" and not read:
|
|
92
|
+
raise ChatError("SESSION_FORBIDDEN")
|
|
93
|
+
if kind in ("operator", "viewer"):
|
|
94
|
+
if actor.session_id is not None or actor.instance_id is not None:
|
|
95
|
+
raise ChatError("SESSION_FORBIDDEN")
|
|
96
|
+
if kind == "viewer" and actor.browser_session_hash is None:
|
|
97
|
+
raise ChatError("UNAUTHENTICATED")
|
|
98
|
+
if actor.browser_session_hash is not None:
|
|
99
|
+
binding = db.execute(
|
|
100
|
+
"""
|
|
101
|
+
SELECT token_hash FROM agent_chat.browser_sessions
|
|
102
|
+
WHERE token_hash = %s AND principal_id = %s FOR UPDATE
|
|
103
|
+
""",
|
|
104
|
+
(actor.browser_session_hash, actor.principal_id),
|
|
105
|
+
).fetchone()
|
|
106
|
+
if binding is None:
|
|
107
|
+
raise ChatError("UNAUTHENTICATED")
|
|
108
|
+
require_current(db, actor, kind)
|
|
109
|
+
return kind
|
|
110
|
+
if kind != "agent" or actor.browser_session_hash is not None:
|
|
111
|
+
raise ChatError("SESSION_FORBIDDEN")
|
|
112
|
+
session = db.execute(
|
|
113
|
+
"""
|
|
114
|
+
SELECT id FROM agent_chat.sessions WHERE id = %s AND principal_id = %s
|
|
115
|
+
"""
|
|
116
|
+
+ (" FOR UPDATE" if exclusive_session else " FOR SHARE"),
|
|
117
|
+
(actor.session_id, actor.principal_id),
|
|
118
|
+
).fetchone()
|
|
119
|
+
if session is None:
|
|
120
|
+
raise ChatError("STALE_INSTANCE")
|
|
121
|
+
# Exclusive instance locking also serializes heartbeat/end without lock upgrades.
|
|
122
|
+
instance = db.execute(
|
|
123
|
+
"""
|
|
124
|
+
SELECT id FROM agent_chat.instances WHERE id = %s AND session_id = %s FOR UPDATE
|
|
125
|
+
""",
|
|
126
|
+
(actor.instance_id, actor.session_id),
|
|
127
|
+
).fetchone()
|
|
128
|
+
if instance is None:
|
|
129
|
+
raise ChatError("STALE_INSTANCE")
|
|
130
|
+
if not allow_closed:
|
|
131
|
+
require_live(db, actor)
|
|
132
|
+
return "instance"
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def require_current(db: Connection[dict[str, Any]], actor: Actor, access: Access) -> None:
|
|
136
|
+
# The principal and binding are already locked. Time, unlike those rows, can change during a later wait.
|
|
137
|
+
if access == "instance":
|
|
138
|
+
require_live(db, actor)
|
|
139
|
+
elif actor.browser_session_hash is not None:
|
|
140
|
+
binding = db.execute(
|
|
141
|
+
"""SELECT expires_at > clock_timestamp() AS live FROM agent_chat.browser_sessions
|
|
142
|
+
WHERE token_hash = %s AND principal_id = %s""",
|
|
143
|
+
(actor.browser_session_hash, actor.principal_id),
|
|
144
|
+
).fetchone()
|
|
145
|
+
if binding is None or not binding["live"]:
|
|
146
|
+
raise ChatError("UNAUTHENTICATED")
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def require_live(db: Connection[dict[str, Any]], actor: Actor) -> None:
|
|
150
|
+
"""Refuse an ended or expired lease. Call it after every lock wait the operation can still incur:
|
|
151
|
+
a WHERE predicate on SELECT FOR UPDATE can be evaluated before its wait, so a late request could
|
|
152
|
+
otherwise act on a lease that expired while it waited."""
|
|
153
|
+
live = db.execute(
|
|
154
|
+
"""
|
|
155
|
+
SELECT ended_at IS NULL AND heartbeat_at > clock_timestamp() - make_interval(secs => %s) AS live
|
|
156
|
+
FROM agent_chat.instances WHERE id = %s
|
|
157
|
+
""",
|
|
158
|
+
(LEASE_SECONDS, actor.instance_id),
|
|
159
|
+
).fetchone()
|
|
160
|
+
assert live is not None
|
|
161
|
+
if not live["live"]:
|
|
162
|
+
raise ChatError("STALE_INSTANCE")
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def lock_agent(db: Connection[dict[str, Any]], actor: Actor, *, allow_closed: bool = False) -> None:
|
|
166
|
+
"""`lock_actor` for operations only an agent instance may perform."""
|
|
167
|
+
if lock_actor(db, actor, allow_closed=allow_closed) != "instance":
|
|
168
|
+
raise ChatError("SESSION_FORBIDDEN")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def authorize_room(db: Connection[dict[str, Any]], actor: Actor, room: UUID, *, read: bool = False) -> Access:
|
|
172
|
+
"""Viewer authority is opt-in for reads; every default path still refuses viewer operations.
|
|
173
|
+
|
|
174
|
+
Always derive access from locked rows, never from a caller-supplied operator shortcut.
|
|
175
|
+
"""
|
|
176
|
+
access = lock_actor(db, actor, allow_closed=True, read=read)
|
|
177
|
+
allowed = db.execute(
|
|
178
|
+
"""
|
|
179
|
+
SELECT 1 FROM agent_chat.principal_rooms WHERE principal_id = %s AND room_id = %s FOR SHARE
|
|
180
|
+
""",
|
|
181
|
+
(actor.principal_id, room),
|
|
182
|
+
).fetchone()
|
|
183
|
+
member = (
|
|
184
|
+
access in ("operator", "viewer")
|
|
185
|
+
or db.execute(
|
|
186
|
+
"""
|
|
187
|
+
SELECT 1 FROM agent_chat.memberships WHERE session_id = %s AND room_id = %s FOR SHARE
|
|
188
|
+
""",
|
|
189
|
+
(actor.session_id, room),
|
|
190
|
+
).fetchone()
|
|
191
|
+
is not None
|
|
192
|
+
)
|
|
193
|
+
require_current(db, actor, access)
|
|
194
|
+
if allowed is None or not member:
|
|
195
|
+
raise ChatError("ROOM_FORBIDDEN")
|
|
196
|
+
return access
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def member(db: Connection[dict[str, Any]], room: UUID, session: UUID) -> bool:
|
|
200
|
+
"""Whether the session is still authorized in the room; lock the rows that say so."""
|
|
201
|
+
return (
|
|
202
|
+
db.execute(
|
|
203
|
+
"""
|
|
204
|
+
SELECT s.id FROM agent_chat.sessions s
|
|
205
|
+
JOIN agent_chat.principals p ON p.id = s.principal_id
|
|
206
|
+
JOIN agent_chat.principal_rooms pr ON pr.principal_id = p.id
|
|
207
|
+
JOIN agent_chat.memberships m ON m.session_id = s.id AND m.room_id = pr.room_id
|
|
208
|
+
WHERE s.id = %s AND m.room_id = %s AND NOT p.revoked
|
|
209
|
+
FOR SHARE OF s, p, pr, m
|
|
210
|
+
""",
|
|
211
|
+
(session, room),
|
|
212
|
+
).fetchone()
|
|
213
|
+
is not None
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def authorize_message_room(
|
|
218
|
+
db: Connection[dict[str, Any]], actor: Actor, room: UUID
|
|
219
|
+
) -> Access:
|
|
220
|
+
"""`authorize_room` for a room learned from a message ID: a refusal reads as NOT_FOUND, never as existence."""
|
|
221
|
+
try:
|
|
222
|
+
return authorize_room(db, actor, room)
|
|
223
|
+
except ChatError as error:
|
|
224
|
+
if error.code == "ROOM_FORBIDDEN":
|
|
225
|
+
raise ChatError("NOT_FOUND") from None
|
|
226
|
+
raise
|
agent_chat/browser.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""Opaque browser sessions and purpose-separated CSRF values; no browser credential storage."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hmac
|
|
6
|
+
import re
|
|
7
|
+
import secrets
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from hashlib import sha256
|
|
10
|
+
from typing import Any, Literal
|
|
11
|
+
|
|
12
|
+
from psycopg import Connection
|
|
13
|
+
|
|
14
|
+
from .auth import lock_actor
|
|
15
|
+
from .models import Actor, ChatError
|
|
16
|
+
from .protocol import token_digest
|
|
17
|
+
|
|
18
|
+
SESSION_COOKIE = "__Host-agent-chat-session"
|
|
19
|
+
LOGIN_COOKIE = "__Host-agent-chat-login"
|
|
20
|
+
SESSION_SECONDS = 28800
|
|
21
|
+
VIEWER_SESSION_SECONDS = 600
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class LoginSession:
|
|
26
|
+
token: str = field(repr=False)
|
|
27
|
+
access: Literal["operator", "viewer"]
|
|
28
|
+
expires_in: int
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def csrf(token: str) -> str:
|
|
32
|
+
token_digest(token)
|
|
33
|
+
return hmac.new(token.encode("ascii"), b"agent-chat-csrf-v1", sha256).hexdigest()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def check_csrf(token: str, supplied: str) -> None:
|
|
37
|
+
if re.fullmatch(r"[0-9a-f]{64}", supplied) is None or not secrets.compare_digest(csrf(token), supplied):
|
|
38
|
+
raise ChatError("CSRF_FORBIDDEN")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def authenticate_cookie(db: Connection[dict[str, Any]], token: str) -> Actor:
|
|
42
|
+
digest = token_digest(token)
|
|
43
|
+
binding = db.execute(
|
|
44
|
+
"""
|
|
45
|
+
SELECT b.principal_id FROM agent_chat.browser_sessions b
|
|
46
|
+
JOIN agent_chat.principals p ON p.id = b.principal_id
|
|
47
|
+
WHERE b.token_hash = %s AND b.expires_at > clock_timestamp()
|
|
48
|
+
AND p.kind IN ('operator', 'viewer') AND NOT p.revoked
|
|
49
|
+
""",
|
|
50
|
+
(digest,),
|
|
51
|
+
).fetchone()
|
|
52
|
+
if binding is None:
|
|
53
|
+
raise ChatError("UNAUTHENTICATED")
|
|
54
|
+
return Actor(binding["principal_id"], None, None, browser_session_hash=digest)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def login(db: Connection[dict[str, Any]], token: str, previous_token: str | None = None) -> LoginSession:
|
|
58
|
+
failure = None
|
|
59
|
+
result = None
|
|
60
|
+
with db.transaction():
|
|
61
|
+
db.execute("SELECT singleton FROM agent_chat.login_guard WHERE singleton FOR UPDATE")
|
|
62
|
+
# Evaluate the window after lock acquisition. The bounded array survives restart.
|
|
63
|
+
guard = db.execute("""
|
|
64
|
+
UPDATE agent_chat.login_guard SET failures = ARRAY(
|
|
65
|
+
SELECT happened FROM unnest(failures) happened
|
|
66
|
+
WHERE happened > clock_timestamp() - interval '60 seconds'
|
|
67
|
+
) WHERE singleton RETURNING cardinality(failures) AS failures
|
|
68
|
+
""").fetchone()
|
|
69
|
+
if guard is None:
|
|
70
|
+
raise ChatError("SCHEMA_MISMATCH")
|
|
71
|
+
if guard["failures"] >= 10:
|
|
72
|
+
failure = "LOGIN_RATE_LIMITED"
|
|
73
|
+
else:
|
|
74
|
+
try:
|
|
75
|
+
# Login is a separate credential purpose; no instance, host or producer fallback.
|
|
76
|
+
principal = db.execute(
|
|
77
|
+
"""SELECT id, kind FROM agent_chat.principals
|
|
78
|
+
WHERE credential_hash = %s AND kind IN ('operator', 'viewer') AND NOT revoked
|
|
79
|
+
AND NOT EXISTS (SELECT 1 FROM agent_chat.instances WHERE token_hash = %s) FOR SHARE""",
|
|
80
|
+
(token_digest(token), token_digest(token)),
|
|
81
|
+
).fetchone()
|
|
82
|
+
if principal is None:
|
|
83
|
+
raise ChatError("UNAUTHENTICATED")
|
|
84
|
+
except ChatError:
|
|
85
|
+
db.execute("""
|
|
86
|
+
UPDATE agent_chat.login_guard SET failures = array_append(failures, clock_timestamp())
|
|
87
|
+
WHERE singleton
|
|
88
|
+
""")
|
|
89
|
+
failure = "UNAUTHENTICATED"
|
|
90
|
+
else:
|
|
91
|
+
session_token = secrets.token_hex(32)
|
|
92
|
+
seconds = VIEWER_SESSION_SECONDS if principal["kind"] == "viewer" else SESSION_SECONDS
|
|
93
|
+
result = LoginSession(session_token, principal["kind"], seconds)
|
|
94
|
+
db.execute(
|
|
95
|
+
"""INSERT INTO agent_chat.browser_sessions(token_hash, principal_id, created_at, expires_at)
|
|
96
|
+
SELECT %s, %s, now, now + make_interval(secs => %s)
|
|
97
|
+
FROM (SELECT clock_timestamp() AS now) clock""",
|
|
98
|
+
(token_digest(session_token), principal["id"], seconds),
|
|
99
|
+
)
|
|
100
|
+
if previous_token is not None and re.fullmatch(r"[0-9a-f]{64}", previous_token):
|
|
101
|
+
# Whoever owned the presented cookie: logging in as another principal ends it too.
|
|
102
|
+
db.execute(
|
|
103
|
+
"DELETE FROM agent_chat.browser_sessions WHERE token_hash = %s",
|
|
104
|
+
(token_digest(previous_token),),
|
|
105
|
+
)
|
|
106
|
+
# Raising inside the transaction would roll back the failed-login counter.
|
|
107
|
+
if failure is not None:
|
|
108
|
+
raise ChatError(failure)
|
|
109
|
+
assert result is not None
|
|
110
|
+
return result
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def logout(db: Connection[dict[str, Any]], actor: Actor) -> None:
|
|
114
|
+
if actor.browser_session_hash is None:
|
|
115
|
+
raise ChatError("UNAUTHENTICATED")
|
|
116
|
+
with db.transaction():
|
|
117
|
+
lock_actor(db, actor, read=True)
|
|
118
|
+
db.execute("DELETE FROM agent_chat.browser_sessions WHERE token_hash = %s", (actor.browser_session_hash,))
|