feat: carry lab 2 over and add X-Instance-Id and idempotency keys

- carry the lab 2 solution over and plan lab 3 (PLAN.md, OpenSpec
  change instance-id-idempotency)
- rename the compose project to mtusi-lab3 and the image to :lab3
- add X-Instance-Id to every response and instance_id to every log
  record ([app] instance_id, hostname by default)
- require Idempotency-Key on POST messages: keys table in Postgres,
  key and message in one transaction, replay returns the same 201,
  another body gets 409, TTL from [idempotency] ttl_seconds
- send the key from student-tests, k6 scenarios and the skill driver;
  add idempotency, X-Instance-Id, key race and fault tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 19:42:36 +03:00
co-authored by Claude Opus 5.5
parent 0ab3a341aa
commit af4bca08c9
294 changed files with 36586 additions and 15 deletions
View File
+18
View File
@@ -0,0 +1,18 @@
from .chat import ChatFullDTO, ChatPageDTO
from .idempotency import IdempotencyRecordDTO
from .message import MessageFullDTO, MessagePageDTO
from .session import CurrentSessionDTO, SessionFullDTO, SessionTokenDTO
from .user import UserCredentialsDTO, UserFullDTO
__all__ = [
"ChatFullDTO",
"ChatPageDTO",
"CurrentSessionDTO",
"IdempotencyRecordDTO",
"MessageFullDTO",
"MessagePageDTO",
"SessionFullDTO",
"SessionTokenDTO",
"UserCredentialsDTO",
"UserFullDTO",
]
+20
View File
@@ -0,0 +1,20 @@
from dataclasses import dataclass
from datetime import datetime
from uuid import UUID
@dataclass(frozen=True, slots=True)
class ChatFullDTO:
id: UUID
title: str
# в порядке, переданном при создании
member_ids: tuple[UUID, ...]
created_at: datetime
@dataclass(frozen=True, slots=True)
class ChatPageDTO:
"""Страница списка чатов пользователя."""
items: tuple[ChatFullDTO, ...]
next_cursor: str | None
+11
View File
@@ -0,0 +1,11 @@
from dataclasses import dataclass
from .message import MessageFullDTO
@dataclass(frozen=True, slots=True)
class IdempotencyRecordDTO:
"""Сохранённый результат ключа: хэш тела и сообщение первой отправки."""
request_hash: bytes
message: MessageFullDTO
+20
View File
@@ -0,0 +1,20 @@
from dataclasses import dataclass
from datetime import datetime
from uuid import UUID
@dataclass(frozen=True, slots=True)
class MessageFullDTO:
id: UUID
chat_id: UUID
sender_id: UUID
text: str
created_at: datetime
@dataclass(frozen=True, slots=True)
class MessagePageDTO:
"""Страница истории чата."""
items: tuple[MessageFullDTO, ...]
next_cursor: str | None
+33
View File
@@ -0,0 +1,33 @@
from dataclasses import dataclass
from datetime import datetime
from uuid import UUID
from .user import UserFullDTO
@dataclass(frozen=True, slots=True)
class SessionFullDTO:
"""Строка сессии: SHA-256 токена, а не сам токен."""
id: UUID
user_id: UUID
token_hash: bytes
expires_at: datetime
created_at: datetime
@dataclass(frozen=True, slots=True)
class SessionTokenDTO:
"""Результат входа: токен существует только здесь и в ответе API."""
token: str
expires_at: datetime
user: UserFullDTO
@dataclass(frozen=True, slots=True)
class CurrentSessionDTO:
"""Действующая сессия запроса и её владелец."""
id: UUID
user: UserFullDTO
+21
View File
@@ -0,0 +1,21 @@
from dataclasses import dataclass
from datetime import datetime
from uuid import UUID
@dataclass(frozen=True, slots=True)
class UserFullDTO:
"""Пользователь без хэша пароля: в таком виде он уходит в API."""
id: UUID
username: str
display_name: str
created_at: datetime
@dataclass(frozen=True, slots=True)
class UserCredentialsDTO:
"""Пользователь и хэш пароля — только для входа."""
user: UserFullDTO
password_hash: str
+5
View File
@@ -0,0 +1,5 @@
from .settings import RunModeEnum
__all__ = [
"RunModeEnum",
]
+8
View File
@@ -0,0 +1,8 @@
from enum import StrEnum
class RunModeEnum(StrEnum):
"""Режим запуска: console — на хосте, docker — в контейнере."""
CONSOLE = "console"
DOCKER = "docker"