Files
MagNetFlix/docs/roadmap.md
T
kiskaadee a3649c3ead docs(architecture): reconcile review findings and enforce fail-closed portable auth
- AGENTS.md: replace hardcoded Traefik/Authelia references with generic reverse-proxy/OIDC SSO triggers and align Tier 1 dangerous extensions (case-insensitive, adding .ps1 and .7z).
- docs/adr/001-pipeline-safety-and-bff-dual-client.md: remove hardcoded addmovie.roadtotech.me/Traefik/Authelia references, add CORS_ALLOWED_ORIGINS portability, and restrict local_user fallback strictly to AUTH_ENABLED=false (fail-closed 401 otherwise).
- docs/adr/002-pipeline-invariants-and-active-reconciliation.md: add Base32-to-hex info_hash normalization and uq_download_jobs_active_info_hash index (DEC-2), refine Tier 1/2 sanitizer and path/subtitle rules (DEC-3/5), add bounded startup retry and zero-delta stall wording (DEC-6), separate relocation executor and subscribe-before-snapshot coalescing (DEC-7), and enforce fail-closed AUTH_ENABLED + CORS_ALLOWED_ORIGINS (DEC-8).
- docs/architecture.md: add Canonical References and Scope/Non-Goals/Operational Limits, document QUEUED->FAILED transition, align DEC-2..DEC-8 invariants, and add /healthz and gRPC health check probes.
- docs/deployment.md: parameterize Compose environment/volume/proxy variables (MAGNETFLIX_HOST, TRAEFIK_AUTH_MIDDLEWARE, proxy_network, MAGNETFLIX_*_DIR), add worker non-root PUID/PGID + depends_on + transmission healthcheck, and update fail-closed identity adapter table.
- docs/roadmap.md & todo.txt: mark M1.1-M1.7 completed, add pending M1.8 (Base32 info_hash normalization + migration 003), and synchronize M2-M4 work items with the reconciled DEC-3..DEC-8 requirements.
2026-11-14 00:16:55 -05:00

133 lines
10 KiB
Markdown

# 🗺️ MagNetFlix — Reconciled Implementation Roadmap
This roadmap breaks down the completion of **MagNetFlix** into clear, dependency-ordered milestones. Each milestone includes explicit prerequisites, work items, and verifiable exit criteria.
---
## 🎯 Milestones Dependency Graph
```mermaid
flowchart TD
M1["Milestone 1: Contracts, Toolchain & DB Hardening\n(GAP-1, GAP-2, TD-1, TD-2, TD-3)"]
M2["Milestone 2: Pipeline Domain Engine & Filesystem Safety\n(GAP-3, GAP-4, GAP-5)"]
M3["Milestone 3: Transmission Integration & Worker Coordinator\n(GAP-6, GAP-7)"]
M4["Milestone 4: FastAPI BFF Gateway, SSE & Web UI\n(GAP-8)"]
M5["Milestone 5: Multi-Container Stack & Deployment Verification\n(Traefik, SSO, Compose)"]
M1 --> M2
M2 --> M3
M3 --> M4
M4 --> M5
```
---
## 📌 Milestone 1: Contracts, Toolchain & Database Hardening
- **Objective:** Fix broken gRPC stub imports, ensure 100% reproducible offline container builds, eliminate the active-job deduplication race, and establish database schema invariants.
- **Dependencies:** None.
- **Requirements Satisfied:** `REQ-1`, `REQ-2`.
- **Work Items:**
- [x] `M1.1`: Create `services/contracts` workspace package (`pyproject.toml`, `module-name = "magnetflix"`).
- [x] `M1.2`: Reconfigure `buf.gen.yaml` to output stubs into `services/contracts/src`; generate and track stubs + `docs/api_spec.md` in Git (`DEC-1`).
- [x] `M1.3`: Update dependencies in `services/worker` and `services/bff` to include `magnetflix-contracts = { workspace = true }`.
- [x] `M1.4`: Update `pyproject.toml` to include `tests/` in `pyright`, remove exclusions that masked broken imports, and add `test:` target to `Makefile`.
- [x] `M1.5`: Update `services/worker/Dockerfile` and `services/bff/Dockerfile` to copy `services/contracts`, and include `alembic.ini` and `alembic/` in the Worker image.
- [x] `M1.6`: Create Alembic migration `002_add_info_hash_and_partial_unique_index.py` adding `info_hash` column and `uq_download_jobs_active_imdb_id` index (`DEC-2`).
- [x] `M1.7`: Update `DownloadJob` model and add unit tests in `tests/test_db.py` verifying duplicate active insertion rejection.
- [ ] `M1.8` *(Architecture Review Follow-up)*: Normalize Base32 `btih` `info_hash` to 40-char lowercase hex in `DownloadJob` and add partial unique index `uq_download_jobs_active_info_hash` via Alembic migration `003`.
- **Verifiable Exit Criteria:**
- `uv run python -c "from magnetflix.v1 import pipeline_pb2, pipeline_pb2_grpc"` succeeds with 0 errors.
- `uv run ruff check .` and `uv run pyright` pass cleanly across the entire workspace including contracts and tests.
- `uv run pytest tests/test_db.py` passes all tests including partial unique index verification.
---
## 📌 Milestone 2: Pipeline Domain Engine & Filesystem Safety
- **Objective:** Implement core business logic and security boundary modules: untrusted payload sanitization, metadata verification, and cross-volume publication.
- **Dependencies:** Milestone 1.
- **Requirements Satisfied:** `REQ-3`, `REQ-4`, `REQ-5`.
- **Work Items:**
- [ ] `M2.1`: Implement Media Sanitizer (`services/worker/src/worker/sanitizer.py`) adhering to `DEC-3`:
- Hard-fail on symlinks, hard links, `..` traversal, special files, and dangerous extensions (case-insensitive: `.exe`, `.bat`, `.cmd`, `.sh`, `.scr`, `.vbs`, `.ps1`, `.lnk`, `.iso`, `.zip`, `.rar`, `.7z`).
- Safely ignore unlisted non-dangerous files (such as `.nfo`, `.txt`, `.jpg`, `.sfv`).
- Discard samples (`< 100 MB` or case-insensitive word-boundary `"sample"` token / `Sample` directory); select largest primary video and allowlisted subtitles (`.srt`, `.vtt`, `.sub`, `.ass`).
- [ ] `M2.2`: Write unit tests in `tests/test_sanitizer.py` verifying all security, allowlist, and sample discard rules.
- [ ] `M2.3`: Implement TMDB Client & Token Verifier (`services/worker/src/worker/metadata.py`) adhering to `DEC-4`:
- Early resolution via `GET /find/{imdb_id}?external_source=imdb_id` using `httpx.AsyncClient` with bounded timeouts/retries.
- Validates IMDb ID format, validates movie entity type, extracts `(title, year)`.
- Post-download normalized token comparison between cached `(title, year)` and primary video filename.
- [ ] `M2.4`: Write unit tests in `tests/test_metadata.py` with mocked TMDB HTTP responses (match, 404, non-movie, mismatch).
- [ ] `M2.5`: Implement Library Relocator (`services/worker/src/worker/relocator.py`) adhering to `DEC-5`:
- Deterministic `<Title>` path sanitization (stripping/replacing `/`, `\`, `:`, `*`, `?`, `"`, `<`, `>`, `|`, `..`).
- Multi-track subtitle format preservation (`.srt`, `.vtt`, `.sub`, `.ass`) with language/forced tags, untagged fallback (`<SanitizedTitle> (<Year>).<ext>`), and disambiguation indices.
- Free-space pre-check + two-stage copy-and-swap: copy to `/media/movies/.staging_<job_id>/`, pre-check destination directory existence, and atomically swap via `os.replace`.
- [ ] `M2.6`: Write unit tests in `tests/test_relocator.py` asserting destination file layout and atomic swap error handling.
- **Verifiable Exit Criteria:**
- `uv run pytest tests/test_sanitizer.py tests/test_metadata.py tests/test_relocator.py` passes 100%.
- Zero filesystem breakout vulnerabilities possible under synthetic traversal fixtures.
---
## 📌 Milestone 3: Transmission Integration & Worker Coordinator
- **Objective:** Wrap Transmission RPC, coordinate the pipeline finite state machine, implement startup crash recovery, and expose the gRPC service.
- **Dependencies:** Milestone 2.
- **Requirements Satisfied:** `REQ-6`, `REQ-7`, `REQ-8`.
- **Work Items:**
- [ ] `M3.1`: Implement Transmission RPC Client Wrapper (`services/worker/src/worker/transmission.py`) adhering to `DEC-6`:
- `add_torrent(magnet_uri, download_dir=/data/staging/<job_id>)`.
- `get_progress(info_hash)` returning progress percentage and download speed.
- `remove_torrent(info_hash, delete_data=True)` for terminal cleanup.
- [ ] `M3.2`: Write unit tests in `tests/test_transmission.py` with mocked RPC client.
- [ ] `M3.3`: Implement Pipeline Coordinator & State Machine (`services/worker/src/worker/pipeline.py`) adhering to `DEC-6` & `DEC-7`:
- State progression: `QUEUED` $\rightarrow$ `DOWNLOADING` $\rightarrow$ `SCANNING` $\rightarrow$ `VERIFYING_METADATA` $\rightarrow$ `ORGANIZING` $\rightarrow$ `COMPLETED` (with `FAILED` reachable from any active state).
- 15-minute zero-progress-delta stalled torrent timeout.
- Startup crash recovery: query non-terminal jobs, fail DB rows, purge staging directories, remove torrents from Transmission (with bounded startup retry/backoff).
- Dedicated bounded relocation executor isolated from lightweight `transmission-rpc` and scan offloading.
- In-memory `asyncio.Queue` broadcaster (subscribe-before-snapshot + bounded per-subscriber coalescing) for real-time status streaming.
- [ ] `M3.4`: Implement gRPC Server Servicer (`services/worker/src/worker/service.py` & `main.py`) adhering to `pipeline.proto`:
- Implements `SubmitDownload`, `GetJob`, `ListJobs`, and `StreamJobStatus` plus gRPC health check.
- Maps application errors to proper gRPC status codes (`ALREADY_EXISTS`, `NOT_FOUND`, `INVALID_ARGUMENT`).
- [ ] `M3.5`: Write integration tests in `tests/test_pipeline.py` and `tests/test_grpc_service.py`.
- **Verifiable Exit Criteria:**
- `uv run pytest tests/test_pipeline.py tests/test_grpc_service.py` passes 100%.
- Worker process cleanly starts, handles startup reconciliation, and shuts down gracefully with `SIGTERM`.
---
## 📌 Milestone 4: FastAPI BFF Gateway, SSE & Web UI
- **Objective:** Implement the user-facing web gateway, pluggable SSO identity adapter, server-side TMDB search proxy, real-time SSE stream, and single-origin UI.
- **Dependencies:** Milestone 3.
- **Requirements Satisfied:** `REQ-9`, `REQ-10`.
- **Work Items:**
- [ ] `M4.1`: Implement Pluggable Identity Adapter Middleware in FastAPI (`DEC-8`):
- Configurable via `AUTH_ENABLED`, `AUTH_HEADER_USER`, and `CORS_ALLOWED_ORIGINS`; fails closed with `401` when `AUTH_ENABLED=true` and falls back to `local_user` only when `AUTH_ENABLED=false`.
- [ ] `M4.2`: Implement BFF gRPC Client Wrapper (`services/bff/src/bff/grpc_client.py`):
- Channels calls to Worker (`SubmitDownload`, `GetJob`, `ListJobs`, `StreamJobStatus`).
- [ ] `M4.3`: Implement REST Endpoints & Schemas (`services/bff/src/bff/main.py`):
- Dual route bindings: `POST /api/v1/downloads` and `POST /download` with Pydantic aliases (`magnet`/`magnet_uri`, `imdbid`/`imdb_id`).
- Server-side TMDB movie search proxy: `GET /api/v1/movies/search?q=...`.
- Server-Sent Events stream: `GET /api/v1/movies/events`.
- [ ] `M4.4`: Embed Hosted Web UI (`services/bff/src/bff/templates/` & `static/`):
- Clean, responsive dashboard (adapted from extension prototype `app.html`/`app.css`).
- Movie search autocomplete with poster previews, magnet input, and live progress bars.
- [ ] `M4.5`: Write API tests in `tests/test_bff_routes.py` covering auth extraction, dual routes, search proxy, and SSE streams.
- **Verifiable Exit Criteria:**
- `uv run pytest tests/test_bff_routes.py` passes 100%.
- BFF connects to gRPC worker, serves Web UI, and streams live download progress without browser CORS errors.
---
## 📌 Milestone 5: Multi-Container Stack & Deployment Verification
- **Objective:** Deliver production-ready containerization, Docker Compose stack, Traefik integration, and end-to-end slice verification.
- **Dependencies:** Milestone 4.
- **Requirements Satisfied:** `REQ-11`.
- **Work Items:**
- [ ] `M5.1`: Finalize `services/worker/Dockerfile` and `services/bff/Dockerfile`.
- [ ] `M5.2`: Author canonical `docker-compose.yml` defining `bff`, `worker`, and `transmission` services, volume mounts (`./data`, `/media/movies`), and internal/external networks.
- [ ] `M5.3`: Configure health probes: BFF `GET /healthz`, Worker gRPC standard health check.
- [ ] `M5.4`: Execute end-to-end integration test with a small public domain/legal torrent:
- Verify full lifecycle: Form submission $\rightarrow$ Transmission download $\rightarrow$ Sanitizer scan $\rightarrow$ Metadata token check $\rightarrow$ Two-stage atomic relocation into Jellyfin/Plex format $\rightarrow$ Live SSE completion update.
- **Verifiable Exit Criteria:**
- `docker compose build` succeeds with zero errors from a fresh checkout.
- Full end-to-end vertical slice runs to completion with media appearing in `/media/movies/<Title> (<Year>) [imdbid-<IMDbID>]/`.