9.9 KiB
Implementation Plan (Version 2)
This plan defines the path from the V1 baseline to Version 2 complete, aligned to the updated multi-image and multi-person relational domain model:
Documentacts as a logical parent container for physical artifacts, supporting multi-author and multi-recipient relationships viaDocumentPerson.Sourcerepresents an individual image page within a document, maintaining sequential order (page_number), cached active machine output (raw_transcription), and inline single user revisions (revised_text).Jobacts as an overarching batch orchestrator for multi-page async processing tasks.JobSourcerecords individual point-in-time API executions per image page, storing Pydantic-validatedai_metadataand raw REST envelopes (raw_api_response).- Pydantic V2 acts as the single source of truth for runtime validation, API payload parsing, and PostgreSQL JSONB serialization.
The objective is to complete the V2 scope with production readiness while keeping non-V2 enhancements out of active delivery.
V2 Completion Definition
V2 is complete when all of the following are true:
- Functional complete
- Multi-image and whole-folder uploads assign sequential page numbers to
Sourcerecords under a singleDocument. - Batch jobs process pages concurrently using an
asyncioworker pool with semaphore rate limiting. - Partial job failures resolve cleanly to
partial_success, allowing single-page retries without re-running successful pages. - Multi-author and multi-recipient tagging is supported on
Document.
- Data-model complete
- SQLite is fully replaced with PostgreSQL (using
asyncpgorpsycopg3). - Pydantic V2 models validate all API payloads, database row mappings, and
JSONBstructures.
- Operational complete
- Concurrency controls, worker pool metrics, and database connections operate safely under batch load.
- Documentation complete
schema_v2.md,DDL_v2.sql, Pydantic model contracts are updated and consistent.
Phase 1 — Data Contract Stabilization & Pydantic Baseline
Goal: Lock the PostgreSQL schema, DDL, and Pydantic V2 models before refactoring service logic.
Tasks
- Finalize DDL for PostgreSQL native types (
UUID,TIMESTAMPTZ,JSONB) and junction tables (document_person,job_source). - Build core Pydantic V2 schemas (
Person,Document,Source,Job,JobSource,PageAIMetadata). - Confirm and document data invariants:
source.raw_transcriptionandjob_source.raw_transcriptionare immutable machine outputs.source.revised_textholds user edits. UI rendersCOALESCE(revised_text, raw_transcription).- Page sequence is strictly ordered by
source.page_number ASC.
- Freeze V2 job status values (
queued,processing,completed,partial_success,failed) and page execution status values (pending,transcribed,failed).
Deliverables
- Canonical
docs/schema_v2.mdanddocs/DDL_v2.sql. - Centralized Pydantic validation suite in
models/schemas_v2.py.
Exit Criteria
- All database tables, relationships, and JSONB structures have corresponding Pydantic V2 models passing unit validation tests.
Phase 2 — Persistence Layer Transition (SQLite to PostgreSQL)
Goal: Replace the SQLite storage layer with an asynchronous PostgreSQL driver (asyncpg or psycopg3).
Tasks
- Configure PostgreSQL database connection pooling and environment configuration.
- Refactor
services/store.py/ repository layers to execute parameterized async SQL queries ($1,$2). - Implement JSONB serialization and deserialization helpers using Pydantic's
.model_dump_json()and.model_validate(). - Implement database bootstrap routines for PostgreSQL table creation and index initialization.
Deliverables
- PostgreSQL-native database connection and query service modules.
- Integration test suite confirming connection pooling and JSONB CRUD operations.
Exit Criteria
- All database reads/writes run asynchronously against PostgreSQL with zero remaining SQLite driver dependencies.
Phase 3 — Service Layer & asyncio Engine Refactor
Goal: Implement batch orchestration and parallel single-image API execution.
Tasks
- Refactor upload service to process folder/multi-image input:
- Group files into a single
Document. - Create ordered
Sourcerows (page_number = 1..N).
- Refactor
services/workflows.pywithasyncioworker pools:
- Use
asyncio.Semaphoreto enforce API provider rate limits. - Issue parallel single-image requests to Vision APIs (OpenAI/Claude).
- Parse API responses directly into Pydantic models (
PageAIMetadata).
- Update execution tracking:
- Create a
JobSourcerow per page call to recordraw_transcription,ai_metadata, andraw_api_response. - Update active
source.raw_transcriptionupon task completion. - Calculate aggregate batch status (
completed,partial_success,failed) on the parentJob.
- Refactor
services/person.pyandservices/documents.pyto handle multi-person roles viadocument_person.
Deliverables
- Asynchronous batch execution engine in
services/workflows.py. - Service routines for multi-person tagging and page-level retries.
Exit Criteria
- Executing a folder upload of 10+ images processes concurrently, populates page-level
JobSourceentries, and handles partial worker errors without crashing the batch.
Phase 4 — UI & API Contract Alignment
Goal: Update API endpoints and frontend/UI views to render multi-page documents and person roles.
Tasks
- Update document and job API endpoints to accept batch file arrays and multi-person ID payloads.
- Update UI document views:
- Render multi-page document transcriptions sequentially by
page_number. - Display author and recipient chips/cards linked from
document_person.
- Update job detail UI to show page-level execution statuses (
transcribedvs.failed) and provide a "Retry Failed Pages" action forpartial_successjobs. - Align inline page editing controls to update
source.revised_textandsource.date_revised.
Deliverables
- Refactored API routes and UI components supporting multi-page rendering and person management.
Exit Criteria
- UI successfully displays multi-page document text, allows per-page human revisions, and shows author/recipient metadata.
Phase 5 — Test Suite Realignment & Concurrency Testing
Goal: Ensure end-to-end system stability under concurrent async execution and load.
Tasks
- Write unit tests for Pydantic models, custom validators, and JSONB conversions.
- Write integration tests for async database operations:
- CRUD for
Document,Person,DocumentPerson,Source,Job, andJobSource.
- Write mock-backed async workflow tests:
- Verify
asyncio.Semaphorebounds concurrent tasks properly. - Validate state transition logic for
completed,partial_success, andfailedjobs. - Confirm retry routines process only targeted
JobSourcerecords marked asfailed.
- Re-enable CI quality gates (linting, type checking with Pyright/mypy, pytest).
Deliverables
- Passing asynchronous test suite covering core workflows, edge cases, and failure recoveries.
Exit Criteria
- CI pipeline is green with comprehensive coverage across database operations, Pydantic models, and worker queues.
Phase 6 — Reliability, Operations, and Release Readiness
Goal: Prepare V2 for production deployment and operator management.
Tasks
- Verify structured logging includes
job_id,document_id,source_id, andperson_id. - Tune PostgreSQL connection pool limits and
asyncioconcurrency thresholds for production infrastructure. - Update operational documentation:
- Review and update
docs/schema_v2.mdas needed. - Create
docs/runbook_v2.mddetailing PostgreSQL maintenance, JSONB index management, and worker queue monitoring. - Create
docs/release_checklist_v2.mdfor launch sign-off.
Deliverables
- Updated project documentation and operational runbooks.
- V2 release sign-off checklist.
Exit Criteria
- All documentation reflects V2 architecture; launch checklist is fully verified.
Requirement Traceability Focus
Maintain evidence against these V2 requirement groups:
- Batch & Multi-Image Pipeline: Folder ingestion, page ordering, async worker execution.
- Database & Persistence: PostgreSQL, native UUIDs, JSONB execution storage,
asyncpgpooling. - Validation & Schemas: Pydantic V2 models for DB rows, API requests, and AI vision responses.
- Attribution & Metadata: Multi-author and multi-recipient tagging, biographical entity management.
- Error Recovery: Partial success states, page-level status flags, isolated retry execution.
Scope Discipline Rule (V2 Focus)
- Only tasks required for V2 scope (PostgreSQL, Pydantic V2, folder/async processing, multi-person roles) enter this plan.
- V3 candidate features (such as side-by-side multi-provider model output comparison) remain strictly in the future backlog.
- Any schema adjustments during implementation require immediate updates to
DDL_v2.sql, Pydantic models, andschema_v2.md.
Technology References
- FastAPI documentation
- NiceGUI documentation
- PostgreSQL documentation
- Python asyncio
- Pydantic Validation
- Pydantic AI
Related Local References
- System Overview
- System Design Intent
- Transcription Methodology
- System Architecture
- System Requirements
- Data model
- Error Handling Policy
- Implementation Plan (this document)