File size: 2,130 Bytes
9291ded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""In-memory chat history per session (ADR-010 decision 1).
Process memory: uvicorn must run with exactly 1 worker. A restart deletes all sessions."""
import secrets
import threading
import time
from dataclasses import dataclass, field

TTL = 30 * 60  # seconds, absolute, the same as the session token
MAX_SESSIONS = 500
MAX_TURNS = 20  # stored user turns; turn 21 gets SESSION_LIMIT
CONTEXT_TURNS = 10  # last turns that go to the model


@dataclass
class Session:
    expires_at: float
    messages: list[tuple[str, str]] = field(default_factory=list)  # ("user" | "assistant", text)
    lang: str = "en"
    turns: int = 0
    busy: bool = False


class SessionStore:
    def __init__(self, clock=time.time):
        self.clock = clock
        self._lock = threading.Lock()
        self._s: dict[str, Session] = {}

    def _sweep(self):
        now = self.clock()
        for sid in [k for k, v in self._s.items() if v.expires_at <= now]:
            del self._s[sid]

    def create(self) -> tuple[str, float]:
        with self._lock:
            self._sweep()
            if len(self._s) >= MAX_SESSIONS:  # full: evict the session that expires first
                del self._s[min(self._s, key=lambda k: self._s[k].expires_at)]
            sid, exp = secrets.token_urlsafe(16), self.clock() + TTL
            self._s[sid] = Session(expires_at=exp)
            return sid, exp

    def get(self, sid: str) -> Session | None:
        with self._lock:
            self._sweep()
            return self._s.get(sid)

    def acquire(self, sid: str) -> Session | None | bool:
        """The session, marked busy. None = unknown or expired, False = a request for it is running."""
        with self._lock:
            self._sweep()
            s = self._s.get(sid)
            if s is None:
                return None
            if s.busy:
                return False
            s.busy = True
            return s

    def release(self, s: Session):
        with self._lock:
            s.busy = False

    @staticmethod
    def context(s: Session) -> list[tuple[str, str]]:
        return s.messages[-2 * CONTEXT_TURNS:]