fix: harden runtime durability and concurrency #30

Merged
isyuricunha merged 1 commit from fix/economy-runtime-durability into main 2026-08-25 15:28:19 +00:00
isyuricunha commented 2026-08-25 14:19:33 +00:00 (Migrated from github.com)

Summary

Hardens Yue's runtime around database concurrency, high-frequency message paths, Voice XP restart durability, internal Bot ↔ API requests, configuration parsing, and reproducible database migrations.

This milestone keeps existing user-facing behavior while moving correctness-sensitive state transitions into PostgreSQL, removing avoidable hot-path work, and restoring clean fresh-database deployment with the same migration path used in production.

Economy and cooldown correctness

  • makes daily reward claims atomic with Serializable transactions and bounded retries
  • guarantees one credit and one transaction under concurrent claims
  • turns command cooldown consumption into one atomic reservation
  • enforces one cooldown row per guild/user/command
  • safely deduplicates legacy cooldown rows before adding the unique invariant
  • adds PostgreSQL integration tests with 12 concurrent daily claims and 12 concurrent cooldown reservations

Message hot path

  • adds bounded positive/negative AFK caching
  • avoids repeated database misses for users who are not AFK
  • gates conversation handling before queue work for ineligible messages
  • reuses the existing XP configuration cache

Voice XP durability

  • persists active Voice XP sessions in PostgreSQL
  • checkpoints full minutes every 60 seconds and on shutdown/leave
  • updates XP and checkpoints together in Serializable transactions
  • reconciles persisted sessions with Discord voice state on startup
  • removes stale sessions and does not award bot downtime after restart

Internal Bot ↔ API hardening

  • shares schemas/types for sensitive internal request bodies
  • adds a bounded request body reader (64 KiB default, 1 MiB configuration ceiling)
  • returns 413 for oversized internal bodies and 400 for malformed JSON
  • authenticates internal requests before consuming request bodies
  • centralizes strict server-only environment parsers under @yuebot/shared/env
  • rejects partial numeric configuration such as 123abc

Database migration replay

  • restores prisma migrate deploy on a brand-new PostgreSQL 16 database
  • keeps every previously committed historical migration unchanged, preserving production checksums
  • adds conditional replay compatibility migrations before historical out-of-order operations
  • adds a production-safe final schema reconciliation for runtime-schema objects missing from migration history
  • uses guarded columns, indexes, tables, and foreign keys so existing installations can deploy safely
  • verifies the migration-produced database has zero drift from schema.prisma

Database / cleanup

  • removes the unused legacy sessions table
  • adds the cooldown uniqueness invariant
  • adds persistent voice_xp_sessions storage and checkpoint index
  • updates the performance audit with the runtime durability and migration replay milestone

Permanent CI guardrails

The normal checks job now:

  • starts PostgreSQL 16 without a committed database password
  • runs the complete migration history with prisma migrate deploy
  • fails if the resulting database drifts from schema.prisma
  • runs database concurrency tests with RUN_DATABASE_INTEGRATION=1
  • runs Knip in addition to lint, type-check, tests and build

Secret-scan remediation

A temporary CI iteration had contained a hardcoded disposable PostgreSQL test credential. The credential was removed and the pull-request branch history was rewritten to a single clean commit based directly on main, so the flagged intermediate commit is no longer part of the PR range.

GitGuardian now scans the single clean PR commit with no secrets detected.

Final validation

  • fresh PostgreSQL 16 full prisma migrate deploy ✅
  • zero schema drift against schema.prisma ✅
  • pnpm lint ✅
  • pnpm type-check ✅
  • pnpm test including real PostgreSQL concurrency tests ✅
  • pnpm build ✅
  • pnpm knip ✅
  • internal API HTTP tests for auth/body limits/invalid JSON ✅
  • Docker smoke linux/amd64 ✅
  • Docker smoke linux/arm64 ✅
  • GitGuardian Security Checks ✅

The pull request remains open and unmerged.

## Summary Hardens Yue's runtime around database concurrency, high-frequency message paths, Voice XP restart durability, internal Bot ↔ API requests, configuration parsing, and reproducible database migrations. This milestone keeps existing user-facing behavior while moving correctness-sensitive state transitions into PostgreSQL, removing avoidable hot-path work, and restoring clean fresh-database deployment with the same migration path used in production. ## Economy and cooldown correctness - makes daily reward claims atomic with Serializable transactions and bounded retries - guarantees one credit and one transaction under concurrent claims - turns command cooldown consumption into one atomic reservation - enforces one cooldown row per guild/user/command - safely deduplicates legacy cooldown rows before adding the unique invariant - adds PostgreSQL integration tests with 12 concurrent daily claims and 12 concurrent cooldown reservations ## Message hot path - adds bounded positive/negative AFK caching - avoids repeated database misses for users who are not AFK - gates conversation handling before queue work for ineligible messages - reuses the existing XP configuration cache ## Voice XP durability - persists active Voice XP sessions in PostgreSQL - checkpoints full minutes every 60 seconds and on shutdown/leave - updates XP and checkpoints together in Serializable transactions - reconciles persisted sessions with Discord voice state on startup - removes stale sessions and does not award bot downtime after restart ## Internal Bot ↔ API hardening - shares schemas/types for sensitive internal request bodies - adds a bounded request body reader (64 KiB default, 1 MiB configuration ceiling) - returns 413 for oversized internal bodies and 400 for malformed JSON - authenticates internal requests before consuming request bodies - centralizes strict server-only environment parsers under `@yuebot/shared/env` - rejects partial numeric configuration such as `123abc` ## Database migration replay - restores `prisma migrate deploy` on a brand-new PostgreSQL 16 database - keeps every previously committed historical migration unchanged, preserving production checksums - adds conditional replay compatibility migrations before historical out-of-order operations - adds a production-safe final schema reconciliation for runtime-schema objects missing from migration history - uses guarded columns, indexes, tables, and foreign keys so existing installations can deploy safely - verifies the migration-produced database has zero drift from `schema.prisma` ## Database / cleanup - removes the unused legacy `sessions` table - adds the cooldown uniqueness invariant - adds persistent `voice_xp_sessions` storage and checkpoint index - updates the performance audit with the runtime durability and migration replay milestone ## Permanent CI guardrails The normal `checks` job now: - starts PostgreSQL 16 without a committed database password - runs the complete migration history with `prisma migrate deploy` - fails if the resulting database drifts from `schema.prisma` - runs database concurrency tests with `RUN_DATABASE_INTEGRATION=1` - runs Knip in addition to lint, type-check, tests and build ## Secret-scan remediation A temporary CI iteration had contained a hardcoded disposable PostgreSQL test credential. The credential was removed and the pull-request branch history was rewritten to a single clean commit based directly on `main`, so the flagged intermediate commit is no longer part of the PR range. GitGuardian now scans the single clean PR commit with no secrets detected. ## Final validation - fresh PostgreSQL 16 full `prisma migrate deploy` ✅ - zero schema drift against `schema.prisma` ✅ - `pnpm lint` ✅ - `pnpm type-check` ✅ - `pnpm test` including real PostgreSQL concurrency tests ✅ - `pnpm build` ✅ - `pnpm knip` ✅ - internal API HTTP tests for auth/body limits/invalid JSON ✅ - Docker smoke `linux/amd64` ✅ - Docker smoke `linux/arm64` ✅ - GitGuardian Security Checks ✅ The pull request remains open and unmerged.
gitguardian[bot] commented 2026-08-25 14:19:38 +00:00 (Migrated from github.com)

️✅ There are no secrets present in this pull request anymore.

If these secrets were true positive and are still valid, we highly recommend you to revoke them.
While these secrets were previously flagged, we no longer have a reference to the
specific commits where they were detected. Once a secret has been leaked into a git
repository, you should consider it compromised, even if it was deleted immediately.
Find here more information about risks.


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

#### ️✅ There are no secrets present in this pull request anymore. If these secrets were true positive and are still valid, we highly recommend you to revoke them. While these secrets were previously flagged, we no longer have a reference to the specific commits where they were detected. Once a secret has been leaked into a git repository, you should consider it compromised, even if it was deleted immediately. Find [here](https://docs.gitguardian.com/platform/remediate/remediate-incidents) more information about risks. --- <sup><sub>🦉 [GitGuardian](https://dashboard.gitguardian.com/auth/login/?utm_medium=checkruns&amp;utm_source=github&amp;utm_campaign=cr1) detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.<br/></sup></sub>
Sign in to join this conversation.
No description provided.