fix: harden download history and attempt ledger
This commit is contained in:
1 parent
510fde7afa
commit
ba673ad732
19 files changed
+2287
-311
No files matched your search
@@ -122,3 +122,56 @@
|
||||
### Remaining concern
|
||||
|
||||
- Real PostgreSQL assertions remain blocked solely by the absent local Docker/container runtime. The expanded suite is not skipped and should be run unchanged on a Docker-enabled CI/release host.
|
||||
|
||||
## Fix Round 2
|
||||
|
||||
### Eligibility alignment
|
||||
|
||||
- Removed the Repeat service's unlocked `findIntent` fast path. Every initial request and same-key retry now enters the repository transaction, takes the editorial/usage locks, checks the original-delivery invariant, and only then returns or creates an intent. The insertion-race fallback repeats that same locked transaction instead of loading a winner outside the invariant.
|
||||
- Persisted intents now carry the real `download_events.status`. `resolveIntent` checks fingerprint/kind conflict semantics first and exposes only a still-`authorized` event; terminal or streaming events are rejected with generic not-found semantics instead of producing a stale 201/file URL.
|
||||
- Centralized the database delivery predicate in `deliveredOriginalEventCondition`: same editorial and usage, `intent_kind = new_usage`, `status in (completed, interrupted)`, and `transferred_bytes > 0`. Repeat creation/claim, prior-usage reads, and catalog dialog choices use this same predicate alongside non-voided billing status.
|
||||
- Catalog choices use one correlated `EXISTS` query, not N+1 reads. Inputs are bounded to the 60-photo catalog page and results to 300 choices. Abandoned, authorized-only, zero-byte, failed, voided, and foreign-editorial usages are not serialized to the dialog; delivered open/billed/reopened usages remain eligible.
|
||||
|
||||
### Global attempt stream
|
||||
|
||||
- Split usage pagination from attempt pagination. Usage snapshots remain bounded to 20+1 rows, while attempts are queried directly from `download_events` by authenticated `editorial_account_id`, independent of the selected usage page.
|
||||
- Attempt traversal includes all five statuses (`authorized`, `streaming`, `completed`, `interrupted`, `failed_before_delivery`). Because membership and ordering depend only on immutable `authorized_at + id`, later terminal transitions cannot insert an old row behind an issued cursor.
|
||||
- The attempt query joins `usages` once with a matching editorial scope and returns at most 25+1 rows. Every serialized attempt includes filename, album, purpose, reference, immutable usage price, effective billable cents, bytes, kind, timestamps, and current outcome. No hidden N+1 or unbounded attempt array remains.
|
||||
- The UI renders a separate German `Downloadversuche` ledger, including in-progress statuses and both effective attempt cost and immutable usage-price context. Attempt navigation preserves the usage page but is no longer constrained by its membership.
|
||||
- The cursor compares the raw indexed `(authorized_at, id)` tuple and serializes exact PostgreSQL microseconds plus UUID. Concurrent later events stay on a refreshed newest page; traversal of the pre-existing older stream has no duplicates or omissions.
|
||||
|
||||
### Migration
|
||||
|
||||
- Added `drizzle/0004_download_history_indexes.sql`, generated `drizzle/meta/0004_snapshot.json`, and updated the journal.
|
||||
- Added `download_events_editorial_authorized_id_idx` on `(editorial_account_id, authorized_at DESC, id DESC)`.
|
||||
- Added `usages_editorial_created_id_idx` on `(editorial_account_id, created_at DESC, id DESC)`.
|
||||
- The deterministic migration contract asserts both exact indexes and that neither usage nor download history is deleted. `drizzle-kit check` passes and the final generate is a no-op.
|
||||
|
||||
### Fix-round TDD evidence
|
||||
|
||||
- Repeat retry RED: an equivalent same-key request resolved successfully after repository eligibility was revoked. GREEN routes the retry through the locked repository; the focused Repeat suite passed 10/10.
|
||||
- Persisted-state RED: a `failed_before_delivery` event was returned as an intent. GREEN selects the true event status and rejects every non-authorized persisted event; combined usage/repeat services passed 2 files / 30 tests with typecheck.
|
||||
- Attempt-stream RED: the new separate all-status UI model failed because the component still dereferenced nested `usage.attempts`. GREEN renders the global bounded ledger; focused history tests passed 2 files / 12 tests.
|
||||
- Price-context RED: attempt rows did not expose their immutable usage price. GREEN renders labeled usage-price context for every attempt.
|
||||
- Migration RED: the 0004 contract failed with ENOENT. GREEN passed after schema generation, snapshot/journal update, Drizzle validation, and a no-op generation.
|
||||
- Combined focused verification passed 9 files / 61 tests before final full verification.
|
||||
|
||||
### PostgreSQL coverage and blocker
|
||||
|
||||
- Expanded the unskipped PostgreSQL suite with: same-key repeat rejection after a usage void; delivered-choice filtering across open/billed/reopened, abandoned and zero-byte rows; foreign-editorial isolation; global traversal from an empty usage page; status-transition and concurrent-new-event cursor stability; and `EXPLAIN` assertions for both new history indexes.
|
||||
- Command: `pnpm test tests/db/download-delivery.test.ts tests/db/usage-accounting.test.ts tests/db/accounting-migration.test.ts`.
|
||||
- Result: all three suites failed before collection with `Could not find a working container runtime strategy`; exactly 0 PostgreSQL tests executed. No test is skipped.
|
||||
|
||||
### Final verification
|
||||
|
||||
- `pnpm test src tests/db/accounting-migration-sql.test.ts tests/db/download-delivery-migration-sql.test.ts tests/db/download-history-index-migration-sql.test.ts` — 28 files / 169 tests passed.
|
||||
- `pnpm typecheck` — passed.
|
||||
- `pnpm lint` — passed.
|
||||
- `pnpm exec drizzle-kit check` — passed.
|
||||
- `pnpm exec drizzle-kit generate --name task_9_fix_round_2_final_noop` — no schema changes, nothing to migrate.
|
||||
- `git diff --check` — passed.
|
||||
- Webpack production build with documented `.env.example` values — passed compilation, TypeScript, page-data collection, 14/14 static pages, and build tracing.
|
||||
|
||||
### Remaining concern
|
||||
|
||||
- The only unresolved verification gap is environmental: the real PostgreSQL migration, race, traversal, and query-plan assertions require a Docker-capable host. Run the unchanged three-suite command above in CI before release.
|
||||
Reference in new issue
Block a user