diff --git a/.dockerignore b/.dockerignore
index bc16e556..ef48c580 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,8 +1,8 @@
.git
.github
+.gitignore
.env
.env.*
-!.env.example
secrets
certs
volumes
@@ -17,4 +17,17 @@ backups
__pycache__
node_modules
evidence
+examples
+scripts
+tests
+*.md
+Dockerfile*
+Makefile
+requirements-dev.txt
+lab.json
.DS_Store
+
+.idea
+.vscode
+.agents
+.codex
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 00000000..7cd6ea0a
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,60 @@
+FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS goose-builder
+RUN GOBIN=/out CGO_ENABLED=0 go install \
+ -tags='no_clickhouse no_libsql no_mssql no_mysql no_sqlite3 no_vertica no_ydb no_spanner' \
+ github.com/pressly/goose/v3/cmd/goose@v3.27.1
+
+FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS migrations
+COPY --from=goose-builder /out/goose /usr/local/bin/goose
+COPY migrations /migrations
+COPY infra/migrate.sh /usr/local/bin/migrate
+USER 10001:10001
+ENTRYPOINT ["/usr/local/bin/migrate"]
+CMD ["up"]
+
+FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS builder
+
+WORKDIR /src
+
+RUN apk add --no-cache gcc musl-dev
+
+COPY go.mod go.sum ./
+RUN go mod download
+
+COPY cmd ./cmd
+COPY contracts ./contracts
+COPY dto ./dto
+COPY internal ./internal
+COPY migrations ./migrations
+
+RUN CGO_ENABLED=0 GOOS=linux go build \
+ -trimpath \
+ -ldflags="-s -w" \
+ -o /out/messenger ./cmd
+RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/reconcile ./cmd/reconcile
+
+FROM builder AS tests
+COPY --from=goose-builder /out/goose /usr/local/bin/goose
+ENV GOOSE_MIGRATION_DIR=/src/migrations
+CMD ["go", "test", "-p=1", "-race", "-count=1", "-v", "./..."]
+
+FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616
+
+RUN addgroup -S messenger \
+ && adduser -S -D -H -u 10001 -G messenger messenger
+
+COPY --from=builder --chown=messenger:messenger /out/messenger /usr/local/bin/messenger
+
+COPY --from=builder --chown=messenger:messenger /out/reconcile /usr/local/bin/reconcile
+
+ENV PORT=8080
+
+EXPOSE 8080
+
+USER messenger:messenger
+
+HEALTHCHECK --interval=10s --timeout=3s --start-period=3s --retries=3 \
+ CMD wget -q -O /dev/null "http://127.0.0.1:${PORT}/health/ready" || exit 1
+
+STOPSIGNAL SIGTERM
+
+ENTRYPOINT ["/usr/local/bin/messenger"]
diff --git a/Dockerfile.storage b/Dockerfile.storage
new file mode 100644
index 00000000..8b7f7d0a
--- /dev/null
+++ b/Dockerfile.storage
@@ -0,0 +1,34 @@
+FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS minio-builder
+ADD --checksum=sha256:8b11129f6a6830768bcdebab569290ad956618f5ff6ef4857b633acb64f95126 https://codeload.github.com/minio/minio/tar.gz/01ce918d8279a20e4706b96a64396146894adee4 /tmp/source.tar.gz
+RUN mkdir /src && tar -xzf /tmp/source.tar.gz -C /src --strip-components=1
+WORKDIR /src
+RUN CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o /out/minio .
+RUN echo '8b11129f6a6830768bcdebab569290ad956618f5ff6ef4857b633acb64f95126 /tmp/source.tar.gz' | sha256sum -c -
+
+FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS mc-builder
+ADD --checksum=sha256:4cea8ed209c8f96e59ea1a5cc4901019b54c2be6d6f5f16c6749ea8bdcf4172e https://codeload.github.com/minio/mc/tar.gz/d6541ea280b73a834b64d4097e21f2be77676104 /tmp/source.tar.gz
+RUN mkdir /src && tar -xzf /tmp/source.tar.gz -C /src --strip-components=1
+WORKDIR /src
+RUN CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o /out/mc .
+RUN echo '4cea8ed209c8f96e59ea1a5cc4901019b54c2be6d6f5f16c6749ea8bdcf4172e /tmp/source.tar.gz' | sha256sum -c -
+
+FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS minio
+LABEL org.opencontainers.image.source="https://github.com/minio/minio" \
+ org.opencontainers.image.revision="01ce918d8279a20e4706b96a64396146894adee4" \
+ org.opencontainers.image.version="RELEASE.2025-09-07T16-13-09Z" \
+ org.opencontainers.image.licenses="AGPL-3.0"
+RUN mkdir /data && chown 10001:10001 /data
+COPY --from=minio-builder /out/minio /usr/local/bin/minio
+COPY infra/minio-entrypoint.sh /usr/local/bin/start-minio
+USER 10001:10001
+EXPOSE 9000
+ENTRYPOINT ["/usr/local/bin/start-minio"]
+
+FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS mc
+LABEL org.opencontainers.image.source="https://github.com/minio/mc" \
+ org.opencontainers.image.revision="d6541ea280b73a834b64d4097e21f2be77676104" \
+ org.opencontainers.image.version="RELEASE.2025-08-13T08-35-41Z" \
+ org.opencontainers.image.licenses="AGPL-3.0"
+COPY --from=mc-builder /out/mc /usr/local/bin/mc
+USER 10001:10001
+ENTRYPOINT ["mc"]
diff --git a/Makefile b/Makefile
index 5d86d171..8c7ec355 100644
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,21 @@
-PYTHON ?= python3
-BASE_URL ?= http://localhost:8080
+PYTHON ?= $(if $(wildcard .venv/bin/python),.venv/bin/python,python3)
+GO ?= go
+PORT ?= 8080
+BASE_URL ?= http://localhost:$(PORT)
+IMAGE ?= mtusi-messenger:lab4
CA_FILE ?=
-ACTION_SCRIPT ?=
+ACTION_SCRIPT ?= scripts/restart.sh
+FAILOVER_SCRIPT ?= scripts/stop-one.sh
+RECONCILE_ARGS ?=
+export PYTHONDONTWRITEBYTECODE=1
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
-.PHONY: help check validate test load test-persistence test-failover
+.PHONY: help run docker-build docker-run check validate test test-own test-all load test-persistence bootstrap up down migrate test-integration test-grade4 test-lab4 test-reconcile measure-uploads measure-load migrate-status rolling-update test-failover wait bootstrap-s3 reconcile reconcile-apply
+
+export PORT BASE_URL
help:
- @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
+ @echo "up: build and deploy Swarm stack; down: stop with volumes retained; test-integration: PostgreSQL race/rollback/EXPLAIN; test-grade4: cross-replica idempotency, both failures and rolling update; check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
check:
$(PYTHON) scripts/check_contract.py
@@ -18,6 +26,11 @@ validate:
test:
$(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)
+test-own:
+ $(GO) test -race ./...
+
+test-all: check validate test-own
+
load:
$(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
@@ -25,6 +38,59 @@ test-persistence:
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
+bootstrap:
+ ./scripts/bootstrap.sh
+
+up:
+ $(PYTHON) scripts/swarm.py up
+
+down:
+ $(PYTHON) scripts/swarm.py down
+
+migrate:
+ $(PYTHON) scripts/swarm.py migrate
+
+migrate-status:
+ $(PYTHON) scripts/swarm.py migrate-status
+
+rolling-update:
+ $(PYTHON) scripts/swarm.py update
+
+run:
+ PORT="$(PORT)" $(GO) run ./cmd
+
+docker-build:
+ docker build -t "$(IMAGE)" .
+
+test-integration:
+ $(PYTHON) scripts/swarm.py test-integration
+
+test-grade4:
+ $(PYTHON) tests/own/swarm_grade4.py
+
+test-lab4:
+ $(PYTHON) tests/own/lab4.py
+
+test-reconcile:
+ $(PYTHON) tests/own/reconcile.py
+
+measure-uploads:
+ $(PYTHON) scripts/measure_uploads.py
+
+bootstrap-s3:
+ $(PYTHON) scripts/swarm.py bootstrap-s3
+
+reconcile:
+ $(PYTHON) scripts/swarm.py reconcile $(RECONCILE_ARGS)
+
+reconcile-apply:
+ $(PYTHON) scripts/swarm.py reconcile --apply $(RECONCILE_ARGS)
+
+measure-load:
+ $(PYTHON) scripts/measure_load.py --base-url "$(BASE_URL)"
+
test-failover:
- @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
- $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
+ $(PYTHON) tests/resilience.py --mode failover --action-script "$(if $(filter command line,$(origin ACTION_SCRIPT)),$(ACTION_SCRIPT),$(FAILOVER_SCRIPT))" --base-url "$(BASE_URL)" $(TLS_ARGS)
+
+wait:
+ $(PYTHON) scripts/swarm.py wait
diff --git a/cmd/main.go b/cmd/main.go
new file mode 100644
index 00000000..6f1f721d
--- /dev/null
+++ b/cmd/main.go
@@ -0,0 +1,105 @@
+package main
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "log"
+ "net/http"
+ "os"
+ "os/signal"
+ "syscall"
+ "time"
+
+ "github.com/jackc/pgx/v5/pgxpool"
+ "github.com/redis/go-redis/v9"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/app/chat"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/attachment"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository/postgres"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/session"
+ chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat"
+)
+
+const shutdownTimeout = 8 * time.Second
+
+func main() {
+ cfg, err := config.Load()
+ if err != nil {
+ log.Fatal(err)
+ }
+ poolConfig, err := pgxpool.ParseConfig("sslmode=disable")
+ if err != nil {
+ log.Fatal("invalid PostgreSQL config")
+ }
+ poolConfig.ConnConfig.Host = cfg.PostgresHost
+ poolConfig.ConnConfig.Port = cfg.PostgresPort
+ poolConfig.ConnConfig.User = cfg.PostgresUser
+ poolConfig.ConnConfig.Database = cfg.PostgresDatabase
+ poolConfig.ConnConfig.Password = cfg.PostgresPassword
+ poolConfig.ConnConfig.ConnectTimeout = cfg.QueryTimeout
+ poolConfig.ConnConfig.RuntimeParams["timezone"] = "UTC"
+ poolConfig.ConnConfig.RuntimeParams["statement_timeout"] = fmt.Sprint(cfg.QueryTimeout.Milliseconds())
+ poolConfig.MaxConns = cfg.PostgresMaxConns
+ poolConfig.MinConns = 1
+ poolConfig.MaxConnLifetime = time.Hour
+ poolConfig.MaxConnIdleTime = 5 * time.Minute
+ pool, err := pgxpool.NewWithConfig(context.Background(), poolConfig)
+ if err != nil {
+ log.Fatal("cannot create PostgreSQL pool")
+ }
+ defer pool.Close()
+ redisClient := redis.NewClient(&redis.Options{
+ Addr: cfg.RedisAddr, PoolSize: 10, MaxActiveConns: 10, PoolTimeout: cfg.QueryTimeout,
+ DialTimeout: cfg.QueryTimeout, ReadTimeout: cfg.QueryTimeout, WriteTimeout: cfg.QueryTimeout,
+ MaxRetries: -1, ContextTimeoutEnabled: true,
+ })
+ defer redisClient.Close()
+ repo := postgres.New(pool, cfg.QueryTimeout)
+ sessions := session.NewRedis(redisClient, cfg.QueryTimeout)
+ useCase := chatusecase.New(repo, sessions, cfg.SessionTTL)
+ objectStore, err := attachment.NewStore(cfg.S3Endpoint, cfg.S3Bucket, cfg.S3AccessKey, cfg.S3SecretKey, cfg.S3UseSSL)
+ if err != nil {
+ log.Fatal("invalid S3 config")
+ }
+ handler := chat.NewChat(useCase, cfg.CursorKey).WithAttachments(attachment.New(pool, objectStore))
+ port := cfg.Port
+ e := handler.Router()
+ e.Server.ReadHeaderTimeout = 5 * time.Second
+ e.Server.ReadTimeout = 50 * time.Second
+ e.Server.WriteTimeout = 55 * time.Second
+ e.Server.IdleTimeout = 60 * time.Second
+
+ serverErrors := make(chan error, 1)
+ go func() {
+ log.Printf("HTTP server is listening on :%s", port)
+ serverErrors <- e.Start(":" + port)
+ }()
+
+ stop := make(chan os.Signal, 1)
+ signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
+ select {
+ case sig := <-stop:
+ log.Printf("received %s, shutting down", sig)
+ case err := <-serverErrors:
+ if !errors.Is(err, http.ErrServerClosed) {
+ log.Fatalf("HTTP server failed: %v", err)
+ }
+ return
+ }
+
+ handler.Drain()
+ // Allow two 1-second LB checks to remove this backend before closing the listener.
+ time.Sleep(3 * time.Second)
+ ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
+ defer cancel()
+ if err := e.Shutdown(ctx); err != nil {
+ log.Printf("graceful shutdown failed: %v", err)
+ if closeErr := e.Close(); closeErr != nil {
+ log.Printf("forced shutdown failed: %v", closeErr)
+ }
+ return
+ }
+ log.Printf("shutdown complete")
+}
diff --git a/cmd/reconcile/main.go b/cmd/reconcile/main.go
new file mode 100644
index 00000000..2534aa6c
--- /dev/null
+++ b/cmd/reconcile/main.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+ "context"
+ "encoding/json"
+ "flag"
+ "log"
+ "os"
+ "time"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/attachment"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config"
+ "github.com/jackc/pgx/v5/pgxpool"
+)
+
+func main() {
+ apply := flag.Bool("apply", false, "apply safe repairs; default dry-run")
+ grace := flag.Duration("grace", 2*time.Minute, "minimum object/metadata age (at least 90s)")
+ timeout := flag.Duration("timeout", 10*time.Minute, "whole scan deadline")
+ flag.Parse()
+ if *grace < attachment.MinimumGrace {
+ log.Fatal("grace must be at least 90s")
+ }
+ cfg, err := config.Load()
+ if err != nil {
+ log.Fatal(err)
+ }
+ pc, err := pgxpool.ParseConfig("sslmode=disable")
+ if err != nil {
+ log.Fatal("invalid DB config")
+ }
+ pc.ConnConfig.Host = cfg.PostgresHost
+ pc.ConnConfig.Port = cfg.PostgresPort
+ pc.ConnConfig.User = cfg.PostgresUser
+ pc.ConnConfig.Password = cfg.PostgresPassword
+ pc.ConnConfig.Database = cfg.PostgresDatabase
+ pc.ConnConfig.ConnectTimeout = 3 * time.Second
+ pc.ConnConfig.RuntimeParams["statement_timeout"] = "5000"
+ pc.MaxConns = 2
+ ctx, cancel := context.WithTimeout(context.Background(), *timeout)
+ defer cancel()
+ pool, err := pgxpool.NewWithConfig(ctx, pc)
+ if err != nil {
+ log.Fatal("cannot create DB pool")
+ }
+ defer pool.Close()
+ store, err := attachment.NewStore(cfg.S3Endpoint, cfg.S3Bucket, cfg.S3AccessKey, cfg.S3SecretKey, cfg.S3UseSSL)
+ if err != nil {
+ log.Fatal("invalid S3 config")
+ }
+ result, err := attachment.New(pool, store).Reconcile(ctx, *apply, *grace)
+ _ = json.NewEncoder(os.Stdout).Encode(result)
+ if err != nil {
+ log.Print(err)
+ os.Exit(1)
+ }
+}
diff --git a/compose.example.yaml b/compose.example.yaml
index 792b3f4e..76a785ff 100644
--- a/compose.example.yaml
+++ b/compose.example.yaml
@@ -1,14 +1,12 @@
# Topology worksheet, deliberately NOT a runnable stack (no application supplied).
# Implement compose.yaml or stack.yaml; do not submit this empty file as deployment.
# Service names below are logical roles, not imposed container names.
-x-lab-number: 5
+x-lab-number: 4
x-required-roles:
- api
- postgres
- load_balancer
- s3
- - broker
- - worker
# TODO: implement real images/builds, network wiring, volumes, healthchecks,
# credentials and bootstrap. Details: contracts/infrastructure.json and README.md.
services: {}
diff --git a/compose.yaml b/compose.yaml
new file mode 100644
index 00000000..4c221cd3
--- /dev/null
+++ b/compose.yaml
@@ -0,0 +1,161 @@
+name: mtusi2026lab4
+services:
+ postgres:
+ image: postgres:17.6-alpine3.22
+ environment:
+ POSTGRES_USER: messenger
+ POSTGRES_DB: messenger
+ POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
+ secrets:
+ - postgres_password
+ volumes:
+ - postgres_data:/var/lib/postgresql/data
+ healthcheck:
+ test: [CMD-SHELL, 'pg_isready -U messenger -d messenger']
+ interval: 2s
+ timeout: 2s
+ retries: 30
+ cpus: 1.0
+ mem_limit: 512m
+ redis:
+ image: redis:7.4.5-alpine3.21
+ command: [redis-server, /usr/local/etc/redis/redis.conf]
+ volumes:
+ - redis_data:/data
+ - ./infra/redis.conf:/usr/local/etc/redis/redis.conf:ro
+ healthcheck:
+ test: [CMD, redis-cli, ping]
+ interval: 2s
+ timeout: 2s
+ retries: 30
+ cpus: 0.5
+ mem_limit: 128m
+ minio:
+ image: mtusi-minio:RELEASE.2025-09-07T16-13-09Z
+ build:
+ context: .
+ dockerfile: Dockerfile.storage
+ target: minio
+ secrets: [s3_root_access_key, s3_root_secret_key]
+ volumes: [s3_data:/data]
+ healthcheck:
+ test: [CMD, wget, -q, -T, '2', -O, /dev/null, 'http://127.0.0.1:9000/minio/health/live']
+ interval: 2s
+ timeout: 3s
+ start_period: 15s
+ retries: 30
+ cpus: 1.0
+ mem_limit: 512m
+ stop_grace_period: 20s
+ s3_bootstrap:
+ image: mtusi-mc:RELEASE.2025-08-13T08-35-41Z
+ build:
+ context: .
+ dockerfile: Dockerfile.storage
+ target: mc
+ entrypoint: [/bin/sh, /bootstrap.sh]
+ configs:
+ - source: s3_bootstrap
+ target: /bootstrap.sh
+ - source: s3_policy
+ target: /policy.json
+ secrets: [s3_root_access_key, s3_root_secret_key, s3_access_key, s3_secret_key]
+ depends_on:
+ minio:
+ condition: service_healthy
+ cpus: 0.5
+ mem_limit: 512m
+ migrations:
+ image: mtusi-migrations:lab4
+ build:
+ context: .
+ target: migrations
+ environment:
+ GOOSE_DRIVER: postgres
+ GOOSE_DBSTRING: host=postgres user=messenger dbname=messenger sslmode=disable connect_timeout=5
+ POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
+ secrets:
+ - postgres_password
+ depends_on:
+ postgres:
+ condition: service_healthy
+ cpus: 0.5
+ mem_limit: 128m
+ api:
+ build: .
+ image: mtusi-messenger:lab4
+ environment: &api_environment
+ POSTGRES_HOST: postgres
+ POSTGRES_USER: messenger
+ POSTGRES_DB: messenger
+ POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
+ POSTGRES_MAX_CONNS: '10'
+ REDIS_ADDR: redis:6379
+ CURSOR_KEY_FILE: /run/secrets/cursor_key
+ SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
+ QUERY_TIMEOUT_MS: '2000'
+ S3_ENDPOINT: minio:9000
+ S3_BUCKET: attachments
+ S3_ACCESS_KEY_FILE: /run/secrets/s3_access_key
+ S3_SECRET_KEY_FILE: /run/secrets/s3_secret_key
+ S3_USE_SSL: 'false'
+ GOMEMLIMIT: 1152MiB
+ GOGC: '50'
+ secrets: &api_secrets
+ - postgres_password
+ - cursor_key
+ - s3_access_key
+ - s3_secret_key
+ ports:
+ - '127.0.0.1:${PORT:-8080}:8080'
+ depends_on:
+ postgres:
+ condition: service_healthy
+ redis:
+ condition: service_healthy
+ migrations:
+ condition: service_completed_successfully
+ s3_bootstrap:
+ condition: service_completed_successfully
+ cpus: 1.0
+ mem_limit: 1536m
+ stop_grace_period: 10s
+ tests:
+ image: mtusi-messenger-tests:lab4
+ profiles: [test]
+ build:
+ context: .
+ target: tests
+ environment:
+ <<: *api_environment
+ INTEGRATION_TESTS: '1'
+ secrets: *api_secrets
+ depends_on:
+ postgres:
+ condition: service_healthy
+ redis:
+ condition: service_healthy
+ cpus: 1.0
+ mem_limit: 1g
+configs:
+ s3_bootstrap:
+ file: ./infra/s3-bootstrap.sh
+ s3_policy:
+ file: ./infra/s3-policy.json
+secrets:
+ s3_root_access_key:
+ file: ./secrets/s3_root_access_key
+ s3_root_secret_key:
+ file: ./secrets/s3_root_secret_key
+ s3_access_key:
+ file: ./secrets/s3_access_key
+ s3_secret_key:
+ file: ./secrets/s3_secret_key
+ postgres_password:
+ file: ./secrets/postgres_password
+ cursor_key:
+ file: ./secrets/cursor_key
+volumes:
+ postgres_data:
+ redis_data:
+ s3_data:
diff --git a/contracts/embed.go b/contracts/embed.go
new file mode 100644
index 00000000..ef4b85ae
--- /dev/null
+++ b/contracts/embed.go
@@ -0,0 +1,9 @@
+package contracts
+
+import _ "embed"
+
+// OpenAPI is embedded so Swagger UI works from a compiled binary and does not
+// depend on the process working directory.
+//
+//go:embed openapi.json
+var OpenAPI []byte
diff --git a/contracts/infrastructure.json b/contracts/infrastructure.json
index a4cfcb7b..dd2edf2d 100644
--- a/contracts/infrastructure.json
+++ b/contracts/infrastructure.json
@@ -1,5 +1,5 @@
{
- "lab": 5,
+ "lab": 4,
"kind": "requirements, not runnable deployment",
"required_for_grade_3": [
{
@@ -27,22 +27,6 @@
],
"private_bucket": true,
"persistent_volume": true
- },
- {
- "role": "broker",
- "choose_one": [
- "RabbitMQ",
- "Redis Streams",
- "Valkey Streams",
- "NATS JetStream",
- "documented durable equivalent"
- ],
- "persistent_volume": true,
- "delivery": "at-least-once"
- },
- {
- "role": "worker",
- "separate_process": true
}
],
"optional_components": [
diff --git a/contracts/jobs.schema.json b/contracts/jobs.schema.json
deleted file mode 100644
index 2e9aa484..00000000
--- a/contracts/jobs.schema.json
+++ /dev/null
@@ -1,63 +0,0 @@
-{
- "$schema": "https://json-schema.org/draft/2020-12/schema",
- "$id": "urn:mtusi:messenger:attachment-job:v1",
- "title": "Durable attachment processing job v1",
- "type": "object",
- "additionalProperties": false,
- "required": [
- "schema_version",
- "job_id",
- "type",
- "attachment_id",
- "pipeline_version",
- "attempt",
- "created_at"
- ],
- "properties": {
- "schema_version": {
- "const": 1
- },
- "job_id": {
- "type": "string",
- "format": "uuid"
- },
- "type": {
- "const": "attachment.process.v1"
- },
- "attachment_id": {
- "type": "string",
- "format": "uuid"
- },
- "pipeline_version": {
- "type": "integer",
- "minimum": 1
- },
- "attempt": {
- "type": "integer",
- "minimum": 1
- },
- "created_at": {
- "type": "string",
- "format": "date-time"
- },
- "traceparent": {
- "type": "string",
- "pattern": "^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$"
- },
- "tracestate": {
- "type": "string",
- "maxLength": 512
- }
- },
- "examples": [
- {
- "schema_version": 1,
- "job_id": "11111111-1111-4111-8111-111111111111",
- "type": "attachment.process.v1",
- "attachment_id": "22222222-2222-4222-8222-222222222222",
- "pipeline_version": 1,
- "attempt": 1,
- "created_at": "2026-09-01T12:00:00Z"
- }
- ]
-}
diff --git a/contracts/openapi.json b/contracts/openapi.json
index 4face909..138f07c0 100644
--- a/contracts/openapi.json
+++ b/contracts/openapi.json
@@ -1,8 +1,8 @@
{
"openapi": "3.1.0",
"info": {
- "title": "MTUSI Messenger — лабораторная 5",
- "version": "1.5.0",
+ "title": "MTUSI Messenger — лабораторная 4",
+ "version": "1.4.0",
"description": "Нормативный минимум на 3. Семантика: contracts/README.md. Расширения на 4/5 не должны менять этот API. Наличие кода ошибки в схеме не требует реализовать факультативную причину его выдачи."
},
"servers": [
@@ -2151,7 +2151,7 @@
"operationId": "uploadAttachment",
"summary": "Принять один PNG/JPEG, до 10 MiB",
"responses": {
- "202": {
+ "201": {
"description": "Успех",
"headers": {
"X-Request-Id": {
@@ -2936,10 +2936,7 @@
"schema": {
"type": "string",
"enum": [
- "original",
- "thumbnail",
- "crop",
- "optimized"
+ "original"
],
"default": "original"
}
@@ -3464,10 +3461,7 @@
"status": {
"type": "string",
"enum": [
- "queued",
- "processing",
- "ready",
- "failed"
+ "ready"
]
},
"variants": {
diff --git a/dto/create_chat.go b/dto/create_chat.go
new file mode 100644
index 00000000..25c42a03
--- /dev/null
+++ b/dto/create_chat.go
@@ -0,0 +1,13 @@
+package dto
+
+type CreateChatRequest struct {
+ Title string `json:"title"`
+ MemberIDs []string `json:"member_ids"`
+}
+
+type CreateChatResponse struct {
+ ID string `json:"id"`
+ Title string `json:"title"`
+ MemberIDs []string `json:"member_ids"`
+ CreatedAt Timestamp `json:"created_at"`
+}
diff --git a/dto/create_message.go b/dto/create_message.go
new file mode 100644
index 00000000..f89c57c2
--- /dev/null
+++ b/dto/create_message.go
@@ -0,0 +1,16 @@
+package dto
+
+type CreateMessageRequest struct {
+ ChatID string `json:"-"`
+ Text string `json:"text"`
+ AttachmentIDs []string `json:"attachment_ids"`
+}
+
+type CreateMessageResponse struct {
+ ID string `json:"id"`
+ ChatID string `json:"chat_id"`
+ SenderID string `json:"sender_id"`
+ Text string `json:"text"`
+ AttachmentIDs []string `json:"attachment_ids"`
+ CreatedAt Timestamp `json:"created_at"`
+}
diff --git a/dto/create_message_json.go b/dto/create_message_json.go
new file mode 100644
index 00000000..b92e99d4
--- /dev/null
+++ b/dto/create_message_json.go
@@ -0,0 +1,33 @@
+package dto
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+)
+
+// Keep absent attachment_ids equivalent to [], while rejecting JSON null and
+// wrong types rather than quietly treating them as an empty message body.
+func (r *CreateMessageRequest) UnmarshalJSON(data []byte) error {
+ type payload CreateMessageRequest
+ var fields map[string]json.RawMessage
+ if err := json.Unmarshal(data, &fields); err != nil || fields == nil {
+ return errors.New("Ожидается JSON-объект")
+ }
+ text, exists := fields["text"]
+ if !exists || bytes.Equal(bytes.TrimSpace(text), []byte("null")) {
+ return errors.New("Требуется строка text")
+ }
+ if ids, exists := fields["attachment_ids"]; exists && bytes.Equal(bytes.TrimSpace(ids), []byte("null")) {
+ return errors.New("attachment_ids должен быть массивом")
+ }
+ decoder := json.NewDecoder(bytes.NewReader(data))
+ decoder.DisallowUnknownFields()
+ var decoded payload
+ if err := decoder.Decode(&decoded); err != nil {
+ return err
+ }
+ decoded.ChatID = r.ChatID
+ *r = CreateMessageRequest(decoded)
+ return nil
+}
diff --git a/dto/create_message_json_test.go b/dto/create_message_json_test.go
new file mode 100644
index 00000000..0d97cd09
--- /dev/null
+++ b/dto/create_message_json_test.go
@@ -0,0 +1,21 @@
+package dto
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestMessageStrictFields(t *testing.T) {
+ for _, body := range []string{`null`, `[]`, `{}`, `{"text":null}`, `{"text":"x","attachment_ids":null}`, `{"text":"x","attachment_ids":"x"}`, `{"text":"x","extra":1}`} {
+ var r CreateMessageRequest
+ if err := json.Unmarshal([]byte(body), &r); err == nil {
+ t.Fatalf("accepted %s", body)
+ }
+ }
+ for _, body := range []string{`{"text":"x"}`, `{"text":"x","attachment_ids":[]}`, `{"text":"","attachment_ids":["id"]}`} {
+ var r CreateMessageRequest
+ if err := json.Unmarshal([]byte(body), &r); err != nil {
+ t.Fatalf("rejected %s: %v", body, err)
+ }
+ }
+}
diff --git a/dto/create_user.go b/dto/create_user.go
new file mode 100644
index 00000000..af41015a
--- /dev/null
+++ b/dto/create_user.go
@@ -0,0 +1,14 @@
+package dto
+
+type CreateUserRequest struct {
+ Password string `json:"password"`
+ Username string `json:"username"`
+ DisplayName string `json:"display_name"`
+}
+
+type CreateUserResponse struct {
+ ID string `json:"id"`
+ Username string `json:"username"`
+ DisplayName string `json:"display_name"`
+ CreatedAt Timestamp `json:"created_at"`
+}
diff --git a/dto/current_user.go b/dto/current_user.go
new file mode 100644
index 00000000..b82a032d
--- /dev/null
+++ b/dto/current_user.go
@@ -0,0 +1,10 @@
+package dto
+
+type CurrentUserRequest struct{}
+
+type CurrentUserResponse struct {
+ ID string `json:"id"`
+ Username string `json:"username"`
+ DisplayName string `json:"display_name"`
+ CreatedAt Timestamp `json:"created_at"`
+}
diff --git a/dto/error.go b/dto/error.go
new file mode 100644
index 00000000..c8c17c4d
--- /dev/null
+++ b/dto/error.go
@@ -0,0 +1,11 @@
+package dto
+
+type ErrorResponse struct {
+ Error ErrorDetails `json:"error"`
+}
+
+type ErrorDetails struct {
+ Code string `json:"code"`
+ Message string `json:"message"`
+ RequestID string `json:"request_id"`
+}
diff --git a/dto/get_chat.go b/dto/get_chat.go
new file mode 100644
index 00000000..6ed9e1ef
--- /dev/null
+++ b/dto/get_chat.go
@@ -0,0 +1,12 @@
+package dto
+
+type GetChatRequest struct {
+ ChatID string `json:"-"`
+}
+
+type GetChatResponse struct {
+ ID string `json:"id"`
+ Title string `json:"title"`
+ MemberIDs []string `json:"member_ids"`
+ CreatedAt Timestamp `json:"created_at"`
+}
diff --git a/dto/list_chats.go b/dto/list_chats.go
new file mode 100644
index 00000000..43f9eb16
--- /dev/null
+++ b/dto/list_chats.go
@@ -0,0 +1,18 @@
+package dto
+
+type ListChatsRequest struct {
+ Limit int `json:"-"`
+ Cursor string `json:"-"`
+}
+
+type ListChatsResponse struct {
+ Items []ListChatsItem `json:"items"`
+ NextCursor *string `json:"next_cursor"`
+}
+
+type ListChatsItem struct {
+ ID string `json:"id"`
+ Title string `json:"title"`
+ MemberIDs []string `json:"member_ids"`
+ CreatedAt Timestamp `json:"created_at"`
+}
diff --git a/dto/list_messages.go b/dto/list_messages.go
new file mode 100644
index 00000000..35a38428
--- /dev/null
+++ b/dto/list_messages.go
@@ -0,0 +1,21 @@
+package dto
+
+type ListMessagesRequest struct {
+ ChatID string `json:"-"`
+ Limit int `json:"-"`
+ Cursor string `json:"-"`
+}
+
+type ListMessagesResponse struct {
+ Items []ListMessagesItem `json:"items"`
+ NextCursor *string `json:"next_cursor"`
+}
+
+type ListMessagesItem struct {
+ ID string `json:"id"`
+ ChatID string `json:"chat_id"`
+ SenderID string `json:"sender_id"`
+ Text string `json:"text"`
+ AttachmentIDs []string `json:"attachment_ids"`
+ CreatedAt Timestamp `json:"created_at"`
+}
diff --git a/dto/liveness.go b/dto/liveness.go
new file mode 100644
index 00000000..830955e9
--- /dev/null
+++ b/dto/liveness.go
@@ -0,0 +1,7 @@
+package dto
+
+type LivenessRequest struct{}
+
+type LivenessResponse struct {
+ Status string `json:"status"`
+}
diff --git a/dto/readiness.go b/dto/readiness.go
new file mode 100644
index 00000000..21573b78
--- /dev/null
+++ b/dto/readiness.go
@@ -0,0 +1,7 @@
+package dto
+
+type ReadinessRequest struct{}
+
+type ReadinessResponse struct {
+ Status string `json:"status"`
+}
diff --git a/dto/session.go b/dto/session.go
new file mode 100644
index 00000000..bd2c8807
--- /dev/null
+++ b/dto/session.go
@@ -0,0 +1,8 @@
+package dto
+
+type CreateSessionResponse struct {
+ Token string `json:"token"`
+ TokenType string `json:"token_type"`
+ ExpiresAt Timestamp `json:"expires_at"`
+ User CreateUserResponse `json:"user"`
+}
diff --git a/dto/timestamp.go b/dto/timestamp.go
new file mode 100644
index 00000000..9dab3e29
--- /dev/null
+++ b/dto/timestamp.go
@@ -0,0 +1,11 @@
+package dto
+
+import "time"
+
+// Timestamp preserves PostgreSQL microseconds and is readable by Python 3.9 clients.
+// time.Time's variable-length fractional part is not supported by those clients.
+type Timestamp time.Time
+
+func (t Timestamp) MarshalJSON() ([]byte, error) {
+ return []byte(`"` + time.Time(t).UTC().Format("2006-01-02T15:04:05.000000Z") + `"`), nil
+}
diff --git a/dto/timestamp_test.go b/dto/timestamp_test.go
new file mode 100644
index 00000000..1b48033a
--- /dev/null
+++ b/dto/timestamp_test.go
@@ -0,0 +1,15 @@
+package dto
+
+import (
+ "encoding/json"
+ "testing"
+ "time"
+)
+
+func TestTimestampFixedMicroseconds(t *testing.T) {
+ timestamp := Timestamp(time.Date(2026, 1, 2, 3, 4, 5, 123400000, time.UTC))
+ encoded, err := json.Marshal(timestamp)
+ if err != nil || string(encoded) != `"2026-01-02T03:04:05.123400Z"` {
+ t.Fatalf("timestamp=%s err=%v", encoded, err)
+ }
+}
diff --git a/evidence/lab4-acceptance.json b/evidence/lab4-acceptance.json
new file mode 100644
index 00000000..8f453015
--- /dev/null
+++ b/evidence/lab4-acceptance.json
@@ -0,0 +1,104 @@
+{
+ "negative_cases": [
+ {
+ "case": "oversized_bytes",
+ "status": 413
+ },
+ {
+ "case": "damaged_png",
+ "status": 415
+ },
+ {
+ "case": "corrupt_crc",
+ "status": 415
+ },
+ {
+ "case": "mime_spoof",
+ "status": 415
+ },
+ {
+ "case": "not_image",
+ "status": 415
+ },
+ {
+ "case": "unsupported_mime",
+ "status": 415
+ },
+ {
+ "case": "pixel_budget",
+ "status": 413
+ },
+ {
+ "case": "unsafe_slash",
+ "status": 400
+ },
+ {
+ "case": "unsafe_backslash",
+ "status": 400
+ },
+ {
+ "case": "unsafe_control",
+ "status": 400
+ },
+ {
+ "case": "empty_filename",
+ "status": 400
+ },
+ {
+ "case": "foreign_attachment",
+ "status": 404
+ },
+ {
+ "case": "foreign_chat_link",
+ "status": 404
+ },
+ {
+ "case": "unknown_attachment",
+ "status": 404
+ },
+ {
+ "case": "duplicate_ids",
+ "status": 400
+ },
+ {
+ "case": "missing_authentication",
+ "status": 401
+ },
+ {
+ "case": "unknown_variant",
+ "status": 404
+ },
+ {
+ "case": "chunked_oversized",
+ "status": 413
+ }
+ ],
+ "failures": {
+ "client_disconnect": {
+ "metadata_added": 0,
+ "spool_files_remaining": 0
+ },
+ "upload_replica_failure": {
+ "upload_instance": "alixbvpbjnzvtqvo5hhfpokc6",
+ "download_instance": "h6fc30dhoabp0jcfl6zrpjdzx",
+ "recovery_seconds": 0.51,
+ "sha256": "8502afe2cc5bab33ac834c834874369d29e7896599b5a256d68d80c5395d99c3"
+ },
+ "s3_outage": {
+ "liveness": 200,
+ "readiness": 503,
+ "recovery_seconds": 19.285
+ }
+ },
+ "checksums": {
+ "image/png": "8502afe2cc5bab33ac834c834874369d29e7896599b5a256d68d80c5395d99c3",
+ "image/jpeg": "41feb40d75a595386c72cfc8a5bc013c2dedc445b73316ebbd3b3eac13491c5d"
+ },
+ "bucket_security": {
+ "anonymous_get_status": 403,
+ "create_bucket_denied": true,
+ "write_outside_prefix_denied": true,
+ "console_published": false,
+ "bootstrap_repeats": 2
+ }
+}
diff --git a/evidence/lab4-acceptance.txt b/evidence/lab4-acceptance.txt
new file mode 100644
index 00000000..3a0ea764
--- /dev/null
+++ b/evidence/lab4-acceptance.txt
@@ -0,0 +1,2 @@
+.venv/bin/python tests/own/lab4.py
+PASS: PNG/JPEG, bounded uploads, MIME/damage/ACL/link errors, disconnect, private S3, repeat bootstrap, S3 outage.
diff --git a/evidence/lab4-bucket-bootstrap.txt b/evidence/lab4-bucket-bootstrap.txt
new file mode 100644
index 00000000..533378d4
--- /dev/null
+++ b/evidence/lab4-bucket-bootstrap.txt
@@ -0,0 +1,8 @@
+image mtusi-mc:RELEASE.2025-08-13T08-35-41Z could not be accessed on a registry to record
+its digest. Each node will access mtusi-mc:RELEASE.2025-08-13T08-35-41Z independently,
+possibly leading to different nodes running different
+versions of the image.
+
+0pza81k652zzumhh8w5zfuxw8
+mtusi-lab4_s3-bootstrap.0.jjp4j3jx92l1@colima | PASS: private bucket and restricted API account configured.
+mtusi-lab4_s3-bootstrap
diff --git a/evidence/lab4-build.txt b/evidence/lab4-build.txt
new file mode 100644
index 00000000..41301955
--- /dev/null
+++ b/evidence/lab4-build.txt
@@ -0,0 +1,112 @@
+DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
+ Install the buildx component to build images with BuildKit:
+ https://docs.docker.com/go/buildx/
+
+Sending build context to Docker daemon 401.9kB
+Step 1/35 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS goose-builder
+ ---> 18b460dd1754
+Step 2/35 : RUN GOBIN=/out CGO_ENABLED=0 go install -tags='no_clickhouse no_libsql no_mssql no_mysql no_sqlite3 no_vertica no_ydb no_spanner' github.com/pressly/goose/v3/cmd/goose@v3.27.1
+ ---> Using cache
+ ---> 7012ef857c0c
+Step 3/35 : FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS migrations
+ ---> 075c2c1a4068
+Step 4/35 : COPY --from=goose-builder /out/goose /usr/local/bin/goose
+ ---> Using cache
+ ---> 6a0ad52336c1
+Step 5/35 : COPY migrations /migrations
+ ---> Using cache
+ ---> 19018c7bfac9
+Step 6/35 : COPY infra/migrate.sh /usr/local/bin/migrate
+ ---> Using cache
+ ---> 0cc075ba3459
+Step 7/35 : USER 10001:10001
+ ---> Using cache
+ ---> 37de51af32cd
+Step 8/35 : ENTRYPOINT ["/usr/local/bin/migrate"]
+ ---> Using cache
+ ---> e7539a967679
+Step 9/35 : CMD ["up"]
+ ---> Using cache
+ ---> 841d9bb239cd
+Step 10/35 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS builder
+ ---> 18b460dd1754
+Step 11/35 : WORKDIR /src
+ ---> Using cache
+ ---> 1ba013278a4a
+Step 12/35 : RUN apk add --no-cache gcc musl-dev
+ ---> Using cache
+ ---> a0ea65f0bded
+Step 13/35 : COPY go.mod go.sum ./
+ ---> Using cache
+ ---> e98ccecfb093
+Step 14/35 : RUN go mod download
+ ---> Using cache
+ ---> 91d546c71507
+Step 15/35 : COPY cmd ./cmd
+ ---> Using cache
+ ---> b4e431a9d5f2
+Step 16/35 : COPY contracts ./contracts
+ ---> Using cache
+ ---> 958477d6c98e
+Step 17/35 : COPY dto ./dto
+ ---> Using cache
+ ---> d115fe2e1af4
+Step 18/35 : COPY internal ./internal
+ ---> 2f61826e3774
+Step 19/35 : COPY migrations ./migrations
+ ---> d96dd811bc83
+Step 20/35 : RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/messenger ./cmd
+ ---> Running in 3d7cd429d659
+ ---> Removed intermediate container 3d7cd429d659
+ ---> 2ad69db4c288
+Step 21/35 : RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/reconcile ./cmd/reconcile
+ ---> Running in 7b9a510ed472
+ ---> Removed intermediate container 7b9a510ed472
+ ---> 4a49dc2719a3
+Step 22/35 : FROM builder AS tests
+ ---> 4a49dc2719a3
+Step 23/35 : COPY --from=goose-builder /out/goose /usr/local/bin/goose
+ ---> 5df1413dfc44
+Step 24/35 : ENV GOOSE_MIGRATION_DIR=/src/migrations
+ ---> Running in c30eee45a5e7
+ ---> Removed intermediate container c30eee45a5e7
+ ---> 173325b59e7d
+Step 25/35 : CMD ["go", "test", "-p=1", "-race", "-count=1", "-v", "./..."]
+ ---> Running in 384e08125582
+ ---> Removed intermediate container 384e08125582
+ ---> 187ea6b3bcac
+Step 26/35 : FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616
+ ---> 075c2c1a4068
+Step 27/35 : RUN addgroup -S messenger && adduser -S -D -H -u 10001 -G messenger messenger
+ ---> Using cache
+ ---> 33192b561bfc
+Step 28/35 : COPY --from=builder --chown=messenger:messenger /out/messenger /usr/local/bin/messenger
+ ---> 37a18e0c6edf
+Step 29/35 : COPY --from=builder --chown=messenger:messenger /out/reconcile /usr/local/bin/reconcile
+ ---> b0e79d0416ba
+Step 30/35 : ENV PORT=8080
+ ---> Running in 48da45f81d52
+ ---> Removed intermediate container 48da45f81d52
+ ---> a7c04e9ff97b
+Step 31/35 : EXPOSE 8080
+ ---> Running in 671ff92f3fad
+ ---> Removed intermediate container 671ff92f3fad
+ ---> e56b07b42f04
+Step 32/35 : USER messenger:messenger
+ ---> Running in 8003e4ea3b5d
+ ---> Removed intermediate container 8003e4ea3b5d
+ ---> 48b4424b156b
+Step 33/35 : HEALTHCHECK --interval=10s --timeout=3s --start-period=3s --retries=3 CMD wget -q -O /dev/null "http://127.0.0.1:${PORT}/health/ready" || exit 1
+ ---> Running in c05d11001e2b
+ ---> Removed intermediate container c05d11001e2b
+ ---> 563fbe4dcd7d
+Step 34/35 : STOPSIGNAL SIGTERM
+ ---> Running in 8fb80a0e0f6c
+ ---> Removed intermediate container 8fb80a0e0f6c
+ ---> c5a0a88ba147
+Step 35/35 : ENTRYPOINT ["/usr/local/bin/messenger"]
+ ---> Running in 9e51b29d5939
+ ---> Removed intermediate container 9e51b29d5939
+ ---> c28dcd6e4410
+Successfully built c28dcd6e4410
+Successfully tagged mtusi-messenger:lab4
diff --git a/evidence/lab4-deploy.txt b/evidence/lab4-deploy.txt
new file mode 100644
index 00000000..6a3e6ce5
--- /dev/null
+++ b/evidence/lab4-deploy.txt
@@ -0,0 +1,313 @@
+.venv/bin/python scripts/swarm.py up
+vphhh79vd5ti8501q6m9lzulj
+Local secrets are ready.
+DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
+ Install the buildx component to build images with BuildKit:
+ https://docs.docker.com/go/buildx/
+
+Sending build context to Docker daemon 398.8kB
+Step 1/20 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS minio-builder
+ ---> 18b460dd1754
+Step 2/20 : ADD --checksum=sha256:8b11129f6a6830768bcdebab569290ad956618f5ff6ef4857b633acb64f95126 https://codeload.github.com/minio/minio/tar.gz/01ce918d8279a20e4706b96a64396146894adee4 /tmp/source.tar.gz
+
+
+ ---> Using cache
+ ---> 4a2b9310629e
+Step 3/20 : RUN mkdir /src && tar -xzf /tmp/source.tar.gz -C /src --strip-components=1
+ ---> Using cache
+ ---> 5166de215d05
+Step 4/20 : WORKDIR /src
+ ---> Using cache
+ ---> 1f12c7444ff0
+Step 5/20 : RUN CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o /out/minio .
+ ---> Using cache
+ ---> 8477e9d7324c
+Step 6/20 : RUN echo '8b11129f6a6830768bcdebab569290ad956618f5ff6ef4857b633acb64f95126 /tmp/source.tar.gz' | sha256sum -c -
+ ---> Using cache
+ ---> 94162de69c8c
+Step 7/20 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS mc-builder
+ ---> 18b460dd1754
+Step 8/20 : ADD --checksum=sha256:4cea8ed209c8f96e59ea1a5cc4901019b54c2be6d6f5f16c6749ea8bdcf4172e https://codeload.github.com/minio/mc/tar.gz/d6541ea280b73a834b64d4097e21f2be77676104 /tmp/source.tar.gz
+
+
+ ---> Using cache
+ ---> 5e135fc72a2c
+Step 9/20 : RUN mkdir /src && tar -xzf /tmp/source.tar.gz -C /src --strip-components=1
+ ---> Using cache
+ ---> cbffdcf35dd5
+Step 10/20 : WORKDIR /src
+ ---> Using cache
+ ---> 3322378e2ebd
+Step 11/20 : RUN CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o /out/mc .
+ ---> Using cache
+ ---> 0aba858f1d48
+Step 12/20 : RUN echo '4cea8ed209c8f96e59ea1a5cc4901019b54c2be6d6f5f16c6749ea8bdcf4172e /tmp/source.tar.gz' | sha256sum -c -
+ ---> Using cache
+ ---> af0a57fdfed1
+Step 13/20 : FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS minio
+ ---> 075c2c1a4068
+Step 14/20 : LABEL org.opencontainers.image.source="https://github.com/minio/minio" org.opencontainers.image.revision="01ce918d8279a20e4706b96a64396146894adee4" org.opencontainers.image.version="RELEASE.2025-09-07T16-13-09Z" org.opencontainers.image.licenses="AGPL-3.0"
+ ---> Using cache
+ ---> 57200264604d
+Step 15/20 : RUN mkdir /data && chown 10001:10001 /data
+ ---> Using cache
+ ---> c7024406f9a9
+Step 16/20 : COPY --from=minio-builder /out/minio /usr/local/bin/minio
+ ---> Using cache
+ ---> 0d1965ab7125
+Step 17/20 : COPY infra/minio-entrypoint.sh /usr/local/bin/start-minio
+ ---> Using cache
+ ---> e5c21da70a4f
+Step 18/20 : USER 10001:10001
+ ---> Using cache
+ ---> 9bc52122d9f2
+Step 19/20 : EXPOSE 9000
+ ---> Using cache
+ ---> 2ca448e77273
+Step 20/20 : ENTRYPOINT ["/usr/local/bin/start-minio"]
+ ---> Using cache
+ ---> 3a0797aa5ead
+Successfully built 3a0797aa5ead
+Successfully tagged mtusi-minio:RELEASE.2025-09-07T16-13-09Z
+DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
+ Install the buildx component to build images with BuildKit:
+ https://docs.docker.com/go/buildx/
+
+Sending build context to Docker daemon 398.8kB
+Step 1/25 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS minio-builder
+ ---> 18b460dd1754
+Step 2/25 : ADD --checksum=sha256:8b11129f6a6830768bcdebab569290ad956618f5ff6ef4857b633acb64f95126 https://codeload.github.com/minio/minio/tar.gz/01ce918d8279a20e4706b96a64396146894adee4 /tmp/source.tar.gz
+
+
+ ---> Using cache
+ ---> 4a2b9310629e
+Step 3/25 : RUN mkdir /src && tar -xzf /tmp/source.tar.gz -C /src --strip-components=1
+ ---> Using cache
+ ---> 5166de215d05
+Step 4/25 : WORKDIR /src
+ ---> Using cache
+ ---> 1f12c7444ff0
+Step 5/25 : RUN CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o /out/minio .
+ ---> Using cache
+ ---> 8477e9d7324c
+Step 6/25 : RUN echo '8b11129f6a6830768bcdebab569290ad956618f5ff6ef4857b633acb64f95126 /tmp/source.tar.gz' | sha256sum -c -
+ ---> Using cache
+ ---> 94162de69c8c
+Step 7/25 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS mc-builder
+ ---> 18b460dd1754
+Step 8/25 : ADD --checksum=sha256:4cea8ed209c8f96e59ea1a5cc4901019b54c2be6d6f5f16c6749ea8bdcf4172e https://codeload.github.com/minio/mc/tar.gz/d6541ea280b73a834b64d4097e21f2be77676104 /tmp/source.tar.gz
+
+
+ ---> Using cache
+ ---> 5e135fc72a2c
+Step 9/25 : RUN mkdir /src && tar -xzf /tmp/source.tar.gz -C /src --strip-components=1
+ ---> Using cache
+ ---> cbffdcf35dd5
+Step 10/25 : WORKDIR /src
+ ---> Using cache
+ ---> 3322378e2ebd
+Step 11/25 : RUN CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o /out/mc .
+ ---> Using cache
+ ---> 0aba858f1d48
+Step 12/25 : RUN echo '4cea8ed209c8f96e59ea1a5cc4901019b54c2be6d6f5f16c6749ea8bdcf4172e /tmp/source.tar.gz' | sha256sum -c -
+ ---> Using cache
+ ---> af0a57fdfed1
+Step 13/25 : FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS minio
+ ---> 075c2c1a4068
+Step 14/25 : LABEL org.opencontainers.image.source="https://github.com/minio/minio" org.opencontainers.image.revision="01ce918d8279a20e4706b96a64396146894adee4" org.opencontainers.image.version="RELEASE.2025-09-07T16-13-09Z" org.opencontainers.image.licenses="AGPL-3.0"
+ ---> Using cache
+ ---> 57200264604d
+Step 15/25 : RUN mkdir /data && chown 10001:10001 /data
+ ---> Using cache
+ ---> c7024406f9a9
+Step 16/25 : COPY --from=minio-builder /out/minio /usr/local/bin/minio
+ ---> Using cache
+ ---> 0d1965ab7125
+Step 17/25 : COPY infra/minio-entrypoint.sh /usr/local/bin/start-minio
+ ---> Using cache
+ ---> e5c21da70a4f
+Step 18/25 : USER 10001:10001
+ ---> Using cache
+ ---> 9bc52122d9f2
+Step 19/25 : EXPOSE 9000
+ ---> Using cache
+ ---> 2ca448e77273
+Step 20/25 : ENTRYPOINT ["/usr/local/bin/start-minio"]
+ ---> Using cache
+ ---> 3a0797aa5ead
+Step 21/25 : FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS mc
+ ---> 075c2c1a4068
+Step 22/25 : LABEL org.opencontainers.image.source="https://github.com/minio/mc" org.opencontainers.image.revision="d6541ea280b73a834b64d4097e21f2be77676104" org.opencontainers.image.version="RELEASE.2025-08-13T08-35-41Z" org.opencontainers.image.licenses="AGPL-3.0"
+ ---> Using cache
+ ---> 9257dca2de26
+Step 23/25 : COPY --from=mc-builder /out/mc /usr/local/bin/mc
+ ---> Using cache
+ ---> 4bea21e69e8d
+Step 24/25 : USER 10001:10001
+ ---> Using cache
+ ---> 45d00b9fce3e
+Step 25/25 : ENTRYPOINT ["mc"]
+ ---> Using cache
+ ---> 0713832dbf4a
+Successfully built 0713832dbf4a
+Successfully tagged mtusi-mc:RELEASE.2025-08-13T08-35-41Z
+DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
+ Install the buildx component to build images with BuildKit:
+ https://docs.docker.com/go/buildx/
+
+Sending build context to Docker daemon 398.3kB
+Step 1/35 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS goose-builder
+ ---> 18b460dd1754
+Step 2/35 : RUN GOBIN=/out CGO_ENABLED=0 go install -tags='no_clickhouse no_libsql no_mssql no_mysql no_sqlite3 no_vertica no_ydb no_spanner' github.com/pressly/goose/v3/cmd/goose@v3.27.1
+ ---> Using cache
+ ---> 7012ef857c0c
+Step 3/35 : FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS migrations
+ ---> 075c2c1a4068
+Step 4/35 : COPY --from=goose-builder /out/goose /usr/local/bin/goose
+ ---> Using cache
+ ---> 6a0ad52336c1
+Step 5/35 : COPY migrations /migrations
+ ---> Using cache
+ ---> 19018c7bfac9
+Step 6/35 : COPY infra/migrate.sh /usr/local/bin/migrate
+ ---> Using cache
+ ---> 0cc075ba3459
+Step 7/35 : USER 10001:10001
+ ---> Using cache
+ ---> 37de51af32cd
+Step 8/35 : ENTRYPOINT ["/usr/local/bin/migrate"]
+ ---> Using cache
+ ---> e7539a967679
+Step 9/35 : CMD ["up"]
+ ---> Using cache
+ ---> 841d9bb239cd
+Step 10/35 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS builder
+ ---> 18b460dd1754
+Step 11/35 : WORKDIR /src
+ ---> Using cache
+ ---> 1ba013278a4a
+Step 12/35 : RUN apk add --no-cache gcc musl-dev
+ ---> Using cache
+ ---> a0ea65f0bded
+Step 13/35 : COPY go.mod go.sum ./
+ ---> Using cache
+ ---> e98ccecfb093
+Step 14/35 : RUN go mod download
+ ---> Using cache
+ ---> 91d546c71507
+Step 15/35 : COPY cmd ./cmd
+ ---> Using cache
+ ---> b4e431a9d5f2
+Step 16/35 : COPY contracts ./contracts
+ ---> Using cache
+ ---> 958477d6c98e
+Step 17/35 : COPY dto ./dto
+ ---> Using cache
+ ---> d115fe2e1af4
+Step 18/35 : COPY internal ./internal
+ ---> Using cache
+ ---> 72552b328db4
+Step 19/35 : COPY migrations ./migrations
+ ---> Using cache
+ ---> a68ac4a31860
+Step 20/35 : RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/messenger ./cmd
+ ---> Using cache
+ ---> 7303ceff230d
+Step 21/35 : RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/reconcile ./cmd/reconcile
+ ---> Using cache
+ ---> e6cc7991dac9
+Step 22/35 : FROM builder AS tests
+ ---> e6cc7991dac9
+Step 23/35 : COPY --from=goose-builder /out/goose /usr/local/bin/goose
+ ---> Using cache
+ ---> 5749aa2abe35
+Step 24/35 : ENV GOOSE_MIGRATION_DIR=/src/migrations
+ ---> Using cache
+ ---> ca0a6f733ba8
+Step 25/35 : CMD ["go", "test", "-p=1", "-race", "-count=1", "-v", "./..."]
+ ---> Using cache
+ ---> c643755b8aac
+Step 26/35 : FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616
+ ---> 075c2c1a4068
+Step 27/35 : RUN addgroup -S messenger && adduser -S -D -H -u 10001 -G messenger messenger
+ ---> Using cache
+ ---> 33192b561bfc
+Step 28/35 : COPY --from=builder --chown=messenger:messenger /out/messenger /usr/local/bin/messenger
+ ---> Using cache
+ ---> 136320eda1d3
+Step 29/35 : COPY --from=builder --chown=messenger:messenger /out/reconcile /usr/local/bin/reconcile
+ ---> Using cache
+ ---> 4acf2a8fc638
+Step 30/35 : ENV PORT=8080
+ ---> Using cache
+ ---> 6fbea06a8135
+Step 31/35 : EXPOSE 8080
+ ---> Using cache
+ ---> 333a62acf202
+Step 32/35 : USER messenger:messenger
+ ---> Using cache
+ ---> 60a29210a7f2
+Step 33/35 : HEALTHCHECK --interval=10s --timeout=3s --start-period=3s --retries=3 CMD wget -q -O /dev/null "http://127.0.0.1:${PORT}/health/ready" || exit 1
+ ---> Using cache
+ ---> 803ac1f944e5
+Step 34/35 : STOPSIGNAL SIGTERM
+ ---> Using cache
+ ---> 40299d0a0f9f
+Step 35/35 : ENTRYPOINT ["/usr/local/bin/messenger"]
+ ---> Using cache
+ ---> c81ae0702578
+Successfully built c81ae0702578
+Successfully tagged mtusi-messenger:lab4
+DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
+ Install the buildx component to build images with BuildKit:
+ https://docs.docker.com/go/buildx/
+
+Sending build context to Docker daemon 398.3kB
+Step 1/9 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS goose-builder
+ ---> 18b460dd1754
+Step 2/9 : RUN GOBIN=/out CGO_ENABLED=0 go install -tags='no_clickhouse no_libsql no_mssql no_mysql no_sqlite3 no_vertica no_ydb no_spanner' github.com/pressly/goose/v3/cmd/goose@v3.27.1
+ ---> Using cache
+ ---> 7012ef857c0c
+Step 3/9 : FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS migrations
+ ---> 075c2c1a4068
+Step 4/9 : COPY --from=goose-builder /out/goose /usr/local/bin/goose
+ ---> Using cache
+ ---> 6a0ad52336c1
+Step 5/9 : COPY migrations /migrations
+ ---> Using cache
+ ---> 19018c7bfac9
+Step 6/9 : COPY infra/migrate.sh /usr/local/bin/migrate
+ ---> Using cache
+ ---> 0cc075ba3459
+Step 7/9 : USER 10001:10001
+ ---> Using cache
+ ---> 37de51af32cd
+Step 8/9 : ENTRYPOINT ["/usr/local/bin/migrate"]
+ ---> Using cache
+ ---> e7539a967679
+Step 9/9 : CMD ["up"]
+ ---> Using cache
+ ---> 841d9bb239cd
+Successfully built 841d9bb239cd
+Successfully tagged mtusi-migrations:lab4
+kkk3r78zn9bb09xudyehchs4p
+mtusi-lab4_migrations.0.mzx2ds4dsbyh@colima | 2026/10/28 19:14:47 OK 001_initial.sql (17.71ms)
+mtusi-lab4_migrations.0.mzx2ds4dsbyh@colima | 2026/10/28 19:14:47 OK 002_idempotency.sql (2.54ms)
+mtusi-lab4_migrations.0.mzx2ds4dsbyh@colima | 2026/10/28 19:14:47 OK 003_attachments.sql (5.13ms)
+mtusi-lab4_migrations.0.mzx2ds4dsbyh@colima | 2026/10/28 19:14:47 OK 004_message_attachments.sql (2.34ms)
+mtusi-lab4_migrations.0.mzx2ds4dsbyh@colima | 2026/10/28 19:14:47 goose: successfully migrated database to version: 4
+mtusi-lab4_migrations
+Since --detach=false was not specified, tasks will be created in the background.
+In a future release, --detach=false will become the default.
+Updating service mtusi-lab4_redis (id: zlhenabo1vgqarsazgqa6d897)
+Updating service mtusi-lab4_minio (id: ygvoo1fje6vqlxw02ugw7awa8)
+Updating service mtusi-lab4_api (id: xiueb8lcewfukmzn8wocqcmjh)
+Updating service mtusi-lab4_lb (id: uo65o8allz5azrqbx4zchi811)
+Updating service mtusi-lab4_postgres (id: vxi6yu73ah85o0xog5w8r1t5k)
+f6qbximmjnxx8ezv05jxgsngz
+mtusi-lab4_s3-bootstrap.0.p9vksdlsfz2l@colima | PASS: private bucket and restricted API account configured.
+mtusi-lab4_s3-bootstrap
+mtusi-lab4_api scaled to 2
+mtusi-lab4_api
+PASS: rolling update completed
+PASS: Swarm stack ready at http://localhost:8080
diff --git a/evidence/lab4-environment.json b/evidence/lab4-environment.json
new file mode 100644
index 00000000..d2856953
--- /dev/null
+++ b/evidence/lab4-environment.json
@@ -0,0 +1,81 @@
+{
+ "docker_engine": {
+ "cpus": 2,
+ "memory_bytes": 2053644288,
+ "os": "Ubuntu 24.04.4 LTS",
+ "kernel": "6.8.0-117-generic",
+ "architecture": "aarch64",
+ "version": "29.5.2"
+ },
+ "host": {
+ "architecture": "arm64",
+ "system": "Darwin",
+ "model": "MacBookPro18,3",
+ "cpu_cores": 8,
+ "memory_bytes": 17179869184
+ },
+ "host_go": "go version go1.27.2 darwin/arm64",
+ "host_python": "3.9.6",
+ "images": [
+ {
+ "tag": "mtusi-messenger:lab4",
+ "image_id": "sha256:c28dcd6e4410ee011a827852286b93296c8a0f20ddb268524b4d92371014403e",
+ "repo_digests": [
+ "mtusi-messenger@sha256:c28dcd6e4410ee011a827852286b93296c8a0f20ddb268524b4d92371014403e"
+ ],
+ "source_labels": {}
+ },
+ {
+ "tag": "mtusi-minio:RELEASE.2025-09-07T16-13-09Z",
+ "image_id": "sha256:3a0797aa5eadafe23e8a7b205c39511a9280b7500da6ef0e5d604cc74f46c3ed",
+ "repo_digests": [
+ "mtusi-minio@sha256:3a0797aa5eadafe23e8a7b205c39511a9280b7500da6ef0e5d604cc74f46c3ed"
+ ],
+ "source_labels": {
+ "org.opencontainers.image.licenses": "AGPL-3.0",
+ "org.opencontainers.image.revision": "01ce918d8279a20e4706b96a64396146894adee4",
+ "org.opencontainers.image.source": "https://github.com/minio/minio",
+ "org.opencontainers.image.version": "RELEASE.2025-09-07T16-13-09Z"
+ }
+ },
+ {
+ "tag": "mtusi-mc:RELEASE.2025-08-13T08-35-41Z",
+ "image_id": "sha256:0713832dbf4ab9a3b716ab0a4cca6629fec96224bc1425f26ab6ba42c5463a99",
+ "repo_digests": [
+ "mtusi-mc@sha256:0713832dbf4ab9a3b716ab0a4cca6629fec96224bc1425f26ab6ba42c5463a99"
+ ],
+ "source_labels": {
+ "org.opencontainers.image.licenses": "AGPL-3.0",
+ "org.opencontainers.image.revision": "d6541ea280b73a834b64d4097e21f2be77676104",
+ "org.opencontainers.image.source": "https://github.com/minio/mc",
+ "org.opencontainers.image.version": "RELEASE.2025-08-13T08-35-41Z"
+ }
+ },
+ {
+ "tag": "postgres:17.6-alpine3.22",
+ "image_id": "sha256:ef257d85f76e48da1c64832459b59fcaba1a4dac97bf5d7450c77753542eee94",
+ "repo_digests": [
+ "postgres@sha256:ef257d85f76e48da1c64832459b59fcaba1a4dac97bf5d7450c77753542eee94"
+ ],
+ "source_labels": {}
+ },
+ {
+ "tag": "redis:7.4.5-alpine3.21",
+ "image_id": "sha256:bb186d083732f669da90be8b0f975a37812b15e913465bb14d845db72a4e3e08",
+ "repo_digests": [
+ "redis@sha256:bb186d083732f669da90be8b0f975a37812b15e913465bb14d845db72a4e3e08"
+ ],
+ "source_labels": {}
+ },
+ {
+ "tag": "haproxy:3.2.6-alpine",
+ "image_id": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c",
+ "repo_digests": [
+ "haproxy@sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c"
+ ],
+ "source_labels": {}
+ }
+ ],
+ "go_container_version": "1.26.4",
+ "test_data": "synthetic; no credentials/tokens saved"
+}
diff --git a/evidence/lab4-failover.txt b/evidence/lab4-failover.txt
new file mode 100644
index 00000000..946745b7
--- /dev/null
+++ b/evidence/lab4-failover.txt
@@ -0,0 +1,4 @@
+.venv/bin/python tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
+Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
+{"observed_instances_before": 2, "successful_writes": 112, "failed_attempts": 2, "first_recovery_seconds": 2.061}
+PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
diff --git a/evidence/lab4-go-tests.txt b/evidence/lab4-go-tests.txt
new file mode 100644
index 00000000..d915cb26
--- /dev/null
+++ b/evidence/lab4-go-tests.txt
@@ -0,0 +1,13 @@
+? gitflic.ru/akselbant/mtusi2026lab1-fork/cmd [no test files]
+? gitflic.ru/akselbant/mtusi2026lab1-fork/cmd/reconcile [no test files]
+? gitflic.ru/akselbant/mtusi2026lab1-fork/contracts [no test files]
+ok gitflic.ru/akselbant/mtusi2026lab1-fork/dto (cached)
+ok gitflic.ru/akselbant/mtusi2026lab1-fork/internal/app/chat 13.184s
+ok gitflic.ru/akselbant/mtusi2026lab1-fork/internal/attachment 2.905s
+? gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config [no test files]
+? gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain [no test files]
+ok gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password (cached)
+? gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository [no test files]
+ok gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository/postgres 2.388s
+? gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/session [no test files]
+ok gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat (cached)
diff --git a/evidence/lab4-integration.txt b/evidence/lab4-integration.txt
new file mode 100644
index 00000000..9b82a00d
--- /dev/null
+++ b/evidence/lab4-integration.txt
@@ -0,0 +1,338 @@
+.venv/bin/python scripts/swarm.py test-integration
+DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
+ Install the buildx component to build images with BuildKit:
+ https://docs.docker.com/go/buildx/
+
+Sending build context to Docker daemon 398.3kB
+Step 1/25 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS goose-builder
+ ---> 18b460dd1754
+Step 2/25 : RUN GOBIN=/out CGO_ENABLED=0 go install -tags='no_clickhouse no_libsql no_mssql no_mysql no_sqlite3 no_vertica no_ydb no_spanner' github.com/pressly/goose/v3/cmd/goose@v3.27.1
+ ---> Using cache
+ ---> 7012ef857c0c
+Step 3/25 : FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS migrations
+ ---> 075c2c1a4068
+Step 4/25 : COPY --from=goose-builder /out/goose /usr/local/bin/goose
+ ---> Using cache
+ ---> 6a0ad52336c1
+Step 5/25 : COPY migrations /migrations
+ ---> Using cache
+ ---> 19018c7bfac9
+Step 6/25 : COPY infra/migrate.sh /usr/local/bin/migrate
+ ---> Using cache
+ ---> 0cc075ba3459
+Step 7/25 : USER 10001:10001
+ ---> Using cache
+ ---> 37de51af32cd
+Step 8/25 : ENTRYPOINT ["/usr/local/bin/migrate"]
+ ---> Using cache
+ ---> e7539a967679
+Step 9/25 : CMD ["up"]
+ ---> Using cache
+ ---> 841d9bb239cd
+Step 10/25 : FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS builder
+ ---> 18b460dd1754
+Step 11/25 : WORKDIR /src
+ ---> Using cache
+ ---> 1ba013278a4a
+Step 12/25 : RUN apk add --no-cache gcc musl-dev
+ ---> Using cache
+ ---> a0ea65f0bded
+Step 13/25 : COPY go.mod go.sum ./
+ ---> Using cache
+ ---> e98ccecfb093
+Step 14/25 : RUN go mod download
+ ---> Using cache
+ ---> 91d546c71507
+Step 15/25 : COPY cmd ./cmd
+ ---> Using cache
+ ---> b4e431a9d5f2
+Step 16/25 : COPY contracts ./contracts
+ ---> Using cache
+ ---> 958477d6c98e
+Step 17/25 : COPY dto ./dto
+ ---> Using cache
+ ---> d115fe2e1af4
+Step 18/25 : COPY internal ./internal
+ ---> Using cache
+ ---> 72552b328db4
+Step 19/25 : COPY migrations ./migrations
+ ---> Using cache
+ ---> a68ac4a31860
+Step 20/25 : RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/messenger ./cmd
+ ---> Using cache
+ ---> 7303ceff230d
+Step 21/25 : RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/reconcile ./cmd/reconcile
+ ---> Using cache
+ ---> e6cc7991dac9
+Step 22/25 : FROM builder AS tests
+ ---> e6cc7991dac9
+Step 23/25 : COPY --from=goose-builder /out/goose /usr/local/bin/goose
+ ---> Using cache
+ ---> 5749aa2abe35
+Step 24/25 : ENV GOOSE_MIGRATION_DIR=/src/migrations
+ ---> Using cache
+ ---> ca0a6f733ba8
+Step 25/25 : CMD ["go", "test", "-p=1", "-race", "-count=1", "-v", "./..."]
+ ---> Using cache
+ ---> c643755b8aac
+Successfully built c643755b8aac
+Successfully tagged mtusi-messenger-tests:lab4
+qs9c5xskdu6nyjiv287eg68b8
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ? gitflic.ru/akselbant/mtusi2026lab1-fork/cmd [no test files]
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ? gitflic.ru/akselbant/mtusi2026lab1-fork/cmd/reconcile [no test files]
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ? gitflic.ru/akselbant/mtusi2026lab1-fork/contracts [no test files]
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestMessageStrictFields
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestMessageStrictFields (0.00s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestTimestampFixedMicroseconds
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestTimestampFixedMicroseconds (0.00s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | PASS
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ok gitflic.ru/akselbant/mtusi2026lab1-fork/dto 1.013s
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestRejectsMalformedJSON
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:40 request_id=94533522-1e48-4953-9857-91c107c2aac7 method=POST path=/api/v1/users status=400 response_bytes=140 latency=130.671µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestRejectsMalformedJSON (0.01s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestHidesForeignChat
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:40 request_id=8b136f4b-a1b6-4756-9dda-92871979845c method=POST path=/api/v1/users status=201 response_bytes=141 latency=369.112982ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:41 request_id=df98ca4b-5f1f-4ef1-a8eb-7708ccfe2367 method=POST path=/api/v1/auth/sessions status=201 response_bytes=269 latency=397.145342ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:41 request_id=c535a54a-6ce6-49b0-8ec7-47e70ea04799 method=POST path=/api/v1/users status=201 response_bytes=147 latency=364.216754ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:41 request_id=fea0410a-7349-4fc3-8541-f479066736b7 method=POST path=/api/v1/auth/sessions status=201 response_bytes=275 latency=359.930021ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:41 request_id=b7201791-c957-4ee8-8237-2aab675777ec method=POST path=/api/v1/chats status=201 response_bytes=163 latency=174.381µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:41 request_id=30b379c6-e33c-42bc-963a-54de252a1e44 method=GET path=/api/v1/chats/:chat_id status=404 response_bytes=126 latency=53.002µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestHidesForeignChat (1.50s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestRejectsEmptyMessage
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:42 request_id=92d81694-3fcf-452b-be3d-24e21f938fb8 method=POST path=/api/v1/users status=201 response_bytes=157 latency=411.707889ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:42 request_id=792cc573-4974-4744-86bc-7ce29f78c5a2 method=POST path=/api/v1/auth/sessions status=201 response_bytes=285 latency=357.462544ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:42 request_id=4aec085d-f3fe-4b56-a0f2-b6bb55c32fd5 method=POST path=/api/v1/chats status=201 response_bytes=163 latency=135.922µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:42 request_id=bae49af4-9cb9-414e-8227-7991f680a0ab method=POST path=/api/v1/chats/:chat_id/messages status=400 response_bytes=172 latency=150.047µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestRejectsEmptyMessage (0.77s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestRejectsDuplicateUsername
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:42 request_id=c6f9b126-b2a0-4672-b4de-a59183cffe86 method=POST path=/api/v1/users status=201 response_bytes=149 latency=370.366912ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:43 request_id=c174982c-6a8d-4fc7-8d4b-6cbe68b34d70 method=POST path=/api/v1/auth/sessions status=201 response_bytes=277 latency=365.829696ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:43 request_id=b8771b83-4e05-4335-a6bd-4de115eb5e20 method=POST path=/api/v1/users status=409 response_bytes=170 latency=358.448604ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestRejectsDuplicateUsername (1.10s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestPaginatesMessagesAcrossMultiplePages
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=fb5f658b-7688-4017-9758-bea1d60af1cf method=POST path=/api/v1/users status=201 response_bytes=151 latency=366.583521ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=ddf8c2e9-34d2-4b17-aaa5-66ee679b9a4b method=POST path=/api/v1/auth/sessions status=201 response_bytes=279 latency=393.325397ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=32abde1e-d2e7-46fe-ad10-210533f83bd5 method=POST path=/api/v1/chats status=201 response_bytes=163 latency=136.838µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=227b702f-3d9d-4307-a1a8-30ab4f7aa8f2 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=228 latency=217.05µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=edc8c38d-6beb-4353-b6c1-d1c8f337282b method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=228 latency=132.629µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=27b25c6f-8b85-4f74-9a26-bc175b6e490f method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=228 latency=120.337µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=148f8872-b793-4898-a57f-78f66d57fbb6 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=228 latency=115.879µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=d2b4fae7-2633-4406-920a-af6bad40e7c9 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=228 latency=113.254µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=65190391-2026-4f9e-b907-b52379ad8afe method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=228 latency=141.005µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=094c52b2-7bb7-4ba0-be47-2695d64f4867 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=228 latency=181.464µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=ebf5592b-9a0e-4ea4-8fa7-b7d2eb14639e method=GET path=/api/v1/chats/:chat_id/messages status=200 response_bytes=725 latency=304.594µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=c0d34ec3-f7c3-4639-8160-d6e32d09ff42 method=GET path=/api/v1/chats/:chat_id/messages status=200 response_bytes=725 latency=180.132µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=0e2daae8-eec7-4e5e-a222-2f11927517a0 method=GET path=/api/v1/chats/:chat_id/messages status=200 response_bytes=725 latency=168.339µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=b8dc3150-f39e-4d7b-a0af-38ffa5a8936d method=GET path=/api/v1/chats/:chat_id/messages status=200 response_bytes=259 latency=110.796µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestPaginatesMessagesAcrossMultiplePages (0.77s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestConcurrentMessagesAreUnique
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:44 request_id=a35f6c93-191f-4fe2-8640-6390ceab09fc method=POST path=/api/v1/users status=201 response_bytes=153 latency=372.343576ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:45 request_id=4dd49805-f2ef-4166-b188-ebbbc1924d9a method=POST path=/api/v1/auth/sessions status=201 response_bytes=281 latency=371.622652ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:45 request_id=745e9e01-1f34-4e95-81fe-6a3ae7cf9ea4 method=POST path=/api/v1/users status=201 response_bytes=153 latency=394.884355ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:45 request_id=95ac49aa-2560-42da-90c1-902d76341ccd method=POST path=/api/v1/auth/sessions status=201 response_bytes=281 latency=358.911214ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:46 request_id=249bef9d-e9fb-462a-99c3-1946037d1d0c method=POST path=/api/v1/users status=201 response_bytes=153 latency=374.597959ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:46 request_id=ef3349aa-4db5-4ab6-998e-50fa2f3262bb method=POST path=/api/v1/auth/sessions status=201 response_bytes=281 latency=364.93841ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:47 request_id=3007dbff-cdb7-42b9-ac50-77dc7c2ebc4f method=POST path=/api/v1/users status=201 response_bytes=153 latency=396.618954ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:47 request_id=01f269ef-e3c5-4240-9070-37312feb0e6b method=POST path=/api/v1/auth/sessions status=201 response_bytes=281 latency=372.262695ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:47 request_id=ab7c6c66-e836-4704-a516-3c4b51322e74 method=POST path=/api/v1/users status=201 response_bytes=153 latency=358.500208ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:48 request_id=b5f8edd3-d534-401f-8e68-fac601795f3f method=POST path=/api/v1/auth/sessions status=201 response_bytes=281 latency=369.620285ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:48 request_id=1362a848-874b-4b9b-b7fe-735ae74cc76d method=POST path=/api/v1/users status=201 response_bytes=153 latency=401.141119ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:49 request_id=953f6f2b-9fd3-49c5-b660-94a56615c24c method=POST path=/api/v1/auth/sessions status=201 response_bytes=281 latency=376.048273ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:49 request_id=56532a32-ca19-4238-b539-486b339d0bbb method=POST path=/api/v1/users status=201 response_bytes=153 latency=365.0437ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:49 request_id=89c7cf01-2523-4ae6-9a85-dd1a76a61039 method=POST path=/api/v1/auth/sessions status=201 response_bytes=281 latency=362.393272ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:50 request_id=0af98040-fde9-4694-a646-c60f794e097f method=POST path=/api/v1/users status=201 response_bytes=153 latency=360.604883ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:50 request_id=92c16b19-7579-47aa-9fb0-a3595b644fc7 method=POST path=/api/v1/auth/sessions status=201 response_bytes=281 latency=373.615906ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:50 request_id=460293e1-83ce-4a90-9255-cea43854fe3f method=POST path=/api/v1/users status=201 response_bytes=153 latency=366.494027ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=f564332d-7da7-40c0-9842-20cf89412495 method=POST path=/api/v1/auth/sessions status=201 response_bytes=281 latency=375.265646ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=62b8365d-f837-4354-8383-1456f1b72154 method=POST path=/api/v1/users status=201 response_bytes=153 latency=358.727985ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=a2a37a46-5e62-41e6-bfa3-2bce4c2e3ad8 method=POST path=/api/v1/auth/sessions status=201 response_bytes=281 latency=395.19545ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=5267f89c-aaf1-47b4-8c6a-9714ca727915 method=POST path=/api/v1/chats status=201 response_bytes=514 latency=188.799µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=c5b24f54-0511-46a1-82e5-0061ec77cb32 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=208.924µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=f72ec60f-87ae-4bf6-b996-ce192a118119 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=159.047µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=8afe7b58-5dce-42a1-adf2-1145ac360d45 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=148.63µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=7b09b2e8-26b4-42af-bee4-016a8e269a6b method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=160.922µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=81276d1e-5a76-4f9b-a7aa-f9857012dc26 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=260.717µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=8904e3d2-be95-47dc-a2fa-d3eaf50a3578 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=142.714µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=a03ae327-db89-4091-b3af-578a51335de2 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=216.716µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=a498db7b-49fb-4935-bb51-891c908fa4f8 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=139.089µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=2c981e76-6d36-494c-89ba-3336d357095d method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=140.255µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=b9b68e40-c117-4bc7-8f41-b16dd197571a method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=150.047µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=7223bf26-18da-419a-a853-3b50f5f04a4b method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=132.047µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=16cc739c-a6a1-4c4c-a0bb-ec393bab1534 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=162.423µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=0aef904a-3e95-41de-b5b4-0bff73ed6722 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=153.297µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=25534c61-60fe-4374-bfda-db1354ea6012 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=145.714µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=53c00161-1317-49e3-84db-eb7c99300efa method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=146.422µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=1adf4594-5a16-4773-bff0-1b097ab75276 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=133.63µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=78902daf-2bbe-4307-a4d3-e6e9042a6291 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=180.09µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=7d53cfa6-047a-48c8-8fef-59024e131a96 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=185.173µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=b2942d4f-3e6a-4c91-8dbf-a32c6af07f43 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=206.424µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=5760c94c-8ecd-4067-832b-11909584e440 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=266.593µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=2b89e564-7bdd-4d5f-9e78-78445d1cbf70 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=153.922µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=f8fe6364-dab9-4122-9364-cc8a4e8e3d75 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=208.757µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=3af65a8b-625c-4fc7-ac62-bfbc353ac7a6 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=151.214µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=755c991e-1d9e-4279-b187-d607b13823ff method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=159.422µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=c36f80e6-63d2-4dc5-a2ee-01e6ab546ef3 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=245.509µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=e03d69f4-1738-49f5-b579-cdf4a632cc45 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=153.131µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=05255c0a-5b54-4b53-b714-08f15260687e method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=180.423µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=70f18e4c-0cdf-40a9-9e45-048f73171b1d method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=126.796µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=ed2a3480-d27e-44eb-bc85-bd088d0cd3a3 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=192.631µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=51c5b7ad-f0a6-4127-aca2-1d300b973d21 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=214.091µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=e8718af1-4fbd-4b3b-8189-9c066828773c method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=151.922µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=cd61dc79-f672-45c5-91a5-797ac5bd8680 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=170.214µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=e9226bfb-5572-4513-9a97-0e90e532cec1 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=129.838µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=8286f6ba-c4cc-4eb9-8b10-da07785cf32a method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=164.173µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=d85b2fd6-05bf-4f9b-818a-09ca9ae2df48 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=142.005µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=0ba6f4c9-0162-48d9-b610-bf28b56d39cd method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=144.339µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=296d49e0-2b8b-4493-9835-bee0c4f23261 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=243.426µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=9cf7e296-e25f-4d63-a1db-de1c262e2b8c method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=208.632µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=8fa7d17d-40d0-4dc6-a4bd-b6c514bceb4a method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=133.171µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=9e1b6e9b-7487-446f-b948-175f7d88e714 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=163.506µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=3cf21baa-455a-4dac-b8c2-732150d32af8 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=323.762µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=688c501c-30c9-46bc-ba4d-9d6a9202200d method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=572.521µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=65eefba1-b010-405b-b534-8e30d577d755 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=503.852µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=988807ed-875a-4d13-be2c-eb083546e583 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=193.299µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=0ae6f088-d7cb-4d57-a3db-bedb1d222bc7 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=129.088µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=038588a3-1ed6-4c91-bb8c-de5e55113175 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=320.887µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=ff151126-07e7-4100-a2af-e9765d071ca2 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=762.652µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=4cf0b177-670d-41b1-aa74-dafd209611e3 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=202.549µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=f893e111-2ccc-49a7-a34e-38c8248d5f3b method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=145.422µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=ece5e0f5-e795-4d1e-8bfe-2722feed55f7 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=150.047µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=45f9158b-a65f-4527-a741-2a8b3af800ac method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=165.089µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=ddc311c8-9d3f-4977-a159-a4d1daff9bf9 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=166.298µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=a0ab6cc9-4a7d-471e-9b65-932e3089cbc7 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=165.84µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=76e8f478-d9dc-4443-9bb8-310b8e9dd45a method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=266.51µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=96cb53f0-70bd-45aa-b735-6b5003310461 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=353.721µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=f1dda2fb-4016-440f-9fb6-c95a024038e6 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=169.048µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=e553ec3c-0ab2-4007-8363-48f94f36f000 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=141.796µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=78043ec3-1ec7-4c62-970c-950382757345 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=187.298µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=6222f780-9cdf-4d85-b99b-2897e61cbc43 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=808.862µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=73702805-906e-46ed-b38a-0206be2646dc method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=695.567µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=1186e970-9cb0-4db8-a44e-6d823c940ba7 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=410.557µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=98aaedba-db19-4504-a633-8a057f94d54e method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=311.22µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=92a3bfc9-1577-4a57-9bdd-351208fd7d93 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=1.151249ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=015c2bd0-266c-478a-99c8-516c482aeebe method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=266.801µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=664b2b6c-39ae-46d5-bed9-72caa9acb7f2 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=249.759µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=286c54ea-04d6-4af2-b00e-8ab00720b2ac method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=273.885µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=89ab422c-a181-43b7-8e4c-cbcdfe3c18b0 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=1.459927ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=9bbdd304-3f76-4f00-85bb-3c964aefb553 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=334.178µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:51 request_id=52dcf62b-df81-4c8f-8b60-c54965aaa140 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=1.887567ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=68f8ee3e-6eec-4f40-9428-dbeff99fd58a method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=1.955945ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=efa4cdeb-c02b-4332-a745-9ba14b490f76 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=2.642094ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=73f2e45a-8d59-4540-b193-98f1a4dca76a method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=486.101µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=c74d1c30-bde3-46d2-9b37-566419bf5376 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=507.602µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=65789068-cb57-4684-82d8-e201be139913 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=2.130868ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=fcffe674-7c32-4ee1-b34f-3841ac041030 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=191.966µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=5ea31e02-1c46-4305-ad02-1fa2217b9738 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=4.155481ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=b281c62b-cbeb-4e81-9c96-40757c5a88bf method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=150.547µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=5aceee05-4b98-45ca-974a-1e919fdde991 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=131.963µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=f82374a1-7d40-403d-aa70-5de8f913d2cc method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=167.798µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=5a725d61-e66a-445b-9b40-57ba6183a195 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=207.841µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=8ab66e91-8556-454b-913e-7b2c130dfd8e method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=152.631µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=423f02cb-0169-42fd-a1ed-5b6952cb262d method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=132.88µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=7da7d9d5-13e8-4495-9ec8-18ef6966bd41 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=160.839µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=870557ee-8823-4a7a-b78e-a440a1d8a0f7 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=146.588µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=52f91333-d9c9-4365-9453-cdfcb0c3f5a7 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=146.172µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=af8b17fd-7edc-4208-a40d-84ca21e160f3 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=167.798µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=d4c625e7-5ebf-4eea-8d60-45af463edf4c method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=165.798µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=083768b9-8f2a-4681-bcd4-5ae52228e0bb method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=186.59µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=46101c43-7641-481a-869d-3543f25ef54d method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=436.099µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=ba6ff758-a13f-4580-8ba2-da63a2351ca8 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=162.255µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=e6379b05-1acd-4b5a-b5a6-99e7bccc5f01 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=134.672µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=d9fb6747-2b9e-41cf-b53c-1a7750fc91f0 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=202.966µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=e5e5ec36-88eb-412f-8c2c-3f82c87c4590 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=171.006µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=8c2d9c31-df32-49d8-9c38-780f4fab1746 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=184.465µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=95fd52ea-b8d8-45df-a68a-ceba9d27b133 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=135.922µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=ae9ee763-096d-480c-b24b-cd349cb18978 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=384.431µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=088337f5-bb46-4911-80fd-fa855dc98200 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=145.463µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=2aa7e674-07c8-4282-8798-fcf641d08133 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=138.797µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=e0339717-77bb-4bd7-a321-34eaf1984fb9 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=163.923µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=74331de8-09a1-4057-8604-bd04d68465f4 method=POST path=/api/v1/chats/:chat_id/messages status=201 response_bytes=239 latency=145.922µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=da1793af-1f78-46eb-8751-52961c9bd4af method=GET path=/api/v1/chats/:chat_id/messages status=200 response_bytes=4332 latency=865.156µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=41cc821a-71d8-48f3-a2ec-618e8d6370b3 method=GET path=/api/v1/chats/:chat_id/messages status=200 response_bytes=4332 latency=796.404µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=58db51d8-2b41-4a61-9f48-e28a79b88470 method=GET path=/api/v1/chats/:chat_id/messages status=200 response_bytes=4332 latency=728.859µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=6b05dcf0-ee67-43cf-96d4-ef406c94761e method=GET path=/api/v1/chats/:chat_id/messages status=200 response_bytes=4332 latency=616.272µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=8bbd35fc-09c2-4ede-af22-3fb7147d5620 method=GET path=/api/v1/chats/:chat_id/messages status=200 response_bytes=4332 latency=611.938µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:52 request_id=10bfa4c2-537d-442a-8e5b-0da397c81cc0 method=GET path=/api/v1/chats/:chat_id/messages status=200 response_bytes=3616 latency=446.391µs
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestConcurrentMessagesAreUnique (7.52s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | PASS
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ok gitflic.ru/akselbant/mtusi2026lab1-fork/internal/app/chat 12.755s
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestSpoolOriginalAndFullValidation
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestSpoolOriginalAndFullValidation (0.00s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestSpoolLimitsBeforeDecode
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestSpoolLimitsBeforeDecode (0.02s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestFilenameAndCancelledUpload
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestFilenameAndCancelledUpload (0.00s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestReconcileRejectsUnsafeGrace
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestReconcileRejectsUnsafeGrace (0.00s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestValid16BitWidePNG
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestValid16BitWidePNG (0.08s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | PASS
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ok gitflic.ru/akselbant/mtusi2026lab1-fork/internal/attachment 1.160s
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ? gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config [no test files]
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ? gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain [no test files]
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestUnicodeAndSalt
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestUnicodeAndSalt (2.23s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | PASS
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ok gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password 3.266s
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ? gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository [no test files]
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestS3WireFixture
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestS3WireFixture (0.00s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestIntegration
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:84: goose up: 2026/10/28 19:17:59 OK 001_initial.sql (24.32ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 002_idempotency.sql (1.98ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 003_attachments.sql (4.14ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 004_message_attachments.sql (1.77ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 goose: successfully migrated database to version: 4
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:85: goose up: 2026/10/28 19:17:59 goose: no migrations to run. current version: 4
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestIntegration/registration_race
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:121: 12 concurrent INSERTs: 1 success, 11 unique conflicts
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestIntegration/rollback
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:139: failed second member INSERT left 0 chats and 0 members
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestIntegration/lab4_attachments_and_reconciliation
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | lab4_test.go:248: ACL, original bytes, cross-chat attachment rejection, ordered/concurrent idempotency and absent=[] passed
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | lab4_test.go:316: dry-run={Apply:false GraceSeconds:90 ObjectsScanned:4 MetadataScanned:2 BytesScanned:94 ListRequestsEstimate:1 StatRequests:3 DBQueries:12 Orphans:1 Missing:1 FreshSkipped:1 LockedSkipped:1 ReferencedMissing:1 Deleted:0 MarkedFailed:0 DurationMS:7 Actions:[{Kind:orphan Key:original/9da5bf1b-bbc2-4c8e-8816-d473c908db4a Applied:false} {Kind:missing Key:original/65f5dff4-bc2f-469b-8b6b-6bc1275f8d6a Applied:false}]} apply={Apply:true GraceSeconds:90 ObjectsScanned:4 MetadataScanned:2 BytesScanned:94 ListRequestsEstimate:1 StatRequests:3 DBQueries:13 Orphans:1 Missing:1 FreshSkipped:1 LockedSkipped:1 ReferencedMissing:1 Deleted:1 MarkedFailed:1 DurationMS:5 Actions:[{Kind:orphan Key:original/9da5bf1b-bbc2-4c8e-8816-d473c908db4a Applied:true} {Kind:missing Key:original/65f5dff4-bc2f-469b-8b6b-6bc1275f8d6a Applied:true}]} repeat={Apply:true GraceSeconds:90 ObjectsScanned:3 MetadataScanned:2 BytesScanned:88 ListRequestsEstimate:1 StatRequests:1 DBQueries:7 Orphans:0 Missing:0 FreshSkipped:1 LockedSkipped:1 ReferencedMissing:0 Deleted:0 MarkedFailed:0 DurationMS:2 Actions:[]}; fresh, locked and referenced objects retained
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestIntegration/keyset_and_explain
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:193: EXPLAIN on 10,000 messages, cursor near the end (9,000):
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:199: Limit (cost=0.29..18.05 rows=51 width=142) (actual time=0.011..0.032 rows=51 loops=1)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:199: Buffers: shared hit=10
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:199: -> Index Scan using messages_history_idx on messages m (cost=0.29..348.68 rows=1000 width=142) (actual time=0.011..0.028 rows=51 loops=1)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:199: Index Cond: ((chat_id = '3945a437-ac3b-4af6-a505-e156a979dd08'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.9+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:199: Buffers: shared hit=10
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:199: Planning Time: 0.031 ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:199: Execution Time: 0.061 ms
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:204: keyset traversal returned 10,000 distinct messages, including equal timestamps
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestIntegration/idempotency_race
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:254: 16 concurrent PostgreSQL requests: one effect, identical replay, changed body conflicts
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestIntegration/goose_legacy_and_down
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:268: goose up: 2026/10/28 19:17:59 OK 001_initial.sql (1.62ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 002_idempotency.sql (1.13ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 003_attachments.sql (583.4µs)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 004_message_attachments.sql (2.74ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 goose: successfully migrated database to version: 4
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:269: goose up: 2026/10/28 19:17:59 goose: no migrations to run. current version: 4
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:276: adopted legacy schema: all existing messages are unchanged
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:277: goose reset: 2026/10/28 19:17:59 OK 004_message_attachments.sql (5.92ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 003_attachments.sql (3.96ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 002_idempotency.sql (2.73ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 001_initial.sql (9.78ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:282: goose up: 2026/10/28 19:17:59 OK 001_initial.sql (10.62ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 002_idempotency.sql (1.69ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 003_attachments.sql (3.22ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 OK 004_message_attachments.sql (1.21ms)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | 2026/10/28 19:17:59 goose: successfully migrated database to version: 4
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | storage_test.go:286: down and up succeeded in isolated schema
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestIntegration (0.64s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestIntegration/registration_race (0.04s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestIntegration/rollback (0.00s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestIntegration/lab4_attachments_and_reconciliation (0.04s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestIntegration/keyset_and_explain (0.08s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestIntegration/idempotency_race (0.01s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestIntegration/goose_legacy_and_down (0.12s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | PASS
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ok gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository/postgres 1.675s
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ? gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/session [no test files]
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | === RUN TestPasswordContract
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | --- PASS: TestPasswordContract (0.00s)
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | PASS
+mtusi-lab4_tests.0.lks3m3xxmw5s@colima | ok gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat 1.011s
+mtusi-lab4_tests
diff --git a/evidence/lab4-persistence.txt b/evidence/lab4-persistence.txt
new file mode 100644
index 00000000..4ba6d9c2
--- /dev/null
+++ b/evidence/lab4-persistence.txt
@@ -0,0 +1,3 @@
+.venv/bin/python tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080"
+Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
+PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
diff --git a/evidence/lab4-reconciliation.json b/evidence/lab4-reconciliation.json
new file mode 100644
index 00000000..0a57b41d
--- /dev/null
+++ b/evidence/lab4-reconciliation.json
@@ -0,0 +1,1226 @@
+{
+ "hypothesis": "fixed grace plus per-object transaction lock permits repeatable repair without deleting fresh or linked data",
+ "grace_seconds": 90,
+ "faults": {
+ "s3_put_db_insert_failure": {
+ "upload_status": 503,
+ "objects_without_metadata": 1
+ },
+ "minio_unavailable": {
+ "exit_nonzero": true,
+ "summary": {
+ "apply": true,
+ "grace_seconds": 90,
+ "objects_scanned": 0,
+ "metadata_scanned": 0,
+ "bytes_scanned": 0,
+ "list_requests_estimate": 0,
+ "stat_requests": 0,
+ "db_queries": 0,
+ "orphans": 0,
+ "missing": 0,
+ "fresh_skipped": 0,
+ "locked_skipped": 0,
+ "referenced_missing": 0,
+ "deleted": 0,
+ "marked_failed": 0,
+ "duration_ms": 201,
+ "actions": []
+ }
+ },
+ "postgres_unavailable": {
+ "exit_nonzero": true,
+ "summary": {
+ "apply": true,
+ "grace_seconds": 90,
+ "objects_scanned": 1,
+ "metadata_scanned": 0,
+ "bytes_scanned": 837,
+ "list_requests_estimate": 1,
+ "stat_requests": 0,
+ "db_queries": 0,
+ "orphans": 0,
+ "missing": 0,
+ "fresh_skipped": 0,
+ "locked_skipped": 0,
+ "referenced_missing": 0,
+ "deleted": 0,
+ "marked_failed": 0,
+ "duration_ms": 48,
+ "actions": []
+ }
+ }
+ },
+ "cost": [
+ {
+ "phase": "fresh",
+ "synthetic_objects": 100,
+ "apply": false,
+ "grace_seconds": 90,
+ "objects_scanned": 130,
+ "metadata_scanned": 30,
+ "bytes_scanned": 31825703,
+ "list_requests_estimate": 1,
+ "stat_requests": 7,
+ "db_queries": 30,
+ "orphans": 0,
+ "missing": 0,
+ "fresh_skipped": 146,
+ "locked_skipped": 0,
+ "referenced_missing": 0,
+ "deleted": 0,
+ "marked_failed": 0,
+ "duration_ms": 27,
+ "actions": []
+ },
+ {
+ "phase": "fresh",
+ "synthetic_objects": 1100,
+ "apply": false,
+ "grace_seconds": 90,
+ "objects_scanned": 1130,
+ "metadata_scanned": 30,
+ "bytes_scanned": 31826703,
+ "list_requests_estimate": 2,
+ "stat_requests": 7,
+ "db_queries": 30,
+ "orphans": 0,
+ "missing": 0,
+ "fresh_skipped": 1146,
+ "locked_skipped": 0,
+ "referenced_missing": 0,
+ "deleted": 0,
+ "marked_failed": 0,
+ "duration_ms": 64,
+ "actions": []
+ },
+ {
+ "phase": "aged",
+ "synthetic_objects": 1100,
+ "apply": false,
+ "grace_seconds": 90,
+ "objects_scanned": 1130,
+ "metadata_scanned": 30,
+ "bytes_scanned": 31826703,
+ "list_requests_estimate": 2,
+ "stat_requests": 1131,
+ "db_queries": 2323,
+ "orphans": 1101,
+ "missing": 1,
+ "fresh_skipped": 0,
+ "locked_skipped": 0,
+ "referenced_missing": 1,
+ "deleted": 0,
+ "marked_failed": 0,
+ "duration_ms": 999,
+ "actions": [
+ {
+ "kind": "orphan",
+ "key": "original/337521a8-44be-4941-a1b1-0eec2ada2b3c",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/1",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/10",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/100",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/1000",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/101",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/102",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/103",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/104",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/105",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/106",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/107",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/108",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/109",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/11",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/110",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/111",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/112",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/113",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/114",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/115",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/116",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/117",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/118",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/119",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/12",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/120",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/121",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/122",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/123",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/124",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/125",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/126",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/127",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/128",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/129",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/13",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/130",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/131",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/132",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/133",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/134",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/135",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/136",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/137",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/138",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/139",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/14",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/140",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/141",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/142",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/143",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/144",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/145",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/146",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/147",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/148",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/149",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/15",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/150",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/151",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/152",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/153",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/154",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/155",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/156",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/157",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/158",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/159",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/16",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/160",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/161",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/162",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/163",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/164",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/165",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/166",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/167",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/168",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/169",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/17",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/170",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/171",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/172",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/173",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/174",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/175",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/176",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/177",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/178",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/179",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/18",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/180",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/181",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/182",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/183",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/184",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/185",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/186",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/extra/187",
+ "applied": false
+ }
+ ]
+ },
+ {
+ "phase": "aged",
+ "synthetic_objects": 100,
+ "apply": false,
+ "grace_seconds": 90,
+ "objects_scanned": 130,
+ "metadata_scanned": 30,
+ "bytes_scanned": 31825703,
+ "list_requests_estimate": 1,
+ "stat_requests": 131,
+ "db_queries": 323,
+ "orphans": 101,
+ "missing": 1,
+ "fresh_skipped": 0,
+ "locked_skipped": 0,
+ "referenced_missing": 1,
+ "deleted": 0,
+ "marked_failed": 0,
+ "duration_ms": 129,
+ "actions": [
+ {
+ "kind": "orphan",
+ "key": "original/337521a8-44be-4941-a1b1-0eec2ada2b3c",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/1",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/10",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/100",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/11",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/12",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/13",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/14",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/15",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/16",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/17",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/18",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/19",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/2",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/20",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/21",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/22",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/23",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/24",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/25",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/26",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/27",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/28",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/29",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/3",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/30",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/31",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/32",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/33",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/34",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/35",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/36",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/37",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/38",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/39",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/4",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/40",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/41",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/42",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/43",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/44",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/45",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/46",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/47",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/48",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/49",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/5",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/50",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/51",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/52",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/53",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/54",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/55",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/56",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/57",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/58",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/59",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/6",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/60",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/61",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/62",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/63",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/64",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/65",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/66",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/67",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/68",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/69",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/7",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/70",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/71",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/72",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/73",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/74",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/75",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/76",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/77",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/78",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/79",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/8",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/80",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/81",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/82",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/83",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/84",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/85",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/86",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/87",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/88",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/89",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/9",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/90",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/91",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/92",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/93",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/94",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/95",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/96",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/97",
+ "applied": false
+ },
+ {
+ "kind": "orphan",
+ "key": "original/cost-7c25c21102134be3935726555c66ed93/small/98",
+ "applied": false
+ }
+ ]
+ }
+ ],
+ "fresh_orphan_preserved": true,
+ "fresh_metadata_preserved": true,
+ "locked_candidate_preserved": true,
+ "dry_run": {
+ "apply": false,
+ "grace_seconds": 90,
+ "objects_scanned": 30,
+ "metadata_scanned": 30,
+ "bytes_scanned": 31825603,
+ "list_requests_estimate": 1,
+ "stat_requests": 31,
+ "db_queries": 123,
+ "orphans": 1,
+ "missing": 1,
+ "fresh_skipped": 0,
+ "locked_skipped": 0,
+ "referenced_missing": 1,
+ "deleted": 0,
+ "marked_failed": 0,
+ "duration_ms": 54,
+ "actions": [
+ {
+ "kind": "orphan",
+ "key": "original/337521a8-44be-4941-a1b1-0eec2ada2b3c",
+ "applied": false
+ },
+ {
+ "kind": "missing",
+ "key": "original/780437df-ef67-4bc1-82e1-209c619b6a3a",
+ "applied": false
+ }
+ ]
+ },
+ "apply": {
+ "apply": true,
+ "grace_seconds": 90,
+ "objects_scanned": 30,
+ "metadata_scanned": 30,
+ "bytes_scanned": 31825603,
+ "list_requests_estimate": 1,
+ "stat_requests": 31,
+ "db_queries": 124,
+ "orphans": 1,
+ "missing": 1,
+ "fresh_skipped": 0,
+ "locked_skipped": 0,
+ "referenced_missing": 1,
+ "deleted": 1,
+ "marked_failed": 1,
+ "duration_ms": 54,
+ "actions": [
+ {
+ "kind": "orphan",
+ "key": "original/337521a8-44be-4941-a1b1-0eec2ada2b3c",
+ "applied": true
+ },
+ {
+ "kind": "missing",
+ "key": "original/780437df-ef67-4bc1-82e1-209c619b6a3a",
+ "applied": true
+ }
+ ]
+ },
+ "repeat_apply": {
+ "apply": true,
+ "grace_seconds": 90,
+ "objects_scanned": 29,
+ "metadata_scanned": 30,
+ "bytes_scanned": 31824766,
+ "list_requests_estimate": 1,
+ "stat_requests": 29,
+ "db_queries": 118,
+ "orphans": 0,
+ "missing": 0,
+ "fresh_skipped": 0,
+ "locked_skipped": 0,
+ "referenced_missing": 0,
+ "deleted": 0,
+ "marked_failed": 0,
+ "duration_ms": 40,
+ "actions": []
+ },
+ "live_linked_preserved": true,
+ "missing_linked_reference_preserved": true
+}
diff --git a/evidence/lab4-reconciliation.txt b/evidence/lab4-reconciliation.txt
new file mode 100644
index 00000000..7a1e6fb5
--- /dev/null
+++ b/evidence/lab4-reconciliation.txt
@@ -0,0 +1,7 @@
+.venv/bin/python tests/own/reconcile.py
+Fresh scan cost: {"objects_scanned": 130, "metadata_scanned": 30, "list_requests_estimate": 1, "stat_requests": 7, "db_queries": 30, "duration_ms": 27}
+Fresh scan cost: {"objects_scanned": 1130, "metadata_scanned": 30, "list_requests_estimate": 2, "stat_requests": 7, "db_queries": 30, "duration_ms": 64}
+Waiting92s for actual S3 timestamps: measuring aged-object locking, DB and HEAD cost next.
+Aged scan cost: {"objects_scanned": 1130, "metadata_scanned": 30, "list_requests_estimate": 2, "stat_requests": 1131, "db_queries": 2323, "duration_ms": 999}
+Aged scan cost: {"objects_scanned": 130, "metadata_scanned": 30, "list_requests_estimate": 1, "stat_requests": 131, "db_queries": 323, "duration_ms": 129}
+PASS: real two-store faults, grace, lock, dry-run/apply/repeat, linked data preserved and paginated scan cost.
diff --git a/evidence/lab4-restart-verification.json b/evidence/lab4-restart-verification.json
new file mode 100644
index 00000000..9702392e
--- /dev/null
+++ b/evidence/lab4-restart-verification.json
@@ -0,0 +1,54 @@
+{
+ "api": {
+ "all_original_tasks_replaced": true,
+ "mounts_unchanged": true,
+ "before": [
+ "05qp70hqicx1m3uten0t88c9p",
+ "1diq18xi0jmwpzqzhhq39hjad"
+ ],
+ "after": [
+ "g2s34c3zuztbxehcxyypsyrxy",
+ "rz3qizaxp0s2bguzbs57me0op"
+ ]
+ },
+ "lb": {
+ "all_original_tasks_replaced": true,
+ "mounts_unchanged": true,
+ "before": [
+ "psq3iyz4kraq9u3n2mtkyfijf"
+ ],
+ "after": [
+ "oaiciqwjik1afkel0hdgwvhwy"
+ ]
+ },
+ "postgres": {
+ "all_original_tasks_replaced": true,
+ "mounts_unchanged": true,
+ "before": [
+ "7xbetpe47jvp1uuu2di2uk7vy"
+ ],
+ "after": [
+ "i3scdtmsi49zwytqye6hc3k15"
+ ]
+ },
+ "redis": {
+ "all_original_tasks_replaced": true,
+ "mounts_unchanged": true,
+ "before": [
+ "x8ejqk47qyz4rgtgygq48wos7"
+ ],
+ "after": [
+ "tqq4m5ygv0nohjefvbdprapzz"
+ ]
+ },
+ "minio": {
+ "all_original_tasks_replaced": true,
+ "mounts_unchanged": true,
+ "before": [
+ "ww19llqoe6yjvteqx3by0i4r1"
+ ],
+ "after": [
+ "lfmr13mco26ik0a37xk2ntene"
+ ]
+ }
+}
diff --git a/evidence/lab4-scaling.txt b/evidence/lab4-scaling.txt
new file mode 100644
index 00000000..9dad5410
--- /dev/null
+++ b/evidence/lab4-scaling.txt
@@ -0,0 +1,6 @@
+.venv/bin/python tests/own/swarm_grade4.py
+PASS: shared-key race on two replicas; replay/conflict/scope/validation/access/logout
+{"action": "stop-one", "target": "rz3qizaxp0s2bguzbs57me0op", "duration_seconds": 15.458, "successful_writes": 1078, "failed_attempts": 7, "successful_rps": 69.739, "first_recovery_seconds": 1.349, "max_success_gap_seconds": 1.055, "instances_before": ["rz3qizaxp0s2bguzbs57me0op", "xcgz2hubz1xrzmrkoyrgwq0tq"], "instances_after": ["j7awgfnedlt9ne1esywrappxx", "xcgz2hubz1xrzmrkoyrgwq0tq"], "confirmed_messages_checked": 1229, "duplicate_bodies": 0}
+{"action": "stop-one", "target": "xcgz2hubz1xrzmrkoyrgwq0tq", "duration_seconds": 15.467, "successful_writes": 1091, "failed_attempts": 7, "successful_rps": 70.537, "first_recovery_seconds": 1.384, "max_success_gap_seconds": 1.057, "instances_before": ["j7awgfnedlt9ne1esywrappxx", "xcgz2hubz1xrzmrkoyrgwq0tq"], "instances_after": ["5vy560hk6k0q3eicqodd9dp2z", "j7awgfnedlt9ne1esywrappxx"], "confirmed_messages_checked": 1251, "duplicate_bodies": 0}
+{"action": "update", "target": null, "duration_seconds": 44.156, "successful_writes": 3362, "failed_attempts": 0, "successful_rps": 76.139, "first_recovery_seconds": null, "max_success_gap_seconds": 0.152, "instances_before": ["5vy560hk6k0q3eicqodd9dp2z", "j7awgfnedlt9ne1esywrappxx"], "instances_after": ["j201rqxerkxdq0vbcw3armc3m", "uqyjmps3u9fyl976lpj944pc5"], "confirmed_messages_checked": 3519, "duplicate_bodies": 0}
+PASS: each original replica stopped and replaced; rolling update under load; confirmations and sessions preserved
diff --git a/evidence/lab4-smoke.txt b/evidence/lab4-smoke.txt
new file mode 100644
index 00000000..607ff815
--- /dev/null
+++ b/evidence/lab4-smoke.txt
@@ -0,0 +1,2 @@
+.venv/bin/python tests/smoke.py --base-url "http://localhost:8080"
+PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
diff --git a/evidence/lab4-status-after-restart.json b/evidence/lab4-status-after-restart.json
new file mode 100644
index 00000000..be72be0f
--- /dev/null
+++ b/evidence/lab4-status-after-restart.json
@@ -0,0 +1,146 @@
+{
+ "stack": "mtusi-lab4",
+ "docker": "29.5.2",
+ "services": {
+ "lb": {
+ "image": "haproxy:3.2.6-alpine",
+ "limits": {
+ "NanoCPUs": 500000000,
+ "MemoryBytes": 134217728
+ },
+ "published_ports": [
+ {
+ "Protocol": "tcp",
+ "TargetPort": 8080,
+ "PublishedPort": 8080,
+ "PublishMode": "host"
+ }
+ ],
+ "secret_names": [],
+ "mounts": [],
+ "tasks": [
+ {
+ "id": "oaiciqwjik1afkel0hdgwvhwy",
+ "state": "running",
+ "container": "ace6b5a9ab2ac3a291bca557e7eb89ba6354ed586625332b99dc3d1cdfaab6b5"
+ }
+ ]
+ },
+ "api": {
+ "image": "mtusi-messenger:lab4",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 1610612736
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_cursor_key",
+ "mtusi-lab4_postgres_password",
+ "mtusi-lab4_s3_access_key",
+ "mtusi-lab4_s3_secret_key"
+ ],
+ "mounts": [],
+ "tasks": [
+ {
+ "id": "g2s34c3zuztbxehcxyypsyrxy",
+ "state": "running",
+ "container": "303dc53972fde0fe71990f644cb1b56658441a4d1cb6410ffc351ccceb0cd3a7"
+ },
+ {
+ "id": "rz3qizaxp0s2bguzbs57me0op",
+ "state": "running",
+ "container": "f69bf37c2237e13f08bf4aa397936d4fed84a17395410f0af469b2cbc3e41699"
+ }
+ ]
+ },
+ "postgres": {
+ "image": "postgres:17.6-alpine3.22",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 536870912
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_postgres_password"
+ ],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_postgres_data",
+ "Target": "/var/lib/postgresql/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "i3scdtmsi49zwytqye6hc3k15",
+ "state": "running",
+ "container": "1622ddce8784f7dc1092ba808f05583898e08ba57a5c390d227265e4243744dc"
+ }
+ ]
+ },
+ "redis": {
+ "image": "redis:7.4.5-alpine3.21",
+ "limits": {
+ "NanoCPUs": 500000000,
+ "MemoryBytes": 134217728
+ },
+ "published_ports": [],
+ "secret_names": [],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_redis_data",
+ "Target": "/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "tqq4m5ygv0nohjefvbdprapzz",
+ "state": "running",
+ "container": "dfe7c35a3887061560c608484e33ce1036222d8f683a5729599fb0a4e33065f3"
+ }
+ ]
+ },
+ "minio": {
+ "image": "mtusi-minio:RELEASE.2025-09-07T16-13-09Z",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 536870912
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_s3_root_access_key",
+ "mtusi-lab4_s3_root_secret_key"
+ ],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_s3_data",
+ "Target": "/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "lfmr13mco26ik0a37xk2ntene",
+ "state": "running",
+ "container": "8110ff5d0c7a776005653182ecfd927fe59a8b38b02b4645e1bd03fa1c6fdeff"
+ }
+ ]
+ }
+ }
+}
diff --git a/evidence/lab4-status-before-restart.json b/evidence/lab4-status-before-restart.json
new file mode 100644
index 00000000..e2dd3804
--- /dev/null
+++ b/evidence/lab4-status-before-restart.json
@@ -0,0 +1,146 @@
+{
+ "stack": "mtusi-lab4",
+ "docker": "29.5.2",
+ "services": {
+ "lb": {
+ "image": "haproxy:3.2.6-alpine",
+ "limits": {
+ "NanoCPUs": 500000000,
+ "MemoryBytes": 134217728
+ },
+ "published_ports": [
+ {
+ "Protocol": "tcp",
+ "TargetPort": 8080,
+ "PublishedPort": 8080,
+ "PublishMode": "host"
+ }
+ ],
+ "secret_names": [],
+ "mounts": [],
+ "tasks": [
+ {
+ "id": "psq3iyz4kraq9u3n2mtkyfijf",
+ "state": "running",
+ "container": "c9246107dbeb5aa78d3574865ddb2e9ac2d117c87830ca7be5214bdc8e8768b4"
+ }
+ ]
+ },
+ "api": {
+ "image": "mtusi-messenger:lab4",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 1610612736
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_cursor_key",
+ "mtusi-lab4_postgres_password",
+ "mtusi-lab4_s3_access_key",
+ "mtusi-lab4_s3_secret_key"
+ ],
+ "mounts": [],
+ "tasks": [
+ {
+ "id": "05qp70hqicx1m3uten0t88c9p",
+ "state": "running",
+ "container": "0beaa8438aa0f61c069729d0d3e83220df07e49288451658d3c92f698c1b7fbc"
+ },
+ {
+ "id": "1diq18xi0jmwpzqzhhq39hjad",
+ "state": "running",
+ "container": "bc1d5078efd2145c84e341ae457360f490f32539d7e4f6d5155378c918da69c8"
+ }
+ ]
+ },
+ "postgres": {
+ "image": "postgres:17.6-alpine3.22",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 536870912
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_postgres_password"
+ ],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_postgres_data",
+ "Target": "/var/lib/postgresql/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "7xbetpe47jvp1uuu2di2uk7vy",
+ "state": "running",
+ "container": "7a24978349b4630091ba38d365fcd74b20c60bd9a27fc15d5cb967e3874e09de"
+ }
+ ]
+ },
+ "redis": {
+ "image": "redis:7.4.5-alpine3.21",
+ "limits": {
+ "NanoCPUs": 500000000,
+ "MemoryBytes": 134217728
+ },
+ "published_ports": [],
+ "secret_names": [],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_redis_data",
+ "Target": "/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "x8ejqk47qyz4rgtgygq48wos7",
+ "state": "running",
+ "container": "33a57e377891ce0c82807b12757cf0103ef9461502d1a64efdeec506744d9c27"
+ }
+ ]
+ },
+ "minio": {
+ "image": "mtusi-minio:RELEASE.2025-09-07T16-13-09Z",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 536870912
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_s3_root_access_key",
+ "mtusi-lab4_s3_root_secret_key"
+ ],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_s3_data",
+ "Target": "/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "ww19llqoe6yjvteqx3by0i4r1",
+ "state": "running",
+ "container": "d71ce1a062fe9ba9060f5266a9f00d89a34583f133a609769bb2510deaa52470"
+ }
+ ]
+ }
+ }
+}
diff --git a/evidence/lab4-status-final.json b/evidence/lab4-status-final.json
new file mode 100644
index 00000000..73fc9e08
--- /dev/null
+++ b/evidence/lab4-status-final.json
@@ -0,0 +1,146 @@
+{
+ "stack": "mtusi-lab4",
+ "docker": "29.5.2",
+ "services": {
+ "lb": {
+ "image": "haproxy:3.2.6-alpine",
+ "limits": {
+ "NanoCPUs": 500000000,
+ "MemoryBytes": 134217728
+ },
+ "published_ports": [
+ {
+ "Protocol": "tcp",
+ "TargetPort": 8080,
+ "PublishedPort": 8080,
+ "PublishMode": "host"
+ }
+ ],
+ "secret_names": [],
+ "mounts": [],
+ "tasks": [
+ {
+ "id": "oaiciqwjik1afkel0hdgwvhwy",
+ "state": "running",
+ "container": "ace6b5a9ab2ac3a291bca557e7eb89ba6354ed586625332b99dc3d1cdfaab6b5"
+ }
+ ]
+ },
+ "api": {
+ "image": "mtusi-messenger:lab4",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 1610612736
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_cursor_key",
+ "mtusi-lab4_postgres_password",
+ "mtusi-lab4_s3_access_key",
+ "mtusi-lab4_s3_secret_key"
+ ],
+ "mounts": [],
+ "tasks": [
+ {
+ "id": "uqyjmps3u9fyl976lpj944pc5",
+ "state": "running",
+ "container": "2f98c5810a5f8557476af364186cb66e02c46003fece8827e200313d27f2fd74"
+ },
+ {
+ "id": "j201rqxerkxdq0vbcw3armc3m",
+ "state": "running",
+ "container": "68730a58baf57c19b9919e752e34b7ad247ff29d94918f28fb7ede200c1a95f1"
+ }
+ ]
+ },
+ "postgres": {
+ "image": "postgres:17.6-alpine3.22",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 536870912
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_postgres_password"
+ ],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_postgres_data",
+ "Target": "/var/lib/postgresql/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "i3scdtmsi49zwytqye6hc3k15",
+ "state": "running",
+ "container": "1622ddce8784f7dc1092ba808f05583898e08ba57a5c390d227265e4243744dc"
+ }
+ ]
+ },
+ "redis": {
+ "image": "redis:7.4.5-alpine3.21",
+ "limits": {
+ "NanoCPUs": 500000000,
+ "MemoryBytes": 134217728
+ },
+ "published_ports": [],
+ "secret_names": [],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_redis_data",
+ "Target": "/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "tqq4m5ygv0nohjefvbdprapzz",
+ "state": "running",
+ "container": "dfe7c35a3887061560c608484e33ce1036222d8f683a5729599fb0a4e33065f3"
+ }
+ ]
+ },
+ "minio": {
+ "image": "mtusi-minio:RELEASE.2025-09-07T16-13-09Z",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 536870912
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_s3_root_access_key",
+ "mtusi-lab4_s3_root_secret_key"
+ ],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_s3_data",
+ "Target": "/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "lfmr13mco26ik0a37xk2ntene",
+ "state": "running",
+ "container": "8110ff5d0c7a776005653182ecfd927fe59a8b38b02b4645e1bd03fa1c6fdeff"
+ }
+ ]
+ }
+ }
+}
diff --git a/evidence/lab4-status-initial.json b/evidence/lab4-status-initial.json
new file mode 100644
index 00000000..80982359
--- /dev/null
+++ b/evidence/lab4-status-initial.json
@@ -0,0 +1,146 @@
+{
+ "stack": "mtusi-lab4",
+ "docker": "29.5.2",
+ "services": {
+ "lb": {
+ "image": "haproxy:3.2.6-alpine",
+ "limits": {
+ "NanoCPUs": 500000000,
+ "MemoryBytes": 134217728
+ },
+ "published_ports": [
+ {
+ "Protocol": "tcp",
+ "TargetPort": 8080,
+ "PublishedPort": 8080,
+ "PublishMode": "host"
+ }
+ ],
+ "secret_names": [],
+ "mounts": [],
+ "tasks": [
+ {
+ "id": "psq3iyz4kraq9u3n2mtkyfijf",
+ "state": "running",
+ "container": "c9246107dbeb5aa78d3574865ddb2e9ac2d117c87830ca7be5214bdc8e8768b4"
+ }
+ ]
+ },
+ "api": {
+ "image": "mtusi-messenger:lab4",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 1610612736
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_cursor_key",
+ "mtusi-lab4_postgres_password",
+ "mtusi-lab4_s3_access_key",
+ "mtusi-lab4_s3_secret_key"
+ ],
+ "mounts": [],
+ "tasks": [
+ {
+ "id": "0gwv6ha33wg3dao0mqxl9gu0n",
+ "state": "running",
+ "container": "f37c59b0556d4bc5b3f5c0432e8c58da4888ca5286635954131ea37c0714ead1"
+ },
+ {
+ "id": "rbtm0mbmnbzr2oqcz8icho3qr",
+ "state": "running",
+ "container": "17d5f64c6fdd70ac3cdc27e655308909d4670ca0a7b743384ac7fe34e172d428"
+ }
+ ]
+ },
+ "postgres": {
+ "image": "postgres:17.6-alpine3.22",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 536870912
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_postgres_password"
+ ],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_postgres_data",
+ "Target": "/var/lib/postgresql/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "uy9m8wn36msy9qzttodwobkez",
+ "state": "running",
+ "container": "41c7c4bb6a37b443982f168a8e8494847581c2e30d1d6f5bf817284aebaf96b9"
+ }
+ ]
+ },
+ "redis": {
+ "image": "redis:7.4.5-alpine3.21",
+ "limits": {
+ "NanoCPUs": 500000000,
+ "MemoryBytes": 134217728
+ },
+ "published_ports": [],
+ "secret_names": [],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_redis_data",
+ "Target": "/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "x8ejqk47qyz4rgtgygq48wos7",
+ "state": "running",
+ "container": "33a57e377891ce0c82807b12757cf0103ef9461502d1a64efdeec506744d9c27"
+ }
+ ]
+ },
+ "minio": {
+ "image": "mtusi-minio:RELEASE.2025-09-07T16-13-09Z",
+ "limits": {
+ "NanoCPUs": 1000000000,
+ "MemoryBytes": 536870912
+ },
+ "published_ports": [],
+ "secret_names": [
+ "mtusi-lab4_s3_root_access_key",
+ "mtusi-lab4_s3_root_secret_key"
+ ],
+ "mounts": [
+ {
+ "Type": "volume",
+ "Source": "mtusi-lab4_s3_data",
+ "Target": "/data",
+ "VolumeOptions": {
+ "Labels": {
+ "com.docker.stack.namespace": "mtusi-lab4"
+ }
+ }
+ }
+ ],
+ "tasks": [
+ {
+ "id": "jafk96quou17emokdyfk1qi4t",
+ "state": "running",
+ "container": "a04bf799191faea4386533680968586c90d67adeef8f37166824aa4723f8ac03"
+ }
+ ]
+ }
+ }
+}
diff --git a/evidence/lab4-update.txt b/evidence/lab4-update.txt
new file mode 100644
index 00000000..2c2ec68f
--- /dev/null
+++ b/evidence/lab4-update.txt
@@ -0,0 +1,3 @@
+.venv/bin/python scripts/swarm.py update
+mtusi-lab4_api
+PASS: rolling update completed
diff --git a/evidence/lab4-upload-memory.json b/evidence/lab4-upload-memory.json
new file mode 100644
index 00000000..5b2026b6
--- /dev/null
+++ b/evidence/lab4-upload-memory.json
@@ -0,0 +1,105 @@
+{
+ "concurrency": 10,
+ "warmup_uploads": 1,
+ "memory_method": "cgroup v2 memory.current sampled every >=100ms; values include file cache",
+ "runs": [
+ {
+ "width": 512,
+ "height": 512,
+ "size_bytes": 787252,
+ "sha256": "1a09c45b4a706ff8009c1adf0e9f10e428d8059d335e074cf4ad456680d4b473",
+ "requests": 10,
+ "successful": 10,
+ "errors": 0,
+ "duration_seconds": 0.325,
+ "successful_rps": 30.816,
+ "latency_ms": {
+ "0.5": 120.287,
+ "0.95": 172.127,
+ "0.99": 172.127
+ },
+ "baseline_bytes": {
+ "a43c3f7e6e92": 45240320,
+ "007b52fbba64": 45174784
+ },
+ "sampled_peak_bytes": {
+ "a43c3f7e6e92": 47005696,
+ "007b52fbba64": 46907392
+ },
+ "kernel_lifetime_peak_before_bytes": {
+ "a43c3f7e6e92": 46804992,
+ "007b52fbba64": 46718976
+ },
+ "kernel_lifetime_peak_after_bytes": {
+ "a43c3f7e6e92": 48623616,
+ "007b52fbba64": 48693248
+ },
+ "sampled_peak_sum_bytes": 93913088,
+ "samples": [
+ {
+ "seconds": 0.045,
+ "bytes": {
+ "a43c3f7e6e92": 47005696,
+ "007b52fbba64": 46907392
+ }
+ },
+ {
+ "seconds": 0.325,
+ "bytes": {
+ "a43c3f7e6e92": 46116864,
+ "007b52fbba64": 46080000
+ }
+ }
+ ]
+ },
+ {
+ "width": 1024,
+ "height": 768,
+ "size_bytes": 2360847,
+ "sha256": "ad94bc1d209135dea4d3caa0712cee92cfaa97ba3ccdd5797aff6664fcce4a2a",
+ "requests": 10,
+ "successful": 10,
+ "errors": 0,
+ "duration_seconds": 0.401,
+ "successful_rps": 24.957,
+ "latency_ms": {
+ "0.5": 258.573,
+ "0.95": 324.694,
+ "0.99": 324.694
+ },
+ "baseline_bytes": {
+ "a43c3f7e6e92": 46067712,
+ "007b52fbba64": 46063616
+ },
+ "sampled_peak_bytes": {
+ "a43c3f7e6e92": 41107456,
+ "007b52fbba64": 13651968
+ },
+ "kernel_lifetime_peak_before_bytes": {
+ "a43c3f7e6e92": 48623616,
+ "007b52fbba64": 48693248
+ },
+ "kernel_lifetime_peak_after_bytes": {
+ "a43c3f7e6e92": 49119232,
+ "007b52fbba64": 49922048
+ },
+ "sampled_peak_sum_bytes": 54759424,
+ "samples": [
+ {
+ "seconds": 0.043,
+ "bytes": {
+ "a43c3f7e6e92": 41107456,
+ "007b52fbba64": 13651968
+ }
+ },
+ {
+ "seconds": 0.401,
+ "bytes": {
+ "a43c3f7e6e92": 13234176,
+ "007b52fbba64": 10956800
+ }
+ }
+ ]
+ }
+ ]
+}
diff --git a/evidence/lab4-upload-memory.txt b/evidence/lab4-upload-memory.txt
new file mode 100644
index 00000000..8077fe94
--- /dev/null
+++ b/evidence/lab4-upload-memory.txt
@@ -0,0 +1,4 @@
+.venv/bin/python scripts/measure_uploads.py
+{"width": 512, "height": 512, "size_bytes": 787252, "sha256": "1a09c45b4a706ff8009c1adf0e9f10e428d8059d335e074cf4ad456680d4b473", "requests": 10, "successful": 10, "errors": 0, "duration_seconds": 0.325, "successful_rps": 30.816, "latency_ms": {"0.5": 120.287, "0.95": 172.127, "0.99": 172.127}, "baseline_bytes": {"a43c3f7e6e92": 45240320, "007b52fbba64": 45174784}, "sampled_peak_bytes": {"a43c3f7e6e92": 47005696, "007b52fbba64": 46907392}, "kernel_lifetime_peak_before_bytes": {"a43c3f7e6e92": 46804992, "007b52fbba64": 46718976}, "kernel_lifetime_peak_after_bytes": {"a43c3f7e6e92": 48623616, "007b52fbba64": 48693248}, "sampled_peak_sum_bytes": 93913088}
+{"width": 1024, "height": 768, "size_bytes": 2360847, "sha256": "ad94bc1d209135dea4d3caa0712cee92cfaa97ba3ccdd5797aff6664fcce4a2a", "requests": 10, "successful": 10, "errors": 0, "duration_seconds": 0.401, "successful_rps": 24.957, "latency_ms": {"0.5": 258.573, "0.95": 324.694, "0.99": 324.694}, "baseline_bytes": {"a43c3f7e6e92": 46067712, "007b52fbba64": 46063616}, "sampled_peak_bytes": {"a43c3f7e6e92": 41107456, "007b52fbba64": 13651968}, "kernel_lifetime_peak_before_bytes": {"a43c3f7e6e92": 48623616, "007b52fbba64": 48693248}, "kernel_lifetime_peak_after_bytes": {"a43c3f7e6e92": 49119232, "007b52fbba64": 49922048}, "sampled_peak_sum_bytes": 54759424}
+PASS: ten simultaneous uploads at two sizes; all originals verified; memory samples saved.
diff --git a/evidence/lab4-validation.txt b/evidence/lab4-validation.txt
new file mode 100644
index 00000000..f973c727
--- /dev/null
+++ b/evidence/lab4-validation.txt
@@ -0,0 +1,8 @@
+python3 scripts/check_contract.py
+PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
+.venv/bin/python scripts/validate_openapi.py
+/Users/dkhakimovig/mtusi2026lab4/.venv/lib/python3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
+ warnings.warn(
+PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
+PASS: goose3.27.1 migration syntax validated.
+PASS: mandatory lab4 contract and public checks unchanged.
diff --git a/evidence/swarm-grade4.json b/evidence/swarm-grade4.json
new file mode 100644
index 00000000..419dfe10
--- /dev/null
+++ b/evidence/swarm-grade4.json
@@ -0,0 +1,72 @@
+{
+ "idempotency": {
+ "parallel_requests": 16,
+ "instances": [
+ "rz3qizaxp0s2bguzbs57me0op",
+ "xcgz2hubz1xrzmrkoyrgwq0tq"
+ ],
+ "unique_message_ids": 1
+ },
+ "failovers": [
+ {
+ "action": "stop-one",
+ "target": "rz3qizaxp0s2bguzbs57me0op",
+ "duration_seconds": 15.458,
+ "successful_writes": 1078,
+ "failed_attempts": 7,
+ "successful_rps": 69.739,
+ "first_recovery_seconds": 1.349,
+ "max_success_gap_seconds": 1.055,
+ "instances_before": [
+ "rz3qizaxp0s2bguzbs57me0op",
+ "xcgz2hubz1xrzmrkoyrgwq0tq"
+ ],
+ "instances_after": [
+ "j7awgfnedlt9ne1esywrappxx",
+ "xcgz2hubz1xrzmrkoyrgwq0tq"
+ ],
+ "confirmed_messages_checked": 1229,
+ "duplicate_bodies": 0
+ },
+ {
+ "action": "stop-one",
+ "target": "xcgz2hubz1xrzmrkoyrgwq0tq",
+ "duration_seconds": 15.467,
+ "successful_writes": 1091,
+ "failed_attempts": 7,
+ "successful_rps": 70.537,
+ "first_recovery_seconds": 1.384,
+ "max_success_gap_seconds": 1.057,
+ "instances_before": [
+ "j7awgfnedlt9ne1esywrappxx",
+ "xcgz2hubz1xrzmrkoyrgwq0tq"
+ ],
+ "instances_after": [
+ "5vy560hk6k0q3eicqodd9dp2z",
+ "j7awgfnedlt9ne1esywrappxx"
+ ],
+ "confirmed_messages_checked": 1251,
+ "duplicate_bodies": 0
+ }
+ ],
+ "rolling_update": {
+ "action": "update",
+ "target": null,
+ "duration_seconds": 44.156,
+ "successful_writes": 3362,
+ "failed_attempts": 0,
+ "successful_rps": 76.139,
+ "first_recovery_seconds": null,
+ "max_success_gap_seconds": 0.152,
+ "instances_before": [
+ "5vy560hk6k0q3eicqodd9dp2z",
+ "j7awgfnedlt9ne1esywrappxx"
+ ],
+ "instances_after": [
+ "j201rqxerkxdq0vbcw3armc3m",
+ "uqyjmps3u9fyl976lpj944pc5"
+ ],
+ "confirmed_messages_checked": 3519,
+ "duplicate_bodies": 0
+ }
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 00000000..7e3203b3
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,42 @@
+module gitflic.ru/akselbant/mtusi2026lab1-fork
+
+go 1.26.4
+
+require (
+ github.com/google/uuid v1.6.0
+ github.com/jackc/pgx/v5 v5.7.6
+ github.com/labstack/echo/v4 v4.13.4
+ github.com/minio/minio-go/v7 v7.0.95
+ github.com/redis/go-redis/v9 v9.12.1
+ github.com/swaggest/swgui v1.8.9
+ golang.org/x/crypto v0.39.0
+)
+
+require (
+ github.com/cespare/xxhash/v2 v2.3.0 // indirect
+ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
+ github.com/dustin/go-humanize v1.0.1 // indirect
+ github.com/go-ini/ini v1.67.0 // indirect
+ github.com/goccy/go-json v0.10.5 // indirect
+ github.com/jackc/pgpassfile v1.0.0 // indirect
+ github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
+ github.com/jackc/puddle/v2 v2.2.2 // indirect
+ github.com/klauspost/compress v1.18.0 // indirect
+ github.com/klauspost/cpuid/v2 v2.2.11 // indirect
+ github.com/labstack/gommon v0.4.2 // indirect
+ github.com/mattn/go-colorable v0.1.14 // indirect
+ github.com/mattn/go-isatty v0.0.20 // indirect
+ github.com/minio/crc64nvme v1.0.2 // indirect
+ github.com/minio/md5-simd v1.1.2 // indirect
+ github.com/philhofer/fwd v1.2.0 // indirect
+ github.com/rs/xid v1.6.0 // indirect
+ github.com/tinylib/msgp v1.3.0 // indirect
+ github.com/valyala/bytebufferpool v1.0.0 // indirect
+ github.com/valyala/fasttemplate v1.2.2 // indirect
+ github.com/vearutop/statigz v1.4.0 // indirect
+ golang.org/x/net v0.41.0 // indirect
+ golang.org/x/sync v0.15.0 // indirect
+ golang.org/x/sys v0.33.0 // indirect
+ golang.org/x/text v0.26.0 // indirect
+ golang.org/x/time v0.11.0 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 00000000..f7913261
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,90 @@
+github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
+github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
+github.com/bool64/dev v0.2.45 h1:3nLKhAS/6Oklk3Mt2lHYSN/Cb4tdAD77KLwzeP+6eYE=
+github.com/bool64/dev v0.2.45/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
+github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
+github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
+github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
+github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
+github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
+github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
+github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
+github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
+github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
+github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
+github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
+github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
+github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
+github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
+github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
+github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
+github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
+github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
+github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
+github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
+github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
+github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
+github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
+github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
+github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
+github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4OxRzU=
+github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
+github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA=
+github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ=
+github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
+github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
+github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
+github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
+github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
+github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
+github.com/minio/crc64nvme v1.0.2 h1:6uO1UxGAD+kwqWWp7mBFsi5gAse66C4NXO8cmcVculg=
+github.com/minio/crc64nvme v1.0.2/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
+github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
+github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
+github.com/minio/minio-go/v7 v7.0.95 h1:ywOUPg+PebTMTzn9VDsoFJy32ZuARN9zhB+K3IYEvYU=
+github.com/minio/minio-go/v7 v7.0.95/go.mod h1:wOOX3uxS334vImCNRVyIDdXX9OsXDm89ToynKgqUKlo=
+github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
+github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/redis/go-redis/v9 v9.12.1 h1:k5iquqv27aBtnTm2tIkROUDp8JBXhXZIVu1InSgvovg=
+github.com/redis/go-redis/v9 v9.12.1/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
+github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
+github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
+github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
+github.com/swaggest/swgui v1.8.9 h1:cxAgIwouPpZPlvX68jY5fpwarzLbkc8/IL6DMj+H460=
+github.com/swaggest/swgui v1.8.9/go.mod h1:eTJfgwudbyw9xMwqO26vs82ei2u6//JnUAofx2vGB3M=
+github.com/tinylib/msgp v1.3.0 h1:ULuf7GPooDaIlbyvgAxBV/FI7ynli6LZ1/nVUNu+0ww=
+github.com/tinylib/msgp v1.3.0/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0=
+github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
+github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
+github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
+github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
+github.com/vearutop/statigz v1.4.0 h1:RQL0KG3j/uyA/PFpHeZ/L6l2ta920/MxlOAIGEOuwmU=
+github.com/vearutop/statigz v1.4.0/go.mod h1:LYTolBLiz9oJISwiVKnOQoIwhO1LWX1A7OECawGS8XE=
+golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
+golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
+golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
+golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
+golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
+golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
+golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
+golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
+golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
+golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
+golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
+golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/infra/README.md b/infra/README.md
index 5a39e862..82e02813 100644
--- a/infra/README.md
+++ b/infra/README.md
@@ -1,7 +1,14 @@
# Инфраструктура студента
-Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли.
+Основной стенд использует `stack.yaml`: HAProxy → две API-реплики → PostgreSQL, Redis AOF и приватный MinIO.
+Запуск: `make up`. Остановка с сохранением данных: `make down`.
+Версии, секреты, ограничения и проверки описаны в [REPORT.md](../REPORT.md).
-В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд.
+`s3-bootstrap.sh` идемпотентно создаёт bucket и отдельного API-пользователя по `s3-policy.json`.
+`minio-entrypoint.sh` читает root keys из Docker secrets. Консоль слушает loopback контейнера.
+`Dockerfile.storage` собирает MinIO и mc из исходников с фиксированными commit и SHA-256.
+`haproxy.cfg` проверяет readiness реплик. `redis.conf` включает AOF и запрещает eviction.
+`migrate.sh` запускает goose без автоматической загрузки файлов окружения.
-Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику.
+Restart hook перезапускает API, HAProxy, PostgreSQL, Redis и MinIO с сохранением volumes.
+Failover hook останавливает API-реплику по `TARGET_INSTANCE_ID`; Swarm создаёт новую task с другим ID.
diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg
new file mode 100644
index 00000000..6a3fb1fd
--- /dev/null
+++ b/infra/haproxy.cfg
@@ -0,0 +1,36 @@
+global
+ log stdout format raw local0
+ maxconn 1024
+
+defaults
+ log global
+ mode http
+ timeout connect 1s
+ timeout client 55s
+ timeout server 55s
+ timeout check 1s
+ retries 0
+ option http-server-close
+
+resolvers docker
+ nameserver dns 127.0.0.11:53
+ resolve_retries 3
+ timeout resolve 1s
+ timeout retry 1s
+ hold valid 1s
+ hold obsolete 1s
+ hold nx 1s
+ hold refused 1s
+ hold timeout 1s
+
+frontend public
+ bind :8080
+ default_backend api
+
+backend api
+ balance roundrobin
+ option httpchk
+ http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host api
+ http-check expect status 200
+ default-server inter 1s fastinter 500ms downinter 1s fall 2 rise 1
+ server-template replica 1-8 tasks.api:8080 check resolvers docker init-addr none
diff --git a/infra/migrate.sh b/infra/migrate.sh
new file mode 100755
index 00000000..70a11b2f
--- /dev/null
+++ b/infra/migrate.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+set -eu
+# Keep the password out of argv and disable automatic dotenv loading.
+if [ -n "${POSTGRES_PASSWORD_FILE:-}" ]; then
+ PGPASSWORD=$(cat "$POSTGRES_PASSWORD_FILE")
+ export PGPASSWORD
+fi
+exec goose -env /dev/null -dir /migrations -timeout 30s "$@"
diff --git a/infra/minio-entrypoint.sh b/infra/minio-entrypoint.sh
new file mode 100755
index 00000000..ba249937
--- /dev/null
+++ b/infra/minio-entrypoint.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+set -eu
+export MINIO_ROOT_USER="$(cat /run/secrets/s3_root_access_key)"
+export MINIO_ROOT_PASSWORD="$(cat /run/secrets/s3_root_secret_key)"
+exec minio server /data --address :9000 --console-address 127.0.0.1:9001
diff --git a/infra/redis.conf b/infra/redis.conf
new file mode 100644
index 00000000..c9e470c6
--- /dev/null
+++ b/infra/redis.conf
@@ -0,0 +1,9 @@
+bind 0.0.0.0
+protected-mode no
+port 6379
+dir /data
+appendonly yes
+appendfsync always
+save ""
+maxmemory 64mb
+maxmemory-policy noeviction
diff --git a/infra/s3-bootstrap.sh b/infra/s3-bootstrap.sh
new file mode 100755
index 00000000..4251f8fd
--- /dev/null
+++ b/infra/s3-bootstrap.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+set -eu
+export MC_CONFIG_DIR=/tmp/mc
+mc alias set admin http://minio:9000 "$(cat /run/secrets/s3_root_access_key)" "$(cat /run/secrets/s3_root_secret_key)" >/dev/null
+mc ready admin >/dev/null
+mc mb --ignore-existing admin/attachments >/dev/null
+mc anonymous set none admin/attachments >/dev/null
+mc admin policy create admin attachments-api /policy.json >/dev/null
+mc admin user add admin "$(cat /run/secrets/s3_access_key)" "$(cat /run/secrets/s3_secret_key)" >/dev/null
+mc admin policy attach admin attachments-api --user "$(cat /run/secrets/s3_access_key)" >/dev/null
+printf '%s\n' 'PASS: private bucket and restricted API account configured.'
diff --git a/infra/s3-policy.json b/infra/s3-policy.json
new file mode 100644
index 00000000..b9fb3b95
--- /dev/null
+++ b/infra/s3-policy.json
@@ -0,0 +1,21 @@
+{
+ "Version": "2012-10-17",
+ "Statement": [
+ {
+ "Effect": "Allow",
+ "Action": ["s3:GetBucketLocation"],
+ "Resource": ["arn:aws:s3:::attachments"]
+ },
+ {
+ "Effect": "Allow",
+ "Action": ["s3:ListBucket"],
+ "Resource": ["arn:aws:s3:::attachments"],
+ "Condition": {"StringLike": {"s3:prefix": ["original/*"]}}
+ },
+ {
+ "Effect": "Allow",
+ "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
+ "Resource": ["arn:aws:s3:::attachments/original/*"]
+ }
+ ]
+}
diff --git a/infra/schema.md b/infra/schema.md
new file mode 100644
index 00000000..9b214069
--- /dev/null
+++ b/infra/schema.md
@@ -0,0 +1,25 @@
+# Схема и транзакционные границы
+
+PostgreSQL — источник истины пользователей, чатов, участников и сообщений.
+Redis — источник истины сессий: SHA-256 токена → user_id, expires_at; TTL до
+абсолютного expires_at, без продления при чтении. Logout удаляет ключ.
+Redis использует AOF с appendfsync always и отдельный именованный volume.
+
+- users: UUID PK, UNIQUE(username), password_hash (Argon2id), display_name, created_at.
+- chats: UUID PK, title, created_at; индекс (created_at, id).
+- chat_members: PK(chat_id, user_id), FK к chats/users; ordinal сохраняет порядок
+ участников; UNIQUE(chat_id, ordinal); индекс (user_id, chat_id).
+- messages: UUID PK, FK chat_id/sender_id через членство; text, created_at.
+ Индекс (chat_id, created_at, id) отвечает равенству по чату и keyset-предикату
+ (created_at, id) > ($timestamp, $id), ORDER BY created_at, id LIMIT $limit.
+- goose_db_version: журнал goose CLI; SQL-миграция и запись её версии
+ коммитятся в одной транзакции. Сервис migrations завершается до старта API.
+
+Регистрация — один INSERT, гонка разрешается UNIQUE, проигравшие получают 409.
+Создание чата — одна транзакция: INSERT чата, затем участников; любой неверный
+FK откатывает всю операцию, включая уже вставленных участников.
+Отправка сообщения — один INSERT с FK на членство. Чтения идут в PostgreSQL,
+кэша истории нет, подтверждённый COMMIT виден следующему запросу.
+При конкурентной записи keyset не даёт snapshot: поздний COMMIT с ключом до
+уже прочитанного cursor может быть пропущен. created_at задаёт PostgreSQL,
+UUID разрешает совпадения времени; часы не используются как уникальный ID.
diff --git a/internal/app/chat/access.go b/internal/app/chat/access.go
new file mode 100644
index 00000000..36f71a5b
--- /dev/null
+++ b/internal/app/chat/access.go
@@ -0,0 +1,31 @@
+package chat
+
+import (
+ "errors"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+func (h *Chat) authorizedChat(c echo.Context, requestedChat domain.Chat) (domain.Chat, bool, error) {
+ chat, err := h.useCase.GetChat(c.Request().Context(), authenticatedUser(c).ID, requestedChat)
+ if err != nil {
+ return domain.Chat{}, false, h.useCaseError(c, err)
+ }
+ return chat, true, nil
+}
+
+func (h *Chat) cursorPosition(raw string, present bool, scope, owner string) (*domain.Position, error) {
+ if !present {
+ return nil, nil
+ }
+ if raw == "" {
+ return nil, errors.New("empty cursor")
+ }
+ createdAt, id, err := h.cursor.decode(raw, scope, owner)
+ if err != nil {
+ return nil, err
+ }
+ return &domain.Position{CreatedAt: createdAt, ID: id}, nil
+}
diff --git a/internal/app/chat/attachments.go b/internal/app/chat/attachments.go
new file mode 100644
index 00000000..f326d707
--- /dev/null
+++ b/internal/app/chat/attachments.go
@@ -0,0 +1,68 @@
+package chat
+
+import (
+ "context"
+ "mime"
+ "net/http"
+ "time"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/attachment"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+ "github.com/labstack/echo/v4"
+)
+
+func (h *Chat) WithAttachments(service *attachment.Service) *Chat { h.attachments = service; return h }
+func (h *Chat) uploadAttachment(c echo.Context) error {
+ chat, ok, err := h.authorizedChat(c, domain.Chat{ID: c.Param("chat_id")})
+ if err != nil || !ok {
+ return err
+ }
+ filename, present, err := oneQueryValue(c, "filename")
+ if err != nil || !present {
+ return h.invalid(c, "Требуется один filename")
+ }
+ if c.Request().ContentLength > attachment.MaxBytes {
+ return h.writeError(c, 413, "payload_too_large", "Файл превышает 10 MiB")
+ }
+ // Both socket reads and the application operation have a fixed deadline.
+ deadline := time.Now().Add(attachment.UploadTimeout)
+ _ = http.NewResponseController(c.Response().Writer).SetReadDeadline(deadline)
+ defer http.NewResponseController(c.Response().Writer).SetReadDeadline(time.Time{})
+ c.Request().Body = http.MaxBytesReader(c.Response(), c.Request().Body, attachment.MaxBytes+1)
+ a, err := h.attachments.Upload(c.Request().Context(), chat.ID, authenticatedUser(c).ID, filename, c.Request().Header.Get("Content-Type"), c.Request().Body)
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ return c.JSON(http.StatusCreated, a)
+}
+func (h *Chat) getAttachment(c echo.Context) error {
+ a, err := h.attachments.Get(c.Request().Context(), c.Param("attachment_id"), authenticatedUser(c).ID)
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ return c.JSON(http.StatusOK, a)
+}
+func (h *Chat) attachmentContent(c echo.Context) error {
+ a, err := h.attachments.Get(c.Request().Context(), c.Param("attachment_id"), authenticatedUser(c).ID)
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ variant, present, err := oneQueryValue(c, "variant")
+ if err != nil {
+ return h.invalid(c, "Некорректный variant")
+ }
+ if present && variant != "original" {
+ return h.writeError(c, 404, "not_found", "Вариант не найден")
+ }
+ ctx, cancel := context.WithTimeout(c.Request().Context(), 45*time.Second)
+ defer cancel()
+ obj, err := h.attachments.Content(ctx, a)
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ defer obj.Close()
+ c.Response().Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": a.Filename}))
+ c.Response().Header().Set("X-Content-Type-Options", "nosniff")
+ c.Response().Header().Set("Cache-Control", "private, no-store")
+ return c.Stream(http.StatusOK, a.ContentType, obj)
+}
diff --git a/internal/app/chat/create_chat.go b/internal/app/chat/create_chat.go
new file mode 100644
index 00000000..7e57b2e5
--- /dev/null
+++ b/internal/app/chat/create_chat.go
@@ -0,0 +1,33 @@
+package chat
+
+import (
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+func (h *Chat) createChat(c echo.Context) error {
+ var request dto.CreateChatRequest
+ if err := decodeJSON(c, &request); err != nil {
+ return h.jsonError(c, err)
+ }
+ callerID := authenticatedUser(c).ID
+ chat, err := h.useCase.CreateChat(c.Request().Context(), callerID, createChatRequestToDomain(request))
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ return c.JSON(http.StatusCreated, createChatResponseFromDomain(chat))
+}
+
+func createChatRequestToDomain(request dto.CreateChatRequest) domain.Chat {
+ return domain.Chat{Title: request.Title, MemberIDs: request.MemberIDs}
+}
+
+func createChatResponseFromDomain(chat domain.Chat) dto.CreateChatResponse {
+ return dto.CreateChatResponse{
+ ID: chat.ID, Title: chat.Title, MemberIDs: chat.MemberIDs, CreatedAt: dto.Timestamp(chat.CreatedAt),
+ }
+}
diff --git a/internal/app/chat/create_message.go b/internal/app/chat/create_message.go
new file mode 100644
index 00000000..3c0c27b7
--- /dev/null
+++ b/internal/app/chat/create_message.go
@@ -0,0 +1,58 @@
+package chat
+
+import (
+ "net/http"
+ "regexp"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
+
+func (h *Chat) createMessage(c echo.Context) error {
+ request := dto.CreateMessageRequest{ChatID: c.Param("chat_id")}
+ chat, ok, err := h.authorizedChat(c, domain.Chat{ID: request.ChatID})
+ if err != nil {
+ return err
+ }
+ if !ok {
+ return nil
+ }
+ if err := decodeJSON(c, &request); err != nil {
+ return h.jsonError(c, err)
+ }
+ request.ChatID = chat.ID
+
+ keys := c.Request().Header.Values("Idempotency-Key")
+ if len(keys) != 1 || !idempotencyKeyPattern.MatchString(keys[0]) {
+ return h.invalid(c, "Требуется Idempotency-Key: ASCII [A-Za-z0-9._:-]{1,128}")
+ }
+ input := createMessageRequestToDomain(request)
+ input.IdempotencyKey = keys[0]
+ message, err := h.useCase.CreateMessage(c.Request().Context(), authenticatedUser(c).ID, input)
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ return c.JSON(http.StatusCreated, createMessageResponseFromDomain(message))
+}
+
+func createMessageRequestToDomain(request dto.CreateMessageRequest) domain.Message {
+ return domain.Message{ChatID: request.ChatID, Text: request.Text, AttachmentIDs: request.AttachmentIDs}
+}
+
+func createMessageResponseFromDomain(message domain.Message) dto.CreateMessageResponse {
+ return dto.CreateMessageResponse{
+ ID: message.ID, ChatID: message.ChatID, SenderID: message.SenderID,
+ Text: message.Text, AttachmentIDs: nonnilIDs(message.AttachmentIDs), CreatedAt: dto.Timestamp(message.CreatedAt),
+ }
+}
+
+func nonnilIDs(ids []string) []string {
+ if ids == nil {
+ return []string{}
+ }
+ return ids
+}
diff --git a/internal/app/chat/create_user.go b/internal/app/chat/create_user.go
new file mode 100644
index 00000000..f6078178
--- /dev/null
+++ b/internal/app/chat/create_user.go
@@ -0,0 +1,32 @@
+package chat
+
+import (
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+func (h *Chat) createUser(c echo.Context) error {
+ var request dto.CreateUserRequest
+ if err := decodeJSON(c, &request); err != nil {
+ return h.jsonError(c, err)
+ }
+ user, err := h.useCase.CreateUser(c.Request().Context(), createUserRequestToDomain(request), request.Password)
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ return c.JSON(http.StatusCreated, createUserResponseFromDomain(user))
+}
+
+func createUserRequestToDomain(request dto.CreateUserRequest) domain.User {
+ return domain.User{Username: request.Username, DisplayName: request.DisplayName}
+}
+
+func createUserResponseFromDomain(user domain.User) dto.CreateUserResponse {
+ return dto.CreateUserResponse{
+ ID: user.ID, Username: user.Username, DisplayName: user.DisplayName, CreatedAt: dto.Timestamp(user.CreatedAt),
+ }
+}
diff --git a/internal/app/chat/current_user.go b/internal/app/chat/current_user.go
new file mode 100644
index 00000000..23277dea
--- /dev/null
+++ b/internal/app/chat/current_user.go
@@ -0,0 +1,21 @@
+package chat
+
+import (
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+func (h *Chat) currentUser(c echo.Context) error {
+ user := authenticatedUser(c)
+ return c.JSON(http.StatusOK, currentUserResponseFromDomain(user))
+}
+
+func currentUserResponseFromDomain(user domain.User) dto.CurrentUserResponse {
+ return dto.CurrentUserResponse{
+ ID: user.ID, Username: user.Username, DisplayName: user.DisplayName, CreatedAt: dto.Timestamp(user.CreatedAt),
+ }
+}
diff --git a/internal/app/chat/cursor.go b/internal/app/chat/cursor.go
new file mode 100644
index 00000000..83836f11
--- /dev/null
+++ b/internal/app/chat/cursor.go
@@ -0,0 +1,75 @@
+package chat
+
+import (
+ "crypto/hmac"
+ "crypto/sha256"
+ "encoding/base64"
+ "encoding/json"
+ "errors"
+ "strings"
+ "time"
+
+ "github.com/google/uuid"
+)
+
+type cursorPayload struct {
+ Scope string `json:"scope"`
+ Owner string `json:"owner"`
+ CreatedAt string `json:"created_at"`
+ ID string `json:"id"`
+}
+
+type cursorCodec struct {
+ secret [32]byte
+}
+
+func (c cursorCodec) encode(scope, owner string, createdAt time.Time, id string) string {
+ payload, err := json.Marshal(cursorPayload{
+ Scope: scope,
+ Owner: owner,
+ CreatedAt: createdAt.UTC().Format(time.RFC3339Nano),
+ ID: id,
+ })
+ if err != nil {
+ panic("cannot encode cursor: " + err.Error())
+ }
+ signature := c.sign(payload)
+ return base64.RawURLEncoding.EncodeToString(payload) + "." + base64.RawURLEncoding.EncodeToString(signature)
+}
+
+func (c cursorCodec) decode(raw, scope, owner string) (time.Time, string, error) {
+ parts := strings.Split(raw, ".")
+ if len(parts) != 2 {
+ return time.Time{}, "", errors.New("malformed cursor")
+ }
+ payload, err := base64.RawURLEncoding.DecodeString(parts[0])
+ if err != nil {
+ return time.Time{}, "", errors.New("malformed cursor")
+ }
+ signature, err := base64.RawURLEncoding.DecodeString(parts[1])
+ if err != nil || !hmac.Equal(signature, c.sign(payload)) {
+ return time.Time{}, "", errors.New("invalid cursor signature")
+ }
+ var value cursorPayload
+ if err := json.Unmarshal(payload, &value); err != nil {
+ return time.Time{}, "", errors.New("malformed cursor payload")
+ }
+ if value.Scope != scope || value.Owner != owner {
+ return time.Time{}, "", errors.New("cursor belongs to another resource")
+ }
+ parsedID, err := uuid.Parse(value.ID)
+ if err != nil || parsedID.String() != value.ID {
+ return time.Time{}, "", errors.New("malformed cursor id")
+ }
+ createdAt, err := time.Parse(time.RFC3339Nano, value.CreatedAt)
+ if err != nil {
+ return time.Time{}, "", errors.New("malformed cursor timestamp")
+ }
+ return createdAt, value.ID, nil
+}
+
+func (c cursorCodec) sign(payload []byte) []byte {
+ mac := hmac.New(sha256.New, c.secret[:])
+ _, _ = mac.Write(payload)
+ return mac.Sum(nil)
+}
diff --git a/internal/app/chat/errors.go b/internal/app/chat/errors.go
new file mode 100644
index 00000000..c556830e
--- /dev/null
+++ b/internal/app/chat/errors.go
@@ -0,0 +1,95 @@
+package chat
+
+import (
+ "errors"
+ "net/http"
+
+ "github.com/google/uuid"
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+func (h *Chat) invalid(c echo.Context, message string) error {
+ return h.writeError(c, http.StatusBadRequest, "invalid_request", message)
+}
+
+func (h *Chat) jsonError(c echo.Context, err error) error {
+ if errors.Is(err, errUnsupportedMediaType) {
+ return h.writeError(c, http.StatusUnsupportedMediaType, "unsupported_media_type", "Content-Type должен быть application/json")
+ }
+ var tooLarge *http.MaxBytesError
+ if errors.As(err, &tooLarge) {
+ return h.writeError(c, http.StatusRequestEntityTooLarge, "payload_too_large", "Тело запроса слишком большое")
+ }
+ return h.invalid(c, err.Error())
+}
+
+func (h *Chat) useCaseError(c echo.Context, err error) error {
+ applicationError, ok := domain.AsApplicationError(err)
+ if !ok {
+ return err
+ }
+ status := http.StatusInternalServerError
+ switch applicationError.Code {
+ case "unsupported_media_type":
+ status = http.StatusUnsupportedMediaType
+ case "payload_too_large":
+ status = http.StatusRequestEntityTooLarge
+ case domain.ErrorUnavailable:
+ status = http.StatusServiceUnavailable
+ case domain.ErrorInvalid:
+ status = http.StatusBadRequest
+ case domain.ErrorUnauthorized:
+ status = http.StatusUnauthorized
+ challenge := `Bearer realm="messenger"`
+ if c.Path() == "/api/v1/auth/sessions" {
+ challenge = `Basic realm="messenger", charset="UTF-8"`
+ }
+ c.Response().Header().Set("WWW-Authenticate", challenge)
+ case domain.ErrorNotFound:
+ status = http.StatusNotFound
+ case domain.ErrorConflict:
+ status = http.StatusConflict
+ }
+ return h.writeError(c, status, string(applicationError.Code), applicationError.Message)
+}
+
+func (h *Chat) writeError(c echo.Context, status int, code, message string) error {
+ return c.JSON(status, dto.ErrorResponse{Error: dto.ErrorDetails{
+ Code: code,
+ Message: message,
+ RequestID: requestID(c),
+ }})
+}
+
+func (h *Chat) httpErrorHandler(err error, c echo.Context) {
+ if c.Response().Committed {
+ return
+ }
+ status := http.StatusInternalServerError
+ code := "internal_error"
+ message := "Внутренняя ошибка сервера"
+ var httpError *echo.HTTPError
+ if errors.As(err, &httpError) {
+ status = httpError.Code
+ switch status {
+ case http.StatusNotFound:
+ code, message = "not_found", "Объект не найден"
+ case http.StatusMethodNotAllowed, http.StatusBadRequest:
+ code, message = "invalid_request", "Некорректный запрос"
+ }
+ }
+ _ = h.writeError(c, status, code, message)
+}
+
+func requestID(c echo.Context) string {
+ value, _ := c.Get(requestIDKey).(string)
+ if value == "" {
+ value = uuid.NewString()
+ c.Set(requestIDKey, value)
+ c.Response().Header().Set("X-Request-Id", value)
+ }
+ return value
+}
diff --git a/internal/app/chat/get_chat.go b/internal/app/chat/get_chat.go
new file mode 100644
index 00000000..eaddd9eb
--- /dev/null
+++ b/internal/app/chat/get_chat.go
@@ -0,0 +1,32 @@
+package chat
+
+import (
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+func (h *Chat) getChat(c echo.Context) error {
+ request := dto.GetChatRequest{ChatID: c.Param("chat_id")}
+ chat, ok, err := h.authorizedChat(c, getChatRequestToDomain(request))
+ if err != nil {
+ return err
+ }
+ if !ok {
+ return nil
+ }
+ return c.JSON(http.StatusOK, getChatResponseFromDomain(chat))
+}
+
+func getChatRequestToDomain(request dto.GetChatRequest) domain.Chat {
+ return domain.Chat{ID: request.ChatID}
+}
+
+func getChatResponseFromDomain(chat domain.Chat) dto.GetChatResponse {
+ return dto.GetChatResponse{
+ ID: chat.ID, Title: chat.Title, MemberIDs: chat.MemberIDs, CreatedAt: dto.Timestamp(chat.CreatedAt),
+ }
+}
diff --git a/internal/app/chat/list_chats.go b/internal/app/chat/list_chats.go
new file mode 100644
index 00000000..1a02d5fa
--- /dev/null
+++ b/internal/app/chat/list_chats.go
@@ -0,0 +1,53 @@
+package chat
+
+import (
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+func (h *Chat) listChats(c echo.Context) error {
+ limit, err := parseLimit(c)
+ if err != nil {
+ return h.invalid(c, err.Error())
+ }
+ rawCursor, cursorPresent, err := oneQueryValue(c, "cursor")
+ if err != nil {
+ return h.invalid(c, "cursor должен быть указан один раз")
+ }
+ request := dto.ListChatsRequest{Limit: limit, Cursor: rawCursor}
+ userID := authenticatedUser(c).ID
+ position, err := h.cursorPosition(request.Cursor, cursorPresent, "chats", userID)
+ if err != nil {
+ return h.invalid(c, "Некорректный cursor")
+ }
+
+ page, err := h.useCase.ListChats(c.Request().Context(), userID, listChatsRequestToDomain(request, position))
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ var next *string
+ if page.HasMore {
+ last := page.Items[len(page.Items)-1]
+ encoded := h.cursor.encode("chats", userID, last.CreatedAt, last.ID)
+ next = &encoded
+ }
+ return c.JSON(http.StatusOK, listChatsResponseFromDomain(page, next))
+}
+
+func listChatsRequestToDomain(request dto.ListChatsRequest, position *domain.Position) domain.Pagination {
+ return domain.Pagination{After: position, Limit: request.Limit}
+}
+
+func listChatsResponseFromDomain(page domain.ChatPage, nextCursor *string) dto.ListChatsResponse {
+ items := make([]dto.ListChatsItem, 0, len(page.Items))
+ for _, chat := range page.Items {
+ items = append(items, dto.ListChatsItem{
+ ID: chat.ID, Title: chat.Title, MemberIDs: chat.MemberIDs, CreatedAt: dto.Timestamp(chat.CreatedAt),
+ })
+ }
+ return dto.ListChatsResponse{Items: items, NextCursor: nextCursor}
+}
diff --git a/internal/app/chat/list_messages.go b/internal/app/chat/list_messages.go
new file mode 100644
index 00000000..1d7f2520
--- /dev/null
+++ b/internal/app/chat/list_messages.go
@@ -0,0 +1,70 @@
+package chat
+
+import (
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+func (h *Chat) listMessages(c echo.Context) error {
+ request := dto.ListMessagesRequest{ChatID: c.Param("chat_id")}
+ chat, ok, err := h.authorizedChat(c, domain.Chat{ID: request.ChatID})
+ if err != nil {
+ return err
+ }
+ if !ok {
+ return nil
+ }
+ limit, err := parseLimit(c)
+ if err != nil {
+ return h.invalid(c, err.Error())
+ }
+ rawCursor, cursorPresent, err := oneQueryValue(c, "cursor")
+ if err != nil {
+ return h.invalid(c, "cursor должен быть указан один раз")
+ }
+ request.ChatID = chat.ID
+ request.Limit = limit
+ request.Cursor = rawCursor
+ position, err := h.cursorPosition(request.Cursor, cursorPresent, "messages", request.ChatID)
+ if err != nil {
+ return h.invalid(c, "Некорректный cursor")
+ }
+
+ page, err := h.useCase.ListMessages(
+ c.Request().Context(), authenticatedUser(c).ID,
+ listMessagesChatToDomain(request), listMessagesPaginationToDomain(request, position),
+ )
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ var next *string
+ if page.HasMore {
+ last := page.Items[len(page.Items)-1]
+ encoded := h.cursor.encode("messages", request.ChatID, last.CreatedAt, last.ID)
+ next = &encoded
+ }
+ return c.JSON(http.StatusOK, listMessagesResponseFromDomain(page, next))
+}
+
+func listMessagesChatToDomain(request dto.ListMessagesRequest) domain.Chat {
+ return domain.Chat{ID: request.ChatID}
+}
+
+func listMessagesPaginationToDomain(request dto.ListMessagesRequest, position *domain.Position) domain.Pagination {
+ return domain.Pagination{After: position, Limit: request.Limit}
+}
+
+func listMessagesResponseFromDomain(page domain.MessagePage, nextCursor *string) dto.ListMessagesResponse {
+ items := make([]dto.ListMessagesItem, 0, len(page.Items))
+ for _, message := range page.Items {
+ items = append(items, dto.ListMessagesItem{
+ ID: message.ID, ChatID: message.ChatID, SenderID: message.SenderID,
+ Text: message.Text, AttachmentIDs: nonnilIDs(message.AttachmentIDs), CreatedAt: dto.Timestamp(message.CreatedAt),
+ })
+ }
+ return dto.ListMessagesResponse{Items: items, NextCursor: nextCursor}
+}
diff --git a/internal/app/chat/liveness.go b/internal/app/chat/liveness.go
new file mode 100644
index 00000000..6b6029f9
--- /dev/null
+++ b/internal/app/chat/liveness.go
@@ -0,0 +1,13 @@
+package chat
+
+import (
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+)
+
+func (h *Chat) liveness(c echo.Context) error {
+ return c.JSON(http.StatusOK, dto.LivenessResponse{Status: "ok"})
+}
diff --git a/internal/app/chat/middleware.go b/internal/app/chat/middleware.go
new file mode 100644
index 00000000..353b517c
--- /dev/null
+++ b/internal/app/chat/middleware.go
@@ -0,0 +1,43 @@
+package chat
+
+import (
+ "net/http"
+
+ "github.com/google/uuid"
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+const (
+ requestIDKey = "request_id"
+ userKey = "current_user"
+)
+
+func (h *Chat) requestIDMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
+ return func(c echo.Context) error {
+ requestID := uuid.NewString()
+ c.Set(requestIDKey, requestID)
+ c.Response().Header().Set("X-Request-Id", requestID)
+ c.Response().Header().Set("X-Instance-Id", h.instanceID)
+ if h.draining.Load() && c.Request().URL.Path != "/health/live" {
+ return h.writeError(c, http.StatusServiceUnavailable, "unavailable", "Реплика завершает работу")
+ }
+ return next(c)
+ }
+}
+
+func (h *Chat) authenticate(next echo.HandlerFunc) echo.HandlerFunc {
+ return func(c echo.Context) error {
+ user, err := h.useCase.Authenticate(c.Request().Context(), bearerToken(c))
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ c.Set(userKey, user)
+ return next(c)
+ }
+}
+
+func authenticatedUser(c echo.Context) domain.User {
+ return c.Get(userKey).(domain.User)
+}
diff --git a/internal/app/chat/readiness.go b/internal/app/chat/readiness.go
new file mode 100644
index 00000000..950ca172
--- /dev/null
+++ b/internal/app/chat/readiness.go
@@ -0,0 +1,25 @@
+package chat
+
+import (
+ "context"
+ "net/http"
+ "time"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+)
+
+func (h *Chat) readiness(c echo.Context) error {
+ ctx, cancel := context.WithTimeout(c.Request().Context(), 2*time.Second)
+ defer cancel()
+ if err := h.useCase.Ready(ctx); err != nil {
+ return h.useCaseError(c, err)
+ }
+ if h.attachments != nil {
+ if err := h.attachments.Ready(ctx); err != nil {
+ return h.useCaseError(c, err)
+ }
+ }
+ return c.JSON(http.StatusOK, dto.ReadinessResponse{Status: "ok"})
+}
diff --git a/internal/app/chat/request.go b/internal/app/chat/request.go
new file mode 100644
index 00000000..2cb05d4d
--- /dev/null
+++ b/internal/app/chat/request.go
@@ -0,0 +1,74 @@
+package chat
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "mime"
+ "net/http"
+ "strconv"
+
+ "github.com/labstack/echo/v4"
+)
+
+const (
+ defaultLimit = 50
+ maxLimit = 100
+ maxJSONBody = 1 << 20
+)
+
+var errUnsupportedMediaType = errors.New("unsupported media type")
+
+func decodeJSON(c echo.Context, target any) error {
+ mediaType, _, err := mime.ParseMediaType(c.Request().Header.Get(echo.HeaderContentType))
+ if err != nil || mediaType != echo.MIMEApplicationJSON {
+ return errUnsupportedMediaType
+ }
+ reader := http.MaxBytesReader(c.Response(), c.Request().Body, maxJSONBody)
+ decoder := json.NewDecoder(reader)
+ decoder.DisallowUnknownFields()
+ if err := decoder.Decode(target); err != nil {
+ var tooLarge *http.MaxBytesError
+ if errors.As(err, &tooLarge) {
+ return err
+ }
+ return errors.New("Некорректное JSON-тело")
+ }
+ var extra any
+ err = decoder.Decode(&extra)
+ var tooLarge *http.MaxBytesError
+ if errors.As(err, &tooLarge) {
+ return err
+ }
+ if !errors.Is(err, io.EOF) {
+ return errors.New("JSON-тело должно содержать ровно один объект")
+ }
+ return nil
+}
+
+func parseLimit(c echo.Context) (int, error) {
+ raw, present, err := oneQueryValue(c, "limit")
+ if err != nil {
+ return 0, errors.New("limit должен быть указан один раз")
+ }
+ if !present {
+ return defaultLimit, nil
+ }
+ limit, err := strconv.Atoi(raw)
+ if err != nil || limit < 1 || limit > maxLimit {
+ return 0, fmt.Errorf("limit должен быть целым числом от 1 до %d", maxLimit)
+ }
+ return limit, nil
+}
+
+func oneQueryValue(c echo.Context, name string) (string, bool, error) {
+ values, present := c.QueryParams()[name]
+ if !present {
+ return "", false, nil
+ }
+ if len(values) != 1 {
+ return "", true, errors.New("query parameter must occur once")
+ }
+ return values[0], true, nil
+}
diff --git a/internal/app/chat/service.go b/internal/app/chat/service.go
new file mode 100644
index 00000000..0e4115c0
--- /dev/null
+++ b/internal/app/chat/service.go
@@ -0,0 +1,93 @@
+package chat
+
+import (
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/attachment"
+ "log"
+ "os"
+ "sync/atomic"
+
+ "github.com/google/uuid"
+ "github.com/labstack/echo/v4"
+ "github.com/labstack/echo/v4/middleware"
+ "github.com/swaggest/swgui/v5emb"
+)
+
+// Chat is the HTTP handler and depends only on the application boundary.
+type Chat struct {
+ attachments *attachment.Service
+ instanceID string
+ draining atomic.Bool
+ useCase UseCase
+ cursor cursorCodec
+}
+
+func NewChat(useCase UseCase, cursorKey [32]byte) *Chat {
+ instanceID := os.Getenv("INSTANCE_ID")
+ if instanceID == "" {
+ instanceID = uuid.NewString()
+ }
+ return &Chat{useCase: useCase, cursor: cursorCodec{secret: cursorKey}, instanceID: instanceID}
+}
+
+func (h *Chat) Drain() { h.draining.Store(true) }
+
+func (h *Chat) Router() *echo.Echo {
+ e := echo.New()
+ e.HideBanner = true
+ e.HidePort = true
+ e.HTTPErrorHandler = h.httpErrorHandler
+ e.Use(h.requestIDMiddleware)
+ e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
+ HandleError: true,
+ LogLatency: true,
+ LogMethod: true,
+ LogRoutePath: true,
+ LogURIPath: true,
+ LogRequestID: true,
+ LogStatus: true,
+ LogResponseSize: true,
+ LogValuesFunc: func(_ echo.Context, values middleware.RequestLoggerValues) error {
+ path := values.RoutePath
+ if path == "" {
+ path = values.URIPath
+ }
+ log.Printf(
+ "request_id=%s method=%s path=%s status=%d response_bytes=%d latency=%s",
+ values.RequestID, values.Method, path, values.Status, values.ResponseSize, values.Latency,
+ )
+ return nil
+ },
+ }))
+ e.Use(middleware.Recover())
+
+ e.GET("/health/live", h.liveness)
+ e.GET("/health/ready", h.readiness)
+ swaggerUI := echo.WrapHandler(v5emb.New(
+ "MTUSI Messenger",
+ "/swagger/openapi.json",
+ "/swagger/",
+ ))
+ e.GET("/swagger", h.swagger)
+ e.GET("/swagger/", swaggerUI)
+ e.GET("/swagger/openapi.json", h.swaggerSpec)
+ e.GET("/swagger/*", swaggerUI)
+ e.POST("/api/v1/users", h.createUser)
+
+ e.POST("/api/v1/auth/sessions", h.createSession)
+
+ api := e.Group("/api/v1", h.authenticate)
+ api.GET("/users/me", h.currentUser)
+ api.DELETE("/auth/sessions/current", h.deleteSession)
+ api.POST("/chats", h.createChat)
+ api.GET("/chats", h.listChats)
+ api.GET("/chats/:chat_id", h.getChat)
+ api.POST("/chats/:chat_id/messages", h.createMessage)
+ api.GET("/chats/:chat_id/messages", h.listMessages)
+ if h.attachments != nil {
+ api.POST("/chats/:chat_id/attachments", h.uploadAttachment)
+ api.GET("/attachments/:attachment_id", h.getAttachment)
+ api.GET("/attachments/:attachment_id/content", h.attachmentContent)
+ }
+
+ return e
+}
diff --git a/internal/app/chat/service_test.go b/internal/app/chat/service_test.go
new file mode 100644
index 00000000..bf582c2c
--- /dev/null
+++ b/internal/app/chat/service_test.go
@@ -0,0 +1,353 @@
+package chat_test
+
+import (
+ "bytes"
+ "context"
+ "encoding/base64"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/google/uuid"
+
+ chathandler "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/app/chat"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository"
+ chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat"
+)
+
+type testAPI struct {
+ server *httptest.Server
+ tokens sync.Map
+}
+
+type userResponse struct {
+ ID string `json:"id"`
+}
+
+type chatResponse struct {
+ ID string `json:"id"`
+}
+
+type messageResponse struct {
+ ID string `json:"id"`
+ Text string `json:"text"`
+}
+
+type messagePageResponse struct {
+ Items []messageResponse `json:"items"`
+ NextCursor *string `json:"next_cursor"`
+}
+
+type errorResponse struct {
+ Error struct {
+ Code string `json:"code"`
+ } `json:"error"`
+}
+
+func newTestAPI(t *testing.T) *testAPI {
+ t.Helper()
+ useCase := chatusecase.New(repository.NewMemory(), &testSessions{values: map[string]domain.Session{}}, time.Hour)
+ server := httptest.NewServer(chathandler.NewChat(useCase, [32]byte{1}).Router())
+ t.Cleanup(server.Close)
+ return &testAPI{server: server}
+}
+
+func (a *testAPI) request(t *testing.T, client *http.Client, method, path, actorID, body string, wantStatus int, target any) {
+ t.Helper()
+ request, err := http.NewRequest(method, a.server.URL+path, bytes.NewBufferString(body))
+ if err != nil {
+ t.Fatalf("create request: %v", err)
+ }
+ if body != "" {
+ request.Header.Set("Content-Type", "application/json")
+ request.Header.Set("Idempotency-Key", uuid.NewString())
+ }
+ if actorID != "" {
+ token, _ := a.tokens.Load(actorID)
+ request.Header.Set("Authorization", "Bearer "+token.(string))
+ }
+ response, err := client.Do(request)
+ if err != nil {
+ t.Fatalf("%s %s: %v", method, path, err)
+ }
+ defer response.Body.Close()
+ responseBody, err := io.ReadAll(response.Body)
+ if err != nil {
+ t.Fatalf("read response: %v", err)
+ }
+ if response.StatusCode != wantStatus {
+ t.Fatalf("%s %s: status=%d, want=%d, body=%s", method, path, response.StatusCode, wantStatus, responseBody)
+ }
+ if response.Header.Get("X-Request-Id") == "" {
+ t.Fatalf("%s %s: missing X-Request-Id", method, path)
+ }
+ if target != nil {
+ if err := json.Unmarshal(responseBody, target); err != nil {
+ t.Fatalf("decode response %q: %v", responseBody, err)
+ }
+ }
+}
+
+func (a *testAPI) createUser(t *testing.T, username string) userResponse {
+ t.Helper()
+ var user userResponse
+ a.request(t, http.DefaultClient, http.MethodPost, "/api/v1/users", "",
+ fmt.Sprintf(`{"username":%q,"display_name":%q,"password":"test-password-12345"}`, username, username), http.StatusCreated, &user)
+ req, err := http.NewRequest(http.MethodPost, a.server.URL+"/api/v1/auth/sessions", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":test-password-12345")))
+ response, err := http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer response.Body.Close()
+ var login struct {
+ Token string `json:"token"`
+ }
+ if response.StatusCode != http.StatusCreated {
+ t.Fatalf("login status: %d", response.StatusCode)
+ }
+ if err = json.NewDecoder(response.Body).Decode(&login); err != nil {
+ t.Fatal(err)
+ }
+ a.tokens.Store(user.ID, login.Token)
+ return user
+}
+
+func (a *testAPI) requestError(t *testing.T, method, path, actorID, body string, wantStatus int, wantCode string) {
+ t.Helper()
+ var response errorResponse
+ a.request(t, http.DefaultClient, method, path, actorID, body, wantStatus, &response)
+ if response.Error.Code != wantCode {
+ t.Fatalf("%s %s: error code=%q, want=%q", method, path, response.Error.Code, wantCode)
+ }
+}
+
+func (a *testAPI) createChat(t *testing.T, ownerID string, memberIDs []string) chatResponse {
+ t.Helper()
+ body, err := json.Marshal(map[string]any{"title": "test chat", "member_ids": memberIDs})
+ if err != nil {
+ t.Fatalf("encode chat: %v", err)
+ }
+ var chat chatResponse
+ a.request(t, http.DefaultClient, http.MethodPost, "/api/v1/chats", ownerID, string(body), http.StatusCreated, &chat)
+ return chat
+}
+
+func (a *testAPI) createMessage(t *testing.T, client *http.Client, actorID, chatID, text string) messageResponse {
+ t.Helper()
+ body, err := json.Marshal(map[string]string{"text": text})
+ if err != nil {
+ t.Fatalf("encode message: %v", err)
+ }
+ var message messageResponse
+ a.request(t, client, http.MethodPost, "/api/v1/chats/"+chatID+"/messages", actorID, string(body), http.StatusCreated, &message)
+ return message
+}
+
+func TestRejectsMalformedJSON(t *testing.T) {
+ api := newTestAPI(t)
+ api.requestError(t, http.MethodPost, "/api/v1/users", "",
+ `{"username":`, http.StatusBadRequest, "invalid_request")
+}
+
+func TestHidesForeignChat(t *testing.T) {
+ api := newTestAPI(t)
+ owner := api.createUser(t, "owner_user")
+ outsider := api.createUser(t, "outsider_user")
+ chat := api.createChat(t, owner.ID, []string{owner.ID})
+ api.requestError(t, http.MethodGet, "/api/v1/chats/"+chat.ID, outsider.ID,
+ "", http.StatusNotFound, "not_found")
+}
+
+func TestRejectsEmptyMessage(t *testing.T) {
+ api := newTestAPI(t)
+ user := api.createUser(t, "empty_message_user")
+ chat := api.createChat(t, user.ID, []string{user.ID})
+ api.requestError(t, http.MethodPost, "/api/v1/chats/"+chat.ID+"/messages", user.ID,
+ `{"text":" "}`, http.StatusBadRequest, "invalid_request")
+}
+
+func TestRejectsDuplicateUsername(t *testing.T) {
+ api := newTestAPI(t)
+ api.createUser(t, "duplicate_user")
+ api.requestError(t, http.MethodPost, "/api/v1/users", "",
+ `{"username":"duplicate_user","display_name":"Duplicate","password":"test-password-12345"}`, http.StatusConflict, "conflict")
+}
+
+func TestPaginatesMessagesAcrossMultiplePages(t *testing.T) {
+ api := newTestAPI(t)
+ user := api.createUser(t, "pagination_user")
+ chat := api.createChat(t, user.ID, []string{user.ID})
+ for index := 0; index < 7; index++ {
+ api.createMessage(t, http.DefaultClient, user.ID, chat.ID, fmt.Sprintf("message-%d", index))
+ }
+
+ seen := make(map[string]struct{})
+ cursor := ""
+ pageSizes := make([]int, 0, 4)
+ for {
+ path := "/api/v1/chats/" + chat.ID + "/messages?limit=2"
+ if cursor != "" {
+ path += "&cursor=" + url.QueryEscape(cursor)
+ }
+ var page messagePageResponse
+ api.request(t, http.DefaultClient, http.MethodGet, path, user.ID, "", http.StatusOK, &page)
+ pageSizes = append(pageSizes, len(page.Items))
+ for _, message := range page.Items {
+ if _, duplicate := seen[message.ID]; duplicate {
+ t.Fatalf("message %s appeared on multiple pages", message.ID)
+ }
+ seen[message.ID] = struct{}{}
+ }
+ if page.NextCursor == nil {
+ break
+ }
+ cursor = *page.NextCursor
+ }
+ if len(seen) != 7 {
+ t.Fatalf("received %d unique messages, want 7", len(seen))
+ }
+ wantPageSizes := []int{2, 2, 2, 1}
+ if fmt.Sprint(pageSizes) != fmt.Sprint(wantPageSizes) {
+ t.Fatalf("page sizes=%v, want=%v", pageSizes, wantPageSizes)
+ }
+}
+
+func TestConcurrentMessagesAreUnique(t *testing.T) {
+ api := newTestAPI(t)
+
+ users := make([]userResponse, 10)
+ memberIDs := make([]string, 10)
+ for index := range users {
+ users[index] = api.createUser(t, fmt.Sprintf("parallel_user_%02d", index))
+ memberIDs[index] = users[index].ID
+ }
+ chat := api.createChat(t, users[0].ID, memberIDs)
+
+ start := make(chan struct{})
+ errors := make(chan error, 10)
+ var waitGroup sync.WaitGroup
+
+ for clientIndex := 0; clientIndex < 10; clientIndex++ {
+ waitGroup.Add(1)
+ go func(clientIndex int) {
+ defer waitGroup.Done()
+ client := &http.Client{Transport: &http.Transport{}, Timeout: 5 * time.Second}
+ defer client.CloseIdleConnections()
+ <-start
+ for messageIndex := 0; messageIndex < 10; messageIndex++ {
+ body, err := json.Marshal(map[string]string{
+ "text": fmt.Sprintf("client-%02d-message-%02d", clientIndex, messageIndex),
+ })
+ if err != nil {
+ errors <- err
+ return
+ }
+ request, err := http.NewRequest(http.MethodPost,
+ api.server.URL+"/api/v1/chats/"+chat.ID+"/messages", bytes.NewReader(body))
+ if err != nil {
+ errors <- err
+ return
+ }
+ request.Header.Set("Content-Type", "application/json")
+ request.Header.Set("Idempotency-Key", uuid.NewString())
+ token, _ := api.tokens.Load(users[clientIndex].ID)
+ request.Header.Set("Authorization", "Bearer "+token.(string))
+ response, err := client.Do(request)
+ if err != nil {
+ errors <- err
+ return
+ }
+ _, readErr := io.Copy(io.Discard, response.Body)
+ closeErr := response.Body.Close()
+ if readErr != nil {
+ errors <- readErr
+ return
+ }
+ if closeErr != nil {
+ errors <- closeErr
+ return
+ }
+ if response.StatusCode != http.StatusCreated {
+ errors <- fmt.Errorf("client %d message %d: status=%d", clientIndex, messageIndex, response.StatusCode)
+ return
+ }
+ }
+ }(clientIndex)
+ }
+ close(start)
+ waitGroup.Wait()
+ close(errors)
+ for err := range errors {
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ seenIDs := make(map[string]struct{}, 10*10)
+ seenTexts := make(map[string]struct{}, 10*10)
+ cursor := ""
+ for {
+ path := "/api/v1/chats/" + chat.ID + "/messages?limit=17"
+ if cursor != "" {
+ path += "&cursor=" + url.QueryEscape(cursor)
+ }
+ var page messagePageResponse
+ api.request(t, http.DefaultClient, http.MethodGet, path, users[0].ID, "", http.StatusOK, &page)
+ for _, message := range page.Items {
+ if _, duplicate := seenIDs[message.ID]; duplicate {
+ t.Fatalf("duplicate message ID %s", message.ID)
+ }
+ if _, duplicate := seenTexts[message.Text]; duplicate {
+ t.Fatalf("duplicate message text %q", message.Text)
+ }
+ seenIDs[message.ID] = struct{}{}
+ seenTexts[message.Text] = struct{}{}
+ }
+ if page.NextCursor == nil {
+ break
+ }
+ cursor = *page.NextCursor
+ }
+ if len(seenIDs) != 10*10 || len(seenTexts) != 10*10 {
+ t.Fatalf("received %d unique IDs and %d unique texts, want %d of each",
+ len(seenIDs), len(seenTexts), 10*10)
+ }
+}
+
+// Session fake is only used by HTTP unit tests; the server always uses Redis.
+type testSessions struct {
+ mu sync.Mutex
+ values map[string]domain.Session
+}
+
+func (s *testSessions) Ready(context.Context) error { return nil }
+func (s *testSessions) Put(_ context.Context, key string, value domain.Session) error {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ s.values[key] = value
+ return nil
+}
+func (s *testSessions) Get(_ context.Context, key string) (domain.Session, bool, error) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ value, ok := s.values[key]
+ return value, ok, nil
+}
+func (s *testSessions) Delete(_ context.Context, key string) (bool, error) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ _, ok := s.values[key]
+ delete(s.values, key)
+ return ok, nil
+}
diff --git a/internal/app/chat/session.go b/internal/app/chat/session.go
new file mode 100644
index 00000000..29ce755c
--- /dev/null
+++ b/internal/app/chat/session.go
@@ -0,0 +1,45 @@
+package chat
+
+import (
+ "io"
+ "net/http"
+ "strings"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+)
+
+func bearerToken(c echo.Context) string {
+ parts := strings.Fields(c.Request().Header.Get("Authorization"))
+ if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") {
+ return ""
+ }
+ return parts[1]
+}
+
+func (h *Chat) createSession(c echo.Context) error {
+ username, password, ok := c.Request().BasicAuth()
+ if !ok {
+ c.Response().Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
+ return h.writeError(c, http.StatusUnauthorized, "unauthorized", "Неверные учётные данные")
+ }
+ body, err := io.ReadAll(io.LimitReader(c.Request().Body, 1))
+ if err != nil || len(body) != 0 {
+ return h.invalid(c, "Тело запроса входа должно быть пустым")
+ }
+ login, err := h.useCase.CreateSession(c.Request().Context(), username, password)
+ if err != nil {
+ return h.useCaseError(c, err)
+ }
+ return c.JSON(http.StatusCreated, dto.CreateSessionResponse{
+ Token: login.Token, TokenType: "Bearer", ExpiresAt: dto.Timestamp(login.ExpiresAt), User: createUserResponseFromDomain(login.User),
+ })
+}
+
+func (h *Chat) deleteSession(c echo.Context) error {
+ if err := h.useCase.DeleteSession(c.Request().Context(), bearerToken(c)); err != nil {
+ return h.useCaseError(c, err)
+ }
+ return c.NoContent(http.StatusNoContent)
+}
diff --git a/internal/app/chat/swagger.go b/internal/app/chat/swagger.go
new file mode 100644
index 00000000..fc87c65e
--- /dev/null
+++ b/internal/app/chat/swagger.go
@@ -0,0 +1,38 @@
+package chat
+
+import (
+ "encoding/json"
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/contracts"
+)
+
+var swaggerOpenAPI = makeSwaggerOpenAPI()
+
+func (h *Chat) swagger(c echo.Context) error {
+ return c.Redirect(http.StatusTemporaryRedirect, "/swagger/")
+}
+
+func (h *Chat) swaggerSpec(c echo.Context) error {
+ return c.Blob(http.StatusOK, echo.MIMEApplicationJSON, swaggerOpenAPI)
+}
+
+func makeSwaggerOpenAPI() []byte {
+ var document map[string]any
+ if err := json.Unmarshal(contracts.OpenAPI, &document); err != nil {
+ panic("cannot decode embedded OpenAPI contract: " + err.Error())
+ }
+ // A relative URL keeps Swagger requests on the same scheme, host and port
+ // as the UI. This also works behind a reverse proxy and on a custom PORT.
+ document["servers"] = []map[string]string{{
+ "url": "/",
+ "description": "Текущий адрес Swagger UI",
+ }}
+ encoded, err := json.Marshal(document)
+ if err != nil {
+ panic("cannot encode Swagger OpenAPI contract: " + err.Error())
+ }
+ return encoded
+}
diff --git a/internal/app/chat/usecase.go b/internal/app/chat/usecase.go
new file mode 100644
index 00000000..811a6ef0
--- /dev/null
+++ b/internal/app/chat/usecase.go
@@ -0,0 +1,21 @@
+package chat
+
+import (
+ "context"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+// UseCase is the application boundary required by the HTTP handler.
+type UseCase interface {
+ Ready(context.Context) error
+ CreateSession(context.Context, string, string) (domain.Login, error)
+ DeleteSession(context.Context, string) error
+ Authenticate(ctx context.Context, token string) (domain.User, error)
+ CreateUser(ctx context.Context, user domain.User, password string) (domain.User, error)
+ CreateChat(ctx context.Context, callerID string, chat domain.Chat) (domain.Chat, error)
+ GetChat(ctx context.Context, callerID string, chat domain.Chat) (domain.Chat, error)
+ ListChats(ctx context.Context, callerID string, pagination domain.Pagination) (domain.ChatPage, error)
+ CreateMessage(ctx context.Context, callerID string, message domain.Message) (domain.Message, error)
+ ListMessages(ctx context.Context, callerID string, chat domain.Chat, pagination domain.Pagination) (domain.MessagePage, error)
+}
diff --git a/internal/attachment/image.go b/internal/attachment/image.go
new file mode 100644
index 00000000..3e688cfc
--- /dev/null
+++ b/internal/attachment/image.go
@@ -0,0 +1,128 @@
+package attachment
+
+import (
+ "context"
+ "crypto/sha256"
+ "encoding/hex"
+ "errors"
+ "image"
+ _ "image/jpeg"
+ _ "image/png"
+ "io"
+ "mime"
+ "os"
+ "strings"
+ "unicode"
+ "unicode/utf8"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+const MaxBytes int64 = 10 * 1024 * 1024
+const MaxPixels int64 = 40_000_000
+
+func appError(code domain.ErrorCode, message string) error {
+ return domain.NewApplicationError(code, message)
+}
+func unavailable() error {
+ return appError(domain.ErrorUnavailable, "Хранилище вложений недоступно")
+}
+func ValidateFilename(name string) error {
+ if !utf8.ValidString(name) || strings.TrimSpace(name) == "" || utf8.RuneCountInString(name) > 255 {
+ return appError(domain.ErrorInvalid, "Некорректный filename")
+ }
+ for _, r := range name {
+ if r == '/' || r == '\\' || unicode.IsControl(r) {
+ return appError(domain.ErrorInvalid, "Недопустимые символы filename")
+ }
+ }
+ return nil
+}
+
+type Original struct {
+ File *os.File
+ Size int64
+ SHA256, ContentType string
+ Width, Height int
+}
+
+func (o *Original) Close() { name := o.File.Name(); o.File.Close(); os.Remove(name) }
+
+// Spool bounds compressed bytes on disk before any decode. DecodeConfig checks the
+// pixel budget before a full decode verifies CRCs/truncated scan data.
+func Spool(ctx context.Context, body io.Reader, declared string) (*Original, error) {
+ media, _, err := mime.ParseMediaType(declared)
+ if err != nil || (media != "image/png" && media != "image/jpeg") {
+ return nil, appError("unsupported_media_type", "Допустимы PNG и JPEG")
+ }
+ f, err := os.CreateTemp("", "messenger-upload-*")
+ if err != nil {
+ return nil, unavailable()
+ }
+ o := &Original{File: f}
+ good := false
+ defer func() {
+ if !good {
+ o.Close()
+ }
+ }()
+ hash := sha256.New()
+ n, err := io.Copy(io.MultiWriter(f, hash), io.LimitReader(&contextReader{ctx: ctx, r: body}, MaxBytes+1))
+ if n > MaxBytes {
+ return nil, appError("payload_too_large", "Файл превышает 10 MiB")
+ }
+ if err != nil {
+ return nil, appError(domain.ErrorInvalid, "Загрузка прервана")
+ }
+ o.Size = n
+ o.SHA256 = hex.EncodeToString(hash.Sum(nil))
+ if _, err = f.Seek(0, 0); err != nil {
+ return nil, unavailable()
+ }
+ cfg, format, err := image.DecodeConfig(f)
+ if err != nil {
+ return nil, appError("unsupported_media_type", "Повреждённое изображение")
+ }
+ actual := "image/" + format
+ if actual != media {
+ return nil, appError("unsupported_media_type", "MIME не соответствует содержимому")
+ }
+ if cfg.Width <= 0 || cfg.Height <= 0 || int64(cfg.Width) > MaxPixels/int64(cfg.Height) {
+ return nil, appError("payload_too_large", "Изображение превышает 40 миллионов пикселей")
+ }
+ if _, err = f.Seek(0, 0); err != nil {
+ return nil, unavailable()
+ }
+ decoded, format, err := image.Decode(f)
+ if err != nil || decoded == nil || "image/"+format != actual {
+ return nil, appError("unsupported_media_type", "Повреждённое изображение")
+ }
+ if decoded.Bounds().Dx() != cfg.Width || decoded.Bounds().Dy() != cfg.Height {
+ return nil, appError("unsupported_media_type", "Некорректные размеры изображения")
+ }
+ decoded = nil
+ if _, err = f.Seek(0, 0); err != nil {
+ return nil, unavailable()
+ }
+ o.ContentType = actual
+ o.Width = cfg.Width
+ o.Height = cfg.Height
+ good = true
+ return o, nil
+}
+
+type contextReader struct {
+ ctx context.Context
+ r io.Reader
+}
+
+func (r *contextReader) Read(p []byte) (int, error) {
+ if err := r.ctx.Err(); err != nil {
+ return 0, err
+ }
+ n, err := r.r.Read(p)
+ if errors.Is(err, context.Canceled) {
+ return n, err
+ }
+ return n, err
+}
diff --git a/internal/attachment/image_test.go b/internal/attachment/image_test.go
new file mode 100644
index 00000000..30700b77
--- /dev/null
+++ b/internal/attachment/image_test.go
@@ -0,0 +1,139 @@
+package attachment
+
+import (
+ "bytes"
+ "context"
+ "encoding/binary"
+ "hash/crc32"
+ "image"
+ "image/color"
+ "image/jpeg"
+ "image/png"
+ "io"
+ "testing"
+ "time"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+func pngFixture(t *testing.T) []byte {
+ t.Helper()
+ m := image.NewRGBA(image.Rect(0, 0, 10, 8))
+ m.Set(1, 1, color.RGBA{R: 255, A: 255})
+ var b bytes.Buffer
+ if err := png.Encode(&b, m); err != nil {
+ t.Fatal(err)
+ }
+ return b.Bytes()
+}
+func code(t *testing.T, err error, want domain.ErrorCode) {
+ t.Helper()
+ app, ok := domain.AsApplicationError(err)
+ if !ok || app.Code != want {
+ t.Fatalf("want %s, got %v", want, err)
+ }
+}
+func TestSpoolOriginalAndFullValidation(t *testing.T) {
+ original := pngFixture(t)
+ good, err := Spool(context.Background(), bytes.NewReader(original), "image/png")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer good.Close()
+ got, err := io.ReadAll(good.File)
+ if err != nil || !bytes.Equal(got, original) {
+ t.Fatal("original changed")
+ }
+ if good.Width != 10 || good.Height != 8 || good.Size != int64(len(original)) || len(good.SHA256) != 64 {
+ t.Fatalf("wrong metadata %+v", good)
+ }
+ _, err = Spool(context.Background(), bytes.NewReader(original), "image/jpeg")
+ code(t, err, "unsupported_media_type")
+ _, err = Spool(context.Background(), bytes.NewReader(original[:len(original)-12]), "image/png")
+ code(t, err, "unsupported_media_type")
+ corrupted := bytes.Clone(original)
+ corrupted[len(corrupted)-18] ^= 0x80
+ _, err = Spool(context.Background(), bytes.NewReader(corrupted), "image/png")
+ code(t, err, "unsupported_media_type")
+ var jpegBytes bytes.Buffer
+ if err = jpeg.Encode(&jpegBytes, image.NewRGBA(image.Rect(0, 0, 12, 9)), nil); err != nil {
+ t.Fatal(err)
+ }
+ jpegOriginal, err := Spool(context.Background(), bytes.NewReader(jpegBytes.Bytes()), "image/jpeg")
+ if err != nil {
+ t.Fatal(err)
+ }
+ jpegOriginal.Close()
+ _, err = Spool(context.Background(), bytes.NewReader(jpegBytes.Bytes()[:jpegBytes.Len()/2]), "image/jpeg")
+ code(t, err, "unsupported_media_type")
+}
+func TestSpoolLimitsBeforeDecode(t *testing.T) {
+ // Oversized compressed input is stopped after exactly MaxBytes+1 reads.
+ source := &countingReader{remaining: MaxBytes + 100_000}
+ _, err := Spool(context.Background(), source, "image/png")
+ code(t, err, "payload_too_large")
+ if source.read != MaxBytes+1 {
+ t.Fatalf("read %d bytes", source.read)
+ }
+ bomb := bytes.Clone(pngFixture(t))
+ binary.BigEndian.PutUint32(bomb[16:20], 40001)
+ binary.BigEndian.PutUint32(bomb[20:24], 1000)
+ binary.BigEndian.PutUint32(bomb[29:33], crc32.ChecksumIEEE(bomb[12:29]))
+ _, err = Spool(context.Background(), bytes.NewReader(bomb), "image/png")
+ code(t, err, "payload_too_large")
+}
+
+type countingReader struct{ remaining, read int64 }
+
+func (c *countingReader) Read(p []byte) (int, error) {
+ if c.remaining == 0 {
+ return 0, io.EOF
+ }
+ n := min(int64(len(p)), c.remaining)
+ clear(p[:n])
+ c.remaining -= n
+ c.read += n
+ return int(n), nil
+}
+func TestFilenameAndCancelledUpload(t *testing.T) {
+ for _, name := range []string{"", "../x.png", "x\\photo.jpg", "a\x00b", "a\nb", "\t"} {
+ code(t, ValidateFilename(name), domain.ErrorInvalid)
+ }
+ for _, name := range []string{"фото.png", "image.jpg", ".."} {
+ if err := ValidateFilename(name); err != nil {
+ t.Fatal(err)
+ }
+ }
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+ _, err := Spool(ctx, bytes.NewReader(pngFixture(t)), "image/png")
+ code(t, err, domain.ErrorInvalid)
+}
+func TestReconcileRejectsUnsafeGrace(t *testing.T) {
+ s := &Service{}
+ if _, err := s.Reconcile(context.Background(), true, 89*time.Second); err == nil {
+ t.Fatal("unsafe grace accepted")
+ }
+ if MinimumGrace <= UploadTimeout {
+ t.Fatal("grace does not cover in-flight PUT")
+ }
+}
+
+func TestValid16BitWidePNG(t *testing.T) {
+ // Exercise the same RGBA64 + scanline allocation path as an extreme-aspect
+ // 40M image, at 1/1000 of that pixel count to keep the unit suite small.
+ img := image.NewNRGBA64(image.Rect(0, 0, 40000, 1))
+ img.SetNRGBA64(1, 0, color.NRGBA64{R: 65535, G: 12345, B: 4, A: 32000})
+ var encoded bytes.Buffer
+ if err := png.Encode(&encoded, img); err != nil {
+ t.Fatal(err)
+ }
+ original, err := Spool(context.Background(), bytes.NewReader(encoded.Bytes()), "image/png")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer original.Close()
+ if original.Width != 40000 || original.Height != 1 {
+ t.Fatal("wide PNG dimensions changed")
+ }
+}
diff --git a/internal/attachment/json.go b/internal/attachment/json.go
new file mode 100644
index 00000000..cb3958fb
--- /dev/null
+++ b/internal/attachment/json.go
@@ -0,0 +1,18 @@
+package attachment
+
+import (
+ "encoding/json"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
+)
+
+// MarshalJSON uses the API's fixed microsecond precision instead of time.Time's
+// variable fractional precision, which Python 3.9 clients cannot always parse.
+func (a Attachment) MarshalJSON() ([]byte, error) {
+ type fields Attachment
+ return json.Marshal(struct {
+ fields
+ CreatedAt dto.Timestamp `json:"created_at"`
+ UpdatedAt dto.Timestamp `json:"updated_at"`
+ }{fields: fields(a), CreatedAt: dto.Timestamp(a.CreatedAt), UpdatedAt: dto.Timestamp(a.UpdatedAt)})
+}
diff --git a/internal/attachment/json_test.go b/internal/attachment/json_test.go
new file mode 100644
index 00000000..4812d608
--- /dev/null
+++ b/internal/attachment/json_test.go
@@ -0,0 +1,37 @@
+package attachment
+
+import (
+ "encoding/json"
+ "testing"
+ "time"
+)
+
+func TestAttachmentTimestampsUseFixedMicroseconds(t *testing.T) {
+ value := Attachment{ID: "attachment-id", CreatedAt: time.Date(2026, 1, 2, 3, 4, 5, 123400000, time.UTC), UpdatedAt: time.Date(2026, 1, 2, 6, 4, 5, 0, time.FixedZone("MSK", 3*60*60)), Key: "original/private", Variants: []any{}}
+ encoded, err := json.Marshal(value)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var fields map[string]any
+ if err = json.Unmarshal(encoded, &fields); err != nil {
+ t.Fatal(err)
+ }
+ if fields["created_at"] != "2026-01-02T03:04:05.123400Z" {
+ t.Fatalf("created_at=%v", fields["created_at"])
+ }
+ if fields["updated_at"] != "2026-01-02T03:04:05.000000Z" {
+ t.Fatalf("updated_at=%v", fields["updated_at"])
+ }
+ if fields["id"] != value.ID {
+ t.Fatal("serialization lost metadata")
+ }
+ if _, exposed := fields["Key"]; exposed {
+ t.Fatal("serialization exposed private object key")
+ }
+ if _, exposed := fields["key"]; exposed {
+ t.Fatal("serialization exposed private object key")
+ }
+ if fields["error_code"] != nil {
+ t.Fatal("ready error_code must be null")
+ }
+}
diff --git a/internal/attachment/reconcile.go b/internal/attachment/reconcile.go
new file mode 100644
index 00000000..402d7b3e
--- /dev/null
+++ b/internal/attachment/reconcile.go
@@ -0,0 +1,222 @@
+package attachment
+
+import (
+ "context"
+ "fmt"
+ "time"
+
+ "github.com/jackc/pgx/v5"
+ "github.com/minio/minio-go/v7"
+)
+
+type Summary struct {
+ Apply bool `json:"apply"`
+ GraceSeconds float64 `json:"grace_seconds"`
+ ObjectsScanned int `json:"objects_scanned"`
+ MetadataScanned int `json:"metadata_scanned"`
+ BytesScanned int64 `json:"bytes_scanned"`
+ ListRequestsEstimate int `json:"list_requests_estimate"`
+ StatRequests int `json:"stat_requests"`
+ DBQueries int `json:"db_queries"`
+ Orphans int `json:"orphans"`
+ Missing int `json:"missing"`
+ FreshSkipped int `json:"fresh_skipped"`
+ LockedSkipped int `json:"locked_skipped"`
+ ReferencedMissing int `json:"referenced_missing"`
+ Deleted int `json:"deleted"`
+ MarkedFailed int `json:"marked_failed"`
+ DurationMS int64 `json:"duration_ms"`
+ Actions []Action `json:"actions"`
+}
+type Action struct {
+ Kind string `json:"kind"`
+ Key string `json:"key"`
+ Applied bool `json:"applied"`
+}
+
+// Reconcile scans a fixed cutoff and rechecks each candidate while holding the
+// same transaction-scoped advisory lock as upload. Metadata, including message
+// references, is never deleted. Missing originals become failed tombstones.
+func (s *Service) Reconcile(ctx context.Context, apply bool, grace time.Duration) (result Summary, err error) {
+ started := time.Now()
+ result = Summary{Apply: apply, GraceSeconds: grace.Seconds(), Actions: []Action{}}
+ defer func() { result.DurationMS = time.Since(started).Milliseconds() }()
+ if grace < MinimumGrace {
+ return result, fmt.Errorf("grace must be at least %s (upload timeout %s)", MinimumGrace, UploadTimeout)
+ }
+ cutoff := started.Add(-grace)
+ for obj := range s.Store.Client.ListObjects(ctx, s.Store.Bucket, minio.ListObjectsOptions{Prefix: "original/", Recursive: true, MaxKeys: 1000}) {
+ if obj.Err != nil {
+ return result, fmt.Errorf("S3 listing failed")
+ }
+ result.ObjectsScanned++
+ result.BytesScanned += obj.Size
+ if result.ObjectsScanned%1000 == 1 {
+ result.ListRequestsEstimate++
+ }
+ if !obj.LastModified.Before(cutoff) {
+ result.FreshSkipped++
+ continue
+ }
+ err = s.reconcileObject(ctx, obj.Key, cutoff, apply, &result)
+ if err != nil {
+ return result, err
+ }
+ }
+ // Bounded keyset pages avoid retaining the entire bucket or a DB cursor while
+ // making per-object transactions. A later run picks up concurrent new rows.
+ last := "00000000-0000-0000-0000-000000000000"
+ for {
+ result.DBQueries++
+ rows, e := s.Pool.Query(ctx, `SELECT id::text,object_key,created_at,status FROM attachments WHERE id>$1 ORDER BY id LIMIT 100`, last)
+ if e != nil {
+ return result, fmt.Errorf("metadata scan failed")
+ }
+ type metadata struct {
+ id, key, status string
+ created time.Time
+ }
+ page := []metadata{}
+ for rows.Next() {
+ var m metadata
+ if e = rows.Scan(&m.id, &m.key, &m.created, &m.status); e != nil {
+ rows.Close()
+ return result, e
+ }
+ page = append(page, m)
+ }
+ e = rows.Err()
+ rows.Close()
+ if e != nil {
+ return result, e
+ }
+ if len(page) == 0 {
+ break
+ }
+ for _, m := range page {
+ last = m.id
+ result.MetadataScanned++
+ if m.status != "ready" {
+ continue
+ }
+ if !m.created.Before(cutoff) {
+ result.FreshSkipped++
+ continue
+ }
+ if e = s.reconcileMetadata(ctx, m.id, m.key, cutoff, apply, &result); e != nil {
+ return result, e
+ }
+ }
+ }
+ if ctx.Err() != nil {
+ return result, ctx.Err()
+ }
+ return result, nil
+}
+func (s *Service) locked(ctx context.Context, key string, r *Summary) (pgx.Tx, bool, error) {
+ tx, err := s.Pool.Begin(ctx)
+ if err != nil {
+ return nil, false, fmt.Errorf("DB unavailable")
+ }
+ var locked bool
+ r.DBQueries++
+ if err = tx.QueryRow(ctx, `SELECT pg_try_advisory_xact_lock(hashtextextended($1,0))`, key).Scan(&locked); err != nil {
+ tx.Rollback(context.Background())
+ return nil, false, fmt.Errorf("lock failed")
+ }
+ if !locked {
+ r.LockedSkipped++
+ tx.Rollback(context.Background())
+ return nil, false, nil
+ }
+ return tx, true, nil
+}
+func (s *Service) reconcileObject(ctx context.Context, key string, cutoff time.Time, apply bool, r *Summary) error {
+ tx, ok, err := s.locked(ctx, key, r)
+ if err != nil || !ok {
+ return err
+ }
+ defer tx.Rollback(context.Background())
+ var exists bool
+ r.DBQueries++
+ if err = tx.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM attachments WHERE object_key=$1)`, key).Scan(&exists); err != nil {
+ return fmt.Errorf("metadata recheck failed")
+ }
+ if exists {
+ return nil
+ }
+ r.StatRequests++
+ obj, err := s.Store.Client.StatObject(ctx, s.Store.Bucket, key, minio.StatObjectOptions{})
+ if IsMissing(err) {
+ return nil
+ }
+ if err != nil {
+ return fmt.Errorf("object recheck failed")
+ }
+ if !obj.LastModified.Before(cutoff) {
+ r.FreshSkipped++
+ return nil
+ }
+ r.Orphans++
+ if apply {
+ if err = s.Store.Client.RemoveObject(ctx, s.Store.Bucket, key, minio.RemoveObjectOptions{}); err != nil {
+ return fmt.Errorf("orphan deletion failed")
+ }
+ r.Deleted++
+ }
+ if len(r.Actions) < 100 {
+ r.Actions = append(r.Actions, Action{Kind: "orphan", Key: key, Applied: apply})
+ }
+ return nil
+}
+func (s *Service) reconcileMetadata(ctx context.Context, id, key string, cutoff time.Time, apply bool, r *Summary) error {
+ tx, ok, err := s.locked(ctx, key, r)
+ if err != nil || !ok {
+ return err
+ }
+ defer tx.Rollback(context.Background())
+ var created time.Time
+ var status string
+ r.DBQueries++
+ err = tx.QueryRow(ctx, `SELECT created_at,status FROM attachments WHERE id=$1 FOR UPDATE`, id).Scan(&created, &status)
+ if err == pgx.ErrNoRows {
+ return nil
+ }
+ if err != nil {
+ return fmt.Errorf("metadata recheck failed")
+ }
+ if status != "ready" || !created.Before(cutoff) {
+ return nil
+ }
+ r.StatRequests++
+ _, err = s.Store.Client.StatObject(ctx, s.Store.Bucket, key, minio.StatObjectOptions{})
+ if err == nil {
+ return nil
+ }
+ if !IsMissing(err) {
+ return fmt.Errorf("object stat failed (no mutation)")
+ }
+ var referenced bool
+ r.DBQueries++
+ if err = tx.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM messages WHERE $1::uuid=ANY(attachment_ids))`, id).Scan(&referenced); err != nil {
+ return fmt.Errorf("reference check failed")
+ }
+ r.Missing++
+ if referenced {
+ r.ReferencedMissing++
+ }
+ if apply {
+ r.DBQueries++
+ if _, err = tx.Exec(ctx, `UPDATE attachments SET status='failed',error_code='original_missing' WHERE id=$1 AND status='ready'`, id); err != nil {
+ return fmt.Errorf("mark missing failed")
+ }
+ if err = tx.Commit(ctx); err != nil {
+ return fmt.Errorf("commit missing failed")
+ }
+ r.MarkedFailed++
+ }
+ if len(r.Actions) < 100 {
+ r.Actions = append(r.Actions, Action{Kind: "missing", Key: key, Applied: apply})
+ }
+ return nil
+}
diff --git a/internal/attachment/s3.go b/internal/attachment/s3.go
new file mode 100644
index 00000000..2b1315c9
--- /dev/null
+++ b/internal/attachment/s3.go
@@ -0,0 +1,44 @@
+package attachment
+
+import (
+ "context"
+ "crypto/tls"
+ "net"
+ "net/http"
+ "time"
+
+ "github.com/minio/minio-go/v7"
+ "github.com/minio/minio-go/v7/pkg/credentials"
+)
+
+type Store struct {
+ Client *minio.Client
+ Bucket string
+}
+
+func NewStore(endpoint, bucket, access, secret string, secure bool) (*Store, error) {
+ transport := &http.Transport{Proxy: http.ProxyFromEnvironment, DialContext: (&net.Dialer{Timeout: 3 * time.Second, KeepAlive: 30 * time.Second}).DialContext, MaxIdleConns: 16, MaxIdleConnsPerHost: 8, IdleConnTimeout: 30 * time.Second, ResponseHeaderTimeout: 5 * time.Second, TLSHandshakeTimeout: 5 * time.Second, TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12}}
+ c, err := minio.New(endpoint, &minio.Options{Creds: credentials.NewStaticV4(access, secret, ""), Secure: secure, Region: "us-east-1", Transport: transport, MaxRetries: 1})
+ if err != nil {
+ return nil, err
+ }
+ return &Store{Client: c, Bucket: bucket}, nil
+}
+func (s *Store) Ready(ctx context.Context) error {
+ ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
+ defer cancel()
+ for item := range s.Client.ListObjects(ctx, s.Bucket, minio.ListObjectsOptions{Prefix: "original/", MaxKeys: 1, Recursive: true}) {
+ if item.Err != nil {
+ return unavailable()
+ }
+ return nil
+ }
+ if ctx.Err() != nil {
+ return unavailable()
+ }
+ return nil
+}
+func IsMissing(err error) bool {
+ code := minio.ToErrorResponse(err).Code
+ return code == "NoSuchKey" || code == "NoSuchObject"
+}
diff --git a/internal/attachment/service.go b/internal/attachment/service.go
new file mode 100644
index 00000000..3156afbb
--- /dev/null
+++ b/internal/attachment/service.go
@@ -0,0 +1,132 @@
+package attachment
+
+import (
+ "context"
+ "errors"
+ "io"
+ "log"
+ "time"
+
+ "github.com/google/uuid"
+ "github.com/jackc/pgx/v5"
+ "github.com/jackc/pgx/v5/pgxpool"
+ "github.com/minio/minio-go/v7"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+const UploadTimeout = 45 * time.Second
+const MinimumGrace = 90 * time.Second
+
+type Attachment struct {
+ ID string `json:"id"`
+ ChatID string `json:"chat_id"`
+ OwnerID string `json:"owner_id"`
+ Filename string `json:"filename"`
+ ContentType string `json:"content_type"`
+ Size int64 `json:"size_bytes"`
+ SHA256 string `json:"sha256"`
+ Status string `json:"status"`
+ Variants []any `json:"variants"`
+ ErrorCode *string `json:"error_code"`
+ CreatedAt time.Time `json:"created_at"`
+ UpdatedAt time.Time `json:"updated_at"`
+ Key string `json:"-"`
+}
+
+type Service struct {
+ Pool *pgxpool.Pool
+ Store *Store
+ slots chan struct{}
+}
+
+func New(pool *pgxpool.Pool, store *Store) *Service {
+ return &Service{Pool: pool, Store: store, slots: make(chan struct{}, 1)}
+}
+func (s *Service) Ready(ctx context.Context) error { return s.Store.Ready(ctx) }
+func (s *Service) Upload(ctx context.Context, chatID, userID, name, contentType string, body io.Reader) (Attachment, error) {
+ if err := ValidateFilename(name); err != nil {
+ return Attachment{}, err
+ }
+ ctx, cancel := context.WithTimeout(ctx, UploadTimeout)
+ defer cancel()
+ select {
+ case s.slots <- struct{}{}:
+ defer func() { <-s.slots }()
+ case <-ctx.Done():
+ return Attachment{}, unavailable()
+ }
+ original, err := Spool(ctx, body, contentType)
+ if err != nil {
+ return Attachment{}, err
+ }
+ defer original.Close()
+ a := Attachment{ID: uuid.NewString(), ChatID: chatID, OwnerID: userID, Filename: name, ContentType: original.ContentType, Size: original.Size, SHA256: original.SHA256, Status: "ready", Variants: []any{}}
+ a.Key = "original/" + a.ID
+ tx, err := s.Pool.Begin(ctx)
+ if err != nil {
+ return Attachment{}, unavailable()
+ }
+ defer tx.Rollback(context.Background())
+ // Keep the object lock through PUT, metadata insert and COMMIT. A crash releases
+ // the lock; the grace protects a still-running client PUT at the S3 server.
+ if _, err = tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1,0))`, a.Key); err != nil {
+ return Attachment{}, unavailable()
+ }
+ _, err = s.Store.Client.PutObject(ctx, s.Store.Bucket, a.Key, original.File, a.Size, minio.PutObjectOptions{ContentType: a.ContentType, DisableMultipart: true, UserMetadata: map[string]string{"sha256": a.SHA256}})
+ if err != nil {
+ return Attachment{}, unavailable()
+ }
+ err = tx.QueryRow(ctx, `INSERT INTO attachments(id,chat_id,uploader_id,filename,object_key,content_type,size_bytes,sha256,width,height) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING created_at`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.Key, a.ContentType, a.Size, a.SHA256, original.Width, original.Height).Scan(&a.CreatedAt)
+ if err != nil {
+ log.Print("attachment metadata failed; orphan retained for grace-period reconciliation")
+ return Attachment{}, unavailable()
+ }
+ // Never DELETE on an ambiguous COMMIT failure: the DB may have committed. A
+ // later reconciler checks both stores under the same lock before any cleanup.
+ if err = tx.Commit(ctx); err != nil {
+ return Attachment{}, unavailable()
+ }
+ a.UpdatedAt = a.CreatedAt
+ return a, nil
+}
+func (s *Service) Get(ctx context.Context, id, userID string) (Attachment, error) {
+ parsed, err := uuid.Parse(id)
+ if err != nil {
+ return Attachment{}, appError(domain.ErrorInvalid, "Некорректный attachment_id")
+ }
+ ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
+ defer cancel()
+ a := Attachment{Variants: []any{}}
+ err = s.Pool.QueryRow(ctx, `SELECT a.id::text,a.chat_id::text,a.uploader_id::text,a.filename,a.object_key,a.content_type,a.size_bytes,a.sha256,a.status,a.error_code,a.created_at FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id WHERE a.id=$1 AND m.user_id=$2`, parsed.String(), userID).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.Key, &a.ContentType, &a.Size, &a.SHA256, &a.Status, &a.ErrorCode, &a.CreatedAt)
+ if errors.Is(err, pgx.ErrNoRows) {
+ return Attachment{}, appError(domain.ErrorNotFound, "Объект не найден")
+ }
+ if err != nil {
+ return Attachment{}, unavailable()
+ }
+ a.UpdatedAt = a.CreatedAt
+ // Keep the mandatory lab4 ready-only metadata schema. Failed originals are
+ // represented by 409; internal status remains inspectable in reconciliation.
+ if a.Status != "ready" {
+ return Attachment{}, appError(domain.ErrorConflict, "Оригинал вложения недоступен")
+ }
+ return a, nil
+}
+func (s *Service) Content(ctx context.Context, a Attachment) (*minio.Object, error) {
+ if a.Status != "ready" {
+ return nil, appError(domain.ErrorConflict, "Вложение не готово")
+ }
+ obj, err := s.Store.Client.GetObject(ctx, s.Store.Bucket, a.Key, minio.GetObjectOptions{})
+ if err != nil {
+ return nil, unavailable()
+ }
+ if _, err = obj.Stat(); err != nil {
+ obj.Close()
+ if IsMissing(err) {
+ return nil, appError(domain.ErrorConflict, "Оригинал вложения недоступен")
+ }
+ return nil, unavailable()
+ }
+ return obj, nil
+}
diff --git a/internal/config/config.go b/internal/config/config.go
new file mode 100644
index 00000000..ace6c240
--- /dev/null
+++ b/internal/config/config.go
@@ -0,0 +1,119 @@
+package config
+
+import (
+ "encoding/hex"
+ "fmt"
+ "os"
+ "strconv"
+ "strings"
+ "time"
+)
+
+type Config struct {
+ S3Endpoint string
+ S3Bucket string
+ S3AccessKey string
+ S3SecretKey string
+ S3UseSSL bool
+ Port string
+ PostgresHost string
+ PostgresPort uint16
+ PostgresUser string
+ PostgresDatabase string
+ PostgresPassword string
+ PostgresMaxConns int32
+ RedisAddr string
+ SessionTTL time.Duration
+ QueryTimeout time.Duration
+ CursorKey [32]byte
+}
+
+func value(name, fallback string) string {
+ if v := os.Getenv(name); v != "" {
+ return v
+ }
+ return fallback
+}
+func positive(name string, fallback, max int) (int, error) {
+ n, err := strconv.Atoi(value(name, strconv.Itoa(fallback)))
+ if err != nil || n < 1 || n > max {
+ return 0, fmt.Errorf("invalid %s", name)
+ }
+ return n, nil
+}
+func secret(name string) (string, error) {
+ if file := os.Getenv(name + "_FILE"); file != "" {
+ data, err := os.ReadFile(file)
+ if err != nil {
+ return "", fmt.Errorf("cannot read %s_FILE", name)
+ }
+ if s := strings.TrimSpace(string(data)); s != "" {
+ return s, nil
+ }
+ }
+ if s := os.Getenv(name); s != "" {
+ return s, nil
+ }
+ return "", fmt.Errorf("%s or %s_FILE is required", name, name)
+}
+
+func Load() (Config, error) {
+ var c Config
+ port, err := positive("PORT", 8080, 65535)
+ if err != nil {
+ return c, err
+ }
+ c.Port = strconv.Itoa(port)
+ pgPort, err := positive("POSTGRES_PORT", 5432, 65535)
+ if err != nil {
+ return c, err
+ }
+ c.PostgresPort = uint16(pgPort)
+ max, err := positive("POSTGRES_MAX_CONNS", 10, 100)
+ if err != nil {
+ return c, err
+ }
+ c.PostgresMaxConns = int32(max)
+ ttl, err := positive("SESSION_TTL_SECONDS", 3600, 86400*30)
+ if err != nil {
+ return c, err
+ }
+ c.SessionTTL = time.Duration(ttl) * time.Second
+ timeout, err := positive("QUERY_TIMEOUT_MS", 2000, 10000)
+ if err != nil {
+ return c, err
+ }
+ c.QueryTimeout = time.Duration(timeout) * time.Millisecond
+ c.PostgresHost = value("POSTGRES_HOST", "localhost")
+ c.PostgresUser = value("POSTGRES_USER", "messenger")
+ c.PostgresDatabase = value("POSTGRES_DB", "messenger")
+ c.RedisAddr = value("REDIS_ADDR", "localhost:6379")
+ c.PostgresPassword, err = secret("POSTGRES_PASSWORD")
+ if err != nil {
+ return c, err
+ }
+ key, err := secret("CURSOR_KEY")
+ if err != nil {
+ return c, err
+ }
+ decoded, err := hex.DecodeString(key)
+ if err != nil || len(decoded) != 32 {
+ return c, fmt.Errorf("CURSOR_KEY must be 32 random bytes encoded as hex")
+ }
+ copy(c.CursorKey[:], decoded)
+ c.S3Endpoint = value("S3_ENDPOINT", "minio:9000")
+ c.S3Bucket = value("S3_BUCKET", "attachments")
+ c.S3UseSSL, err = strconv.ParseBool(value("S3_USE_SSL", "false"))
+ if err != nil {
+ return c, fmt.Errorf("invalid S3_USE_SSL")
+ }
+ c.S3AccessKey, err = secret("S3_ACCESS_KEY")
+ if err != nil {
+ return c, err
+ }
+ c.S3SecretKey, err = secret("S3_SECRET_KEY")
+ if err != nil {
+ return c, err
+ }
+ return c, nil
+}
diff --git a/internal/pkg/domain/chat.go b/internal/pkg/domain/chat.go
new file mode 100644
index 00000000..2de7174e
--- /dev/null
+++ b/internal/pkg/domain/chat.go
@@ -0,0 +1,10 @@
+package domain
+
+import "time"
+
+type Chat struct {
+ ID string
+ Title string
+ MemberIDs []string
+ CreatedAt time.Time
+}
diff --git a/internal/pkg/domain/errors.go b/internal/pkg/domain/errors.go
new file mode 100644
index 00000000..a8447d22
--- /dev/null
+++ b/internal/pkg/domain/errors.go
@@ -0,0 +1,32 @@
+package domain
+
+import "errors"
+
+type ErrorCode string
+
+const (
+ ErrorUnavailable ErrorCode = "unavailable"
+ ErrorInvalid ErrorCode = "invalid_request"
+ ErrorUnauthorized ErrorCode = "unauthorized"
+ ErrorNotFound ErrorCode = "not_found"
+ ErrorConflict ErrorCode = "conflict"
+)
+
+type ApplicationError struct {
+ Code ErrorCode
+ Message string
+}
+
+func (e *ApplicationError) Error() string {
+ return e.Message
+}
+
+func NewApplicationError(code ErrorCode, message string) error {
+ return &ApplicationError{Code: code, Message: message}
+}
+
+func AsApplicationError(err error) (*ApplicationError, bool) {
+ var applicationError *ApplicationError
+ ok := errors.As(err, &applicationError)
+ return applicationError, ok
+}
diff --git a/internal/pkg/domain/message.go b/internal/pkg/domain/message.go
new file mode 100644
index 00000000..34879969
--- /dev/null
+++ b/internal/pkg/domain/message.go
@@ -0,0 +1,13 @@
+package domain
+
+import "time"
+
+type Message struct {
+ IdempotencyKey string
+ ID string
+ ChatID string
+ SenderID string
+ AttachmentIDs []string
+ Text string
+ CreatedAt time.Time
+}
diff --git a/internal/pkg/domain/pagination.go b/internal/pkg/domain/pagination.go
new file mode 100644
index 00000000..d3ab68f4
--- /dev/null
+++ b/internal/pkg/domain/pagination.go
@@ -0,0 +1,23 @@
+package domain
+
+import "time"
+
+type Position struct {
+ CreatedAt time.Time
+ ID string
+}
+
+type Pagination struct {
+ After *Position
+ Limit int
+}
+
+type ChatPage struct {
+ Items []Chat
+ HasMore bool
+}
+
+type MessagePage struct {
+ Items []Message
+ HasMore bool
+}
diff --git a/internal/pkg/domain/session.go b/internal/pkg/domain/session.go
new file mode 100644
index 00000000..50029b96
--- /dev/null
+++ b/internal/pkg/domain/session.go
@@ -0,0 +1,14 @@
+package domain
+
+import "time"
+
+type Session struct {
+ UserID string `json:"user_id"`
+ ExpiresAt time.Time `json:"expires_at"`
+}
+
+type Login struct {
+ Token string
+ ExpiresAt time.Time
+ User User
+}
diff --git a/internal/pkg/domain/user.go b/internal/pkg/domain/user.go
new file mode 100644
index 00000000..34d52353
--- /dev/null
+++ b/internal/pkg/domain/user.go
@@ -0,0 +1,10 @@
+package domain
+
+import "time"
+
+type User struct {
+ ID string
+ Username string
+ DisplayName string
+ CreatedAt time.Time
+}
diff --git a/internal/pkg/password/password.go b/internal/pkg/password/password.go
new file mode 100644
index 00000000..1280dca7
--- /dev/null
+++ b/internal/pkg/password/password.go
@@ -0,0 +1,65 @@
+package password
+
+import (
+ "context"
+ "crypto/rand"
+ "crypto/subtle"
+ "encoding/base64"
+ "errors"
+ "strings"
+
+ "golang.org/x/crypto/argon2"
+)
+
+// Bound simultaneous KDF work to 4 * 19 MiB, including concurrent registrations.
+var slots = make(chan struct{}, 4)
+
+const prefix = "$argon2id$v=19$m=19456,t=2,p=1$"
+
+func derive(ctx context.Context, password string, salt []byte) ([]byte, error) {
+ select {
+ case slots <- struct{}{}:
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ }
+ defer func() { <-slots }()
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ return argon2.IDKey([]byte(password), salt, 2, 19456, 1, 32), nil
+}
+
+func Hash(ctx context.Context, password string) (string, error) {
+ salt := make([]byte, 16)
+ if _, err := rand.Read(salt); err != nil {
+ return "", err
+ }
+ key, err := derive(ctx, password, salt)
+ if err != nil {
+ return "", err
+ }
+ return prefix + base64.RawStdEncoding.EncodeToString(salt) + "$" + base64.RawStdEncoding.EncodeToString(key), nil
+}
+
+func Verify(ctx context.Context, password, encoded string) (bool, error) {
+ if !strings.HasPrefix(encoded, prefix) {
+ return false, errors.New("unsupported password hash")
+ }
+ parts := strings.Split(strings.TrimPrefix(encoded, prefix), "$")
+ if len(parts) != 2 {
+ return false, errors.New("invalid password hash")
+ }
+ salt, err := base64.RawStdEncoding.DecodeString(parts[0])
+ if err != nil || len(salt) != 16 {
+ return false, errors.New("invalid salt")
+ }
+ expected, err := base64.RawStdEncoding.DecodeString(parts[1])
+ if err != nil || len(expected) != 32 {
+ return false, errors.New("invalid hash")
+ }
+ actual, err := derive(ctx, password, salt)
+ return subtle.ConstantTimeCompare(actual, expected) == 1, err
+}
+
+// A valid fixed encoding for equal-cost verification when username does not exist.
+const DummyHash = prefix + "AAAAAAAAAAAAAAAAAAAAAA$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
diff --git a/internal/pkg/password/password_test.go b/internal/pkg/password/password_test.go
new file mode 100644
index 00000000..6a0ef969
--- /dev/null
+++ b/internal/pkg/password/password_test.go
@@ -0,0 +1,35 @@
+package password_test
+
+import (
+ "context"
+ "strings"
+ "testing"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password"
+)
+
+func TestUnicodeAndSalt(t *testing.T) {
+ ctx := context.Background()
+ secret := strings.Repeat("🔑", 128)
+ first, err := password.Hash(ctx, secret)
+ if err != nil {
+ t.Fatal(err)
+ }
+ second, err := password.Hash(ctx, secret)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if first == second {
+ t.Fatal("password hashes reused salt")
+ }
+ for _, hash := range []string{first, second} {
+ ok, err := password.Verify(ctx, secret, hash)
+ if err != nil || !ok {
+ t.Fatal("128-code-point UTF-8 password rejected")
+ }
+ ok, err = password.Verify(ctx, strings.Repeat("🔑", 127)+"x", hash)
+ if err != nil || ok {
+ t.Fatal("password suffix was ignored")
+ }
+ }
+}
diff --git a/internal/pkg/repository/memory.go b/internal/pkg/repository/memory.go
new file mode 100644
index 00000000..1c269015
--- /dev/null
+++ b/internal/pkg/repository/memory.go
@@ -0,0 +1,202 @@
+package repository
+
+import (
+ "context"
+ "sort"
+ "sync"
+ "time"
+
+ "github.com/google/uuid"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+ chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat"
+)
+
+// Memory is the lab1 in-memory Repository implementation.
+type Memory struct {
+ mu sync.RWMutex
+ users map[string]domain.User
+ hashes map[string]string
+ userByUsername map[string]string
+ chats map[string]domain.Chat
+ messages map[string][]domain.Message
+}
+
+var _ chatusecase.Repository = (*Memory)(nil)
+
+func NewMemory() *Memory {
+ return &Memory{
+ users: make(map[string]domain.User),
+ hashes: make(map[string]string),
+ userByUsername: make(map[string]string),
+ chats: make(map[string]domain.Chat),
+ messages: make(map[string][]domain.Message),
+ }
+}
+
+func (m *Memory) CreateUser(_ context.Context, username, displayName, hash string) (domain.User, error) {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+
+ if _, exists := m.userByUsername[username]; exists {
+ return domain.User{}, chatusecase.ErrUsernameTaken
+ }
+ user := domain.User{
+ ID: uuid.NewString(),
+ Username: username,
+ DisplayName: displayName,
+ CreatedAt: now(),
+ }
+ m.users[user.ID] = user
+ m.hashes[user.ID] = hash
+ m.userByUsername[user.Username] = user.ID
+ return user, nil
+}
+
+func (m *Memory) User(_ context.Context, id string) (domain.User, bool, error) {
+ m.mu.RLock()
+ defer m.mu.RUnlock()
+ user, ok := m.users[id]
+ return user, ok, nil
+}
+
+func (m *Memory) CreateChat(_ context.Context, title string, memberIDs []string) (domain.Chat, error) {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+
+ for _, memberID := range memberIDs {
+ if _, exists := m.users[memberID]; !exists {
+ return domain.Chat{}, chatusecase.ErrUserNotFound
+ }
+ }
+ chat := domain.Chat{
+ ID: uuid.NewString(),
+ Title: title,
+ MemberIDs: append([]string(nil), memberIDs...),
+ CreatedAt: now(),
+ }
+ m.chats[chat.ID] = chat
+ return cloneChat(chat), nil
+}
+
+func (m *Memory) ChatForUser(_ context.Context, chatID, userID string) (domain.Chat, error) {
+ m.mu.RLock()
+ defer m.mu.RUnlock()
+ chat, exists := m.chats[chatID]
+ if !exists {
+ return domain.Chat{}, chatusecase.ErrChatNotFound
+ }
+ if !contains(chat.MemberIDs, userID) {
+ return domain.Chat{}, chatusecase.ErrNotMember
+ }
+ return cloneChat(chat), nil
+}
+
+func (m *Memory) ListChats(_ context.Context, userID string, after *domain.Position, limit int) ([]domain.Chat, error) {
+ m.mu.RLock()
+ defer m.mu.RUnlock()
+
+ result := make([]domain.Chat, 0)
+ for _, chat := range m.chats {
+ if contains(chat.MemberIDs, userID) && isAfter(chat.CreatedAt, chat.ID, after) {
+ result = append(result, cloneChat(chat))
+ }
+ }
+ sort.Slice(result, func(i, j int) bool {
+ return less(result[i].CreatedAt, result[i].ID, result[j].CreatedAt, result[j].ID)
+ })
+ if len(result) > limit {
+ result = result[:limit]
+ }
+ return result, nil
+}
+
+func (m *Memory) CreateMessage(_ context.Context, chatID, senderID, text, key string) (domain.Message, error) {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+
+ chat, exists := m.chats[chatID]
+ if !exists {
+ return domain.Message{}, chatusecase.ErrChatNotFound
+ }
+ if !contains(chat.MemberIDs, senderID) {
+ return domain.Message{}, chatusecase.ErrNotMember
+ }
+ if key != "" {
+ for _, previous := range m.messages[chatID] {
+ if previous.SenderID == senderID && previous.IdempotencyKey == key {
+ if previous.Text != text {
+ return domain.Message{}, domain.NewApplicationError(domain.ErrorConflict, "Idempotency-Key уже использован с другим телом")
+ }
+ return previous, nil
+ }
+ }
+ }
+ message := domain.Message{
+ IdempotencyKey: key,
+ ID: uuid.NewString(),
+ ChatID: chatID,
+ SenderID: senderID,
+ Text: text,
+ CreatedAt: now(),
+ }
+ m.messages[chatID] = append(m.messages[chatID], message)
+ return message, nil
+}
+
+func (m *Memory) ListMessages(_ context.Context, chatID string, after *domain.Position, limit int) ([]domain.Message, error) {
+ m.mu.RLock()
+ defer m.mu.RUnlock()
+
+ values := m.messages[chatID]
+ result := make([]domain.Message, 0, min(len(values), limit))
+ for _, message := range values {
+ if isAfter(message.CreatedAt, message.ID, after) {
+ result = append(result, message)
+ }
+ }
+ sort.Slice(result, func(i, j int) bool {
+ return less(result[i].CreatedAt, result[i].ID, result[j].CreatedAt, result[j].ID)
+ })
+ if len(result) > limit {
+ result = result[:limit]
+ }
+ return result, nil
+}
+
+func cloneChat(chat domain.Chat) domain.Chat {
+ chat.MemberIDs = append([]string(nil), chat.MemberIDs...)
+ return chat
+}
+
+func now() time.Time {
+ return time.Now().UTC().Truncate(time.Second)
+}
+
+func contains(values []string, target string) bool {
+ for _, value := range values {
+ if value == target {
+ return true
+ }
+ }
+ return false
+}
+
+func isAfter(createdAt time.Time, id string, after *domain.Position) bool {
+ return after == nil || createdAt.After(after.CreatedAt) || createdAt.Equal(after.CreatedAt) && id > after.ID
+}
+
+func less(leftTime time.Time, leftID string, rightTime time.Time, rightID string) bool {
+ if leftTime.Equal(rightTime) {
+ return leftID < rightID
+ }
+ return leftTime.Before(rightTime)
+}
+
+func (m *Memory) Ready(context.Context) error { return nil }
+func (m *Memory) UserByUsername(_ context.Context, username string) (domain.User, string, bool, error) {
+ m.mu.RLock()
+ defer m.mu.RUnlock()
+ id, ok := m.userByUsername[username]
+ return m.users[id], m.hashes[id], ok, nil
+}
diff --git a/internal/pkg/repository/postgres/chats.go b/internal/pkg/repository/postgres/chats.go
new file mode 100644
index 00000000..60c9051c
--- /dev/null
+++ b/internal/pkg/repository/postgres/chats.go
@@ -0,0 +1,78 @@
+package postgres
+
+import (
+ "context"
+ "errors"
+
+ "github.com/google/uuid"
+ "github.com/jackc/pgx/v5"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+ chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat"
+)
+
+func (s *Storage) CreateChat(ctx context.Context, title string, members []string) (domain.Chat, error) {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ tx, err := s.pool.Begin(ctx)
+ if err != nil {
+ return domain.Chat{}, dbError(err)
+ }
+ defer tx.Rollback(context.WithoutCancel(ctx))
+ chat := domain.Chat{ID: uuid.NewString(), Title: title, MemberIDs: append([]string(nil), members...)}
+ err = tx.QueryRow(ctx, `INSERT INTO chats(id,title) VALUES($1,$2) RETURNING created_at`, chat.ID, title).Scan(&chat.CreatedAt)
+ if err != nil {
+ return domain.Chat{}, dbError(err)
+ }
+ for i, member := range members {
+ if _, err = tx.Exec(ctx, `INSERT INTO chat_members(chat_id,user_id,ordinal) VALUES($1,$2,$3)`, chat.ID, member, i); err != nil {
+ return domain.Chat{}, dbError(err)
+ }
+ }
+ if err = tx.Commit(ctx); err != nil {
+ return domain.Chat{}, dbError(err)
+ }
+ return chat, nil
+}
+
+const chatColumns = `c.id::text,c.title,c.created_at,ARRAY(SELECT m.user_id::text FROM chat_members m WHERE m.chat_id=c.id ORDER BY m.ordinal)`
+
+func (s *Storage) ChatForUser(ctx context.Context, chatID, userID string) (domain.Chat, error) {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ var chat domain.Chat
+ err := s.pool.QueryRow(ctx, `SELECT `+chatColumns+` FROM chats c
+ WHERE c.id=$1 AND EXISTS(SELECT 1 FROM chat_members m WHERE m.chat_id=c.id AND m.user_id=$2)`, chatID, userID).
+ Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs)
+ if errors.Is(err, pgx.ErrNoRows) {
+ return chat, chatusecase.ErrChatNotFound
+ }
+ return chat, dbError(err)
+}
+
+func (s *Storage) ListChats(ctx context.Context, userID string, after *domain.Position, limit int) ([]domain.Chat, error) {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ query := `SELECT ` + chatColumns + ` FROM chats c JOIN chat_members member ON member.chat_id=c.id
+ WHERE member.user_id=$1`
+ args := []any{userID, limit}
+ if after != nil {
+ query += ` AND (c.created_at,c.id)>($3,$4)`
+ args = append(args, after.CreatedAt, after.ID)
+ }
+ query += ` ORDER BY c.created_at,c.id LIMIT $2`
+ rows, err := s.pool.Query(ctx, query, args...)
+ if err != nil {
+ return nil, dbError(err)
+ }
+ defer rows.Close()
+ chats := make([]domain.Chat, 0)
+ for rows.Next() {
+ var chat domain.Chat
+ if err = rows.Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs); err != nil {
+ return nil, dbError(err)
+ }
+ chats = append(chats, chat)
+ }
+ return chats, dbError(rows.Err())
+}
diff --git a/internal/pkg/repository/postgres/lab4_test.go b/internal/pkg/repository/postgres/lab4_test.go
new file mode 100644
index 00000000..53ab8d94
--- /dev/null
+++ b/internal/pkg/repository/postgres/lab4_test.go
@@ -0,0 +1,341 @@
+package postgres_test
+
+import (
+ "bufio"
+ "bytes"
+ "context"
+ "encoding/xml"
+ "fmt"
+ "image"
+ "image/png"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strconv"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/attachment"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository/postgres"
+ "github.com/google/uuid"
+ "github.com/jackc/pgx/v5/pgxpool"
+ "github.com/minio/minio-go/v7"
+ "github.com/minio/minio-go/v7/pkg/credentials"
+)
+
+type fakeObject struct {
+ data []byte
+ modified time.Time
+}
+type fakeS3 struct {
+ mu sync.Mutex
+ objects map[string]fakeObject
+ failPUT bool
+}
+
+func (f *fakeS3) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ key := strings.TrimPrefix(r.URL.Path, "/attachments/")
+ if r.Method == "GET" && r.URL.Path == "/attachments/" || r.Method == "GET" && r.URL.Path == "/attachments" {
+ type item struct {
+ Key string
+ LastModified string
+ ETag string
+ Size int
+ }
+ type listing struct {
+ XMLName xml.Name `xml:"ListBucketResult"`
+ Xmlns string `xml:"xmlns,attr"`
+ Name string
+ Prefix string
+ MaxKeys int
+ IsTruncated bool
+ Contents []item
+ }
+ l := listing{Xmlns: "http://s3.amazonaws.com/doc/2006-03-01/", Name: "attachments", Prefix: r.URL.Query().Get("prefix"), MaxKeys: 1000}
+ for k, v := range f.objects {
+ if strings.HasPrefix(k, l.Prefix) {
+ l.Contents = append(l.Contents, item{Key: k, LastModified: v.modified.UTC().Format("2006-01-02T15:04:05.000Z"), ETag: `"abc"`, Size: len(v.data)})
+ }
+ }
+ w.Header().Set("Content-Type", "application/xml")
+ xml.NewEncoder(w).Encode(l)
+ return
+ }
+ if r.Method == "PUT" {
+ if f.failPUT {
+ w.WriteHeader(503)
+ fmt.Fprint(w, `ServiceUnavailable`)
+ return
+ }
+ var data []byte
+ var err error
+ if strings.Contains(r.Header.Get("Content-Encoding"), "aws-chunked") || strings.HasPrefix(r.Header.Get("X-Amz-Content-Sha256"), "STREAMING-") {
+ reader := bufio.NewReader(r.Body)
+ for {
+ line, e := reader.ReadString('\n')
+ if e != nil {
+ err = e
+ break
+ }
+ size, e := strconv.ParseInt(strings.Split(strings.TrimSpace(line), ";")[0], 16, 64)
+ if e != nil {
+ err = e
+ break
+ }
+ if size == 0 {
+ break
+ }
+ chunk := make([]byte, size)
+ if _, e = io.ReadFull(reader, chunk); e != nil {
+ err = e
+ break
+ }
+ data = append(data, chunk...)
+ if _, e = reader.Discard(2); e != nil {
+ err = e
+ break
+ }
+ }
+ } else {
+ data, err = io.ReadAll(r.Body)
+ }
+ if err != nil {
+ w.WriteHeader(400)
+ return
+ }
+ f.objects[key] = fakeObject{data: data, modified: time.Now().UTC()}
+ w.Header().Set("ETag", `"900150983cd24fb0d6963f7d28e17f72"`)
+ w.WriteHeader(200)
+ return
+ }
+ obj, ok := f.objects[key]
+ if !ok {
+ w.WriteHeader(404)
+ if r.Method != "HEAD" {
+ fmt.Fprint(w, `NoSuchKey`)
+ }
+ return
+ }
+ if r.Method == "DELETE" {
+ delete(f.objects, key)
+ w.WriteHeader(204)
+ return
+ }
+ w.Header().Set("Last-Modified", obj.modified.UTC().Format(http.TimeFormat))
+ w.Header().Set("ETag", `"abc"`)
+ w.Header().Set("Content-Length", fmt.Sprint(len(obj.data)))
+ w.Header().Set("Content-Type", "image/png")
+ if r.Method == "GET" {
+ w.Write(obj.data)
+ }
+}
+func lab4Backend(t *testing.T, pool *pgxpool.Pool, repo *postgres.Storage, user domain.User, chat domain.Chat) {
+ ctx := context.Background()
+ fake := &fakeS3{objects: map[string]fakeObject{}}
+ server := httptest.NewServer(fake)
+ defer server.Close()
+ client, err := minio.New(strings.TrimPrefix(server.URL, "http://"), &minio.Options{Creds: credentials.NewStaticV4("test-key", "test-secret", ""), Region: "us-east-1", MaxRetries: 1})
+ if err != nil {
+ t.Fatal(err)
+ }
+ service := attachment.New(pool, &attachment.Store{Client: client, Bucket: "attachments"})
+ var imageBytes bytes.Buffer
+ if err = png.Encode(&imageBytes, image.NewRGBA(image.Rect(0, 0, 10, 8))); err != nil {
+ t.Fatal(err)
+ }
+ upload := func(name string) attachment.Attachment {
+ t.Helper()
+ a, e := service.Upload(ctx, chat.ID, user.ID, name, "image/png", bytes.NewReader(imageBytes.Bytes()))
+ if e != nil {
+ t.Fatal(e)
+ }
+ return a
+ }
+ first := upload("first.png")
+ second := upload("second.png")
+ requireCode := func(err error, want domain.ErrorCode) {
+ t.Helper()
+ e, ok := domain.AsApplicationError(err)
+ if !ok || e.Code != want {
+ t.Fatalf("expected %s, got %v", want, err)
+ }
+ }
+ outside, err := repo.CreateUser(ctx, "attachment_outsider", "Outside", "hash")
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = service.Get(ctx, first.ID, outside.ID)
+ requireCode(err, domain.ErrorNotFound)
+ obj, err := service.Content(ctx, first)
+ if err != nil {
+ t.Fatal(err)
+ }
+ data, err := io.ReadAll(obj)
+ obj.Close()
+ if err != nil || !bytes.Equal(data, imageBytes.Bytes()) {
+ t.Fatalf("original mismatch: %v", err)
+ }
+ otherChat, err := repo.CreateChat(ctx, "Other", []string{user.ID})
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = repo.CreateMessageWithAttachments(ctx, otherChat.ID, user.ID, "", "cross-chat", []string{first.ID})
+ requireCode(err, domain.ErrorNotFound)
+ _, err = repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "unknown", []string{uuid.NewString()})
+ requireCode(err, domain.ErrorNotFound)
+ sent, err := repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "ordered", []string{first.ID, second.ID})
+ if err != nil {
+ t.Fatal(err)
+ }
+ replay, err := repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "ordered", []string{first.ID, second.ID})
+ if err != nil || sent.ID != replay.ID {
+ t.Fatalf("replay failed: %v", err)
+ }
+ _, err = repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "ordered", []string{second.ID, first.ID})
+ requireCode(err, domain.ErrorConflict)
+ textOnly, err := repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "text", "empty-equivalent", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ replay, err = repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "text", "empty-equivalent", []string{})
+ if err != nil || textOnly.ID != replay.ID {
+ t.Fatalf("absent/empty differs: %v", err)
+ }
+
+ const callers = 12
+ outcomes := make(chan domain.Message, callers)
+ failures := make(chan error, callers)
+ var concurrent sync.WaitGroup
+ start := make(chan struct{})
+ for i := 0; i < callers; i++ {
+ concurrent.Add(1)
+ go func() {
+ defer concurrent.Done()
+ <-start
+ m, e := repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "concurrent-attachments", []string{first.ID, second.ID})
+ if e != nil {
+ failures <- e
+ } else {
+ outcomes <- m
+ }
+ }()
+ }
+ close(start)
+ concurrent.Wait()
+ close(outcomes)
+ close(failures)
+ for e := range failures {
+ t.Fatal(e)
+ }
+ identity := ""
+ for m := range outcomes {
+ if identity == "" {
+ identity = m.ID
+ }
+ if m.ID != identity {
+ t.Fatal("duplicate concurrent attachment message")
+ }
+ }
+ var effects int
+ if err = pool.QueryRow(ctx, `SELECT count(*) FROM messages WHERE chat_id=$1 AND idempotency_key='concurrent-attachments'`, chat.ID).Scan(&effects); err != nil || effects != 1 {
+ t.Fatalf("concurrent effects=%d %v", effects, err)
+ }
+ t.Log("ACL, original bytes, cross-chat attachment rejection, ordered/concurrent idempotency and absent=[] passed")
+
+ old := time.Now().Add(-3 * time.Minute)
+ orphan := "original/" + uuid.NewString()
+ fresh := "original/" + uuid.NewString()
+ locked := "original/" + uuid.NewString()
+ fake.mu.Lock()
+ delete(fake.objects, first.Key)
+ v := fake.objects[second.Key]
+ v.modified = old
+ fake.objects[second.Key] = v
+ fake.objects[orphan] = fakeObject{data: []byte("orphan"), modified: old}
+ fake.objects[fresh] = fakeObject{data: []byte("fresh"), modified: time.Now()}
+ fake.objects[locked] = fakeObject{data: []byte("locked"), modified: old}
+ fake.mu.Unlock()
+ if _, err = pool.Exec(ctx, `UPDATE attachments SET created_at=$1 WHERE id=ANY($2::uuid[])`, old, []string{first.ID, second.ID}); err != nil {
+ t.Fatal(err)
+ }
+ hold, err := pool.Begin(ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer hold.Rollback(ctx)
+ if _, err = hold.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1,0))`, locked); err != nil {
+ t.Fatal(err)
+ }
+ dry, err := service.Reconcile(ctx, false, 90*time.Second)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if dry.Orphans != 1 || dry.Missing != 1 || dry.ReferencedMissing != 1 || dry.LockedSkipped != 1 || dry.FreshSkipped < 1 || dry.Deleted != 0 || dry.MarkedFailed != 0 {
+ t.Fatalf("bad dry-run %+v", dry)
+ }
+ var status string
+ if err = pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id=$1`, first.ID).Scan(&status); err != nil || status != "ready" {
+ t.Fatalf("dry-run mutated metadata %s %v", status, err)
+ }
+ applied, err := service.Reconcile(ctx, true, 90*time.Second)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if applied.Deleted != 1 || applied.MarkedFailed != 1 {
+ t.Fatalf("bad apply %+v", applied)
+ }
+ fake.mu.Lock()
+ _, hasOrphan := fake.objects[orphan]
+ _, hasFresh := fake.objects[fresh]
+ _, hasLinked := fake.objects[second.Key]
+ _, hasLocked := fake.objects[locked]
+ fake.mu.Unlock()
+ if hasOrphan || !hasFresh || !hasLinked || !hasLocked {
+ t.Fatal("unsafe object cleanup")
+ }
+ _, err = service.Get(ctx, first.ID, user.ID)
+ requireCode(err, domain.ErrorConflict)
+ _, err = repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "failed-attachment", []string{first.ID})
+ requireCode(err, domain.ErrorConflict)
+ replay, err = repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "ordered", []string{first.ID, second.ID})
+ if err != nil || replay.ID != sent.ID {
+ t.Fatalf("historical replay changed after missing: %v", err)
+ }
+ repeated, err := service.Reconcile(ctx, true, 90*time.Second)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if repeated.Deleted != 0 || repeated.MarkedFailed != 0 || repeated.Orphans != 0 || repeated.Missing != 0 {
+ t.Fatalf("not repeatable %+v", repeated)
+ }
+ t.Logf("dry-run=%+v apply=%+v repeat=%+v; fresh, locked and referenced objects retained", dry, applied, repeated)
+}
+
+func TestS3WireFixture(t *testing.T) {
+ fake := &fakeS3{objects: map[string]fakeObject{}}
+ server := httptest.NewServer(fake)
+ defer server.Close()
+ client, err := minio.New(strings.TrimPrefix(server.URL, "http://"), &minio.Options{Creds: credentials.NewStaticV4("test-key", "test-secret", ""), Region: "us-east-1", MaxRetries: 1})
+ if err != nil {
+ t.Fatal(err)
+ }
+ payload := []byte("exact object bytes")
+ _, err = client.PutObject(context.Background(), "attachments", "original/test", bytes.NewReader(payload), int64(len(payload)), minio.PutObjectOptions{ContentType: "image/png", DisableMultipart: true})
+ if err != nil {
+ t.Fatal(err)
+ }
+ obj, err := client.GetObject(context.Background(), "attachments", "original/test", minio.GetObjectOptions{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer obj.Close()
+ got, err := io.ReadAll(obj)
+ if err != nil || !bytes.Equal(got, payload) {
+ t.Fatalf("wire fixture did not store original: %q %v", got, err)
+ }
+}
diff --git a/internal/pkg/repository/postgres/messages.go b/internal/pkg/repository/postgres/messages.go
new file mode 100644
index 00000000..3453e78a
--- /dev/null
+++ b/internal/pkg/repository/postgres/messages.go
@@ -0,0 +1,96 @@
+package postgres
+
+import (
+ "context"
+ "errors"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+ "github.com/google/uuid"
+ "github.com/jackc/pgx/v5"
+ "slices"
+)
+
+func (s *Storage) CreateMessage(ctx context.Context, chatID, senderID, text, key string) (domain.Message, error) {
+ return s.CreateMessageWithAttachments(ctx, chatID, senderID, text, key, nil)
+}
+
+func (s *Storage) CreateMessageWithAttachments(ctx context.Context, chatID, senderID, text, key string, ids []string) (domain.Message, error) {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ if ids == nil {
+ ids = []string{}
+ }
+ tx, err := s.pool.Begin(ctx)
+ if err != nil {
+ return domain.Message{}, dbError(err)
+ }
+ defer tx.Rollback(context.Background())
+ // Serialize this idempotency scope before validating attachments. A replay remains a
+ // replay even when reconciliation has since marked an original as missing.
+ if key != "" {
+ _, err = tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1,0))`, senderID+":"+chatID+":"+key)
+ if err != nil {
+ return domain.Message{}, dbError(err)
+ }
+ old := domain.Message{ChatID: chatID, SenderID: senderID}
+ err = tx.QueryRow(ctx, `SELECT id::text,text,attachment_ids::text[],created_at FROM messages WHERE sender_id=$1 AND chat_id=$2 AND idempotency_key=$3`, senderID, chatID, key).Scan(&old.ID, &old.Text, &old.AttachmentIDs, &old.CreatedAt)
+ if err == nil {
+ if old.Text != text || !slices.Equal(old.AttachmentIDs, ids) {
+ return domain.Message{}, domain.NewApplicationError(domain.ErrorConflict, "Idempotency-Key уже использован с другим телом")
+ }
+ return old, nil
+ }
+ if !errors.Is(err, pgx.ErrNoRows) {
+ return domain.Message{}, dbError(err)
+ }
+ }
+ // Lock each metadata row against reconciler updates until the link is committed.
+ for _, id := range ids {
+ var status string
+ err = tx.QueryRow(ctx, `SELECT status FROM attachments WHERE id=$1 AND chat_id=$2 FOR SHARE`, id, chatID).Scan(&status)
+ if errors.Is(err, pgx.ErrNoRows) {
+ return domain.Message{}, domain.NewApplicationError(domain.ErrorNotFound, "Объект не найден")
+ }
+ if err != nil {
+ return domain.Message{}, dbError(err)
+ }
+ if status != "ready" {
+ return domain.Message{}, domain.NewApplicationError(domain.ErrorConflict, "Вложение не готово")
+ }
+ }
+ m := domain.Message{ID: uuid.NewString(), ChatID: chatID, SenderID: senderID, Text: text, AttachmentIDs: ids}
+ var storedKey *string
+ if key != "" {
+ storedKey = &key
+ }
+ err = tx.QueryRow(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,idempotency_key,attachment_ids) VALUES($1,$2,$3,$4,$5,$6) RETURNING created_at`, m.ID, chatID, senderID, text, storedKey, ids).Scan(&m.CreatedAt)
+ if err != nil {
+ return domain.Message{}, dbError(err)
+ }
+ return m, dbError(tx.Commit(ctx))
+}
+
+func (s *Storage) ListMessages(ctx context.Context, chatID string, after *domain.Position, limit int) ([]domain.Message, error) {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ query := `SELECT m.id::text,m.chat_id::text,m.sender_id::text,m.text,m.attachment_ids::text[],m.created_at FROM messages m WHERE m.chat_id=$1`
+ args := []any{chatID, limit}
+ if after != nil {
+ query += ` AND (m.created_at,m.id)>($3,$4)`
+ args = append(args, after.CreatedAt, after.ID)
+ }
+ query += ` ORDER BY m.created_at,m.id LIMIT $2`
+ rows, err := s.pool.Query(ctx, query, args...)
+ if err != nil {
+ return nil, dbError(err)
+ }
+ defer rows.Close()
+ result := []domain.Message{}
+ for rows.Next() {
+ var m domain.Message
+ if err = rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.AttachmentIDs, &m.CreatedAt); err != nil {
+ return nil, dbError(err)
+ }
+ result = append(result, m)
+ }
+ return result, dbError(rows.Err())
+}
diff --git a/internal/pkg/repository/postgres/storage.go b/internal/pkg/repository/postgres/storage.go
new file mode 100644
index 00000000..22a93b14
--- /dev/null
+++ b/internal/pkg/repository/postgres/storage.go
@@ -0,0 +1,51 @@
+package postgres
+
+import (
+ "context"
+ "errors"
+ "log"
+ "time"
+
+ "github.com/jackc/pgx/v5"
+ "github.com/jackc/pgx/v5/pgconn"
+ "github.com/jackc/pgx/v5/pgxpool"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+ chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat"
+)
+
+type Storage struct {
+ pool *pgxpool.Pool
+ timeout time.Duration
+}
+
+var _ chatusecase.Repository = (*Storage)(nil)
+
+func New(pool *pgxpool.Pool, timeout time.Duration) *Storage {
+ return &Storage{pool: pool, timeout: timeout}
+}
+
+func (s *Storage) Ready(ctx context.Context) error {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ return dbError(s.pool.Ping(ctx))
+}
+
+func dbError(err error) error {
+ if err == nil {
+ return nil
+ }
+ var pgErr *pgconn.PgError
+ if errors.As(err, &pgErr) {
+ switch pgErr.Code {
+ case "23505":
+ return chatusecase.ErrUsernameTaken
+ case "23503":
+ return chatusecase.ErrUserNotFound
+ }
+ log.Printf("postgres operation failed: sqlstate=%s", pgErr.Code)
+ } else if !errors.Is(err, pgx.ErrNoRows) {
+ log.Print("postgres operation failed")
+ }
+ return domain.NewApplicationError(domain.ErrorUnavailable, "Хранилище недоступно")
+}
diff --git a/internal/pkg/repository/postgres/storage_test.go b/internal/pkg/repository/postgres/storage_test.go
new file mode 100644
index 00000000..838f1ad0
--- /dev/null
+++ b/internal/pkg/repository/postgres/storage_test.go
@@ -0,0 +1,289 @@
+package postgres_test
+
+import (
+ "context"
+ "errors"
+ "net"
+ "net/url"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strconv"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/google/uuid"
+ "github.com/jackc/pgx/v5/pgxpool"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository/postgres"
+ chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat"
+)
+
+func TestIntegration(t *testing.T) {
+ if os.Getenv("INTEGRATION_TESTS") != "1" {
+ t.Skip("run make test-integration against the Compose stack")
+ }
+ ctx := context.Background()
+ cfg, err := config.Load()
+ if err != nil {
+ t.Fatal(err)
+ }
+ pc, err := pgxpool.ParseConfig("sslmode=disable")
+ if err != nil {
+ t.Fatal(err)
+ }
+ pc.ConnConfig.Host = cfg.PostgresHost
+ pc.ConnConfig.Port = cfg.PostgresPort
+ pc.ConnConfig.User = cfg.PostgresUser
+ pc.ConnConfig.Database = cfg.PostgresDatabase
+ pc.ConnConfig.Password = cfg.PostgresPassword
+ pc.ConnConfig.RuntimeParams["timezone"] = "UTC"
+ pc.MaxConns = 4
+ admin, err := pgxpool.NewWithConfig(ctx, pc.Copy())
+ if err != nil {
+ t.Fatal("connect failed")
+ }
+ defer admin.Close()
+ // Identifier is generated by the test, never from an API request.
+ schema := "test_" + uuid.New().String()[:8]
+ if _, err = admin.Exec(ctx, `CREATE SCHEMA `+schema); err != nil {
+ t.Fatal(err)
+ }
+ defer admin.Exec(ctx, `DROP SCHEMA `+schema+` CASCADE`)
+ pc.ConnConfig.RuntimeParams["search_path"] = schema
+ pool, err := pgxpool.NewWithConfig(ctx, pc)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer pool.Close()
+ runGoose := func(t *testing.T, command string) {
+ t.Helper()
+ migrationDir := os.Getenv("GOOSE_MIGRATION_DIR")
+ if migrationDir == "" {
+ migrationDir, err = filepath.Abs("../../../../migrations")
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ connection := url.URL{Scheme: "postgres", User: url.User(cfg.PostgresUser), Host: net.JoinHostPort(cfg.PostgresHost, strconv.Itoa(int(cfg.PostgresPort))), Path: cfg.PostgresDatabase}
+ connection.RawQuery = url.Values{"sslmode": {"disable"}, "search_path": {schema}, "connect_timeout": {"5"}}.Encode()
+ commandCtx, cancel := context.WithTimeout(ctx, 45*time.Second)
+ defer cancel()
+ commandExec := exec.CommandContext(commandCtx, "goose", "-env", "/dev/null", "-dir", migrationDir, "-timeout", "30s", command)
+ commandExec.Env = append(os.Environ(), "GOOSE_DRIVER=postgres", "GOOSE_DBSTRING="+connection.String(), "PGPASSWORD="+cfg.PostgresPassword)
+ output, err := commandExec.CombinedOutput()
+ if err != nil {
+ t.Fatalf("goose %s: %v: %s", command, err, strings.ReplaceAll(string(output), cfg.PostgresPassword, "[redacted]"))
+ }
+ t.Logf("goose %s: %s", command, output)
+ }
+ runGoose(t, "up")
+ runGoose(t, "up")
+ var versions int
+ if err = pool.QueryRow(ctx, `SELECT count(*) FROM goose_db_version WHERE version_id=1 AND is_applied`).Scan(&versions); err != nil || versions != 1 {
+ t.Fatalf("migration versions=%d error=%v", versions, err)
+ }
+ repo := postgres.New(pool, 2*time.Second)
+ t.Run("registration_race", func(t *testing.T) {
+ const count = 12
+ results := make(chan error, count)
+ var wg sync.WaitGroup
+ start := make(chan struct{})
+ for i := 0; i < count; i++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ _, err := repo.CreateUser(ctx, "same_username", "Same", "test-hash")
+ results <- err
+ }()
+ }
+ close(start)
+ wg.Wait()
+ close(results)
+ successes, conflicts := 0, 0
+ for err := range results {
+ if err == nil {
+ successes++
+ } else if errors.Is(err, chatusecase.ErrUsernameTaken) {
+ conflicts++
+ } else {
+ t.Fatal(err)
+ }
+ }
+ if successes != 1 || conflicts != count-1 {
+ t.Fatalf("successes=%d conflicts=%d", successes, conflicts)
+ }
+ t.Logf("12 concurrent INSERTs: %d success, %d unique conflicts", successes, conflicts)
+ })
+ user, err := repo.CreateUser(ctx, "history_owner", "Owner", "test-hash")
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Run("rollback", func(t *testing.T) {
+ _, err := repo.CreateChat(ctx, "must rollback", []string{user.ID, uuid.NewString()})
+ if !errors.Is(err, chatusecase.ErrUserNotFound) {
+ t.Fatalf("unexpected error %v", err)
+ }
+ var chats, members int
+ if err = pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM chats),(SELECT count(*) FROM chat_members)`).Scan(&chats, &members); err != nil {
+ t.Fatal(err)
+ }
+ if chats != 0 || members != 0 {
+ t.Fatalf("partial transaction: chats=%d members=%d", chats, members)
+ }
+ t.Log("failed second member INSERT left 0 chats and 0 members")
+ })
+ chat, err := repo.CreateChat(ctx, "History", []string{user.ID})
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Run("lab4_attachments_and_reconciliation", func(t *testing.T) {
+ isolated, e := repo.CreateChat(ctx, "Lab4", []string{user.ID})
+ if e != nil {
+ t.Fatal(e)
+ }
+ lab4Backend(t, pool, repo, user, isolated)
+ })
+ _, err = pool.Exec(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,created_at)
+ SELECT md5(i::text)::uuid,$1,$2,'synthetic message '||i,
+ timestamptz '2026-01-01 00:00:00+00' + (i / 10) * interval '1 millisecond'
+ FROM generate_series(1,10000) i`, chat.ID, user.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err = pool.Exec(ctx, `ANALYZE messages`); err != nil {
+ t.Fatal(err)
+ }
+ t.Run("keyset_and_explain", func(t *testing.T) {
+ seen := make(map[string]bool)
+ var after *domain.Position
+ for {
+ page, err := repo.ListMessages(ctx, chat.ID, after, 97)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(page) == 0 {
+ break
+ }
+ for _, message := range page {
+ if seen[message.ID] {
+ t.Fatal("duplicate on page boundary")
+ }
+ seen[message.ID] = true
+ }
+ last := page[len(page)-1]
+ after = &domain.Position{CreatedAt: last.CreatedAt, ID: last.ID}
+ }
+ if len(seen) != 10000 {
+ t.Fatalf("history count=%d", len(seen))
+ }
+ rows, err := pool.Query(ctx, `EXPLAIN (ANALYZE, BUFFERS)
+ SELECT m.id::text,m.chat_id::text,m.sender_id::text,m.text,m.created_at FROM messages m
+ WHERE m.chat_id=$1 AND (m.created_at,m.id)>($2,$3) ORDER BY m.created_at,m.id LIMIT $4`,
+ chat.ID, time.Date(2026, 1, 1, 0, 0, 0, 900000000, time.UTC), "00000000-0000-0000-0000-000000000000", 51)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer rows.Close()
+ t.Log("EXPLAIN on 10,000 messages, cursor near the end (9,000):")
+ for rows.Next() {
+ var line string
+ if err = rows.Scan(&line); err != nil {
+ t.Fatal(err)
+ }
+ t.Log(line)
+ }
+ if err = rows.Err(); err != nil {
+ t.Fatal(err)
+ }
+ t.Log("keyset traversal returned 10,000 distinct messages, including equal timestamps")
+ })
+ t.Run("idempotency_race", func(t *testing.T) {
+ isolated, err := repo.CreateChat(ctx, "Idempotency", []string{user.ID})
+ if err != nil {
+ t.Fatal(err)
+ }
+ const count = 16
+ results := make(chan domain.Message, count)
+ failures := make(chan error, count)
+ start := make(chan struct{})
+ var wg sync.WaitGroup
+ for i := 0; i < count; i++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ message, err := repo.CreateMessage(ctx, isolated.ID, user.ID, "exactly once", "shared-key")
+ if err != nil {
+ failures <- err
+ return
+ }
+ results <- message
+ }()
+ }
+ close(start)
+ wg.Wait()
+ close(results)
+ close(failures)
+ for err := range failures {
+ t.Fatal(err)
+ }
+ var first domain.Message
+ for message := range results {
+ if first.ID == "" {
+ first = message
+ }
+ if message.ID != first.ID || !message.CreatedAt.Equal(first.CreatedAt) || message.Text != first.Text {
+ t.Fatal("concurrent replay changed result")
+ }
+ }
+ var rows int
+ if err := pool.QueryRow(ctx, `SELECT count(*) FROM messages WHERE chat_id=$1`, isolated.ID).Scan(&rows); err != nil || rows != 1 {
+ t.Fatalf("expected one durable effect, got %d: %v", rows, err)
+ }
+ _, err = repo.CreateMessage(ctx, isolated.ID, user.ID, "different", "shared-key")
+ applicationError, ok := domain.AsApplicationError(err)
+ if !ok || applicationError.Code != domain.ErrorConflict {
+ t.Fatalf("expected conflict, got %v", err)
+ }
+ t.Log("16 concurrent PostgreSQL requests: one effect, identical replay, changed body conflicts")
+ })
+ t.Run("goose_legacy_and_down", func(t *testing.T) {
+ // This is the disposable test schema, never the application's public schema.
+ const digestSQL = `SELECT md5(string_agg(ROW(id,chat_id,sender_id,text,created_at)::text,',' ORDER BY id)) FROM messages`
+ var before, after string
+ if err = pool.QueryRow(ctx, digestSQL).Scan(&before); err != nil {
+ t.Fatal(err)
+ }
+ if _, err = pool.Exec(ctx, `DROP TABLE goose_db_version;
+ CREATE TABLE schema_migrations(version text PRIMARY KEY,applied_at timestamptz NOT NULL DEFAULT now());
+ INSERT INTO schema_migrations(version) VALUES('001_initial.sql')`); err != nil {
+ t.Fatal(err)
+ }
+ runGoose(t, "up")
+ runGoose(t, "up")
+ if err = pool.QueryRow(ctx, digestSQL).Scan(&after); err != nil {
+ t.Fatal(err)
+ }
+ if before != after {
+ t.Fatal("legacy migration changed message data")
+ }
+ t.Log("adopted legacy schema: all existing messages are unchanged")
+ runGoose(t, "reset")
+ var tables int
+ if err = pool.QueryRow(ctx, `SELECT count(*) FROM information_schema.tables WHERE table_schema=$1 AND table_name IN ('users','chats','chat_members','messages','schema_migrations')`, schema).Scan(&tables); err != nil || tables != 0 {
+ t.Fatalf("down left %d application tables: %v", tables, err)
+ }
+ runGoose(t, "up")
+ if err = pool.QueryRow(ctx, `SELECT count(*) FROM messages`).Scan(&tables); err != nil || tables != 0 {
+ t.Fatalf("up after down failed: %v", err)
+ }
+ t.Log("down and up succeeded in isolated schema")
+ })
+
+}
diff --git a/internal/pkg/repository/postgres/users.go b/internal/pkg/repository/postgres/users.go
new file mode 100644
index 00000000..ad3f8c15
--- /dev/null
+++ b/internal/pkg/repository/postgres/users.go
@@ -0,0 +1,46 @@
+package postgres
+
+import (
+ "context"
+ "errors"
+
+ "github.com/google/uuid"
+ "github.com/jackc/pgx/v5"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+func (s *Storage) CreateUser(ctx context.Context, username, displayName, hash string) (domain.User, error) {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ var user domain.User
+ err := s.pool.QueryRow(ctx, `INSERT INTO users(id,username,display_name,password_hash) VALUES($1,$2,$3,$4)
+ RETURNING id::text,username,display_name,created_at`, uuid.NewString(), username, displayName, hash).
+ Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
+ return user, dbError(err)
+}
+
+func (s *Storage) User(ctx context.Context, id string) (domain.User, bool, error) {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ var user domain.User
+ err := s.pool.QueryRow(ctx, `SELECT id::text,username,display_name,created_at FROM users WHERE id=$1`, id).
+ Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
+ if errors.Is(err, pgx.ErrNoRows) {
+ return user, false, nil
+ }
+ return user, err == nil, dbError(err)
+}
+
+func (s *Storage) UserByUsername(ctx context.Context, username string) (domain.User, string, bool, error) {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ var user domain.User
+ var hash string
+ err := s.pool.QueryRow(ctx, `SELECT id::text,username,display_name,created_at,password_hash FROM users WHERE username=$1`, username).
+ Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash)
+ if errors.Is(err, pgx.ErrNoRows) {
+ return user, "", false, nil
+ }
+ return user, hash, err == nil, dbError(err)
+}
diff --git a/internal/pkg/session/redis.go b/internal/pkg/session/redis.go
new file mode 100644
index 00000000..13721b4b
--- /dev/null
+++ b/internal/pkg/session/redis.go
@@ -0,0 +1,71 @@
+package session
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "log"
+ "time"
+
+ "github.com/redis/go-redis/v9"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+type Redis struct {
+ client *redis.Client
+ timeout time.Duration
+}
+
+func NewRedis(client *redis.Client, timeout time.Duration) *Redis {
+ return &Redis{client: client, timeout: timeout}
+}
+
+func unavailable(err error) error {
+ if err == nil {
+ return nil
+ }
+ log.Print("redis operation failed")
+ return domain.NewApplicationError(domain.ErrorUnavailable, "Хранилище сессий недоступно")
+}
+
+func (s *Redis) Ready(ctx context.Context) error {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ return unavailable(s.client.Ping(ctx).Err())
+}
+
+func (s *Redis) Put(ctx context.Context, key string, value domain.Session) error {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ data, err := json.Marshal(value)
+ if err != nil {
+ return err
+ }
+ // Absolute expiry prevents TTL extension by network delay or restart.
+ return unavailable(s.client.Do(ctx, "SET", "session:"+key, data, "PXAT", value.ExpiresAt.UnixMilli()).Err())
+}
+
+func (s *Redis) Get(ctx context.Context, key string) (domain.Session, bool, error) {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ var value domain.Session
+ data, err := s.client.Get(ctx, "session:"+key).Bytes()
+ if errors.Is(err, redis.Nil) {
+ return value, false, nil
+ }
+ if err != nil {
+ return value, false, unavailable(err)
+ }
+ if err = json.Unmarshal(data, &value); err != nil {
+ return value, false, unavailable(err)
+ }
+ return value, time.Now().Before(value.ExpiresAt), nil
+}
+
+func (s *Redis) Delete(ctx context.Context, key string) (bool, error) {
+ ctx, cancel := context.WithTimeout(ctx, s.timeout)
+ defer cancel()
+ count, err := s.client.Del(ctx, "session:"+key).Result()
+ return count > 0, unavailable(err)
+}
diff --git a/internal/usecase/chat/repository.go b/internal/usecase/chat/repository.go
new file mode 100644
index 00000000..004d1f67
--- /dev/null
+++ b/internal/usecase/chat/repository.go
@@ -0,0 +1,28 @@
+package chat
+
+import (
+ "context"
+ "errors"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+)
+
+var (
+ ErrUsernameTaken = errors.New("username is already taken")
+ ErrUserNotFound = errors.New("user not found")
+ ErrChatNotFound = errors.New("chat not found")
+ ErrNotMember = errors.New("user is not a chat member")
+)
+
+// Repository is the persistence port required by the chat use case.
+type Repository interface {
+ Ready(ctx context.Context) error
+ UserByUsername(ctx context.Context, username string) (domain.User, string, bool, error)
+ CreateUser(ctx context.Context, username, displayName, passwordHash string) (domain.User, error)
+ User(ctx context.Context, id string) (domain.User, bool, error)
+ CreateChat(ctx context.Context, title string, memberIDs []string) (domain.Chat, error)
+ ChatForUser(ctx context.Context, chatID, userID string) (domain.Chat, error)
+ ListChats(ctx context.Context, userID string, after *domain.Position, limit int) ([]domain.Chat, error)
+ CreateMessage(ctx context.Context, chatID, senderID, text, key string) (domain.Message, error)
+ ListMessages(ctx context.Context, chatID string, after *domain.Position, limit int) ([]domain.Message, error)
+}
diff --git a/internal/usecase/chat/service.go b/internal/usecase/chat/service.go
new file mode 100644
index 00000000..0ca9425e
--- /dev/null
+++ b/internal/usecase/chat/service.go
@@ -0,0 +1,182 @@
+package chat
+
+import (
+ "context"
+ "errors"
+ "strings"
+ "time"
+ "unicode/utf8"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password"
+)
+
+type UseCase struct {
+ repository Repository
+ sessions Sessions
+ sessionTTL time.Duration
+}
+
+func New(repository Repository, sessions Sessions, ttl time.Duration) *UseCase {
+ return &UseCase{repository: repository, sessions: sessions, sessionTTL: ttl}
+}
+
+func (u *UseCase) CreateUser(ctx context.Context, user domain.User, secret string) (domain.User, error) {
+ if !validUsername(user.Username) {
+ return domain.User{}, invalid("username должен соответствовать [a-z0-9_]{3,32}")
+ }
+ if !validRequiredText(user.DisplayName, 100) {
+ return domain.User{}, invalid("display_name должен содержать от 1 до 100 символов")
+ }
+ if !validPassword(secret) {
+ return domain.User{}, invalid("password должен содержать от 12 до 128 символов")
+ }
+ hash, err := password.Hash(ctx, secret)
+ if err != nil {
+ return domain.User{}, err
+ }
+ created, err := u.repository.CreateUser(ctx, user.Username, user.DisplayName, hash)
+ if errors.Is(err, ErrUsernameTaken) {
+ return domain.User{}, domain.NewApplicationError(domain.ErrorConflict, "Пользователь с таким username уже существует")
+ }
+ return created, err
+}
+
+func (u *UseCase) CreateChat(ctx context.Context, callerID string, chat domain.Chat) (domain.Chat, error) {
+ if !validRequiredText(chat.Title, 100) {
+ return domain.Chat{}, invalid("title должен содержать от 1 до 100 символов")
+ }
+ if len(chat.MemberIDs) < 1 || len(chat.MemberIDs) > 100 {
+ return domain.Chat{}, invalid("member_ids должен содержать от 1 до 100 участников")
+ }
+
+ normalizedIDs := make([]string, len(chat.MemberIDs))
+ seen := make(map[string]struct{}, len(chat.MemberIDs))
+ callerIncluded := false
+ for index, memberID := range chat.MemberIDs {
+ normalized, valid := normalizeUUID(memberID)
+ if !valid {
+ return domain.Chat{}, invalid("member_ids содержит некорректный UUID")
+ }
+ if _, duplicate := seen[normalized]; duplicate {
+ return domain.Chat{}, invalid("member_ids содержит повторяющиеся UUID")
+ }
+ normalizedIDs[index] = normalized
+ seen[normalized] = struct{}{}
+ callerIncluded = callerIncluded || normalized == callerID
+ }
+ if !callerIncluded {
+ return domain.Chat{}, invalid("текущий пользователь должен входить в member_ids")
+ }
+
+ created, err := u.repository.CreateChat(ctx, chat.Title, normalizedIDs)
+ if errors.Is(err, ErrUserNotFound) {
+ return domain.Chat{}, invalid("member_ids содержит неизвестного пользователя")
+ }
+ return created, err
+}
+
+func (u *UseCase) GetChat(ctx context.Context, callerID string, chat domain.Chat) (domain.Chat, error) {
+ chatID, valid := normalizeUUID(chat.ID)
+ if !valid {
+ return domain.Chat{}, invalid("Некорректный chat_id")
+ }
+ return u.chatForUser(ctx, chatID, callerID)
+}
+
+func (u *UseCase) ListChats(ctx context.Context, callerID string, pagination domain.Pagination) (domain.ChatPage, error) {
+ if pagination.Limit < 1 || pagination.Limit > 100 {
+ return domain.ChatPage{}, invalid("limit должен быть целым числом от 1 до 100")
+ }
+ items, err := u.repository.ListChats(ctx, callerID, pagination.After, pagination.Limit+1)
+ if err != nil {
+ return domain.ChatPage{}, err
+ }
+ page := domain.ChatPage{Items: items, HasMore: len(items) > pagination.Limit}
+ if page.HasMore {
+ page.Items = page.Items[:pagination.Limit]
+ }
+ return page, nil
+}
+
+func (u *UseCase) CreateMessage(ctx context.Context, callerID string, message domain.Message) (domain.Message, error) {
+ chatID, valid := normalizeUUID(message.ChatID)
+ if !valid {
+ return domain.Message{}, invalid("Некорректный chat_id")
+ }
+ if _, err := u.chatForUser(ctx, chatID, callerID); err != nil {
+ return domain.Message{}, err
+ }
+ if !utf8.ValidString(message.Text) || utf8.RuneCountInString(message.Text) > 4000 || (strings.TrimSpace(message.Text) == "" && len(message.AttachmentIDs) == 0) {
+ return domain.Message{}, invalid("text должен содержать от 1 до 4000 символов")
+ }
+ if len(message.AttachmentIDs) > 10 {
+ return domain.Message{}, invalid("Не более 10 вложений")
+ }
+ seen := map[string]bool{}
+ for i, id := range message.AttachmentIDs {
+ normalized, ok := normalizeUUID(id)
+ if !ok || seen[normalized] {
+ return domain.Message{}, invalid("Некорректные или повторные attachment_ids")
+ }
+ seen[normalized] = true
+ message.AttachmentIDs[i] = normalized
+ }
+ var created domain.Message
+ var err error
+ if repository, ok := u.repository.(interface {
+ CreateMessageWithAttachments(context.Context, string, string, string, string, []string) (domain.Message, error)
+ }); ok {
+ created, err = repository.CreateMessageWithAttachments(ctx, chatID, callerID, message.Text, message.IdempotencyKey, message.AttachmentIDs)
+ } else if len(message.AttachmentIDs) > 0 {
+ return domain.Message{}, notFound()
+ } else {
+ created, err = u.repository.CreateMessage(ctx, chatID, callerID, message.Text, message.IdempotencyKey)
+ }
+ if errors.Is(err, ErrChatNotFound) || errors.Is(err, ErrNotMember) {
+ return domain.Message{}, notFound()
+ }
+ return created, err
+}
+
+func (u *UseCase) ListMessages(ctx context.Context, callerID string, chat domain.Chat, pagination domain.Pagination) (domain.MessagePage, error) {
+ chatID, valid := normalizeUUID(chat.ID)
+ if !valid {
+ return domain.MessagePage{}, invalid("Некорректный chat_id")
+ }
+ if _, err := u.chatForUser(ctx, chatID, callerID); err != nil {
+ return domain.MessagePage{}, err
+ }
+ if pagination.Limit < 1 || pagination.Limit > 100 {
+ return domain.MessagePage{}, invalid("limit должен быть целым числом от 1 до 100")
+ }
+ items, err := u.repository.ListMessages(ctx, chatID, pagination.After, pagination.Limit+1)
+ if err != nil {
+ return domain.MessagePage{}, err
+ }
+ page := domain.MessagePage{Items: items, HasMore: len(items) > pagination.Limit}
+ if page.HasMore {
+ page.Items = page.Items[:pagination.Limit]
+ }
+ return page, nil
+}
+
+func (u *UseCase) chatForUser(ctx context.Context, chatID, callerID string) (domain.Chat, error) {
+ chat, err := u.repository.ChatForUser(ctx, chatID, callerID)
+ if errors.Is(err, ErrChatNotFound) || errors.Is(err, ErrNotMember) {
+ return domain.Chat{}, notFound()
+ }
+ return chat, err
+}
+
+func invalid(message string) error {
+ return domain.NewApplicationError(domain.ErrorInvalid, message)
+}
+
+func unauthorized() error {
+ return domain.NewApplicationError(domain.ErrorUnauthorized, "Требуется действительная сессия")
+}
+
+func notFound() error {
+ return domain.NewApplicationError(domain.ErrorNotFound, "Объект не найден")
+}
diff --git a/internal/usecase/chat/session.go b/internal/usecase/chat/session.go
new file mode 100644
index 00000000..07d16684
--- /dev/null
+++ b/internal/usecase/chat/session.go
@@ -0,0 +1,102 @@
+package chat
+
+import (
+ "context"
+ "crypto/rand"
+ "crypto/sha256"
+ "encoding/base64"
+ "encoding/hex"
+ "strings"
+ "time"
+ "unicode/utf8"
+
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
+ "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password"
+)
+
+type Sessions interface {
+ Put(context.Context, string, domain.Session) error
+ Get(context.Context, string) (domain.Session, bool, error)
+ Delete(context.Context, string) (bool, error)
+ Ready(context.Context) error
+}
+
+func validPassword(value string) bool {
+ n := utf8.RuneCountInString(value)
+ return utf8.ValidString(value) && n >= 12 && n <= 128 && strings.TrimSpace(value) != ""
+}
+
+func tokenKey(token string) string {
+ sum := sha256.Sum256([]byte(token))
+ return hex.EncodeToString(sum[:])
+}
+
+func (u *UseCase) Authenticate(ctx context.Context, token string) (domain.User, error) {
+ decoded, err := base64.RawURLEncoding.DecodeString(token)
+ if err != nil || len(decoded) != 32 {
+ return domain.User{}, unauthorized()
+ }
+ session, exists, err := u.sessions.Get(ctx, tokenKey(token))
+ if err != nil {
+ return domain.User{}, err
+ }
+ if !exists || !time.Now().Before(session.ExpiresAt) {
+ return domain.User{}, unauthorized()
+ }
+ user, exists, err := u.repository.User(ctx, session.UserID)
+ if err != nil {
+ return domain.User{}, err
+ }
+ if !exists {
+ return domain.User{}, unauthorized()
+ }
+ return user, nil
+}
+
+func (u *UseCase) CreateSession(ctx context.Context, username, secret string) (domain.Login, error) {
+ if !validUsername(username) || !validPassword(secret) {
+ return domain.Login{}, unauthorized()
+ }
+ user, hash, exists, err := u.repository.UserByUsername(ctx, username)
+ if err != nil {
+ return domain.Login{}, err
+ }
+ if !exists {
+ hash = password.DummyHash
+ }
+ valid, err := password.Verify(ctx, secret, hash)
+ if err != nil {
+ return domain.Login{}, err
+ }
+ if !valid || !exists {
+ return domain.Login{}, unauthorized()
+ }
+ var raw [32]byte
+ if _, err = rand.Read(raw[:]); err != nil {
+ return domain.Login{}, err
+ }
+ token := base64.RawURLEncoding.EncodeToString(raw[:])
+ expires := time.Now().UTC().Add(u.sessionTTL)
+ if err = u.sessions.Put(ctx, tokenKey(token), domain.Session{UserID: user.ID, ExpiresAt: expires}); err != nil {
+ return domain.Login{}, err
+ }
+ return domain.Login{Token: token, ExpiresAt: expires, User: user}, nil
+}
+
+func (u *UseCase) DeleteSession(ctx context.Context, token string) error {
+ deleted, err := u.sessions.Delete(ctx, tokenKey(token))
+ if err != nil {
+ return err
+ }
+ if !deleted {
+ return unauthorized()
+ }
+ return nil
+}
+
+func (u *UseCase) Ready(ctx context.Context) error {
+ if err := u.repository.Ready(ctx); err != nil {
+ return err
+ }
+ return u.sessions.Ready(ctx)
+}
diff --git a/internal/usecase/chat/session_test.go b/internal/usecase/chat/session_test.go
new file mode 100644
index 00000000..5096e1ba
--- /dev/null
+++ b/internal/usecase/chat/session_test.go
@@ -0,0 +1,19 @@
+package chat
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestPasswordContract(t *testing.T) {
+ for _, value := range []string{"", "too-short", strings.Repeat(" ", 12), strings.Repeat("x", 129), strings.Repeat("x", 12) + string([]byte{0xff})} {
+ if validPassword(value) {
+ t.Fatal("invalid password accepted")
+ }
+ }
+ for _, value := range []string{strings.Repeat("🔑", 128), "valid:password", " password with spaces "} {
+ if !validPassword(value) {
+ t.Fatal("valid Unicode password rejected")
+ }
+ }
+}
diff --git a/internal/usecase/chat/validation.go b/internal/usecase/chat/validation.go
new file mode 100644
index 00000000..5fe43df1
--- /dev/null
+++ b/internal/usecase/chat/validation.go
@@ -0,0 +1,37 @@
+package chat
+
+import (
+ "strings"
+ "unicode/utf8"
+
+ "github.com/google/uuid"
+)
+
+func normalizeUUID(value string) (string, bool) {
+ parsed, err := uuid.Parse(value)
+ if err != nil {
+ return "", false
+ }
+ normalized := parsed.String()
+ if !strings.EqualFold(value, normalized) {
+ return "", false
+ }
+ return normalized, true
+}
+
+func validUsername(value string) bool {
+ if len(value) < 3 || len(value) > 32 {
+ return false
+ }
+ for _, char := range value {
+ if char != '_' && (char < 'a' || char > 'z') && (char < '0' || char > '9') {
+ return false
+ }
+ }
+ return true
+}
+
+func validRequiredText(value string, maxRunes int) bool {
+ length := utf8.RuneCountInString(value)
+ return utf8.ValidString(value) && length >= 1 && length <= maxRunes && strings.TrimSpace(value) != ""
+}
diff --git a/lab.json b/lab.json
index db0c9c35..cc86272e 100644
--- a/lab.json
+++ b/lab.json
@@ -1,8 +1,8 @@
{
- "number": 5,
- "title": "Очереди и асинхронная обработка изображений",
- "contract_version": "1.5.0",
+ "number": 4,
+ "title": "S3-хранилище и вложения",
+ "contract_version": "1.4.0",
"default_base_url": "http://localhost:8080",
"grading": "3; 4 includes 3; 5 includes 4 plus one completed research track",
- "scaffold_has_application": false
+ "scaffold_has_application": true
}
diff --git a/migrations/001_initial.sql b/migrations/001_initial.sql
new file mode 100644
index 00000000..2433b274
--- /dev/null
+++ b/migrations/001_initial.sql
@@ -0,0 +1,54 @@
+-- +goose Up
+-- +goose StatementBegin
+DO $$
+BEGIN
+ -- Adopt the schema created by the previous runner without changing its data.
+ IF to_regclass('schema_migrations') IS NOT NULL THEN
+ IF NOT EXISTS (SELECT 1 FROM schema_migrations WHERE version = '001_initial.sql')
+ OR to_regclass('users') IS NULL OR to_regclass('chats') IS NULL
+ OR to_regclass('chat_members') IS NULL OR to_regclass('messages') IS NULL THEN
+ RAISE EXCEPTION 'incomplete legacy schema: cannot adopt migration 1';
+ END IF;
+ RETURN;
+ END IF;
+ CREATE TABLE users (
+ id uuid PRIMARY KEY,
+ username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
+ display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
+ password_hash text NOT NULL,
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp()
+ );
+ CREATE TABLE chats (
+ id uuid PRIMARY KEY,
+ title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp()
+ );
+ CREATE INDEX chats_history_idx ON chats (created_at, id);
+ CREATE TABLE chat_members (
+ chat_id uuid NOT NULL REFERENCES chats(id),
+ user_id uuid NOT NULL REFERENCES users(id),
+ ordinal integer NOT NULL CHECK (ordinal >= 0),
+ PRIMARY KEY (chat_id, user_id),
+ UNIQUE (chat_id, ordinal)
+ );
+ CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id);
+ CREATE TABLE messages (
+ id uuid PRIMARY KEY,
+ chat_id uuid NOT NULL,
+ sender_id uuid NOT NULL,
+ text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
+ );
+ CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id);
+END;
+$$;
+-- +goose StatementEnd
+
+-- +goose Down
+DROP TABLE messages;
+DROP TABLE chat_members;
+DROP TABLE chats;
+DROP TABLE users;
+-- The old journal must not claim that the dropped schema is still applied.
+DROP TABLE IF EXISTS schema_migrations;
diff --git a/migrations/002_idempotency.sql b/migrations/002_idempotency.sql
new file mode 100644
index 00000000..fb02f93c
--- /dev/null
+++ b/migrations/002_idempotency.sql
@@ -0,0 +1,10 @@
+-- +goose Up
+-- Additive: old lab2 writers can still insert NULL keys during migration.
+ALTER TABLE messages ADD COLUMN IF NOT EXISTS idempotency_key text
+ CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$');
+CREATE UNIQUE INDEX IF NOT EXISTS messages_idempotency_idx
+ ON messages(sender_id, chat_id, idempotency_key);
+
+-- +goose Down
+DROP INDEX messages_idempotency_idx;
+ALTER TABLE messages DROP COLUMN idempotency_key;
diff --git a/migrations/003_attachments.sql b/migrations/003_attachments.sql
new file mode 100644
index 00000000..a8b812ad
--- /dev/null
+++ b/migrations/003_attachments.sql
@@ -0,0 +1,20 @@
+-- +goose Up
+CREATE TABLE IF NOT EXISTS attachments (
+ id uuid PRIMARY KEY,
+ chat_id uuid NOT NULL REFERENCES chats(id),
+ uploader_id uuid NOT NULL REFERENCES users(id),
+ filename text NOT NULL,
+ object_key text NOT NULL UNIQUE,
+ content_type text NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
+ size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
+ sha256 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
+ width integer NOT NULL,
+ height integer NOT NULL,
+ status text NOT NULL DEFAULT 'ready' CHECK (status IN ('ready','failed')),
+ error_code text,
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ CHECK ((status='ready' AND error_code IS NULL) OR (status='failed' AND error_code IS NOT NULL))
+);
+
+-- +goose Down
+DROP TABLE IF EXISTS attachments;
diff --git a/migrations/004_message_attachments.sql b/migrations/004_message_attachments.sql
new file mode 100644
index 00000000..1fd41a98
--- /dev/null
+++ b/migrations/004_message_attachments.sql
@@ -0,0 +1,13 @@
+-- +goose Up
+ALTER TABLE messages
+ DROP CONSTRAINT IF EXISTS messages_text_check,
+ ADD CONSTRAINT messages_text_check CHECK (char_length(text) BETWEEN 0 AND 4000),
+ ADD COLUMN IF NOT EXISTS attachment_ids uuid[] NOT NULL DEFAULT '{}';
+
+-- +goose Down
+-- Attachment-only messages cannot exist in lab3; retain an explicit placeholder when downgrading.
+UPDATE messages SET text='[attachment]' WHERE text='';
+ALTER TABLE messages
+ DROP COLUMN IF EXISTS attachment_ids,
+ DROP CONSTRAINT IF EXISTS messages_text_check,
+ ADD CONSTRAINT messages_text_check CHECK (char_length(text) BETWEEN 1 AND 4000);
diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh
new file mode 100755
index 00000000..3a183673
--- /dev/null
+++ b/scripts/bootstrap.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+set -eu
+cd "$(dirname "$0")/.."
+umask 077
+mkdir -p secrets
+chmod 700 secrets
+for name in postgres_password cursor_key s3_root_secret_key s3_secret_key; do
+ if [ ! -s "secrets/$name" ]; then
+ openssl rand -hex 32 > "secrets/$name"
+ fi
+done
+for name in s3_root_access_key s3_access_key; do
+ if [ ! -s "secrets/$name" ]; then
+ openssl rand -hex 8 > "secrets/$name"
+ fi
+done
+# Compose mounts files directly on Linux; the unprivileged API must read them.
+# The host directory remains private (0700).
+chmod 444 secrets/postgres_password secrets/cursor_key secrets/s3_root_access_key secrets/s3_root_secret_key secrets/s3_access_key secrets/s3_secret_key
+printf '%s\n' 'Local secrets are ready.'
diff --git a/scripts/compose.sh b/scripts/compose.sh
new file mode 100755
index 00000000..078e4f0d
--- /dev/null
+++ b/scripts/compose.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+set -eu
+cd "$(dirname "$0")/.."
+if docker compose version >/dev/null 2>&1; then
+ exec docker compose --env-file /dev/null -f compose.yaml "$@"
+fi
+exec docker-compose --env-file /dev/null -f compose.yaml "$@"
diff --git a/scripts/measure_load.py b/scripts/measure_load.py
new file mode 100644
index 00000000..82481f07
--- /dev/null
+++ b/scripts/measure_load.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+"""Reproducible local benchmark with resource samples; no credentials in output."""
+import argparse
+import json
+import os
+from pathlib import Path
+import subprocess
+import sys
+import time
+
+ROOT = Path(__file__).resolve().parents[1]
+STACK = os.environ.get('STACK_NAME', 'mtusi-lab4')
+
+def containers(service=None):
+ label = 'com.docker.swarm.service.name='+STACK+'_'+service if service else 'com.docker.stack.namespace='+STACK
+ return subprocess.check_output(['docker','ps','--filter','label='+label,'-q'],text=True).split()
+
+
+def counts():
+ sql = 'SELECT json_build_object(\'users\',(SELECT count(*) FROM users),\'chats\',(SELECT count(*) FROM chats),\'messages\',(SELECT count(*) FROM messages))'
+ return json.loads(subprocess.check_output(['docker', 'exec', containers('postgres')[0], 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', sql], text=True))
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--base-url', default=os.environ.get('BASE_URL', 'http://localhost:8080'))
+ parser.add_argument('--duration', type=int, default=30)
+ parser.add_argument('--concurrency', type=int, default=4)
+ args = parser.parse_args()
+ command = [sys.executable, str(ROOT / 'tests/load.py'), '--base-url', args.base_url, '--concurrency', str(args.concurrency)]
+ subprocess.run(command + ['--duration', '3'], check=True, stdout=subprocess.DEVNULL)
+ before = counts()
+ ids = containers()
+ samples = []
+ process = subprocess.Popen(command + ['--duration', str(args.duration), '--output', str(ROOT / 'evidence/swarm-load.json')], stdout=subprocess.DEVNULL)
+ try:
+ while process.poll() is None:
+ stats = subprocess.check_output(['docker', 'stats', '--no-stream', '--format', '{{json .}}', *ids], text=True)
+ samples.append({'elapsed_sample': len(samples), 'containers': [json.loads(line) for line in stats.splitlines()]})
+ time.sleep(1)
+ finally:
+ if process.poll() is None:
+ process.terminate()
+ code = process.wait()
+ if code:
+ raise SystemExit(code)
+ report = {'warmup_seconds': 3, 'dataset_before': before, 'dataset_after': counts(), 'samples': samples}
+ (ROOT / 'evidence/swarm-resources.json').write_text(json.dumps(report, indent=2) + '\n')
+ print((ROOT / 'evidence/swarm-load.json').read_text())
+ print('Resource samples and dataset counts: evidence/swarm-resources.json')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/scripts/measure_uploads.py b/scripts/measure_uploads.py
new file mode 100644
index 00000000..7a0de357
--- /dev/null
+++ b/scripts/measure_uploads.py
@@ -0,0 +1,85 @@
+#!/usr/bin/env python3
+"""Ten simultaneous proxy uploads, checksums, latency and cgroup memory samples."""
+from concurrent.futures import ThreadPoolExecutor
+import hashlib
+import json
+import math
+from pathlib import Path
+import sys
+import threading
+import time
+
+ROOT = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(ROOT/'tests/own'))
+from helpers import Client, check, containers, docker, png, save, upload
+
+
+def memory(ids):
+ result = {}
+ for container in ids:
+ raw = docker('exec',container,'cat','/sys/fs/cgroup/memory.current').stdout
+ result[container] = int(raw)
+ return result
+
+
+def percentile(values, p):
+ return sorted(values)[max(0, math.ceil(len(values)*p)-1)]
+
+
+def kernel_peak(ids):
+ return {container:int(docker('exec',container,'cat','/sys/fs/cgroup/memory.peak').stdout)
+ for container in ids}
+
+
+def main():
+ c = Client(timeout=55)
+ alice,bob,_,chat = c.bootstrap()
+ upload(c,alice,chat,png())
+ ids = containers('api')
+ results = {'concurrency':10, 'warmup_uploads':1, 'memory_method':'cgroup v2 memory.current sampled every >=100ms; values include file cache', 'runs':[]}
+ for width,height in ((512,512),(1024,768)):
+ content = png(width,height,noisy=True)
+ sha = hashlib.sha256(content).hexdigest()
+ baseline = memory(ids)
+ baseline_kernel_peak = kernel_peak(ids)
+ samples = []
+ barrier = threading.Barrier(11)
+ def worker(number):
+ local = Client(timeout=55)
+ barrier.wait()
+ return upload(local,alice,chat,content,filename='concurrent-'+str(number)+'.png')
+ started = time.monotonic()
+ with ThreadPoolExecutor(max_workers=10) as pool:
+ futures = [pool.submit(worker,n) for n in range(10)]
+ barrier.wait()
+ while not all(f.done() for f in futures):
+ samples.append({'seconds':round(time.monotonic()-started,3), 'bytes':memory(ids)})
+ time.sleep(.1)
+ responses = [f.result() for f in futures]
+ elapsed = time.monotonic()-started
+ samples.append({'seconds':round(elapsed,3), 'bytes':memory(ids)})
+ for response in responses:
+ att = response['json']
+ check(att['sha256']==sha,'concurrent upload checksum mismatch')
+ downloaded = c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=bob)
+ check(hashlib.sha256(downloaded['bytes']).hexdigest()==sha,'concurrent downloaded bytes changed')
+ latency = [r['elapsed']*1000 for r in responses]
+ peak = {container:max(sample['bytes'][container] for sample in samples) for container in ids}
+ run = {'width':width, 'height':height, 'size_bytes':len(content), 'sha256':sha,
+ 'requests':10, 'successful':len(responses), 'errors':0, 'duration_seconds':round(elapsed,3),
+ 'successful_rps':round(len(responses)/elapsed,3),
+ 'latency_ms':{str(p):round(percentile(latency,p),3) for p in (.5,.95,.99)},
+ 'baseline_bytes':baseline, 'sampled_peak_bytes':peak,
+ 'kernel_lifetime_peak_before_bytes':baseline_kernel_peak,
+ 'kernel_lifetime_peak_after_bytes':kernel_peak(ids),
+ 'sampled_peak_sum_bytes':max(sum(sample['bytes'].values()) for sample in samples),
+ 'samples':samples}
+ check(all(value<1536*1024*1024 for value in peak.values()),'API exceeded memory limit')
+ results['runs'].append(run)
+ print(json.dumps({k:v for k,v in run.items() if k!='samples'}),flush=True)
+ save('lab4-upload-memory.json',results)
+ print('PASS: ten simultaneous uploads at two sizes; all originals verified; memory samples saved.')
+
+
+if __name__=='__main__':
+ main()
diff --git a/scripts/restart.sh b/scripts/restart.sh
new file mode 100755
index 00000000..211f35b4
--- /dev/null
+++ b/scripts/restart.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+set -eu
+cd "$(dirname "$0")/.."
+exec python3 scripts/swarm.py restart
diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh
new file mode 100755
index 00000000..8a93f7f0
--- /dev/null
+++ b/scripts/stop-one.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+set -eu
+cd "$(dirname "$0")/.."
+exec python3 scripts/swarm.py stop-one
diff --git a/scripts/swarm.py b/scripts/swarm.py
new file mode 100644
index 00000000..a40d801f
--- /dev/null
+++ b/scripts/swarm.py
@@ -0,0 +1,269 @@
+#!/usr/bin/env python3
+"""Single-node Swarm lab lifecycle; never deletes volumes or prints credentials."""
+import json
+import os
+from pathlib import Path
+import subprocess
+import sys
+import time
+import urllib.request
+
+ROOT = Path(__file__).resolve().parents[1]
+os.chdir(ROOT)
+STACK = os.environ.get('STACK_NAME', 'mtusi-lab4')
+API = os.environ.get('API_IMAGE', 'mtusi-messenger:lab4')
+MIGRATIONS = os.environ.get('MIGRATIONS_IMAGE', 'mtusi-migrations:lab4')
+TESTS = os.environ.get('TESTS_IMAGE', 'mtusi-messenger-tests:lab4')
+S3 = os.environ.get('S3_IMAGE', 'mtusi-minio:RELEASE.2025-09-07T16-13-09Z')
+MC = os.environ.get('S3_CLIENT_IMAGE', 'mtusi-mc:RELEASE.2025-08-13T08-35-41Z')
+PORT = os.environ.get('PORT', '8080')
+SECRETS = ('postgres_password', 'cursor_key', 's3_root_access_key',
+ 's3_root_secret_key', 's3_access_key', 's3_secret_key')
+
+
+def docker(*args, capture=False, **kwargs):
+ return subprocess.run(['docker', *args], check=True, text=True,
+ stdout=subprocess.PIPE if capture else None, **kwargs).stdout
+
+
+def inspect(kind, name):
+ return json.loads(docker(kind, 'inspect', name, capture=True))[0]
+
+
+def tasks(service):
+ ids = docker('service', 'ps', '--filter', 'desired-state=running', '-q', STACK+'_'+service, capture=True).split()
+ return [json.loads(docker('inspect', task, capture=True))[0] for task in ids]
+
+
+def wait_service(service, count=1, timeout=120):
+ deadline = time.monotonic() + timeout
+ while time.monotonic() < deadline:
+ active = tasks(service)
+ if len(active) == count and all(t['Status']['State'] == 'running' for t in active):
+ # This lab pins storage to this manager and runs on one Docker engine.
+ healthy = True
+ for task in active:
+ container = task['Status'].get('ContainerStatus', {}).get('ContainerID')
+ state = json.loads(docker('inspect', container, capture=True))[0]['State']
+ healthy &= state.get('Health', {}).get('Status', 'healthy') == 'healthy'
+ if healthy:
+ return
+ time.sleep(1)
+ raise RuntimeError('service did not become healthy: '+service)
+
+
+def wait_api():
+ wait_service('api', int(os.environ.get('API_REPLICAS', '2')))
+ deadline = time.monotonic()+45
+ while time.monotonic() < deadline:
+ try:
+ with urllib.request.urlopen('http://localhost:'+PORT+'/health/ready', timeout=3) as response:
+ if response.status == 200:
+ return
+ except OSError:
+ pass
+ time.sleep(.5)
+ raise RuntimeError('LB readiness timeout')
+
+
+def job(kind, image, command, integration=False):
+ name = STACK+'_'+kind
+ # Fixed service name is a deployment lock: concurrent runners fail to create it.
+ args = ['service', 'create', '--detach', '--no-resolve-image', '--name', name, '--mode', 'replicated-job',
+ '--replicas', '1', '--restart-condition', 'none', '--network', STACK+'_data',
+ '--constraint', 'node.labels.mtusi_lab4_storage==true',
+ '--limit-cpu', '1', '--limit-memory', '1g' if integration else '128m',
+ '--secret', 'source='+STACK+'_postgres_password,target=postgres_password',
+ '--env', 'POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password']
+ if integration:
+ for value in ['INTEGRATION_TESTS=1', 'POSTGRES_HOST=postgres', 'POSTGRES_USER=messenger',
+ 'POSTGRES_DB=messenger', 'REDIS_ADDR=redis:6379', 'CURSOR_KEY_FILE=/run/secrets/cursor_key',
+ 'S3_ENDPOINT=minio:9000', 'S3_BUCKET=attachments',
+ 'S3_ACCESS_KEY_FILE=/run/secrets/s3_access_key', 'S3_SECRET_KEY_FILE=/run/secrets/s3_secret_key']:
+ args += ['--env', value]
+ args += ['--secret', 'source='+STACK+'_cursor_key,target=cursor_key']
+ for secret in ('s3_access_key', 's3_secret_key'):
+ args += ['--secret', 'source='+STACK+'_'+secret+',target='+secret]
+ else:
+ args += ['--env', 'GOOSE_DRIVER=postgres', '--env',
+ 'GOOSE_DBSTRING=host=postgres user=messenger dbname=messenger sslmode=disable connect_timeout=5']
+ docker(*args, image, *command)
+ wait_job(name, kind, 300 if integration else 90)
+
+
+def wait_job(name, kind, timeout=90):
+ try:
+ deadline = time.monotonic()+timeout
+ while time.monotonic() < deadline:
+ ids = docker('service', 'ps', '-q', name, capture=True).split()
+ states = [json.loads(docker('inspect', i, capture=True))[0]['Status']['State'] for i in ids]
+ if 'complete' in states:
+ docker('service', 'logs', name)
+ return
+ if any(s in ('failed', 'rejected') for s in states):
+ docker('service', 'logs', name)
+ raise RuntimeError(kind+' failed')
+ time.sleep(1)
+ raise RuntimeError(kind+' timed out')
+ finally:
+ docker('service', 'rm', name)
+
+
+def bootstrap_s3():
+ name = STACK+'_s3-bootstrap'
+ args = ['service', 'create', '--detach', '--no-resolve-image', '--name', name, '--mode', 'replicated-job',
+ '--replicas', '1', '--restart-condition', 'none', '--network', STACK+'_data',
+ '--constraint', 'node.labels.mtusi_lab4_storage==true',
+ '--limit-cpu', '0.5', '--limit-memory', '512m', '--entrypoint', '/bin/sh',
+ '--config', 'source='+STACK+'_s3_bootstrap,target=/bootstrap.sh',
+ '--config', 'source='+STACK+'_s3_policy,target=/policy.json']
+ for secret in SECRETS[2:]:
+ args += ['--secret', 'source='+STACK+'_'+secret+',target='+secret]
+ docker(*args, MC, '/bootstrap.sh')
+ wait_job(name, 'S3 bootstrap')
+
+
+def reconcile():
+ active = tasks('api')
+ if not active:
+ raise RuntimeError('no API task available for reconciliation')
+ container = active[0]['Status']['ContainerStatus']['ContainerID']
+ docker('exec', container, '/usr/local/bin/reconcile', *sys.argv[2:])
+
+
+def build():
+ docker('build', '-f', 'Dockerfile.storage', '--target', 'minio', '-t', S3, '.')
+ docker('build', '-f', 'Dockerfile.storage', '--target', 'mc', '-t', MC, '.')
+ docker('build', '-t', API, '.')
+ docker('build', '--target', 'migrations', '-t', MIGRATIONS, '.')
+
+
+def up():
+ swarm = json.loads(docker('info', '--format', '{{json .Swarm}}', capture=True))
+ if swarm['LocalNodeState'] == 'inactive':
+ docker('swarm', 'init', capture=True) # join token stays out of logs
+ swarm = json.loads(docker('info', '--format', '{{json .Swarm}}', capture=True))
+ if not swarm['ControlAvailable']:
+ raise RuntimeError('run on the Swarm manager')
+ if len(docker('node', 'ls', '-q', capture=True).split()) != 1:
+ raise RuntimeError('automatic lifecycle supports one Swarm node; see REPORT.md for image distribution')
+ docker('node', 'update', '--label-add', 'mtusi_lab4_storage=true', swarm['NodeID'])
+ subprocess.run(['./scripts/bootstrap.sh'], check=True)
+ for secret in SECRETS:
+ known = docker('secret', 'ls', '--format', '{{.Name}}', capture=True).split()
+ if STACK+'_'+secret not in known:
+ docker('secret', 'create', STACK+'_'+secret, 'secrets/'+secret)
+ build()
+ existing = docker('service', 'ls', '--format', '{{.Name}}', capture=True).split()
+ # Upgrade the shared schema before replacing any running API image.
+ fresh = STACK+'_api' not in existing
+ if not fresh:
+ job('migrations', MIGRATIONS, ['up'])
+ # Initial deployment keeps API at zero until the one-shot migration succeeds.
+ env = dict(os.environ)
+ env['STACK_NAME'] = STACK
+ if fresh:
+ env['API_REPLICAS'] = '0'
+ docker('stack', 'deploy', '--resolve-image', 'never', '-c', 'stack.yaml', STACK, env=env)
+ wait_service('postgres')
+ wait_service('redis')
+ wait_service('minio')
+ bootstrap_s3()
+ if fresh:
+ job('migrations', MIGRATIONS, ['up'])
+ docker('service', 'scale', '--detach', STACK+'_api='+os.environ.get('API_REPLICAS', '2'))
+ if fresh:
+ wait_api()
+ else:
+ update()
+ print('PASS: Swarm stack ready at http://localhost:'+PORT)
+
+
+def restart():
+ # Force replaces every task while retaining the same Swarm secrets and volumes.
+ services = ('postgres', 'redis', 'minio', 'api', 'lb')
+ previous = {name: {t['ID'] for t in tasks(name)} for name in services}
+ for service in services:
+ docker('service', 'update', '--detach', '--force', STACK+'_'+service)
+ deadline = time.monotonic()+75
+ while any(previous[name].intersection(t['ID'] for t in tasks(name)) for name in services):
+ if time.monotonic() > deadline:
+ raise RuntimeError('restart did not replace every original task')
+ time.sleep(1)
+ wait_service('lb')
+ wait_service('postgres')
+ wait_service('redis')
+ wait_service('minio')
+ wait_api()
+
+
+def stop_one():
+ target = os.environ['TARGET_INSTANCE_ID']
+ matches = [t for t in tasks('api') if t['ID'] == target]
+ if len(matches) != 1:
+ raise RuntimeError('TARGET_INSTANCE_ID is not a running API task in this stack')
+ task = matches[0]
+ container = task['Status']['ContainerStatus']['ContainerID']
+ print('Stopping task '+target+' container '+container, flush=True)
+ docker('kill', container) # Swarm replaces it with a different task/instance ID.
+
+
+def update():
+ previous = {t['ID'] for t in tasks('api')}
+ docker('service', 'update', '--detach', '--force', '--no-resolve-image', '--image', API, STACK+'_api')
+ deadline = time.monotonic()+180
+ while time.monotonic() < deadline:
+ state = inspect('service', STACK+'_api').get('UpdateStatus', {}).get('State')
+ if state == 'completed' and previous.isdisjoint(t['ID'] for t in tasks('api')):
+ wait_api()
+ print('PASS: rolling update completed')
+ return
+ if state in ('paused', 'rollback_started', 'rollback_paused', 'rollback_completed'):
+ raise RuntimeError('rolling update: '+state)
+ time.sleep(1)
+ raise RuntimeError('rolling update timed out')
+
+
+def status():
+ result = {'stack': STACK, 'docker': docker('version','--format','{{.Server.Version}}',capture=True).strip(), 'services': {}}
+ for name in ('lb','api','postgres','redis','minio'):
+ service = inspect('service', STACK+'_'+name)
+ spec = service['Spec']
+ template = spec['TaskTemplate']
+ result['services'][name] = {
+ 'image': template['ContainerSpec']['Image'],
+ 'limits': template.get('Resources',{}).get('Limits',{}),
+ 'published_ports': spec.get('EndpointSpec',{}).get('Ports',[]),
+ 'secret_names': [x['SecretName'] for x in template['ContainerSpec'].get('Secrets',[])],
+ 'mounts': template['ContainerSpec'].get('Mounts',[]),
+ 'tasks': [{'id':t['ID'],'state':t['Status']['State'],
+ 'container':t['Status'].get('ContainerStatus',{}).get('ContainerID')}
+ for t in tasks(name)]}
+ print(json.dumps(result,indent=2))
+
+def main():
+ action = sys.argv[1]
+ if action == 'status': status()
+ elif action == 'up': up()
+ elif action == 'build': build()
+ elif action == 'bootstrap-s3': bootstrap_s3()
+ elif action == 'reconcile': reconcile()
+ elif action == 'wait': wait_api()
+ elif action == 'restart': restart()
+ elif action == 'stop-one': stop_one()
+ elif action == 'update': update()
+ elif action == 'migrate': job('migrations', MIGRATIONS, ['up'])
+ elif action == 'migrate-status': job('migrations', MIGRATIONS, ['status'])
+ elif action == 'test-integration':
+ docker('build', '--target', 'tests', '-t', TESTS, '.')
+ job('tests', TESTS, [], integration=True)
+ elif action == 'down': docker('stack', 'rm', STACK)
+ else: raise RuntimeError('unknown action: '+action)
+
+
+if __name__ == '__main__':
+ try:
+ main()
+ except (RuntimeError, subprocess.CalledProcessError) as error:
+ print('FAIL: '+str(error), file=sys.stderr)
+ sys.exit(1)
diff --git a/stack.yaml b/stack.yaml
new file mode 100644
index 00000000..da599861
--- /dev/null
+++ b/stack.yaml
@@ -0,0 +1,164 @@
+version: '3.8'
+services:
+ postgres:
+ image: postgres:17.6-alpine3.22
+ environment:
+ POSTGRES_USER: messenger
+ POSTGRES_DB: messenger
+ POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
+ secrets: [postgres_password]
+ volumes: [postgres_data:/var/lib/postgresql/data]
+ networks: [data]
+ healthcheck:
+ test: [CMD-SHELL, 'pg_isready -U messenger -d messenger']
+ interval: 2s
+ timeout: 2s
+ retries: 15
+ stop_grace_period: 20s
+ deploy:
+ replicas: 1
+ placement:
+ constraints: [node.labels.mtusi_lab4_storage == true]
+ resources:
+ limits: {cpus: '1.0', memory: 512M}
+ update_config: {parallelism: 1, order: stop-first}
+ redis:
+ image: redis:7.4.5-alpine3.21
+ command: [redis-server, /usr/local/etc/redis/redis.conf]
+ configs:
+ - source: redis
+ target: /usr/local/etc/redis/redis.conf
+ volumes: [redis_data:/data]
+ networks: [data]
+ healthcheck:
+ test: [CMD, redis-cli, ping]
+ interval: 2s
+ timeout: 1s
+ retries: 15
+ stop_grace_period: 20s
+ deploy:
+ replicas: 1
+ placement:
+ constraints: [node.labels.mtusi_lab4_storage == true]
+ resources:
+ limits: {cpus: '0.5', memory: 128M}
+ update_config: {parallelism: 1, order: stop-first}
+ minio:
+ image: ${S3_IMAGE:-mtusi-minio:RELEASE.2025-09-07T16-13-09Z}
+ secrets: [s3_root_access_key, s3_root_secret_key]
+ volumes: [s3_data:/data]
+ networks: [data]
+ healthcheck:
+ test: [CMD, wget, -q, -T, '2', -O, /dev/null, 'http://127.0.0.1:9000/minio/health/live']
+ interval: 2s
+ timeout: 3s
+ start_period: 15s
+ retries: 15
+ stop_grace_period: 20s
+ deploy:
+ replicas: 1
+ placement:
+ constraints: [node.labels.mtusi_lab4_storage == true]
+ resources:
+ limits: {cpus: '1.0', memory: 512M}
+ update_config: {parallelism: 1, order: stop-first}
+ api:
+ image: ${API_IMAGE:-mtusi-messenger:lab4}
+ environment:
+ INSTANCE_ID: '{{.Task.ID}}'
+ POSTGRES_HOST: postgres
+ POSTGRES_USER: messenger
+ POSTGRES_DB: messenger
+ POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
+ CURSOR_KEY_FILE: /run/secrets/cursor_key
+ POSTGRES_MAX_CONNS: '10'
+ REDIS_ADDR: redis:6379
+ SESSION_TTL_SECONDS: '3600'
+ QUERY_TIMEOUT_MS: '1000'
+ S3_ENDPOINT: minio:9000
+ S3_BUCKET: attachments
+ S3_ACCESS_KEY_FILE: /run/secrets/s3_access_key
+ S3_SECRET_KEY_FILE: /run/secrets/s3_secret_key
+ S3_USE_SSL: 'false'
+ GOMEMLIMIT: 1152MiB
+ GOGC: '50'
+ secrets: [postgres_password, cursor_key, s3_access_key, s3_secret_key]
+ networks: [backend, data]
+ stop_grace_period: 15s
+ healthcheck:
+ test: [CMD, wget, -q, -T, '2', -O, /dev/null, 'http://127.0.0.1:8080/health/ready']
+ interval: 2s
+ timeout: 3s
+ start_period: 10s
+ retries: 3
+ deploy:
+ replicas: ${API_REPLICAS:-2}
+ endpoint_mode: dnsrr
+ resources:
+ limits: {cpus: '1.0', memory: 1536M}
+ restart_policy: {condition: any, delay: 1s}
+ update_config:
+ parallelism: 1
+ order: start-first
+ delay: 5s
+ monitor: 15s
+ failure_action: rollback
+ rollback_config: {parallelism: 1, order: start-first}
+ lb:
+ image: haproxy:3.2.6-alpine
+ configs:
+ - source: haproxy
+ target: /usr/local/etc/haproxy/haproxy.cfg
+ networks: [backend]
+ ports:
+ - target: 8080
+ published: ${PORT:-8080}
+ protocol: tcp
+ mode: host
+ healthcheck:
+ test: [CMD, haproxy, -c, -f, /usr/local/etc/haproxy/haproxy.cfg]
+ interval: 5s
+ timeout: 2s
+ retries: 3
+ deploy:
+ replicas: 1
+ resources:
+ limits: {cpus: '0.5', memory: 128M}
+ update_config: {parallelism: 1, order: stop-first}
+networks:
+ backend:
+ driver: overlay
+ data:
+ driver: overlay
+volumes:
+ postgres_data:
+ redis_data:
+ s3_data:
+configs:
+ haproxy:
+ file: ./infra/haproxy.cfg
+ redis:
+ file: ./infra/redis.conf
+ s3_bootstrap:
+ file: ./infra/s3-bootstrap.sh
+ s3_policy:
+ file: ./infra/s3-policy.json
+secrets:
+ postgres_password:
+ external: true
+ name: ${STACK_NAME:-mtusi-lab4}_postgres_password
+ cursor_key:
+ external: true
+ name: ${STACK_NAME:-mtusi-lab4}_cursor_key
+ s3_root_access_key:
+ external: true
+ name: ${STACK_NAME:-mtusi-lab4}_s3_root_access_key
+ s3_root_secret_key:
+ external: true
+ name: ${STACK_NAME:-mtusi-lab4}_s3_root_secret_key
+ s3_access_key:
+ external: true
+ name: ${STACK_NAME:-mtusi-lab4}_s3_access_key
+ s3_secret_key:
+ external: true
+ name: ${STACK_NAME:-mtusi-lab4}_s3_secret_key
diff --git a/tests/own/README.md b/tests/own/README.md
new file mode 100644
index 00000000..b446401a
--- /dev/null
+++ b/tests/own/README.md
@@ -0,0 +1,20 @@
+# Собственные проверки lab4
+
+`make test-lab4` проверяет реальные PNG/JPEG, лимиты, MIME, повреждения, права, приватность bucket и отказы.
+Тест останавливает именно API-реплику, которая приняла файл, и проверяет скачивание через другую.
+`make measure-uploads` запускает десять клиентов на двух размерах PNG и сохраняет замеры cgroup memory.
+`make test-reconcile` внедряет ошибки S3/БД и проверяет grace, блокировки, dry-run, повторное исправление и стоимость обхода.
+Эти сценарии используют реальный MinIO. Запускайте их последовательно на учебном Swarm-стенде.
+
+Результаты находятся в `evidence/lab4-*`. Подробная методика приведена в [REPORT.md](../../REPORT.md).
+
+Публичные tests/*.py и обязательный контракт сохранены.
+
+- `make test-own`: Go tests с race detector. Без INTEGRATION_TESTS=1 PostgreSQL-сценарий пропускается.
+- `make test-integration`: отдельный Swarm job, Go 1.26.4 и goose. Временная схема, миграции с нуля/повторно/legacy/reset/up, регистрационная гонка, rollback, 10 000 сообщений с одинаковыми timestamps, EXPLAIN и 16 конкурентных запросов одного ключа.
+- `make test-grade4`: `swarm_grade4.py`, ровно две API tasks. Проверяет общий ключ через LB, одинаковый результат, 409, границы ключа, область пользователя/чата, права и logout. По очереди останавливает обе исходные tasks с восстановлением, затем выполняет rolling update под нагрузкой. Проверяет историю, отсутствие дублей и повтор исходного ключа после воздействия. Сохраняет `evidence/swarm-grade4.json`.
+- `make test-persistence`: перезапуск всех API, PostgreSQL, Redis, MinIO и LB; volumes/secrets сохраняются.
+- `make test-failover ACTION_SCRIPT=scripts/stop-one.sh`: публичный сценарий отказа; `make wait` дожидается восстановления двух реплик.
+- `make measure-load`: 3s прогрева + 30s, concurrency 4, метрики и Docker stats в `evidence/swarm-*`.
+
+Сценарии отказов, persistence и rolling update выполняйте **последовательно**, на локальном учебном стенде. Все данные синтетические; пароль и токены в результаты не записываются. Старый `grade4.py` относится к Compose-стенду lab2 и не является entrypoint текущей работы.
diff --git a/tests/own/grade4.py b/tests/own/grade4.py
new file mode 100644
index 00000000..60f89b87
--- /dev/null
+++ b/tests/own/grade4.py
@@ -0,0 +1,120 @@
+#!/usr/bin/env python3
+"""Additional lab2 acceptance checks. Never print credentials or session tokens."""
+import base64
+from concurrent.futures import ThreadPoolExecutor
+import os
+from pathlib import Path
+import subprocess
+import sys
+import time
+import uuid
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+from client import Client, Failure, check, run
+
+ROOT = Path(__file__).resolve().parents[2]
+
+
+def compose(*args, ttl=None):
+ env = dict(os.environ)
+ if ttl is not None:
+ env['SESSION_TTL_SECONDS'] = str(ttl)
+ result = subprocess.run([str(ROOT / 'scripts/compose.sh'), *args], env=env,
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=180)
+ check(result.returncode == 0, 'compose operation failed: ' + args[0])
+
+
+def login(c, username, password, expected=201):
+ basic = base64.b64encode((username + ':' + password).encode()).decode()
+ return c.call('POST', '/api/v1/auth/sessions', expected,
+ headers={'Authorization': 'Basic ' + basic}, schema='Session' if expected == 201 else None)
+
+
+def main():
+ c = Client(timeout=5)
+ alice, bob, eve, chat = c.bootstrap()
+ credentials = alice['registration']
+ login(c, credentials['username'], 'incorrect-password', 401)
+ login(c, 'unknown_' + uuid.uuid4().hex[:12], 'incorrect-password', 401)
+ c.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': alice['id']})
+ basic = base64.b64encode((credentials['username'] + ':' + credentials['password']).encode()).decode()
+ c.call('GET', '/api/v1/users/me', 401, headers={'Authorization': 'Basic ' + basic})
+ response = c.call('GET', '/api/v1/users/me', actor=alice, headers={'X-User-Id': bob['id']}, schema='User')
+ check(response['json']['id'] == alice['id'], 'X-User-Id changed session owner')
+ c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
+ c.call('GET', '/api/v1/users/me', 401, actor=alice)
+ c.call('DELETE', '/api/v1/auth/sessions/current', 401, actor=alice)
+ print('PASS: wrong password, unknown user, Bearer-only access, X-User-Id ignored, logout')
+
+ # Maximum UTF-8 password length and a colon are supported without truncation.
+ username = 'unicode_' + uuid.uuid4().hex[:12]
+ password = '🔑' * 127 + ':'
+ c.call('POST', '/api/v1/users', 201, body={'username': username, 'display_name': 'Unicode', 'password': password}, schema='User')
+ login(c, username, password)
+ login(c, username, '🔑' * 127 + 'x', 401)
+ print('PASS: 128-code-point Unicode password including colon; suffix verified')
+
+ username = 'race_' + uuid.uuid4().hex[:12]
+ def register(_):
+ return Client().call('POST', '/api/v1/users', [201, 409], body={
+ 'username': username, 'display_name': 'Concurrent', 'password': 'synthetic-password-123'
+ })['status']
+ with ThreadPoolExecutor(max_workers=8) as pool:
+ statuses = list(pool.map(register, range(8)))
+ check(statuses.count(201) == 1 and statuses.count(409) == 7, 'registration race broken')
+ print('PASS: HTTP registration race: one 201, seven 409')
+
+ # Revoke before restart: absence must also survive AOF recovery.
+ for service in ('redis', 'postgres'):
+ compose('stop', service)
+ try:
+ c.call('GET', '/health/live', schema='Health')
+ start = time.monotonic()
+ c.call('GET', '/health/ready', 503)
+ check(time.monotonic() - start < 4, 'readiness error exceeded timeout budget')
+ c.call('GET', '/api/v1/users/me', 503, actor=bob)
+ c.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': bob['id']})
+ login(c, bob['registration']['username'], bob['registration']['password'], 503)
+ c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 503, actor=bob, body={'text': 'must fail'})
+ finally:
+ compose('up', '-d', '--wait', service)
+ c.call('GET', '/health/ready', schema='Health')
+ c.call('GET', '/api/v1/users/me', actor=bob, schema='User')
+ c.call('GET', '/api/v1/users/me', 401, actor=alice)
+ print('PASS: ' + service + ' outage bounded; live=200, ready=503, protected=503, anonymous=401; session recovered, logout retained')
+
+ # The same signed cursor remains valid across an API process restart.
+ c.message(bob, chat, 'cursor before restart 1')
+ c.message(bob, chat, 'cursor before restart 2')
+ page = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=1', actor=bob, schema='MessagePage')['json']
+ check(page['next_cursor'] is not None, 'missing cursor')
+ compose('restart', 'api')
+ deadline = time.monotonic() + 30
+ while True:
+ try:
+ c.call('GET', '/health/ready', schema='Health')
+ break
+ except Failure:
+ check(time.monotonic() < deadline, 'API did not recover')
+ time.sleep(.2)
+ from urllib.parse import quote
+ c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?cursor=' + quote(page['next_cursor']), actor=bob, schema='MessagePage')
+ print('PASS: cursor and existing session survive API restart')
+
+ try:
+ compose('up', '-d', '--wait', '--force-recreate', 'api', ttl=2)
+ short = c.user('expiry')
+ c.call('GET', '/api/v1/users/me', actor=short, schema='User')
+ time.sleep(2.2)
+ c.call('GET', '/api/v1/users/me', 401, actor=short)
+ compose('restart', 'redis')
+ compose('up', '-d', '--wait', 'redis')
+ c.call('GET', '/api/v1/users/me', 401, actor=short)
+ print('PASS: SESSION_TTL_SECONDS=2 expires; Redis restart does not revive expired session')
+ finally:
+ compose('up', '-d', '--wait', '--force-recreate', 'api', ttl=3600)
+ print('PASS: all grade-4 HTTP checks; normal TTL restored to 3600')
+
+
+if __name__ == '__main__':
+ run(main)
diff --git a/tests/own/helpers.py b/tests/own/helpers.py
new file mode 100644
index 00000000..ca4dcfb3
--- /dev/null
+++ b/tests/own/helpers.py
@@ -0,0 +1,116 @@
+"""Local lab4 experiment helpers. Never print credentials or session tokens."""
+import json
+import os
+from pathlib import Path
+import random
+import struct
+import subprocess
+import sys
+import time
+import uuid
+import zlib
+
+ROOT = Path(__file__).resolve().parents[2]
+sys.path.insert(0, str(ROOT / 'tests'))
+from client import Client, check
+
+STACK = os.environ.get('STACK_NAME', 'mtusi-lab4')
+MC = os.environ.get('S3_CLIENT_IMAGE', 'mtusi-mc:RELEASE.2025-08-13T08-35-41Z')
+
+
+def docker(*args, data=None, check_result=True):
+ return subprocess.run(['docker', *args], input=data, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE, check=check_result)
+
+
+def containers(service):
+ ids = docker('ps', '--filter', 'label=com.docker.swarm.service.name='+STACK+'_'+service,
+ '--format', '{{.ID}}').stdout.decode().split()
+ check(bool(ids), 'no container for '+service)
+ return ids
+
+
+def sql(statement):
+ result = docker('exec', '-i', containers('postgres')[0], 'psql', '-U', 'messenger',
+ '-d', 'messenger', '-At', '-v', 'ON_ERROR_STOP=1', data=statement.encode())
+ return result.stdout.decode().strip()
+
+
+def reconcile(apply=False, grace='90s'):
+ args = ['exec', containers('api')[0], '/usr/local/bin/reconcile', '--grace='+grace]
+ if apply:
+ args.append('--apply')
+ return json.loads(docker(*args).stdout)
+
+
+def upload(c, actor, chat, content, filename='sample.png', mime='image/png', expected=201):
+ from urllib.parse import quote
+ return c.call('POST', '/api/v1/chats/'+chat['id']+'/attachments?filename='+quote(filename, safe=''),
+ expected, actor=actor, raw=content, headers={'Content-Type': mime},
+ schema='Attachment' if expected == 201 else None)
+
+
+def png(width=320, height=200, noisy=False):
+ def chunk(kind, body):
+ return struct.pack('>I', len(body))+kind+body+struct.pack('>I', zlib.crc32(kind+body))
+ rng = random.Random(419)
+ lines = b''.join(b'\0'+(rng.randbytes(width*3) if noisy else b'\x18\x60\xc0'*width)
+ for _ in range(height))
+ return (b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR', struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0))
+ +chunk(b'IDAT', zlib.compress(lines))+chunk(b'IEND', b''))
+
+
+def oversized_dimensions():
+ body = struct.pack('>IIBBBBB', 10000, 4001, 8, 2, 0, 0, 0)
+ return b'\x89PNG\r\n\x1a\n'+struct.pack('>I', len(body))+b'IHDR'+body+struct.pack('>I', zlib.crc32(b'IHDR'+body))
+
+
+def save(name, result):
+ (ROOT / 'evidence').mkdir(exist_ok=True)
+ (ROOT / 'evidence' / name).write_text(json.dumps(result, indent=2, ensure_ascii=False)+'\n')
+
+
+def wait_ready(timeout=90):
+ deadline = time.monotonic()+timeout
+ while time.monotonic() < deadline:
+ try:
+ Client(timeout=2).call('GET', '/health/ready', schema='Health')
+ return
+ except AssertionError:
+ time.sleep(.5)
+ raise AssertionError('readiness did not recover')
+
+
+class Probe:
+ """Short-lived mc service; root secrets remain inside Docker secrets."""
+ def __enter__(self):
+ self.name = STACK+'_probe_'+uuid.uuid4().hex[:8]
+ args = ['service', 'create', '--detach', '--no-resolve-image', '--name', self.name, '--network', STACK+'_data',
+ '--restart-condition', 'none', '--limit-memory', '512m', '--limit-cpu', '0.5',
+ '--entrypoint', '/bin/sh']
+ for secret in ('s3_root_access_key', 's3_root_secret_key', 's3_access_key', 's3_secret_key'):
+ args += ['--secret', 'source='+STACK+'_'+secret+',target='+secret]
+ docker(*args, MC, '-c', 'sleep 3600')
+ deadline = time.monotonic()+45
+ while time.monotonic() < deadline:
+ ids = docker('ps', '--filter', 'label=com.docker.swarm.service.name='+self.name, '-q').stdout.decode().split()
+ if ids:
+ self.container = ids[0]
+ return self
+ time.sleep(.3)
+ docker('service', 'rm', self.name, check_result=False)
+ raise AssertionError('probe did not start')
+
+ def mc(self, *args, data=None, admin=False, expected=0):
+ access = 's3_root_access_key' if admin else 's3_access_key'
+ secret = 's3_root_secret_key' if admin else 's3_secret_key'
+ script = ('set -eu; export MC_CONFIG_DIR=/tmp/mc; '
+ 'mc alias set local http://minio:9000 "$(cat /run/secrets/'+access+')" '
+ '"$(cat /run/secrets/'+secret+')" >/dev/null; exec mc "$@"')
+ result = docker('exec', '-i', self.container, '/bin/sh', '-c', script, 'sh', *args,
+ data=data, check_result=False)
+ check((result.returncode == 0) == (expected == 0), 'unexpected mc outcome: '+args[0])
+ return result
+
+ def __exit__(self, *args):
+ docker('service', 'rm', self.name, check_result=False)
diff --git a/tests/own/lab4.py b/tests/own/lab4.py
new file mode 100644
index 00000000..8622723b
--- /dev/null
+++ b/tests/own/lab4.py
@@ -0,0 +1,154 @@
+#!/usr/bin/env python3
+"""Real S3/API negative cases and failures; only synthetic lab data."""
+import hashlib
+import http.client
+import json
+import os
+from pathlib import Path
+import subprocess
+import time
+import uuid
+from urllib.parse import urlsplit
+
+from helpers import Client, Probe, ROOT, STACK, check, containers, docker, png, oversized_dimensions, save, sql, upload, wait_ready
+
+
+def main():
+ c = Client(timeout=55)
+ alice, bob, eve, chat = c.bootstrap()
+ data = png()
+ result = {'negative_cases': [], 'failures': {}, 'checksums': {}}
+ for name, body, mime, filename, status in (
+ ('oversized_bytes', b'x'*(10*1024*1024+1), 'image/png', 'large.png', 413),
+ ('damaged_png', data[:50], 'image/png', 'broken.png', 415),
+ ('corrupt_crc', data[:-16]+b'\xff'*16, 'image/png', 'crc.png', 415),
+ ('mime_spoof', data, 'image/jpeg', 'fake.jpg', 415),
+ ('not_image', b'plain text', 'image/png', 'fake.png', 415),
+ ('unsupported_mime', data, 'application/octet-stream', 'file.png', 415),
+ ('pixel_budget', oversized_dimensions(), 'image/png', 'large-dimensions.png', 413),
+ ('unsafe_slash', data, 'image/png', '../file.png', 400),
+ ('unsafe_backslash', data, 'image/png', 'x\\y.png', 400),
+ ('unsafe_control', data, 'image/png', 'x\x00.png', 400),
+ ('empty_filename', data, 'image/png', '', 400),
+ ):
+ upload(c, alice, chat, body, filename, mime, status)
+ result['negative_cases'].append({'case': name, 'status': status})
+ for content, filename, mime in ((data, 'изображение.png', 'image/png'),
+ ((ROOT/'tests/own/fixtures/sample.jpg').read_bytes(), 'photo.jpg', 'image/jpeg')):
+ att = upload(c, alice, chat, content, filename, mime)['json']
+ downloaded = c.call('GET', '/api/v1/attachments/'+att['id']+'/content', actor=bob)
+ check(downloaded['bytes'] == content, 'original bytes changed')
+ check(att['sha256'] == hashlib.sha256(content).hexdigest(), 'incorrect SHA256')
+ check(downloaded['headers'].get_content_type() == mime, 'incorrect content MIME')
+ result['checksums'][mime] = att['sha256']
+ att = upload(c, alice, chat, data)['json']
+ for suffix in ('', '/content'):
+ c.call('GET', '/api/v1/attachments/'+att['id']+suffix, 404, actor=eve)
+ c.call('GET', '/api/v1/attachments/'+str(uuid.uuid4())+suffix, 404, actor=alice)
+ c.call('GET', '/api/v1/attachments/'+att['id']+suffix, 401)
+ upload(c, eve, chat, data, expected=404)
+ c.call('GET', '/api/v1/attachments/'+att['id']+'/content?variant=thumbnail', 404, actor=bob)
+ other = c.call('POST', '/api/v1/chats', 201, actor=alice,
+ body={'title':'other chat', 'member_ids':[alice['id'], bob['id']]}, schema='Chat')['json']
+ path = '/api/v1/chats/'+other['id']+'/messages'
+ for ids, status in (([att['id']], 404), ([str(uuid.uuid4())], 404), ([att['id'],att['id']], 400)):
+ c.call('POST', path, status, actor=alice, body={'text':'', 'attachment_ids':ids},
+ headers={'Idempotency-Key':uuid.uuid4().hex})
+ result['negative_cases'] += [{'case': x, 'status': s} for x,s in (
+ ('foreign_attachment',404), ('foreign_chat_link',404), ('unknown_attachment',404),
+ ('duplicate_ids',400), ('missing_authentication',401), ('unknown_variant',404))]
+
+ # The proxy must stop oversized chunked bodies even without Content-Length.
+ address = urlsplit(c.base)
+ conn = http.client.HTTPConnection(address.hostname, address.port, timeout=55)
+ conn.request('POST', '/api/v1/chats/'+chat['id']+'/attachments?filename=chunked.png',
+ body=iter([b'x'*65536]*161), encode_chunked=True,
+ headers={'Authorization':'Bearer '+alice['token'], 'Content-Type':'image/png'})
+ response = conn.getresponse()
+ check(response.status == 413, 'chunked upload bypassed byte limit')
+ response.read(); conn.close()
+ result['negative_cases'].append({'case':'chunked_oversized', 'status':413})
+
+ # Disconnect halfway through the declared body. No object/metadata can be published.
+ before = int(sql('SELECT count(*) FROM attachments;'))
+ conn = http.client.HTTPConnection(address.hostname, address.port, timeout=5)
+ conn.putrequest('POST', '/api/v1/chats/'+chat['id']+'/attachments?filename=aborted.png')
+ conn.putheader('Authorization', 'Bearer '+alice['token'])
+ conn.putheader('Content-Type', 'image/png')
+ conn.putheader('Content-Length', str(len(data)+10000))
+ conn.endheaders(); conn.send(data[:40]); conn.close()
+ time.sleep(1)
+ check(int(sql('SELECT count(*) FROM attachments;')) == before, 'aborted upload published metadata')
+ leftovers = [docker('exec', container, 'find', '/tmp', '-maxdepth', '1', '-name', 'messenger-upload-*').stdout.decode()
+ for container in containers('api')]
+ check(not any(leftovers), 'aborted upload left spool files')
+ result['failures']['client_disconnect'] = {'metadata_added':0, 'spool_files_remaining':0}
+
+ with Probe() as probe:
+ key = sql("SELECT object_key FROM attachments WHERE id='"+att['id']+"';")
+ raw = docker('exec', containers('minio')[0], 'wget', '-S', '-O', '/dev/null',
+ 'http://127.0.0.1:9000/attachments/'+key, check_result=False)
+ check(b'403' in raw.stderr, 'unauthenticated direct S3 GET was not forbidden')
+ probe.mc('stat', 'local/attachments/'+key)
+ probe.mc('mb', 'local/forbidden-'+uuid.uuid4().hex[:8], expected=1)
+ probe.mc('pipe', 'local/attachments/outside-prefix', data=b'x', expected=1)
+ result['bucket_security'] = {'anonymous_get_status':403, 'create_bucket_denied':True,
+ 'write_outside_prefix_denied':True, 'console_published':False}
+ service = json.loads(docker('service', 'inspect', STACK+'_minio').stdout)[0]
+ check(not service['Spec'].get('EndpointSpec', {}).get('Ports'), 'MinIO publishes an external port')
+ for _ in range(2):
+ subprocess.run(['python3', str(ROOT/'scripts/swarm.py'), 'bootstrap-s3'], check=True,
+ stdout=subprocess.DEVNULL)
+ check(c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=bob)['bytes']==data,
+ 'repeated bucket bootstrap changed data')
+ result['bucket_security']['bootstrap_repeats'] = 2
+
+ # Stop the replica that accepted THIS upload, then read through another one.
+ accepted = upload(c,alice,chat,data)
+ source = accepted['headers']['X-Instance-Id']
+ env = dict(os.environ, TARGET_INSTANCE_ID=source)
+ started = time.monotonic()
+ subprocess.run(['python3',str(ROOT/'scripts/swarm.py'),'stop-one'],env=env,check=True,
+ stdout=subprocess.DEVNULL)
+ deadline = time.monotonic()+10
+ while True:
+ try:
+ downloaded = Client(timeout=2).call('GET','/api/v1/attachments/'+accepted['json']['id']+'/content',actor=bob)
+ check(downloaded['headers']['X-Instance-Id']!=source,'stopped upload replica still serves bytes')
+ check(downloaded['bytes']==data,'cross-replica original changed')
+ break
+ except AssertionError:
+ check(time.monotonic()0
+
+ # Keep one old linked object and remove a second original behind PostgreSQL.
+ live = upload(c,alice,chat,data)['json']
+ c.message(alice,chat,'keep live original',attachments=[live['id']])
+ missing = upload(c,alice,chat,data)['json']
+ message = c.message(alice,chat,'keep missing reference',attachments=[missing['id']])['json']
+ missing_key = sql("SELECT object_key FROM attachments WHERE id='"+missing['id']+"';")
+ probe.mc('rm','local/attachments/'+missing_key,admin=True)
+ current = reconcile(apply=True)
+ check(sql("SELECT status FROM attachments WHERE id='"+missing['id']+"';")=='ready','fresh metadata changed before grace')
+ result['fresh_metadata_preserved'] = current['fresh_skipped']>0
+
+ # Measure a real scan at two bucket sizes, crossing an S3 page boundary.
+ cost_prefix = 'original/cost-'+experiment+'/'
+ try:
+ for group,count,total in (('small',100,100),('extra',1000,1100)):
+ script = ('mkdir -p /tmp/cost/"$1"; i=1; while [ "$i" -le "$2" ]; do '
+ 'printf x > /tmp/cost/"$1"/"$i"; i=$((i+1)); done')
+ docker('exec',probe.container,'/bin/sh','-c',script,'sh',group,str(count))
+ probe.mc('mirror','--quiet','/tmp/cost/'+group,'local/attachments/'+cost_prefix+group,admin=True)
+ summary = reconcile()
+ check(summary['objects_scanned']>=total,'scan skipped S3 objects')
+ result['cost'].append({'phase':'fresh', 'synthetic_objects':total, **summary})
+ print('Fresh scan cost:',json.dumps({k:summary[k] for k in ('objects_scanned','metadata_scanned','list_requests_estimate','stat_requests','db_queries','duration_ms')}),flush=True)
+ print('Waiting92s for actual S3 timestamps: measuring aged-object locking, DB and HEAD cost next.',flush=True)
+ time.sleep(92)
+ for total in (1100,100):
+ summary = reconcile()
+ check(summary['orphans']>=total,'aged scan did not inspect all orphan fixtures')
+ result['cost'].append({'phase':'aged', 'synthetic_objects':total, **summary})
+ print('Aged scan cost:',json.dumps({k:summary[k] for k in ('objects_scanned','metadata_scanned','list_requests_estimate','stat_requests','db_queries','duration_ms')}),flush=True)
+ if total==1100:
+ probe.mc('rm','--recursive','--force','local/attachments/'+cost_prefix+'extra/',admin=True)
+ finally:
+ probe.mc('rm','--recursive','--force','local/attachments/'+cost_prefix,admin=True)
+
+ # S3/DB unavailability must stop reconciliation without destructive guesses.
+ for service in ('minio','postgres'):
+ try:
+ docker('service','scale','--detach',STACK+'_'+service+'=0')
+ deadline=time.monotonic()+30
+ while time.monotonic()0:
+ print('Waiting for actual S3 object age to exceed90s grace:',round(remaining,1),'seconds',flush=True)
+ time.sleep(remaining)
+ sql("UPDATE attachments SET created_at=clock_timestamp()-interval '10 minutes' WHERE id IN ('"+live['id']+"','"+missing['id']+"');")
+
+ # A second process holds the upload lock: even an old orphan is protected.
+ lock_script = "BEGIN; SELECT pg_advisory_xact_lock(hashtextextended('"+orphan+"',0));\n"
+ lock = subprocess.Popen(['docker','exec','-i',containers('postgres')[0],'psql','-U','messenger','-d','messenger','-At','-v','ON_ERROR_STOP=1'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
+ lock.stdin.write(lock_script.encode()); lock.stdin.flush()
+ try:
+ deadline = time.monotonic()+10
+ while sql("SELECT pg_try_advisory_xact_lock(hashtextextended('"+orphan+"',0));") != 'f':
+ check(time.monotonic()=1 and orphan in objects(probe),'upload lock did not protect orphan')
+ finally:
+ lock.stdin.write(b'ROLLBACK;\n'); lock.stdin.close()
+ lock.wait(timeout=15)
+ result['locked_candidate_preserved'] = True
+ # The apply above may mark the aged missing row; restore only its internal state
+ # to compare the following dry-run with an unchanged, well-defined fixture.
+ sql("UPDATE attachments SET status='ready',error_code=NULL WHERE id='"+missing['id']+"';")
+ dry = reconcile()
+ check(dry['orphans']>=1 and dry['missing']>=1,'dry-run did not detect injected inconsistencies')
+ check(orphan in objects(probe),'dry-run deleted an object')
+ check(sql("SELECT status FROM attachments WHERE id='"+missing['id']+"';")=='ready','dry-run modified metadata')
+ applied = reconcile(apply=True)
+ check(orphan not in objects(probe),'apply did not delete old orphan')
+ check(sql("SELECT status FROM attachments WHERE id='"+missing['id']+"';")=='failed','missing original not marked failed')
+ check(sql("SELECT count(*) FROM messages WHERE id='"+message['id']+"' AND '"+missing['id']+"'::uuid=ANY(attachment_ids);")=='1','repair deleted linked metadata or reference')
+ c.call('GET','/api/v1/attachments/'+missing['id'],409,actor=alice)
+ check(c.call('GET','/api/v1/attachments/'+live['id']+'/content',actor=bob)['bytes']==data,'repair deleted a live linked original')
+ repeated = reconcile(apply=True)
+ check(repeated['deleted']==0 and repeated['marked_failed']==0,'repeat apply was not idempotent')
+ result.update({'dry_run':dry, 'apply':applied, 'repeat_apply':repeated,
+ 'live_linked_preserved':True, 'missing_linked_reference_preserved':True})
+ save('lab4-reconciliation.json',result)
+ print('PASS: real two-store faults, grace, lock, dry-run/apply/repeat, linked data preserved and paginated scan cost.')
+
+
+if __name__=='__main__':
+ main()
diff --git a/tests/own/swarm_grade4.py b/tests/own/swarm_grade4.py
new file mode 100644
index 00000000..fd37a5bd
--- /dev/null
+++ b/tests/own/swarm_grade4.py
@@ -0,0 +1,165 @@
+#!/usr/bin/env python3
+"""Grade-4 acceptance through the public LB; writes only synthetic test data."""
+from concurrent.futures import ThreadPoolExecutor
+import json
+import os
+from pathlib import Path
+import subprocess
+import sys
+import threading
+import time
+import uuid
+
+ROOT = Path(__file__).resolve().parents[2]
+sys.path.insert(0, str(ROOT/'tests'))
+from client import Client, Failure, check, run
+from resilience import history
+
+STACK = os.environ.get('STACK_NAME', 'mtusi-lab4')
+
+
+def docker_json(*args):
+ return json.loads(subprocess.check_output(['docker', *args], text=True))
+
+
+def task_ids():
+ ids = subprocess.check_output(['docker','service','ps','--filter','desired-state=running','-q',STACK+'_api'], text=True).split()
+ return {i for i in ids if docker_json('inspect',i)[0]['Status']['State']=='running'}
+
+
+def wait_ready():
+ subprocess.run([sys.executable, str(ROOT/'scripts/swarm.py'), 'wait'], check=True, stdout=subprocess.DEVNULL)
+ time.sleep(3) # DNS and HAProxy rise interval
+
+
+def idempotency(c, alice, bob, eve, chat):
+ key = uuid.uuid4().hex
+ barrier = threading.Barrier(16)
+ def send(_):
+ local = Client(timeout=5)
+ barrier.wait()
+ return local.message(alice, chat, 'one concurrent effect', key=key)
+ with ThreadPoolExecutor(max_workers=16) as pool:
+ results = list(pool.map(send, range(16)))
+ expected = results[0]['json']
+ check(all(r['json']==expected and r['status']==201 for r in results), 'concurrent replay changed result')
+ instances = {r['headers']['X-Instance-Id'] for r in results}
+ check(len(instances)>=2, 'same-key race did not reach two replicas')
+ path = '/api/v1/chats/'+chat['id']+'/messages'
+ c.call('POST', path, 409, actor=alice, body={'text':'changed'}, headers={'Idempotency-Key':key})
+ c.call('POST', path, 404, actor=eve, body={'text':expected['text']}, headers={'Idempotency-Key':key})
+ other = c.message(bob, chat, expected['text'], key=key)['json']
+ check(other['id']!=expected['id'], 'key leaked across users')
+ second_chat = c.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Key scope','member_ids':[alice['id'],bob['id']]},schema='Chat')['json']
+ separate = c.message(alice,second_chat,expected['text'],key=key)['json']
+ check(separate['id']!=expected['id'], 'key leaked across chats')
+ for bad in ['', 'x'*129, 'bad key', 'bad,key', 'bad/key']:
+ c.call('POST', path, 400, actor=alice, body={'text':'invalid key'}, headers={'Idempotency-Key':bad})
+ c.message(alice,chat,'128 ASCII characters',key='x'*128)
+ retry = uuid.uuid4().hex
+ c.call('POST',path,400,actor=alice,body={'text':''},headers={'Idempotency-Key':retry})
+ c.message(alice,chat,'valid after invalid',key=retry)
+ check(sum(m['id']==expected['id'] for m in history(c,bob,chat))==1, 'race created duplicate')
+ # Revocation must be rechecked even for an already committed key.
+ c.call('DELETE','/api/v1/auth/sessions/current',204,actor=alice)
+ c.call('POST',path,401,actor=alice,body={'text':expected['text']},headers={'Idempotency-Key':key})
+ print('PASS: shared-key race on two replicas; replay/conflict/scope/validation/access/logout', flush=True)
+ return {'parallel_requests':16, 'instances':sorted(instances), 'unique_message_ids':1}
+
+
+def exercise(action, target=None):
+ c = Client(timeout=2)
+ alice,bob,eve,chat = c.bootstrap()
+ original_key = uuid.uuid4().hex
+ original = c.message(alice,chat,'confirmed before '+action,key=original_key)['json']
+ before = task_ids()
+ stop = threading.Event()
+ events = []
+ confirmations = []
+ lock = threading.Lock()
+ def worker(number):
+ local = Client(timeout=2)
+ key = uuid.uuid4().hex
+ body = 'load-'+key
+ while not stop.is_set():
+ started = time.monotonic()
+ try:
+ result = local.message(alice,chat,body,key=key)
+ with lock:
+ confirmations.append(result['json'])
+ local.call('GET','/api/v1/users/me',actor=bob,schema='User')
+ event = (started,time.monotonic(),result['json'],result['headers']['X-Instance-Id'],None)
+ key = uuid.uuid4().hex
+ body = 'load-'+key
+ except Failure as exc:
+ event = (started,time.monotonic(),None,None,str(exc))
+ with lock:
+ events.append(event)
+ time.sleep(.03)
+ threads = [threading.Thread(target=worker,args=(i,)) for i in range(4)]
+ for thread in threads: thread.start()
+ time.sleep(2)
+ env = dict(os.environ)
+ if target: env['TARGET_INSTANCE_ID'] = target
+ started = time.monotonic()
+ proc = subprocess.Popen([sys.executable,str(ROOT/'scripts/swarm.py'),action],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
+ try:
+ code = proc.wait(timeout=180)
+ check(code==0, action+' command failed')
+ completed = time.monotonic()
+ time.sleep(15 if target else 5)
+ finally:
+ stop.set()
+ for thread in threads: thread.join(timeout=10)
+ if proc.poll() is None:
+ proc.kill(); proc.wait()
+ ended = time.monotonic()
+ wait_ready()
+ window = [e for e in events if e[0]>=started]
+ successes = [e for e in window if e[2] is not None]
+ failures = [e for e in window if e[2] is None]
+ check(bool(successes), 'no writes during '+action)
+ recovery = min(e[1] for e in successes if e[0]>=completed)-started if target else None
+ if target:
+ check(recovery<=10, 'failure recovery exceeded 10 seconds')
+ check(target not in [e[3] for e in successes[-20:]], 'stopped instance returned')
+ check(target not in task_ids(), 'old task still running')
+ else:
+ check(before.isdisjoint(task_ids()), 'rolling update did not replace every replica')
+ replay = c.message(alice,chat,original['text'],key=original_key)['json']
+ check(replay==original, 'committed idempotency result lost after '+action)
+ all_messages = history(c,bob,chat)
+ by_id = {m['id']:m for m in all_messages}
+ check(by_id.get(original['id'])==original, 'pre-action confirmation lost')
+ check(all(by_id.get(m['id'])==m for m in confirmations), 'confirmation lost/changed')
+ texts = [m['text'] for m in all_messages]
+ check(len(texts)==len(set(texts)), 'uncertain retry created duplicate')
+ times = [started]+sorted(e[1] for e in successes)+[ended]
+ max_gap = max(b-a for a,b in zip(times,times[1:]))
+ check(max_gap<=10, 'writes stalled for more than 10 seconds')
+ report = {'action':action,'target':target,'duration_seconds':round(ended-started,3),
+ 'successful_writes':len(successes),'failed_attempts':len(failures),
+ 'successful_rps':round(len(successes)/(ended-started),3),
+ 'first_recovery_seconds':round(recovery,3) if recovery is not None else None,
+ 'max_success_gap_seconds':round(max_gap,3),
+ 'instances_before':sorted(before),'instances_after':sorted(task_ids()),
+ 'confirmed_messages_checked':len(confirmations)+1,
+ 'duplicate_bodies':0}
+ print(json.dumps(report),flush=True)
+ return report
+
+
+def main():
+ wait_ready()
+ c = Client()
+ results = {'idempotency':idempotency(c,*c.bootstrap())}
+ originals = sorted(task_ids())
+ check(len(originals)==2,'grade4 scenario expects exactly two API replicas')
+ results['failovers'] = [exercise('stop-one',target) for target in originals]
+ results['rolling_update'] = exercise('update')
+ (ROOT/'evidence/swarm-grade4.json').write_text(json.dumps(results,indent=2)+'\n')
+ print('PASS: each original replica stopped and replaced; rolling update under load; confirmations and sessions preserved')
+
+
+if __name__=='__main__':
+ run(main)