generated from john/python-template
5.5 KiB
5.5 KiB
System Architecture (Version 4)
This document describes the production architecture of the document transcription system.
Architecture Objectives
- Preserve original source material and immutable machine transcription output.
- Support batching one or more images into ordered multi-page documents.
- Capture complete submission-time prompt provenance and per-page provider response evidence.
- Execute page transcription concurrently with bounded
asyncioworkers. - Maintain relational portability across SQLite and PostgreSQL.
- Keep operator workflows cross-platform and Python-driven.
- Support many-to-many document-person relationships with extensible roles.
- Support registry-driven document type classification.
- Enforce relationship-role exclusivity rules consistently across UI, API, and persistence boundaries.
Runtime Topology
The runtime operates as an asynchronous Python application:
- FastAPI + NiceGUI web application process.
- In-process
asyncioworker engine for transcription execution. - Relational persistence via SQLModel / SQLAlchemy.
- Pydantic V2 validation across API payloads, prompt configuration, and structured metadata.
^^^mermaid flowchart LR U[Browser User] --> A[FastAPI + NiceGUI App] A --> W[Asyncio Worker Engine] A --> DB[(Relational DB)] W --> P[Vision Provider APIs] W --> DB ^^^
Lifecycle Ownership
Application lifespan owns runtime setup and teardown:
- Initialize logging, settings, directories, and prompt configuration.
- Manage asynchronous database engine connection pools.
- Execute database bootstrap or migrations.
- Recover stale or interrupted jobs on startup.
- Manage graceful shutdown of active background tasks.
Layered Module Structure
Interface Layer
src/transcription/ui/**src/transcription/api/**
Responsibilities:
- Render document, source, person, job, and classification views.
- Accept user input for uploads, editing, linking, and revisions.
- Present structured validation and conflict feedback.
Application and Async Worker Layer
src/transcription/services/workflows.pysrc/transcription/worker.py
Responsibilities:
- Orchestrate uploads, job creation, and status transitions.
- Execute per-page provider calls through bounded concurrency.
- Persist page-level outcomes and update aggregate job state.
Domain and Service Layer
src/transcription/db/models.pysrc/transcription/services/*.py
Responsibilities:
- Manage transactional operations for documents, people, types, links, sources, jobs, and job sources.
- Apply deterministic conflict handling for relationship-role writes.
- Use set-based synchronization for many-to-many relationship updates.
- Resolve and validate registry-backed document types.
Infrastructure Layer
src/transcription/db/**src/transcription/providers/**
Responsibilities:
- Provide async database sessions and engine configuration.
- Provide provider adapters for vision model execution.
Core Workflows
1. Multi-Page Transcription
- User uploads one or more images for a
Document. - System stores files, hashes them, creates ordered
Sourcerows, and creates aJob. - Worker claims the job, marks it
processing, and executes page calls concurrently. - Each page writes a
JobSourceresult with raw output, metadata, and full provider response evidence. - Aggregate status becomes
completed,partial_success, orfailed.
2. Document-Person Relationship Management
- User opens a document or person edit flow.
- UI loads existing links grouped by role.
- User adds or removes people within one or more roles.
- Service computes add/remove deltas rather than replacing all links blindly.
- Conflict checks enforce exclusivity policy before persistence commits.
3. Document Type Management
- User selects a registry-backed document type for a document.
- Service resolves the stable type code or id.
- Persistence stores the
document_type_idreference. - Inactive types remain valid for historical rows but are excluded from default selectors.
Domain Invariants
Source.raw_transcriptionstores immutable machine output.- Human corrections occur only in
Source.revised_text. - Prompt and parameter provenance is frozen on
Jobat submission time. - Provider output evidence is stored on
JobSourcefor each page execution. DocumentPersonlinks are unique for(document_id, person_id, role_id).- Configured exclusive role pairs cannot coexist for the same
(document_id, person_id). - Relationship mutations are deterministic and set-based.
DocumentType.codeis stable;DocumentType.labelmay evolve.
Data Model Summary
Documenthas oneDocumentType, manySourcepages, manyJobruns, and manyPersonrecords throughDocumentPerson.Sourcebelongs to oneDocumentand may participate in manyJobSourceexecutions.Jobhas manyJobSourcerows.PersonRoledefines available relationship roles.RoleExclusivitydefines role pairs that cannot coexist for the same document-person pair.
Test Strategy
- Unit tests for models, validation, hashing, and registry resolution.
- Service tests for CRUD, set-based sync, and exclusivity enforcement.
- Async workflow tests for page isolation, partial failure handling, and stored evidence.
- UI integration tests for multi-page rendering, role grouping, and document type selection.