ЛР 2 — БВТ2301 — Сафронов — на 5 #4

Closed
ricoroma wants to merge 43 commits from ricoroma/mtusi2026lab2-safronov:solution/bvt2301-safronov into master
43 Commits
Author SHA1 Message Date
ricoromaandClaude Opus 5.5 c6ad34d5ae docs: state that the cache offloads Postgres without a measured speedup
Skeleton contracts / contracts (pull_request) Canceled after 0s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 14:06:45 +03:00
ricoromaandClaude Opus 5.5 ad3a507b70 chore: archive track2-backup-restore change
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 08:06:49 +03:00
ricoromaandClaude Opus 5.5 5fe50db58b fix: fail the restore stage when writes during backup fail
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 08:06:14 +03:00
ricoromaandClaude Opus 5.5 3e9ec10613 docs: report track 2 backup and restore
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 07:45:26 +03:00
ricoromaandClaude Opus 5.5 d4fad59444 docs: add track 2 backup and restore evidence
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 07:43:24 +03:00
ricoromaandClaude Opus 5.5 67cb4d7d9a fix: keep the host awake during bench runs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 07:25:20 +03:00
ricoromaandClaude Opus 5.5 4be2316bf8 docs: record track 2 backup and restore hypotheses
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 05:18:06 +03:00
ricoromaandClaude Opus 5.5 8bc42720d7 feat: add backup and restore stage to bench
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 05:17:41 +03:00
ricoromaandClaude Opus 5.5 860bdac4f3 chore: archive track2-query-plans change
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 05:05:24 +03:00
ricoromaandClaude Opus 5.5 a44a576bbc fix: reset app history index per bench size
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 05:04:08 +03:00
ricoromaandClaude Opus 5.5 472ed0bca5 docs: report track 2 history index comparison
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 05:01:47 +03:00
ricoromaandClaude Opus 5.5 159001e781 docs: add track 2 query plan bench evidence
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 04:58:29 +03:00
ricoromaandClaude Opus 5.5 67be41e049 docs: record track 2 query plan hypotheses
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 04:25:39 +03:00
ricoromaandClaude Opus 5.5 d2656ab986 feat: compare history indexes on uneven chats in bench
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 04:24:59 +03:00
ricoromaandClaude Opus 5.5 3f3acc95b8 chore: archive bench-postgres change
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 04:04:15 +03:00
ricoromaandClaude Opus 5.5 23ceedb954 fix: compare Valkey maxmemory settings between bench labels
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 04:04:15 +03:00
ricoromaandClaude Opus 5.5 5a68f8e592 docs: report PostgreSQL baseline and history index plans
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 03:53:48 +03:00
ricoromaandClaude Opus 5.5 894b9786ae docs: add PostgreSQL baseline bench evidence
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 03:50:49 +03:00
ricoromaandClaude Opus 5.5 e61a0e59e4 refactor: shorten bench matrix to 500k and prefill chunks to 25k
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 03:20:54 +03:00
ricoromaandClaude Opus 5.5 b2624cce5c docs: record bench baseline hypotheses
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 01:24:35 +03:00
ricoromaandClaude Opus 5.5 e76eed789f feat: run load bench on PostgreSQL with query plans
- bench stand is a separate compose project mtusi-bench (API, Postgres,
  Valkey) with fixed limits, fresh volume per size and migrations
- k6 registers with a password, logs in with Basic and sends Bearer
- VACUUM (ANALYZE) and CHECKPOINT after prefill
- EXPLAIN (ANALYZE, BUFFERS) of the app's history query via auto_explain,
  plus the same query without the index in a rolled back transaction
- Postgres CPU, table and index size in results; storages in passport
- bench_compare checks storage limits, images and settings
- remove inproc.py and make bench_inproc

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 01:21:15 +03:00
ricoromaandClaude Opus 5.5 5582302d68 chore: archive failure-checks change
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 00:20:20 +03:00
ricoromaandClaude Opus 5.5 d5a44ecc59 fix: check leftover containers in failure stand cleanup
cleanup() now also requires that no container of the failure project is
left, not only volumes. REPORT.md says where uv is needed (run_dev and
the own test commands), the hook notes that CACHE_KEY_PREFIX is a copy,
and the student-tests spec names the sessions capability consistently.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 00:17:46 +03:00
ricoromaandClaude Opus 5.5 b0357468ff docs: add failure checks evidence and report
Evidence from 5b65051: make failure_checks passes 12 checks in 45 s and
leaves the working stand untouched (same StartedAt and row counts);
make test_isolated reports 98 of 108 scenarios tested, 13 of them by the
failure checks. REPORT.md gets the level 4 rows for automatic expiry,
database and Valkey checks and the chat creation rollback. The spec and
rule note that two failure check runs must not overlap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 23:39:39 +03:00
ricoromaandClaude Opus 5.5 5b65051a60 test: add automatic failure checks on a separate stand
make failure_checks brings up a throwaway compose project with its own
PostgreSQL and Valkey, a default and a tight API (2 s sessions, pool of
one), and runs the pytest tests marked fault through an explicit hook:
session expiry, database stop and return, pool exhaustion, chat creation
rollback, readiness, Valkey stop, hang, return and flush. The stand and
its volume are removed on any outcome; the working stand is untouched.
Regular runs deselect the fault tests, and 13 coverage exclusions go away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 23:20:34 +03:00
ricoromaandClaude Opus 5.5 3e79c6360f chore: archive membership-cache change
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 22:12:31 +03:00
ricoromaandClaude Opus 5.5 287a61c704 docs: re-shoot Valkey outage and restart evidence
Evidence from d7c9a59: Valkey stop adds 97 ms (one reconnect), pause
207 ms, 30 concurrent commands stay within 201 ms, and restarting Valkey
alone no longer switches the cache off. REPORT.md and the skill describe
the retry on a dropped connection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 22:07:35 +03:00
ricoromaandClaude Opus 5.5 d7c9a5972b fix: retry Valkey once on a dropped connection
The client passed the sync redis.retry.Retry to async connections, so no
retry could ever run. Use redis.asyncio.retry.Retry with one immediate
retry on ConnectionError only: after a Valkey restart the stale pooled
connection is reopened instead of switching the cache off for 5 s.
Timeouts are not retried, and asyncio.timeout bounds each cache command
(pool wait, connect, retry, reply) by [cache] timeout_seconds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 21:41:15 +03:00
ricoromaandClaude Opus 5.5 25a60d36cd docs: add membership-cache evidence and report
Evidence from cae4b09: cache hits against chats table reads, Valkey
stop/pause/restart and FLUSHALL, restart hook with Valkey, Valkey ports
and password from .env, and a fresh make test_isolated run (103 tests).
REPORT.md describes the role of Valkey, the service table and the new
level 4 rows. The cache-read check now counts seq_scan + idx_scan: the
small chats table is read by sequential scan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 21:31:55 +03:00
ricoromaandClaude Opus 5.5 cae4b09147 feat: cache chat cards in Valkey
Valkey 9.1 in compose caches the immutable chat card (title, members,
created_at) used by GET /chats/{id} and by the membership check of the
message endpoints. Cache-aside with a write after commit, keys per
database, TTL plus allkeys-lru, persistence off. redis-py runs without
retries; on failure the cache is skipped for a few seconds and requests
go to PostgreSQL. Readiness does not depend on Valkey.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 21:17:55 +03:00
ricoromaandClaude Opus 5.5 c7681b0e18 chore: archive session-auth change
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 19:49:41 +03:00
ricoromaandClaude Opus 5.5 7887e03ade fix: precompute dummy password hash at startup
- lifespan computes the dummy Argon2id hash: first unknown-user login
  takes as long as a wrong password
- evidence for session TTL from config.toml, default and environment
- REPORT.md: manual /docs check, TTL configuration row
- tasks.md: why checks ran on a separate dev database

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 19:16:18 +03:00
ricoromaandClaude Opus 5.5 0cef33335d docs: add session-auth evidence and report
- passwords and tokens: Argon2id hashes, no secrets in db and logs
- session expiry and database outage with a valid token
- make test-persistence and make test_isolated on ae4154e
- migration drops lab 1 data without passwords
- REPORT.md: sessions, defense scenario, evidence rows

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 18:59:53 +03:00
ricoromaandClaude Opus 5.5 ae4154eaf5 feat: add password login and bearer sessions
- Argon2id password hashes, KDF in a thread with its own limit
- POST /auth/sessions with Basic, DELETE /auth/sessions/current
- Bearer sessions in PostgreSQL, X-User-Id ignored
- migration drops lab 1 data without passwords
- student-tests on tokens, sessions spec and synced specs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 18:50:42 +03:00
ricoromaandClaude Opus 5.5 ee2643d21e chore: archive postgres-storage change
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 16:50:05 +03:00
ricoromaandClaude Opus 5.5 484914c38a docs: add evidence for postgres-storage verify fixes
- statement timeout: 503 and no SQL parameters in logs, before and after
- failed migration keeps the API from starting
- fresh clone: console from zero, then docker and make down
- restart evidence with docker compose ps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 16:47:42 +03:00
ricoromaandClaude Opus 5.5 125145df15 fix: hide SQL parameters in database error logs
- hide_parameters=True in the engine, rule in infra.md
- design: 79 of 80 tests, $(PYTHON) for test_isolated
- AGENTS.md: hand off deferred scenarios
- REPORT.md: unknown outcome of a timed-out commit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 16:41:47 +03:00
ricoromaandClaude Opus 5.5 8a5d2b8c8a docs: add postgres-storage evidence
- migrations from zero, schema and rerun on 2d0a7c8
- restart hook, database outage, pool timeout, postgres port
- make test_isolated mechanics: failure, stop, Ctrl+C, kill -9, keep db
- align REPORT.md numbers with the new evidence

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 16:27:58 +03:00
ricoromaandClaude Opus 5.5 2d0a7c83e9 feat: finish postgres-storage change
- keep engine and session factory in src/infra/db/session.py
- sync delta specs into openspec/specs, add db-migrations spec
- update rules, AGENTS.md and run skill for lab 2 storage
- fill REPORT.md for the storage part

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 16:20:49 +03:00
ricoromaandClaude Opus 5.5 90394ecdfb fix: finish test_isolated cleanup on Ctrl+C
uv run forwards SIGINT to the child that already got it from the
process group, so the second KeyboardInterrupt aborted the cleanup and
left the test container and database behind. Run the script with
$(PYTHON) like the skeleton checks and ignore SIGINT during cleanup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 15:41:37 +03:00
ricoromaandClaude Opus 5.5 4921afa39d feat: store messenger data in PostgreSQL
Replace the in-memory store with PostgreSQL 18.6: SQLAlchemy 2.1 async
engine on asyncpg, alembic migrations with an async env.py, repos on
AsyncSession and services that own the transaction. Username
uniqueness is enforced by the database (ON CONFLICT), a chat and its
members are created in one transaction, member order is kept in
chat_members.position, and a composite foreign key makes the sender a
member of the chat. History and chat lists use keyset pagination on
(chat_id, created_at, id) and (user_id, created_at, chat_id).

The compose stand runs postgres, a one-shot migrate service on the
API image and the API. All Postgres addresses come from .env without
literals; only the 127.0.0.1 publish address stays literal. make
run_dev starts Postgres and applies migrations before the server, and
make env_setup generates a random Postgres password.

Unavailable PostgreSQL yields 503: get_db_session bounds the request
with asyncio and classifies driver errors. BoundedCloseConnection
limits asyncpg's graceful close, which otherwise waited without limit
on a frozen server and held the request until it was resumed.

Add scripts/restart.sh for make test-persistence and make
test_isolated, which runs the image on a throwaway test database.
Remove make run_no_compose. X-User-Id identification is unchanged
until the session-auth change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 15:33:49 +03:00
ricoromaandClaude Opus 5.5 93b699a478 docs: align agent guide and rules with lab 2
Describe the lab 2 target in AGENTS.md and the layer rules: PostgreSQL
as the source of truth, Basic login and Bearer sessions, Redis or
Valkey on grade 4, compose stand with pinned images, named volumes and
the restart hook, migrations before the API, level 4 checks.

Add .claude/rules/auth.md for password hashing, Basic parsing, session
tokens, expiry and revocation. Record that FastAPI HTTPBasic decodes
credentials as ASCII and cannot parse UTF-8 passwords.

Add a transitional section to AGENTS.md: the code is still the lab 1
in-memory implementation until the OpenSpec changes land.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 00:05:48 +03:00
ricoromaandClaude Opus 5.5 7b775a29a9 chore: carry over lab 1 solution into lab 2 skeleton
Copy the committed lab 1 sources as-is: src/, student-tests/, bench/,
Dockerfile, docker-compose.yml, pyproject.toml, uv.lock,
.python-version, config_example.toml, AGENTS.md, CLAUDE.md, .claude/,
openspec/, .graphifyignore and KNOWN_ISSUES.md. Lab 1 evidence stays
in the lab 1 repository.

Merge the new skeleton files with own sections: Makefile (lab 2
targets plus own run, docker, bench and common targets), .gitignore,
.dockerignore and .env.example. Rename the image to
mtusi-messenger:lab2 and point AGENTS.md at lab 2.

The application is still the in-memory lab 1 implementation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 22:11:46 +03:00